• threejs 3D标注


    在这里插入图片描述

    import { CSS3DObject } from "three/examples/jsm/renderers/CSS3DRenderer";
    
    	gltfLoader.load("./model/exhibit2.glb", (gltf) =>{
                let array = ["雕像", "中药房", "浸制区", "道地沙盘","动物标本区","灵芝","茶话室","药材制作"];
                console.log(gltf)
                this.exhibitionHall = gltf.scene;
                // 判断子元素是否是物体
                this.exhibitionHall.traverse((child) => {
                    if(child.isMesh){
                        child.material.emissiveIntensity = 15;
                    }
                    if (array.indexOf(child.name) !== -1){
                        console.log(child)
                        const css3DObject = this.createTag(child);
                        css3DObject.visible =true
                        this.floor2Tags.push(css3DObject);
                        this.exhibitionHall.add(css3DObject);
                    }
                })
                let fakeChild = {
                    name: "标注",
                    position: {
                        x:100,
                        y:100,
                        z:20}
                }
                const css3DObject = this.createTag(fakeChild);
                css3DObject.visible =true
                this.floor2Tags.push(css3DObject);
                this.exhibitionHall.add(css3DObject);
    
                this.exhibitionHall.position.set(0,0,0)
                this.exhibitionHall.scale.set(15,15,15)
                console.log(gltf)
                scene.add(this.exhibitionHall)
    
            })
    
        createTag(object3d) {
            // 创建各个区域的元素
            const element = document.createElement("div");
            element.className = "elementTag";
            element.innerHTML = `
          

    ${object3d.name}

    温度:26℃

    湿度:50%

    `
    ; const objectCSS3D = new CSS3DObject(element); objectCSS3D.position.copy(object3d.position); objectCSS3D.scale.set(0.2, 0.2, 0.2); return objectCSS3D; // scene.add(objectCSS3D); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    css 样式内不要加 scoped 会找不到样式
    // app.vue
    <style lang="less">
    
    .cssrender {
      display: flex;
      position: fixed;
      top: 0;
      left: 0;
      z-index: 100;
      pointer-events: none;
    }
    
    .elementTag {
      position: relative;
      left: -30px;
      top: 20px;
    }
    .elementTag::before {
      content: "";
      display: block;
      position: absolute;
      width: 100px;
      height: 1px;
      background: rgb(127 177 255 / 75%);
      bottom: 0;
      right: -100px;
      transform-origin: 0 0;
      transform: rotate(30deg);
    }
    .elementTag::after {
      content: "";
      display: block;
      position: absolute;
      width: 20px;
      height: 20px;
      background: rgb(127 177 255 / 75%);
      bottom: -65px;
      right: -105px;
      border-radius: 50%;
    }
    
    .elementContent {
      background-color: rgb(20 143 221 / 68%);
      box-shadow: 0px 0px 12px rgb(0 128 255 / 75%);
      border: 1px solid rgb(127 177 255 / 75%);
      padding: 20px;
      color: #efefef;
    }
    
    .elementContent h3 {
      font-size: 20px;
      font-weight: bold;
      margin: 15px 0;
      color: #efefef;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    //index.html 
    // 加入css样式渲染区
    <div class="cssrender"></div>
    
    • 1
    • 2
    • 3
    // renderer.js
    import { CSS3DRenderer } from "three/examples/jsm/renderers/CSS3DRenderer.js";
    
    // 创建css3drender
    // 所有涉及到渲染的 都需要css3drender 去渲染
    const css3drender = new CSS3DRenderer();
    css3drender.setSize(window.innerWidth, window.innerHeight);
    document.querySelector(".cssrender").appendChild(css3drender.domElement);
    export default { css3drender };
    
    
    import rendererModule from "@/three/renderer.js";
    // 使用渲染器渲染相机看到这个场景的内容渲染出来
    rendererModule.renderer.render(scene, cameraModule.activeCamera);
    rendererModule.css3drender.render(scene, cameraModule.activeCamera);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    TatukGIS Developer Kernel使用教程:如何为FMX创建第一个应用程序
    [StartingPoint][Tier1]Bike
    [译]2023年 Web Component 现状
    html2canvas 截图空白 或出现toDataURL‘ on ‘HTMLCanvasElement或img标签没截下来 的所有解决办法
    【ESP32-Face】ESP32人脸检测MTMN 模型以及face_detect()函数详解
    从新手到熟练,怎么学好Zbrush?
    控制DAIKIN大金比例压力溢流阀放大器
    学习开发一个RISC-V上的操作系统(汪辰老师) — unrecognized opcode `csrr t0,mhartid‘报错问题
    conda虚拟环境安装pytorch(gpu版本)纪实
    谷粒商城 高级篇 (十六) --------- 社交登录
  • 原文地址:https://blog.csdn.net/w710537643/article/details/136186073