浏览代码

Adding needStaticUpdate on shadow maps.

clementlandrin 9 月之前
父节点
当前提交
8e23d8133a
共有 5 个文件被更改,包括 57 次插入32 次删除
  1. 2 0
      h3d/pass/CascadeShadowMap.hx
  2. 5 2
      h3d/pass/CubeShadowMap.hx
  3. 9 3
      h3d/pass/DirShadowMap.hx
  4. 35 25
      h3d/pass/Shadows.hx
  5. 6 2
      h3d/pass/SpotShadowMap.hx

+ 2 - 0
h3d/pass/CascadeShadowMap.hx

@@ -57,6 +57,8 @@ class CascadeShadowMap extends DirShadowMap {
 		return cshader.cascadeShadowMaps;
 	}
 
+	override function needStaticUpdate() { }
+
 	function computeNearFar( i : Int, previousFar : Float ) {
 		var max = maxDist < 0.0 ? ctx.camera.zFar : maxDist;
 		var step = max - firstCascadeSize;

+ 5 - 2
h3d/pass/CubeShadowMap.hx

@@ -160,7 +160,9 @@ class CubeShadowMap extends Shadows {
 			return;
 		}
 
-		var texture = ctx.computingStatic ? createStaticTexture() : ctx.textures.allocTarget("pointShadowMap", size, size, false, format, [Cube]);
+		var computingStatic = ctx.computingStatic || updateStatic;  
+
+		var texture = computingStatic ? createStaticTexture() : ctx.textures.allocTarget("pointShadowMap", size, size, false, format, [Cube]);
 		if( depth == null || depth.width != texture.width || depth.height != texture.height || depth.isDisposed() ) {
 			if( depth != null ) depth.dispose();
 			depth = new h3d.mat.Texture(texture.width, texture.height, Depth24Stencil8);
@@ -213,11 +215,12 @@ class CubeShadowMap extends Shadows {
 		if( blur.radius > 0 )
 			blur.apply(ctx, texture);
 
-		if( mode == Mixed && !ctx.computingStatic )
+		if( mode == Mixed && !computingStatic )
 			syncShader(merge(texture));
 		else
 			syncShader(texture);
 
+		updateStatic = false;
 	}
 
 	function merge( dynamicTex : h3d.mat.Texture ) : h3d.mat.Texture{

+ 9 - 3
h3d/pass/DirShadowMap.hx

@@ -273,14 +273,16 @@ class DirShadowMap extends Shadows {
 		}
 		super.draw(passes, sort);
 
-		var doBlur = blur.radius > 0 && (mode != Mixed || !ctx.computingStatic);
+		var computingStatic = ctx.computingStatic || updateStatic;
+
+		var doBlur = blur.radius > 0 && (mode != Mixed || !computingStatic);
 
 		if( border != null && !doBlur )
 			border.render();
 
 		ctx.engine.popTarget();
 
-		if( mode == Mixed && !ctx.computingStatic && staticTexture != null && !staticTexture.isDisposed() ) {
+		if( mode == Mixed && !computingStatic && staticTexture != null && !staticTexture.isDisposed() ) {
 			if ( staticTexture.width != tex.width )
 				throw "Static shadow map doesnt match dynamic shadow map";
 			var merge = ctx.textures.allocTarget("mergedDirShadowMap", size, size, false, format);
@@ -320,7 +322,9 @@ class DirShadowMap extends Shadows {
 		if( !filterPasses(passes) )
 			return;
 
-		if( mode != Mixed || ctx.computingStatic ) {
+		var computingStatic = ctx.computingStatic || updateStatic;
+
+		if( mode != Mixed || computingStatic ) {
 			var ct = ctx.camera.target;
 			var slight = light == null ? ctx.lightSystem.shadowLight : light;
 			var ldir = slight == null ? null : @:privateAccess slight.getShadowDirection();
@@ -360,6 +364,8 @@ class DirShadowMap extends Shadows {
 
 		syncShader(texture);
 
+		updateStatic = false;
+		
 		#if editor
 		drawDebug();
 		#end

+ 35 - 25
h3d/pass/Shadows.hx

@@ -19,6 +19,7 @@ class Shadows extends Output {
 	var format : hxd.PixelFormat;
 	var staticTexture : h3d.mat.Texture;
 	var light : h3d.scene.Light;
+	var updateStatic : Bool = false;
 	public var enabled(default,set) : Bool = true;
 	public var mode(default,set) : RenderMode = None;
 	public var size(default,set) : Int = 1024;
@@ -98,6 +99,17 @@ class Shadows extends Output {
 		throw "Not implemented";
 	}
 
+	/**
+	 * Triggers update of static part of shadows (if any).
+	**/ 
+	public function needStaticUpdate() {
+		switch ( mode ) {
+		case Mixed, Static:
+			updateStatic = true;
+		case None, Dynamic:
+		}
+	}
+
 	function createDefaultShadowMap() {
 		var tex = h3d.mat.Texture.fromColor(0xFFFFFF);
 		tex.name = "defaultShadowMap";
@@ -105,39 +117,38 @@ class Shadows extends Output {
 	}
 
 	function syncEarlyExit() {
-		syncShader(staticTexture == null ? createDefaultShadowMap() : staticTexture);	
+		syncShader(staticTexture == null ? createDefaultShadowMap() : staticTexture);
 	}
 
 	function syncShader( texture : h3d.mat.Texture ) {
 	}
 
 	function filterPasses( passes : h3d.pass.PassList ) {
-		if( !ctx.computingStatic ) {
+		if ( ctx.computingStatic || updateStatic ) {
 			switch( mode ) {
-				case None:
-					return false;
-				case Dynamic:
-					return true;
-				case Mixed:
-					passes.filter(function(p) return p.pass.isStatic == false);
-					return true;
-				case Static:
-					syncEarlyExit();
-					return false;
+			case None:
+				return false;
+			case Dynamic:
+				return false;
+			case Mixed:
+				passes.filter(function(p) return p.pass.isStatic == true);
+				return true;
+			case Static:
+				passes.filter(function(p) return p.pass.isStatic == true);
+				return true;
 			}
-		}
-		else {
+		} else {
 			switch( mode ) {
-				case None:
-					return false;
-				case Dynamic:
-					return false;
-				case Mixed:
-					passes.filter(function(p) return p.pass.isStatic == true);
-					return true;
-				case Static:
-					passes.filter(function(p) return p.pass.isStatic == true);
-					return true;
+			case None:
+				return false;
+			case Dynamic:
+				return true;
+			case Mixed:
+				passes.filter(function(p) return p.pass.isStatic == false);
+				return true;
+			case Static:
+				syncEarlyExit();
+				return false;
 			}
 		}
 	}
@@ -156,5 +167,4 @@ class Shadows extends Output {
 			return prevResult;
 		});
 	}
-
 }

+ 6 - 2
h3d/pass/SpotShadowMap.hx

@@ -120,7 +120,9 @@ class SpotShadowMap extends Shadows {
 		@:privateAccess ctx.cameraFar = lightCamera.zFar;
 		@:privateAccess ctx.cameraPos = lightCamera.pos;
 
-		var texture = ctx.computingStatic ? createStaticTexture() : ctx.textures.allocTarget("spotShadowMap", size, size, false, format);
+		var computingStatic = ctx.computingStatic || updateStatic;
+
+		var texture = computingStatic ? createStaticTexture() : ctx.textures.allocTarget("spotShadowMap", size, size, false, format);
 		if( depth == null || depth.width != texture.width || depth.height != texture.height || depth.isDisposed() ) {
 			if( depth != null ) depth.dispose();
 			depth = new h3d.mat.Texture(texture.width, texture.height, Depth24Stencil8);
@@ -135,7 +137,7 @@ class SpotShadowMap extends Shadows {
 		if( blur.radius > 0 )
 			blur.apply(ctx, texture);
 
-		if( mode == Mixed && !ctx.computingStatic && staticTexture != null && !staticTexture.isDisposed() ) {
+		if( mode == Mixed && !computingStatic && staticTexture != null && !staticTexture.isDisposed() ) {
 			if ( staticTexture.width != texture.width )
 				throw "Static shadow map doesnt match dynamic shadow map";
 			var merge = ctx.textures.allocTarget("mergedSpotShadowMap", size, size, false, format);
@@ -151,6 +153,8 @@ class SpotShadowMap extends Shadows {
 		@:privateAccess ctx.cameraViewProj = prevViewProj;
 
 		syncShader(texture);
+
+		updateStatic = false;
 	}
 
 	function updateCamera(){