NodeMaterial.js 5.4 KB

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