webgl_lightshafts.html 5.6 KB

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