NodeMaterial.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.uniforms = this.uniforms;
  41. shader.vertexShader = this.vertexShader;
  42. shader.fragmentShader = this.fragmentShader;
  43. };
  44. NodeMaterial.prototype.customProgramCacheKey = function () {
  45. var hash = this.getHash();
  46. return hash;
  47. };
  48. NodeMaterial.prototype.getHash = function () {
  49. var hash = '{';
  50. hash += '"vertex":' + this.vertex.getHash() + ',';
  51. hash += '"fragment":' + this.fragment.getHash();
  52. hash += '}';
  53. return hash;
  54. };
  55. NodeMaterial.prototype.updateFrame = function ( frame ) {
  56. for ( var i = 0; i < this.updaters.length; ++ i ) {
  57. frame.updateNode( this.updaters[ i ] );
  58. }
  59. };
  60. NodeMaterial.prototype.build = function ( params ) {
  61. params = params || {};
  62. var builder = params.builder || new NodeBuilder();
  63. builder.setMaterial( this, params.renderer );
  64. builder.build( this.vertex, this.fragment );
  65. this.vertexShader = builder.getCode( 'vertex' );
  66. this.fragmentShader = builder.getCode( 'fragment' );
  67. this.defines = builder.defines;
  68. this.uniforms = builder.uniforms;
  69. this.extensions = builder.extensions;
  70. this.updaters = builder.updaters;
  71. this.fog = builder.requires.fog;
  72. this.lights = builder.requires.lights;
  73. this.transparent = builder.requires.transparent || this.blending > NormalBlending;
  74. return this;
  75. };
  76. NodeMaterial.prototype.copy = function ( source ) {
  77. var uuid = this.uuid;
  78. for ( var name in source ) {
  79. this[ name ] = source[ name ];
  80. }
  81. this.uuid = uuid;
  82. if ( source.userData !== undefined ) {
  83. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  84. }
  85. return this;
  86. };
  87. NodeMaterial.prototype.toJSON = function ( meta ) {
  88. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  89. if ( isRootObject ) {
  90. meta = {
  91. nodes: {}
  92. };
  93. }
  94. if ( meta && ! meta.materials ) meta.materials = {};
  95. if ( ! meta.materials[ this.uuid ] ) {
  96. var data = {};
  97. data.uuid = this.uuid;
  98. data.type = this.type;
  99. meta.materials[ data.uuid ] = data;
  100. if ( this.name !== "" ) data.name = this.name;
  101. if ( this.size !== undefined ) data.size = this.size;
  102. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  103. if ( this.blending !== NormalBlending ) data.blending = this.blending;
  104. if ( this.flatShading === true ) data.flatShading = this.flatShading;
  105. if ( this.side !== FrontSide ) data.side = this.side;
  106. if ( this.vertexColors !== NoColors ) data.vertexColors = this.vertexColors;
  107. if ( this.depthFunc !== LessEqualDepth ) data.depthFunc = this.depthFunc;
  108. if ( this.depthTest === false ) data.depthTest = this.depthTest;
  109. if ( this.depthWrite === false ) data.depthWrite = this.depthWrite;
  110. if ( this.linewidth !== 1 ) data.linewidth = this.linewidth;
  111. if ( this.dashSize !== undefined ) data.dashSize = this.dashSize;
  112. if ( this.gapSize !== undefined ) data.gapSize = this.gapSize;
  113. if ( this.scale !== undefined ) data.scale = this.scale;
  114. if ( this.dithering === true ) data.dithering = true;
  115. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  116. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  117. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  118. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  119. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  120. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
  121. if ( this.morphTargets === true ) data.morphTargets = true;
  122. if ( this.skinning === true ) data.skinning = true;
  123. if ( this.visible === false ) data.visible = false;
  124. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  125. data.fog = this.fog;
  126. data.lights = this.lights;
  127. data.vertex = this.vertex.toJSON( meta ).uuid;
  128. data.fragment = this.fragment.toJSON( meta ).uuid;
  129. }
  130. meta.material = this.uuid;
  131. return meta;
  132. };
  133. export { NodeMaterial };