forked from joshmarinacci/webxr-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snow2.html
322 lines (268 loc) · 10.8 KB
/
snow2.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebVR + ThreeJS Application</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<!-- required -->
<script src="./node_modules/three/build/three.min.js"></script>
<!-- needed for loading GLTF files -->
<script src="./node_modules/three/examples/js/loaders/GLTFLoader.js"></script>
<!-- needed for loading Truetype Fonts -->
<script src="./node_modules/three/examples/js/loaders/TTFLoader.js"></script>
<script src="./node_modules/three/examples/js/libs/opentype.min.js"></script>
<!-- needed to enter VR -->
<script src="../boilerplate/webvr.js"></script>
<!-- stats (fps, polygons, fps) -->
<script src="../boilerplate/vrstats.js" type="module"></script>
<!-- mouse/touch/vr-controller support -->
<!--<script src="./pointer.js" type="module"></script>-->
<style type="text/css">
#overlay {
position: fixed;
font-size: 5vh;
width: 100vw;
height: 100vh;
background-color: rgba(0,0,0,0.5);
text-align: center;
}
#loading-indicator {
display: block;
}
#click-to-play {
/*display: none;*/
color: black;
background-color: white;
border: 1px solid black;
}
/* this button is generated by the VR subsystem, disabled if not available */
#enter-vr {
position: absolute;
bottom: 20px;
left: 50%;
transform: translate(-50%,0);
}
</style>
</head>
<body>
<div id="overlay">
<h1>Application Name</h1>
<div id="loading-indicator">
<label>loading</label>
<progress max="100" value="0" id="progress"></progress>
</div>
<h3 id="click-to-play">click to start</h3>
</div>
<script type="module">
import {POINTER_CLICK, POINTER_ENTER, POINTER_EXIT, Pointer} from '../boilerplate/pointer.js'
import VRStats from "../boilerplate/vrstats.js"
import {FaceColors, Vector3, Vector2, Face3, Color} from "./node_modules/three/build/three.module.js"
const clickStartEnabled = false
//JQuery-like selector
const $ = (sel) => document.querySelector(sel)
const on = (elem, type, cb) => elem.addEventListener(type,cb)
// global constants and variables for your app go here
let camera, scene, renderer, pointer, stats;
let mesh
//called on setup. Customize this
function initContent(scene,camera,renderer) {
//set the background color of the scene
scene.background = new THREE.Color( 0xf0f0f0 );
//load a cat texture
const texture_loader = new THREE.TextureLoader()
//cat from http://creative-commons-cats.tumblr.com/page/3
// const texture = texture_loader.load('./cat.jpg')
//a standard light
const light = new THREE.DirectionalLight( 0xffffff, 1.0 );
light.position.set( 1, 1, 1 ).normalize();
scene.add( light );
scene.add(new THREE.AmbientLight())
const vertexShader = `
uniform float uTime;
attribute vec3 velocity;
attribute vec3 startPosition;
varying vec2 vUv;
mat4 rotation3d(vec3 axis, float angle) {
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat4(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0
);
}
vec3 rotate(vec3 v, vec3 axis, float angle) {
return (rotation3d(axis, angle) * vec4(v, 1.0)).xyz;
}
void main() {
vUv = uv;
float t = uTime - 0.0;
vec3 vel = vec3(0.1,0.0,0.0);
vec3 axis = vec3(0.0, 1.0, 0.0);
float angle = 3.14;
vec3 pos2 = rotate(position, axis, angle*t);
vec3 newPosition = pos2 + startPosition + velocity*t;
gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition,1.0);
}
`
const fragmentShader = `
uniform sampler2D tSprite;
varying vec2 vUv;
void main() {
vec4 color = texture2D( tSprite, vUv );
gl_FragColor = color * 1.0;//mix(tex,vec4(1.0,0.0,0.0,1.0),0.5);
}
`
//create a quad with correct UVs
const geom = new THREE.Geometry()
function addQuad(x,y) {
const start = geom.vertices.length
geom.vertices.push(
new Vector3(-0.5+x, y-0.5, 0), // 0
new Vector3(0.5+x, y-0.5, 0), // 1
new Vector3(0.5+x, y+0.5, 0), // 2
new Vector3(-0.5+x, y+0.5, 0), // 3
)
geom.faces.push(new Face3(start+0, start+1, start+2))
geom.faces.push(new Face3(start+2, start+3, start+0))
geom.faceVertexUvs[0].push([
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(1, 1),
])
geom.faceVertexUvs[0].push([
new Vector2(1, 1),
new Vector2(1, 0),
new Vector2(0, 0),
])
}
addQuad(0,0)
addQuad(0,0)
const bgeom = new THREE.BufferGeometry()
bgeom.fromGeometry(geom)
bgeom.addAttribute('startPosition', new THREE.BufferAttribute(new Float32Array(12*3),3).setDynamic(true))
bgeom.addAttribute('velocity', new THREE.BufferAttribute(new Float32Array(12*3),3).setDynamic(true))
bgeom.addAttribute('rotation', new THREE.BufferAttribute(new Float32Array(12*3),3).setDynamic(true))
const sa = bgeom.getAttribute('startPosition')
const va = bgeom.getAttribute('velocity')
//set the first 6, which is for the first quad
for(let i=0; i<6; i++) {
sa.array[i*3+0] = -2.0
sa.array[i*3+1] = 5.0
sa.array[i*3+2] = 0.0
va.array[i*3+0] = 0.0
va.array[i*3+1] = -0.5
va.array[i*3+2] = 0.0
}
//set the second 6, which is for the second quad
for(let i=6; i<12; i++) {
sa.array[i*3+0] = 2.0
sa.array[i*3+1] = 5.0
sa.array[i*3+2] = 0.0
va.array[i*3+0] = 0.0
va.array[i*3+1] = -0.5
va.array[i*3+2] = 0.0
}
const sprite = texture_loader.load('tex/test1.png')
const mat = new THREE.ShaderMaterial({
uniforms: {
'uTime': {
value: 0.0
},
'uScale': {
value: 1.0
},
'tSprite': {
value: sprite
}
},
vertexShader: vertexShader,
fragmentShader: fragmentShader,
side: THREE.DoubleSide,
blending: THREE.NormalBlending,
})
mesh = new THREE.Mesh(bgeom,mat)
mesh.position.z = -10
mesh.position.y = 1
mesh.position.x = -0.5
scene.add(mesh)
// enable stats visible inside VR
stats = new VRStats(renderer)
camera.add(stats)
scene.add(camera)
//class which handles mouse and VR controller
pointer = new Pointer(scene,renderer,camera, {
//Pointer searches everything in the scene by default
//override this to match just certain things
intersectionFilter: ((o) => o.userData.clickable),
//make the camera pan when moving the mouse. good for simulating head turning on desktop
cameraFollowMouse:false,
})
//change cube to red BG when clicking
if(!clickStartEnabled) {
$("#overlay").style.display = 'none'
$("#enter-vr").disabled = false
}
}
function updateParticles(time) {
const t = time/1000
mesh.material.uniforms.uTime.value = t
}
//called on every frame. customize this
function render(time) {
//update the pointer and stats, if configured
if(pointer) pointer.tick(time)
if(stats) stats.update(time)
updateParticles(time)
renderer.render( scene, camera );
}
// you shouldn't need to modify much below here
function initScene() {
//create DIV for the canvas
const container = document.createElement( 'div' );
document.body.appendChild( container );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 50 );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.vr.enabled = true;
container.appendChild( renderer.domElement );
document.body.appendChild( WEBVR.createButton( renderer ) );
initContent(scene,camera,renderer)
window.addEventListener( 'resize', ()=>{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}, false );
THREE.DefaultLoadingManager.onStart = (url, loaded, total) => {
console.log(`loading ${url}. loaded ${loaded} of ${total}`)
}
THREE.DefaultLoadingManager.onLoad = () => {
console.log(`loading complete`)
console.log("really setting it up now")
$('#loading-indicator').style.display = 'none'
$('#click-to-play').style.display = 'block'
const overlay = $('#overlay')
$("#click-to-play").addEventListener('click',()=>{
overlay.style.visibility = 'hidden'
if($('#enter-vr')) $('#enter-vr').removeAttribute('disabled')
})
}
THREE.DefaultLoadingManager.onProgress = (url, loaded, total) => {
console.log(`prog ${url}. loaded ${loaded} of ${total}`)
$("#progress").setAttribute('value',100*(loaded/total))
}
THREE.DefaultLoadingManager.onError = (url) => {
console.log(`error loading ${url}`)
}
}
// initPage()
initScene()
renderer.setAnimationLoop(render)
</script>
</body>
</html>