Browse Source

-Some more work on 2D Lights (NOT FUNCTIONAL YET!)

reduz 10 years ago
parent
commit
2bea642583

+ 182 - 0
scene/2d/light_2d.cpp

@@ -0,0 +1,182 @@
+#include "light_2d.h"
+#include "servers/visual_server.h"
+
+void Light2D::set_enabled( bool p_enabled) {
+
+	VS::get_singleton()->canvas_light_set_enabled(canvas_light,p_enabled);
+	enabled=p_enabled;
+}
+
+bool Light2D::is_enabled() const {
+
+	return enabled;
+}
+
+void Light2D::set_texture( const Ref<Texture>& p_texture) {
+
+	texture=p_texture;
+	if (texture.is_valid())
+		VS::get_singleton()->canvas_light_set_texture(canvas_light,texture->get_rid());
+	else
+		VS::get_singleton()->canvas_light_set_texture(canvas_light,RID());
+}
+
+Ref<Texture> Light2D::get_texture() const {
+
+	return texture;
+}
+
+void Light2D::set_texture_offset( const Vector2& p_offset) {
+
+	texture_offset=p_offset;
+	VS::get_singleton()->canvas_light_set_texture_offset(canvas_light,texture_offset);
+}
+
+Vector2 Light2D::get_texture_offset() const {
+
+	return texture_offset;
+}
+
+void Light2D::set_color( const Color& p_color) {
+
+	color=p_color;
+	VS::get_singleton()->canvas_light_set_color(canvas_light,color);
+
+}
+Color Light2D::get_color() const {
+
+	return color;
+}
+
+void Light2D::set_height( float p_height) {
+
+	height=p_height;
+	VS::get_singleton()->canvas_light_set_height(canvas_light,height);
+
+}
+float Light2D::get_height() const {
+
+	return height;
+}
+
+void Light2D::set_z_range_min( int p_min_z) {
+
+	z_min=p_min_z;
+	VS::get_singleton()->canvas_light_set_z_range(canvas_light,z_min,z_max);
+
+}
+int Light2D::get_z_range_min() const {
+
+	return z_min;
+}
+
+void Light2D::set_z_range_max( int p_max_z) {
+
+	z_max=p_max_z;
+	VS::get_singleton()->canvas_light_set_z_range(canvas_light,z_min,z_max);
+
+}
+int Light2D::get_z_range_max() const {
+
+	return z_max;
+}
+
+void Light2D::set_item_mask( int p_mask) {
+
+	item_mask=p_mask;
+	VS::get_singleton()->canvas_light_set_item_mask(canvas_light,item_mask);
+
+}
+
+int Light2D::get_item_mask() const {
+
+	return item_mask;
+}
+
+void Light2D::set_blend_mode( LightBlendMode p_blend_mode ) {
+
+	blend_mode=p_blend_mode;
+	VS::get_singleton()->canvas_light_set_blend_mode(canvas_light,VS::CanvasLightBlendMode(blend_mode));
+}
+
+Light2D::LightBlendMode Light2D::get_blend_mode() const {
+
+	return blend_mode;
+}
+
+void Light2D::set_shadow_enabled( bool p_enabled) {
+
+	shadow=p_enabled;
+	VS::get_singleton()->canvas_light_set_shadow_enabled(canvas_light,shadow);
+
+}
+bool Light2D::is_shadow_enabled() const {
+
+	return shadow;
+}
+
+void Light2D::_bind_methods() {
+
+
+	ObjectTypeDB::bind_method(_MD("set_enabled","enabled"),&Light2D::set_enabled);
+	ObjectTypeDB::bind_method(_MD("is_enabled"),&Light2D::is_enabled);
+
+	ObjectTypeDB::bind_method(_MD("set_texture","texture"),&Light2D::set_texture);
+	ObjectTypeDB::bind_method(_MD("get_texture"),&Light2D::get_texture);
+
+	ObjectTypeDB::bind_method(_MD("set_texture_offset","texture_offset"),&Light2D::set_texture_offset);
+	ObjectTypeDB::bind_method(_MD("get_texture_offset"),&Light2D::get_texture_offset);
+
+	ObjectTypeDB::bind_method(_MD("set_color","color"),&Light2D::set_color);
+	ObjectTypeDB::bind_method(_MD("get_color"),&Light2D::get_color);
+
+	ObjectTypeDB::bind_method(_MD("set_height","height"),&Light2D::set_height);
+	ObjectTypeDB::bind_method(_MD("get_height"),&Light2D::get_height);
+
+	ObjectTypeDB::bind_method(_MD("set_z_range_min","z"),&Light2D::set_z_range_min);
+	ObjectTypeDB::bind_method(_MD("get_z_range_min"),&Light2D::get_z_range_min);
+
+	ObjectTypeDB::bind_method(_MD("set_z_range_max","z"),&Light2D::set_z_range_max);
+	ObjectTypeDB::bind_method(_MD("get_z_range_max"),&Light2D::get_z_range_max);
+
+	ObjectTypeDB::bind_method(_MD("set_item_mask","item_mask"),&Light2D::set_item_mask);
+	ObjectTypeDB::bind_method(_MD("get_item_mask"),&Light2D::get_item_mask);
+
+	ObjectTypeDB::bind_method(_MD("set_blend_mode","blend_mode"),&Light2D::set_blend_mode);
+	ObjectTypeDB::bind_method(_MD("get_blend_mode"),&Light2D::get_blend_mode);
+
+	ObjectTypeDB::bind_method(_MD("set_shadow_enabled","enabled"),&Light2D::set_shadow_enabled);
+	ObjectTypeDB::bind_method(_MD("is_shadow_enabled"),&Light2D::is_shadow_enabled);
+
+	ADD_PROPERTY( PropertyInfo(Variant::BOOL,"enabled"),_SCS("set_enabled"),_SCS("is_enabled"));
+	ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"texture",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_texture"),_SCS("get_texture"));
+	ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"texture_offset"),_SCS("set_texture_offset"),_SCS("get_texture_offset"));
+	ADD_PROPERTY( PropertyInfo(Variant::COLOR,"color"),_SCS("set_color"),_SCS("get_color"));
+	ADD_PROPERTY( PropertyInfo(Variant::REAL,"height"),_SCS("set_height"),_SCS("get_height"));
+	ADD_PROPERTY( PropertyInfo(Variant::INT,"z_range_min",PROPERTY_HINT_RANGE,itos(VS::CANVAS_ITEM_Z_MIN)+","+itos(VS::CANVAS_ITEM_Z_MAX)+",1"),_SCS("set_z_range_min"),_SCS("get_z_range_min"));
+	ADD_PROPERTY( PropertyInfo(Variant::INT,"z_range_max",PROPERTY_HINT_RANGE,itos(VS::CANVAS_ITEM_Z_MIN)+","+itos(VS::CANVAS_ITEM_Z_MAX)+",1"),_SCS("set_z_range_max"),_SCS("get_z_range_max"));
+	ADD_PROPERTY( PropertyInfo(Variant::INT,"item_mask",PROPERTY_HINT_ALL_FLAGS),_SCS("set_item_mask"),_SCS("get_item_mask"));
+	ADD_PROPERTY( PropertyInfo(Variant::INT,"blend_mode",PROPERTY_HINT_ENUM,"Add,Sub,Mul,Dodge,Burn,Lighten,Darken,Overlay,Screen"),_SCS("set_blend_mode"),_SCS("get_blend_mode"));
+	ADD_PROPERTY( PropertyInfo(Variant::BOOL,"shadow_enabled"),_SCS("set_shadow_enabled"),_SCS("is_shadow_enabled"));
+
+
+}
+
+Light2D::Light2D() {
+
+	canvas_light=VisualServer::get_singleton()->canvas_light_create();
+	enabled=true;
+	shadow=false;
+	color=Color(1,1,1);
+	height=0;
+	z_min=-1024;
+	z_max=1024;
+	item_mask=1;
+	blend_mode=LIGHT_BLEND_ADD;
+
+}
+
+Light2D::~Light2D() {
+
+	VisualServer::get_singleton()->free(canvas_light);
+}

+ 80 - 0
scene/2d/light_2d.h

@@ -0,0 +1,80 @@
+#ifndef LIGHT_2D_H
+#define LIGHT_2D_H
+
+#include "scene/2d/node_2d.h"
+
+class Light2D : public Node2D {
+
+	OBJ_TYPE(Light2D,Node2D);
+public:
+
+	enum LightBlendMode {
+		LIGHT_BLEND_ADD,
+		LIGHT_BLEND_SUB,
+		LIGHT_BLEND_MULTIPLY,
+		LIGHT_BLEND_DODGE,
+		LIGHT_BLEND_BURN,
+		LIGHT_BLEND_LIGHTEN,
+		LIGHT_BLEND_DARKEN,
+		LIGHT_BLEND_OVERLAY,
+		LIGHT_BLEND_SCREEN,
+	};
+
+private:
+	RID canvas_light;
+	bool enabled;
+	bool shadow;
+	Color color;
+	float height;
+	int z_min;
+	int z_max;
+	int item_mask;
+	LightBlendMode blend_mode;
+	Ref<Texture> texture;
+	Vector2 texture_offset;
+
+protected:
+
+	static void _bind_methods();
+public:
+
+
+	void set_enabled( bool p_enabled);
+	bool is_enabled() const;
+
+	void set_texture( const Ref<Texture>& p_texture);
+	Ref<Texture> get_texture() const;
+
+	void set_texture_offset( const Vector2& p_offset);
+	Vector2 get_texture_offset() const;
+
+	void set_color( const Color& p_color);
+	Color get_color() const;
+
+	void set_height( float p_height);
+	float get_height() const;
+
+	void set_z_range_min( int p_min_z);
+	int get_z_range_min() const;
+
+	void set_z_range_max( int p_max_z);
+	int get_z_range_max() const;
+
+	void set_item_mask( int p_mask);
+	int get_item_mask() const;
+
+	void set_blend_mode( LightBlendMode p_blend_mode );
+	LightBlendMode get_blend_mode() const;
+
+	void set_shadow_enabled( bool p_enabled);
+	bool is_shadow_enabled() const;
+
+
+	Light2D();
+	~Light2D();
+};
+
+
+VARIANT_ENUM_CAST(Light2D::LightBlendMode);
+
+#endif // LIGHT_2D_H

+ 2 - 0
scene/register_scene_types.cpp

@@ -79,6 +79,7 @@
 #include "scene/resources/video_stream.h"
 #include "scene/2d/particles_2d.h"
 #include "scene/2d/path_2d.h"
+#include "scene/2d/light_2d.h"
 
 #include "scene/2d/canvas_item.h"
 #include "scene/2d/sprite.h"
@@ -472,6 +473,7 @@ void register_scene_types() {
 	ObjectTypeDB::register_type<VisibilityNotifier2D>();
 	ObjectTypeDB::register_type<VisibilityEnabler2D>();
 	ObjectTypeDB::register_type<Polygon2D>();
+	ObjectTypeDB::register_type<Light2D>();
 	ObjectTypeDB::register_type<YSort>();
 
 	ObjectTypeDB::set_type_enabled("CollisionShape2D",false);

+ 33 - 0
servers/visual/rasterizer.h

@@ -567,6 +567,39 @@ public:
 		CANVAS_RECT_FLIP_V=8
 	};
 
+
+	struct CanvasLight {
+
+		bool enabled;
+		bool shadow;
+		Color color;
+		Matrix32 xform;
+		float height;
+		int z_min;
+		int z_max;
+		int item_mask;
+		VS::CanvasLightBlendMode blend_mode;
+		RID texture;
+		void *texture_cache; // implementation dependent
+		Vector2 texture_offset;
+
+		CanvasLight *next_ptr;
+
+		CanvasLight() {
+			enabled=true;
+			shadow=false;
+			color=Color(1,1,1);
+			height=0;
+			z_min=-1024;
+			z_max=1024;
+			item_mask=1;
+			blend_mode=VS::CANVAS_LIGHT_BLEND_ADD;
+			texture_cache=NULL;
+			next_ptr=NULL;
+		}
+	};
+
+
 	struct CanvasItem {
 
 		struct Command {

+ 50 - 1
servers/visual/visual_server_raster.cpp

@@ -3823,60 +3823,100 @@ void VisualServerRaster::canvas_item_raise(RID p_item) {
 
 RID VisualServerRaster::canvas_light_create() {
 
-	return RID();
+	Rasterizer::CanvasLight *clight = memnew( Rasterizer::CanvasLight );
+	return canvas_light_owner.make_rid(clight);
 }
 
 void VisualServerRaster::canvas_light_attach_to_canvas(RID p_light,RID p_canvas){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+
 
 }
 void VisualServerRaster::canvas_light_set_enabled(RID p_light, bool p_enabled){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+	clight->enabled=p_enabled;
 
 }
 void VisualServerRaster::canvas_light_set_transform(RID p_light, const Matrix32& p_transform){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+	clight->xform=p_transform;
 
 }
 void VisualServerRaster::canvas_light_set_texture(RID p_light, RID p_texture){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+	clight->texture=p_texture;
 
 }
 void VisualServerRaster::canvas_light_set_texture_offset(RID p_light, const Vector2& p_offset){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+	clight->texture_offset=p_offset;
 
 }
 void VisualServerRaster::canvas_light_set_color(RID p_light, const Color& p_color){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+	clight->color=p_color;
+
 
 }
 void VisualServerRaster::canvas_light_set_height(RID p_light, float p_height){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+	clight->height=p_height;
 
 }
 void VisualServerRaster::canvas_light_set_z_range(RID p_light, int p_min_z,int p_max_z){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+	clight->z_min=p_min_z;
+	clight->z_max=p_max_z;
 
 }
 void VisualServerRaster::canvas_light_set_item_mask(RID p_light, int p_mask){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+	clight->item_mask=p_mask;
 
 }
 
 void VisualServerRaster::canvas_light_set_blend_mode(RID p_light, CanvasLightBlendMode p_blend_mode){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+	clight->blend_mode=p_blend_mode;
 
 }
 void VisualServerRaster::canvas_light_set_shadow_enabled(RID p_light, bool p_enabled){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
+	clight->shadow=p_enabled;
 
 }
 void VisualServerRaster::canvas_light_set_shadow_buffer_size(RID p_light, int p_size){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
 
 }
 void VisualServerRaster::canvas_light_set_shadow_filter(RID p_light, int p_size){
 
+	Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
+	ERR_FAIL_COND(!clight);
 
 }
 
@@ -4186,6 +4226,15 @@ void VisualServerRaster::free( RID p_rid ) {
 		canvas_item_owner.free( p_rid );
 		
 		memdelete( canvas_item );
+
+	} else if (canvas_light_owner.owns(p_rid)) {
+
+		Rasterizer::CanvasLight *canvas_light = canvas_light_owner.get(p_rid);
+		ERR_FAIL_COND(!canvas_light);
+
+		canvas_light_owner.free( p_rid );
+		memdelete( canvas_light );
+
 	} else if (scenario_owner.owns(p_rid)) {
 		
 		Scenario *scenario=scenario_owner.get(p_rid);

+ 2 - 0
servers/visual/visual_server_raster.h

@@ -440,6 +440,8 @@ class VisualServerRaster : public VisualServer {
 	};
 
 
+	RID_Owner<Rasterizer::CanvasLight> canvas_light_owner;
+
 
 	struct Viewport {
 

BIN
tools/editor/icons/icon_light_2d.png