vue+threejs写界面:标注发光动画

415 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第17天,点击查看活动详情


写在前面

本文用vue+threejs实现标注发光动画。

代码说明

首先是threejs相机camera、场景scene、灯光light、渲染器renderer、CSS 2D渲染器、轨道控制器controls的创建,然后是地面的创建、模型的导入,在完整代码中都有详细注释,以前写的文章也都详细说明,这里就不多说了。

实现标注发光动画的关键其实是写css动画,所以在这里主要说明用css写发光动画的代码。

效果gif如下:

20221014_102553.gif

关键代码是css动画,如下,使用animation给元素border添加动画animation: spread 1.5s linear infinite;,添加后,元素会在1.5s内线性变化元素的宽度、高度和透明度。

spread是动画的名称,根据这个名称去找对应的动画,1.5s 是动画的持续时间,linear是动画的速度曲线,infinite表示动画执行无数次

  .border {
    ...
    animation: spread linear 1.5s infinite;
  }
  @keyframes spread {
    0% {
      width: 40px;
      height: 24px;
      opacity: 1;
      // transform: scale(1);
    }

    100% {
      width: 80px;
      height: 48px;
      opacity: 0;
      // transform: scale(4);
    }
  }

然后将标注代码添加到threejs的 CSS 2D渲染器

this.Rabbit = document.getElementById("Rabbit");
const RabbitTag = new CSS2DObject(this.Rabbit);
RabbitTag.position.set(mesh.position.x, 0.25, mesh.position.z);

最终渲染的时候就会按设置的位置position显示该动画。

完整代码

<template>
  <div class="item">
    <div id="THREE54">
      <div id="Rabbit">
        <div class="danger">
          <div class="border"></div>
        </div>
        <img src="../../assets/imgs/tag.png" class="img" />
        <div class="name">兔子</div>
      </div>
    </div>
  </div>
</template>

<script>
import * as THREE from "three";

import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader.js";

import {
  CSS2DRenderer,
  CSS2DObject,
} from "three/examples/jsm/renderers/CSS2DRenderer.js"; // CSS 2D渲染器

export default {
  data() {
    return {
      camera: null,
      scene: null,
      dracoLoader: null,
      renderer: null,
      controls: null,

      labelRenderer: null,
    };
  },
  mounted() {
    this.dracoLoader = new DRACOLoader();
    this.dracoLoader.setDecoderPath("js/libs/draco/");
    this.dracoLoader.setDecoderConfig({ type: "js" });
    this.initScene(); // 创建场景
    this.initCamera(); // 创建相机
    this.initLight(); // 创建灯光
    this.initRenderer(); // 创建渲染器
    this.initLabelRenderer(); // 创建 CSS 2D渲染器
    this.initControls(); //创建轨道控制器
    this.initGround(); // 创建地面
    this.initModel(); // 加载模型
  },
  methods: {
    initScene() {
      this.scene = new THREE.Scene();
      this.scene.background = new THREE.Color(0x000000); // 设置场景背景颜色
    },
    initCamera() {
      this.camera = new THREE.PerspectiveCamera(
        35,
        (window.innerWidth - 201) / window.innerHeight,
        1,
        500
      ); // 透视相机
      this.camera.position.x = 0.5;
      this.camera.position.y = 0.5; // 设置相机的位置
      this.camera.position.z = 1.8;
      this.scene.add(this.camera); // 将相机添加到场景中
    },
    initLight() {
      const light = new THREE.DirectionalLight(0xffffff); // 平行光
      light.position.set(0.5, 1.0, 0.5).normalize(); // 设置平行光的方向,从(0.5, 1.0, 0.5)->target一般(0, 0, 0)
      this.scene.add(light); // 将灯光添加到场景中

      const ambLight = new THREE.AmbientLight(0xf0f0f0, 0.1); // 环境光
      this.scene.add(ambLight);
    },
    initRenderer() {
      this.renderer = new THREE.WebGLRenderer({ antialias: true });
      this.renderer.outputEncoding = THREE.sRGBEncoding;
      this.renderer.setPixelRatio(window.devicePixelRatio);
      this.renderer.setSize(window.innerWidth - 201, window.innerHeight);
      document.getElementById("THREE54").appendChild(this.renderer.domElement);
    },
    initLabelRenderer() {
      this.labelRenderer = new CSS2DRenderer();
      this.labelRenderer.setSize(window.innerWidth - 201, window.innerHeight);
      this.labelRenderer.domElement.style.position = "absolute";
      this.labelRenderer.domElement.style.top = "0px";
      document
        .getElementById("THREE54")
        .appendChild(this.labelRenderer.domElement);
    },
    initControls() {
      this.controls = new OrbitControls(
        this.camera,
        this.labelRenderer.domElement
      );
      this.controls.addEventListener("change", this.render);
      this.controls.enableDamping = true; // 开启惯性
    },
    initGround() {
      const ground = new THREE.Mesh(
        new THREE.BoxGeometry(1, 0.0015, 1),
        new THREE.MeshPhongMaterial({
          color: 0x999999,
          depthWrite: false,
          transparent: true,
          opacity: 1,
        })
      );
      ground.receiveShadow = true;
      this.scene.add(ground);
    },
    initModel() {
      this.dracoLoader.load("/models/models/draco/bunny.drc", (geometry) => {
        geometry.computeVertexNormals();

        const material = new THREE.MeshStandardMaterial({
          color: 0xffffff,
        });
        let mesh = new THREE.Mesh(geometry, material);
        mesh.castShadow = true;
        mesh.receiveShadow = true;
        mesh.position.y = -0.035;

        // x左右 z前后
        this.Rabbit = document.getElementById("Rabbit");
        const RabbitTag = new CSS2DObject(this.Rabbit);
        RabbitTag.position.set(mesh.position.x, 0.25, mesh.position.z);
        mesh.add(RabbitTag);
        RabbitTag.layers.set(0);

        this.scene.add(mesh);

        this.dracoLoader.dispose();

        this.animate();
      });
    },
    animate() {
      requestAnimationFrame(this.animate);

      this.controls.update();

      this.render();
    },
    render() {
      this.renderer.render(this.scene, this.camera);
      this.labelRenderer.render(this.scene, this.camera);
    },
  },
};
</script>

<style lang="less" scoped>
#Rabbit {
  position: relative;
  .img,
  .name {
    position: absolute;
    width: 60px;
    left: -40px;
  }
  .name {
    top: 6px;
    left: -36px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
    color: #0b49b0;
  }
  .danger {
    width: 100px;
    height: 100px;
    position: absolute;
    left: -60px;
    top: -34px;
  }
  .border {
    width: 20px;
    height: 12px;
    background-color: #ff0000;
    border-radius: 3px;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    animation: spread linear 1.5s infinite;
  }

  @keyframes spread {
    0% {
      width: 40px;
      height: 24px;
      opacity: 1;
      // transform: scale(1);
    }

    100% {
      width: 80px;
      height: 48px;
      opacity: 0;
      // transform: scale(4);
    }
  }
}
</style>

写在最后

以上就是所有的代码和说明。