Browse Source

Fix varyings

David Catuhe 6 years ago
parent
commit
b7b7438771

+ 106 - 0
localDev/src/webgl-debug.js

@@ -95,3 +95,109 @@ var createScene = function() {
     return scene;
 
 };
+
+/////////////////////////
+
+
+var createNodeMaterial = function(scene) {
+    var nodeMaterial = new BABYLON.NodeMaterial("node", scene, { emitComments: true });
+    // nodeMaterial.setToDefault();
+    // Blocks
+  
+    // Vertex
+    var positionInput = new BABYLON.InputBlock("position");
+    positionInput.setAsAttribute("position");
+  
+    var worldInput = new BABYLON.InputBlock("world");
+    worldInput.setAsWellKnownValue(BABYLON.NodeMaterialWellKnownValues.World);
+  
+    var worldPos = new BABYLON.Vector4TransformBlock("worldPos");
+    positionInput.connectTo(worldPos);
+    worldInput.connectTo(worldPos);
+  
+    var normalInput = new BABYLON.InputBlock("normal");
+    normalInput.setAsAttribute("normal");
+  
+    var worldNormal = new BABYLON.Vector4TransformBlock("worldNormal");
+    normalInput.connectTo(worldNormal);
+    worldInput.connectTo(worldNormal);
+  
+    var viewProjectionInput = new BABYLON.InputBlock("viewProjection");
+    viewProjectionInput.setAsWellKnownValue(BABYLON.NodeMaterialWellKnownValues.ViewProjection);
+  
+    var worldPosdMultipliedByViewProjection = new BABYLON.Vector4TransformBlock("worldPos * viewProjectionTransform");
+    worldPos.connectTo(worldPosdMultipliedByViewProjection);
+    viewProjectionInput.connectTo(worldPosdMultipliedByViewProjection);
+  
+    var vertexOutput = new BABYLON.VertexOutputBlock("vertexOutput");
+    worldPosdMultipliedByViewProjection.connectTo(vertexOutput);
+  
+    // Pixel
+    var colorInput = new BABYLON.InputBlock("color");
+    colorInput.value = new BABYLON.Color4(1, 0, 0, 1);
+  
+    var colorMultiplier2 = new BABYLON.MultiplyBlock("color multiplier2");
+    
+    var diffuseTextureBlock = new BABYLON.TextureBlock("diffuseTexture");
+    diffuseTextureBlock.texture = new BABYLON.Texture("/playground/textures/bloc.jpg");
+  
+    diffuseTextureBlock.connectTo(colorMultiplier2);
+    colorInput.connectTo(colorMultiplier2);
+  
+    var pixelOutput = new BABYLON.FragmentOutputBlock("pixelOutput");
+    colorMultiplier2.connectTo(pixelOutput);
+  
+    // Add to nodes
+    nodeMaterial.addOutputNode(vertexOutput);
+    nodeMaterial.addOutputNode(pixelOutput);
+  
+    // Build
+    nodeMaterial.build(true);
+  
+    scene.debugLayer.show();
+    scene.debugLayer.select(nodeMaterial);
+  
+    return nodeMaterial;
+  }
+  
+  var createScene = function() {
+  
+    // This creates a basic Babylon Scene object (non-mesh)
+    var scene = new BABYLON.Scene(engine);
+  
+    // This creates and positions a free camera (non-mesh)
+    var camera = new BABYLON.ArcRotateCamera("camera1", 1.14, 1.13, 10, BABYLON.Vector3.Zero(), scene);
+  
+    // This targets the camera to scene origin
+    camera.setTarget(BABYLON.Vector3.Zero());
+  
+    // This attaches the camera to the canvas
+    camera.attachControl(canvas, true);
+  
+    // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
+    var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
+  
+    // Default intensity is 1. Let's dim the light a small amount
+    light.intensity = 0.7;
+  
+    var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, -11, 5), scene);
+  
+    // Default intensity is 1. Let's dim the light a small amount
+    light2.intensity = 0.7;
+  
+    // Our built-in 'sphere' shape. Params: name, subdivs, size, scene
+    var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);
+  
+    sphere.material = createNodeMaterial(scene);
+  
+  
+    return scene;
+  
+  };
+  
+
+  // Must test:
+  - fog
+  - morph
+  - bones
+  - multi textures

+ 0 - 1
src/Materials/Node/Blocks/Dual/fogBlock.ts

@@ -102,7 +102,6 @@ export class FogBlock extends NodeMaterialBlock {
             fogParametersInput.setAsWellKnownValue(NodeMaterialWellKnownValues.Automatic);
             fogParametersInput.output.connectTo(this.fogParameters);
         }
-        this._outputs[0]._needToEmitVarying = true;
     }
 
     public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {

+ 3 - 3
src/Materials/Node/Blocks/Dual/lightBlock.ts

@@ -141,10 +141,10 @@ export class LightBlock extends NodeMaterialBlock {
 
         // Inject code in vertex
         let worldPosVaryingName = "v_" + worldPos.associatedVariableName;
-        state._emitVaryings(worldPos, undefined, true, false, worldPosVaryingName, NodeMaterialBlockConnectionPointTypes.Vector3);
+        state._emitVaryingFromString(worldPosVaryingName, "vec3");
 
-        let worldNormalVaryingName = "v_" + worldNormal.associatedVariableName;
-        state._emitVaryings(worldNormal, undefined, true, false, worldNormalVaryingName, NodeMaterialBlockConnectionPointTypes.Vector3);
+        let worldNormalVaryingName = "v_" + worldNormal.associatedVariableName;     
+        state._emitVaryingFromString(worldNormalVaryingName, "vec3");
 
         state.compilationString += `${worldPosVaryingName} = ${worldPos.associatedVariableName}.xyz;\r\n`;
         state.compilationString += `${worldNormalVaryingName} = ${worldNormal.associatedVariableName}.xyz;\r\n`;

+ 14 - 16
src/Materials/Node/Blocks/Fragment/textureBlock.ts

@@ -15,10 +15,12 @@ import { Mesh } from '../../../../Meshes/mesh';
  */
 export class TextureBlock extends NodeMaterialBlock {
     private _defineName: string;
+    private _mainUVDefineName: string;
     private _samplerName: string;
     private _transformedUVName: string;
     private _textureTransformName: string;
     private _textureInfoName: string;
+    private _mainUVName: string;
 
     /**
      * Gets or sets the texture associated with the node
@@ -32,12 +34,9 @@ export class TextureBlock extends NodeMaterialBlock {
     public constructor(name: string) {
         super(name, NodeMaterialBlockTargets.Fragment);
 
-        this.registerInput("uv", NodeMaterialBlockConnectionPointTypes.Vector2);
+        this.registerInput("uv", NodeMaterialBlockConnectionPointTypes.Vector2, false);
 
         this.registerOutput("color", NodeMaterialBlockConnectionPointTypes.Color4);
-
-        // Setup
-        this._inputs[0]._needToEmitVarying = false;
     }
 
     /**
@@ -75,15 +74,12 @@ export class TextureBlock extends NodeMaterialBlock {
             return;
         }
 
-        let uvInput = this.uv;
-        let mainUVName = ("vMain" + uvInput.associatedVariableName).toUpperCase();
-
         if (!this.texture.getTextureMatrix().isIdentityAs3x2()) {
             defines.setValue(this._defineName, true);
-            defines.setValue(mainUVName, false);
+            defines.setValue(this._mainUVDefineName, false);
         } else {
             defines.setValue(this._defineName, false);
-            defines.setValue(mainUVName, true);
+            defines.setValue(this._mainUVDefineName, true);
         }
     }
 
@@ -110,19 +106,17 @@ export class TextureBlock extends NodeMaterialBlock {
 
         // Inject code in vertex
         this._defineName = state._getFreeDefineName("UVTRANSFORM");
-        let mainUVName = "vMain" + uvInput.associatedVariableName;
+        this._mainUVDefineName = ("vMain" + uvInput.associatedVariableName).toUpperCase();
 
+        this._mainUVName = "vMain" + uvInput.associatedVariableName;
         this._transformedUVName = state._getFreeVariableName("transformedUV");
         this._textureTransformName = state._getFreeVariableName("textureTransform");
         this._textureInfoName = state._getFreeVariableName("textureInfoName");
 
         state._emitVaryingFromString(this._transformedUVName, "vec2", this._defineName);
+        state._emitVaryingFromString(this._mainUVName, "vec2", this._mainUVDefineName);
 
-        state.uniforms.push(this._textureTransformName);
-        state.uniforms.push(this._textureInfoName);
-
-        state.compilationString += `mat4 ${this._textureTransformName};\r\n`;
-        state.compilationString += `float ${this._textureInfoName};\r\n`;
+        state._emitUniformFromString(this._transformedUVName, "mat4", this._defineName)
 
         if (state.sharedData.emitComments) {
             state.compilationString += `\r\n//${this.name}\r\n`;
@@ -130,7 +124,7 @@ export class TextureBlock extends NodeMaterialBlock {
         state.compilationString += `#ifdef ${this._defineName}\r\n`;
         state.compilationString += `${this._transformedUVName} = vec2(${this._textureTransformName} * vec4(${uvInput.associatedVariableName}, 1.0, 0.0));\r\n`;
         state.compilationString += `#else\r\n`;
-        state.compilationString += `${mainUVName} = ${uvInput.associatedVariableName};\r\n`;
+        state.compilationString += `${this._mainUVName} = ${uvInput.associatedVariableName};\r\n`;
         state.compilationString += `#endif\r\n`;
     }
 
@@ -141,12 +135,16 @@ export class TextureBlock extends NodeMaterialBlock {
 
         this._samplerName = state._getFreeVariableName(this.name + "Sampler");
         state.samplers.push(this._samplerName);
+        state._samplerDeclaration += `uniform sampler2D ${this._samplerName};\r\n`;
 
         // Vertex
         this._injectVertexCode(state._vertexState);
 
         // Fragment
         state.sharedData.blocksWithDefines.push(this);
+        state.sharedData.bindableBlocks.push(this);
+
+        state._emitUniformFromString(this._textureInfoName, "float")
 
         let uvInput = this.uv;
         let output = this._outputs[0];

+ 10 - 11
src/Materials/Node/Blocks/Input/inputBlock.ts

@@ -80,10 +80,11 @@ export class InputBlock extends NodeMaterialBlock {
     /**
      * Creates a new InputBlock
      * @param name defines the block name
+     * @param target defines the target of that block (Vertex by default)
      * @param type defines the type of the input (can be set to NodeMaterialBlockConnectionPointTypes.AutoDetect)
      */
-    public constructor(name: string, type: NodeMaterialBlockConnectionPointTypes = NodeMaterialBlockConnectionPointTypes.AutoDetect) {
-        super(name, undefined, false, true);
+    public constructor(name: string, target = NodeMaterialBlockTargets.Vertex, type: NodeMaterialBlockConnectionPointTypes = NodeMaterialBlockConnectionPointTypes.AutoDetect) {
+        super(name, target, false, true);
 
         this._type = type;
 
@@ -241,8 +242,7 @@ export class InputBlock extends NodeMaterialBlock {
         return `#ifdef ${define}\r\n`;
     }
 
-    /** @hidden */
-    public _emit(state: NodeMaterialBuildState, define?: string) {
+    private _emit(state: NodeMaterialBuildState, define?: string) {
         // Uniforms
         if (this.isUniform) {
             if (!this.associatedVariableName) {
@@ -282,13 +282,8 @@ export class InputBlock extends NodeMaterialBlock {
         if (this.isAttribute) {
             this.associatedVariableName = this.name;
 
-            if (this.target === NodeMaterialBlockTargets.Fragment) { // Attribute for fragment need to be carried over by varyings
+            if (this.target === NodeMaterialBlockTargets.Vertex && state._vertexState) { // Attribute for fragment need to be carried over by varyings
                 this._emit(state._vertexState, define);
-
-                // if (this._needToEmitVarying) {
-                //     state._vertexState._emitVaryings(point, undefined, true, true, "v_" + this.associatedVariableName);
-                //     this.associatedVariableName = "v_" + this.associatedVariableName;
-                // }
                 return;
             }
 
@@ -397,6 +392,10 @@ export class InputBlock extends NodeMaterialBlock {
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
-        state.sharedData.inputBlocks.push(this);
+        if (this.isUniform || this.isWellKnownValue) {
+            state.sharedData.inputBlocks.push(this);
+        }
+
+        this._emit(state);
     }
 }

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

@@ -276,10 +276,6 @@ export class NodeMaterialBlock {
         // Empty. Must be defined by child nodes
     }
 
-    protected _emit(state: NodeMaterialBuildState, define?: string) {
-        // Empty. Must be defined by child nodes
-    }
-
     /**
      * Add uniforms, samplers and uniform buffers at compilation time
      * @param state defines the state to update
@@ -382,21 +378,19 @@ export class NodeMaterialBlock {
             console.log(`${state.target === NodeMaterialBlockTargets.Vertex ? "Vertex shader" : "Fragment shader"}: Building ${this.name} [${this.getClassName()}]`);
         }
 
+        if (!this.isInput) {
+            /** Prepare outputs */
+            for (var output of this._outputs) {
+                if ((output.target & this.target!) === 0) {
+                    continue;
+                }
+                if ((output.target & state.target!) === 0) {
+                    continue;
+                }
 
-        /** Emit input blocks */
-        this._emit(state);
-
-        /** Prepare outputs */
-        for (var output of this._outputs) {
-            if ((output.target & this.target!) === 0) {
-                continue;
-            }
-            if ((output.target & state.target!) === 0) {
-                continue;
-            }
-
-            if (!output.associatedVariableName) {
-                output.associatedVariableName = state._getFreeVariableName(output.name);
+                if (!output.associatedVariableName) {
+                    output.associatedVariableName = state._getFreeVariableName(output.name);
+                }
             }
         }
 

+ 0 - 3
src/Materials/Node/nodeMaterialBlockConnectionPoint.ts

@@ -20,9 +20,6 @@ export class NodeMaterialConnectionPoint {
     /** @hidden */
     public _typeConnectionSource: Nullable<NodeMaterialConnectionPoint> = null;
 
-    /** @hidden */
-    public _needToEmitVarying = true;
-
     private _type = NodeMaterialBlockConnectionPointTypes.Float;
 
     /**

+ 18 - 30
src/Materials/Node/nodeMaterialBuildState.ts

@@ -275,36 +275,6 @@ export class NodeMaterialBuildState {
     }
 
     /** @hidden */
-    public _emitVaryings(point: NodeMaterialConnectionPoint, define: string = "", force = false, fromFragment = false, replacementName: string = "", type: Nullable<NodeMaterialBlockConnectionPointTypes> = null) {
-        let name = replacementName || point.associatedVariableName;
-        if (point._needToEmitVarying || force) {
-            if (this.sharedData.varyings.indexOf(name) !== -1) {
-                return;
-            }
-
-            this.sharedData.varyings.push(name);
-
-            if (define) {
-                this.sharedData.varyingDeclaration += `#ifdef ${define}\r\n`;
-            }
-            this.sharedData.varyingDeclaration += `varying ${this._getGLType(point.type)} ${name};\r\n`;
-            if (define) {
-                this.sharedData.varyingDeclaration += `#endif\r\n`;
-            }
-
-            if (this.target === NodeMaterialBlockTargets.Vertex && fromFragment) {
-                if (define) {
-                    this.sharedData.varyingDeclaration += `#ifdef ${define}\r\n`;
-                }
-                this._varyingTransfer += `${name} = ${point.name};\r\n`;
-                if (define) {
-                    this.sharedData.varyingDeclaration += `#endif\r\n`;
-                }
-            }
-        }
-    }
-
-    /** @hidden */
     public _emitVaryingFromString(name: string, type: string, define: string = "") {
         if (this.sharedData.varyings.indexOf(name) !== -1) {
             return;
@@ -320,4 +290,22 @@ export class NodeMaterialBuildState {
             this.sharedData.varyingDeclaration += `#endif\r\n`;
         }
     }
+
+    
+    /** @hidden */
+    public _emitUniformFromString(name: string, type: string, define: string = "") {
+        if (this.uniforms.indexOf(name) !== -1) {
+            return;
+        }
+
+        this.uniforms.push(name);
+        
+        if (define) {
+            this._uniformDeclaration += `#ifdef ${define}\r\n`;
+        }
+        this._uniformDeclaration += `uniform ${type} ${name};\r\n`;
+        if (define) {
+            this._uniformDeclaration += `#endif\r\n`;
+        }
+    }
 }