Quellcode durchsuchen

light shadows fixes

ncannasse vor 7 Jahren
Ursprung
Commit
e0f5168275
5 geänderte Dateien mit 39 neuen und 24 gelöschten Zeilen
  1. 1 1
      h3d/pass/DefaultShadowMap.hx
  2. 22 19
      h3d/pass/DirShadowMap.hx
  3. 14 2
      h3d/pass/Shadows.hx
  4. 1 1
      h3d/scene/pbr/DirLight.hx
  5. 1 1
      h3d/scene/pbr/Light.hx

+ 1 - 1
h3d/pass/DefaultShadowMap.hx

@@ -11,7 +11,7 @@ class DefaultShadowMap extends DirShadowMap {
 	public var color : h3d.Vector;
 
 	public function new(size=1024) {
-		super();
+		super(null);
 		this.size = size;
 		color = new h3d.Vector();
 		mode = Dynamic;

+ 22 - 19
h3d/pass/DirShadowMap.hx

@@ -7,14 +7,10 @@ class DirShadowMap extends Shadows {
 	var depth : h3d.mat.DepthBuffer;
 	var dshader : h3d.shader.DirShadow;
 	var border : Border;
-	var staticTexture : h3d.mat.Texture;
 	var mergePass = new h3d.pass.ScreenFx(new h3d.shader.MinMaxShader());
-	public var power = 30.0;
-	public var bias = 0.01;
-
-	public function new() {
-		super();
 
+	public function new( light : h3d.scene.Light ) {
+		super(light);
 		lightCamera = new h3d.Camera();
 		lightCamera.orthoBounds = new h3d.col.Bounds();
 		shader = dshader = new h3d.shader.DirShadow();
@@ -33,13 +29,16 @@ class DirShadowMap extends Shadows {
 			border.dispose();
 			border = new Border(s, s);
 		}
-		return size = s;
+		return super.set_size(s);
 	}
 
 	override function dispose() {
 		super.dispose();
 		if( customDepth && depth != null ) depth.dispose();
-		if( staticTexture != null ) staticTexture.dispose();
+	}
+
+	function getShadowProj() {
+		return lightCamera.m;
 	}
 
 	public dynamic function calcShadowBounds( camera : h3d.Camera ) {
@@ -107,11 +106,15 @@ class DirShadowMap extends Shadows {
 			calcShadowBounds(lightCamera);
 			lightCamera.update();
 		}
-		cameraViewProj = lightCamera.m;
+		cameraViewProj = getShadowProj();
 	}
 
-	public function getShadowProj() {
-		return lightCamera.m;
+	function syncShader(texture) {
+		dshader.shadowMap = texture;
+		dshader.shadowMapChannel = format == h3d.mat.Texture.nativeFormat ? PackedFloat : R;
+		dshader.shadowBias = bias;
+		dshader.shadowPower = power;
+		dshader.shadowProj = getShadowProj();
 	}
 
 	override function draw( passes ) {
@@ -123,8 +126,12 @@ class DirShadowMap extends Shadows {
 			case Dynamic:
 				// nothing
 			case Static, Mixed:
-				if( staticTexture == null ) throw "Static texture is missing, call s3d.computeStatic() first";
-				if( mode == Static ) return passes;
+				if( staticTexture == null || staticTexture.isDisposed() )
+					staticTexture = h3d.mat.Texture.fromColor(0xFFFFFF);
+				if( mode == Static ) {
+					syncShader(staticTexture);
+					return passes;
+				}
 			}
 
 		passes = filterPasses(passes);
@@ -138,7 +145,7 @@ class DirShadowMap extends Shadows {
 
 		if( mode != Mixed || ctx.computingStatic ) {
 			var ct = ctx.camera.target;
-			var slight = ctx.lightSystem.shadowLight;
+			var slight = light == null ? ctx.lightSystem.shadowLight : light;
 			var ldir = slight == null ? null : @:privateAccess slight.getShadowDirection();
 			if( ldir == null )
 				lightCamera.target.set(0, 0, -1);
@@ -172,11 +179,7 @@ class DirShadowMap extends Shadows {
 		if( blur.radius > 0 && (mode != Mixed || !ctx.computingStatic) )
 			blur.apply(ctx, texture);
 
-		dshader.shadowMap = texture;
-		dshader.shadowMapChannel = format == h3d.mat.Texture.nativeFormat ? PackedFloat : R;
-		dshader.shadowBias = bias;
-		dshader.shadowPower = power;
-		dshader.shadowProj = getShadowProj();
+		syncShader(texture);
 		return passes;
 	}
 

+ 14 - 2
h3d/pass/Shadows.hx

@@ -10,32 +10,44 @@ enum RenderMode {
 class Shadows extends Default {
 
 	var format : hxd.PixelFormat;
+	var staticTexture : h3d.mat.Texture;
+	var light : h3d.scene.Light;
 	public var mode(default,set) : RenderMode = None;
 	public var size(default,set) : Int = 1024;
 	public var shader(default,null) : hxsl.Shader;
 	public var blur : Blur;
 
-	public function new() {
+	public var power = 30.0;
+	public var bias = 0.01;
+
+	public function new(light) {
 		if( format == null ) format = R16F;
 		if( !h3d.Engine.getCurrent().driver.isSupportedFormat(format) ) format = h3d.mat.Texture.nativeFormat;
 		super("shadows");
+		this.light = light;
 		blur = new Blur(5);
 		blur.quality = 0.5;
 		blur.shader.isDepth = format == h3d.mat.Texture.nativeFormat;
 	}
 
 	function set_mode(m:RenderMode) {
-		if( m != None ) throw "Shadow mode "+m+" not supported for "+this;
+		if( m != None ) throw "Shadow mode "+m+" not supported for "+light;
 		return mode = m;
 	}
 
 	function set_size(s) {
+		if( s != size && staticTexture != null ) {
+			staticTexture.dispose();
+			staticTexture = null;
+		}
 		return size = s;
 	}
 
 	override function dispose() {
 		super.dispose();
 		blur.dispose();
+		// don't set to null
+		if( staticTexture != null ) staticTexture.dispose();
 	}
 
 	override function getOutputs() : Array<hxsl.Output> {

+ 1 - 1
h3d/scene/pbr/DirLight.hx

@@ -6,7 +6,7 @@ class DirLight extends Light {
 
 	public function new(?dir: h3d.Vector, ?parent) {
 		pbr = new h3d.shader.pbr.Light.DirLight();
-		shadows = new h3d.pass.DirShadowMap();
+		shadows = new h3d.pass.DirShadowMap(this);
 		super(pbr,parent);
 		if( dir != null ) setDirection(dir);
 	}

+ 1 - 1
h3d/scene/pbr/Light.hx

@@ -17,7 +17,7 @@ class Light extends h3d.scene.Light {
 	function new(shader,?parent) {
 		super(shader,parent);
 		_color = new h3d.Vector(1,1,1,1);
-		if( shadows == null ) shadows = new h3d.pass.Shadows();
+		if( shadows == null ) shadows = new h3d.pass.Shadows(this);
 	}
 
 	override function get_color() {