瀏覽代碼

Merge pull request #7980 from Popov72/custommat-fix

Fix some bugs on custom materials
David Catuhe 5 年之前
父節點
當前提交
ac24fbfcd0
共有 2 個文件被更改,包括 62 次插入54 次删除
  1. 31 27
      materialsLibrary/src/custom/customMaterial.ts
  2. 31 27
      materialsLibrary/src/custom/pbrCustomMaterial.ts

+ 31 - 27
materialsLibrary/src/custom/customMaterial.ts

@@ -57,49 +57,53 @@ export class CustomMaterial extends StandardMaterial {
     _createdShaderName: string;
     _customUniform: string[];
     _newUniforms: string[];
-    _newUniformInstances: any[];
-    _newSamplerInstances: Texture[];
+    _newUniformInstances: { [name: string]: any };
+    _newSamplerInstances: { [name: string]: Texture };
     _customAttributes: string[];
 
     public FragmentShader: string;
     public VertexShader: string;
 
     public AttachAfterBind(mesh: Mesh, effect: Effect) {
-         for (var el in this._newUniformInstances) {
-            var ea = el.toString().split('-');
-            if (ea[0] == 'vec2') {
-                effect.setVector2(ea[1], this._newUniformInstances[el]);
-            }
-            else if (ea[0] == 'vec3') {
-                effect.setVector3(ea[1], this._newUniformInstances[el]);
-            }
-            else if (ea[0] == 'vec4') {
-                effect.setVector4(ea[1], this._newUniformInstances[el]);
-            }
-            else if (ea[0] == 'mat4') {
-                effect.setMatrix(ea[1], this._newUniformInstances[el]);
-            }
-            else if (ea[0] == 'float') {
-                effect.setFloat(ea[1], this._newUniformInstances[el]);
+        if (this._newUniformInstances) {
+            for (let el in this._newUniformInstances) {
+                const ea = el.toString().split('-');
+                if (ea[0] == 'vec2') {
+                    effect.setVector2(ea[1], this._newUniformInstances[el]);
+                }
+                else if (ea[0] == 'vec3') {
+                    effect.setVector3(ea[1], this._newUniformInstances[el]);
+                }
+                else if (ea[0] == 'vec4') {
+                    effect.setVector4(ea[1], this._newUniformInstances[el]);
+                }
+                else if (ea[0] == 'mat4') {
+                    effect.setMatrix(ea[1], this._newUniformInstances[el]);
+                }
+                else if (ea[0] == 'float') {
+                    effect.setFloat(ea[1], this._newUniformInstances[el]);
+                }
             }
         }
-        for (var el in this._newSamplerInstances) {
-            var ea = el.toString().split('-');
-            if (ea[0] == 'sampler2D' && this._newSamplerInstances[el].isReady && this._newSamplerInstances[el].isReady()) {
-                effect.setTexture(ea[1], this._newSamplerInstances[el]);
+        if (this._newSamplerInstances) {
+            for (let el in this._newSamplerInstances) {
+                const ea = el.toString().split('-');
+                if (ea[0] == 'sampler2D' && this._newSamplerInstances[el].isReady && this._newSamplerInstances[el].isReady()) {
+                    effect.setTexture(ea[1], this._newSamplerInstances[el]);
+                }
             }
         }
     }
 
     public ReviewUniform(name: string, arr: string[]): string[] {
-        if (this._newUniforms && name == "uniform") {
+        if (name == "uniform" && this._newUniforms) {
             for (var ind = 0; ind < this._newUniforms.length ; ind ++) {
                 if (this._customUniform[ind].indexOf('sampler') == -1) {
                     arr.push(this._newUniforms[ind]);
                 }
             }
         }
-        if (this._newUniforms && name == "sampler") {
+        if (name == "sampler" && this._newUniforms) {
             for (var ind = 0; ind < this._newUniforms.length ; ind ++) {
                 if (this._customUniform[ind].indexOf('sampler') != -1) {
                     arr.push(this._newUniforms[ind]);
@@ -177,12 +181,12 @@ export class CustomMaterial extends StandardMaterial {
         if (!this._customUniform) {
             this._customUniform = new Array();
             this._newUniforms = new Array();
-            this._newSamplerInstances = new Array();
-            this._newUniformInstances = new Array();
+            this._newSamplerInstances = {};
+            this._newUniformInstances = {};
         }
         if (param) {
             if (kind.indexOf("sampler") == -1) {
-                (<any>this._newUniformInstances)[kind + "-" + name] = param;
+                (<any>this._newSamplerInstances)[kind + "-" + name] = param;
             }
             else {
                 (<any>this._newUniformInstances)[kind + "-" + name] = param;

+ 31 - 27
materialsLibrary/src/custom/pbrCustomMaterial.ts

@@ -53,49 +53,53 @@ export class PBRCustomMaterial extends PBRMaterial {
     _createdShaderName: string;
     _customUniform: string[];
     _newUniforms: string[];
-    _newUniformInstances: any[];
-    _newSamplerInstances: Texture[];
+    _newUniformInstances: { [name: string]: any };
+    _newSamplerInstances: { [name: string]: Texture };
     _customAttributes: string[];
 
     public FragmentShader: string;
     public VertexShader: string;
 
     public AttachAfterBind(mesh: Mesh, effect: Effect) {
-        for (var el in this._newUniformInstances) {
-            var ea = el.toString().split('-');
-            if (ea[0] == 'vec2') {
-                effect.setVector2(ea[1], this._newUniformInstances[el]);
-            }
-            else if (ea[0] == 'vec3') {
-                effect.setVector3(ea[1], this._newUniformInstances[el]);
-            }
-            else if (ea[0] == 'vec4') {
-                effect.setVector4(ea[1], this._newUniformInstances[el]);
-            }
-            else if (ea[0] == 'mat4') {
-                effect.setMatrix(ea[1], this._newUniformInstances[el]);
-            }
-            else if (ea[0] == 'float') {
-                effect.setFloat(ea[1], this._newUniformInstances[el]);
+        if (this._newUniformInstances) {
+            for (let el in this._newUniformInstances) {
+                const ea = el.toString().split('-');
+                if (ea[0] == 'vec2') {
+                    effect.setVector2(ea[1], this._newUniformInstances[el]);
+                }
+                else if (ea[0] == 'vec3') {
+                    effect.setVector3(ea[1], this._newUniformInstances[el]);
+                }
+                else if (ea[0] == 'vec4') {
+                    effect.setVector4(ea[1], this._newUniformInstances[el]);
+                }
+                else if (ea[0] == 'mat4') {
+                    effect.setMatrix(ea[1], this._newUniformInstances[el]);
+                }
+                else if (ea[0] == 'float') {
+                    effect.setFloat(ea[1], this._newUniformInstances[el]);
+                }
             }
         }
-        for (var el in this._newSamplerInstances) {
-            var ea = el.toString().split('-');
-            if (ea[0] == 'sampler2D' && this._newSamplerInstances[el].isReady && this._newSamplerInstances[el].isReady()) {
-                effect.setTexture(ea[1], this._newSamplerInstances[el]);
+        if (this._newSamplerInstances) {
+            for (let el in this._newSamplerInstances) {
+                const ea = el.toString().split('-');
+                if (ea[0] == 'sampler2D' && this._newSamplerInstances[el].isReady && this._newSamplerInstances[el].isReady()) {
+                    effect.setTexture(ea[1], this._newSamplerInstances[el]);
+                }
             }
         }
     }
 
     public ReviewUniform(name: string, arr: string[]): string[] {
-        if (this._newUniforms && name == "uniform") {
+        if (name == "uniform" && this._newUniforms) {
             for (var ind = 0; ind < this._newUniforms.length ; ind ++) {
                 if (this._customUniform[ind].indexOf('sampler') == -1) {
                     arr.push(this._newUniforms[ind]);
                 }
             }
         }
-        if (this._newUniforms && name == "sampler") {
+        if (name == "sampler" && this._newUniforms) {
             for (var ind = 0; ind < this._newUniforms.length ; ind ++) {
                 if (this._customUniform[ind].indexOf('sampler') != -1) {
                     arr.push(this._newUniforms[ind]);
@@ -175,12 +179,12 @@ export class PBRCustomMaterial extends PBRMaterial {
         if (!this._customUniform) {
             this._customUniform = new Array();
             this._newUniforms = new Array();
-            this._newSamplerInstances = new Array();
-            this._newUniformInstances = new Array();
+            this._newSamplerInstances = {};
+            this._newUniformInstances = {};
         }
         if (param) {
             if (kind.indexOf("sampler") == -1) {
-                (<any>this._newUniformInstances)[kind + "-" + name] = param;
+                (<any>this._newSamplerInstances)[kind + "-" + name] = param;
             }
             else {
                 (<any>this._newUniformInstances)[kind + "-" + name] = param;