three中加css2d属性框展示
three中加css2d属性框展示
vue项目
一、引入文件CSS2DRenderer, CSS2DObject
1 | import { CSS2DRenderer, CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js' ; |
二、data中定义全局变量
1 2 | divLabel: null , labelRenderer: null , |
三、methods中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | divRender(position) { this .divLabel= document.getElementById( "divPop" ) this .windowPosition = this .transPosition(position); }, labelRender(){ this .labelRenderer = new CSS2DRenderer() this .labelRenderer.setSize(window.innerWidth, window.innerHeight); this .labelRenderer.domElement.style.position = 'absolute' ; this .labelRenderer.domElement.style.top = 0; // 去掉鼠标捕捉事件 this .labelRenderer.domElement.style.pointerEvents = "none" ; document.body.appendChild( this .labelRenderer.domElement); }, transPosition(position) { let world_vector = new THREE.Vector3(position.x, position.y, position.z); let vector = world_vector.project( this .camera); let halfWidth = window.innerWidth / 2, halfHeight = window.innerHeight / 2; return { x: Math.round(vector.x * halfWidth + halfWidth), y: Math.round(-vector.y * halfHeight + halfHeight) }; }, |
四、在render()方法中写入
1 | this .labelRenderer.render( this .scene, this .camera) |
五、在resize()方法中写入
1 | this .labelRenderer.setSize( window.innerWidth, window.innerHeight ); |
六、在获取模型位置的地方加上(换成你对应的)
1 2 3 4 | let objPosition = gltf.scene.children[0].position; this .divRender(objPosition) let pointLabel = new CSS2DObject( this .divLabel) gltf.scene.add(pointLabel); |
有疑问或不懂的,请加qq学习群讨论qq: 910316886
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现