Ver código fonte

Merge pull request #6466 from openhood/add-setColor4Array-to-shaderMaterial

Add `ShaderMaterial.setColor4Array`
David Catuhe 6 anos atrás
pai
commit
a170896d04

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

@@ -48,12 +48,13 @@
 ### Physics
 ### Physics
 - Update Ammo.js library to support global collision contact callbacks ([MackeyK24](https://github.com/MackeyK24/))
 - Update Ammo.js library to support global collision contact callbacks ([MackeyK24](https://github.com/MackeyK24/))
 - Update AmmoJSPlugin to allow your own broadphase overlapping pair cache ([MackeyK24](https://github.com/MackeyK24/))
 - Update AmmoJSPlugin to allow your own broadphase overlapping pair cache ([MackeyK24](https://github.com/MackeyK24/))
-- Update Ammo.js library and AmmoJS plugin to support ellipsoid ([CedricGuillemet](https://github.com/CedricGuillemet/)) 
+- Update Ammo.js library and AmmoJS plugin to support ellipsoid ([CedricGuillemet](https://github.com/CedricGuillemet/))
 
 
 ### Loaders
 ### Loaders
 - Added support for non-float accessors in animation data for glTF loader. ([bghgary](https://github.com/bghgary))
 - Added support for non-float accessors in animation data for glTF loader. ([bghgary](https://github.com/bghgary))
 
 
 ### Materials
 ### Materials
+- Added `ShaderMaterial.setColor4Array` ([JonathanTron](https://github.com/JonathanTron/))
 - Added `ShaderMaterial.setArray4` ([JonathanTron](https://github.com/JonathanTron/))
 - Added `ShaderMaterial.setArray4` ([JonathanTron](https://github.com/JonathanTron/))
 
 
 ## Bug fixes
 ## Bug fixes

+ 41 - 0
src/Materials/shaderMaterial.ts

@@ -70,6 +70,7 @@ export class ShaderMaterial extends Material {
     private _colors3: { [name: string]: Color3 } = {};
     private _colors3: { [name: string]: Color3 } = {};
     private _colors3Arrays: { [name: string]: number[] } = {};
     private _colors3Arrays: { [name: string]: number[] } = {};
     private _colors4: { [name: string]: Color4 } = {};
     private _colors4: { [name: string]: Color4 } = {};
+    private _colors4Arrays: { [name: string]: number[] } = {};
     private _vectors2: { [name: string]: Vector2 } = {};
     private _vectors2: { [name: string]: Vector2 } = {};
     private _vectors3: { [name: string]: Vector3 } = {};
     private _vectors3: { [name: string]: Vector3 } = {};
     private _vectors4: { [name: string]: Vector4 } = {};
     private _vectors4: { [name: string]: Vector4 } = {};
@@ -265,6 +266,21 @@ export class ShaderMaterial extends Material {
     }
     }
 
 
     /**
     /**
+     * Set a vec4 array in the shader from a Color4 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 setColor4Array(name: string, value: Color4[]): ShaderMaterial {
+        this._checkUniform(name);
+        this._colors4Arrays[name] = value.reduce((arr, color) => {
+            color.toArray(arr, arr.length);
+            return arr;
+        }, []);
+        return this;
+    }
+
+    /**
      * Set a vec2 in the shader from a Vector2.
      * Set a vec2 in the shader from a Vector2.
      * @param name Define the name of the uniform as defined in the shader
      * @param name Define the name of the uniform as defined in the shader
      * @param value Define the value to give to the uniform
      * @param value Define the value to give to the uniform
@@ -603,6 +619,7 @@ export class ShaderMaterial extends Material {
                 this._effect.setColor3(name, this._colors3[name]);
                 this._effect.setColor3(name, this._colors3[name]);
             }
             }
 
 
+            // Color3Array
             for (name in this._colors3Arrays) {
             for (name in this._colors3Arrays) {
                 this._effect.setArray3(name, this._colors3Arrays[name]);
                 this._effect.setArray3(name, this._colors3Arrays[name]);
             }
             }
@@ -613,6 +630,11 @@ export class ShaderMaterial extends Material {
                 this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
                 this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
             }
             }
 
 
+            // Color4Array
+            for (name in this._colors4Arrays) {
+                this._effect.setArray4(name, this._colors4Arrays[name]);
+            }
+
             // Vector2
             // Vector2
             for (name in this._vectors2) {
             for (name in this._vectors2) {
                 this._effect.setVector2(name, this._vectors2[name]);
                 this._effect.setVector2(name, this._vectors2[name]);
@@ -808,6 +830,12 @@ export class ShaderMaterial extends Material {
             serializationObject.colors4[name] = this._colors4[name].asArray();
             serializationObject.colors4[name] = this._colors4[name].asArray();
         }
         }
 
 
+        // Color4 array
+        serializationObject.colors4Arrays = {};
+        for (name in this._colors4Arrays) {
+            serializationObject.colors4Arrays[name] = this._colors4Arrays[name];
+        }
+
         // Vector2
         // Vector2
         serializationObject.vectors2 = {};
         serializationObject.vectors2 = {};
         for (name in this._vectors2) {
         for (name in this._vectors2) {
@@ -926,6 +954,19 @@ export class ShaderMaterial extends Material {
             material.setColor4(name, Color4.FromArray(source.colors4[name]));
             material.setColor4(name, Color4.FromArray(source.colors4[name]));
         }
         }
 
 
+        // Color4 arrays
+        for (name in source.colors4Arrays) {
+            const colors: Color4[] = source.colors4Arrays[name].reduce((arr: Array<Array<number>>, num: number, i: number) => {
+                if (i % 4 === 0) {
+                    arr.push([num]);
+                } else {
+                    arr[arr.length - 1].push(num);
+                }
+                return arr;
+            }, []).map((color: ArrayLike<number>) => Color4.FromArray(color));
+            material.setColor4Array(name, colors);
+        }
+
         // Vector2
         // Vector2
         for (name in source.vectors2) {
         for (name in source.vectors2) {
             material.setVector2(name, Vector2.FromArray(source.vectors2[name]));
             material.setVector2(name, Vector2.FromArray(source.vectors2[name]));