Преглед на файлове

refactor lightsystem and base light to move fwd specific parameters

Nicolas Cannasse преди 4 години
родител
ревизия
2591171739
променени са 7 файла, в които са добавени 65 реда и са изтрити 82 реда
  1. 0 30
      h3d/scene/Light.hx
  2. 0 31
      h3d/scene/LightSystem.hx
  3. 19 0
      h3d/scene/fwd/Light.hx
  4. 41 7
      h3d/scene/fwd/LightSystem.hx
  5. 0 9
      h3d/scene/pbr/Light.hx
  6. 3 3
      h3d/scene/pbr/PointLight.hx
  7. 2 2
      h3d/scene/pbr/SpotLight.hx

+ 0 - 30
h3d/scene/Light.hx

@@ -3,13 +3,9 @@ package h3d.scene;
 class Light extends Object {
 
 	var shader : hxsl.Shader;
-	var objectDistance : Float; // used internaly
 	@:noCompletion public var next : Light; // used internaly (public to allow sorting)
 
-	@:s var cullingDistance : Float = -1;
-	@:s public var priority : Int = 0;
 	public var color(get, set) : h3d.Vector;
-	public var enableSpecular(get, set) : Bool;
 
 	function new(shader,?parent) {
 		super(parent);
@@ -25,15 +21,6 @@ class Light extends Object {
 		return v;
 	}
 
-	function get_enableSpecular() {
-		return false;
-	}
-
-	function set_enableSpecular(b) {
-		if( b ) throw "Not implemented for this light";
-		return false;
-	}
-
 	override function emit(ctx:RenderContext) {
 		ctx.emitLight(this);
 	}
@@ -42,21 +29,4 @@ class Light extends Object {
 		return null;
 	}
 
-	#if (hxbit && !macro && heaps_enable_serialize)
-	override function customSerialize(ctx:hxbit.Serializer) {
-		super.customSerialize(ctx);
-		ctx.addDouble(color.x);
-		ctx.addDouble(color.y);
-		ctx.addDouble(color.z);
-		ctx.addDouble(color.w);
-		ctx.addBool(enableSpecular);
-	}
-	override function customUnserialize(ctx:hxbit.Serializer) {
-		super.customUnserialize(ctx);
-		color.set(ctx.getDouble(), ctx.getDouble(), ctx.getDouble(), ctx.getDouble());
-		enableSpecular = ctx.getBool();
-	}
-	#end
-
-
 }

+ 0 - 31
h3d/scene/LightSystem.hx

@@ -3,49 +3,18 @@ package h3d.scene;
 class LightSystem {
 
 	public var drawPasses : Int = 0;
-	public var ambientLight(default,null) : h3d.Vector;
 	public var shadowLight : h3d.scene.Light;
 
-	var lightCount : Int;
 	var ctx : RenderContext;
 
 	public function new() {
-		ambientLight = new h3d.Vector(1,1,1);
 	}
 
 	public function initGlobals( globals : hxsl.Globals ) {
 	}
 
-	function cullLights() @:privateAccess  {
-		// remove lights which cullingDistance is outside of frustum
-		var l = ctx.lights, prev : h3d.scene.Light = null;
-		var s = new h3d.col.Sphere();
-		while( l != null ) {
-			s.x = l.absPos._41;
-			s.y = l.absPos._42;
-			s.z = l.absPos._43;
-			s.r = l.cullingDistance;
-
-			if( l.cullingDistance > 0 && !ctx.computingStatic && !ctx.camera.frustum.hasSphere(s) ) {
-				if( prev == null )
-					ctx.lights = l.next;
-				else
-					prev.next = l.next;
-				l = l.next;
-				continue;
-			}
-
-			lightCount++;
-			l.objectDistance = 0.;
-			prev = l;
-			l = l.next;
-		}
-	}
-
 	public function initLights( ctx : h3d.scene.RenderContext ) @:privateAccess {
-		lightCount = 0;
 		this.ctx = ctx;
-		cullLights();
 		if( shadowLight == null || !shadowLight.allocated) {
 			var l = ctx.lights;
 			while( l != null ) {

+ 19 - 0
h3d/scene/fwd/Light.hx

@@ -0,0 +1,19 @@
+package h3d.scene.fwd;
+
+class Light extends h3d.scene.Light {
+
+	var objectDistance : Float; // used internaly
+	var cullingDistance : Float = -1;
+	public var priority : Int = 0;
+	public var enableSpecular(get, set) : Bool;
+
+	function get_enableSpecular() {
+		return false;
+	}
+
+	function set_enableSpecular(b) {
+		if( b ) throw "Not implemented for this light";
+		return false;
+	}
+
+}

+ 41 - 7
h3d/scene/fwd/LightSystem.hx

@@ -5,7 +5,9 @@ class LightSystem extends h3d.scene.LightSystem {
 	public var maxLightsPerObject = 6;
 	var globals : hxsl.Globals;
 	var ambientShader : hxsl.Shader;
+	var lightCount : Int;
 	public var perPixelLighting : Bool = true;
+	public var ambientLight(default,null) : h3d.Vector;
 
 	/**
 		In the additive lighting model (by default), the lights are added after the ambient.
@@ -15,7 +17,7 @@ class LightSystem extends h3d.scene.LightSystem {
 
 	public function new() {
 		super();
-		ambientLight.set(0.5, 0.5, 0.5);
+		ambientLight = new h3d.Vector(0.5, 0.5, 0.5);
 		ambientShader = new h3d.shader.AmbientLight();
 		additiveLighting = true;
 	}
@@ -29,9 +31,12 @@ class LightSystem extends h3d.scene.LightSystem {
 	}
 
 	override function initLights(ctx) {
+		lightCount = 0;
+		this.ctx = ctx;
+		cullLights();
 		super.initLights(ctx);
 		if( lightCount <= maxLightsPerObject )
-			@:privateAccess ctx.lights = haxe.ds.ListSort.sortSingleLinked(ctx.lights, sortLight);
+			@:privateAccess ctx.lights = haxe.ds.ListSort.sortSingleLinked(ctx.lights, cast sortLight);
 	}
 
 	override function initGlobals( globals : hxsl.Globals ) {
@@ -39,7 +44,35 @@ class LightSystem extends h3d.scene.LightSystem {
 		globals.set("global.perPixelLighting", perPixelLighting);
 	}
 
-	function sortLight( l1 : h3d.scene.Light, l2 : h3d.scene.Light ) {
+	function cullLights() @:privateAccess  {
+		// remove lights which cullingDistance is outside of frustum
+		var ll = ctx.lights, prev : h3d.scene.Light = null;
+		var s = new h3d.col.Sphere();
+		while( ll != null ) {
+			var l = Std.downcast(ll, h3d.scene.fwd.Light);
+			if( l != null ) {
+				s.x = l.absPos._41;
+				s.y = l.absPos._42;
+				s.z = l.absPos._43;
+				s.r = l.cullingDistance;
+			}
+			if( l == null || (l.cullingDistance > 0 && !ctx.computingStatic && !ctx.camera.frustum.hasSphere(s)) ) {
+				if( prev == null )
+					ctx.lights = ll.next;
+				else
+					prev.next = ll.next;
+				ll = ll.next;
+				continue;
+			}
+
+			lightCount++;
+			l.objectDistance = 0.;
+			prev = ll;
+			ll = ll.next;
+		}
+	}
+
+	function sortLight( l1 : h3d.scene.fwd.Light, l2 : h3d.scene.fwd.Light ) {
 		var p = l1.priority - l2.priority;
 		if( p != 0 ) return -p;
 		return @:privateAccess (l1.objectDistance < l2.objectDistance ? -1 : 1);
@@ -47,15 +80,16 @@ class LightSystem extends h3d.scene.LightSystem {
 
 	override function computeLight( obj : h3d.scene.Object, shaders : hxsl.ShaderList ) : hxsl.ShaderList @:privateAccess {
 		if( lightCount > maxLightsPerObject ) {
-			var l = ctx.lights;
-			while( l != null ) {
+			var ll = ctx.lights;
+			while( ll != null ) {
+				var l = Std.downcast(ll, h3d.scene.fwd.Light);
 				if( obj.lightCameraCenter )
 					l.objectDistance = hxd.Math.distanceSq(l.absPos._41 - ctx.camera.target.x, l.absPos._42 - ctx.camera.target.y, l.absPos._43 - ctx.camera.target.z);
 				else
 					l.objectDistance = hxd.Math.distanceSq(l.absPos._41 - obj.absPos._41, l.absPos._42 - obj.absPos._42, l.absPos._43 - obj.absPos._43);
-				l = l.next;
+				ll = ll.next;
 			}
-			ctx.lights = haxe.ds.ListSort.sortSingleLinked(ctx.lights, sortLight);
+			ctx.lights = haxe.ds.ListSort.sortSingleLinked(ctx.lights, cast sortLight);
 		}
 		inline function add( s : hxsl.Shader ) {
 			shaders = ctx.allocShaderList(s, shaders);

+ 0 - 9
h3d/scene/pbr/Light.hx

@@ -40,13 +40,4 @@ class Light extends h3d.scene.Light {
 		return _color = v;
 	}
 
-	override function get_enableSpecular() {
-		return true;
-	}
-
-	override function set_enableSpecular(b) {
-		if( !b ) throw "Not implemented for this light";
-		return true;
-	}
-
 }

+ 3 - 3
h3d/scene/pbr/PointLight.hx

@@ -27,12 +27,12 @@ class PointLight extends Light {
 	}
 
 	function get_range() {
-		return cullingDistance;
+		return scaleX;
 	}
 
 	function set_range(v:Float) {
 		setScale(v);
-		return cullingDistance = v;
+		return v;
 	}
 
 	override function draw(ctx:RenderContext) {
@@ -66,7 +66,7 @@ class PointLight extends Light {
 		s.x = absPos._41;
 		s.y = absPos._42;
 		s.z = absPos._43;
-		s.r = cullingDistance;
+		s.r = range;
 
 		if( !ctx.camera.frustum.hasSphere(s) )
 			return;

+ 2 - 2
h3d/scene/pbr/SpotLight.hx

@@ -33,13 +33,13 @@ class SpotLight extends Light {
 	}
 
 	function get_range() {
-		return cullingDistance;
+		return scaleX;
 	}
 
 	function set_range(v:Float) {
 		scaleX = v;
 		lightProj.zFar = v;
-		return cullingDistance = v;
+		return v;
 	}
 
 	function set_angle(v:Float) {