NodeMaterial.js 5.2 KB

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