-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
494 lines (458 loc) · 23.4 KB
/
index.ts
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader'
import * as CANNON from 'cannon-es'
import gsap from 'gsap'
import CircleProgress from 'js-circle-progress'
import CannonUtils from './cannonUtils'
import CannonDebugRenderer from './cannonDebugRenderer'
//circle progress bar
const cp = new CircleProgress({
min:0,
max:100,
value:0,
textFormat: 'percent',
})
cp.style.top = '45%'
cp.style.left = '47%'
cp.style.position = 'absolute'
document.body.append(cp)
//navigate button
const navigate = document.createElement("img")
navigate.src = "navigation.png"
navigate.style.position = 'absolute'
navigate.style.width = "100px"
navigate.style.height = "100px"
// navigate.style.backgroundImage = "url('navigation.png')"
// navigate.style.backgroundSize = "cover"
// navigate.style.borderRadius = "50%"
navigate.style.bottom = "0%"
navigate.style.left = "47%"
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('./draco/')
const gltfLoader = new GLTFLoader();
gltfLoader.crossOrigin = true;
// gltfLoader.setDRACOLoader(dracoLoader)
let walking = true;
let running = false;
let walk
let run
let crash = false;
gltfLoader.load(
'models/map.glb',
(gltf) => {
let clicked = true // navigate click event
const scene = new THREE.Scene()
// scene.fog = new THREE.Fog('#60a3e0', 1, 100)
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
)
camera.position.set(0, 10, -2)
scene.add(camera)
var bgTexture = new THREE.TextureLoader().load("background.jpg")
// bgTexture.mapping = THREE.EquirectangularReflectionMapping
// bgTexture.minFilter = THREE.LinearFilter;
scene.background = bgTexture
scene.environment = bgTexture
var aLight = new THREE.AmbientLight( 0xffffff )
scene.add( aLight )
const directionalLight = new THREE.DirectionalLight(0xffff00, 1);
directionalLight.position.set(100, 100, 100);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2 * 2048;
directionalLight.shadow.mapSize.height = 2 * 2048;
directionalLight.shadow.camera.near = 0.1;
directionalLight.shadow.camera.far = 500;
directionalLight.shadow.camera.top = 500;
directionalLight.shadow.camera.right = 500;
directionalLight.shadow.camera.bottom = -500;
directionalLight.shadow.camera.left = -500;
scene.add(directionalLight)
const renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } )
renderer.setClearColor('#60a3e0')
renderer.useLegacyLights = true // this option is to load light embed on glb file.
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setPixelRatio(window.devicePixelRatio)
renderer.shadowMap.enabled = true
renderer.shadowMap.type = THREE.VSMShadowMap // PCFShadowMap
document.body.appendChild(renderer.domElement)
const raycaster = new THREE.Raycaster()
const world = new CANNON.World()
// const cannonDebugRenderer = new CannonDebugRenderer(scene, world)
world.gravity.set(0, -9.82, 0)
const groundMaterial = new CANNON.Material('groundMaterial')
const slipperyMaterial = new CANNON.Material('slipperyMaterial')
const slippery_ground_cm = new CANNON.ContactMaterial(
groundMaterial,
slipperyMaterial,
{
friction: 0,
restitution: 0,
contactEquationStiffness: 1e8,
contactEquationRelaxation: 3,
}
)
world.addContactMaterial(slippery_ground_cm)
;(world.solver as CANNON.GSSolver).iterations = 10
// Character Collider
const characterCollider = new THREE.Object3D()
const colliderShape = new CANNON.Sphere(0.5)
const colliderBody = new CANNON.Body({ mass: 1, material: slipperyMaterial })
let mixer: THREE.AnimationMixer
let modelReady = false
let modelMesh: THREE.Object3D
let targetMesh: THREE.Object3D
const animationActions: THREE.AnimationAction[] = []
let activeAction: THREE.AnimationAction
let lastAction: THREE.AnimationAction
let mapModel = gltf.scene
scene.add(mapModel)
const normalMaterial = new THREE.MeshNormalMaterial()
const video = document.createElement('video')
video.src = 'video.mp4'
video.crossOrigin = 'anonymous'
video.loop = true
// video.muted = false
video.play()
const videoTexture = new THREE.VideoTexture(video)
videoTexture.minFilter = THREE.LinearFilter
videoTexture.wrapS = THREE.MirroredRepeatWrapping
videoTexture.wrapT = THREE.MirroredRepeatWrapping
let videoMesh1 = mapModel.getObjectByName("poster1")
videoMesh1.material.map = videoTexture;
videoMesh1.material.side = THREE.FrontSide;
videoMesh1.material.needsUpdate = true;
let videoMesh2 = mapModel.getObjectByName("poster2")
videoMesh2.material.map = videoTexture;
videoMesh2.material.side = THREE.FrontSide;
videoMesh2.material.needsUpdate = true;
let videoMesh3 = mapModel.getObjectByName("poster3")
videoMesh3.material.map = videoTexture;
videoMesh3.material.side = THREE.FrontSide;
videoMesh3.material.needsUpdate = true;
let videoMesh4 = mapModel.getObjectByName("poster4")
videoMesh4.material.map = videoTexture;
videoMesh4.material.side = THREE.DoubleSide;
videoMesh4.material.needsUpdate = true;
let alink = scene.getObjectByName("alink")
alink.material = new THREE.MeshBasicMaterial({ color: 0xff0000 })
gltf.scene.traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
if (child.name == 'Ground'|| child.name == 'Rectangle009' || child.name == 'B_basis' || child.name == 'Rectangle010' || child.name == 'Rectangle015' || child.name == 'Rectangle001' || child.name == 'Rectangle025' || child.name == 'Rectangle019' || child.name == 'Rectangle032' || child.name == 'Rectangle029' || child.name == 'Rectangle026' || child.name == 'Rectangle027' || child.name == 'Rectangle008' || child.name == 'Rectangle003' || child.name == 'Rectangle012' || child.name == 'Rectangle011' || child.name == 'Rectangle019') {
let cityMesh: THREE.Object3D
const cityBody = new CANNON.Body({ mass: 0, material: groundMaterial })
cityMesh = child
const position = new THREE.Vector3()
console.log(cityMesh)
cityMesh.getWorldPosition(position)
const cityShape = CannonUtils.CreateTrimesh(
(cityMesh as THREE.Mesh).geometry
)
cityBody.position.x = position.x
cityBody.position.y = position.y
cityBody.position.z = position.z
cityBody.addShape(cityShape)
world.addBody(cityBody)
}
})
gltfLoader.load(
'models/idle.glb',
(gltf) => {
gltf.scene.traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
})
const avatar = gltf.scene
avatar.scale.set(1.5, 1.5, 1.5)
avatar.castShadow = true
avatar.receiveShadow = true
const orbitControls = new OrbitControls(camera, renderer.domElement)
const inputVelocity = new THREE.Vector3()
const velocity = new CANNON.Vec3()
orbitControls.enableDamping = true
orbitControls.dampingFactor = 0.05
orbitControls.minPolarAngle = Math.PI/3
orbitControls.maxPolarAngle = Math.PI/3
orbitControls.enableZoom = false
orbitControls.enabled = false
mixer = new THREE.AnimationMixer(gltf.scene)
animationActions.push(mixer.clipAction(gltf.animations[0]))
gltfLoader.load(
'models/walk.glb',
(gltf) => {
gltf.scene.traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
})
animationActions.push(mixer.clipAction(gltf.animations[0]))
gltfLoader.load(
'models/run.glb',
(gltf) => {
gltf.scene.traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
})
animationActions.push(mixer.clipAction(gltf.animations[0]))
}
)
}
)
activeAction = animationActions[0]
activeAction.loop = THREE.LoopRepeat
activeAction.play()
scene.add(avatar)
modelMesh = gltf.scene
modelMesh.add(camera)
const creatCollider = () => {
characterCollider.position.x = 0
characterCollider.position.y = 3
characterCollider.position.z = 0
scene.add(characterCollider)
// colliderBody.addShape(colliderShape, new CANNON.Vec3(0, 0.5, 0))
colliderBody.addShape(colliderShape, new CANNON.Vec3(0, -0.5, 0))
colliderBody.position.set(
characterCollider.position.x,
characterCollider.position.y,
characterCollider.position.z
)
colliderBody.linearDamping = 0.95
colliderBody.angularFactor.set(0, 1, 0) // prevents rotation X,Z axis
world.addBody(colliderBody)
gsap.to(camera.position, {x: 0, y: 40, z: -50, duration: 2})
}
const setAction = (toAction: THREE.AnimationAction, loop: Boolean) => {
if (toAction != activeAction) {
lastAction = activeAction
activeAction = toAction
lastAction.fadeOut(0.1)
activeAction.reset()
activeAction.fadeIn(0.2)
activeAction.play()
if (!loop) {
activeAction.clampWhenFinished = true
activeAction.loop = THREE.LoopOnce
}
}
}
modelReady = true
creatCollider()
const mouse = new THREE.Vector2()
renderer.domElement.addEventListener('mousedown', (event) => {
event.preventDefault();
if (!clicked) {
// Set the mouse coordinates (normalized between -1 and 1)
mouse.x = (event.clientX / window.innerWidth) * 2 - 1
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1
// Set the origin of the raycaster to the camera position
raycaster.setFromCamera(mouse, camera)
const intersects = raycaster.intersectObjects(scene.children)
if (intersects.length > 0) {
if(intersects[0].object.name=='Ground' || intersects[0].object.name == 'Ground'|| intersects[0].object.name == 'Rectangle009' || intersects[0].object.name == 'B_basis' || intersects[0].object.name == 'Rectangle010' || intersects[0].object.name == 'Rectangle015' || intersects[0].object.name == 'Rectangle001' || intersects[0].object.name == 'Rectangle025'){
//mouse pointer mesh
crash = false;
targetMesh = intersects[0]
if(walk)
walk.kill()
else if(run)
run.kill()
distance = colliderBody.position.distanceTo(targetMesh.point)
if(distance>20){
run = gsap.to(colliderBody.position, {
x: targetMesh.point.x,
// y: targetMesh.point.y,
z: targetMesh.point.z,
duration: distance/6
})
running = true;
walking = false;
}
else{
walk = gsap.to(colliderBody.position, {
x: targetMesh.point.x,
// y: targetMesh.point.y,
z: targetMesh.point.z,
duration: distance/2
})
running = false;
walking = true;
}
//mouse pointer mesh
const ringGeometry = new THREE.RingGeometry(0.1, 0.2)
// Define the material
const material = new THREE.MeshBasicMaterial({ color: '#ff66cc', side: THREE.DoubleSide })
// Create the mesh
const ringMesh = new THREE.Mesh(ringGeometry, material)
ringMesh.rotation.x = Math.PI / 2 //
ringMesh.position.set(targetMesh.point.x, targetMesh.point.y, targetMesh.point.z)
scene.add(ringMesh)
gsap.to(ringMesh.scale, {
x: 0,
y: 0,
z: 0,
duration: 1,
})
}
// if(intersects[0].object.name=='alink') {
// window.open("https://example.com", "_blank")
// }
}
}
})
window.addEventListener('resize', onWindowResize, false)
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
render()
}
const targetQuaternion = new THREE.Quaternion()
const clock = new THREE.Clock()
let delta = 0
let distance = 0
let distance1 = 0
function animate() {
requestAnimationFrame(animate)
if (modelReady) {
const p = characterCollider.position
p.y -= 1
modelMesh.position.y = characterCollider.position.y
const rotationMatrix = new THREE.Matrix4()
distance1 = modelMesh.position.distanceTo(p)
if(targetMesh)
distance = colliderBody.position.distanceTo(targetMesh.point)
if(distance > 1){
if(targetMesh&&!crash){
rotationMatrix.lookAt(p, modelMesh.position, modelMesh.up)
targetQuaternion.setFromRotationMatrix(rotationMatrix)
}
}
if (!modelMesh.quaternion.equals(targetQuaternion)) {
modelMesh.quaternion.rotateTowards(targetQuaternion, delta * 10 )
}
modelMesh.position.lerp(characterCollider.position, 0.1)
}
if (distance>=1) {
if(running) {
setAction(animationActions[2], true)
mixer.update(delta)
}
else {
setAction(animationActions[1], true)
// mixer.update(delta * distance1 * 2 )
mixer.update(delta)
}
}
else {
setAction(animationActions[0], true)
mixer.update(delta)
}
// velocity.set(inputVelocity.x, inputVelocity.y, inputVelocity.z)
// colliderBody.position.x += inputVelocity.x
// colliderBody.position.z += inputVelocity.z
delta = Math.min(clock.getDelta(), 0.1)
world.step(delta)
// cannonDebugRenderer.update()
characterCollider.position.set(
colliderBody.position.x,
colliderBody.position.y,
colliderBody.position.z
)
orbitControls.update()
if(video) {
videoTexture.needsUpdate = true
video.play()
}
render()
}
function render() {
if(!clicked)
camera.lookAt(modelMesh.position.x, modelMesh.position.y, modelMesh.position.z)
camera.updateProjectionMatrix()
renderer.render(scene, camera)
}
colliderBody.addEventListener('collide', function (e: any) {
crash = true;
if(walk)
walk.kill()
if(run)
run.kill()
})
animate()
navigate.addEventListener( 'click', function () {
if (clicked == false) {
clicked = true
gsap.to(camera.position, {
x: 0,
y: 40,
z: -50,
duration: 4,
onStart: () => {
orbitControls.enabled = false
},
onUpdate: () => {
orbitControls.enabled = false
},
onComplete: () => {
orbitControls.enabled = true
orbitControls.autoRotate = true
},
})
} else {
clicked = false
gsap.to(camera.position, {
x: 0,
y: 10,
z: -2,
duration: 4,
onStart: () => {
orbitControls.enabled = false
},
onUpdate: () => {
orbitControls.enabled = false
},
onComplete: () => {
orbitControls.enabled = false
orbitControls.autoRotate = false
},
})
}
})
},
(xhr) => {
console.log('avatar_glb has been loaded')
},
(error) => {
console.log(error)
}
)
},
(xhr) => {
cp.value = (xhr.loaded) / 76807588 * 100
if(cp.value==100) {
gsap.to(cp, {
duration: 2,
opacity: 0,
onComplete: () => {
cp.remove()
document.body.append(navigate)
}
})
}
},
(error) => {
console.log(error)
}
)