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

InstancedPointsNodeMaterial: Add sizeAttenuation #29491

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions examples/webgpu_instance_points.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
minWidth: uniform( 6 ),
maxWidth: uniform( 12 ),
alphaToCoverage: true,
sizeAttenuation: false,

};

Expand Down Expand Up @@ -142,6 +143,7 @@
pointWidth: 10, // in pixel units
vertexColors: true,
alphaToCoverage: true,
sizeAttenuation: effectController.sizeAttenuation,

} );

Expand All @@ -168,6 +170,12 @@

} );

gui.add( effectController, 'sizeAttenuation' ).onChange( function ( val ) {

material.sizeAttenuation = val;

} );

gui.add( effectController.minWidth, 'value', 1, 20, 1 ).name( 'minWidth' );
gui.add( effectController.maxWidth, 'value', 2, 20, 1 ).name( 'maxWidth' );
gui.add( effectController.pulseSpeed, 'value', 1, 20, 0.1 ).name( 'pulseSpeed' );
Expand Down
61 changes: 49 additions & 12 deletions src/materials/nodes/InstancedPointsNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class InstancedPointsNodeMaterial extends NodeMaterial {

this.lights = false;

this.useAlphaToCoverage = true;
this._useSizeAttenuation = true;
this._useAlphaToCoverage = true;

this.useColor = params.vertexColors;
this._useColor = params.vertexColors;

this.pointWidth = 1;

Expand All @@ -51,28 +52,47 @@ class InstancedPointsNodeMaterial extends NodeMaterial {

}

setupShaders( { renderer } ) {
setupShaders( { renderer, camera } ) {

const useAlphaToCoverage = this.alphaToCoverage;
const useColor = this.useColor;
const alphaToCoverage = this.alphaToCoverage;
const sizeAttenuation = this.sizeAttenuation;

const color = this._useColor;

this.vertexNode = Fn( () => {

const instancePosition = attribute( 'instancePosition' ).xyz;

// camera space
const mvPos = vec4( modelViewMatrix.mul( vec4( instancePosition, 1.0 ) ) );
const mvPosition = vec4( modelViewMatrix.mul( vec4( instancePosition, 1.0 ) ) );

const aspect = viewport.z.div( viewport.w );

// clip space
const clipPos = cameraProjectionMatrix.mul( mvPos );
const clipPos = cameraProjectionMatrix.mul( mvPosition );

// offset in ndc space
const offset = positionGeometry.xy.toVar();

offset.mulAssign( this.pointWidthNode ? this.pointWidthNode : materialPointWidth );


const pixelRatio = renderer.getPixelRatio();
const scale = float( 1.0 ).toVar();

if ( sizeAttenuation && camera.isPerspectiveCamera ) {

const fovAdjustment = cameraProjectionMatrix[ 1 ][ 1 ];
scale.assign( viewport.w.mul( 0.5 ).mul( pixelRatio ).div( mvPosition.z.negate() ).div( fovAdjustment.mul( pixelRatio ) ) );

} else {

scale.assign( pixelRatio );

}

offset.mulAssign( scale );

offset.assign( offset.div( viewport.z ) );
offset.y.assign( offset.y.mul( aspect ) );

Expand All @@ -92,7 +112,7 @@ class InstancedPointsNodeMaterial extends NodeMaterial {

const len2 = lengthSq( uv().mul( 2 ).sub( 1 ) );

if ( useAlphaToCoverage && renderer.samples > 1 ) {
if ( alphaToCoverage && renderer.samples > 1 ) {

const dlen = float( len2.fwidth() ).toVar();

Expand All @@ -112,7 +132,7 @@ class InstancedPointsNodeMaterial extends NodeMaterial {

} else {

if ( useColor ) {
if ( color ) {

const instanceColor = attribute( 'instanceColor' );

Expand All @@ -136,15 +156,32 @@ class InstancedPointsNodeMaterial extends NodeMaterial {

get alphaToCoverage() {

return this.useAlphaToCoverage;
return this._useAlphaToCoverage;

}

set alphaToCoverage( value ) {

if ( this.useAlphaToCoverage !== value ) {
if ( this._useAlphaToCoverage !== value ) {

this._useAlphaToCoverage = value;
this.needsUpdate = true;

}

}

get sizeAttenuation() {

return this._useSizeAttenuation;

}

set sizeAttenuation( value ) {

if ( this._useSizeAttenuation !== value ) {

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

}
Expand Down