123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - Light Shafts</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Light Shafts<br/>
- Model by <a href="https://skfb.ly/6ICER" target="_blank" rel="noopener">Splodeman</a><br />
- </div>
- <script type="x-shader/x-vertex" id="vertexShader">
- #include <common>
- uniform float speed;
- uniform float time;
- uniform float timeOffset;
- varying vec2 vUv;
- varying float vAlpha;
- void main() {
- vec3 pos = position;
- float l = ( time * speed * 0.01 ) + timeOffset;
- float f = fract( l ); // linear time factor [0,1)
- float a = f * f; // quadratic time factor [0,1)
- // slightly animate the vertices of light shaft if necessary
- // pos.x += cos( l * 20.0 ) * sin( l * 10.0 );
- vAlpha = saturate( 0.7 + min( 1.0, a * 10.0 ) * ( sin( a * 40.0 ) * 0.25 ) );
- vUv = uv;
- gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 );
- }
- </script>
- <script type="x-shader/x-fragment" id="fragmentShader">
- uniform float attenuation;
- uniform vec3 color;
- uniform sampler2D colorTexture;
- varying vec2 vUv;
- varying float vAlpha;
- void main() {
- vec4 textureColor = texture2D( colorTexture, vUv );
- gl_FragColor = vec4( textureColor.rgb * color.rgb, textureColor.a * vAlpha );
- gl_FragColor.a *= pow( gl_FragCoord.z, attenuation );
- }
- </script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
- var container, controls, clock;
- var camera, scene, renderer, uniforms;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
- camera.position.set( 3.2, 3, 3.7 );
- clock = new THREE.Clock();
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xcf8b74 );
- var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
- light.position.set( 0, 20, 0 );
- scene.add( light );
- light = new THREE.DirectionalLight( 0xffffff );
- light.position.set( 0, 20, 10 );
- scene.add( light );
- // light shafts definition
- var textureLoader = new THREE.TextureLoader();
- var texture = textureLoader.load( 'textures/lightShaft.png' );
- uniforms = {
- // controls how fast the ray attenuates when the camera comes closer
- attenuation: {
- value: 10
- },
- // controls the speed of the animation
- speed: {
- value: 2
- },
- // the color of the ray
- color: {
- value: new THREE.Color( 0xdadc9f )
- },
- // the visual representation of the ray highly depends on the used texture
- colorTexture: {
- value: texture
- },
- // global time value for animation
- time: {
- value: 0
- },
- // individual time offset so rays are animated differently if necessary
- timeOffset: {
- value: 0
- }
- };
- var lightShaftMaterial = new THREE.ShaderMaterial( {
- uniforms: uniforms,
- vertexShader: document.getElementById( 'vertexShader' ).textContent,
- fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
- blending: THREE.AdditiveBlending,
- depthWrite: false,
- transparent: true,
- side: THREE.DoubleSide
- } );
- var lightShaftGeometry = new THREE.PlaneBufferGeometry( 0.5, 5 );
- // model
- var loader = new GLTFLoader().setPath( 'models/gltf/Tree/' );
- loader.load( 'tree.glb', function ( gltf ) {
- gltf.scene.traverse( function ( child ) {
- if ( child.isMesh ) {
- child.material.transparent = false;
- child.material.alphaTest = 0.5;
- }
- } );
- scene.add( gltf.scene );
- // when the model is loaded, add light shafts
- for ( var i = 0; i < 5; i ++ ) {
- var lightShaft = new THREE.Mesh( lightShaftGeometry, lightShaftMaterial );
- lightShaft.position.x = - 1 + 1.5 * Math.sign( ( i % 2 ) );
- lightShaft.position.y = 2;
- lightShaft.position.z = - 1.5 + ( i * 0.5 );
- lightShaft.rotation.y = Math.PI * 0.2;
- lightShaft.rotation.z = Math.PI * - ( 0.15 + 0.1 * Math.random() );
- scene.add( lightShaft );
- }
- } );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.outputEncoding = THREE.sRGBEncoding;
- container.appendChild( renderer.domElement );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 0.5, 1.5, 0 );
- controls.minDistance = 2;
- controls.maxDistance = 20;
- controls.update();
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- const delta = clock.getDelta();
- uniforms.time.value += delta;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|