vue3的three几何体加入材质贴图

如何引用three请看这篇文章
引入three

//贴纸
请添加图片描述

//效果
在这里插入图片描述

//代码

<!--  -->
<template>
    <div id="container">123</div>
</template>
  
  <script>
//导入three
let scene = null,
camera = null,
cube = null,
renderer = null
import * as THREE from 'three'
//导入轨道控制器
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls'
import { defineComponent, onMounted, reactive, toRefs} from 'vue';
export default defineComponent({
  setup () {
    console.log(THREE)
    const state = reactive({
        controls:null
    })
    const render = () =>{
       //1.创建场景
       scene = new THREE.Scene();
       //2.创建相机
       camera = new THREE.PerspectiveCamera(35,window.innerWidth/window.innerHeight,0.1,1000);
       //设置相机位置
       camera.position.set(0,0,10);
       scene.add(camera);
       //3.定义材质贴图
       const textureLoader = new THREE.TextureLoader();
       //这里是引入贴纸,文件放在public文件夹下面
       const doorColorTexture = textureLoader.load("./1001.png");
       //4.添加物体
       const cubeGeometry = new THREE.SphereGeometry();
       //材质
       const basicMaterial = new THREE.MeshBasicMaterial({
        map:doorColorTexture
       })
       cube = new THREE.Mesh(cubeGeometry,basicMaterial);
       scene.add(cube);

       //5.初始化渲染器
       renderer = new THREE.WebGLRenderer();
       //设置渲染器尺寸大小
       renderer.setSize(window.innerWidth,window.innerHeight);
       //将webgl渲染的canvas内容添加到div
       let container = document.getElementById('container');
       container.appendChild(renderer.domElement);
       //使用渲染器 通过相机将场景渲染出来
       renderer.render(scene,camera);
       
       console.log(OrbitControls);
       //加入轨道控制器
       state.controls = new OrbitControls(camera,renderer.domElement);
    }
    const animate = () => {
        //需要加动画效果 否则轨道控制器无非使用
        requestAnimationFrame(animate);
        cube.rotation.x += 0.01;
        cube.rotation.y += 0.01;
        cube.rotation.z += 0.01;
        renderer.render(scene,camera);
    }
    onMounted(()=>{
        render()
        animate()
    })
    return {
        ...toRefs(state)
    };
  },

})
  </script>
  <style scoped>
  #container{
    width:100vw;
    height:100vh;
  }
  </style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

范天缘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值