SpriteNode.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { GLNode } from '../../core/GLNode.js';
  5. import { ColorNode } from '../../inputs/ColorNode.js';
  6. function SpriteNode() {
  7. GLNode.call( this );
  8. this.color = new ColorNode( 0xEEEEEE );
  9. this.spherical = true;
  10. };
  11. SpriteNode.prototype = Object.create( GLNode.prototype );
  12. SpriteNode.prototype.constructor = SpriteNode;
  13. SpriteNode.prototype.nodeType = "Sprite";
  14. SpriteNode.prototype.build = function ( builder ) {
  15. var output, code;
  16. builder.define( 'SPRITE' );
  17. builder.requires.lights = false;
  18. builder.requires.transparent = this.alpha != undefined;
  19. if ( builder.isShader( 'vertex' ) ) {
  20. var transform = this.transform ? this.transform.parseAndBuildCode( builder, 'v3', { cache: 'transform' } ) : undefined;
  21. builder.mergeUniform( THREE.UniformsUtils.merge( [
  22. THREE.UniformsLib[ "fog" ]
  23. ] ) );
  24. builder.addParsCode( [
  25. "#include <fog_pars_vertex>"
  26. ].join( "\n" ) );
  27. output = [
  28. "#include <begin_vertex>"
  29. ];
  30. if ( transform ) {
  31. output.push(
  32. transform.code,
  33. transform.result ? "transformed = " + transform.result + ";" : ''
  34. );
  35. }
  36. output.push(
  37. "#include <project_vertex>",
  38. "#include <fog_vertex>",
  39. 'mat4 modelViewMtx = modelViewMatrix;',
  40. 'mat4 modelMtx = modelMatrix;',
  41. // ignore position from modelMatrix (use vary position)
  42. 'modelMtx[3][0] = 0.0;',
  43. 'modelMtx[3][1] = 0.0;',
  44. 'modelMtx[3][2] = 0.0;'
  45. );
  46. if ( ! this.spherical ) {
  47. output.push(
  48. 'modelMtx[1][1] = 1.0;'
  49. );
  50. }
  51. output.push(
  52. // http://www.geeks3d.com/20140807/billboarding-vertex-shader-glsl/
  53. // First colunm.
  54. 'modelViewMtx[0][0] = 1.0;',
  55. 'modelViewMtx[0][1] = 0.0;',
  56. 'modelViewMtx[0][2] = 0.0;'
  57. );
  58. if ( this.spherical ) {
  59. output.push(
  60. // Second colunm.
  61. 'modelViewMtx[1][0] = 0.0;',
  62. 'modelViewMtx[1][1] = 1.0;',
  63. 'modelViewMtx[1][2] = 0.0;'
  64. );
  65. }
  66. output.push(
  67. // Thrid colunm.
  68. 'modelViewMtx[2][0] = 0.0;',
  69. 'modelViewMtx[2][1] = 0.0;',
  70. 'modelViewMtx[2][2] = 1.0;',
  71. // apply
  72. 'gl_Position = projectionMatrix * modelViewMtx * modelMtx * vec4( transformed, 1.0 );'
  73. );
  74. } else {
  75. builder.addParsCode( [
  76. "#include <fog_pars_fragment>",
  77. ].join( "\n" ) );
  78. // parse all nodes to reuse generate codes
  79. this.color.parse( builder, { slot: 'color' } );
  80. if ( this.alpha ) this.alpha.parse( builder );
  81. // build code
  82. var color = this.color.buildCode( builder, 'c', { slot: 'color' } );
  83. var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
  84. output = [ color.code ];
  85. if ( alpha ) {
  86. output.push(
  87. alpha.code,
  88. "gl_FragColor = vec4( " + color.result + ", " + alpha.result + " );"
  89. );
  90. } else {
  91. output.push( "gl_FragColor = vec4( " + color.result + ", 1.0 );" );
  92. }
  93. output.push( "#include <fog_fragment>" );
  94. }
  95. return output.join( "\n" );
  96. };
  97. SpriteNode.prototype.copy = function ( source ) {
  98. GLNode.prototype.copy.call( this, source );
  99. // vertex
  100. if ( source.transform ) this.transform = source.transform;
  101. // fragment
  102. this.color = source.color;
  103. if ( source.spherical !== undefined ) this.spherical = source.transform;
  104. if ( source.alpha ) this.alpha = source.alpha;
  105. };
  106. SpriteNode.prototype.toJSON = function ( meta ) {
  107. var data = this.getJSONNode( meta );
  108. if ( ! data ) {
  109. data = this.createJSONNode( meta );
  110. // vertex
  111. if ( this.transform ) data.transform = this.transform.toJSON( meta ).uuid;
  112. // fragment
  113. data.color = this.color.toJSON( meta ).uuid;
  114. if ( this.spherical === false ) data.spherical = false;
  115. if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
  116. }
  117. return data;
  118. };
  119. export { SpriteNode };