SpriteNode.js 3.2 KB

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