NodeMaterial.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import {
  2. FrontSide,
  3. LessEqualDepth,
  4. NoColors,
  5. NormalBlending,
  6. ShaderMaterial
  7. } from '../../../../build/three.module.js';
  8. import { NodeBuilder } from '../core/NodeBuilder.js';
  9. import { ColorNode } from '../inputs/ColorNode.js';
  10. import { PositionNode } from '../accessors/PositionNode.js';
  11. import { RawNode } from './nodes/RawNode.js';
  12. class NodeMaterial extends ShaderMaterial {
  13. constructor( vertex, fragment ) {
  14. super();
  15. this.vertex = vertex || new RawNode( new PositionNode( PositionNode.PROJECTION ) );
  16. this.fragment = fragment || new RawNode( new ColorNode( 0xFF0000 ) );
  17. this.updaters = [];
  18. this.type = 'NodeMaterial';
  19. }
  20. get properties() {
  21. return this.fragment.properties;
  22. }
  23. get needsUpdate() {
  24. return this.needsCompile;
  25. }
  26. set needsUpdate( value ) {
  27. if ( value === true ) this.version ++;
  28. this.needsCompile = value;
  29. }
  30. onBeforeCompile( shader, renderer ) {
  31. this.build( { renderer: renderer } );
  32. shader.defines = this.defines;
  33. shader.uniforms = this.uniforms;
  34. shader.vertexShader = this.vertexShader;
  35. shader.fragmentShader = this.fragmentShader;
  36. shader.extensionDerivatives = ( this.extensions.derivatives === true );
  37. shader.extensionFragDepth = ( this.extensions.fragDepth === true );
  38. shader.extensionDrawBuffers = ( this.extensions.drawBuffers === true );
  39. shader.extensionShaderTextureLOD = ( this.extensions.shaderTextureLOD === true );
  40. }
  41. customProgramCacheKey() {
  42. const hash = this.getHash();
  43. return hash;
  44. }
  45. getHash() {
  46. let hash = '{';
  47. hash += '"vertex":' + this.vertex.getHash() + ',';
  48. hash += '"fragment":' + this.fragment.getHash();
  49. hash += '}';
  50. return hash;
  51. }
  52. updateFrame( frame ) {
  53. for ( let i = 0; i < this.updaters.length; ++ i ) {
  54. frame.updateNode( this.updaters[ i ] );
  55. }
  56. }
  57. build( params = {} ) {
  58. const builder = params.builder || new NodeBuilder();
  59. builder.setMaterial( this, params.renderer );
  60. builder.build( this.vertex, this.fragment );
  61. this.vertexShader = builder.getCode( 'vertex' );
  62. this.fragmentShader = builder.getCode( 'fragment' );
  63. this.defines = builder.defines;
  64. this.uniforms = builder.uniforms;
  65. this.extensions = builder.extensions;
  66. this.updaters = builder.updaters;
  67. this.fog = builder.requires.fog;
  68. this.lights = builder.requires.lights;
  69. this.transparent = builder.requires.transparent || this.blending > NormalBlending;
  70. return this;
  71. }
  72. copy( source ) {
  73. const uuid = this.uuid;
  74. for ( const name in source ) {
  75. this[ name ] = source[ name ];
  76. }
  77. this.uuid = uuid;
  78. if ( source.userData !== undefined ) {
  79. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  80. }
  81. return this;
  82. }
  83. toJSON( meta ) {
  84. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  85. if ( isRootObject ) {
  86. meta = {
  87. nodes: {}
  88. };
  89. }
  90. if ( meta && ! meta.materials ) meta.materials = {};
  91. if ( ! meta.materials[ this.uuid ] ) {
  92. const data = {};
  93. data.uuid = this.uuid;
  94. data.type = this.type;
  95. meta.materials[ data.uuid ] = data;
  96. if ( this.name !== '' ) data.name = this.name;
  97. if ( this.size !== undefined ) data.size = this.size;
  98. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  99. if ( this.blending !== NormalBlending ) data.blending = this.blending;
  100. if ( this.flatShading === true ) data.flatShading = this.flatShading;
  101. if ( this.side !== FrontSide ) data.side = this.side;
  102. if ( this.vertexColors !== NoColors ) data.vertexColors = this.vertexColors;
  103. if ( this.depthFunc !== LessEqualDepth ) data.depthFunc = this.depthFunc;
  104. if ( this.depthTest === false ) data.depthTest = this.depthTest;
  105. if ( this.depthWrite === false ) data.depthWrite = this.depthWrite;
  106. if ( this.linewidth !== 1 ) data.linewidth = this.linewidth;
  107. if ( this.dashSize !== undefined ) data.dashSize = this.dashSize;
  108. if ( this.gapSize !== undefined ) data.gapSize = this.gapSize;
  109. if ( this.scale !== undefined ) data.scale = this.scale;
  110. if ( this.dithering === true ) data.dithering = true;
  111. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  112. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  113. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  114. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  115. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  116. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
  117. if ( this.visible === false ) data.visible = false;
  118. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  119. data.fog = this.fog;
  120. data.lights = this.lights;
  121. data.vertex = this.vertex.toJSON( meta ).uuid;
  122. data.fragment = this.fragment.toJSON( meta ).uuid;
  123. }
  124. meta.material = this.uuid;
  125. return meta;
  126. }
  127. }
  128. NodeMaterial.prototype.isNodeMaterial = true;
  129. export { NodeMaterial };