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

vue3中如何用threejs畫一些簡單的幾何體

 更新時(shí)間:2022年03月14日 16:46:27   作者:阿鋒凌凌漆  
最近學(xué)習(xí)threejs有些時(shí)間了,就想著著手做些東西,下面這篇文章主要給大家介紹了關(guān)于vue3中如何用threejs畫一些簡單的幾何體的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

在vue3中使用threejs畫了幾個(gè)最簡單的幾何體,自動旋轉(zhuǎn)的立方體,圓柱體,球體,并且加入了光照,幾何體影陰部分即光沒照到的地方,成果如下,感興趣的可以看看具體實(shí)現(xiàn)過程~

threejs簡述

  • Three.js是基于原生WebGL封裝運(yùn)行的三維引擎
  • 程序結(jié)構(gòu)->場景——相機(jī)——渲染器
  • 場景Scene

    場景是一個(gè)容器,用來放置幾何體

  • 相機(jī)Camera

    相機(jī)是用來拍攝的工具,通過控制相機(jī)的位置和方向可以獲取不同角度的圖像。

  • 渲染器Renderer

    渲染器利用場景和相機(jī)進(jìn)行渲染,渲染過程好比攝影師拍攝圖像,如果只渲染一次就是靜態(tài)的圖像,如果連續(xù)渲染就能得到動態(tài)的畫面。在JS中可以使用requestAnimationFrame實(shí)現(xiàn)高效的連續(xù)渲染。

[注] 涉及到幾何體,材質(zhì)等具體API沒有做很具體的說明,需要可自行查閱CylinderGeometry – three.js中文文檔 (webgl3d.cn)

依賴包版本

"vite": "^2.8.0",
"three": "^0.138.0",
"vue": "^3.2.25"

vue3操作DOM

-threejs底層是對webgl的封裝,最終是利用canvas做圖形渲染,所以第一步是做操作dom的工作

  • 如下,在steup函數(shù)中使用ref定義一個(gè)響應(yīng)式常量dom后暴露給template使用,把ref掛載到具體的元素上
  • 在initThree中做具體繪制的工作
<template>
  <div>
    demo
  </div>
  <div class="demo-main" ref="dom"></div>
</template>
 <script lang="ts">
  import { defineComponent, onMounted, ref } from "vue";
  import * as THREE from "three";
  import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
  export default defineComponent({
    setup() {
      const dom = ref<HTMLElement | null>(null);

      onMounted(() => {
        initThree(dom.value);
      });

      function initThree(instance: HTMLElement | null) {
           //dosomething
      }

      return { dom };
    },
  });
</script>
<style scoped>
  .demo-main {
    height:500px;
    padding: 9px 14px;
    margin-bottom: 14px;
    background-color: #f7f7f9;
    border: 1px solid #e1e1e8;
    border-radius: 4px;
  }
</style>

創(chuàng)建場景,相機(jī),渲染器 本節(jié)及后續(xù)都在initThree方法中寫

  • 如下我們創(chuàng)建了threejs最基礎(chǔ)的一些東西
        var hetght = instance.clientHeight - 25;
        var width = instance.clientWidth - 25;

        // 創(chuàng)建場景對象Scene
        var scene = new THREE.Scene();

        // 創(chuàng)建相機(jī)對象
        var camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);

        // 創(chuàng)建渲染器對象
        var renderer = new THREE.WebGLRenderer();
        renderer.setSize(width, hetght);

        instance.append(renderer.domElement);
        renderer.render(scene, camera);
        camera.position.z = 5;
        renderer.setClearColor(0xeeeeee, 1.0);

立方體

       // 立方體網(wǎng)格模型
        var cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
        //材質(zhì)對象Material
        //材質(zhì)決定了幾何圖形中的表面是如何畫的。如果幾何圖形是骨架,定義了形狀,那么材質(zhì)就是皮膚。three.js 中有許多不同種類的材質(zhì),他們擁有不同的屬性,像反光,紋理映射,調(diào)整透明度。
        var cubeMaterial = new THREE.MeshLambertMaterial({
          color: 0xff0000,
          opacity: 0.7,
          transparent: true,
        });
        var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
        scene.add(cube);

球體

        // 球體網(wǎng)格模型
        var sphereGeometry = new THREE.SphereGeometry(1, 20, 20);
        var sphereMaterial = new THREE.MeshLambertMaterial({
          color: 0xff00ff,
          specular: 0x4488ee,
          shininess: 12,
        });
        var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); //網(wǎng)格模型對象Mesh
        sphere.translateY(120); //球體網(wǎng)格模型沿Y軸正方向平移120
        sphere.position.set(0, 0, 5);
        scene.add(sphere);

圓柱體

       // 圓柱網(wǎng)格模型
        var cylinderGeometry = new THREE.CylinderGeometry(1, 1, 5, 32);
        var cylinderMaterial = new THREE.MeshLambertMaterial({
          color: 0xffff00,
        });

        var cylinder = new THREE.Mesh(cylinderGeometry, cylinderMaterial); //網(wǎng)格模型對象Mesh
        cylinder.position.set(10, 0, 0); //設(shè)置cylinder模型對象的xyz坐標(biāo)為10,0,0
        scene.add(cylinder); 

坐標(biāo)系

       // 輔助坐標(biāo)系  參數(shù)400表示坐標(biāo)系大小,可以根據(jù)場景大小去設(shè)置
        var axisHelper = new THREE.AxisHelper(20);
        scene.add(axisHelper);

點(diǎn)光源

        //點(diǎn)光源
        var point = new THREE.PointLight(0xffffff);
        point.position.set(0, 0, 0);
        scene.add(point); //點(diǎn)光源添加到場景中

        // 點(diǎn)光源2  位置和point關(guān)于原點(diǎn)對稱
        var point2 = new THREE.PointLight(0xffffff);
        point2.position.set(-400, -200, -300); //點(diǎn)光源位置
        scene.add(point2); //點(diǎn)光源添加到場景中

鼠標(biāo)操作旋轉(zhuǎn),縮放

         //鼠標(biāo)操作旋轉(zhuǎn)、縮放,OrbitControls需要單獨(dú)引入
        new OrbitControls(camera, renderer.domElement);

球體,立方體自動旋轉(zhuǎn)

         var animate = function () {
          requestAnimationFrame(animate);

          cube.rotation.x += 0.01;
          cube.rotation.y += 0.01;

          sphere.rotation.x += 0.01;
          sphere.rotation.y += 0.01;
          renderer.render(scene, camera);
        };

        animate();

initThree完整代碼

function initThree(instance: HTMLElement | null) {
        var hetght = instance.clientHeight - 25;
        var width = instance.clientWidth - 25;

        // 創(chuàng)建場景對象Scene
        var scene = new THREE.Scene();

        // 創(chuàng)建相機(jī)對象
        var camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);

        // 創(chuàng)建渲染器對象
        var renderer = new THREE.WebGLRenderer();
        renderer.setSize(width, hetght);

        instance.append(renderer.domElement);
        renderer.render(scene, camera);
        camera.position.z = 5;
        renderer.setClearColor(0xeeeeee, 1.0);

        // 立方體網(wǎng)格模型
        var cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
        //材質(zhì)對象Material
        var cubeMaterial = new THREE.MeshLambertMaterial({
          color: 0xff0000,
          opacity: 0.7,
          transparent: true,
        });
        var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
        scene.add(cube);

        // 球體網(wǎng)格模型
        var sphereGeometry = new THREE.SphereGeometry(1, 20, 20);
        var sphereMaterial = new THREE.MeshLambertMaterial({
          color: 0xff00ff,
          specular: 0x4488ee,
          shininess: 12,
        });
        var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); //網(wǎng)格模型對象Mesh
        sphere.translateY(120); //球體網(wǎng)格模型沿Y軸正方向平移120
        sphere.position.set(0, 0, 5);
        scene.add(sphere);

        // 圓柱網(wǎng)格模型
        var cylinderGeometry = new THREE.CylinderGeometry(1, 1, 5, 32);
        var cylinderMaterial = new THREE.MeshLambertMaterial({
          color: 0xffff00,
        });

        var cylinder = new THREE.Mesh(cylinderGeometry, cylinderMaterial); //網(wǎng)格模型對象Mesh
        cylinder.position.set(10, 0, 0); //設(shè)置cylinder模型對象的xyz坐標(biāo)為10,0,0
        scene.add(cylinder); 

        // 輔助坐標(biāo)系  參數(shù)400表示坐標(biāo)系大小,可以根據(jù)場景大小去設(shè)置
        var axisHelper = new THREE.AxisHelper(20);
        scene.add(axisHelper);

        //點(diǎn)光源
        var point = new THREE.PointLight(0xffffff);
        point.position.set(0, 0, 0);
        scene.add(point); //點(diǎn)光源添加到場景中

        // 點(diǎn)光源2  位置和point關(guān)于原點(diǎn)對稱
        var point2 = new THREE.PointLight(0xffffff);
        point2.position.set(-400, -200, -300); //點(diǎn)光源位置
        scene.add(point2); //點(diǎn)光源添加到場景中

        //鼠標(biāo)操作旋轉(zhuǎn)、縮放
        new OrbitControls(camera, renderer.domElement);
        var animate = function () {
          requestAnimationFrame(animate);

          cube.rotation.x += 0.01;
          cube.rotation.y += 0.01;

          sphere.rotation.x += 0.01;
          sphere.rotation.y += 0.01;
          renderer.render(scene, camera);
        };

        animate();
      }

總結(jié)

到此這篇關(guān)于vue3中如何用threejs畫一些簡單幾何體的文章就介紹到這了,更多相關(guān)vue3用threejs畫幾何體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue的基本用法與常見指令

    vue的基本用法與常見指令

    Vue.js是JavaScript MVVM(Model-View-ViewModel)庫,十分簡潔,Vue核心只關(guān)注視圖層,相對AngularJS提供更加簡潔、易于理解的API。接下來通過本文給大家介紹vue的基本用法與常見指令,感興趣的朋友一起看看吧
    2017-08-08
  • 解決vue單頁面應(yīng)用打包后相對路徑、絕對路徑相關(guān)問題

    解決vue單頁面應(yīng)用打包后相對路徑、絕對路徑相關(guān)問題

    這篇文章主要介紹了解決vue單頁面應(yīng)用打包后相對路徑、絕對路徑相關(guān)問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 在vue-cli3中使用axios獲取本地json操作

    在vue-cli3中使用axios獲取本地json操作

    這篇文章主要介紹了在vue-cli3中使用axios獲取本地json操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue項(xiàng)目展示pdf文件的方法實(shí)現(xiàn)

    vue項(xiàng)目展示pdf文件的方法實(shí)現(xiàn)

    本文主要介紹了vue項(xiàng)目展示pdf文件的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • mpvue構(gòu)建小程序的方法(步驟+地址)

    mpvue構(gòu)建小程序的方法(步驟+地址)

    mpvue是一個(gè)使用Vue.js開發(fā)小程序的前端框架。框架基于 Vue.js 核心,這篇文章主要介紹了mpvue構(gòu)建小程序的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • vue3導(dǎo)入excel并解析excel數(shù)據(jù)渲染到表格中(純前端實(shí)現(xiàn))

    vue3導(dǎo)入excel并解析excel數(shù)據(jù)渲染到表格中(純前端實(shí)現(xiàn))

    在Vue中實(shí)現(xiàn)導(dǎo)出Excel有多種方式,可以通過前端實(shí)現(xiàn),也可以通過前后端配合實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于vue3導(dǎo)入excel并解析excel數(shù)據(jù)渲染到表格中的相關(guān)資料,文中介紹的方法是純前端實(shí)現(xiàn),需要的朋友可以參考下
    2024-04-04
  • element中form組件prop嵌套屬性的問題解決

    element中form組件prop嵌套屬性的問題解決

    本文主要介紹了element中form組件prop嵌套屬性的問題解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Vue?Router修改query參數(shù)url參數(shù)沒有變化問題及解決

    Vue?Router修改query參數(shù)url參數(shù)沒有變化問題及解決

    這篇文章主要介紹了Vue?Router修改query參數(shù)url參數(shù)沒有變化問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue3父子傳值實(shí)現(xiàn)彈框功能的示例詳解

    vue3父子傳值實(shí)現(xiàn)彈框功能的示例詳解

    這篇文章主要為大家詳細(xì)介紹了vue3如何利用父子傳值實(shí)現(xiàn)彈框功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • 簡單實(shí)現(xiàn)Vue的observer和watcher

    簡單實(shí)現(xiàn)Vue的observer和watcher

    這篇文章主要教大家如何簡單實(shí)現(xiàn)Vue的observer和watcher,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12

最新評論