webgl_shaders_sky.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - shaders - sky sun shader</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #000;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. font-weight: bold;
  14. background-color: #fff;
  15. margin: 0px;
  16. overflow: hidden;
  17. text
  18. }
  19. #info {
  20. color:#333;
  21. text-shadow: 1px 1px #fff;
  22. position: absolute;
  23. top: 0px; width: 100%;
  24. padding: 5px;
  25. }
  26. a {
  27. color: #333;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - webgl sky + sun shader
  33. <br/><a href="https://plus.google.com/117614030945250277079/posts/MYkgKdvLhNj">More info</a> by <a href="http://twitter.com/blurspline">@blurspline</a>
  34. </div>
  35. <script src="../build/three.min.js"></script>
  36. <script src="js/controls/TrackballControls.js"></script>
  37. <script src="js/SkyShader.js"></script>
  38. <script src="js/Detector.js"></script>
  39. <script src="js/libs/stats.min.js"></script>
  40. <script src="js/libs/dat.gui.min.js"></script>
  41. <script>
  42. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  43. var container, stats;
  44. var camera, controls, scene, renderer;
  45. var sky, sunSphere;
  46. init();
  47. animate();
  48. function initSky(){
  49. // Add Sky Mesh
  50. sky = new THREE.Sky();
  51. scene.add( sky.mesh );
  52. // Add Sun Helper
  53. sunSphere = new THREE.Mesh( new THREE.SphereGeometry( 20000, 30, 30 ),
  54. new THREE.MeshBasicMaterial({color: 0xffffff, wireframe: false }));
  55. sunSphere.position.y = -700000;
  56. sunSphere.visible = true;
  57. scene.add( sunSphere );
  58. /// GUI
  59. var effectController = {
  60. turbidity: 10,
  61. reileigh: 2,
  62. mieCoefficient: 0.005,
  63. mieDirectionalG: 0.8,
  64. luminance: 1,
  65. inclination: 0.49, // elevation / inclination
  66. azimuth: 0.25, // Facing front,
  67. sun: !true
  68. }
  69. var distance = 400000;
  70. function guiChanged() {
  71. var uniforms = sky.uniforms;
  72. uniforms.turbidity.value = effectController.turbidity;
  73. uniforms.reileigh.value = effectController.reileigh;
  74. uniforms.luminance.value = effectController.luminance;
  75. uniforms.mieCoefficient.value = effectController.mieCoefficient;
  76. uniforms.mieDirectionalG.value = effectController.mieDirectionalG;
  77. var theta = Math.PI * (effectController.inclination - 0.5);
  78. var phi = 2 * Math.PI * (effectController.azimuth - 0.5);
  79. sunSphere.position.x = distance * Math.cos(phi);
  80. sunSphere.position.y = distance * Math.sin(phi) * Math.sin(theta);
  81. sunSphere.position.z = distance * Math.sin(phi) * Math.cos(theta);
  82. sunSphere.visible = effectController.sun;
  83. sky.uniforms.sunPosition.value.copy(sunSphere.position);
  84. }
  85. var gui = new dat.GUI();
  86. gui.add( effectController, "turbidity", 1.0, 20.0, 0.1 ).onChange( guiChanged );
  87. gui.add( effectController, "reileigh", 0.0, 4, 0.001 ).onChange( guiChanged );
  88. gui.add( effectController, "mieCoefficient", 0.0, 0.1, 0.001 ).onChange( guiChanged );
  89. gui.add( effectController, "mieDirectionalG", 0.0, 1, 0.001 ).onChange( guiChanged );
  90. gui.add( effectController, "luminance", 0.0, 2).onChange( guiChanged );;
  91. gui.add( effectController, "inclination", 0, 1, 0.0001).onChange( guiChanged );
  92. gui.add( effectController, "azimuth", 0, 1, 0.0001).onChange( guiChanged );
  93. gui.add( effectController, "sun").onChange( guiChanged );
  94. guiChanged();
  95. camera.lookAt(sunSphere.position)
  96. }
  97. function init() {
  98. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.5, 2000000 );
  99. camera.position.z = 2000;
  100. camera.position.y = 100;
  101. camera.setLens(20);
  102. scene = new THREE.Scene();
  103. var size = 500;
  104. var geometryLines = new THREE.BoxGeometry( size, size, size );
  105. var geometryPlane = new THREE.PlaneGeometry( size * 10, size * 10, 1, 1);
  106. geometryPlane.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
  107. var materialLines = new THREE.MeshBasicMaterial( { wireframe: true } );
  108. meshLines = new THREE.Mesh( geometryLines, materialLines );
  109. // scene.add( meshLines );
  110. scene.add( new THREE.Mesh( geometryPlane, materialLines ) );
  111. initSky();
  112. renderer = new THREE.WebGLRenderer( { antialias: false } );
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. document.body.appendChild( renderer.domElement );
  115. controls = new THREE.TrackballControls( camera, renderer.domElement );
  116. stats = new Stats();
  117. stats.domElement.style.position = 'absolute';
  118. stats.domElement.style.top = '0px';
  119. stats.domElement.style.zIndex = 100;
  120. document.body.appendChild( stats.domElement );
  121. //
  122. window.addEventListener( 'resize', onWindowResize, false );
  123. }
  124. function onWindowResize() {
  125. camera.aspect = window.innerWidth / window.innerHeight;
  126. camera.updateProjectionMatrix();
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. render();
  129. }
  130. var time = 0;
  131. function animate() {
  132. time = Date.now();
  133. requestAnimationFrame( animate );
  134. controls.update();
  135. render();
  136. }
  137. function render() {
  138. renderer.render( scene, camera );
  139. stats.update();
  140. }
  141. </script>
  142. </body>
  143. </html>