Browse Source

[LIGHTS] Moved lights sorting for forward pass into LightBuffer, cleaned Light System.

clementlandrin 4 years ago
parent
commit
74e73eb879
3 changed files with 31 additions and 42 deletions
  1. 0 24
      h3d/scene/LightSystem.hx
  2. 31 4
      h3d/scene/pbr/LightBuffer.hx
  3. 0 14
      h3d/scene/pbr/LightSystem.hx

+ 0 - 24
h3d/scene/LightSystem.hx

@@ -13,32 +13,8 @@ class LightSystem {
 	public function initGlobals( globals : hxsl.Globals ) {
 	}
 
-	function sortingCriteria ( l1 : Light, l2 : Light ) {
-		var d1 = l1.getAbsPos().getPosition().sub(ctx.camera.target).length();
-		var d2 = l2.getAbsPos().getPosition().sub(ctx.camera.target).length();
-		return d1 > d2 ? 1 : -1;
-	}
-
-	public function sortLights ( ctx : h3d.scene.RenderContext ) @:privateAccess {
-		var lights = [];
-		var l = ctx.lights;
-		if ( l == null )
-			return;
-		while ( l != null ) {
-			lights.push(l);
-			l = l.next;
-		}
-		lights.sort(function(l1,l2) { return sortingCriteria(l1, l2); });
-		ctx.lights = lights[0];
-		for (i in 0...lights.length - 1) {
-			lights[i].next = lights[i + 1];
-		}
-		lights[lights.length-1].next = null;
-	}
-
 	public function initLights( ctx : h3d.scene.RenderContext ) @:privateAccess {
 		this.ctx = ctx;
-		sortLights(ctx);
 		if( shadowLight == null || !shadowLight.allocated) {
 			var l = ctx.lights;
 			while( l != null ) {

+ 31 - 4
h3d/scene/pbr/LightBuffer.hx

@@ -76,6 +76,35 @@ class LightBuffer {
 		b[i+3] = v.a;
 	}
 
+	function sortingCriteria ( l1 : Light, l2 : Light, cameraTarget : Vector ) {
+		var pbr1 = Std.downcast(l1, h3d.scene.pbr.Light);
+		var pbr2 = Std.downcast(l2, h3d.scene.pbr.Light);
+		var last1 = pbr1 != null && !pbr1.enableForward;
+		var last2 = pbr2 != null && !pbr2.enableForward;
+		if (last1 && !last2) {
+			return 1;
+		}
+		if (last2 && !last1) {
+			return -1;
+		}
+		var d1 = l1.getAbsPos().getPosition().sub(cameraTarget).length();
+		var d2 = l2.getAbsPos().getPosition().sub(cameraTarget).length();
+		return d1 > d2 ? 1 : -1;
+	}
+
+	public function sortLights ( ctx : h3d.scene.RenderContext ) : Array<Light> @:privateAccess {
+		var lights = [];
+		var l = Std.downcast(ctx.lights, Light);
+		if ( l == null )
+			return null;
+		while ( l != null ) {
+			lights.push(l);
+			l = Std.downcast(l.next, Light);
+		}
+		lights.sort(function(l1,l2) { return sortingCriteria(l1, l2, @:privateAccess ctx.camera.target); });
+		return lights;
+	}
+
 	public function sync( ctx : h3d.scene.RenderContext ) {
 		if (defaultForwardShader.lightInfos.isDisposed())
 			createBuffers();
@@ -98,8 +127,8 @@ class LightBuffer {
 		for( i in 0 ... lightInfos.length )
 			lightInfos[i] = 0;
 
-		var l = @:privateAccess ctx.lights;
-		while( l != null ) {
+		var lights = sortLights(ctx);
+		for ( l in lights ) {
 
 			// Dir Light
 			var dl = Std.downcast(l, DirLight);
@@ -167,8 +196,6 @@ class LightBuffer {
 				}*/
 				s.spotLightCount++;
 			}
-
-			l = l.next;
 		}
 		s.lightInfos.uploadVector(lightInfos, 0, s.lightInfos.vertices, 0);
 

+ 0 - 14
h3d/scene/pbr/LightSystem.hx

@@ -34,20 +34,6 @@ class LightSystem extends h3d.scene.LightSystem {
 		return shaders;
 	}
 
-	override function sortingCriteria ( l1, l2 ) {
-		var pbr1 = Std.downcast(l1, h3d.scene.pbr.Light);
-		var pbr2 = Std.downcast(l2, h3d.scene.pbr.Light);
-		var last1 = pbr1 != null && !pbr1.enableForward;
-		var last2 = pbr2 != null && !pbr2.enableForward;
-		if (last1 && !last2) {
-			return 1;
-		}
-		if (last2 && !last1) {
-			return -1;
-		}
-		return super.sortingCriteria(l1, l2);
-	}
-
 	override function initLights( ctx : h3d.scene.RenderContext ) @:privateAccess {
 		super.initLights(ctx);
 		lightBuffer.sync(ctx);