vue three.js sprite canvas img 预加载图片 更新文本

前言

老早之前就搞过sprite用canvas做材质然后更新canvas里的文本的案例,当时比较乱,还用了windows.requestAnimationFrame(),而且在html里加一个我感觉有点不整洁,再次总结提高一下

效果

温度值实时更新

核心代码
mounted(){
	//温度值
	this.tem1 = null
	//预设一些需要的变量
	this.canvas = document.createElement("canvas");
    this.context = null;
    this.sprite1 = null;
    this.canvasTexture = null;
    //预加载图片
    this.img = new Image()
    this.img.src="Images/biankuang.png"
}
methods:{
	//绘制canvas
	drawCanvas(){
      this.canvas.setAttribute("width", 498);
      this.canvas.setAttribute("height", 528); //两倍于图片高度
      this.context = this.canvas.getContext("2d"); //获取画图环境,指明为2d
      this.context.drawImage(this.img, 0, 0);
      //文字
      this.context.font = "60px Arial";
      this.context.fillStyle = "#fff";
      this.context.fillText("电机温度:" + this.tem1.toFixed(2) + "℃", 20, 85,450);
    },
    
    //利用绘制的canvas做成材质赋予sprite
    initSprite() {
      this.drawCanvas()
      this.canvasTexture = new THREE.CanvasTexture(this.canvas);
      let spriteMaterial = new THREE.SpriteMaterial({
        map: this.canvasTexture,
        transparent: true,
        opacity: 1,
        depthTest: true,
        depthWrite: false,
      });
      this.sprite1 = new THREE.Sprite(spriteMaterial);
      this.sprite1.scale.set(50, 50, 50);
      this.sprite1.position.set(70, 20, 0);
      this.scene.add(this.sprite1);
      console.log(this.sprite1);
      //模拟更新
      let that =this
      setInterval(() => {
        that.tem1 = parseInt(21) + Math.random()
      }, 2000);
    },
    animate: function () {
      this.drawCanvas()
      this.canvasTexture.needsUpdate = true;
      this.sprite1.material.map = this.canvasTexture
      this.renderer.render(this.scene, this.camera);
      requestAnimationFrame(this.animate);
    },
}
注意点
  • 最开始的优化后代码是在animate()中每帧new一个THREE.CanvasTexture(this.canvas)然后赋值给 this.sprite1.material.map,导致一个问题,页面有时候会运行一段时间突然三维场景就没了,控制台报错为THREE.WebGLRenderer: Context Lost.,原因是显存爆了,从任务管理器可以看到,专用GPU内存利用率会匀速上升,置到占满4.0GB,然后一会儿可能就报那个错并且场景就没了,然后显存会到很低的位置
    专用GPU内存利用率
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vue.js 是一个流行的 JavaScript 框架,用于构建现代化的 web 应用程序。Three.js 是一个使用 WebGL 技术来创建 3D 图形的 JavaScript 库。gltf 文件格式是一种开放、可扩展的文件格式,用于在 3D 应用程序之间传输和共享 3D 模型和场景。 要在 Vue.js 应用程序中加载 gltf 文件,需要使用 Three.js 库来加载和呈现 3D 模型。首先,需要在 Vue.js 应用程序中安装 Three.js 库,可以使用 npm 进行安装。在 Vue 组件中引入 Three.js 库后,可以通过以下步骤加载 gltf 文件: 1. 创建一个 Three.js 场景和相机对象。 2. 创建一个 Three.js 的 GLTFLoader 对象,用于加载 gltf 文件。 3. 使用 GLTFLoader 对象的 load() 方法来加载 gltf 文件,并为其指定回调函数。 4. 在回调函数中将加载的 3D 模型添加到场景中,并渲染相机。 以下是一个简单的示例代码: ``` <template> <div id="container"></div> </template> <script> import * as THREE from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; export default { data() { return { scene: null, camera: null, renderer: null, mesh: null, } }, mounted() { // 创建场景和相机 this.scene = new THREE.Scene(); this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); this.camera.position.z = 5; // 创建渲染器并添加到页面 this.renderer = new THREE.WebGLRenderer({ antialias: true }); this.renderer.setSize(window.innerWidth, window.innerHeight); this.renderer.setClearColor(0x000000, 1); document.getElementById('container').appendChild(this.renderer.domElement); // 创建 GLTFLoader 对象并加载 gltf 文件 const loader = new GLTFLoader(); loader.load('/path/to/model.gltf', (gltf) => { // 将加载的 3D 模型添加到场景中 this.mesh = gltf.scene; this.scene.add(this.mesh); // 开始渲染场景和相机 this.animate(); }); }, methods: { animate() { requestAnimationFrame(this.animate); // 控制模型的旋转角度 if (this.mesh) { this.mesh.rotation.x += 0.01; this.mesh.rotation.y += 0.01; } // 渲染场景和相机 this.renderer.render(this.scene, this.camera); }, }, }; </script> ``` 在这个示例中,首先创建了一个场景和相机对象,然后创建了一个 GLTFLoader 对象并加载 gltf 文件。在回调函数中将加载的 3D 模型添加到场景中,并开始渲染场景和相机。在 animate() 方法中使用 requestAnimationFrame() 方法来更新场景中模型的旋转角度,并最终渲染场景和相机。 ### 回答2: Vuethree.js结合使用可以实现非常炫酷的3D互动效果。其中,加载gltf文件是实现这种效果的常用手段。gltf是一种基于JSON的3D文件格式,它可以携带模型、材质贴图、纹理、动画等信息,非常适合在三维场景中使用。 在Vue的项目中,我们可以使用Vue CLI脚手架工具快速搭建一个基于Vue的Web应用。然后,在Vue组件中引入three.js库和GLTFLoader.js加载器。 接着,在Vue组件中通过three.js提供的Scene、Camera、Renderer等对象,创建一个三维场景。然后,使用GLTFLoader.js加载加载gltf文件,并在回调函数中将模型添加至场景中。 最后,为了让模型动起来,我们可以通过three.js提供的AnimationMixer和AnimationClip等对象,给模型添加动画效果。 除了以上的基本步骤,加载gltf文件还需要注意一些细节,比如模型坐标系的问题、贴图和纹理的路径配置、模型大小适配等等,这需要开发者仔细排查。 总之,通过Vuethree.js的结合使用,加载gltf文件可以让我们在Web应用中实现更加丰富的3D视觉体验。 ### 回答3: Vue是一款流行的JavaScript框架,而Three.js是一个强大的3D渲染引擎,它能够在网页上显示出3D模型和动画效果。而gltf文件格式则是现代的3D模型和场景的标准格式之一。 要在Vue加载gltf文件,我们可以使用Three.js的GLTFLoader。首先,我们需要在Vue项目中安装Three.js和GLTFLoader,可以使用以下命令进行安装: ``` npm install three gltf-loader ``` 接下来,在Vue的组件中创建一个canvas元素和一个Three.js场景。然后,使用GLTFLoader从服务器加载gltf文件。 ``` <template> <div> <canvas ref="canvas"></canvas> </div> </template> <script> import * as THREE from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'; export default { mounted() { // 获取canvas元素 const canvasEl = this.$refs.canvas; // 创建Three.js场景 const scene = new THREE.Scene(); // 创建Three.js相机 const camera = new THREE.PerspectiveCamera(75, canvasEl.clientWidth / canvasEl.clientHeight, 0.1, 1000); camera.position.z = 5; // 创建Three.js渲染器 const renderer = new THREE.WebGLRenderer({ canvas: canvasEl }); renderer.setSize(canvasEl.clientWidth, canvasEl.clientHeight); // 加载gltf文件 const loader = new GLTFLoader(); loader.load('/path/to/model.gltf', function (gltf) { // 将模型添加到场景中 scene.add(gltf.scene); // 渲染场景 renderer.render(scene, camera); }); } }; </script> ``` 在这段代码中,我们首先创建了一个canvas元素作为Three.js渲染器的渲染目标。然后,我们创建了一个场景和相机,并使用GLTFLoader从服务器加载了一个gltf文件。在加载完成后,我们将获取到的3D模型添加到场景中,并使用渲染器进行渲染。 以上就是在Vue加载gltf文件的基本步骤,当然还有更多的细节可以根据具体需求进行优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值