Skip to content

Commit

Permalink
WebGPURenderer: Add mat2 uniform support (#30368)
Browse files Browse the repository at this point in the history
* WebGPURenderer: Add mat2 uniform support

* feedbacks

* Update Uniform.js

Clean up.

---------

Co-authored-by: Michael Herzog <[email protected]>
  • Loading branch information
RenaudRohlinger and Mugen87 authored Jan 21, 2025
1 parent 325bb90 commit 6f2a92d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { NodeUpdateType, defaultBuildStages, shaderStages } from './constants.js

import {
NumberNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform,
ColorNodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
ColorNodeUniform, Matrix2NodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
} from '../../renderers/common/nodes/NodeUniform.js';

import { stack } from './StackNode.js';
Expand Down Expand Up @@ -2366,6 +2366,7 @@ class NodeBuilder {
if ( type === 'vec3' || type === 'ivec3' || type === 'uvec3' ) return new Vector3NodeUniform( uniformNode );
if ( type === 'vec4' || type === 'ivec4' || type === 'uvec4' ) return new Vector4NodeUniform( uniformNode );
if ( type === 'color' ) return new ColorNodeUniform( uniformNode );
if ( type === 'mat2' ) return new Matrix2NodeUniform( uniformNode );
if ( type === 'mat3' ) return new Matrix3NodeUniform( uniformNode );
if ( type === 'mat4' ) return new Matrix4NodeUniform( uniformNode );

Expand Down
38 changes: 37 additions & 1 deletion src/renderers/common/Uniform.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Color } from '../../math/Color.js';
import { Matrix2 } from '../../math/Matrix2.js';
import { Matrix3 } from '../../math/Matrix3.js';
import { Matrix4 } from '../../math/Matrix4.js';
import { Vector2 } from '../../math/Vector2.js';
Expand Down Expand Up @@ -256,6 +257,41 @@ class ColorUniform extends Uniform {

}

/**
* Represents a Matrix2 uniform.
*
* @private
* @augments Uniform
*/
class Matrix2Uniform extends Uniform {

/**
* Constructs a new Number uniform.
*
* @param {String} name - The uniform's name.
* @param {Matrix2} value - The uniform's value.
*/
constructor( name, value = new Matrix2() ) {

super( name, value );

/**
* This flag can be used for type testing.
*
* @type {Boolean}
* @readonly
* @default true
*/
this.isMatrix2Uniform = true;

this.boundary = 16;
this.itemSize = 4;

}

}


/**
* Represents a Matrix3 uniform.
*
Expand Down Expand Up @@ -327,5 +363,5 @@ class Matrix4Uniform extends Uniform {
export {
NumberUniform,
Vector2Uniform, Vector3Uniform, Vector4Uniform, ColorUniform,
Matrix3Uniform, Matrix4Uniform
Matrix2Uniform, Matrix3Uniform, Matrix4Uniform
};
56 changes: 54 additions & 2 deletions src/renderers/common/nodes/NodeUniform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
NumberUniform, Vector2Uniform, Vector3Uniform, Vector4Uniform,
ColorUniform, Matrix3Uniform, Matrix4Uniform
ColorUniform, Matrix2Uniform, Matrix3Uniform, Matrix4Uniform
} from '../Uniform.js';

/**
Expand Down Expand Up @@ -258,6 +258,58 @@ class ColorNodeUniform extends ColorUniform {

}


/**
* A special form of Matrix2 uniform binding type.
* It's value is managed by a node object.
*
* @private
* @augments Matrix2Uniform
*/
class Matrix2NodeUniform extends Matrix2Uniform {

/**
* Constructs a new node-based Matrix2 uniform.
*
* @param {NodeUniform} nodeUniform - The node uniform.
*/
constructor( nodeUniform ) {

super( nodeUniform.name, nodeUniform.value );

/**
* The node uniform.
*
* @type {NodeUniform}
*/
this.nodeUniform = nodeUniform;

}

/**
* Overwritten to return the value of the node uniform.
*
* @return {Matrix2} The value.
*/
getValue() {

return this.nodeUniform.value;

}

/**
* Returns the node uniform data type.
*
* @return {String} The data type.
*/
getType() {

return this.nodeUniform.type;

}

}

/**
* A special form of Matrix3 uniform binding type.
* It's value is managed by a node object.
Expand Down Expand Up @@ -362,5 +414,5 @@ class Matrix4NodeUniform extends Matrix4Uniform {

export {
NumberNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform,
ColorNodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
ColorNodeUniform, Matrix2NodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
};

0 comments on commit 6f2a92d

Please sign in to comment.