BasicNode.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { Node } from '../../core/Node.js';
  5. import { ColorNode } from '../../inputs/ColorNode.js';
  6. function BasicNode() {
  7. Node.call( this );
  8. this.color = new ColorNode( 0xFFFFFF );
  9. }
  10. BasicNode.prototype = Object.create( Node.prototype );
  11. BasicNode.prototype.constructor = BasicNode;
  12. BasicNode.prototype.nodeType = "Basic";
  13. BasicNode.prototype.generate = function ( builder ) {
  14. var code;
  15. if ( builder.isShader( 'vertex' ) ) {
  16. var position = this.position ? this.position.analyzeAndFlow( builder, 'v3', { cache: 'position' } ) : undefined;
  17. var output = [
  18. "vec3 transformed = position;"
  19. ];
  20. if ( position ) {
  21. output.push(
  22. position.code,
  23. position.result ? "gl_Position = " + position.result + ";" : ''
  24. );
  25. } else {
  26. output.push( "gl_Position = projectionMatrix * modelViewMatrix * vec4(transformed, 1.0)" );
  27. }
  28. code = output.join( "\n" );
  29. } else {
  30. // Analyze all nodes to reuse generate codes
  31. this.color.analyze( builder, { slot: 'color' } );
  32. if ( this.alpha ) this.alpha.analyze( builder );
  33. // Build code
  34. var color = this.color.flow( builder, 'c', { slot: 'color' } );
  35. var alpha = this.alpha ? this.alpha.flow( builder, 'f' ) : undefined;
  36. builder.requires.transparent = alpha !== undefined;
  37. var output = [
  38. color.code,
  39. ];
  40. if ( alpha ) {
  41. output.push(
  42. alpha.code,
  43. '#ifdef ALPHATEST',
  44. ' if ( ' + alpha.result + ' <= ALPHATEST ) discard;',
  45. '#endif'
  46. );
  47. }
  48. if ( alpha ) {
  49. output.push( "gl_FragColor = vec4(" + color.result + ", " + alpha.result + " );" );
  50. } else {
  51. output.push( "gl_FragColor = vec4(" + color.result + ", 1.0 );" );
  52. }
  53. code = output.join( "\n" );
  54. }
  55. return code;
  56. };
  57. BasicNode.prototype.copy = function ( source ) {
  58. Node.prototype.copy.call( this, source );
  59. this.position = source.position;
  60. this.color = source.color;
  61. this.alpha = source.alpha;
  62. return this;
  63. };
  64. BasicNode.prototype.toJSON = function ( meta ) {
  65. var data = this.getJSONNode( meta );
  66. if ( ! data ) {
  67. data = this.createJSONNode( meta );
  68. data.position = this.position.toJSON( meta ).uuid;
  69. data.color = this.color.toJSON( meta ).uuid;
  70. data.alpha = this.alpha.toJSON( meta ).uuid;
  71. }
  72. return data;
  73. };
  74. export { BasicNode };