Skip to content

SpriteNodeMaterial: Add sizeAttenuation #29394

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

Merged
merged 22 commits into from
Sep 23, 2024
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
35 changes: 32 additions & 3 deletions src/materials/nodes/SpriteNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import { uniform } from '../../nodes/core/UniformNode.js';
import { cameraProjectionMatrix } from '../../nodes/accessors/Camera.js';
import { materialRotation } from '../../nodes/accessors/MaterialNode.js';
import { modelViewMatrix, modelWorldMatrix } from '../../nodes/accessors/ModelNode.js';
Expand All @@ -8,6 +7,7 @@ import { rotate } from '../../nodes/utils/RotateNode.js';
import { float, vec2, vec3, vec4 } from '../../nodes/tsl/TSLBase.js';

import { SpriteMaterial } from '../SpriteMaterial.js';
import { reference } from '../../nodes/accessors/ReferenceBaseNode.js';

const _defaultValues = /*@__PURE__*/ new SpriteMaterial();

Expand All @@ -26,6 +26,7 @@ class SpriteNodeMaterial extends NodeMaterial {
this.isSpriteNodeMaterial = true;

this.lights = false;
this._useSizeAttenuation = true;

this.positionNode = null;
this.rotationNode = null;
Expand All @@ -37,7 +38,9 @@ class SpriteNodeMaterial extends NodeMaterial {

}

setupPosition( { object, context } ) {
setupPosition( { object, camera, context } ) {

const sizeAttenuation = this.sizeAttenuation;

// < VERTEX STAGE >

Expand All @@ -55,11 +58,20 @@ class SpriteNodeMaterial extends NodeMaterial {

}


if ( ! sizeAttenuation && camera.isPerspectiveCamera ) {

scale = scale.mul( mvPosition.z.negate() );

}

let alignedPosition = vertex.xy;

if ( object.center && object.center.isVector2 === true ) {

alignedPosition = alignedPosition.sub( uniform( object.center ).sub( 0.5 ) );
const center = reference( 'center', 'vec2' );

alignedPosition = alignedPosition.sub( center.sub( 0.5 ) );

}

Expand Down Expand Up @@ -89,6 +101,23 @@ class SpriteNodeMaterial extends NodeMaterial {

}

get sizeAttenuation() {

return this._useSizeAttenuation;

}

set sizeAttenuation( value ) {

if ( this._useSizeAttenuation !== value ) {

this._useSizeAttenuation = value;
this.needsUpdate = true;

}

}

}

export default SpriteNodeMaterial;