Vue three.js基础四大组件实现三维效果(三)贴图/镜头移动/地板

Vue three.js基础四大组件实现三维效果(三)贴图/镜头移动/地板

//未贴图
 let material = new THREE.MeshPhongMaterial({color:"#f60"})
//贴图
 const loader = new THREE.TextureLoader()//贴图
 let material = new THREE.MeshPhongMaterial({ map: loader.load('https://threejsfundamentals.org/threejs/resources/images/wall.jpg'),})
<template>
  <div id="three" style="height: 1100px;width: 1100px;"></div>
</template>

<script>
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls'
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  mounted(){
    this.qwe()
  },
  methods: {
    qwe () {
      const conter = document.getElementById("three")
      let scren = new THREE.Scene() // 场景
      //灯光
      {
        const color = 0xFFFFFF;
        const intensity = 1;
        const light = new THREE.DirectionalLight(color, intensity);
        light.position.set(1, 1, 2);//x,y,z
        scren.add(light);
      }
      let camera = new THREE.PerspectiveCamera(50, conter.clientWidth / conter.clientHeight, 1, 1000)//透视相机
      let render = new THREE.WebGL1Renderer()//渲染器
      render.setClearColor("#f60")
      render.setSize(conter.clientWidth, conter.clientHeight)//渲染器大小
      conter.appendChild(render.domElement)//渲染结果加载到容器里面

      //镜头可移动
      const controls = new OrbitControls(camera,render.domElement);
      controls.screenSpacePanning = true
      controls.update();

      const loader = new THREE.TextureLoader()//贴图
      //添加地板
      const geome = new THREE.PlaneBufferGeometry(20,20)
      const mate = new THREE.MeshPhongMaterial({ map: loader.load('https://threejsfundamentals.org/threejs/resources/images/wall.jpg'),})
      let mesh = new THREE.Mesh(geome,mate)
      mesh.rotation.x = -Math.PI/2
      mesh.rotation.z = -Math.PI
      // mesh.rotation.y = Math.PI/2
      mesh.position.y = -1.3
      scren.add(mesh)

      let geometry = new THREE.BoxGeometry(0.5,0.5,0.5)//几合体 宽长高

      let material = new THREE.MeshPhongMaterial({ map: loader.load('https://threejsfundamentals.org/threejs/resources/images/wall.jpg'),})//材质 MeshBasicMaterial不受灯光影响
      let cub = new THREE.Mesh(geometry, material)//网格
      cub.position.z = -2
      cub.position.y = 2

      let geometry2 = new THREE.DodecahedronBufferGeometry (0.5,1)//球
      let material2 = new THREE.MeshPhongMaterial({color:0xffff00 })//材质 MeshBasicMaterial不受灯光影响
      let cub2 = new THREE.Mesh(geometry2, material2)//网格

      const geometry3 = new THREE.ConeBufferGeometry(0.3,0.5,50);//圆锥
      let material3 = new THREE.MeshPhongMaterial({color:0xff0f00 })//材质 MeshBasicMaterial不受灯光影响
      let cub3 = new THREE.Mesh(geometry3, material3)//网格

      let geometry4 = new THREE.BoxGeometry(1,1,1)//几合体 宽长高
      const loader4 = new THREE.TextureLoader()//贴图
      let material4 = [
        new THREE.MeshPhongMaterial({ map: loader4.load('https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1107222352,326734774&fm=26&gp=0.jpg'),}),//材质 MeshBasicMaterial不受灯光影响
        new THREE.MeshPhongMaterial({ map: loader4.load('https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2710640311,2174739898&fm=26&gp=0.jpg'),}),//材质 MeshBasicMaterial不受灯光影响
        new THREE.MeshPhongMaterial({ map: loader4.load('https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3740053622,1149390982&fm=26&gp=0.jpg'),}),//材质 MeshBasicMaterial不受灯光影响
        new THREE.MeshPhongMaterial({ map: loader4.load('https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1032090787,3027113824&fm=26&gp=0.jpg'),}),//材质 MeshBasicMaterial不受灯光影响
        new THREE.MeshPhongMaterial({ map: loader4.load('https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3738970635,2195747010&fm=26&gp=0.jpg'),}),//材质 MeshBasicMaterial不受灯光影响
        new THREE.MeshPhongMaterial({ map: loader4.load('https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1532844926,3671374399&fm=26&gp=0.jpg'),})//材质 MeshBasicMaterial不受灯光影响
      ]
      let cub4 = new THREE.Mesh(geometry4, material4)//网格

      cub3.position.x = 1.5
      cub2.position.x = -1.5
      scren.add(cub)//将网格加到场景去
      scren.add(cub2)
      scren.add(cub3)
      scren.add(cub4)
      camera.position.z = 5//改变相机位置


      function renders(){
        requestAnimationFrame(renders)
        cub.rotation.x+=0.01//动起来
        cub.rotation.y+=0.01//动起来
        cub2.rotation.x-=0.01//动起来
        cub2.rotation.y-=0.01//动起来
        cub3.rotation.x-=0.01//动起来
        cub3.rotation.z-=0.01//动起来
        cub4.rotation.x-=0.01//动起来
        cub4.rotation.z-=0.01//动起来
        render.render(scren,camera)
      }

      renders()

    }
  }
}
</script>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Three.js提供的SphereGeometry和MeshPhongMaterial创建一个具有光泽感的球体。以下是一个示例代码: ``` <template> <div id="container"></div> </template> <script> import * as THREE from 'three' import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' export default { mounted() { const container = document.getElementById('container') const scene = new THREE.Scene() const camera = new THREE.PerspectiveCamera( 75, container.clientWidth / container.clientHeight, 0.1, 1000 ) camera.position.z = 5 const renderer = new THREE.WebGLRenderer() renderer.setSize(container.clientWidth, container.clientHeight) container.appendChild(renderer.domElement) const controls = new OrbitControls(camera, renderer.domElement) const sphereGeometry = new THREE.SphereGeometry(1, 32, 32) const sphereMaterial = new THREE.MeshPhongMaterial({ color: 0xffffff, specular: 0x222222, shininess: 25 }) const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial) scene.add(sphere) const ambientLight = new THREE.AmbientLight(0x404040) scene.add(ambientLight) const pointLight = new THREE.PointLight(0xffffff, 1, 100) pointLight.position.set(10, 10, 10) scene.add(pointLight) const animate = function () { requestAnimationFrame(animate) sphere.rotation.x += 0.01 sphere.rotation.y += 0.01 renderer.render(scene, camera) } animate() } } </script> ``` 这个代码在Vue.js项目中使用Three.js创建了一个带有OrbitControls(鼠标交互控制)的场景。可以根据需要修改球体的大小、材质等属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值