BasicNode.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { Node } from '../../core/Node.js';
  2. import { ColorNode } from '../../inputs/ColorNode.js';
  3. function BasicNode() {
  4. Node.call( this );
  5. this.color = new ColorNode( 0xFFFFFF );
  6. }
  7. BasicNode.prototype = Object.create( Node.prototype );
  8. BasicNode.prototype.constructor = BasicNode;
  9. BasicNode.prototype.nodeType = "Basic";
  10. BasicNode.prototype.generate = function ( builder ) {
  11. var code;
  12. if ( builder.isShader( 'vertex' ) ) {
  13. var position = this.position ? this.position.analyzeAndFlow( builder, 'v3', { cache: 'position' } ) : undefined;
  14. builder.addParsCode( [
  15. "varying vec3 vViewPosition;",
  16. "#ifndef FLAT_SHADED",
  17. " varying vec3 vNormal;",
  18. "#endif",
  19. ].join( "\n" ) );
  20. var output = [
  21. "#include <beginnormal_vertex>",
  22. "#include <defaultnormal_vertex>",
  23. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  24. " vNormal = normalize( transformedNormal );",
  25. "#endif",
  26. "#include <begin_vertex>",
  27. ];
  28. if ( position ) {
  29. output.push(
  30. position.code,
  31. position.result ? "transformed = " + position.result + ";" : ''
  32. );
  33. }
  34. output.push(
  35. "#include <morphtarget_vertex>",
  36. "#include <skinning_vertex>",
  37. "#include <project_vertex>",
  38. "#include <fog_vertex>",
  39. "#include <logdepthbuf_vertex>",
  40. "#include <clipping_planes_vertex>",
  41. " vViewPosition = - mvPosition.xyz;",
  42. "#include <worldpos_vertex>",
  43. "#include <shadowmap_vertex>"
  44. );
  45. code = output.join( "\n" );
  46. } else {
  47. // Analyze all nodes to reuse generate codes
  48. this.color.analyze( builder, { slot: 'color' } );
  49. if ( this.alpha ) this.alpha.analyze( builder );
  50. if ( this.mask ) this.mask.analyze( builder );
  51. // Build code
  52. var color = this.color.flow( builder, 'c', { slot: 'color' } );
  53. var alpha = this.alpha ? this.alpha.flow( builder, 'f' ) : undefined;
  54. var mask = this.mask ? this.mask.flow( builder, 'b' ) : undefined;
  55. builder.requires.transparent = alpha !== undefined;
  56. builder.addParsCode( [
  57. "varying vec3 vViewPosition;",
  58. "#ifndef FLAT_SHADED",
  59. " varying vec3 vNormal;",
  60. "#endif",
  61. ].join( "\n" ) );
  62. var output = [
  63. // add before: prevent undeclared normal
  64. "#include <normal_fragment_begin>",
  65. color.code,
  66. ];
  67. if ( mask ) {
  68. output.push(
  69. mask.code,
  70. 'if ( ! ' + mask.result + ' ) discard;'
  71. );
  72. }
  73. if ( alpha ) {
  74. output.push(
  75. alpha.code,
  76. '#ifdef ALPHATEST',
  77. ' if ( ' + alpha.result + ' <= ALPHATEST ) discard;',
  78. '#endif'
  79. );
  80. }
  81. if ( alpha ) {
  82. output.push( "gl_FragColor = vec4(" + color.result + ", " + alpha.result + " );" );
  83. } else {
  84. output.push( "gl_FragColor = vec4(" + color.result + ", 1.0 );" );
  85. }
  86. code = output.join( "\n" );
  87. }
  88. return code;
  89. };
  90. BasicNode.prototype.copy = function ( source ) {
  91. Node.prototype.copy.call( this, source );
  92. this.color = source.color;
  93. if ( source.position ) this.position = source.position;
  94. if ( source.alpha ) this.alpha = source.alpha;
  95. if ( source.mask ) this.mask = source.mask;
  96. return this;
  97. };
  98. BasicNode.prototype.toJSON = function ( meta ) {
  99. var data = this.getJSONNode( meta );
  100. if ( ! data ) {
  101. data = this.createJSONNode( meta );
  102. data.color = this.color.toJSON( meta ).uuid;
  103. if ( this.position ) data.position = this.position.toJSON( meta ).uuid;
  104. if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
  105. if ( this.mask ) data.mask = this.mask.toJSON( meta ).uuid;
  106. }
  107. return data;
  108. };
  109. export { BasicNode };
粤ICP备19079148号