-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW05-2.html
292 lines (222 loc) · 7.33 KB
/
HW05-2.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
<!DOCTYPE html>
<html>
<head>
<style>
#info {
position: absolute;
top: 10px;
left: 45vw;
text-align: center;
color: #ffff00
}
#teapot {
position: absolute;
top: 30px;
left: 40vw;
font-size:3em;
text-align: center;
color: #ffff00
}
body {
overflow: hidden;
}
</style>
</head>
<body>
<div id="info">
hw5-3<br>
</div>
<div id="teapot">Teapot : 0</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/109/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://raw.githack.com/mrdoob/three.js/dev/examples/js/geometries/TeapotBufferGeometry.js"></script>
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://mrdoob.github.io/stats.js/build/stats.min.js"></script>
<script id="myVertexShader" type="x-shader/x-vertex">
uniform vec3 lightpos;
varying vec3 eyelightdir;
varying vec3 eyenormal;
varying vec4 eyepos;
void main() {
gl_Position = projectionMatrix* modelViewMatrix * vec4( position, 1.0);
eyepos = modelViewMatrix * vec4 (position, 1.0);
vec4 eyelightpos = viewMatrix * vec4 (lightpos, 1.0);
eyelightdir = normalize (eyelightpos.xyz - eyepo.xyz);
eyenormal = normalMatrix * normal;
}
</script>
<script id="myFragmentShader" type="x-shader/x-fragment">
varying vec3 eyelightdir; //direction
varying vec3 eyenormal;
varying vec4 eyepos;
uniform float opacity;
uniform vec3 kcool;
uniform vec3 kwarm;
void main() {
float intensity = dot (normalize (eyenormal), normalize (eyelightdir));
float k = (1.0 + intensity)/2.;
vec3 h = normalize(-normalize (eyepos.xyz) + normalize (eyelightdir));
float shininess = 40.;
vec3 specular = pow (dot (eyenormal, h), shininess) *vec3 (1,0,0);
gl_FragColor = vec4(mix (kcool, kwarm, k) + specular, opacity);
}
</script>
<script type="text/javascript">
var scene, renderer, camera;
var mesh,target,pointLight,stats;
var teapots = [];
var clock;
var clock = new THREE.Clock();
var raycaster;
var mouse = new THREE.Vector2();
var pickables = [], delArray = [];
var movingTeapot;
var angle = 0;
var teapot,teapotMaterial,teapotMaterial2;
class Teapot{
constructor(mesh,material,pos,x){
this.mesh = new THREE.Mesh(mesh,material);
this.mesh.name = "teapot";
this.mesh.x = x;
this.mesh.position.copy(pos);
scene.add(this.mesh);
this.opacity = 1.0; //透明度
this.lifepoint = 100;
this.isRotate = true;
this.disease = false; //消失
}
update(dt, lightpos){
if(this.isRotate == true){
this.mesh.rotation.y += 0.08;
if(this.lifepoint > 0)
this.lifepoint -= 0.5;
else if(this.lifepoint <= 0 && this.lifepoint > -1){
this.opacity = THREE.Math.lerp(this.opacity, 0, 0.1);
this.mesh.material.uniforms.opacity.value = this.opacity;
this.life -= 0.035;
if(this.lifepoint <= -1)
this.destroy();
}
}
this.movingTeapot.material.uniforms.lightpos.value.copy(lightpos);
}
destory(){
scene.remove(this.mesh);
this.mesh.geometry.dispose();
this.mesh.material.dispose();
this.disease = true;
}
changeState(){
this.isRotate = !this.isRotate;
}
}
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 140;
camera.position.y = 140;
light = new THREE.PointLight(0xffffff);
light.position.set(100, 300, 200);
scene.add(light);
var light2 = new THREE.AmbientLight(0x404040); // soft white light
scene.add(light2);
stats = new Stats();
stats.showPanel(0);
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '10px';
document.body.appendChild(stats.domElement);
/////////////////////////////////////////////////////////////////
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x888888);
let controls = new THREE.OrbitControls(camera, renderer.domElement);
document.body.appendChild(renderer.domElement);
teapotMaterial = new THREE.ShaderMaterial({
uniforms: {
lightpos: {type:'v3', value: new THREE.Vector3(0, 30, 20) },
opacity: {type: 'f', value: 1.0},
kcool: {type: 'v3', value: new THREE.Vector3(0,0,0.55)},
kwarm: {type: 'v3', value: new THREE.Vector3(0.3,0.3,0)}
},
vertexShader: document.getElementById('myVertexShader').textContent,
fragmentShader: document.getElementById('myFragmentShader').textContent
});
teapotMaterial.transparent = true;
let plane = new THREE.Mesh(new THREE.PlaneGeometry(200, 200, 20, 20), new THREE.MeshPhongMaterial());
plane.rotation.x = -Math.PI / 2;
plane.position.y -= 10;
scene.add(plane);
pickables.push(plane);
let box1 = new THREE.Mesh(new THREE.BoxGeometry(20, 20, 20), new THREE.MeshPhongMaterial());
box1.position.set(-90, 0, -90);
scene.add(box1);
pickables.push(box1);
let box2 = new THREE.Mesh(new THREE.BoxGeometry(20, 20, 200), new THREE.MeshPhongMaterial());
box2.position.set(90, 0, 0);
scene.add(box2);
pickables.push(box2);
raycaster = new THREE.Raycaster();
window.addEventListener('resize', onWindowResize, false);
document.addEventListener('mousedown', onDocumentMouseDown, false);
}
function animate() {
stats.begin();
let dt = clock.getDelta();
let str = "teapot : " + teapots.length + "個";
$("#teapot").html(str);
for(let i=0;i<teapots.length;i++){
if(teapots[i].dis == true){
delArray.push(i);
}
else {
teapots[i].update(dt,light.position);
}
}
for(let i=0;i<delArray.length;i++){
for(let j=delArray[i]+1;j<teapots.length;j++){
teapots[j].mesh.n -= 1;
}
teapots.splice(delArray[i], 1);
pickables.splice(delArray[i]+1, 1);
}
delArray = [];
angle += 0.05;
light.position.set(60*Math.cos(angle), 30, 60*Math.sin(angle));
stats.end();
requestAnimationFrame(animate);
render();
}
function render() {
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function onDocumentMouseDown(event) {
event.preventDefault();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(pickables);
if (intersects.length > 0) {
if(intersects[0].object.name == "teapot"){
teapots[intersects[0].object.n].changeState();
}
else {
let pos = intersects[0].point;
pos.y += 10;
let teapot = new teaPot(new THREE.TeapotBufferGeometry(10), teapotMaterial.clone(), pos, teapots.length);
teapots.push(teapot);
pickables.push(teapot.mesh);
}
}
}
</script>
</body>
</html>