Selaa lähdekoodia

Update Ligths

ShiroSmith 6 vuotta sitten
vanhempi
commit
6a76ea6922

+ 3 - 2
h3d/pass/DirShadowMap.hx

@@ -174,7 +174,7 @@ class DirShadowMap extends Shadows {
 
 
 	override function draw( passes ) {
 	override function draw( passes ) {
 
 
-		if( !ctx.computingStatic )
+		if( !ctx.computingStatic ){
 			switch( mode ) {
 			switch( mode ) {
 			case None, Mixed:
 			case None, Mixed:
 				return passes;
 				return passes;
@@ -190,8 +190,9 @@ class DirShadowMap extends Shadows {
 					return passes;
 					return passes;
 				}
 				}
 			}
 			}
+		}
 
 
-		passes = filterPasses(passes);
+		var passes = filterPasses(passes);
 
 
 		var texture = ctx.textures.allocTarget("dirShadowMap", size, size, false, format);
 		var texture = ctx.textures.allocTarget("dirShadowMap", size, size, false, format);
 		if( customDepth && (depth == null || depth.width != size || depth.height != size || depth.isDisposed()) ) {
 		if( customDepth && (depth == null || depth.width != size || depth.height != size || depth.isDisposed()) ) {

+ 27 - 3
h3d/scene/pbr/LightSystem.hx

@@ -3,17 +3,41 @@ package h3d.scene.pbr;
 @:access(h3d.scene.pbr.Light)
 @:access(h3d.scene.pbr.Light)
 class LightSystem extends h3d.scene.LightSystem {
 class LightSystem extends h3d.scene.LightSystem {
 
 
+	public function init( r : h3d.scene.Renderer ) {
+		this.ctx = @:privateAccess r.ctx;
+	}
+
 	override function computeLight( obj : h3d.scene.Object, shaders : hxsl.ShaderList ) : hxsl.ShaderList {
 	override function computeLight( obj : h3d.scene.Object, shaders : hxsl.ShaderList ) : hxsl.ShaderList {
 		var light = Std.instance(obj, h3d.scene.pbr.Light);
 		var light = Std.instance(obj, h3d.scene.pbr.Light);
 		if( light != null ) {
 		if( light != null ) {
-			if( light.shadows.shader != null )
+			shaders = ctx.allocShaderList(light.shader, shaders);
+			if( light.shadows.shader != null && light.shadows.mode != None ){
+
+				var pointShadow = Std.instance(light.shadows.shader, h3d.shader.PointShadow );
+				if(pointShadow != null && pointShadow.shadowMap == null){
+					throw "Rendering a pbr pointlight with shadows, without the shadowMap";
+					return shaders;
+				}
+
+				var dirShadow = Std.instance(light.shadows.shader, h3d.shader.DirShadow );
+				if(dirShadow != null && dirShadow.shadowMap == null){
+					throw "Rendering a pbr dirLight with shadows, without the shadowMap";
+					return shaders;
+				}
+
+				var spotShadow = Std.instance(light.shadows.shader, h3d.shader.SpotShadow );
+				if(spotShadow != null && spotShadow.shadowMap == null){
+					throw "Rendering a pbr spotLight with shadows, without the shadowMap";
+					return shaders;
+				}
+
 				shaders = ctx.allocShaderList(light.shadows.shader, shaders);
 				shaders = ctx.allocShaderList(light.shadows.shader, shaders);
-			return ctx.allocShaderList(light.shader, shaders);
+			}
 		}
 		}
 		return shaders;
 		return shaders;
 	}
 	}
 
 
-	public function drawLights( r : h3d.scene.Renderer, lightPass : h3d.pass.ScreenFx<Dynamic> ) {
+	public function drawScreenLights( r : h3d.scene.Renderer, lightPass : h3d.pass.ScreenFx<Dynamic> ) {
 		var plight = @:privateAccess ctx.lights;
 		var plight = @:privateAccess ctx.lights;
 		var currentTarget = ctx.engine.getCurrentTarget();
 		var currentTarget = ctx.engine.getCurrentTarget();
 		var width = currentTarget == null ? ctx.engine.width : currentTarget.width;
 		var width = currentTarget == null ? ctx.engine.width : currentTarget.width;

+ 15 - 0
h3d/scene/pbr/PointLight.hx

@@ -53,9 +53,24 @@ class PointLight extends Light {
 	}
 	}
 
 
 	override function emit(ctx:RenderContext) {
 	override function emit(ctx:RenderContext) {
+
+		if( ctx.computingStatic ){
+			super.emit(ctx);
+			return;
+		}
+
 		if( ctx.pbrLightPass == null )
 		if( ctx.pbrLightPass == null )
 			throw "Rendering a pbr light require a PBR compatible scene renderer";
 			throw "Rendering a pbr light require a PBR compatible scene renderer";
 
 
+		var s = new h3d.col.Sphere();
+		s.x = absPos._41;
+		s.y = absPos._42;
+		s.z = absPos._43;
+		s.r = cullingDistance;
+
+		if( !ctx.camera.frustum.hasSphere(s) )
+			return;
+
 		super.emit(ctx);
 		super.emit(ctx);
 		ctx.emitPass(ctx.pbrLightPass, this);
 		ctx.emitPass(ctx.pbrLightPass, this);
 	}
 	}

+ 9 - 6
h3d/scene/pbr/Renderer.hx

@@ -174,9 +174,6 @@ class Renderer extends h3d.scene.Renderer {
 
 
 	override function render() {
 	override function render() {
 		var props : RenderProps = props;
 		var props : RenderProps = props;
-		var ls = getLightSystem();
-
-		drawShadows();
 
 
 		var albedo = allocTarget("albedo", true, 1.);
 		var albedo = allocTarget("albedo", true, 1.);
 		var normal = allocTarget("normal",false,1.,RGBA16F);
 		var normal = allocTarget("normal",false,1.,RGBA16F);
@@ -189,6 +186,10 @@ class Renderer extends h3d.scene.Renderer {
 		ctx.setGlobal("occlusionMap",{ texture : pbr, channel : hxsl.Channel.B });
 		ctx.setGlobal("occlusionMap",{ texture : pbr, channel : hxsl.Channel.B });
 		ctx.setGlobal("bloom",null);
 		ctx.setGlobal("bloom",null);
 
 
+		var ls = Std.instance(getLightSystem(), LightSystem);
+		if( ls != null ) ls.init(this);
+		drawShadows();
+
 		setTargets([albedo,normal,pbr,other]);
 		setTargets([albedo,normal,pbr,other]);
 		clear(0, 1, 0);
 		clear(0, 1, 0);
 		mainDraw();
 		mainDraw();
@@ -267,7 +268,6 @@ class Renderer extends h3d.scene.Renderer {
 
 
 		pbrDirect.doDiscard = true;
 		pbrDirect.doDiscard = true;
 
 
-		var ls = Std.instance(ls, LightSystem);
 		var lpass = screenLightPass;
 		var lpass = screenLightPass;
 		if( lpass == null ) {
 		if( lpass == null ) {
 			lpass = new h3d.pass.ScreenFx(new h3d.shader.ScreenShader());
 			lpass = new h3d.pass.ScreenFx(new h3d.shader.ScreenShader());
@@ -276,10 +276,13 @@ class Renderer extends h3d.scene.Renderer {
 			lpass.pass.setBlendMode(Add);
 			lpass.pass.setBlendMode(Add);
 			screenLightPass = lpass;
 			screenLightPass = lpass;
 		}
 		}
-		if( ls != null ) ls.drawLights(this, lpass);
 
 
+		// Draw DirLight, screenShader
+		pbrProps.isScreen = true;
+		if( ls != null )  ls.drawScreenLights(this, lpass);
+		// Draw others lights with their primitive
 		pbrProps.isScreen = false;
 		pbrProps.isScreen = false;
-		draw("lights");
+		draw(pbrLightPass.name);
 
 
 		if( renderMode == LightProbe ) {
 		if( renderMode == LightProbe ) {
 			pbrProps.isScreen = true;
 			pbrProps.isScreen = true;