Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpriteNodeMaterial: Add .scaleNode property and support to sprite.center #24158

Merged
merged 1 commit into from
May 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions examples/jsm/nodes/materials/SpriteNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SpriteMaterial } from 'three';
import {
vec2, vec3, vec4,
assign, add, mul, sub,
positionLocal, bypass, length, cos, sin,
positionLocal, bypass, length, cos, sin, uniform,
modelViewMatrix, cameraProjectionMatrix, modelWorldMatrix, materialRotation
} from '../shadernode/ShaderNodeElements.js';

Expand All @@ -22,13 +22,13 @@ class SpriteNodeMaterial extends NodeMaterial {
this.colorNode = null;
this.opacityNode = null;

this.rotationNode = null;

this.alphaTestNode = null;

this.lightNode = null;

this.positionNode = null;
this.rotationNode = null;
this.scaleNode = null;

this.setDefaultValues( defaultValues );

Expand All @@ -40,25 +40,42 @@ class SpriteNodeMaterial extends NodeMaterial {

// < VERTEX STAGE >

const { positionNode, rotationNode, scaleNode } = this;

let vertex = positionLocal;

if ( this.positionNode !== null ) {
if ( positionNode !== null ) {

vertex = bypass( vertex, assign( positionLocal, this.positionNode ) );
vertex = bypass( vertex, assign( positionLocal, positionNode ) );

}

let mvPosition = mul( modelViewMatrix, vec4( 0, 0, 0, 1 ) );

const scale = vec2(
let scale = vec2(
length( vec3( modelWorldMatrix[ 0 ].x, modelWorldMatrix[ 0 ].y, modelWorldMatrix[ 0 ].z ) ),
length( vec3( modelWorldMatrix[ 1 ].x, modelWorldMatrix[ 1 ].y, modelWorldMatrix[ 1 ].z ) )
);

const alignedPosition = mul( positionLocal.xy, scale );
const rotation = this.rotationNode || materialRotation;
if ( scaleNode !== null ) {

scale = mul( scale, scaleNode );

}

let alignedPosition = vertex.xy;

if ( builder.object.center?.isVector2 === true ) {

let rotatedPosition = vec2(
alignedPosition = sub( alignedPosition, sub( uniform( builder.object.center ), vec2( 0.5 ) ) );

}

alignedPosition = mul( alignedPosition, scale );

const rotation = rotationNode || materialRotation;

const rotatedPosition = vec2(
sub( mul( cos( rotation ), alignedPosition.x ), mul( sin( rotation ), alignedPosition.y ) ),
add( mul( sin( rotation ), alignedPosition.x ), mul( cos( rotation ), alignedPosition.y ) )
);
Expand All @@ -78,13 +95,13 @@ class SpriteNodeMaterial extends NodeMaterial {
this.colorNode = source.colorNode;
this.opacityNode = source.opacityNode;

this.rotationNode = source.rotationNode;

this.alphaTestNode = source.alphaTestNode;

this.lightNode = source.lightNode;

this.positionNode = source.positionNode;
this.rotationNode = source.rotationNode;
this.scaleNode = source.scaleNode;

return super.copy( source );

Expand Down