فهرست منبع

init lights only once before render, lightsystem detects shadow direction

ncannasse 10 سال پیش
والد
کامیت
fc88eea0c9
4فایلهای تغییر یافته به همراه25 افزوده شده و 8 حذف شده
  1. 1 1
      h3d/pass/Default.hx
  2. 16 3
      h3d/pass/LightSystem.hx
  3. 7 4
      h3d/pass/ShadowMap.hx
  4. 1 0
      h3d/scene/Scene.hx

+ 1 - 1
h3d/pass/Default.hx

@@ -73,7 +73,7 @@ class Default extends Base {
 			shaders = processShaders(p, shaders);
 			if( p.pass.enableLights && ctx.lightSystem != null ) {
 				if( !lightInit ) {
-					ctx.lightSystem.initLights(globals, ctx);
+					ctx.lightSystem.initGlobals(globals);
 					lightInit = true;
 				}
 				shaders = ctx.lightSystem.computeLight(p.obj, shaders);

+ 16 - 3
h3d/pass/LightSystem.hx

@@ -10,7 +10,7 @@ class LightSystem {
 	var ambientShader : hxsl.Shader;
 	var lightCount : Int;
 	var ctx : h3d.scene.RenderContext;
-	public var shadowDirection : h3d.Vector;
+	public var shadowLight : h3d.scene.DirLight;
 	public var ambientLight : h3d.Vector;
 	public var perPixelLighting : Bool = true;
 
@@ -21,7 +21,6 @@ class LightSystem {
 	public var additiveLighting(get, set) : Bool;
 
 	public function new() {
-		shadowDirection = new h3d.Vector(0, 0, -1);
 		ambientLight = new h3d.Vector(0.5, 0.5, 0.5);
 		ambientShader = new h3d.shader.AmbientLight();
 		additiveLighting = true;
@@ -35,7 +34,7 @@ class LightSystem {
 		return Std.instance(ambientShader,h3d.shader.AmbientLight).additive = b;
 	}
 
-	public function initLights( globals : hxsl.Globals, ctx : h3d.scene.RenderContext ) {
+	public function initLights( ctx : h3d.scene.RenderContext ) {
 		lightCount = 0;
 		this.ctx = ctx;
 		var l = ctx.lights, prev = null;
@@ -63,6 +62,20 @@ class LightSystem {
 		}
 		if( lightCount <= maxLightsPerObject )
 			ctx.lights = haxe.ds.ListSort.sortSingleLinked(ctx.lights, sortLight);
+		if( shadowLight == null || shadowLight.parent == null ) {
+			var l = ctx.lights;
+			while( l != null ) {
+				var dl = Std.instance(l, h3d.scene.DirLight);
+				if( dl != null ) {
+					shadowLight = dl;
+					break;
+				}
+				l = l.next;
+			}
+		}
+	}
+
+	public function initGlobals( globals : hxsl.Globals ) {
 		globals.set("global.ambientLight", ambientLight);
 		globals.set("global.perPixelLighting", perPixelLighting);
 	}

+ 7 - 4
h3d/pass/ShadowMap.hx

@@ -10,7 +10,6 @@ class ShadowMap extends Default {
 	var shadowBiasId : Int;
 	public var border : Border;
 	public var size(default,set) : Int;
-	public var lightDirection : h3d.Vector;
 	public var color : h3d.Vector;
 	public var power = 10.0;
 	public var bias = 0.01;
@@ -20,7 +19,6 @@ class ShadowMap extends Default {
 		super();
 		this.size = size;
 		priority = 9;
-		lightDirection = new h3d.Vector(0, 0, -1);
 		lightCamera = new h3d.Camera();
 		lightCamera.orthoBounds = new h3d.col.Bounds();
 		shadowMapId = hxsl.Globals.allocID("shadow.map");
@@ -72,8 +70,13 @@ class ShadowMap extends Default {
 	override function draw( passes ) {
 		var texture = tcache.allocTarget("shadowMap", ctx, size, size);
 		var ct = ctx.camera.target;
-		lightCamera.target.set(lightDirection.x, lightDirection.y, lightDirection.z);
-		lightCamera.target.normalize();
+		var slight = ctx.lightSystem.shadowLight;
+		if( slight == null )
+			lightCamera.target.set(0, 0, -1);
+		else {
+			lightCamera.target.set(slight.direction.x, slight.direction.y, slight.direction.z);
+			lightCamera.target.normalize();
+		}
 		lightCamera.target.x += ct.x;
 		lightCamera.target.y += ct.y;
 		lightCamera.target.z += ct.z;

+ 1 - 0
h3d/scene/Scene.hx

@@ -85,6 +85,7 @@ class Scene extends Object implements h3d.IDrawable {
 
 		// send to rendered
 		ctx.lightSystem = lightSystem;
+		lightSystem.initLights(ctx);
 		renderer.process(ctx, passes);
 
 		// check that passes have been rendered