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 | import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import Stats from 'three/examples/jsm/libs/stats.module'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import TWEEN from '@tweenjs/tween.js'
const scene = new THREE.Scene()
scene.add(new THREE.AxesHelper(5))
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
)
camera.position.set(-0.6, 0.45, 2)
const renderer = new THREE.WebGLRenderer()
//renderer.physicallyCorrectLights = true //deprecated
renderer.useLegacyLights = false //use this instead of setting physicallyCorrectLights=true property
renderer.shadowMap.enabled = true
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
const raycaster = new THREE.Raycaster()
const sceneMeshes: THREE.Object3D[] = []
let monkey: THREE.Object3D
const loader = new GLTFLoader()
loader.load(
'models/monkey.glb',
function (gltf) {
gltf.scene.traverse(function (child) {
if ((child as THREE.Mesh).isMesh) {
const m = child as THREE.Mesh
if (m.name === 'Suzanne') {
m.castShadow = true
monkey = m
} else {
// floor
m.receiveShadow = true
}
sceneMeshes.push(m)
}
if ((child as THREE.Light).isLight) {
const l = child as THREE.Light
l.castShadow = true
l.shadow.bias = -0.001
}
})
scene.add(gltf.scene)
chainTweens()
},
(xhr) => {
console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
},
(error) => {
console.log(error)
}
)
function chainTweens() {
// Demonstrating a repeating sequence of tweens.
const changePositionZ = new TWEEN.Tween(monkey.position).to({ z: -2 }, 2000) // 2 seconds
const rotateY = new TWEEN.Tween(monkey.rotation).to(
{ y: Math.PI * 2 },
2000
)
const scaleXZ = new TWEEN.Tween(monkey.scale).to({ x: 2, z: 0.5 }, 2000)
const rotateZ = new TWEEN.Tween(monkey.rotation).to(
{ z: Math.PI * 2 },
2000
)
const resetScaleXZ = new TWEEN.Tween(monkey.scale).to({ x: 1, z: 1 }, 2000)
const resetPositionZ = new TWEEN.Tween(monkey.position).to({ z: 0 }, 2000)
const rotateX = new TWEEN.Tween(monkey.rotation).to(
{ x: Math.PI * 2 },
2000
)
const resetRotations = new TWEEN.Tween(monkey.rotation).to(
{ x: 0, y: 0, z: 0 },
0
) // 0 seconds results in an instant tween
changePositionZ.chain(rotateY)
rotateY.chain(scaleXZ)
scaleXZ.chain(rotateZ)
rotateZ.chain(resetScaleXZ)
resetScaleXZ.chain(resetPositionZ)
resetPositionZ.chain(rotateX)
rotateX.chain(resetRotations)
resetRotations.chain(changePositionZ) // begin the loop again
changePositionZ.start()
}
const mouse = new THREE.Vector2()
function onDoubleClick(event: MouseEvent) {
mouse.set(
(event.clientX / renderer.domElement.clientWidth) * 2 - 1,
-(event.clientY / renderer.domElement.clientHeight) * 2 + 1
)
raycaster.setFromCamera(mouse, camera)
const intersects = raycaster.intersectObjects(sceneMeshes, false)
if (intersects.length > 0) {
const p = intersects[0].point
new TWEEN.Tween(controls.target)
.to(
{
x: p.x,
y: p.y,
z: p.z,
},
500
)
.easing(TWEEN.Easing.Cubic.Out)
.start()
}
}
renderer.domElement.addEventListener('dblclick', onDoubleClick, false)
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
render()
}
window.addEventListener('resize', onWindowResize, false)
const stats = new Stats()
document.body.appendChild(stats.dom)
function animate() {
requestAnimationFrame(animate)
controls.update()
TWEEN.update()
render()
stats.update()
}
function render() {
renderer.render(scene, camera)
}
animate()
|