From 5b7fe402bc4319dd065c79fd082901c065f17c28 Mon Sep 17 00:00:00 2001 From: aardgoose Date: Fri, 12 Jan 2024 14:50:27 +0000 Subject: [PATCH 1/4] add mat2 --- examples/jsm/nodes/core/NodeBuilder.js | 2 ++ examples/jsm/nodes/core/constants.js | 1 + examples/jsm/nodes/shadernode/ShaderNode.js | 9 +++++++++ examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js | 5 +++++ 4 files changed, 17 insertions(+) diff --git a/examples/jsm/nodes/core/NodeBuilder.js b/examples/jsm/nodes/core/NodeBuilder.js index 6fa1f8d56d3f79..7b29cae898153a 100644 --- a/examples/jsm/nodes/core/NodeBuilder.js +++ b/examples/jsm/nodes/core/NodeBuilder.js @@ -29,6 +29,7 @@ const typeFromLength = new Map( [ [ 2, 'vec2' ], [ 3, 'vec3' ], [ 4, 'vec4' ], + [ 4, 'mat2' ], [ 9, 'mat3' ], [ 16, 'mat4' ] ] ); @@ -575,6 +576,7 @@ class NodeBuilder { if ( vecNum !== null ) return Number( vecNum[ 1 ] ); if ( vecType === 'float' || vecType === 'bool' || vecType === 'int' || vecType === 'uint' ) return 1; + if ( /mat2/.test( type ) === true ) return 4; if ( /mat3/.test( type ) === true ) return 9; if ( /mat4/.test( type ) === true ) return 16; diff --git a/examples/jsm/nodes/core/constants.js b/examples/jsm/nodes/core/constants.js index db85dd24520db2..1ef0448f8dccca 100644 --- a/examples/jsm/nodes/core/constants.js +++ b/examples/jsm/nodes/core/constants.js @@ -17,6 +17,7 @@ export const NodeType = { VECTOR2: 'vec2', VECTOR3: 'vec3', VECTOR4: 'vec4', + MATRIX2: 'mat2', MATRIX3: 'mat3', MATRIX4: 'mat4' }; diff --git a/examples/jsm/nodes/shadernode/ShaderNode.js b/examples/jsm/nodes/shadernode/ShaderNode.js index 13a547070caa3b..edf7a754bf5a78 100644 --- a/examples/jsm/nodes/shadernode/ShaderNode.js +++ b/examples/jsm/nodes/shadernode/ShaderNode.js @@ -574,6 +574,11 @@ export const ivec4 = new ConvertType( 'ivec4' ); export const uvec4 = new ConvertType( 'uvec4' ); export const bvec4 = new ConvertType( 'bvec4' ); +export const mat2 = new ConvertType( 'mat2' ); +export const imat2 = new ConvertType( 'imat2' ); +export const umat2 = new ConvertType( 'umat2' ); +export const bmat2 = new ConvertType( 'bmat2' ); + export const mat3 = new ConvertType( 'mat3' ); export const imat3 = new ConvertType( 'imat3' ); export const umat3 = new ConvertType( 'umat3' ); @@ -604,6 +609,10 @@ addNodeElement( 'vec4', vec4 ); addNodeElement( 'ivec4', ivec4 ); addNodeElement( 'uvec4', uvec4 ); addNodeElement( 'bvec4', bvec4 ); +addNodeElement( 'mat2', mat2 ); +addNodeElement( 'imat2', imat2 ); +addNodeElement( 'umat2', umat2 ); +addNodeElement( 'bmat2', bmat2 ); addNodeElement( 'mat3', mat3 ); addNodeElement( 'imat3', imat3 ); addNodeElement( 'umat3', umat3 ); diff --git a/examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js b/examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js index 7cf6aead3af1a2..e8d558244ddc24 100644 --- a/examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js +++ b/examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js @@ -55,6 +55,11 @@ const wgslTypeLib = { uvec4: 'vec4', bvec4: 'vec4', + mat2: 'mat2x2', + imat2: 'mat2x2', + umat2: 'mat2x2', + bmat2: 'mat2x2', + mat3: 'mat3x3', imat3: 'mat3x3', umat3: 'mat3x3', From 59b814e7a565fc18fadd239171a89fa52b10e20d Mon Sep 17 00:00:00 2001 From: aardgoose Date: Tue, 23 Jan 2024 11:40:08 +0000 Subject: [PATCH 2/4] extend rotateNode to vec2 --- .../jsm/nodes/materials/SpriteNodeMaterial.js | 8 +---- examples/jsm/nodes/utils/RotateNode.js | 35 +++++++++++++++---- examples/jsm/nodes/utils/RotateUVNode.js | 10 +----- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/examples/jsm/nodes/materials/SpriteNodeMaterial.js b/examples/jsm/nodes/materials/SpriteNodeMaterial.js index 4580ba777175f8..e6b54b5613dbe5 100644 --- a/examples/jsm/nodes/materials/SpriteNodeMaterial.js +++ b/examples/jsm/nodes/materials/SpriteNodeMaterial.js @@ -61,13 +61,7 @@ class SpriteNodeMaterial extends NodeMaterial { const rotation = float( rotationNode || materialRotation ); - const cosAngle = rotation.cos(); - const sinAngle = rotation.sin(); - - const rotatedPosition = vec2( // @TODO: Maybe we can create mat2 and write something like rotationMatrix.mul( alignedPosition )? - vec2( cosAngle, sinAngle.negate() ).dot( alignedPosition ), - vec2( sinAngle, cosAngle ).dot( alignedPosition ) - ); + const rotatedPosition = alignedPosition.rotate( rotation ); mvPosition = vec4( mvPosition.xy.add( rotatedPosition ), mvPosition.zw ); diff --git a/examples/jsm/nodes/utils/RotateNode.js b/examples/jsm/nodes/utils/RotateNode.js index 902d48ddf04fc1..ddeb616b9b953d 100644 --- a/examples/jsm/nodes/utils/RotateNode.js +++ b/examples/jsm/nodes/utils/RotateNode.js @@ -4,6 +4,7 @@ import { addNodeElement, nodeProxy, vec4, + mat2, mat4, } from '../shadernode/ShaderNode.js'; import { cos, sin } from '../math/MathNode.js'; @@ -12,23 +13,43 @@ class RotateNode extends TempNode { constructor( positionNode, rotationNode ) { - super( 'vec3' ); + super(); this.positionNode = positionNode; this.rotationNode = rotationNode; } - setup() { + setup( builder ) { const { rotationNode, positionNode } = this; - const rotation = rotationNode; - const rotationXMatrix = mat4( vec4( 1.0, 0.0, 0.0, 0.0 ), vec4( 0.0, cos( rotation.x ), sin( rotation.x ).negate(), 0.0 ), vec4( 0.0, sin( rotation.x ), cos( rotation.x ), 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) ); - const rotationYMatrix = mat4( vec4( cos( rotation.y ), 0.0, sin( rotation.y ), 0.0 ), vec4( 0.0, 1.0, 0.0, 0.0 ), vec4( sin( rotation.y ).negate(), 0.0, cos( rotation.y ), 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) ); - const rotationZMatrix = mat4( vec4( cos( rotation.z ), sin( rotation.z ).negate(), 0.0, 0.0 ), vec4( sin( rotation.z ), cos( rotation.z ), 0.0, 0.0 ), vec4( 0.0, 0.0, 1.0, 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) ); + const nodeType = positionNode.getNodeType( builder ); - return rotationXMatrix.mul( rotationYMatrix ).mul( rotationZMatrix ).mul( vec4( positionNode, 1.0 ) ).xyz; + this.nodeType = nodeType; + + if ( nodeType === 'vec2' ) { + + const cosAngle = rotationNode.cos(); + const sinAngle = rotationNode.sin(); + + const rotationMatrix = mat2( + cosAngle, sinAngle, + sinAngle.negate(), cosAngle + ); + + return rotationMatrix.mul( positionNode ); + + } else { + + const rotation = rotationNode; + const rotationXMatrix = mat4( vec4( 1.0, 0.0, 0.0, 0.0 ), vec4( 0.0, cos( rotation.x ), sin( rotation.x ).negate(), 0.0 ), vec4( 0.0, sin( rotation.x ), cos( rotation.x ), 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) ); + const rotationYMatrix = mat4( vec4( cos( rotation.y ), 0.0, sin( rotation.y ), 0.0 ), vec4( 0.0, 1.0, 0.0, 0.0 ), vec4( sin( rotation.y ).negate(), 0.0, cos( rotation.y ), 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) ); + const rotationZMatrix = mat4( vec4( cos( rotation.z ), sin( rotation.z ).negate(), 0.0, 0.0 ), vec4( sin( rotation.z ), cos( rotation.z ), 0.0, 0.0 ), vec4( 0.0, 0.0, 1.0, 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) ); + + return rotationXMatrix.mul( rotationYMatrix ).mul( rotationZMatrix ).mul( vec4( positionNode, 1.0 ) ).xyz; + + } } diff --git a/examples/jsm/nodes/utils/RotateUVNode.js b/examples/jsm/nodes/utils/RotateUVNode.js index 45bfe1cdb6804e..4f432cd09d7873 100644 --- a/examples/jsm/nodes/utils/RotateUVNode.js +++ b/examples/jsm/nodes/utils/RotateUVNode.js @@ -18,17 +18,9 @@ class RotateUVNode extends TempNode { const { uvNode, rotationNode, centerNode } = this; - const cosAngle = rotationNode.cos(); - const sinAngle = rotationNode.sin(); - const vector = uvNode.sub( centerNode ); - const rotatedVector = vec2( // @TODO: Maybe we can create mat2 and write something like rotationMatrix.mul( vector )? - vec2( cosAngle, sinAngle ).dot( vector ), - vec2( sinAngle.negate(), cosAngle ).dot( vector ) - ); - - return rotatedVector.add( centerNode ); + return vector.rotate( rotationNode ).add( centerNode ); } From 5fa28cb497028270535bf25940242e35d61a8e05 Mon Sep 17 00:00:00 2001 From: aardgoose Date: Tue, 23 Jan 2024 13:26:52 +0000 Subject: [PATCH 3/4] remove unused code --- examples/jsm/nodes/core/NodeBuilder.js | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/jsm/nodes/core/NodeBuilder.js b/examples/jsm/nodes/core/NodeBuilder.js index 7b29cae898153a..3855dba05c3677 100644 --- a/examples/jsm/nodes/core/NodeBuilder.js +++ b/examples/jsm/nodes/core/NodeBuilder.js @@ -29,7 +29,6 @@ const typeFromLength = new Map( [ [ 2, 'vec2' ], [ 3, 'vec3' ], [ 4, 'vec4' ], - [ 4, 'mat2' ], [ 9, 'mat3' ], [ 16, 'mat4' ] ] ); From d8054a03b88a2ea5432d7ef179ca26b7d72a474f Mon Sep 17 00:00:00 2001 From: aardgoose Date: Tue, 23 Jan 2024 20:25:40 +0000 Subject: [PATCH 4/4] rework --- examples/jsm/nodes/utils/RotateNode.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/jsm/nodes/utils/RotateNode.js b/examples/jsm/nodes/utils/RotateNode.js index ddeb616b9b953d..2dc9be8d63b1b0 100644 --- a/examples/jsm/nodes/utils/RotateNode.js +++ b/examples/jsm/nodes/utils/RotateNode.js @@ -20,13 +20,17 @@ class RotateNode extends TempNode { } + getNodeType( builder ) { + + return this.positionNode.getNodeType( builder ); + + } + setup( builder ) { const { rotationNode, positionNode } = this; - const nodeType = positionNode.getNodeType( builder ); - - this.nodeType = nodeType; + const nodeType = this.getNodeType( builder ); if ( nodeType === 'vec2' ) {