|
1 | | -importScripts("https://cdnjs.cloudflare.com/ajax/libs/three.js/101/three.min.js"); |
| 1 | +importScripts( |
| 2 | + 'https://cdnjs.cloudflare.com/ajax/libs/three.js/101/three.min.js' |
| 3 | +); |
2 | 4 |
|
| 5 | +// メインスレッドから通達があったとき |
3 | 6 | onmessage = event => { |
| 7 | + // メインスレッドからオフスクリーンキャンバスを受け取る |
| 8 | + const canvas = event.data.canvas; |
| 9 | + // Three.jsのライブラリの内部で style.width にアクセスされてしまう |
| 10 | + // 対策しないと、エラーが発生するためダミーの値を指定 |
| 11 | + canvas.style = { width: 0, height: 0 }; |
4 | 12 |
|
5 | 13 | // サイズを指定 |
6 | 14 | const width = 960; |
7 | 15 | const height = 540; |
8 | 16 |
|
9 | | - // メインスレッドからオフスクリーンキャンバスを受け取る |
10 | | - const canvas = event.data.canvas; |
11 | | - canvas.style = { width: 0, height: 0 }; |
12 | | - |
13 | 17 | // レンダラーを作成 |
14 | 18 | const renderer = new THREE.WebGLRenderer({ canvas }); |
15 | 19 | renderer.setSize(width, height); |
16 | 20 |
|
17 | 21 | // シーンを作成 |
18 | 22 | const scene = new THREE.Scene(); |
19 | | - |
20 | | - // カメラを作成 |
21 | 23 | const camera = new THREE.PerspectiveCamera(45, width / height); |
22 | | - |
23 | | - // マテリアルを作成する |
24 | | - const material = new THREE.MeshStandardMaterial({ color: 0xffFF00 }); |
25 | | - |
26 | | - // 平行光源 |
27 | 24 | const directionalLight = new THREE.DirectionalLight(0xffffff); |
28 | | - // シーンに追加 |
29 | 25 | scene.add(directionalLight); |
30 | 26 |
|
31 | | - // ビルボードを作成 |
| 27 | + // マテリアルを作成 |
| 28 | + const material = new THREE.MeshStandardMaterial({ color: 0xffff00 }); |
| 29 | + |
| 30 | + // たくさん作成 |
32 | 31 | for (let i = 0; i < 300; i++) { |
33 | | - // 球体を作成 |
34 | 32 | const geometry = new THREE.SphereGeometry(20, 20, 20); |
35 | | - // メッシュを作成 |
36 | 33 | const mesh = new THREE.Mesh(geometry, material); |
37 | | - // 3D空間にメッシュを追加 |
38 | 34 | scene.add(mesh); |
39 | | - mesh.position.set(1000 * (Math.random() - 0.5), |
| 35 | + // 適当に配置 |
| 36 | + mesh.position.set( |
| 37 | + 1000 * (Math.random() - 0.5), |
40 | 38 | 1000 * (Math.random() - 0.5), |
41 | | - 1000 * (Math.random() - 0.5)); |
| 39 | + 1000 * (Math.random() - 0.5) |
| 40 | + ); |
42 | 41 | } |
43 | 42 |
|
44 | 43 | tick(); |
45 | 44 |
|
46 | 45 | // 毎フレーム時に実行されるループイベントです |
47 | 46 | function tick() { |
48 | 47 | // カメラの自動移動 |
49 | | - camera.position.x = 100 * Math.sin(Date.now() / 2000); |
50 | | - camera.position.z = 100 * Math.cos(Date.now() / 2000); |
51 | | - camera.position.y = 50 * Math.sin(Date.now() / 1000) + 60; |
| 48 | + camera.position.set( |
| 49 | + 100 * Math.sin(Date.now() / 2000), |
| 50 | + 100 * Math.cos(Date.now() / 2000), |
| 51 | + 50 * Math.sin(Date.now() / 1000) + 60 |
| 52 | + ); |
52 | 53 | camera.lookAt(new THREE.Vector3(0, 0, 0)); |
53 | 54 |
|
54 | 55 | // レンダリング |
|
0 commit comments