IndexNode.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Node from './Node.js';
  2. import { nodeImmutable, varying } from '../tsl/TSLBase.js';
  3. /**
  4. * This class represents shader indices of different types. The following predefined node
  5. * objects cover frequent use cases:
  6. *
  7. * - `vertexIndex`: The index of a vertex within a mesh.
  8. * - `instanceIndex`: The index of either a mesh instance or an invocation of a compute shader.
  9. * - `drawIndex`: The index of a draw call.
  10. * - `invocationLocalIndex`: The index of a compute invocation within the scope of a workgroup load.
  11. * - `invocationSubgroupIndex`: The index of a compute invocation within the scope of a subgroup.
  12. * - `subgroupIndex`: The index of the subgroup the current compute invocation belongs to.
  13. *
  14. * @augments Node
  15. */
  16. class IndexNode extends Node {
  17. static get type() {
  18. return 'IndexNode';
  19. }
  20. /**
  21. * Constructs a new index node.
  22. *
  23. * @param {('vertex'|'instance'|'subgroup'|'invocationLocal'|'invocationSubgroup'|'draw')} value - The scope of the index node.
  24. */
  25. constructor( scope ) {
  26. super( 'uint' );
  27. /**
  28. * The scope of the index node.
  29. *
  30. * @type {String}
  31. */
  32. this.scope = scope;
  33. /**
  34. * This flag can be used for type testing.
  35. *
  36. * @type {Boolean}
  37. * @readonly
  38. * @default true
  39. */
  40. this.isIndexNode = true;
  41. }
  42. generate( builder ) {
  43. const nodeType = this.getNodeType( builder );
  44. const scope = this.scope;
  45. let propertyName;
  46. if ( scope === IndexNode.VERTEX ) {
  47. propertyName = builder.getVertexIndex();
  48. } else if ( scope === IndexNode.INSTANCE ) {
  49. propertyName = builder.getInstanceIndex();
  50. } else if ( scope === IndexNode.DRAW ) {
  51. propertyName = builder.getDrawIndex();
  52. } else if ( scope === IndexNode.INVOCATION_LOCAL ) {
  53. propertyName = builder.getInvocationLocalIndex();
  54. } else if ( scope === IndexNode.INVOCATION_SUBGROUP ) {
  55. propertyName = builder.getInvocationSubgroupIndex();
  56. } else if ( scope === IndexNode.SUBGROUP ) {
  57. propertyName = builder.getSubgroupIndex();
  58. } else {
  59. throw new Error( 'THREE.IndexNode: Unknown scope: ' + scope );
  60. }
  61. let output;
  62. if ( builder.shaderStage === 'vertex' || builder.shaderStage === 'compute' ) {
  63. output = propertyName;
  64. } else {
  65. const nodeVarying = varying( this );
  66. output = nodeVarying.build( builder, nodeType );
  67. }
  68. return output;
  69. }
  70. }
  71. IndexNode.VERTEX = 'vertex';
  72. IndexNode.INSTANCE = 'instance';
  73. IndexNode.SUBGROUP = 'subgroup';
  74. IndexNode.INVOCATION_LOCAL = 'invocationLocal';
  75. IndexNode.INVOCATION_SUBGROUP = 'invocationSubgroup';
  76. IndexNode.DRAW = 'draw';
  77. export default IndexNode;
  78. export const vertexIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.VERTEX );
  79. export const instanceIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.INSTANCE );
  80. export const subgroupIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.SUBGROUP );
  81. export const invocationSubgroupIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.INVOCATION_SUBGROUP );
  82. export const invocationLocalIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.INVOCATION_LOCAL );
  83. export const drawIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.DRAW );
粤ICP备19079148号