NodeMaterial.js 5.0 KB

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