-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
115 lines (87 loc) · 3.36 KB
/
main.js
File metadata and controls
115 lines (87 loc) · 3.36 KB
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
// ページの読み込みを待つ
window.addEventListener('load', init);
function init() {
// サイズを指定
const width = 800;
const height = 400;
// マウス座標管理用のベクトルを作成
const mouse = new THREE.Vector2();
// canvas 要素の参照を取得する
const canvas = document.querySelector('#myCanvas');
// レンダラーを作成
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(width, height);
// シーンを作成
const scene = new THREE.Scene();
// カメラを作成
const camera = new THREE.PerspectiveCamera(45, width / height);
camera.position.set(0, 0, +500);
const geometry = new THREE.BoxBufferGeometry(50, 50, 50);
// マウスとの交差を調べたいものは配列に格納する
const meshList = [];
for (let i = 0; i < 5; i++) {
const material = new THREE.MeshStandardMaterial({ color: 0xffffff });
const mesh = new THREE.Mesh(geometry, material);
mesh.position.x = (Math.random() - 0.5) * 400;
mesh.position.y = (Math.random() - 0.5) * 400;
mesh.position.z = (Math.random() - 0.5) * 400;
// mesh.rotation.x = Math.random() * 2 * Math.PI;
// mesh.rotation.y = Math.random() * 2 * Math.PI;
// mesh.rotation.z = Math.random() * 2s* Math.PI;
scene.add(mesh);
// 配列に保存
meshList.push(mesh);
mesh.name = "mesh-" + scene.children.length;
//const ObjectName = mesh.name;
//console.log(ObjectName);
}
// 平行光源
const directionalLight = new THREE.DirectionalLight(0xffffff);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
// 環境光源
const ambientLight = new THREE.AmbientLight(0x666666);
scene.add(ambientLight);
// レイキャストを作成
const raycaster = new THREE.Raycaster();
canvas.addEventListener('click', handleMouseMove);
// マウスを動かしたときのイベント
function handleMouseMove(event) {
const element = event.currentTarget;
// canvas要素上のXY座標
const x = event.clientX - element.offsetLeft;
const y = event.clientY - element.offsetTop;
// canvas要素の幅・高さ
const w = element.offsetWidth;
const h = element.offsetHeight;
// -1〜+1の範囲で現在のマウス座標を登録する
mouse.x = (x / w) * 2 - 1;
mouse.y = -(y / h) * 2 + 1;
// レイキャスト = マウス位置からまっすぐに伸びる光線ベクトルを生成
raycaster.setFromCamera(mouse, camera);
// その光線とぶつかったオブジェクトを得る
const intersects = raycaster.intersectObjects(meshList);
meshList.map((mesh) => {
// 交差しているオブジェクトが1つ以上存在し、
// 交差しているオブジェクトの1番目(最前面)のものだったら
if (intersects.length > 0 && mesh === intersects[0].object) {
// 色を赤くする
mesh.material.color.setHex(0xff0000);
console.log(mesh.name);
} else {
// それ以外は元の色にする
mesh.material.color.setHex(0xffffff);
}
});
}
tick();
// 毎フレーム時に実行されるループイベントです
function tick() {
// レンダリング
renderer.render(scene, camera);
requestAnimationFrame(tick);
}
}