David Catuhe 6 年之前
父節點
當前提交
b6d255d211
共有 2 個文件被更改,包括 15 次插入6 次删除
  1. 5 3
      materialsLibrary/src/grid/grid.vertex.fx
  2. 10 3
      materialsLibrary/src/grid/gridMaterial.ts

+ 5 - 3
materialsLibrary/src/grid/grid.vertex.fx

@@ -10,11 +10,12 @@ attribute vec2 uv;
 attribute vec2 uv2;
 #endif
 
+#include<instancesDeclaration>
+
 // Uniforms
 uniform mat4 projection;
 uniform mat4 world;
 uniform mat4 view;
-uniform mat4 worldView;
 
 // Varying
 varying vec3 vPosition;
@@ -29,14 +30,15 @@ uniform vec2 vOpacityInfos;
 #endif
 
 void main(void) {
+	#include<instancesVertex>
 
     #ifdef FOG
-    vec4 worldPos = world * vec4(position, 1.0);
+    vec4 worldPos = finalWorld * vec4(position, 1.0);
     #endif
 
     #include<fogVertex>
 
-    vec4 cameraSpacePosition = worldView * vec4(position, 1.0);
+    vec4 cameraSpacePosition = view * finalWorld * vec4(position, 1.0);
     gl_Position = projection * cameraSpacePosition;
 
 #ifdef OPACITY

+ 10 - 3
materialsLibrary/src/grid/gridMaterial.ts

@@ -23,6 +23,7 @@ class GridMaterialDefines extends MaterialDefines {
     public PREMULTIPLYALPHA = false;
     public UV1 = false;
     public UV2 = false;
+    public INSTANCES = false;
 
     constructor() {
         super();
@@ -160,6 +161,9 @@ export class GridMaterial extends PushMaterial {
 
         MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, false, this.fogEnabled, false, defines);
 
+        // Values that need to be evaluated on every frame
+        MaterialHelper.PrepareDefinesForFrameBoundValues(scene, scene.getEngine(), defines, !!useInstances);
+
         // Get correct effect
         if (defines.isDirty) {
             defines.markAsProcessed();
@@ -175,12 +179,14 @@ export class GridMaterial extends PushMaterial {
             if (defines.UV2) {
                 attribs.push(VertexBuffer.UV2Kind);
             }
+            
+            MaterialHelper.PrepareAttributesForInstances(attribs, defines);
 
             // Defines
             var join = defines.toString();
             subMesh.setEffect(scene.getEngine().createEffect("grid",
                 attribs,
-                ["projection", "worldView", "mainColor", "lineColor", "gridControl", "gridOffset", "vFogInfos", "vFogColor", "world", "view",
+                ["projection", "mainColor", "lineColor", "gridControl", "gridOffset", "vFogInfos", "vFogColor", "world", "view",
                     "opacityMatrix", "vOpacityInfos"],
                 ["opacitySampler"],
                 join,
@@ -214,8 +220,9 @@ export class GridMaterial extends PushMaterial {
         this._activeEffect = effect;
 
         // Matrices
-        this.bindOnlyWorldMatrix(world);
-        this._activeEffect.setMatrix("worldView", world.multiply(scene.getViewMatrix()));
+        if (!defines.INSTANCES) {
+            this.bindOnlyWorldMatrix(world);
+        }
         this._activeEffect.setMatrix("view", scene.getViewMatrix());
         this._activeEffect.setMatrix("projection", scene.getProjectionMatrix());