v3+three.js鼠标触碰物体高亮显示文字

<template>
  <div ref="threeRef"></div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import Stats from 'three/examples/jsm/libs/stats.module.js'
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
// 性能检测
let stats = new Stats()
stats.setMode(0)
document.body.appendChild(stats.domElement)
let threeRef=ref()
const scene=new THREE.Scene()
scene.background=new THREE.Color('#2fa799')
const camera=new THREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,0.1,1000)
camera.position.y=200 // 40
camera.position.x=0
camera.position.z=100 // 30
// 相机朝向
camera.lookAt(scene.position)
// 坐标轴
const axes=new THREE.AxesHelper(50)
scene.add(axes)
// 渲染器
const renderer=new THREE.WebGLRenderer({antialias:true})
renderer.setSize(window.innerWidth,window.innerHeight)
renderer.render( scene, camera );
renderer.outputEncoding=THREE.sRGBEncoding
// 监听窗口变化
window.addEventListener('resize',()=>{
  camera.aspect=window.innerWidth/window.innerHeight
  camera.updateProjectionMatrix()
  renderer.setSize(window.innerWidth,window.innerHeight)
},false)
// 相机
const controls=new OrbitControls(camera,renderer.domElement)
// 环境光
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
// 点光源
const pointLight = new THREE.PointLight(0xffffff, 0.5);
pointLight.position.set(0, 3, 0);
scene.add(pointLight);

const pieceJ=new THREE.BoxGeometry(80,80,80)
const pieceC=new THREE.MeshBasicMaterial({color:'#808080'})
const pieceW=new THREE.Mesh(pieceJ,pieceC)
pieceW.position.x=50
pieceW.position.y=50
pieceW.position.z=50
scene.add(pieceW)


// 创建高亮材质
const highlightMaterial = new THREE.MeshBasicMaterial({
  color: '#1ea6b9'
});
// 悬浮提示框
let currentIntersected;
let tooltip = document.createElement('div');
tooltip.style.position = 'absolute';
tooltip.style.zIndex = '999';
tooltip.style.background = '#e0e7e8';
tooltip.style.padding = '4px';
tooltip.style.border = '1px solid #ccc';
tooltip.style.visibility = 'hidden';
document.body.appendChild(tooltip);
// 监听鼠标移动事件,显示悬浮提示框 点击 mouseup 触碰mousemove
renderer.domElement.addEventListener('mousemove', function(event) {
  let mouse = new THREE.Vector2();
  mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  let raycaster = new THREE.Raycaster();
  raycaster.setFromCamera(mouse, camera);
  let intersects = raycaster.intersectObjects(scene.children, true);
  if (currentIntersected) {
    currentIntersected.material = pieceW;
    currentIntersected = null;
  }
  if (intersects.length > 0) {
    tooltip.style.visibility = 'visible';
    tooltip.style.left = (event.clientX + 20) + 'px';
    tooltip.style.top = (event.clientY - 20) + 'px';
    // tooltip.innerHTML = intersects[0].object.name;
    tooltip.innerHTML=`
    <h1>哇!!!<h1>
    `
    // 触碰变颜色
    currentIntersected = intersects[0].object;
    currentIntersected.material = highlightMaterial;
  } else {
    tooltip.style.visibility = 'hidden';
  }
});

const init=()=>{
  threeRef.value?.appendChild(renderer.domElement)
  stats.update()
  controls.update()
  requestAnimationFrame(init)
  renderer.render( scene, camera );

}
onMounted(()=>{
  init()
})
</script>

<style scoped lang="less">

</style>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值