解决three.js中加载纹理贴图时,初次渲染不显示的问题

本文介绍了如何在Three.js中创建场景、网格、纹理贴图,并通过TextureLoader加载图片,设置Mesh、AmbientLight和PerspectiveCamera,最后实现渲染。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果:

 

解决方法:主要是将一些构建网格对象的操作放在了textureLoader.load()方法中,加载图片也用了require

init() {
      // 1, 创建场景对象
      this.scene = new this.$three.Scene();
      // 2, 创建立方缓冲几何体
      this.geometry = new this.$three.BoxGeometry(100, 100, 100);
      // 3,创建纹理贴图加载器对象
      const textureLoader = new this.$three.TextureLoader();
      const texture = textureLoader.load(require("../../assets/a.png"), () => {
        // 4,创建网格材质对象
        // const material = new this.$three.MeshLambertMaterial({
        const material = new this.$three.MeshBasicMaterial({
          map: texture, // map 表示材质的颜色贴图属性
        });
        // 5, 创建网格对象
        this.mesh = new this.$three.Mesh(this.geometry, material);
        this.scene.add(this.mesh);
        // 6,创建辅助坐标轴对象
        const axesHelper = new this.$three.AxesHelper(200);
        this.scene.add(axesHelper);
        // 6.1 创建环境光对象
        const light = new this.$three.AmbientLight(0xffffff);
        this.scene.add(light);
        // 7,创建透视投影相机对象
        this.camera = new this.$three.PerspectiveCamera(60, 1, 0.01, 1000);
        this.camera.position.set(200, 200, 200);
        this.camera.lookAt(0, 0, 0);
        // 8,创建渲染器对象
        this.renderer = new this.$three.WebGLRenderer();
        this.renderer.setSize(1000, 800);
        this.renderer.render(this.scene, this.camera);
        document
          .getElementById("threejs")
          .appendChild(this.renderer.domElement);
      });

      const controls = new OrbitControls(this.camera, this.renderer.domElement);
      controls.addEventListener("change", () => {
        this.renderer.render(this.scene, this.camera);
      });
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值