AttributeNode.js 1.6 KB

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