webgl_lightshafts.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - Light Shafts</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. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. z-index: 100;
  22. display:block;
  23. }
  24. #info a {
  25. color: #ffffff;
  26. font-weight: bold;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Light Shafts -
  33. Model by <a href="https://skfb.ly/6ICER" target="_blank" rel="noopener">Splodeman</a><br />
  34. </div>
  35. <script src="../build/three.js"></script>
  36. <script src="js/controls/OrbitControls.js"></script>
  37. <script src="js/loaders/GLTFLoader.js"></script>
  38. <script src="js/WebGL.js"></script>
  39. <script type="x-shader/x-vertex" id="vertexShader">
  40. #include <common>
  41. uniform float speed;
  42. uniform float time;
  43. uniform float timeOffset;
  44. varying vec2 vUv;
  45. varying float vAlpha;
  46. void main() {
  47. vec3 pos = position;
  48. float l = ( time * speed * 0.01 ) + timeOffset;
  49. float f = fract( l ); // linear time factor [0,1)
  50. float a = f * f; // quadratic time factor [0,1)
  51. // slightly animate the vertices of light shaft if necessary
  52. // pos.x += cos( l * 20.0 ) * sin( l * 10.0 );
  53. vAlpha = saturate( 0.7 + min( 1.0, a * 10.0 ) * ( sin( a * 40.0 ) * 0.25 ) );
  54. vUv = uv;
  55. gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 );
  56. }
  57. </script>
  58. <script type="x-shader/x-fragment" id="fragmentShader">
  59. uniform float attenuation;
  60. uniform vec3 color;
  61. uniform sampler2D texture;
  62. varying vec2 vUv;
  63. varying float vAlpha;
  64. void main() {
  65. vec4 textureColor = texture2D( texture, vUv );
  66. gl_FragColor = vec4( textureColor.rgb * color.rgb, textureColor.a * vAlpha );
  67. gl_FragColor.a *= pow( gl_FragCoord.z, attenuation );
  68. }
  69. </script>
  70. <script>
  71. if ( WEBGL.isWebGLAvailable() === false ) {
  72. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  73. }
  74. var container, controls, clock;
  75. var camera, scene, renderer, uniforms;
  76. init();
  77. animate();
  78. function init() {
  79. container = document.createElement( 'div' );
  80. document.body.appendChild( container );
  81. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  82. camera.position.set( 3.2, 3, 3.7 );
  83. controls = new THREE.OrbitControls( camera );
  84. controls.target.set( 0.5, 1.5, 0 );
  85. controls.minDistance = 2;
  86. controls.maxDistance = 20;
  87. controls.update();
  88. clock = new THREE.Clock();
  89. scene = new THREE.Scene();
  90. scene.background = new THREE.Color( 0xcf8b74 );
  91. var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  92. light.position.set( 0, 20, 0 );
  93. scene.add( light );
  94. light = new THREE.DirectionalLight( 0xffffff );
  95. light.position.set( 0, 20, 10 );
  96. scene.add( light );
  97. // light shafts definition
  98. var textureLoader = new THREE.TextureLoader();
  99. var texture = textureLoader.load( 'textures/lightShaft.png' );
  100. uniforms = {
  101. // controls how fast the ray attenuates when the camera comes closer
  102. attenuation: {
  103. value: 10
  104. },
  105. // controls the speed of the animation
  106. speed: {
  107. value: 2
  108. },
  109. // the color of the ray
  110. color: {
  111. value: new THREE.Color( 0xdadc9f )
  112. },
  113. // the visual representation of the ray highly depends on the used texture
  114. texture: {
  115. value: texture
  116. },
  117. // global time value for animation
  118. time: {
  119. value: 0
  120. },
  121. // individual time offset so rays are animated differently if necessary
  122. timeOffset: {
  123. value: 0
  124. }
  125. };
  126. var lightShaftMaterial = new THREE.ShaderMaterial( {
  127. uniforms: uniforms,
  128. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  129. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  130. blending: THREE.AdditiveBlending,
  131. depthWrite: false,
  132. transparent: true,
  133. side: THREE.DoubleSide
  134. } );
  135. var lightShaftGeometry = new THREE.PlaneBufferGeometry( 0.5, 5 );
  136. // model
  137. var loader = new THREE.GLTFLoader().setPath( 'models/gltf/' );
  138. loader.load( 'tree.glb', function ( gltf ) {
  139. gltf.scene.traverse( function ( child ) {
  140. if ( child.isMesh ) {
  141. child.material.transparent = false;
  142. child.material.alphaTest = 0.5;
  143. }
  144. } );
  145. scene.add( gltf.scene );
  146. // when the model is loaded, add light shafts
  147. for ( var i = 0; i < 5; i ++ ) {
  148. var lightShaft = new THREE.Mesh( lightShaftGeometry, lightShaftMaterial );
  149. lightShaft.position.x = - 1 + 1.5 * Math.sign( ( i % 2 ) );
  150. lightShaft.position.y = 2;
  151. lightShaft.position.z = - 1.5 + ( i * 0.5 );
  152. lightShaft.rotation.y = Math.PI * 0.2;
  153. lightShaft.rotation.z = Math.PI * - ( 0.15 + 0.1 * Math.random() );
  154. scene.add( lightShaft );
  155. }
  156. } );
  157. //
  158. renderer = new THREE.WebGLRenderer( { antialias: true } );
  159. renderer.setPixelRatio( window.devicePixelRatio );
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. renderer.gammaOutput = true;
  162. renderer.gammaFactor = 2.2;
  163. container.appendChild( renderer.domElement );
  164. window.addEventListener( 'resize', onWindowResize, false );
  165. }
  166. function onWindowResize() {
  167. camera.aspect = window.innerWidth / window.innerHeight;
  168. camera.updateProjectionMatrix();
  169. renderer.setSize( window.innerWidth, window.innerHeight );
  170. }
  171. //
  172. function animate() {
  173. requestAnimationFrame( animate );
  174. const delta = clock.getDelta();
  175. uniforms.time.value += delta;
  176. renderer.render( scene, camera );
  177. }
  178. </script>
  179. </body>
  180. </html>