Explorar el Código

Add support for ambient from red channel and metallic from blue channel

Gary Hsu hace 8 años
padre
commit
0ed5ae0e11
Se han modificado 2 ficheros con 35 adiciones y 9 borrados
  1. 21 5
      src/Materials/babylon.pbrMaterial.ts
  2. 14 4
      src/Shaders/pbr.fragment.fx

+ 21 - 5
src/Materials/babylon.pbrMaterial.ts

@@ -2,6 +2,7 @@
     class PBRMaterialDefines extends MaterialDefines {
         public ALBEDO = false;
         public AMBIENT = false;
+        public AMBIENTSTOREINRED = false;
         public OPACITY = false;
         public OPACITYRGB = false;
         public REFLECTION = false;
@@ -68,8 +69,9 @@
 
         public METALLICWORKFLOW = false;
         public METALLICROUGHNESSMAP = false;
-        public METALLICROUGHNESSGSTOREINALPHA = false;
-        public METALLICROUGHNESSGSTOREINGREEN = false;
+        public METALLICSTOREINBLUE = false;
+        public ROUGHNESSGSTOREINALPHA = false;
+        public ROUGHNESSGSTOREINGREEN = false;
 
         constructor() {
             super();
@@ -273,6 +275,12 @@
         @serialize()
         public ambientTextureStrength: number = 1.0;
 
+        /**
+         * Specifies if the ambient occlusion texture contains the information in its red channel only.
+         */
+        @serialize()
+        public useAmbientFromAmbientTextureRed: boolean = false;
+
         @serializeAsTexture()
         public opacityTexture: BaseTexture;
 
@@ -319,7 +327,7 @@
 
         @serializeAsColor3("ambient")
         public ambientColor = new Color3(0, 0, 0);
-        
+
         /**
          * AKA Diffuse Color in other nomenclature.
          */
@@ -405,6 +413,12 @@
         public useMicroSurfaceFromReflectivityMapAlpha = false;
 
         /**
+         * Specifies if the metallic texture contains the metallic information in its blue channel.
+         */
+        @serialize()
+        public useMetallicFromMetallicTextureBlue = false;
+
+        /**
          * Specifies if the metallic texture contains the roughness information in its alpha channel.
          */
         @serialize()
@@ -687,6 +701,7 @@
                     } else {
                         needUVs = true;
                         this._defines.AMBIENT = true;
+                        this._defines.AMBIENTSTOREINRED = this.useAmbientFromAmbientTextureRed;
                     }
                 }
 
@@ -779,8 +794,9 @@
                             needUVs = true;
                             this._defines.METALLICWORKFLOW = true;
                             this._defines.METALLICROUGHNESSMAP = true;
-                            this._defines.METALLICROUGHNESSGSTOREINALPHA = this.useRoughnessFromMetallicTextureAlpha;
-                            this._defines.METALLICROUGHNESSGSTOREINGREEN = !this.useRoughnessFromMetallicTextureAlpha && this.useRoughnessFromMetallicTextureGreen;
+                            this._defines.METALLICSTOREINBLUE = this.useMetallicFromMetallicTextureBlue;
+                            this._defines.ROUGHNESSGSTOREINALPHA = this.useRoughnessFromMetallicTextureAlpha;
+                            this._defines.ROUGHNESSGSTOREINGREEN = !this.useRoughnessFromMetallicTextureAlpha && this.useRoughnessFromMetallicTextureGreen;
                         }
                     }
                     else if (this.reflectivityTexture) {

+ 14 - 4
src/Shaders/pbr.fragment.fx

@@ -238,7 +238,12 @@ void main(void) {
 	vec3 ambientColor = vec3(1., 1., 1.);
 
 #ifdef AMBIENT
+#ifdef AMBIENTSTOREINRED
+	ambientColor *= texture2D(ambientSampler, vAmbientUV + uvOffset).r * vAmbientInfos.y;
+#else
 	ambientColor = texture2D(ambientSampler, vAmbientUV + uvOffset).rgb * vAmbientInfos.y;
+#endif
+
 	ambientColor = vec3(1., 1., 1.) - ((vec3(1., 1., 1.) - ambientColor) * vAmbientInfos.z);
 
 #ifdef OVERLOADEDVALUES
@@ -279,11 +284,16 @@ void main(void) {
 		vec4 surfaceMetallicColorMap = texture2D(reflectivitySampler, vReflectivityUV + uvOffset);
 
 		// No gamma space from the metallic map in metallic workflow.
-		metallicRoughness.r *= surfaceMetallicColorMap.r;
-		#ifdef METALLICROUGHNESSGSTOREINALPHA
+		#ifdef METALLICSTOREINBLUE
+			metallicRoughness.r *= surfaceMetallicColorMap.b;
+		#else
+			metallicRoughness.r *= surfaceMetallicColorMap.r;
+		#endif
+
+		#ifdef ROUGHNESSGSTOREINALPHA
 			metallicRoughness.g *= surfaceMetallicColorMap.a;
 		#else
-			#ifdef METALLICROUGHNESSGSTOREINGREEN
+			#ifdef ROUGHNESSGSTOREINGREEN
 				metallicRoughness.g *= surfaceMetallicColorMap.g;
 			#endif
 		#endif
@@ -294,7 +304,7 @@ void main(void) {
 
 	// Drop the surface diffuse by the 1.0 - metalness.
 	surfaceAlbedo.rgb *= (1.0 - metallicRoughness.r);
-	
+
 	// Default specular reflectance at normal incidence.
 	// 4% corresponds to index of refraction (IOR) of 1.50, approximately equal to glass.
 	const vec3 DefaultSpecularReflectanceDielectric = vec3(0.04, 0.04, 0.04);