SpriteNode.js 2.7 KB

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