当前位置:   article > 正文

基于vue与three.js给模型添加标签(CSS2DRenderer与CSS2DObject)_threejs css2dobject

threejs css2dobject

第一步,引入

  1. import {
  2. CSS2DRenderer,
  3. CSS2DObject,
  4. } from "three/examples/jsm/renderers/CSS2DRenderer.js";

第二步,创建渲染器

  1. this.tagRenderer = new CSS2DRenderer();
  2. this.tagRenderer.setSize(this.w, this.h);
  3. this.tagRenderer.domElement.style.position = "absolute";
  4. this.tagRenderer.domElement.style.zIndex = 9999999;
  5. this.tagRenderer.domElement.style.top = "0px";
  6. this.tagRenderer.domElement.style.pointerEvents = "none";
  7. this.$refs.three_container.appendChild(this.tagRenderer.domElement);

this.$refs.three_container为3D渲染的容器,注意zindex必须足够高

animate方法:

  1. animate() {
  2. if (!this.renderer) {
  3. return;
  4. }
  5. requestAnimationFrame(this.animate);
  6. this.controls.update(); // 更新控制器状态
  7. TWEEN.update();
  8. this.renderer.render(this.scene, this.camera);
  9. if (!this.tagRenderer) {
  10. return;
  11. }
  12. this.tagRenderer.render(this.scene, this.camera);
  13. },

第三步,创建dom

  1. this.options.forEach((item) => {
  2. if (item.position) {
  3. let domEle = document.createElement("div");
  4. domEle.className = "tag";
  5. domEle.innerHTML = `<div>${item.label}</div>
  6. <div>今日电耗:1892.01kwh</div>
  7. `;
  8. let domEleObj = new CSS2DObject(domEle);
  9. domEleObj.position.set(
  10. item.position.x,
  11. item.position.y,
  12. item.position.z
  13. );
  14. this.scene.add(domEleObj);
  15. }
  16. });
  1. ::v-deep .tag {
  2. box-shadow: 0 0 2px #00ffff inset;
  3. background: linear-gradient(#00ffff, #00ffff) left top;
  4. background-repeat: no-repeat;
  5. background-size: 1px 6px, 6px 1px;
  6. background-color: rgba(0, 0, 0, 0.8);
  7. color: #ffffff;
  8. font-size: 12px;
  9. padding: 4px 10px;
  10. position: absolute;
  11. z-index: 9;
  12. display: inline;
  13. }

得用deep,不然容易不生效 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/121629
推荐阅读
相关标签
  

闽ICP备14008679号