threex.atmospherematerial.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var THREEx = THREEx || {}
  2. /**
  3. * from http://stemkoski.blogspot.fr/2013/07/shaders-in-threejs-glow-and-halo.html
  4. * @return {[type]} [description]
  5. */
  6. THREEx.createAtmosphereMaterial = function(){
  7. var vertexShader = [
  8. 'uniform float coeficient;',
  9. 'uniform float power;',
  10. 'uniform vec3 viewVector;',
  11. 'varying float intensity;',
  12. 'void main(){',
  13. ' // compute intensity',
  14. ' vec3 vNormal = normalize(normalMatrix * normal);',
  15. // ' vec3 vNormel = normalize( normalMatrix * cameraPosition );',
  16. // ' intensity = pow( coeficient - dot(vNormal, vNormel), power );',
  17. // ' vec3 camPosition= normalize(normalMatrix * cameraPosition);',
  18. // ' intensity = pow( coeficient - dot(vNormal, camPosition), power );',
  19. // ' intensity = pow( coeficient - dot(vNormal, normalize(viewVector)), power );',
  20. ' vec3 vertex2Cam = normalMatrix * cameraPosition;',
  21. ' vertex2Cam = vertex2Cam - (modelViewMatrix * vec4(position, 1.0 )).xyz;',
  22. ' vertex2Cam = normalize(vertex2Cam);',
  23. ' intensity = pow(coeficient - dot(vNormal, vertex2Cam), power);',
  24. ' // set gl_Position',
  25. ' gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);',
  26. '}',
  27. ].join('\n')
  28. var fragmentShader = [
  29. 'uniform vec3 glowColor;',
  30. 'varying float intensity;',
  31. 'void main(){',
  32. ' gl_FragColor = vec4(glowColor, intensity);',
  33. // ' gl_FragColor = vec4(glowColor*intensity, intensity);',
  34. // ' gl_FragColor = vec4(vec3(intensity), 1.0);',
  35. // ' gl_FragColor = vec4(length(normalMatrix[0]), length(normalMatrix[1]), length(normalMatrix[2])), 1.0);',
  36. // ' gl_FragColor = vec4(glowColor, intensity);',
  37. // ' gl_FragColor = vec4(normalize(cameraPosition2), 1.0);',
  38. '}',
  39. ].join('\n')
  40. // create custom material from the shader code above
  41. // that is within specially labeled script tags
  42. var material = new THREE.ShaderMaterial({
  43. uniforms: {
  44. coeficient : {
  45. type : "f",
  46. value : 1.0
  47. },
  48. power : {
  49. type : "f",
  50. value : 2
  51. },
  52. glowColor : {
  53. type : "c",
  54. value : new THREE.Color('pink')
  55. },
  56. viewVector : {
  57. type : "v3",
  58. value : new THREE.Vector3(0,0,1)
  59. },
  60. },
  61. vertexShader : vertexShader,
  62. fragmentShader : fragmentShader,
  63. blending : THREE.AdditiveBlending,
  64. transparent : true,
  65. depthWrite : false,
  66. });
  67. return material
  68. }