Переглянути джерело

Merge pull request #6486 from sebavan/master

Fix Glow/Shadow/Default material bones fallback
David Catuhe 6 роки тому
батько
коміт
7933932af6

+ 7 - 3
src/Layers/effectLayer.ts

@@ -18,7 +18,7 @@ import { _DepthCullingState, _StencilState, _AlphaState } from "../States/index"
 import { BaseTexture } from "../Materials/Textures/baseTexture";
 import { Texture } from "../Materials/Textures/texture";
 import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
-import { Effect } from "../Materials/effect";
+import { Effect, EffectFallbacks } from "../Materials/effect";
 import { Material } from "../Materials/material";
 import { MaterialHelper } from "../Materials/materialHelper";
 import { Constants } from "../Engines/constants";
@@ -468,6 +468,7 @@ export abstract class EffectLayer {
         }
 
         // Bones
+        const fallbacks = new EffectFallbacks();
         if (mesh.useBones && mesh.computeBonesUsingShaders) {
             attribs.push(VertexBuffer.MatricesIndicesKind);
             attribs.push(VertexBuffer.MatricesWeightsKind);
@@ -477,6 +478,9 @@ export abstract class EffectLayer {
             }
             defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
             defines.push("#define BonesPerMesh " + (mesh.skeleton ? (mesh.skeleton.bones.length + 1) : 0));
+            if (mesh.numBoneInfluencers > 0) {
+                fallbacks.addCPUSkinningFallback(0, mesh);
+            }
         } else {
             defines.push("#define NUM_BONE_INFLUENCERS 0");
         }
@@ -489,7 +493,7 @@ export abstract class EffectLayer {
                 defines.push("#define MORPHTARGETS");
                 morphInfluencers = manager.numInfluencers;
                 defines.push("#define NUM_MORPH_INFLUENCERS " + morphInfluencers);
-                MaterialHelper.PrepareAttributesForMorphTargets(attribs, mesh, { "NUM_MORPH_INFLUENCERS": morphInfluencers });
+                MaterialHelper.PrepareAttributesForMorphTargetsInfluencers(attribs, mesh, morphInfluencers);
             }
         }
 
@@ -511,7 +515,7 @@ export abstract class EffectLayer {
                     "glowColor", "morphTargetInfluences",
                     "diffuseMatrix", "emissiveMatrix", "opacityMatrix", "opacityIntensity"],
                 ["diffuseSampler", "emissiveSampler", "opacitySampler"], join,
-                undefined, undefined, undefined, { maxSimultaneousMorphTargets: morphInfluencers });
+                fallbacks, undefined, undefined, { maxSimultaneousMorphTargets: morphInfluencers });
         }
 
         return this._effectLayerMapGenerationEffect.isReady();

+ 7 - 3
src/Lights/Shadows/shadowGenerator.ts

@@ -13,7 +13,7 @@ import { Light } from "../../Lights/light";
 import { Material } from "../../Materials/material";
 import { MaterialDefines } from "../../Materials/materialDefines";
 import { MaterialHelper } from "../../Materials/materialHelper";
-import { Effect } from "../../Materials/effect";
+import { Effect, EffectFallbacks } from "../../Materials/effect";
 import { Texture } from "../../Materials/Textures/texture";
 import { RenderTargetTexture } from "../../Materials/Textures/renderTargetTexture";
 
@@ -1186,6 +1186,7 @@ export class ShadowGenerator implements IShadowGenerator {
         }
 
         // Bones
+        const fallbacks = new EffectFallbacks();
         if (mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
             attribs.push(VertexBuffer.MatricesIndicesKind);
             attribs.push(VertexBuffer.MatricesWeightsKind);
@@ -1195,6 +1196,9 @@ export class ShadowGenerator implements IShadowGenerator {
             }
             const skeleton = mesh.skeleton;
             defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
+            if (mesh.numBoneInfluencers > 0) {
+                fallbacks.addCPUSkinningFallback(0, mesh);
+            }
 
             if (skeleton.isUsingTextureForMatrices) {
                 defines.push("#define BONETEXTURE");
@@ -1214,7 +1218,7 @@ export class ShadowGenerator implements IShadowGenerator {
                 defines.push("#define MORPHTARGETS");
                 morphInfluencers = manager.numInfluencers;
                 defines.push("#define NUM_MORPH_INFLUENCERS " + morphInfluencers);
-                MaterialHelper.PrepareAttributesForMorphTargets(attribs, mesh, { "NUM_MORPH_INFLUENCERS": morphInfluencers });
+                MaterialHelper.PrepareAttributesForMorphTargetsInfluencers(attribs, mesh, morphInfluencers);
             }
         }
 
@@ -1275,7 +1279,7 @@ export class ShadowGenerator implements IShadowGenerator {
             this._effect = this._scene.getEngine().createEffect(shaderName,
                 attribs, uniforms,
                 samplers, join,
-                undefined, undefined, undefined, { maxSimultaneousMorphTargets: morphInfluencers });
+                fallbacks, undefined, undefined, { maxSimultaneousMorphTargets: morphInfluencers });
         }
 
         if (!this._effect.isReady()) {

+ 0 - 4
src/Materials/Background/backgroundMaterial.ts

@@ -848,10 +848,6 @@ export class BackgroundMaterial extends PushMaterial {
 
             MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this._maxSimultaneousLights);
 
-            if (defines.NUM_BONE_INFLUENCERS > 0) {
-                fallbacks.addCPUSkinningFallback(0, mesh);
-            }
-
             //Attributes
             var attribs = [VertexBuffer.PositionKind];
 

+ 0 - 4
src/Materials/PBR/pbrBaseMaterial.ts

@@ -1131,10 +1131,6 @@ export abstract class PBRBaseMaterial extends PushMaterial {
             fallbacks.addFallback(fallbackRank++, "VERTEXCOLOR");
         }
 
-        if (defines.NUM_BONE_INFLUENCERS > 0) {
-            fallbacks.addCPUSkinningFallback(fallbackRank++, mesh);
-        }
-
         if (defines.MORPHTARGETS) {
             fallbacks.addFallback(fallbackRank++, "MORPHTARGETS");
         }

+ 4 - 1
src/Materials/effect.ts

@@ -86,7 +86,7 @@ export class EffectFallbacks {
      */
     public reduce(currentDefines: string, effect: Effect): string {
         // First we try to switch to CPU skinning
-        if (this._mesh && this._mesh.computeBonesUsingShaders && this._mesh.numBoneInfluencers > 0 && this._mesh.material) {
+        if (this._mesh && this._mesh.computeBonesUsingShaders && this._mesh.numBoneInfluencers > 0) {
             this._mesh.computeBonesUsingShaders = false;
             currentDefines = currentDefines.replace("#define NUM_BONE_INFLUENCERS " + this._mesh.numBoneInfluencers, "#define NUM_BONE_INFLUENCERS 0");
             effect._bonesComputationForcedToCPU = true;
@@ -96,6 +96,9 @@ export class EffectFallbacks {
                 var otherMesh = scene.meshes[index];
 
                 if (!otherMesh.material) {
+                    if (!this._mesh.material && otherMesh.computeBonesUsingShaders && otherMesh.numBoneInfluencers > 0) {
+                        otherMesh.computeBonesUsingShaders = false;
+                    }
                     continue;
                 }
 

+ 12 - 0
src/Materials/materialHelper.ts

@@ -554,6 +554,18 @@ export class MaterialHelper {
         return lightFallbackRank++;
     }
 
+    private static _TmpMorphInfluencers = { "NUM_MORPH_INFLUENCERS": 0 };
+    /**
+     * Prepares the list of attributes required for morph targets according to the effect defines.
+     * @param attribs The current list of supported attribs
+     * @param mesh The mesh to prepare the morph targets attributes for
+     * @param influencers The number of influencers
+     */
+    public static PrepareAttributesForMorphTargetsInfluencers(attribs: string[], mesh: AbstractMesh, influencers: number): void {
+        this._TmpMorphInfluencers.NUM_MORPH_INFLUENCERS = influencers;
+        this.PrepareAttributesForMorphTargets(attribs, mesh, this._TmpMorphInfluencers);
+    }
+
     /**
      * Prepares the list of attributes required for morph targets according to the effect defines.
      * @param attribs The current list of supported attribs

+ 1 - 1
src/Rendering/depthRenderer.ts

@@ -204,7 +204,7 @@ export class DepthRenderer {
                 defines.push("#define MORPHTARGETS");
                 defines.push("#define NUM_MORPH_INFLUENCERS " + numMorphInfluencers);
 
-                MaterialHelper.PrepareAttributesForMorphTargets(attribs, mesh, { "NUM_MORPH_INFLUENCERS": numMorphInfluencers });
+                MaterialHelper.PrepareAttributesForMorphTargetsInfluencers(attribs, mesh, numMorphInfluencers);
             }
         }
 

+ 1 - 1
src/Rendering/geometryBufferRenderer.ts

@@ -237,7 +237,7 @@ export class GeometryBufferRenderer {
                 defines.push("#define MORPHTARGETS");
                 defines.push("#define NUM_MORPH_INFLUENCERS " + numMorphInfluencers);
 
-                MaterialHelper.PrepareAttributesForMorphTargets(attribs, mesh, { "NUM_MORPH_INFLUENCERS": numMorphInfluencers });
+                MaterialHelper.PrepareAttributesForMorphTargetsInfluencers(attribs, mesh, numMorphInfluencers);
             }
         }
 

+ 1 - 1
src/Rendering/outlineRenderer.ts

@@ -266,7 +266,7 @@ export class OutlineRenderer implements ISceneComponent {
                 defines.push("#define MORPHTARGETS");
                 defines.push("#define NUM_MORPH_INFLUENCERS " + numMorphInfluencers);
 
-                MaterialHelper.PrepareAttributesForMorphTargets(attribs, mesh, { "NUM_MORPH_INFLUENCERS": numMorphInfluencers });
+                MaterialHelper.PrepareAttributesForMorphTargetsInfluencers(attribs, mesh, numMorphInfluencers);
             }
         }
 

+ 0 - 30
src/Shaders/ShadersInclude/bones300Declaration.fx

@@ -1,30 +0,0 @@
-#if NUM_BONE_INFLUENCERS > 0
-	#ifdef BONETEXTURE
-		uniform sampler2D boneSampler;
-	#else
-		uniform mat4 mBones[BonesPerMesh];
-	#endif	
-
-		in vec4 matricesIndices;
-		in vec4 matricesWeights;
-		#if NUM_BONE_INFLUENCERS > 4
-			in vec4 matricesIndicesExtra;
-			in vec4 matricesWeightsExtra;
-		#endif
-
-	#ifdef BONETEXTURE
-		mat4 readMatrixFromRawSampler(sampler2D smp, float index)
-		{
-			mat4 result;
-			float offset = index  * 4.0;	
-			float dx = 1.0 / boneTextureWidth;
-
-			result[0] = texture(smp, vec2(dx * (offset + 0.5), 0.));
-			result[1] = texture(smp, vec2(dx * (offset + 1.5), 0.));
-			result[2] = texture(smp, vec2(dx * (offset + 2.5), 0.));
-			result[3] = texture(smp, vec2(dx * (offset + 3.5), 0.));
-
-			return result;
-		}
-	#endif
-#endif

+ 0 - 8
src/Shaders/ShadersInclude/instances300Declaration.fx

@@ -1,8 +0,0 @@
-#ifdef INSTANCES
-	in vec4 world0;
-	in vec4 world1;
-	in vec4 world2;
-	in vec4 world3;
-#else
-	uniform mat4 world;
-#endif