ShaderSprite.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author alteredq / http://alteredqualia.com/
  4. *
  5. */
  6. THREE.ShaderSprite = {
  7. 'sprite': {
  8. vertexShader: [
  9. "uniform int useScreenCoordinates;",
  10. "uniform int affectedByDistance;",
  11. "uniform vec3 screenPosition;",
  12. "uniform mat4 modelViewMatrix;",
  13. "uniform mat4 projectionMatrix;",
  14. "uniform float rotation;",
  15. "uniform vec2 scale;",
  16. "uniform vec2 alignment;",
  17. "uniform vec2 uvOffset;",
  18. "uniform vec2 uvScale;",
  19. "attribute vec2 position;",
  20. "attribute vec2 uv;",
  21. "varying vec2 vUV;",
  22. "void main() {",
  23. "vUV = uvOffset + uv * uvScale;",
  24. "vec2 alignedPosition = position + alignment;",
  25. "vec2 rotatedPosition;",
  26. "rotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;",
  27. "rotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;",
  28. "vec4 finalPosition;",
  29. "if( useScreenCoordinates != 0 ) {",
  30. "finalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );",
  31. "} else {",
  32. "finalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
  33. "finalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );",
  34. "}",
  35. "gl_Position = finalPosition;",
  36. "}"
  37. ].join( "\n" ),
  38. fragmentShader: [
  39. "precision mediump float;",
  40. "uniform vec3 color;",
  41. "uniform sampler2D map;",
  42. "uniform float opacity;",
  43. "uniform int fogType;",
  44. "uniform vec3 fogColor;",
  45. "uniform float fogDensity;",
  46. "uniform float fogNear;",
  47. "uniform float fogFar;",
  48. "uniform float alphaTest;",
  49. "varying vec2 vUV;",
  50. "void main() {",
  51. "vec4 texture = texture2D( map, vUV );",
  52. "if ( texture.a < alphaTest ) discard;",
  53. "gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );",
  54. "if ( fogType > 0 ) {",
  55. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  56. "float fogFactor = 0.0;",
  57. "if ( fogType == 1 ) {",
  58. "fogFactor = smoothstep( fogNear, fogFar, depth );",
  59. "} else {",
  60. "const float LOG2 = 1.442695;",
  61. "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
  62. "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
  63. "}",
  64. "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
  65. "}",
  66. "}"
  67. ].join( "\n" )
  68. }
  69. };