webgl_lightshafts.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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="https://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 colorTexture;
  37. varying vec2 vUv;
  38. varying float vAlpha;
  39. void main() {
  40. vec4 textureColor = texture2D( colorTexture, 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 * as THREE from '../build/three.module.js';
  47. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  48. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  49. var container, controls, clock;
  50. var camera, scene, renderer, uniforms;
  51. init();
  52. animate();
  53. function init() {
  54. container = document.createElement( 'div' );
  55. document.body.appendChild( container );
  56. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  57. camera.position.set( 3.2, 3, 3.7 );
  58. clock = new THREE.Clock();
  59. scene = new THREE.Scene();
  60. scene.background = new THREE.Color( 0xcf8b74 );
  61. var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  62. light.position.set( 0, 20, 0 );
  63. scene.add( light );
  64. light = new THREE.DirectionalLight( 0xffffff );
  65. light.position.set( 0, 20, 10 );
  66. scene.add( light );
  67. // light shafts definition
  68. var textureLoader = new THREE.TextureLoader();
  69. var texture = textureLoader.load( 'textures/lightShaft.png' );
  70. uniforms = {
  71. // controls how fast the ray attenuates when the camera comes closer
  72. attenuation: {
  73. value: 10
  74. },
  75. // controls the speed of the animation
  76. speed: {
  77. value: 2
  78. },
  79. // the color of the ray
  80. color: {
  81. value: new THREE.Color( 0xdadc9f )
  82. },
  83. // the visual representation of the ray highly depends on the used texture
  84. colorTexture: {
  85. value: texture
  86. },
  87. // global time value for animation
  88. time: {
  89. value: 0
  90. },
  91. // individual time offset so rays are animated differently if necessary
  92. timeOffset: {
  93. value: 0
  94. }
  95. };
  96. var lightShaftMaterial = new THREE.ShaderMaterial( {
  97. uniforms: uniforms,
  98. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  99. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  100. blending: THREE.AdditiveBlending,
  101. depthWrite: false,
  102. transparent: true,
  103. side: THREE.DoubleSide
  104. } );
  105. var lightShaftGeometry = new THREE.PlaneBufferGeometry( 0.5, 5 );
  106. // model
  107. var loader = new GLTFLoader().setPath( 'models/gltf/Tree/' );
  108. loader.load( 'tree.glb', function ( gltf ) {
  109. gltf.scene.traverse( function ( child ) {
  110. if ( child.isMesh ) {
  111. child.material.transparent = false;
  112. child.material.alphaTest = 0.5;
  113. }
  114. } );
  115. scene.add( gltf.scene );
  116. // when the model is loaded, add light shafts
  117. for ( var i = 0; i < 5; i ++ ) {
  118. var lightShaft = new THREE.Mesh( lightShaftGeometry, lightShaftMaterial );
  119. lightShaft.position.x = - 1 + 1.5 * Math.sign( ( i % 2 ) );
  120. lightShaft.position.y = 2;
  121. lightShaft.position.z = - 1.5 + ( i * 0.5 );
  122. lightShaft.rotation.y = Math.PI * 0.2;
  123. lightShaft.rotation.z = Math.PI * - ( 0.15 + 0.1 * Math.random() );
  124. scene.add( lightShaft );
  125. }
  126. } );
  127. //
  128. renderer = new THREE.WebGLRenderer( { antialias: true } );
  129. renderer.setPixelRatio( window.devicePixelRatio );
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. renderer.outputEncoding = THREE.sRGBEncoding;
  132. container.appendChild( renderer.domElement );
  133. controls = new OrbitControls( camera, renderer.domElement );
  134. controls.target.set( 0.5, 1.5, 0 );
  135. controls.minDistance = 2;
  136. controls.maxDistance = 20;
  137. controls.update();
  138. window.addEventListener( 'resize', onWindowResize, false );
  139. }
  140. function onWindowResize() {
  141. camera.aspect = window.innerWidth / window.innerHeight;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. }
  145. //
  146. function animate() {
  147. requestAnimationFrame( animate );
  148. const delta = clock.getDelta();
  149. uniforms.time.value += delta;
  150. renderer.render( scene, camera );
  151. }
  152. </script>
  153. </body>
  154. </html>