ShaderSprite.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. *
  4. */
  5. THREE.ShaderSprite = {
  6. 'sprite': {
  7. vertexShader: [
  8. "uniform int useScreenCoordinates;",
  9. "uniform int affectedByDistance;",
  10. "uniform vec3 screenPosition;",
  11. "uniform mat4 modelViewMatrix;",
  12. "uniform mat4 projectionMatrix;",
  13. "uniform float rotation;",
  14. "uniform vec2 scale;",
  15. "uniform vec2 alignment;",
  16. "uniform vec2 uvOffset;",
  17. "uniform vec2 uvScale;",
  18. "attribute vec2 position;",
  19. "attribute vec2 uv;",
  20. "varying vec2 vUV;",
  21. "void main() {",
  22. "vUV = uvOffset + uv * uvScale;",
  23. "vec2 alignedPosition = position + alignment;",
  24. "vec2 rotatedPosition;",
  25. "rotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;",
  26. "rotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;",
  27. "vec4 finalPosition;",
  28. "if( useScreenCoordinates != 0 ) {",
  29. "finalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );",
  30. "} else {",
  31. "finalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
  32. "finalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );",
  33. "}",
  34. "gl_Position = finalPosition;",
  35. "}"
  36. ].join( "\n" ),
  37. fragmentShader: [
  38. "#ifdef GL_ES",
  39. "precision highp float;",
  40. "#endif",
  41. "uniform vec3 color;",
  42. "uniform sampler2D map;",
  43. "uniform float opacity;",
  44. "varying vec2 vUV;",
  45. "void main() {",
  46. "vec4 texture = texture2D( map, vUV );",
  47. "gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );",
  48. "}"
  49. ].join( "\n" )
  50. }
  51. };