欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

手把手教你在vue中使用three.js

 更新時(shí)間:2023年03月01日 10:55:00   作者:繁星如夢&  
最近在vue3項(xiàng)目中通過three.js實(shí)現(xiàn)了實(shí)際的三維效果demo,下面這篇文章主要給大家介紹了關(guān)于在vue中使用three.js的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

在vue中使用three.js

下面我會(huì)介紹three.js的基礎(chǔ)寫法,和在vue中寫入three.js的寫法。

three在vue中使用的時(shí)候,用到紋理圖片、模型加載不出來的時(shí)候的解決辦法,在下面會(huì)具體體現(xiàn)出來。

效果展示:vr看房效果

1.首先安裝three.js、引入

npm install three

在你需要的頁面內(nèi)引入three.js

//import * as THREE from 'three' 
import * as Three from 'three'  

前邊這個(gè)名字是自己定義的

2.在頁面內(nèi)寫入three.js

首先寫過three.js基礎(chǔ)的要分為幾部分:

01:創(chuàng)建場景

const scene = new THREE.Scene()

02.創(chuàng)建相機(jī)

代表著相機(jī):角度、寬高比、近看、遠(yuǎn)

const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)

這代表相機(jī)的位置:x y z

camera.position.set(0, 0, 10)

把相機(jī)添加到場景中

scene.add(camera)

03.創(chuàng)建一個(gè)幾何體

這是一個(gè)正方體

const Geometry = new THREE.BoxGeometry(1, 1, 1)

// 幾何體材質(zhì)
const texture = new THREE.MeshBasicMaterial({
  color: 0xffff
})

// 將幾何體和材質(zhì)創(chuàng)建成物體
const mesh = new THREE.Mesh(Geometry, texture)
// 將幾何體添加到場景中
scene.add(mesh)

04.創(chuàng)建渲染器

// 設(shè)置渲染器的大小
render.setSize(window.innerWidth, window.innerHeight)

// 將渲染器添加到頁面 將webgl渲染的canves添加到body
document.body.appendChild(render.domElement)

下邊是把three.js寫入vue2中:

上面介紹了three.js的基礎(chǔ)寫法,下邊是vue2的寫法

01.引入

軌道控制器為了讓模型顯示的更為自然

import * as Three from 'three'
// 導(dǎo)入軌道控制器
import {
  OrbitControls
} from 'three/examples/jsm/controls/OrbitControls'
// import {RGBELoader} from 'three/examples/jsm/loaders/RGBELoader'
let scene = null,
camera=null,
renderer=null,
mesh=null

02.頁面div的承載

<template>
  <div id="container">
   
  </div>
</template>

03.js中獲取div、創(chuàng)建相機(jī)場景

我用的是最原始js獲取的,在vue中可以使用ref

let container = document.getElementById('container');

      // 添加相機(jī)
      camera = new Three.PerspectiveCamera(70, container.clientWidth/container.clientHeight, 0.01, 1000);
      // 相機(jī)的位置
      camera.position.z = 0.1

      // 場景
      scene = new Three.Scene()

04.創(chuàng)建物體

我這里邊創(chuàng)建的是一個(gè)比較大的模型,用的貼圖紋理,也可以創(chuàng)建正方體之類的,上面有介紹到

let geometry=new Three.SphereBufferGeometry(5,32,32)
       let a=new Three.TextureLoader().load(`static/image/8.jpg`)
        
      let material = new Three.MeshBasicMaterial({map: a });
      mesh = new Three.Mesh(geometry, material);
  mesh.geometry.scale(1, 1, -1);
  scene.add(mesh);

05.初始化渲染器

 // 初始化渲染器
      renderer = new Three.WebGLRenderer({antialias:true});
      // 渲染器的大小
      renderer.setSize(container.clientWidth,container.clientHeight);
      // 將渲染器添加到頁面
      container.appendChild(renderer.domElement);

06.軌道控制器

 // 創(chuàng)建軌道控制器 相機(jī)圍繞模型看到的角度
      const OrbitControl = new OrbitControls(camera, renderer.domElement)
      // 設(shè)置軌道自然
      OrbitControl.enableDamping = true
      // 設(shè)置慣性
      OrbitControl.update()

07.瀏覽器自動(dòng)渲染

animate(){
      // 瀏覽器自動(dòng)渲染下一幀
      requestAnimationFrame(this.animate);
     
      // 渲染到頁面
      renderer.render(scene,camera); 
    }

以上是three.js在vue的寫法,是要放到事件里邊的,下面我會(huì)吧完整代碼拿出來

遇到的問題:圖片的位置具體原因我前邊的文章提到過,要把圖片放到publice下面

<!--  -->
<template>
  <div id="container">
   
  </div>
</template>

<script>
import * as Three from 'three'
// 導(dǎo)入軌道控制器
import {
  OrbitControls
} from 'three/examples/jsm/controls/OrbitControls'
// import {RGBELoader} from 'three/examples/jsm/loaders/RGBELoader'
let scene = null,
camera=null,
renderer=null,
mesh=null
export default {
  data () {
    return {
        
    };
  },
  methods:{
    init(){
      let container = document.getElementById('container');

      // 添加相機(jī)
      camera = new Three.PerspectiveCamera(70, container.clientWidth/container.clientHeight, 0.01, 1000);
      // 相機(jī)的位置
      camera.position.z = 0.1

      // 場景
      scene = new Three.Scene()
      // 創(chuàng)建球
      let geometry=new Three.SphereBufferGeometry(5,32,32)
       let a=new Three.TextureLoader().load(`static/image/8.jpg`)
        
      let material = new Three.MeshBasicMaterial({map: a });
      mesh = new Three.Mesh(geometry, material);
  mesh.geometry.scale(1, 1, -1);
  scene.add(mesh);
      
      // 初始化渲染器
      renderer = new Three.WebGLRenderer({antialias:true});
      // 渲染器的大小
      renderer.setSize(container.clientWidth,container.clientHeight);
      // 將渲染器添加到頁面
      container.appendChild(renderer.domElement);



      // 創(chuàng)建軌道控制器 相機(jī)圍繞模型看到的角度
      const OrbitControl = new OrbitControls(camera, renderer.domElement)
      // 設(shè)置軌道自然
      OrbitControl.enableDamping = true
      // 設(shè)置慣性
      OrbitControl.update()


    },
    animate(){
      // 瀏覽器自動(dòng)渲染下一幀
      requestAnimationFrame(this.animate);
     
      // 渲染到頁面
      renderer.render(scene,camera); 
    }
  },
  mounted(){
      this.init()
      this.animate()
  }

}

</script>
<style scoped>
#container{
    width: 100vw;
    height: 100vh;
}
</style>

 總結(jié)

到此這篇關(guān)于在vue中使用three.js的文章就介紹到這了,更多相關(guān)vue使用three.js內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于Vue的Drawer組件實(shí)現(xiàn)

    基于Vue的Drawer組件實(shí)現(xiàn)

    本文將從零實(shí)現(xiàn)一個(gè)Drawer抽屜組件,組件用 vue2 語法寫的,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • vue項(xiàng)目實(shí)現(xiàn)背景顏色以及下劃線從左到右漸變動(dòng)畫效果

    vue項(xiàng)目實(shí)現(xiàn)背景顏色以及下劃線從左到右漸變動(dòng)畫效果

    這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)背景顏色以及下劃線從左到右漸變動(dòng)畫效果,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 詳解Vue項(xiàng)目引入CreateJS的方法(親測可用)

    詳解Vue項(xiàng)目引入CreateJS的方法(親測可用)

    CreateJS是基于HTML5開發(fā)的一套模塊化的庫和工具。這篇文章主要介紹了Vue項(xiàng)目引入CreateJS的方法(親測),需要的朋友可以參考下
    2019-05-05
  • VUE 3D輪播圖封裝實(shí)現(xiàn)方法

    VUE 3D輪播圖封裝實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了VUE 3D輪播圖封裝實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 如何在 Vue.js 中使用第三方j(luò)s庫

    如何在 Vue.js 中使用第三方j(luò)s庫

    本篇文章主要介紹了如何在 Vue.js 中使用第三方j(luò)s庫,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • VUE 使用中踩過的坑

    VUE 使用中踩過的坑

    本篇是我對vue使用過程中以及對一些社區(qū)朋友提問我的問題中做的一些總結(jié),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-02-02
  • Vue中常用的鼠標(biāo)移入移出事件詳解

    Vue中常用的鼠標(biāo)移入移出事件詳解

    這篇文章主要給大家介紹了關(guān)于Vue中常用的鼠標(biāo)移入移出事件的相關(guān)資料,鼠標(biāo)移入移出事件在 Vue 中可以通過@mouseenter和@mouseleave來綁定,需要的朋友可以參考下
    2023-07-07
  • 在vue中路由使用this.$router.go(-1)返回兩次問題

    在vue中路由使用this.$router.go(-1)返回兩次問題

    這篇文章主要介紹了在vue中路由使用this.$router.go(-1)返回兩次問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • vue-devtools安裝使用全過程

    vue-devtools安裝使用全過程

    這篇文章主要介紹了vue-devtools安裝使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • vuex根據(jù)不同的用戶權(quán)限展示不同的路由列表功能

    vuex根據(jù)不同的用戶權(quán)限展示不同的路由列表功能

    最近接到一個(gè)新的需求,要求將系統(tǒng)的用戶進(jìn)行分類,用戶登陸后根據(jù)不同的用戶權(quán)限展示不同的功能列表。這篇文章主要介紹了vuex根據(jù)不同的用戶權(quán)限展示不同的路由列表,需要的朋友可以參考下
    2019-09-09

最新評論