瀏覽代碼

Merge pull request #3239 from sebavan/master

Fix PBR Opacity, Linear Opacity Fresnel, and issue #3237 Mesh.lookat
sebavan 7 年之前
父節點
當前提交
9ec4199c92

+ 11 - 3
src/Materials/PBR/babylon.pbrBaseMaterial.ts

@@ -27,6 +27,7 @@
         public SPECULAROVERALPHA = false;
         public RADIANCEOVERALPHA = false;
         public ALPHAFRESNEL = false;
+        public LINEARALPHAFRESNEL = false;
         public PREMULTIPLYALPHA = false;
 
         public EMISSIVE = false;
@@ -394,11 +395,17 @@
 
         /**
          * A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
-         * And/Or occlude the blended part.
+         * And/Or occlude the blended part. (alpha is converted to gamma to compute the fresnel)
          */
         protected _useAlphaFresnel = false;
 
         /**
+         * A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
+         * And/Or occlude the blended part. (alpha stays linear to compute the fresnel)
+         */
+        protected _useLinearAlphaFresnel = false;
+
+        /**
          * The transparency mode of the material.
          */
         protected _transparencyMode: Nullable<number> = null;
@@ -572,7 +579,7 @@
                 return false;
             }
 
-            return this._albedoTexture != null && this._albedoTexture.hasAlpha && this._transparencyMode === PBRMaterial.PBRMATERIAL_ALPHATEST;
+            return this._albedoTexture != null && this._albedoTexture.hasAlpha && (this._transparencyMode == null || this._transparencyMode === PBRMaterial.PBRMATERIAL_ALPHATEST);
         }
 
         /**
@@ -871,7 +878,8 @@
                 defines.ALPHATESTVALUE = this._alphaCutOff;
                 defines.PREMULTIPLYALPHA = (this.alphaMode === Engine.ALPHA_PREMULTIPLIED || this.alphaMode === Engine.ALPHA_PREMULTIPLIED_PORTERDUFF);
                 defines.ALPHABLEND = this.needAlphaBlendingForMesh(mesh);
-                defines.ALPHAFRESNEL = this._useAlphaFresnel;
+                defines.ALPHAFRESNEL = this._useAlphaFresnel || this._useLinearAlphaFresnel;
+                defines.LINEARALPHAFRESNEL = this._useLinearAlphaFresnel;
             }
 
             if (defines._areImageProcessingDirty) {

+ 0 - 1
src/Materials/PBR/babylon.pbrBaseSimpleMaterial.ts

@@ -161,7 +161,6 @@
         constructor(name: string, scene: Scene) {
             super(name, scene);
 
-            this._transparencyMode = PBRMaterial.PBRMATERIAL_OPAQUE;
             this._useAlphaFromAlbedoTexture = true;
             this._useAmbientInGrayScale = true;
         }

+ 9 - 1
src/Materials/PBR/babylon.pbrMaterial.ts

@@ -382,7 +382,7 @@
 
         /**
          * A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
-         * And/Or occlude the blended part.
+         * And/Or occlude the blended part. (alpha is converted to gamma to compute the fresnel)
          */
         @serialize()
         @expandToProperty("_markAllSubMeshesAsTexturesDirty")
@@ -390,6 +390,14 @@
 
         /**
          * A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
+         * And/Or occlude the blended part. (alpha stays linear to compute the fresnel)
+         */
+        @serialize()
+        @expandToProperty("_markAllSubMeshesAsTexturesDirty")
+        public useLinearAlphaFresnel = false;
+
+        /**
+         * A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
          * And/Or occlude the blended part.
          */
         @serializeAsTexture()

+ 19 - 9
src/Mesh/babylon.transformNode.ts

@@ -331,22 +331,32 @@ module BABYLON {
         }
 
         private static _lookAtVectorCache = new Vector3(0, 0, 0);
-        public lookAt(targetPoint: Vector3, yawCor: number = 0, pitchCor: number = 0, rollCor: number = 0, space: Space = Space.LOCAL): TransformNode {
-            /// <summary>Orients a mesh towards a target point. Mesh must be drawn facing user.</summary>
-            /// <param name="targetPoint" type="Vector3">The position (must be in same space as current mesh) to look at</param>
-            /// <param name="yawCor" type="Number">optional yaw (y-axis) correction in radians</param>
-            /// <param name="pitchCor" type="Number">optional pitch (x-axis) correction in radians</param>
-            /// <param name="rollCor" type="Number">optional roll (z-axis) correction in radians</param>
-            /// <returns>Mesh oriented towards targetMesh</returns>
 
+        /**
+         * Orients a mesh towards a target point. Mesh must be drawn facing user.
+         * @param targetPoint the position (must be in same space as current mesh) to look at
+         * @param yawCor optional yaw (y-axis) correction in radians
+         * @param pitchCor optional pitch (x-axis) correction in radians
+         * @param rollCor optional roll (z-axis) correction in radians
+         * @param space the choosen space of the target
+         * @returns the TransformNode. 
+         */
+        public lookAt(targetPoint: Vector3, yawCor: number = 0, pitchCor: number = 0, rollCor: number = 0, space: Space = Space.LOCAL): TransformNode {
             var dv = AbstractMesh._lookAtVectorCache;
             var pos = space === Space.LOCAL ? this.position : this.getAbsolutePosition();
             targetPoint.subtractToRef(pos, dv);
             var yaw = -Math.atan2(dv.z, dv.x) - Math.PI / 2;
             var len = Math.sqrt(dv.x * dv.x + dv.z * dv.z);
             var pitch = Math.atan2(dv.y, len);
-            this.rotationQuaternion = this.rotationQuaternion || new Quaternion();
-            Quaternion.RotationYawPitchRollToRef(yaw + yawCor, pitch + pitchCor, rollCor, this.rotationQuaternion);
+            if (this.rotationQuaternion) {
+                this.rotationQuaternion = this.rotationQuaternion || new Quaternion();
+                Quaternion.RotationYawPitchRollToRef(yaw + yawCor, pitch + pitchCor, rollCor, this.rotationQuaternion);
+            }
+            else {
+                this.rotation.x = pitch + pitchCor;
+                this.rotation.y = yaw + yawCor;
+                this.rotation.z = rollCor;
+            }
             return this;
         }
 

+ 6 - 1
src/Shaders/pbr.fragment.fx

@@ -388,7 +388,12 @@ void main(void) {
 		// for use with the linear HDR render target. The final composition will be converted back to gamma encoded values for eventual display.
 		// Uses power 2.0 rather than 2.2 for simplicity/efficiency, and because the mapping does not need to map the gamma applied to RGB.
 		float opacityPerceptual = alpha;
-		float opacity0 = opacityPerceptual * opacityPerceptual;
+
+		#ifdef LINEARALPHAFRESNEL
+			float opacity0 = opacityPerceptual;
+		#else
+			float opacity0 = opacityPerceptual * opacityPerceptual;
+		#endif
 		float opacity90 = fresnelGrazingReflectance(opacity0);
 
 		vec3 normalForward = faceforward(normalW, -viewDirectionW, normalW);