VertexColorNode.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import AttributeNode from '../core/AttributeNode.js';
  2. import { nodeObject } from '../tsl/TSLBase.js';
  3. import { Vector4 } from '../../math/Vector4.js';
  4. /**
  5. * An attribute node for representing vertex colors.
  6. *
  7. * @augments AttributeNode
  8. */
  9. class VertexColorNode extends AttributeNode {
  10. static get type() {
  11. return 'VertexColorNode';
  12. }
  13. /**
  14. * Constructs a new vertex color node.
  15. *
  16. * @param {Number} [index=0] - The attribute index.
  17. */
  18. constructor( index = 0 ) {
  19. super( null, 'vec4' );
  20. /**
  21. * This flag can be used for type testing.
  22. *
  23. * @type {Boolean}
  24. * @readonly
  25. * @default true
  26. */
  27. this.isVertexColorNode = true;
  28. /**
  29. * The attribute index to enable more than one sets of vertex colors.
  30. *
  31. * @type {Number}
  32. * @default 0
  33. */
  34. this.index = index;
  35. }
  36. /**
  37. * Overwrites the default implementation by honoring the attribute index.
  38. *
  39. * @param {NodeBuilder} builder - The current node builder.
  40. * @return {String} The attribute name.
  41. */
  42. getAttributeName( /*builder*/ ) {
  43. const index = this.index;
  44. return 'color' + ( index > 0 ? index : '' );
  45. }
  46. generate( builder ) {
  47. const attributeName = this.getAttributeName( builder );
  48. const geometryAttribute = builder.hasGeometryAttribute( attributeName );
  49. let result;
  50. if ( geometryAttribute === true ) {
  51. result = super.generate( builder );
  52. } else {
  53. // Vertex color fallback should be white
  54. result = builder.generateConst( this.nodeType, new Vector4( 1, 1, 1, 1 ) );
  55. }
  56. return result;
  57. }
  58. serialize( data ) {
  59. super.serialize( data );
  60. data.index = this.index;
  61. }
  62. deserialize( data ) {
  63. super.deserialize( data );
  64. this.index = data.index;
  65. }
  66. }
  67. export default VertexColorNode;
  68. /**
  69. * TSL function for creating a reference node.
  70. *
  71. * @tsl
  72. * @function
  73. * @param {Number} index - The attribute index.
  74. * @returns {VertexColorNode}
  75. */
  76. export const vertexColor = ( index ) => nodeObject( new VertexColorNode( index ) );
粤ICP备19079148号