aframe-sun-sky.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /******/ (function(modules) { // webpackBootstrap
  2. /******/ // The module cache
  3. /******/ var installedModules = {};
  4. /******/ // The require function
  5. /******/ function __webpack_require__(moduleId) {
  6. /******/ // Check if module is in cache
  7. /******/ if(installedModules[moduleId])
  8. /******/ return installedModules[moduleId].exports;
  9. /******/ // Create a new module (and put it into the cache)
  10. /******/ var module = installedModules[moduleId] = {
  11. /******/ exports: {},
  12. /******/ id: moduleId,
  13. /******/ loaded: false
  14. /******/ };
  15. /******/ // Execute the module function
  16. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  17. /******/ // Flag the module as loaded
  18. /******/ module.loaded = true;
  19. /******/ // Return the exports of the module
  20. /******/ return module.exports;
  21. /******/ }
  22. /******/ // expose the modules object (__webpack_modules__)
  23. /******/ __webpack_require__.m = modules;
  24. /******/ // expose the module cache
  25. /******/ __webpack_require__.c = installedModules;
  26. /******/ // __webpack_public_path__
  27. /******/ __webpack_require__.p = "";
  28. /******/ // Load entry module and return exports
  29. /******/ return __webpack_require__(0);
  30. /******/ })
  31. /************************************************************************/
  32. /******/ ([
  33. /* 0 */
  34. /***/ function(module, exports, __webpack_require__) {
  35. var vertexShader = __webpack_require__(1);
  36. var fragmentShader = __webpack_require__(2);
  37. AFRAME.registerShader('sunSky', {
  38. schema: {
  39. luminance: {default: 1, max: 0, min: 2, is: 'uniform'},
  40. mieCoefficient: {default: 0.005, min: 0, max: 0.1, is: 'uniform'},
  41. mieDirectionalG: {default: 0.8, min: 0, max: 1, is: 'uniform'},
  42. reileigh: {default: 1, max: 0, min: 4, is: 'uniform'},
  43. sunPosition: {type: 'vec3', default: '0 0 -1', is: 'uniform'},
  44. turbidity: {default: 2, max: 0, min: 20, is: 'uniform'}
  45. },
  46. vertexShader: vertexShader,
  47. fragmentShader: fragmentShader
  48. });
  49. AFRAME.registerPrimitive('a-sun-sky', {
  50. defaultComponents: {
  51. geometry: {
  52. primitive: 'sphere',
  53. radius: 5000,
  54. segmentsWidth: 64,
  55. segmentsHeight: 20
  56. },
  57. material: {
  58. shader: 'sunSky'
  59. },
  60. scale: '-1 1 1'
  61. },
  62. mappings: {
  63. luminance: 'material.luminance',
  64. mieCoefficient: 'material.mieCoefficient',
  65. mieDirectionalG: 'material.mieDirectionalG',
  66. reileigh: 'material.reileigh',
  67. sunPosition: 'material.sunPosition',
  68. turbidity: 'material.turbidity'
  69. }
  70. });
  71. /***/ },
  72. /* 1 */
  73. /***/ function(module, exports) {
  74. module.exports = "varying vec3 vWorldPosition;\n\nvoid main() {\n vec4 worldPosition = modelMatrix * vec4(position, 1.0);\n vWorldPosition = worldPosition.xyz;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}\n"
  75. /***/ },
  76. /* 2 */
  77. /***/ function(module, exports) {
  78. module.exports = "uniform sampler2D skySampler;\nuniform vec3 sunPosition;\nvarying vec3 vWorldPosition;\n\nvec3 cameraPos = vec3(0., 0., 0.);\n\nuniform float luminance;\nuniform float turbidity;\nuniform float reileigh;\nuniform float mieCoefficient;\nuniform float mieDirectionalG;\n\n// constants for atmospheric scattering\nconst float e = 2.71828182845904523536028747135266249775724709369995957;\nconst float pi = 3.141592653589793238462643383279502884197169;\n\nconst float n = 1.0003; // refractive index of air\nconst float N = 2.545E25; // number of molecules per unit volume for air at\n// 288.15K and 1013mb (sea level -45 celsius)\nconst float pn = 0.035; // depolatization factor for standard air\n\n// wavelength of used primaries, according to preetham\nconst vec3 lambda = vec3(680E-9, 550E-9, 450E-9);\n\n// mie stuff\n// K coefficient for the primaries\nconst vec3 K = vec3(0.686, 0.678, 0.666);\nconst float v = 4.0;\n\n// optical length at zenith for molecules\nconst float rayleighZenithLength = 8.4E3;\nconst float mieZenithLength = 1.25E3;\nconst vec3 up = vec3(0.0, 1.0, 0.0);\n\nconst float EE = 1000.0;\nconst float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324;\n// 66 arc seconds -> degrees, and the cosine of that\n\n// earth shadow hack\nconst float cutoffAngle = pi/1.95;\nconst float steepness = 1.5;\n\nvec3 totalRayleigh(vec3 lambda)\n{\n return (8.0 * pow(pi, 3.0) * pow(pow(n, 2.0) - 1.0, 2.0) * (6.0 + 3.0 * pn)) / (3.0 * N * pow(lambda, vec3(4.0)) * (6.0 - 7.0 * pn));\n}\n\n// see http://blenderartists.org/forum/showthread.php?321110-Shaders-and-Skybox-madness\n// A simplied version of the total Rayleigh scattering to works on browsers that use ANGLE\nvec3 simplifiedRayleigh()\n{\n return 0.0005 / vec3(94, 40, 18);\n}\n\nfloat rayleighPhase(float cosTheta)\n{\n return (3.0 / (16.0*pi)) * (1.0 + pow(cosTheta, 2.0));\n}\n\nvec3 totalMie(vec3 lambda, vec3 K, float T)\n{\n float c = (0.2 * T ) * 10E-18;\n return 0.434 * c * pi * pow((2.0 * pi) / lambda, vec3(v - 2.0)) * K;\n}\n\nfloat hgPhase(float cosTheta, float g)\n{\n return (1.0 / (4.0*pi)) * ((1.0 - pow(g, 2.0)) / pow(1.0 - 2.0*g*cosTheta + pow(g, 2.0), 1.5));\n}\n\nfloat sunIntensity(float zenithAngleCos)\n{\n return EE * max(0.0, 1.0 - exp(-((cutoffAngle - acos(zenithAngleCos))/steepness)));\n}\n\n// Filmic ToneMapping http://filmicgames.com/archives/75\nfloat A = 0.15;\nfloat B = 0.50;\nfloat C = 0.10;\nfloat D = 0.20;\nfloat E = 0.02;\nfloat F = 0.30;\nfloat W = 1000.0;\n\nvec3 Uncharted2Tonemap(vec3 x)\n{\n return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;\n}\n\nvoid main()\n{\n float sunfade = 1.0-clamp(1.0-exp((sunPosition.y/450000.0)),0.0,1.0);\n\n float reileighCoefficient = reileigh - (1.0* (1.0-sunfade));\n\n vec3 sunDirection = normalize(sunPosition);\n\n float sunE = sunIntensity(dot(sunDirection, up));\n\n // extinction (absorbtion + out scattering)\n // rayleigh coefficients\n\n vec3 betaR = simplifiedRayleigh() * reileighCoefficient;\n\n // mie coefficients\n vec3 betaM = totalMie(lambda, K, turbidity) * mieCoefficient;\n\n // optical length\n // cutoff angle at 90 to avoid singularity in next formula.\n float zenithAngle = acos(max(0.0, dot(up, normalize(vWorldPosition - cameraPos))));\n float sR = rayleighZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));\n float sM = mieZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));\n\n // combined extinction factor\n vec3 Fex = exp(-(betaR * sR + betaM * sM));\n\n // in scattering\n float cosTheta = dot(normalize(vWorldPosition - cameraPos), sunDirection);\n\n float rPhase = rayleighPhase(cosTheta*0.5+0.5);\n vec3 betaRTheta = betaR * rPhase;\n\n float mPhase = hgPhase(cosTheta, mieDirectionalG);\n vec3 betaMTheta = betaM * mPhase;\n\n vec3 Lin = pow(sunE * ((betaRTheta + betaMTheta) / (betaR + betaM)) * (1.0 - Fex),vec3(1.5));\n Lin *= mix(vec3(1.0),pow(sunE * ((betaRTheta + betaMTheta) / (betaR + betaM)) * Fex,vec3(1.0/2.0)),clamp(pow(1.0-dot(up, sunDirection),5.0),0.0,1.0));\n\n //nightsky\n vec3 direction = normalize(vWorldPosition - cameraPos);\n float theta = acos(direction.y); // elevation --> y-axis, [-pi/2, pi/2]\n float phi = atan(direction.z, direction.x); // azimuth --> x-axis [-pi/2, pi/2]\n vec2 uv = vec2(phi, theta) / vec2(2.0*pi, pi) + vec2(0.5, 0.0);\n // vec3 L0 = texture2D(skySampler, uv).rgb+0.1 * Fex;\n vec3 L0 = vec3(0.1) * Fex;\n\n // composition + solar disc\n float sundisk = smoothstep(sunAngularDiameterCos,sunAngularDiameterCos+0.00002,cosTheta);\n L0 += (sunE * 19000.0 * Fex)*sundisk;\n\n vec3 whiteScale = 1.0/Uncharted2Tonemap(vec3(W));\n\n vec3 texColor = (Lin+L0);\n texColor *= 0.04 ;\n texColor += vec3(0.0,0.001,0.0025)*0.3;\n\n float g_fMaxLuminance = 1.0;\n float fLumScaled = 0.1 / luminance;\n float fLumCompressed = (fLumScaled * (1.0 + (fLumScaled / (g_fMaxLuminance * g_fMaxLuminance)))) / (1.0 + fLumScaled);\n\n float ExposureBias = fLumCompressed;\n\n vec3 curr = Uncharted2Tonemap((log2(2.0/pow(luminance,4.0)))*texColor);\n vec3 color = curr*whiteScale;\n\n vec3 retColor = pow(color,vec3(1.0/(1.2+(1.2*sunfade))));\n\n gl_FragColor.rgb = retColor;\n\n gl_FragColor.a = 1.0;\n}\n"
  79. /***/ }
  80. /******/ ]);