import {OrbitControls} from "three/examples/jsm/controls/OrbitControls.js";
import {CSS2DRenderer,CSS2DObject} from "three/examples/jsm/renderers/CSS2DRenderer.js"
// 创建物体
const cube = new THREE.BoxGeometry(2,2,2);
const cubeMaterial = new THREE.MeshBasicMaterial({color:"#ff0000"})
const code = new THREE.Mesh(cube,cubeMaterial)
scene.add(code)
// 创建标签
const earthDic = document.createElement("div");
earthDic.className = "label";
earthDic.textContent = "文字内容"
const earthLabel = new CSS2DObject(earthDic);
earthLabel.position.set(0,1,0);
code.add(earthLabel)
// 实例化CSS2D渲染器
labelRenderer = new CSS2DRenderer();
labelRenderer.setSize( window.innerWidth, window.innerHeight );
labelRenderer.domElement.style.position = "fixed";
labelRenderer.domElement.style.left = "0px";
labelRenderer.domElement.style.top = "0px";
labelRenderer.domElement.style.color = "#ffffff";
// labelRenderer.domElement.style.pointerEvents = 'none';
threeContainer.appendChild(labelRenderer.domElement)
// 初始化轨道器,为CSS2D可以和模型同步移动,需要设置为labelRenderer渲染器
controls = new OrbitControls(camera,labelRenderer.domElement)
暂无评论