AttributeNode.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Node, { addNodeClass } from './Node.js';
  2. import { varying } from './VaryingNode.js';
  3. import { nodeObject } from '../shadernode/ShaderNode.js';
  4. class AttributeNode extends Node {
  5. constructor( attributeName, nodeType = null ) {
  6. super( nodeType );
  7. this._attributeName = attributeName;
  8. }
  9. getHash( builder ) {
  10. return this.getAttributeName( builder );
  11. }
  12. getNodeType( builder ) {
  13. const attributeName = this.getAttributeName( builder );
  14. let nodeType = super.getNodeType( builder );
  15. if ( nodeType === null ) {
  16. if ( builder.hasGeometryAttribute( attributeName ) ) {
  17. const attribute = builder.geometry.getAttribute( attributeName );
  18. nodeType = builder.getTypeFromAttribute( attribute );
  19. } else {
  20. nodeType = 'float';
  21. }
  22. }
  23. return nodeType;
  24. }
  25. setAttributeName( attributeName ) {
  26. this._attributeName = attributeName;
  27. return this;
  28. }
  29. getAttributeName( /*builder*/ ) {
  30. return this._attributeName;
  31. }
  32. generate( builder ) {
  33. const attributeName = this.getAttributeName( builder );
  34. const nodeType = this.getNodeType( builder );
  35. const geometryAttribute = builder.hasGeometryAttribute( attributeName );
  36. if ( geometryAttribute === true ) {
  37. const attribute = builder.geometry.getAttribute( attributeName );
  38. const attributeType = builder.getTypeFromAttribute( attribute );
  39. const nodeAttribute = builder.getAttribute( attributeName, attributeType );
  40. if ( builder.isShaderStage( 'vertex' ) ) {
  41. return builder.format( nodeAttribute.name, attributeType, nodeType );
  42. } else {
  43. const nodeVarying = varying( this );
  44. return nodeVarying.build( builder, nodeType );
  45. }
  46. } else {
  47. console.warn( `AttributeNode: Attribute "${ attributeName }" not found.` );
  48. return builder.getConst( nodeType );
  49. }
  50. }
  51. }
  52. export default AttributeNode;
  53. export const attribute = ( name, nodeType ) => nodeObject( new AttributeNode( name, nodeType ) );
  54. addNodeClass( AttributeNode );