Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/materials/nodes/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ class NodeMaterial extends Material {
*/
this.positionNode = null;

/**
* Allows to overwrite {@link positionLocal} immediately after it has
* been transformed by the instance matrix in the vertex shader.
* Note: this node property only has an effect when used with
* an {@link InstancedMesh} or a {@link BatchedMesh}.
*
* @type {?Node<vec3>}
* @default null
*/
this.instancePositionNode = null;

/**
* This node property is intended for logic which modifies geometry data once or per animation step.
* Apps usually place such logic randomly in initialization routines or in the animation loop.
Expand Down
7 changes: 7 additions & 0 deletions src/nodes/accessors/BatchNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { textureSize } from './TextureSizeNode.js';
import { tangentLocal } from './Tangent.js';
import { instanceIndex, drawIndex } from '../core/IndexNode.js';
import { varyingProperty } from '../core/PropertyNode.js';
import NodeMaterial from '../../materials/nodes/NodeMaterial.js';

/**
* This node implements the vertex shader logic which is required
Expand Down Expand Up @@ -134,6 +135,12 @@ class BatchNode extends Node {

positionLocal.assign( batchingMatrix.mul( positionLocal ) );

if ( builder.material instanceof NodeMaterial && builder.material.instancePositionNode != null ) {

positionLocal.assign( builder.material.instancePositionNode );

}

const transformedNormal = normalLocal.div( vec3( bm[ 0 ].dot( bm[ 0 ] ), bm[ 1 ].dot( bm[ 1 ] ), bm[ 2 ].dot( bm[ 2 ] ) ) );

const batchingNormal = bm.mul( transformedNormal ).xyz;
Expand Down
7 changes: 7 additions & 0 deletions src/nodes/accessors/InstanceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { instanceIndex } from '../core/IndexNode.js';
import { InstancedInterleavedBuffer } from '../../core/InstancedInterleavedBuffer.js';
import { InstancedBufferAttribute } from '../../core/InstancedBufferAttribute.js';
import { DynamicDrawUsage } from '../../constants.js';
import NodeMaterial from '../../materials/nodes/NodeMaterial.js';

/**
* This node implements the vertex shader logic which is required
Expand Down Expand Up @@ -187,6 +188,12 @@ class InstanceNode extends Node {
const instancePosition = instanceMatrixNode.mul( positionLocal ).xyz;
positionLocal.assign( instancePosition );

if ( builder.material instanceof NodeMaterial && builder.material.instancePositionNode != null ) {

positionLocal.assign( builder.material.instancePositionNode );

}

if ( builder.needsPreviousData() ) {

positionPrevious.assign( this.getPreviousInstancedPosition( builder ) );
Expand Down