|
| 1 | +import { VRMHumanBoneName } from '@pixiv/three-vrm'; |
1 | 2 | import { Parser } from 'mmd-parser';
|
2 | 3 | import * as THREE from 'three';
|
3 | 4 | import { Audio, GridHelper, Mesh, MeshLambertMaterial, PlaneGeometry } from 'three';
|
4 | 5 | import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
|
5 |
| -import { LoopOnce } from 'three/src/constants'; |
6 |
| - |
7 |
| -import { loadVMDAnimation } from '@/libs/VMDAnimation/loadVMDAnimation'; |
8 |
| -import { AudioPlayer } from '@/libs/audioPlayer/audioPlayer'; |
9 | 6 |
|
10 | 7 | import { Model } from './model';
|
11 | 8 |
|
@@ -159,6 +156,49 @@ export class Viewer {
|
159 | 156 |
|
160 | 157 | resizeObserver.observe(parentElement!);
|
161 | 158 |
|
| 159 | + // 假设你已经有一个场景和 VRM 模型 |
| 160 | + const raycaster = new THREE.Raycaster(); |
| 161 | + const mouse = new THREE.Vector2(); |
| 162 | + |
| 163 | + // 添加触摸事件 |
| 164 | + canvas.addEventListener( |
| 165 | + 'click', |
| 166 | + (event) => { |
| 167 | + if (!this.model) { |
| 168 | + return; |
| 169 | + } |
| 170 | + const rect = canvas.getBoundingClientRect(); |
| 171 | + const canvasWidth = rect.width; |
| 172 | + const canvasHeight = rect.height; |
| 173 | + |
| 174 | + // 将鼠标坐标转换到[-1, 1]范围 |
| 175 | + mouse.x = ((event.clientX - rect.left) / canvasWidth) * 2 - 1; // 水平坐标 |
| 176 | + mouse.y = -((event.clientY - rect.top) / canvasHeight) * 2 + 1; // 垂直坐标 |
| 177 | + |
| 178 | + // 更新射线 |
| 179 | + raycaster.setFromCamera(mouse, this._camera!); |
| 180 | + |
| 181 | + // 检测与 VRM 模型的交互 |
| 182 | + const intersects = raycaster.intersectObjects(this._scene.children, true); |
| 183 | + if (intersects.length > 0) { |
| 184 | + // 触摸到模型,执行反馈效果 |
| 185 | + const touchedObject = intersects[0].object; |
| 186 | + const vrmNodeName = this.model.vrm?.humanoid?.getNormalizedBoneNode( |
| 187 | + VRMHumanBoneName.Chest, |
| 188 | + )?.name; // 'Normalized_J_Bip_C_Chest' |
| 189 | + |
| 190 | + // TODO: 如何对应 |
| 191 | + |
| 192 | + // console.log(touchedObject, vrmNodeName); |
| 193 | + // console.log(intersects); |
| 194 | + // console.log(this.model.vrm?.scene.children); |
| 195 | + // touchedObject.material.color.set(0xff0000); // 改变颜色为红色 |
| 196 | + // 你可以在这里添加更多的反馈效果 |
| 197 | + } |
| 198 | + }, |
| 199 | + false, |
| 200 | + ); |
| 201 | + |
162 | 202 | this.isReady = true;
|
163 | 203 | this.update();
|
164 | 204 | }
|
|
0 commit comments