SpriteNode.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.build = function ( builder ) {
  12. var material = builder.material;
  13. var output, code;
  14. material.define( 'SPRITE' );
  15. material.requestAttribs.light = false;
  16. material.requestAttribs.transparent = this.alpha != undefined;
  17. if ( builder.isShader( 'vertex' ) ) {
  18. var transform = this.transform ? this.transform.parseAndBuildCode( builder, 'v3', { cache: 'transform' } ) : undefined;
  19. material.mergeUniform( THREE.UniformsUtils.merge( [
  20. THREE.UniformsLib[ "fog" ]
  21. ] ) );
  22. material.addVertexPars( [
  23. "#include <fog_pars_vertex>"
  24. ].join( "\n" ) );
  25. output = [
  26. "#include <begin_vertex>"
  27. ];
  28. if ( transform ) {
  29. output.push(
  30. transform.code,
  31. "transformed = " + transform.result + ";"
  32. );
  33. }
  34. output.push(
  35. "#include <project_vertex>",
  36. "#include <fog_vertex>",
  37. 'mat4 modelViewMtx = modelViewMatrix;',
  38. 'mat4 modelMtx = modelMatrix;',
  39. // ignore position from modelMatrix (use vary position)
  40. 'modelMtx[3][0] = 0.0;',
  41. 'modelMtx[3][1] = 0.0;',
  42. 'modelMtx[3][2] = 0.0;'
  43. );
  44. if ( ! this.spherical ) {
  45. output.push(
  46. 'modelMtx[1][1] = 1.0;'
  47. );
  48. }
  49. output.push(
  50. // http://www.geeks3d.com/20140807/billboarding-vertex-shader-glsl/
  51. // First colunm.
  52. 'modelViewMtx[0][0] = 1.0;',
  53. 'modelViewMtx[0][1] = 0.0;',
  54. 'modelViewMtx[0][2] = 0.0;'
  55. );
  56. if ( this.spherical ) {
  57. output.push(
  58. // Second colunm.
  59. 'modelViewMtx[1][0] = 0.0;',
  60. 'modelViewMtx[1][1] = 1.0;',
  61. 'modelViewMtx[1][2] = 0.0;'
  62. );
  63. }
  64. output.push(
  65. // Thrid colunm.
  66. 'modelViewMtx[2][0] = 0.0;',
  67. 'modelViewMtx[2][1] = 0.0;',
  68. 'modelViewMtx[2][2] = 1.0;',
  69. // apply
  70. 'gl_Position = projectionMatrix * modelViewMtx * modelMtx * vec4( position, 1.0 );'
  71. );
  72. } else {
  73. material.addFragmentPars( [
  74. "#include <fog_pars_fragment>",
  75. ].join( "\n" ) );
  76. // parse all nodes to reuse generate codes
  77. this.color.parse( builder, { slot: 'color' } );
  78. if ( this.alpha ) this.alpha.parse( builder );
  79. // build code
  80. var color = this.color.buildCode( builder, 'c', { slot: 'color' } );
  81. var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
  82. output = [ color.code ];
  83. if ( alpha ) {
  84. output.push(
  85. alpha.code,
  86. "gl_FragColor = vec4( " + color.result + ", " + alpha.result + " );"
  87. );
  88. } else {
  89. output.push( "gl_FragColor = vec4( " + color.result + ", 1.0 );" );
  90. }
  91. output.push( "#include <fog_fragment>" );
  92. }
  93. return output.join( "\n" );
  94. };