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 sizeAttenuation;",
  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. "uniform vec2 halfViewport;",
  20. "attribute vec2 position;",
  21. "attribute vec2 uv;",
  22. "varying vec2 vUV;",
  23. "void main() {",
  24. "vUV = uvOffset + uv * uvScale;",
  25. "vec2 alignedPosition = ( position + alignment ) * scale;",
  26. "vec2 rotatedPosition;",
  27. "rotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;",
  28. "rotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;",
  29. "vec4 finalPosition;",
  30. "if( useScreenCoordinates != 0 ) {",
  31. "finalPosition = vec4( screenPosition.xy + ( rotatedPosition / halfViewport ), screenPosition.z, 1.0 );",
  32. "} else {",
  33. "finalPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
  34. "finalPosition.xy += rotatedPosition * ( sizeAttenuation == 1 ? 1.0 : finalPosition.z );",
  35. "finalPosition = projectionMatrix * finalPosition;",
  36. "}",
  37. "gl_Position = finalPosition;",
  38. "}"
  39. ].join( "\n" ),
  40. fragmentShader: [
  41. "uniform vec3 color;",
  42. "uniform sampler2D map;",
  43. "uniform float opacity;",
  44. "uniform int fogType;",
  45. "uniform vec3 fogColor;",
  46. "uniform float fogDensity;",
  47. "uniform float fogNear;",
  48. "uniform float fogFar;",
  49. "uniform float alphaTest;",
  50. "varying vec2 vUV;",
  51. "void main() {",
  52. "vec4 texture = texture2D( map, vUV );",
  53. "if ( texture.a < alphaTest ) discard;",
  54. "gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );",
  55. "if ( fogType > 0 ) {",
  56. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  57. "float fogFactor = 0.0;",
  58. "if ( fogType == 1 ) {",
  59. "fogFactor = smoothstep( fogNear, fogFar, depth );",
  60. "} else {",
  61. "const float LOG2 = 1.442695;",
  62. "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
  63. "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
  64. "}",
  65. "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
  66. "}",
  67. "}"
  68. ].join( "\n" )
  69. }
  70. };
粤ICP备19079148号