使用射线投射(Raycasting)来检测鼠标点击位置是否与模型相交
- 射线的起点设置为相机的位置,鼠标点击的位置创建一条射线
- intersectObjects判断射线有没有与模型相交
<template>
<div class="app">
<div class="btns"></div>
<div ref="canvesRef" class="canvas-wrap"></div>
</div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import Stats from "three/addons/libs/stats.module.js";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
const canvesRef = ref(null);
const canvasWidth = window.innerWidth;
const canvasHeight = window.innerHeight;
let model = null;
const scene = new THREE.Scene();
const loader = new GLTFLoader();
loader.load(
"../src/model/factory/工厂.gltf",
function (gltf) {
console.log("控制台查看加载gltf文件返回的对象结构", gltf);
model = gltf;
scene.add(gltf.scene);
},
function (val) {
},
function (err) {
console.log(err);
}
);
const camera = new THREE.PerspectiveCamera(
75,
canvasWidth / canvasHeight,
0.1,
3000
);
camera.position.set(-84, 78, 1.3);
camera.lookAt(0, 0, 0);
const pointLight = new THREE.PointLight(0xffffff, 1.0);
pointLight.decay = 0.0;
pointLight.position.set(-100, 190, 110);
scene.add(pointLight);
const axesHelper = new THREE.AxesHelper(200);
scene.add(axesHelper);
const stats = new Stats();
document.body.appendChild(stats.domElement);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(canvasWidth, canvasHeight);
function animate() {
stats.update();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
const cameraControls = new OrbitControls(camera, renderer.domElement);
cameraControls.addEventListener("change", function () {
});
cameraControls.target.set(0, 0, 0);
cameraControls.maxPolarAngle = Math.PI / 2;
onMounted(() => {
canvesRef.value.appendChild(renderer.domElement);
renderer.domElement.addEventListener("click", (event) => {
console.log("模型点击", model);
const px = event.offsetX;
const py = event.offsetY;
const x = (px / canvasWidth) * 2 - 1;
const y = -(py / canvasHeight) * 2 + 1;
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(new THREE.Vector2(x, y), camera);
const intersects = raycaster.intersectObjects(model.scene.children);
console.log("射线器返回的对象", intersects);
if (intersects.length > 0) {
intersects[0].object.material.color.set(0xff0000);
console.log(currentSelectedModel);
} else {
currentSelectedModel = null;
}
});
});
</script>
<style lang="scss" scoped>
.app {
position: relative;
.canvas-wrap {
}
.btns {
position: absolute;
top: 0;
left: 50%;
}
}
</style>