-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain2.js
162 lines (133 loc) · 3.73 KB
/
main2.js
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
import * as three from './files/three.js';
import {OrbitControls} from './files/OrbitControls.js';
let renderer, camera, scene, mesh;
let axesHelper;
let clip, mixer;
let clock = new THREE.Clock();
initRenderer();
initCamera();
initScene();
initLight();
initMeshes();
initAxesHelper();
initControls();
initAnimation();
enableAnimation();
animate();
render();
window.addEventListener('resize', function () {//渲染结果随着窗体的变化而变化(浏览器变窄了,渲染窗口也变窄)
camera.aspect = window.innerWidth / this.window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, this.window.innerHeight);
})
/* ------------------场景三要素初始化------------------- */
function initRenderer() {
renderer = new THREE.WebGL1Renderer();
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
function initCamera() {
camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(10, 30, 50);
}
function initScene() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x888888);
}
/* ------------------场景三要素初始化------------------- */
function initLight() {
scene.add(new THREE.AmbientLight(0xffffff, 0.2));
const dirLight = new THREE.DirectionalLight(0xffffff, 1);
dirLight.position.set(10, 10, 5);
scene.add(dirLight);
}
function initAxesHelper() {
axesHelper = new THREE.AxesHelper(10);
scene.add(axesHelper);
}
function initMeshes() {
const geometry = new THREE.BoxGeometry(2, 2, 2);
const material = new THREE.MeshPhongMaterial({ color: 0xff0000 });
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
function initControls() {
const controls = new OrbitControls(camera, renderer.domElement);
}
function initAnimation() {
//位置
const positionKF = new THREE.VectorKeyframeTrack(
'.position',
[0, 1, 2, 3],
[
0, 0, 0,
10, 0, 0,
10, 10, 0,
0, 0, 0
]
);
//缩放
const scaleKF = new THREE.VectorKeyframeTrack(
'.scale',
[0, 1, 2, 3],
[
1, 1, 1,
2, 2, 2,
0.5, 2, 2,
1, 1, 1
]
);
//旋转
const xAxis = new THREE.Vector3(1, 0, 0);
const qInitial = new THREE.Quaternion().
setFromAxisAngle(xAxis,0);
const qFinal = new THREE.Quaternion().
setFromAxisAngle(xAxis,Math.PI);
const quaternionKF = new THREE.QuaternionKeyframeTrack(
'.quaternion',
[0,1,2,3],
[
qInitial.x, qInitial.y, qInitial.z, qInitial.w,
qFinal.x, qFinal.y, qFinal.z, qFinal.w,
qInitial.x, qInitial.y, qInitial.z, qInitial.w,
qFinal.x, qFinal.y, qFinal.z, qFinal.w
]
);
//颜色
const colorKF = new THREE.ColorKeyframeTrack(
'.material.color',
[0,1,2,3],
[
1,0,0,
0,1,0,
0,0,1,
0,0,0
]
);
//透明度
const opacityKF = new THREE.NumberKeyframeTrack(
'.material.opacity',
[0,1,2,3],
[1,0,1,1]
);
clip = new THREE.AnimationClip(
'Action',
4,
[positionKF, scaleKF, quaternionKF,colorKF,opacityKF]
);
}
function enableAnimation() {
mixer = new THREE.AnimationMixer(mesh);
const clipAction = mixer.clipAction(clip);
clipAction.play();
};
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
const delta = clock.getDelta();
renderer.render(scene, camera);
mixer.update(delta);
}