Skip to content

Commit

Permalink
TSL: vertexIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
sunag committed Jun 24, 2023
1 parent 5a6b2b0 commit e1d7fef
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 47 deletions.
2 changes: 1 addition & 1 deletion examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export { default as BypassNode, bypass } from './core/BypassNode.js';
export { default as CacheNode, cache } from './core/CacheNode.js';
export { default as ConstNode } from './core/ConstNode.js';
export { default as ContextNode, context, label } from './core/ContextNode.js';
export { default as InstanceIndexNode, instanceIndex } from './core/InstanceIndexNode.js';
export { default as IndexNode, vertexIndex, instanceIndex } from './core/IndexNode.js';
export { default as LightingModel, lightingModel } from './core/LightingModel.js';
export { default as Node, addNodeClass, createNodeFromType } from './core/Node.js';
export { default as NodeAttribute } from './core/NodeAttribute.js';
Expand Down
66 changes: 66 additions & 0 deletions examples/jsm/nodes/core/IndexNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Node, { addNodeClass } from './Node.js';
import { varying } from './VaryingNode.js';
import { nodeImmutable } from '../shadernode/ShaderNode.js';

class IndexNode extends Node {

constructor( scope ) {

super( 'uint' );

this.scope = scope;

this.isInstanceIndexNode = true;

}

generate( builder ) {

const nodeType = this.getNodeType( builder );
const scope = this.scope;

let propertyName;

if ( scope === IndexNode.VERTEX ) {

propertyName = builder.getVertexIndex();

} else if ( scope === IndexNode.INSTANCE ) {

propertyName = builder.getInstanceIndex();

} else {

throw new Error( 'THREE.IndexNode: Unknown scope: ' + scope );

}

let output;

if ( builder.shaderStage === 'vertex' || builder.shaderStage === 'compute' ) {

output = propertyName;

} else {

const nodeVarying = varying( this );

output = nodeVarying.build( builder, nodeType );

}

return output;

}

}

IndexNode.VERTEX = 'vertex';
IndexNode.INSTANCE = 'instance';

export default IndexNode;

export const vertexIndex = nodeImmutable( IndexNode, IndexNode.VERTEX );
export const instanceIndex = nodeImmutable( IndexNode, IndexNode.INSTANCE );

addNodeClass( IndexNode );
45 changes: 0 additions & 45 deletions examples/jsm/nodes/core/InstanceIndexNode.js

This file was deleted.

6 changes: 6 additions & 0 deletions examples/jsm/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ class NodeBuilder {

}

getVertexIndex() {

console.warn( 'Abstract function.' );

}

getInstanceIndex() {

console.warn( 'Abstract function.' );
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/geometry/RangeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Node, { addNodeClass } from '../core/Node.js';
import { getValueType } from '../core/NodeUtils.js';
import { buffer } from '../accessors/BufferNode.js';
//import { bufferAttribute } from '../accessors/BufferAttributeNode.js';
import { instanceIndex } from '../core/InstanceIndexNode.js';
import { instanceIndex } from '../core/IndexNode.js';
import { nodeProxy, float } from '../shadernode/ShaderNode.js';

import { Vector4, MathUtils } from 'three';
Expand Down
6 changes: 6 additions & 0 deletions examples/jsm/renderers/webgl/nodes/GLSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ class GLSLNodeBuilder extends NodeBuilder {

}

getVertexIndex() {

return 'gl_VertexID';

}

getFrontFacing() {

return 'gl_FrontFacing';
Expand Down
6 changes: 6 additions & 0 deletions examples/jsm/renderers/webgl/nodes/WebGLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,12 @@ class WebGLNodeBuilder extends NodeBuilder {

}

getVertexIndex() {

return 'gl_VertexID';

}

getFrontFacing() {

return 'gl_FrontFacing';
Expand Down
12 changes: 12 additions & 0 deletions examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,18 @@ class WGSLNodeBuilder extends NodeBuilder {

}

getVertexIndex() {

if ( this.shaderStage === 'vertex' ) {

return this.getBuiltin( 'vertex_index', 'vertexIndex', 'u32', 'attribute' );

}

return 'vertexIndex';

}

getInstanceIndex() {

if ( this.shaderStage === 'vertex' ) {
Expand Down

0 comments on commit e1d7fef

Please sign in to comment.