瀏覽代碼

Move defines under block responsibility
expose inputs as properties

David Catuhe 6 年之前
父節點
當前提交
667d2ae998

+ 27 - 9
src/Materials/Node/Blocks/Dual/fogBlock.ts

@@ -5,6 +5,11 @@ import { NodeMaterialWellKnownValues } from '../../nodeMaterialWellKnownValues';
 import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
 import { Mesh } from '../../../../Meshes/mesh';
 import { Effect } from '../../../effect';
+import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
+import { MaterialDefines } from '../../../materialDefines';
+import { AbstractMesh } from '../../../../Meshes/abstractMesh';
+import { MaterialHelper} from '../../../materialHelper';
+import { NodeMaterial} from '../../nodeMaterial';
 
 /**
  * Block used to add support for scene fog
@@ -24,9 +29,10 @@ export class FogBlock extends NodeMaterialBlock {
         this.registerOutput("vFogDistance", NodeMaterialBlockConnectionPointTypes.Vector3, NodeMaterialBlockTargets.Vertex);
 
         // Fragment
-        this.registerInput("input", NodeMaterialBlockConnectionPointTypes.Color3OrColor4, false, NodeMaterialBlockTargets.Fragment);
+        this.registerInput("color", NodeMaterialBlockConnectionPointTypes.Color3OrColor4, false, NodeMaterialBlockTargets.Fragment);
         this.registerInput("fogColor", NodeMaterialBlockConnectionPointTypes.Color3, false, NodeMaterialBlockTargets.Fragment);
         this.registerInput("fogParameters", NodeMaterialBlockConnectionPointTypes.Vector4, false, NodeMaterialBlockTargets.Fragment);
+        
         this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
 
         // Auto configuration
@@ -44,6 +50,18 @@ export class FogBlock extends NodeMaterialBlock {
         return "FogBlock";
     }
 
+    /**
+     * Gets the color input component
+     */
+    public get color(): NodeMaterialConnectionPoint {
+        return this._inputs[2];
+    }
+
+    public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: MaterialDefines) {
+        let scene = mesh.getScene();
+        defines["FOG"] = nodeMaterial.fogEnabled && MaterialHelper.GetFogState(mesh, scene)
+    }
+
     public bind(effect: Effect, mesh?: Mesh) {
         if (!mesh) {
             return;
@@ -54,31 +72,31 @@ export class FogBlock extends NodeMaterialBlock {
         effect.setFloat4("fogParameters", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
     }
 
-    /** @hidden */
-    public get _canAddAtFragmentRoot(): boolean {
-        return false;
-    }
-
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
+        state.sharedData.blocksWithDefines.push(this);
+
         if (state.target === NodeMaterialBlockTargets.Fragment) {
             state._emitFunctionFromInclude("CalcFogFactor", "fogFragmentDeclaration", {
                 removeUniforms: true,
                 removeVaryings: true,
-                removeifDef: true,
+                removeifDef: false,
                 replaceStrings: [{ search: /float CalcFogFactor\(\)/, replace: "float CalcFogFactor(vec3 vFogDistance, vec4 vFogInfos)" }]
             });
 
             let tempFogVariablename = state._getFreeVariableName("fog");
-            let input = this._inputs[2];
+            let color = this.color;
             let fogColor = this._inputs[3];
             let fogParameters = this._inputs[4];
             let output = this._outputs[1];
             let vFogDistance = this._outputs[0];
 
+            state.compilationString += `#ifdef FOG\r\n`;
             state.compilationString += `float ${tempFogVariablename} = CalcFogFactor(${vFogDistance.associatedVariableName}, ${fogParameters.associatedVariableName});\r\n`;
-            state.compilationString += this._declareOutput(output, state) + ` = ${tempFogVariablename} * ${input.associatedVariableName}.rgb + (1.0 - ${tempFogVariablename}) * ${fogColor.associatedVariableName};\r\n`;
+            state.compilationString += this._declareOutput(output, state) + ` = ${tempFogVariablename} * ${color.associatedVariableName}.rgb + (1.0 - ${tempFogVariablename}) * ${fogColor.associatedVariableName};\r\n`;
+            state.compilationString += `#else\r\n${this._declareOutput(output, state)} =  ${color.associatedVariableName}.rgb;\r\n`;
+            state.compilationString += `#endif\r\n`;
         } else {
             let worldPos = this._inputs[0];
             let view = this._inputs[1];

+ 16 - 4
src/Materials/Node/Blocks/Fragment/fragmentOutputBlock.ts

@@ -2,6 +2,7 @@ import { NodeMaterialBlock } from '../../nodeMaterialBlock';
 import { NodeMaterialBlockConnectionPointTypes } from '../../nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
 import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
+import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
 
 /**
  * Block used to output the final color
@@ -18,7 +19,7 @@ export class FragmentOutputBlock extends NodeMaterialBlock {
     public constructor(name: string) {
         super(name, NodeMaterialBlockTargets.Fragment, true);
 
-        this.registerInput("color", NodeMaterialBlockConnectionPointTypes.Color4);
+        this.registerInput("color", NodeMaterialBlockConnectionPointTypes.Color3OrColor4);
     }
 
     /**
@@ -28,6 +29,13 @@ export class FragmentOutputBlock extends NodeMaterialBlock {
     public getClassName() {
         return "FragmentOutputBlock";
     }
+        
+    /**
+     * Gets the color input component
+     */
+    public get color(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
 
     /** @hidden */
     public get _canAddAtVertexRoot(): boolean {
@@ -36,16 +44,20 @@ export class FragmentOutputBlock extends NodeMaterialBlock {
 
     /** @hidden */
     public get _canAddAtFragmentRoot(): boolean {
-        return false;
+        return true;
     }
 
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
-        let input = this._inputs[0];
+        let input = this.color;
         state.sharedData.hints.needAlphaBlending = this.alphaBlendingEnabled;
 
-        state.compilationString += `gl_FragColor = ${input.associatedVariableName};\r\n`;
+        if (input.connectedPoint!.type === NodeMaterialBlockConnectionPointTypes.Color3) {
+            state.compilationString += `gl_FragColor = vec4(${input.associatedVariableName}, 1.0);\r\n`;
+        } else {
+            state.compilationString += `gl_FragColor = ${input.associatedVariableName};\r\n`;
+        }
 
         return this;
     }

+ 25 - 3
src/Materials/Node/Blocks/Fragment/rgbMergerBlock.ts

@@ -2,6 +2,7 @@ import { NodeMaterialBlock } from '../../nodeMaterialBlock';
 import { NodeMaterialBlockConnectionPointTypes } from '../../nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
 import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
+import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
 
 /**
  * Block used to create a Color3 out of 3 inputs (one for each component)
@@ -29,12 +30,33 @@ export class RGBMergerBlock extends NodeMaterialBlock {
         return "RGBMergerBlock";
     }
 
+    /**
+     * Gets the R component input
+     */
+    public get r(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
+    /**
+     * Gets the G component input
+     */
+    public get g(): NodeMaterialConnectionPoint {
+        return this._inputs[1];
+    }    
+
+    /**
+     * Gets the B component input
+     */
+    public get b(): NodeMaterialConnectionPoint {
+        return this._inputs[2];
+    }
+
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
-        let rInput = this._inputs[0];
-        let gInput = this._inputs[1];
-        let bInput = this._inputs[2];
+        let rInput = this.r;
+        let gInput = this.g;
+        let bInput = this.b;
 
         let output = this._outputs[0];
 

+ 9 - 1
src/Materials/Node/Blocks/Fragment/rgbSplitterBlock.ts

@@ -2,6 +2,7 @@ import { NodeMaterialBlock } from '../../nodeMaterialBlock';
 import { NodeMaterialBlockConnectionPointTypes } from '../../nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
 import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
+import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
 
 /**
  * Block used to expand a Color3 or a Vector3 into 3 outputs (one for each component)
@@ -29,10 +30,17 @@ export class RGBSplitterBlock extends NodeMaterialBlock {
         return "RGBSplitterBlock";
     }
 
+    /**
+     * Gets the input component
+     */
+    public get input(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
-        let input = this._inputs[0];
+        let input = this.input;
         let rOutput = this._outputs[0];
         let gOutput = this._outputs[1];
         let bOutput = this._outputs[2];

+ 38 - 2
src/Materials/Node/Blocks/Fragment/rgbaMergerBlock.ts

@@ -2,6 +2,7 @@ import { NodeMaterialBlock } from '../../nodeMaterialBlock';
 import { NodeMaterialBlockConnectionPointTypes } from '../../nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
 import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
+import { NodeMaterialConnectionPoint } from 'Materials/Node/nodeMaterialBlockConnectionPoint';
 
 /**
  * Block used to create a Color4 out of 4 inputs (one for each component)
@@ -31,11 +32,46 @@ export class RGBAMergerBlock extends NodeMaterialBlock {
         return "RGBAMergerBlock";
     }
 
+    /**
+     * Gets the R component input
+     */
+    public get r(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
+    /**
+     * Gets the G component input
+     */
+    public get g(): NodeMaterialConnectionPoint {
+        return this._inputs[1];
+    }    
+
+    /**
+     * Gets the B component input
+     */
+    public get b(): NodeMaterialConnectionPoint {
+        return this._inputs[2];
+    }
+
+    /**
+     * Gets the RGB component input
+     */
+    public get rgb(): NodeMaterialConnectionPoint {
+        return this._inputs[3];
+    }   
+    
+    /**
+     * Gets the R component input
+     */
+    public get a(): NodeMaterialConnectionPoint {
+        return this._inputs[4];
+    }    
+
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
-        let rgbInput = this._inputs[3];
-        let aInput = this._inputs[4];
+        let rgbInput = this.rgb;
+        let aInput = this.a;
         let output = this._outputs[0];
 
         if (rgbInput.connectedPoint) {

+ 9 - 1
src/Materials/Node/Blocks/Fragment/rgbaSplitterBlock.ts

@@ -2,6 +2,7 @@ import { NodeMaterialBlock } from '../../nodeMaterialBlock';
 import { NodeMaterialBlockConnectionPointTypes } from '../../nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
 import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
+import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
 
 /**
  * Block used to expand a Color4 or a Vector4 into 4 outputs (one for each component)
@@ -30,10 +31,17 @@ export class RGBASplitterBlock extends NodeMaterialBlock {
         return "RGBASplitterBlock";
     }
 
+    /**
+     * Gets the input component
+     */
+    public get input(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
-        let input = this._inputs[0];
+        let input = this.input;
         let rOutput = this._outputs[0];
         let gOutput = this._outputs[1];
         let bOutput = this._outputs[2];

+ 9 - 1
src/Materials/Node/Blocks/Vertex/vertexOutputBlock.ts

@@ -2,6 +2,7 @@ import { NodeMaterialBlock } from '../../nodeMaterialBlock';
 import { NodeMaterialBlockConnectionPointTypes } from '../../nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
 import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
+import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
 
 /**
  * Block used to output the vertex position
@@ -25,6 +26,13 @@ export class VertexOutputBlock extends NodeMaterialBlock {
     public getClassName() {
         return "VertexOutputBlock";
     }
+    
+    /**
+     * Gets the vector input component
+     */
+    public get input(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
 
     /** @hidden */
     public get _canAddAtVertexRoot(): boolean {
@@ -39,7 +47,7 @@ export class VertexOutputBlock extends NodeMaterialBlock {
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
-        let input = this._inputs[0];
+        let input = this.input;
 
         state.compilationString += `gl_Position = ${input.associatedVariableName};\r\n`;
 

+ 1 - 1
src/Materials/Node/Blocks/index.ts

@@ -8,4 +8,4 @@ export * from "./textureBlock";
 export * from "./vector2TransformBlock";
 export * from "./vector3TransformBlock";
 export * from "./vector4TransformBlock";
-export * from "./matrixMultiplication";
+export * from "./matrixMultiplicationBlock";

+ 16 - 3
src/Materials/Node/Blocks/matrixMultiplication.ts

@@ -2,6 +2,7 @@ import { NodeMaterialBlock } from '../nodeMaterialBlock';
 import { NodeMaterialBlockConnectionPointTypes } from '../nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
 import { NodeMaterialBlockTargets } from '../nodeMaterialBlockTargets';
+import { NodeMaterialConnectionPoint } from '../nodeMaterialBlockConnectionPoint';
 
 /**
  * Block used to multiply two matrices
@@ -20,6 +21,20 @@ export class MatrixMultiplicationBlock extends NodeMaterialBlock {
     }
 
     /**
+     * Gets the left operand
+     */
+    public get left(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
+    /**
+     * Gets the right operand
+     */
+    public get right(): NodeMaterialConnectionPoint {
+        return this._inputs[1];
+    }
+
+    /**
      * Gets the current class name
      * @returns the class name
      */
@@ -31,10 +46,8 @@ export class MatrixMultiplicationBlock extends NodeMaterialBlock {
         super._buildBlock(state);
 
         let output = this._outputs[0];
-        let input0 = this._inputs[0];
-        let input1 = this._inputs[1];
 
-        state.compilationString += this._declareOutput(output, state) + ` = ${input0.associatedVariableName} * ${input1.associatedVariableName};\r\n`;
+        state.compilationString += this._declareOutput(output, state) + ` = ${this.left.associatedVariableName} * ${this.right.associatedVariableName};\r\n`;
 
         return this;
     }

+ 17 - 2
src/Materials/Node/Blocks/vector2TransformBlock.ts

@@ -2,6 +2,7 @@ import { NodeMaterialBlock } from '../nodeMaterialBlock';
 import { NodeMaterialBlockConnectionPointTypes } from '../nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
 import { NodeMaterialBlockTargets } from '../nodeMaterialBlockTargets';
+import { NodeMaterialConnectionPoint } from '../nodeMaterialBlockConnectionPoint';
 
 /**
  * Block used to transform a vector2 with a matrix
@@ -30,6 +31,20 @@ export class Vector2TransformBlock extends NodeMaterialBlock {
     }
 
     /**
+     * Gets the vector input
+     */
+    public get vector(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
+    /**
+     * Gets the matrix transform input
+     */
+    public get transform(): NodeMaterialConnectionPoint {
+        return this._inputs[1];
+    }
+
+    /**
      * Gets the current class name
      * @returns the class name
      */
@@ -41,8 +56,8 @@ export class Vector2TransformBlock extends NodeMaterialBlock {
         super._buildBlock(state);
 
         let output = this._outputs[0];
-        let vector = this._inputs[0];
-        let transform = this._inputs[1];
+        let vector = this.vector;
+        let transform = this.transform;
 
         state.compilationString += this._declareOutput(output, state) + ` = vec2(${transform.associatedVariableName} * vec4(${vector.associatedVariableName}, ${this.complementZ}, ${this.complementW}));\r\n`;
 

+ 17 - 2
src/Materials/Node/Blocks/vector3TransformBlock.ts

@@ -2,6 +2,7 @@ import { NodeMaterialBlock } from '../nodeMaterialBlock';
 import { NodeMaterialBlockConnectionPointTypes } from '../nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
 import { NodeMaterialBlockTargets } from '../nodeMaterialBlockTargets';
+import { NodeMaterialConnectionPoint } from '../nodeMaterialBlockConnectionPoint';
 
 /**
  * Block used to transform a vector3 with a matrix
@@ -25,6 +26,20 @@ export class Vector3TransformBlock extends NodeMaterialBlock {
     }
 
     /**
+     * Gets the vector input
+     */
+    public get vector(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
+    /**
+     * Gets the matrix transform input
+     */
+    public get transform(): NodeMaterialConnectionPoint {
+        return this._inputs[1];
+    }
+
+    /**
      * Gets the current class name
      * @returns the class name
      */
@@ -36,8 +51,8 @@ export class Vector3TransformBlock extends NodeMaterialBlock {
         super._buildBlock(state);
 
         let output = this._outputs[0];
-        let vector = this._inputs[0];
-        let transform = this._inputs[1];
+        let vector = this.vector;
+        let transform = this.transform;
 
         state.compilationString += this._declareOutput(output, state) + ` = ${transform.associatedVariableName} * vec4(${vector.associatedVariableName}, ${this.complement});\r\n`;
 

+ 17 - 2
src/Materials/Node/Blocks/vector4TransformBlock.ts

@@ -2,6 +2,7 @@ import { NodeMaterialBlock } from '../nodeMaterialBlock';
 import { NodeMaterialBlockConnectionPointTypes } from '../nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
 import { NodeMaterialBlockTargets } from '../nodeMaterialBlockTargets';
+import { NodeMaterialConnectionPoint } from '../nodeMaterialBlockConnectionPoint';
 
 /**
  * Block used to transform a vector4 with a matrix
@@ -28,12 +29,26 @@ export class Vector4TransformBlock extends NodeMaterialBlock {
         return "Vector4TransformBlock";
     }
 
+    /**
+     * Gets the vector input
+     */
+    public get vector(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
+    /**
+     * Gets the matrix transform input
+     */
+    public get transform(): NodeMaterialConnectionPoint {
+        return this._inputs[1];
+    }    
+
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
         let output = this._outputs[0];
-        let vector = this._inputs[0];
-        let transform = this._inputs[1];
+        let vector = this.vector;
+        let transform = this.transform;
 
         state.compilationString += this._declareOutput(output, state) + ` = ${transform.associatedVariableName} * ${vector.associatedVariableName};\r\n`;
 

+ 26 - 3
src/Materials/Node/nodeMaterial.ts

@@ -17,6 +17,25 @@ import { SubMesh } from '../../Meshes/subMesh';
 import { MaterialDefines } from '../../Materials/materialDefines';
 import { MaterialHelper } from '../../Materials/materialHelper';
 
+
+/** @hidden */
+export class NodeMaterialDefines extends MaterialDefines {
+
+    /** MISC */
+    public FOG = false;
+
+    /** Bones */
+    public NUM_BONE_INFLUENCERS = 0;
+    public BonesPerMesh = 0;
+    public BONETEXTURE = false;
+
+
+    constructor() {
+        super();
+        this.rebuild();
+    }
+}
+
 /**
  * Class used to configure NodeMaterial
  */
@@ -313,7 +332,7 @@ export class NodeMaterial extends PushMaterial {
         }
 
         if (!subMesh._materialDefines) {
-            subMesh._materialDefines = new MaterialDefines();
+            subMesh._materialDefines = new NodeMaterialDefines();
         }
 
         var scene = this.getScene();
@@ -333,9 +352,13 @@ export class NodeMaterial extends PushMaterial {
             }
         }
 
-        // Bones
-        MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true, false, false);
+        // Shared defines
+        MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true, false, false);  
+        this._sharedData.blocksWithDefines.forEach(b => {
+            b.prepareDefines(mesh, this, defines);
+        })
 
+        // Need to recompile?
         if (defines.isDirty) {
             defines.markAsProcessed();
             // Uniforms

+ 12 - 0
src/Materials/Node/nodeMaterialBlock.ts

@@ -6,6 +6,8 @@ import { NodeMaterialBlockTargets } from './nodeMaterialBlockTargets';
 import { Effect, EffectFallbacks } from '../effect';
 import { AbstractMesh } from '../../Meshes/abstractMesh';
 import { Mesh } from '../../Meshes/mesh';
+import { MaterialDefines } from '../materialDefines';
+import { NodeMaterial } from './nodeMaterial';
 
 /**
  * Defines a block that can be used inside a node based material
@@ -286,6 +288,16 @@ export class NodeMaterialBlock {
     }
 
     /**
+     * Update defines for shader compilation
+     * @param mesh defines the mesh to be rendered
+     * @param nodeMaterial defines the node material requesting the update
+     * @param defines defines the material defines to update\
+     */
+    public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: MaterialDefines) {
+        // Do nothing
+    }
+
+    /**
      * Compile the current node and generate the shader code
      * @param state defines the current compilation state (uniforms, samplers, current string)
      * @returns the current block

+ 18 - 8
src/Materials/Node/nodeMaterialBlockConnectionPoint.ts

@@ -259,15 +259,20 @@ export class NodeMaterialConnectionPoint {
      * @param worldViewProjection defines the worldxviewxprojection matrix
      */
     public transmitWorld(effect: Effect, world: Matrix, worldView: Matrix, worldViewProjection: Matrix) {
+        if (!this._wellKnownValue) { 
+            return;
+        }
+        
+        let variableName = this.associatedVariableName;
         switch (this._wellKnownValue) {
             case NodeMaterialWellKnownValues.World:
-                effect.setMatrix("world", world);
+                effect.setMatrix(variableName, world);
                 break;
             case NodeMaterialWellKnownValues.WorldView:
-                effect.setMatrix("worldView", worldView);
+                effect.setMatrix(variableName, worldView);
                 break;
             case NodeMaterialWellKnownValues.WorldViewProjection:
-                effect.setMatrix("worldViewProjection", worldViewProjection);
+                effect.setMatrix(variableName, worldViewProjection);
                 break;
         }
     }
@@ -279,21 +284,26 @@ export class NodeMaterialConnectionPoint {
      */
     public transmit(effect: Effect, scene: Scene) {
         if (this._wellKnownValue) {
+            let variableName = this.associatedVariableName;
             switch (this._wellKnownValue) {
+                case NodeMaterialWellKnownValues.World:
+                case NodeMaterialWellKnownValues.WorldView:
+                case NodeMaterialWellKnownValues.WorldViewProjection:
+                    return;
                 case NodeMaterialWellKnownValues.View:
-                    effect.setMatrix("world", scene.getViewMatrix());
+                    effect.setMatrix(variableName, scene.getViewMatrix());
                     break;
                 case NodeMaterialWellKnownValues.Projection:
-                    effect.setMatrix("world", scene.getProjectionMatrix());
+                    effect.setMatrix(variableName, scene.getProjectionMatrix());
                     break;
                 case NodeMaterialWellKnownValues.ViewProjection:
-                    effect.setMatrix("world", scene.getTransformMatrix());
+                    effect.setMatrix(variableName, scene.getTransformMatrix());
                     break;
                 case NodeMaterialWellKnownValues.FogColor:
-                    effect.setColor3("fogColor", scene.fogColor);
+                    effect.setColor3(variableName, scene.fogColor);
                     break;
                 case NodeMaterialWellKnownValues.FogParameters:
-                    effect.setFloat4("fogParameters", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
+                    effect.setFloat4(variableName, scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
                     break;
             }
             return;

+ 4 - 3
src/Materials/Node/nodeMaterialBuildState.ts

@@ -114,6 +114,7 @@ export class NodeMaterialBuildState {
             case NodeMaterialBlockConnectionPointTypes.Color3:
             case NodeMaterialBlockConnectionPointTypes.Vector3:
             case NodeMaterialBlockConnectionPointTypes.Vector3OrColor3:
+            case NodeMaterialBlockConnectionPointTypes.Color3OrColor4:
                 return "vec3";
             case NodeMaterialBlockConnectionPointTypes.Color4:
             case NodeMaterialBlockConnectionPointTypes.Vector4:
@@ -261,9 +262,9 @@ export class NodeMaterialBuildState {
                         hints.needWorldViewProjectionMatrix = true;
                         break;
                 }
-            } else {
-                this.sharedData.uniformConnectionPoints.push(point);
-            }
+            } 
+        
+            this.sharedData.uniformConnectionPoints.push(point);                    
 
             return;
         }

+ 5 - 0
src/Materials/Node/nodeMaterialBuildStateSharedData.ts

@@ -31,6 +31,11 @@ export class NodeMaterialBuildStateSharedData {
     public blocksWithFallbacks = new Array<NodeMaterialBlock>();
 
     /**
+     * List of blocks that can provide a define update
+     */
+    public blocksWithDefines = new Array<NodeMaterialBlock>();
+
+    /**
      * Build Id used to avoid multiple recompilations
      */
     public buildId: number;

+ 8 - 8
src/Materials/Node/nodeMaterialWellKnownValues.ts

@@ -3,19 +3,19 @@
  */
 export enum NodeMaterialWellKnownValues {
     /** World */
-    World,
+    World = 1,
     /** View */
-    View,
+    View = 2,
     /** Projection */
-    Projection,
+    Projection = 3,
     /** ViewProjection */
-    ViewProjection,
+    ViewProjection = 4,
     /** WorldView */
-    WorldView,
+    WorldView = 5,
     /** WorldViewProjection */
-    WorldViewProjection,
+    WorldViewProjection = 6,
     /** Fog color */
-    FogColor,
+    FogColor = 7,
     /** Fog parameters */
-    FogParameters
+    FogParameters = 8
 }

+ 11 - 1
src/Materials/materialHelper.ts

@@ -80,6 +80,16 @@ export class MaterialHelper {
     }
 
     /**
+     * Gets the current status of the fog (should it be enabled?)
+     * @param mesh defines the mesh to evaluate for fog support
+     * @param scene defines the hosting scene
+     * @returns true if fog must be enabled
+     */
+    public static GetFogState(mesh: AbstractMesh, scene: Scene) {
+        return (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE);
+    }
+
+    /**
      * Helper used to prepare the list of defines associated with misc. values for shader compilation
      * @param mesh defines the current mesh
      * @param scene defines the current scene
@@ -93,7 +103,7 @@ export class MaterialHelper {
         if (defines._areMiscDirty) {
             defines["LOGARITHMICDEPTH"] = useLogarithmicDepth;
             defines["POINTSIZE"] = pointsCloud;
-            defines["FOG"] = (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && fogEnabled);
+            defines["FOG"] = fogEnabled && this.GetFogState(mesh, scene);
             defines["NONUNIFORMSCALING"] = mesh.nonUniformScaling;
             defines["ALPHATEST"] = alphaTest;
         }