NodeMaterial.js 5.3 KB

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