Browse Source

Support shadows - #6012

David Catuhe 6 years ago
parent
commit
d2ee54437a

+ 2 - 2
src/Engines/engine.ts

@@ -3379,7 +3379,7 @@ export class Engine {
             if (!this._gl.getShaderParameter(vertexShader, this._gl.COMPILE_STATUS)) {
                 let log = this._gl.getShaderInfoLog(vertexShader);
                 if (log) {
-                    throw new Error(log);
+                    throw new Error("VERTEX SHADER " + log);
                 }
             }
 
@@ -3387,7 +3387,7 @@ export class Engine {
             if (!this._gl.getShaderParameter(fragmentShader, this._gl.COMPILE_STATUS)) {
                 let log = this._gl.getShaderInfoLog(fragmentShader);
                 if (log) {
-                    throw new Error(log);
+                    throw new Error("FRAGMENT SHADER " + log);
                 }
             }
 

+ 42 - 7
src/Materials/Node/Blocks/Dual/lightBlock.ts

@@ -145,6 +145,26 @@ export class LightBlock extends NodeMaterialBlock {
         let worldPos = this.worldPosition;
         let worldNormal = this.worldNormal;
 
+        let comments = `//${this.name}`;
+
+        // Declaration
+        if (!this.light) { // Emit for all lights
+            state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
+                repeatKey: "maxSimultaneousLights"
+            });
+            this._lightId = 0;
+
+            state.sharedData.dynamicUniformBlocks.push(this);
+        } else {
+
+            this._lightId = (state.counters["lightCounter"] !== undefined ? state.counters["lightCounter"] : -1) + 1;
+            state.counters["lightCounter"] = this._lightId;
+
+            state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
+                replaceStrings: [{ search: /{X}/g, replace: this._lightId.toString() }]
+            }, this._lightId.toString());
+        }        
+
         // Inject code in vertex
         let worldPosVaryingName = "v_" + worldPos.associatedVariableName;
         state._emitVaryingFromString(worldPosVaryingName, "vec3");
@@ -152,6 +172,20 @@ export class LightBlock extends NodeMaterialBlock {
         let worldNormalVaryingName = "v_" + worldNormal.associatedVariableName;
         state._emitVaryingFromString(worldNormalVaryingName, "vec3");
 
+        if (this.light) {
+            state.compilationString += state._emitCodeFromInclude("shadowsVertex", comments, {
+                replaceStrings: [
+                    { search: /{X}/g, replace: this._lightId.toString() },
+                    { search: /worldPos/g, replace: worldPos.associatedVariableName }
+                ]
+            });
+        } else {            
+            state.compilationString += `vec4 worldPos = ${worldPos.associatedVariableName};\r\n`;
+            state.compilationString += state._emitCodeFromInclude("shadowsVertex", comments, {
+                repeatKey: "maxSimultaneousLights"
+            });
+        }        
+
         state.compilationString += `${worldPosVaryingName} = ${worldPos.associatedVariableName}.xyz;\r\n`;
         state.compilationString += `${worldNormalVaryingName} = ${worldNormal.associatedVariableName}.xyz;\r\n`;
     }
@@ -172,6 +206,8 @@ export class LightBlock extends NodeMaterialBlock {
 
         let comments = `//${this.name}`;
         let worldPos = this.worldPosition;
+        
+        state._emitFunctionFromInclude("helperFunctions", comments);
 
         state._emitFunctionFromInclude("lightsFragmentFunctions", comments, {
             replaceStrings: [
@@ -179,18 +215,17 @@ export class LightBlock extends NodeMaterialBlock {
             ]
         });
 
+        state._emitFunctionFromInclude("shadowsFragmentFunctions", comments, {
+            replaceStrings: [
+                { search: /vPositionW/g, replace: "v_" + worldPos.associatedVariableName }
+            ]
+        });
+
         if (!this.light) { // Emit for all lights
             state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
                 repeatKey: "maxSimultaneousLights"
             });
-            this._lightId = 0;
-
-            state.sharedData.dynamicUniformBlocks.push(this);
         } else {
-
-            this._lightId = (state.counters["lightCounter"] !== undefined ? state.counters["lightCounter"] : -1) + 1;
-            state.counters["lightCounter"] = this._lightId;
-
             state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
                 replaceStrings: [{ search: /{X}/g, replace: this._lightId.toString() }]
             }, this._lightId.toString());

+ 4 - 0
src/Materials/Node/nodeMaterial.ts

@@ -779,6 +779,10 @@ export class NodeMaterial extends PushMaterial {
             return true;
         }
 
+        if (!this._sharedData) {
+            return false;
+        }
+
         for (var t of this._sharedData.textureBlocks) {
             if (t.texture === texture) {
                 return true;

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

@@ -113,6 +113,7 @@ export class NodeMaterialBuildStateSharedData {
         this.variableNames["matricesWeightsExtra"] = 0;
         this.variableNames["diffuseBase"] = 0;
         this.variableNames["specularBase"] = 0;
+        this.variableNames["worldPos"] = 0;
 
         // Exclude defines
         this.defineNames["MAINUV0"] = 0;