Переглянути джерело

Merge pull request #6465 from openhood/add-setArray4-to-shaderMaterial

Add `ShaderMaterial.setArray4`
David Catuhe 6 роки тому
батько
коміт
062e276135
2 змінених файлів з 33 додано та 0 видалено
  1. 3 0
      dist/preview release/what's new.md
  2. 30 0
      src/Materials/shaderMaterial.ts

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

@@ -53,6 +53,9 @@
 ### Loaders
 - Added support for non-float accessors in animation data for glTF loader. ([bghgary](https://github.com/bghgary))
 
+### Materials
+- Added `ShaderMaterial.setArray4` ([JonathanTron](https://github.com/JonathanTron/))
+
 ## Bug fixes
 - Added support for `AnimationGroup` serialization ([Drigax](https://github.com/drigax/))
 - Removing assetContainer from scene will also remove gui layers ([TrevorDev](https://github.com/TrevorDev))

+ 30 - 0
src/Materials/shaderMaterial.ts

@@ -78,6 +78,7 @@ export class ShaderMaterial extends Material {
     private _matrices2x2: { [name: string]: Float32Array } = {};
     private _vectors2Arrays: { [name: string]: number[] } = {};
     private _vectors3Arrays: { [name: string]: number[] } = {};
+    private _vectors4Arrays: { [name: string]: number[] } = {};
     private _cachedWorldViewMatrix = new Matrix();
     private _cachedWorldViewProjectionMatrix = new Matrix();
     private _renderId: number;
@@ -367,6 +368,19 @@ export class ShaderMaterial extends Material {
         return this;
     }
 
+    /**
+     * Set a vec4 array in the shader from a number array.
+     * @param name Define the name of the uniform as defined in the shader
+     * @param value Define the value to give to the uniform
+     * @return the material itself allowing "fluent" like uniform updates
+     */
+    public setArray4(name: string, value: number[]): ShaderMaterial {
+        this._checkUniform(name);
+        this._vectors4Arrays[name] = value;
+
+        return this;
+    }
+
     private _checkCache(mesh?: AbstractMesh, useInstances?: boolean): boolean {
         if (!mesh) {
             return true;
@@ -638,6 +652,11 @@ export class ShaderMaterial extends Material {
             for (name in this._vectors3Arrays) {
                 this._effect.setArray3(name, this._vectors3Arrays[name]);
             }
+
+            // Vector4Array
+            for (name in this._vectors4Arrays) {
+                this._effect.setArray4(name, this._vectors4Arrays[name]);
+            }
         }
 
         this._afterBind(mesh);
@@ -837,6 +856,12 @@ export class ShaderMaterial extends Material {
             serializationObject.vectors3Arrays[name] = this._vectors3Arrays[name];
         }
 
+        // Vector4Array
+        serializationObject.vectors4Arrays = {};
+        for (name in this._vectors4Arrays) {
+            serializationObject.vectors4Arrays[name] = this._vectors4Arrays[name];
+        }
+
         return serializationObject;
     }
 
@@ -941,6 +966,11 @@ export class ShaderMaterial extends Material {
             material.setArray3(name, source.vectors3Arrays[name]);
         }
 
+        // Vector4Array
+        for (name in source.vectors4Arrays) {
+            material.setArray4(name, source.vectors4Arrays[name]);
+        }
+
         return material;
     }
 }