Просмотр исходного кода

cleanup emissive handling, use local vars for pbr properties, cleanup pbr decals

Nicolas Cannasse 4 лет назад
Родитель
Сommit
5c417ee3a1

+ 9 - 13
h3d/mat/PbrMaterial.hx

@@ -185,8 +185,8 @@ class PbrMaterial extends Material {
 		mainPass.removeShader(mainPass.getShader(h3d.shader.pbr.StrengthValues));
 		mainPass.removeShader(mainPass.getShader(h3d.shader.pbr.AlphaMultiply));
 		mainPass.removeShader(mainPass.getShader(h3d.shader.Parallax));
-		mainPass.removeShader(mainPass.getShader(h3d.shader.Emissive));
 		mainPass.removeShader(mainPass.getShader(h3d.shader.pbr.GammaCorrect));
+		#if editor
 		// Backward compatibility
 		if( props.depthTest == null ) props.depthTest = Less;
 		if( (props:Dynamic).colorMask == null ) props.colorMask = 15;
@@ -203,6 +203,7 @@ class PbrMaterial extends Material {
 			Reflect.deleteField(props, "stencilPassOp");
 			Reflect.deleteField(props, "stencilCompare");
 		}
+		#end
 	}
 
 	override function refreshProps() {
@@ -217,17 +218,10 @@ class PbrMaterial extends Material {
 			mainPass.setPassName("forward");
 		case BeforeTonemapping:
 			mainPass.setPassName("beforeTonemapping");
-			if( props.emissive > 0 ) {
-				var e = mainPass.getShader(h3d.shader.Emissive);
-				if( e == null ) {
-					e = new h3d.shader.Emissive(props.emissive);
-					e.setPriority(-1);
-					mainPass.addShader(e);
-				}
-			}
 			var gc = mainPass.getShader(h3d.shader.pbr.GammaCorrect);
 			if( gc == null ) {
 				gc = new h3d.shader.pbr.GammaCorrect();
+				gc.useEmissiveHDR = true;
 				gc.setPriority(-1);
 				mainPass.addShader(gc);
 			}
@@ -295,8 +289,10 @@ class PbrMaterial extends Material {
 			def = new h3d.shader.pbr.PropsValues();
 			mainPass.addShader(def);
 		}
-		if( tex != null ) tex.emissive = emit;
-		if( def != null ) def.emissive = emit;
+
+		// we should have either one or other
+		if( tex != null ) tex.emissiveValue = emit;
+		if( def != null ) def.emissiveValue = emit;
 
 		// Parallax
 		var ps = mainPass.getShader(h3d.shader.Parallax);
@@ -389,7 +385,7 @@ class PbrMaterial extends Material {
 		if( t != null ) {
 			if( spec == null ) {
 				spec = new h3d.shader.pbr.PropsTexture();
-				spec.emissive = emit;
+				spec.emissiveValue = emit;
 				mainPass.addShader(spec);
 			}
 			spec.texture = t;
@@ -400,7 +396,7 @@ class PbrMaterial extends Material {
 			// default values (if no texture)
 			if( def == null ) {
 				def = new h3d.shader.pbr.PropsValues();
-				def.emissive = emit;
+				def.emissiveValue = emit;
 				mainPass.addShader(def);
 			}
 		}

+ 0 - 20
h3d/shader/Emissive.hx

@@ -1,20 +0,0 @@
-package h3d.shader;
-
-class Emissive extends hxsl.Shader {
-
-	static var SRC = {
-		var pixelColor : Vec4;
-		@param var emissive : Float;
-
-		function fragment() {
-			pixelColor.rgb += pixelColor.rgb * emissive;
-		}
-
-	};
-
-	public function new( emissive : Float = 0 ) {
-		super();
-		this.emissive = emissive;
-	}
-
-}

+ 3 - 5
h3d/shader/pbr/DefaultForward.hx

@@ -28,9 +28,11 @@ class DefaultForward extends hxsl.Shader {
 		// Direct Lighting
 		@param var cameraPosition : Vec3;
 		@param var emissivePower : Float;
+
+		@private var albedo : Vec3;
+
 		var view : Vec3;
 		var NdV : Float;
-		var albedo : Vec3;
 		var pbrSpecularColor : Vec3;
 		var metalness : Float;
 		var roughness : Float;
@@ -97,10 +99,6 @@ class DefaultForward extends hxsl.Shader {
 		function init() {
 			view = (cameraPosition - transformedPosition).normalize();
 			NdV = transformedNormal.dot(view).max(0.);
-			metalness = output.metalness;
-			roughness = output.roughness;
-			occlusion = output.occlusion;
-			emissive = output.emissive;
 		}
 
 		function evaluateDirLight( index : Int ) : Vec3 {

+ 6 - 0
h3d/shader/pbr/GammaCorrect.hx

@@ -3,8 +3,14 @@ package h3d.shader.pbr;
 class GammaCorrect extends hxsl.Shader {
 	static var SRC = {
 		var pixelColor : Vec4;
+
+		@const var useEmissiveHDR : Bool;
+		var emissive : Float;
+
 		function fragment() {
 			pixelColor.rgb *= pixelColor.rgb;
+			// use emissive value to increase light intensity
+			if( useEmissiveHDR ) pixelColor.rgb *= 1 + emissive;
 		}
 	}
 }

+ 26 - 6
h3d/shader/pbr/PropsTexture.hx

@@ -2,23 +2,43 @@ package h3d.shader.pbr;
 
 class PropsTexture extends hxsl.Shader {
 	static var SRC = {
+
 		@param var texture : Sampler2D;
-		@param var emissive : Float;
+		@param var emissiveValue : Float;
+
 		var output : {
 			metalness : Float,
 			roughness : Float,
 			occlusion : Float,
 			emissive : Float,
 		};
+
+		var metalness : Float;
+		var roughness : Float;
+		var occlusion : Float;
+		var emissive : Float;
+
 		var calculatedUV : Vec2;
+
+		function __init__fragment() {
+			{
+				var v = texture.get(calculatedUV);
+				metalness = v.r;
+				roughness = 1 - v.g * v.g;
+				occlusion = v.b;
+				emissive = emissiveValue * v.a;
+			}
+		}
+
 		function fragment() {
-			var v = texture.get(calculatedUV);
-			output.metalness = v.r;
-			output.roughness = 1 - v.g * v.g;
-			output.occlusion = v.b;
-			output.emissive = emissive * v.a;
+			output.metalness = metalness;
+			output.roughness = roughness;
+			output.occlusion = occlusion;
+			output.emissive = emissive;
 		}
+
 	}
+
 	public function new(?t) {
 		super();
 		this.texture = t;

+ 20 - 20
h3d/shader/pbr/PropsValues.hx

@@ -11,38 +11,38 @@ class PropsValues extends hxsl.Shader {
 			emissive : Float,
 		};
 
-		@param var metalness : Float;
-		@param var roughness : Float;
-		@param var occlusion : Float;
-		@param var emissive : Float;
+		@param var metalnessValue : Float;
+		@param var roughnessValue : Float;
+		@param var occlusionValue : Float;
+		@param var emissiveValue : Float;
 
-		var metalnessValue : Float;
-		var roughnessValue : Float;
-		var occlusionValue : Float;
-		var emissiveValue : Float;
+		var metalness : Float;
+		var roughness : Float;
+		var occlusion : Float;
+		var emissive : Float;
 
 		function __init__() {
-			metalnessValue = metalness;
-			roughnessValue = roughness;
-			occlusionValue = occlusion;
-			emissiveValue = emissive;
+			metalness = metalnessValue;
+			roughness = roughnessValue;
+			occlusion = occlusionValue;
+			emissive = emissiveValue;
 		}
 
 		function fragment() {
-			output.metalness = metalnessValue;
-			output.roughness = roughnessValue;
-			output.occlusion = occlusionValue;
-			output.emissive = emissiveValue;
+			output.metalness = metalness;
+			output.roughness = roughness;
+			output.occlusion = occlusion;
+			output.emissive = emissive;
 		}
 
 	};
 
 	public function new(metalness=0.,roughness=1.,occlusion=1.,emissive=0.) {
 		super();
-		this.metalness = metalness;
-		this.roughness = roughness;
-		this.occlusion = occlusion;
-		this.emissive = emissive;
+		this.metalnessValue = metalness;
+		this.roughnessValue = roughness;
+		this.occlusionValue = occlusion;
+		this.emissiveValue = emissive;
 	}
 
 }

+ 7 - 27
h3d/shader/pbr/VolumeDecal.hx

@@ -91,11 +91,6 @@ class DecalPBR extends hxsl.Shader {
 		};
 
 		var output : {
-			color : Vec4,
-			normal : Vec3,
-			metalness : Float,
-			roughness : Float,
-			occlusion : Float,
 			albedoStrength : Float,
 			normalStrength : Float,
 			pbrStrength : Float,
@@ -104,7 +99,6 @@ class DecalPBR extends hxsl.Shader {
 		@const var CENTERED : Bool;
 		@const var USE_ALBEDO : Bool;
 		@const var USE_NORMAL : Bool;
-		@const var USE_PBR : Bool;
 
 		@param var albedoStrength : Float;
 		@param var normalStrength : Float;
@@ -171,14 +165,13 @@ class DecalPBR extends hxsl.Shader {
 			if(	outsideBounds(localPos) )
 				discard;
 
-			var strength = vec4(0,0,0,0);
-			var prbValues = vec4(0,0,0,0);
-
+			var albedoSt = 0.;
+			var normalSt = 0.;
 			if( USE_ALBEDO ) {
 				var albedo = albedoTexture.get(calculatedUV);
 				pixelColor *= albedo;
 				alpha = pixelColor.a;
-				strength.r = albedoStrength * alpha;
+				albedoSt = albedoStrength;
 			}
 
 			if( USE_NORMAL ) {
@@ -193,25 +186,12 @@ class DecalPBR extends hxsl.Shader {
 				var tanX = worldTangent.xyz.normalize();
 				var tanY = n.cross(tanX) * -1;
 				transformedNormal = (nf.x * tanX + nf.y * tanY + nf.z * n).normalize();
-				strength.g = normalStrength * alpha;
-			}
-
-			if( USE_PBR ) {
-				var pbr = pbrTexture.get(calculatedUV).rgba;
-				prbValues.r = pbr.r;
-				prbValues.g = 1 - pbr.g * pbr.g;
-				prbValues.b = pbr.b;
-				strength.b = pbrStrength * alpha;
+				normalSt = normalStrength;
 			}
 
-			//output.color = pixelColor; // Allow override
-			output.normal = transformedNormal;
-			output.metalness = prbValues.r;
-			output.roughness = prbValues.g;
-			output.occlusion = prbValues.b;
-			output.albedoStrength = strength.r * fadeFactor;
-			output.normalStrength = strength.g * fadeFactor;
-			output.pbrStrength = strength.b * fadeFactor;
+			output.albedoStrength = albedoSt * alpha * fadeFactor;
+			output.normalStrength = normalSt * alpha * fadeFactor;
+			output.pbrStrength = pbrStrength * alpha * fadeFactor;
 		}
 	};