forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_lightshafts.html
253 lines (180 loc) · 5.84 KB
/
webgl_lightshafts.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<!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">
<style>
body {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a {
color: #ffffff;
font-weight: bold;
}
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Light Shafts -
Model by <a href="https://skfb.ly/6ICER" target="_blank" rel="noopener">Splodeman</a><br />
</div>
<script src="../build/three.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/loaders/GLTFLoader.js"></script>
<script src="js/WebGL.js"></script>
<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 texture;
varying vec2 vUv;
varying float vAlpha;
void main() {
vec4 textureColor = texture2D( texture, vUv );
gl_FragColor = vec4( textureColor.rgb * color.rgb, textureColor.a * vAlpha );
gl_FragColor.a *= pow( gl_FragCoord.z, attenuation );
}
</script>
<script>
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
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 );
controls = new THREE.OrbitControls( camera );
controls.target.set( 0.5, 1.5, 0 );
controls.minDistance = 2;
controls.maxDistance = 20;
controls.update();
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
texture: {
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 THREE.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.gammaOutput = true;
renderer.gammaFactor = 2.2;
container.appendChild( renderer.domElement );
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>