webgl_lightshafts.html 5.6 KB

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