Browse Source

Merge pull request #7849 from Popov72/custommaterial-addatribute

Added AddAttribute to CustomMaterial and PBRCustomMaterial
David Catuhe 5 years ago
parent
commit
039f15aef9

+ 1 - 0
dist/preview release/what's new.md

@@ -52,6 +52,7 @@
 - Updated the energy conservation factor for the clear coat layer in PBR materials ([Popov72](https://github.com/Popov72))
 - Updated the energy conservation factor for the clear coat layer in PBR materials ([Popov72](https://github.com/Popov72))
 - Added the `transparencyMode` property to the `StandardMaterial` class ([Popov72](https://github.com/Popov72))
 - Added the `transparencyMode` property to the `StandardMaterial` class ([Popov72](https://github.com/Popov72))
 - Added to `FresnelParameters` constructor options and equals method ([brianzinn](https://github.com/brianzinn))
 - Added to `FresnelParameters` constructor options and equals method ([brianzinn](https://github.com/brianzinn))
+- Added `AddAttribute` to `CustomMaterial` and `PBRCustomMaterial` ([Popov72](https://github.com/Popov72))
 
 
 ### WebXR
 ### WebXR
 
 

+ 16 - 1
materialsLibrary/src/custom/customMaterial.ts

@@ -56,6 +56,7 @@ export class CustomMaterial extends StandardMaterial {
     _newUniforms: string[];
     _newUniforms: string[];
     _newUniformInstances: any[];
     _newUniformInstances: any[];
     _newSamplerInstances: Texture[];
     _newSamplerInstances: Texture[];
+    _customAttributes: string[];
 
 
     public FragmentShader: string;
     public FragmentShader: string;
     public VertexShader: string;
     public VertexShader: string;
@@ -105,7 +106,7 @@ export class CustomMaterial extends StandardMaterial {
         return arr;
         return arr;
     }
     }
 
 
-    public Builder(shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines): string {
+    public Builder(shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines, attributes?: string[]): string {
 
 
         if (this._isCreatedShader) {
         if (this._isCreatedShader) {
             return this._createdShaderName;
             return this._createdShaderName;
@@ -115,6 +116,10 @@ export class CustomMaterial extends StandardMaterial {
         CustomMaterial.ShaderIndexer++;
         CustomMaterial.ShaderIndexer++;
         var name: string = "custom_" + CustomMaterial.ShaderIndexer;
         var name: string = "custom_" + CustomMaterial.ShaderIndexer;
 
 
+        if (attributes && this._customAttributes && this._customAttributes.length > 0) {
+            attributes.push(...this._customAttributes);
+        }
+
         this.ReviewUniform("uniform", uniforms);
         this.ReviewUniform("uniform", uniforms);
         this.ReviewUniform("sampler", samplers);
         this.ReviewUniform("sampler", samplers);
 
 
@@ -182,6 +187,16 @@ export class CustomMaterial extends StandardMaterial {
         return this;
         return this;
     }
     }
 
 
+    public AddAttribute(name: string): CustomMaterial {
+        if (!this._customAttributes) {
+            this._customAttributes = [];
+        }
+
+        this._customAttributes.push(name);
+
+        return this;
+    }
+
     public Fragment_Begin(shaderPart: string): CustomMaterial {
     public Fragment_Begin(shaderPart: string): CustomMaterial {
         this.CustomParts.Fragment_Begin = shaderPart;
         this.CustomParts.Fragment_Begin = shaderPart;
         return this;
         return this;

+ 16 - 1
materialsLibrary/src/custom/pbrCustomMaterial.ts

@@ -52,6 +52,7 @@ export class PBRCustomMaterial extends PBRMaterial {
     _newUniforms: string[];
     _newUniforms: string[];
     _newUniformInstances: any[];
     _newUniformInstances: any[];
     _newSamplerInstances: Texture[];
     _newSamplerInstances: Texture[];
+    _customAttributes: string[];
 
 
     public FragmentShader: string;
     public FragmentShader: string;
     public VertexShader: string;
     public VertexShader: string;
@@ -101,7 +102,7 @@ export class PBRCustomMaterial extends PBRMaterial {
         return arr;
         return arr;
     }
     }
 
 
-    public Builder(shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: PBRMaterialDefines): string {
+    public Builder(shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: PBRMaterialDefines, attributes?: string[]): string {
 
 
         if (this._isCreatedShader) {
         if (this._isCreatedShader) {
             return this._createdShaderName;
             return this._createdShaderName;
@@ -111,6 +112,10 @@ export class PBRCustomMaterial extends PBRMaterial {
         PBRCustomMaterial.ShaderIndexer++;
         PBRCustomMaterial.ShaderIndexer++;
         var name: string = "custom_" + PBRCustomMaterial.ShaderIndexer;
         var name: string = "custom_" + PBRCustomMaterial.ShaderIndexer;
 
 
+        if (attributes && this._customAttributes && this._customAttributes.length > 0) {
+            attributes.push(...this._customAttributes);
+        }
+
         this.ReviewUniform("uniform", uniforms);
         this.ReviewUniform("uniform", uniforms);
         this.ReviewUniform("sampler", samplers);
         this.ReviewUniform("sampler", samplers);
 
 
@@ -180,6 +185,16 @@ export class PBRCustomMaterial extends PBRMaterial {
         return this;
         return this;
     }
     }
 
 
+    public AddAttribute(name: string): PBRCustomMaterial {
+        if (!this._customAttributes) {
+            this._customAttributes = [];
+        }
+
+        this._customAttributes.push(name);
+
+        return this;
+    }
+
     public Fragment_Begin(shaderPart: string): PBRCustomMaterial {
     public Fragment_Begin(shaderPart: string): PBRCustomMaterial {
         this.CustomParts.Fragment_Begin = shaderPart;
         this.CustomParts.Fragment_Begin = shaderPart;
         return this;
         return this;

+ 2 - 2
src/Materials/PBR/pbrBaseMaterial.ts

@@ -748,7 +748,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
     /**
     /**
      * Custom callback helping to override the default shader used in the material.
      * Custom callback helping to override the default shader used in the material.
      */
      */
-    public customShaderNameResolve: (shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: PBRMaterialDefines) => string;
+    public customShaderNameResolve: (shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: PBRMaterialDefines, attributes?: string[]) => string;
 
 
     protected _rebuildInParallel = false;
     protected _rebuildInParallel = false;
 
 
@@ -1207,7 +1207,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
         });
         });
 
 
         if (this.customShaderNameResolve) {
         if (this.customShaderNameResolve) {
-            shaderName = this.customShaderNameResolve(shaderName, uniforms, uniformBuffers, samplers, defines);
+            shaderName = this.customShaderNameResolve(shaderName, uniforms, uniformBuffers, samplers, defines, attribs);
         }
         }
 
 
         var join = defines.toString();
         var join = defines.toString();

+ 2 - 2
src/Materials/standardMaterial.ts

@@ -665,7 +665,7 @@ export class StandardMaterial extends PushMaterial {
     /**
     /**
      * Custom callback helping to override the default shader used in the material.
      * Custom callback helping to override the default shader used in the material.
      */
      */
-    public customShaderNameResolve: (shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines) => string;
+    public customShaderNameResolve: (shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines, attributes?: string[]) => string;
 
 
     protected _renderTargets = new SmartArray<RenderTargetTexture>(16);
     protected _renderTargets = new SmartArray<RenderTargetTexture>(16);
     protected _worldViewProjectionMatrix = Matrix.Zero();
     protected _worldViewProjectionMatrix = Matrix.Zero();
@@ -1163,7 +1163,7 @@ export class StandardMaterial extends PushMaterial {
             });
             });
 
 
             if (this.customShaderNameResolve) {
             if (this.customShaderNameResolve) {
-                shaderName = this.customShaderNameResolve(shaderName, uniforms, uniformBuffers, samplers, defines);
+                shaderName = this.customShaderNameResolve(shaderName, uniforms, uniformBuffers, samplers, defines, attribs);
             }
             }
 
 
             var join = defines.toString();
             var join = defines.toString();