-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththree_suzanne.html
97 lines (83 loc) · 3.13 KB
/
three_suzanne.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
<html>
<head>
<title>Three.js WebGPU Renderer Suzanne</title>
<style>
body {
margin: 0;
background-color: #000;
color: #fff;
overscroll-behavior: none;
}
</style>
</head>
<body>
<script type="module">
import {
BoxBufferGeometry,
BufferGeometryLoader,
Camera,
Mesh,
MeshNormalMaterial,
PerspectiveCamera,
Scene
} from 'https://raw.githack.com/mrdoob/three.js/r111/build/three.module.js';
import WebGPURenderer from './js/webgpu_renderer.js';
// @TODO: Detect WebGPU capability
const run = () => {
let mouseX = 0, mouseY = 0;
let windowHalfX = window.innerWidth / 2;
let windowHalfY = window.innerHeight / 2;
const renderer = new WebGPURenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);
const scene = new Scene();
const camera = new PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.1, 1000.0);
camera.position.z = 25;
const objects = [];
const material = new MeshNormalMaterial();
const loader = new BufferGeometryLoader();
loader.load( 'assets/json/suzanne_buffergeometry.json', geometry => {
geometry.computeVertexNormals();
for (let i = 0; i < 200; i++) {
const mesh = new Mesh(geometry, material);
mesh.position.x = Math.random() * 50 - 25;
mesh.position.y = Math.random() * 50 - 25;
mesh.position.z = Math.random() * 50 - 25;
mesh.rotation.x = Math.random() * 2 * Math.PI;
mesh.rotation.y = Math.random() * 2 * Math.PI;
mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 2 + 1;
objects.push(mesh);
scene.add(mesh);
}
});
const onResize = event => {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
};
const onMouseMove = event => {
mouseX = (event.clientX - windowHalfX) * 0.10;
mouseY = (event.clientY - windowHalfY) * 0.10;
};
const render = () => {
requestAnimationFrame(render);
camera.position.x += (mouseX - camera.position.x) * .05;
camera.position.y += (-mouseY - camera.position.y) * .05;
camera.lookAt( scene.position );
for (const object of objects) {
object.rotation.x += 0.01;
object.rotation.y += 0.02;
}
renderer.render(scene, camera);
};
window.addEventListener('resize', onResize, false);
document.addEventListener('mousemove', onMouseMove, false);
render();
};
run();
</script>
</body>
</html>