美文网首页ThreeJS
【ThreeJS】01 - 简单入门案例(Vue版)

【ThreeJS】01 - 简单入门案例(Vue版)

作者: itlu | 来源:发表于2021-04-10 18:40 被阅读0次

1. 使用Vue-Cli创建一个Vue项目

  1. 创建一个vue项目,只需要选择一些简单的配置即可。
vue create three-demo 
  1. 安装ThreeJS运行时依赖:
// 安装为运行时依赖
npm i three --save
  1. 创建一个简单的组件 ,并配置路由开始第一个ThreeJS的demo
<template>
  <div class="home"></div>
</template>

<script>
    import * as THREE from 'three'
  // 这样引入一下灯光和材质 也可以使用 THREE.的方式
    import {HemisphereLight,SphereGeometry} from 'three'
    // 轨道控制器
    import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'

    export default {
        name: "index",
        data() {
            return {
                renderer: null,
                // camera 相机
                camera: null,
                // Scene场景
                scene: null,
                // 灯光
                light: null,
                // 形状 球形
                sphereGeometry: null,
                // 材质
                material: null,
                // 网格
                sphereMesh: null,
                // 控制器
                control: null
            }
        },
        methods: {
            /**
             * 初始化方法
             */
            init() {
                /*
        * 1. 渲染器 WebGLRenderer
        * 2. 相机
        * 3. 场景
        * 4. Mesh 简单理解为渲染对象
        *   4.1 Mesh 几何体 (形状等)
        *   4.2 材质
        * 5. 渲染器不断的刷新 requestFrame
        * 6. 控制相机的运动需要一个控制器
        * */
                // antialias:true抗锯齿
                this.renderer = new THREE.WebGLRenderer({antialias: true})
                // 设置渲染器的大小和页面大小一样大
                this.renderer.setSize(window.innerWidth, window.innerHeight)
                // 切换背景颜色
                this.renderer.setClearColor(0xffffff)
                // 将canvas装载在主体的模型上
                document.body.appendChild(this.renderer.domElement)

                // 相机
                this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, window.innerWidth / window.innerHeight, 1000)
                // 监听窗口大小
                window.addEventListener('resize', () => {
                    this.renderer.setSize(window.innerWidth, window.innerHeight)
                    // 相机的宽高比
                    this.camera.aspect = window.innerWidth / window.innerHeight
                    // 设置了Aspect 之后必须更新相机的投影矩阵
                    this.camera.updateProjectionMatrix()
                })
                // 设置相机的位置
                this.camera.position.set(0, 0, 50)

                // 创建一个场景
                this.scene = new THREE.Scene()

                // 灯光
                this.light = new HemisphereLight(0x00ff00, 0xff0000)
                // 将灯光添加到场景中
                this.scene.add(this.light)

                // 创建形状
                this.sphereGeometry = new SphereGeometry(5, 12, 12)
                // 创建材质
                this.material = new THREE.MeshPhongMaterial()
                // 根据形状和材质创建网格
                this.sphereMesh = new THREE.Mesh(this.sphereGeometry, this.material)
                this.scene.add(this.sphereMesh)

                // 渲染场景和相机
                this.renderer.render(this.scene, this.camera)
                // 控制器 第一个参数是相机第二个参数是渲染器
                this.control = new OrbitControls(this.camera, this.renderer.domElement)
                this.update()
            },

            /**
             * 定义一个刷新函数
             */
            update() {
                // 需要不断的进行渲染更新
                this.renderer.render(this.scene, this.camera)
                requestAnimationFrame(this.update)
            }
        },
        created() {
            this.init()
        }
    }
</script>
  1. 全局样式配置:
html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
  }

相关文章

  • 【ThreeJS】01 - 简单入门案例(Vue版)

    1. 使用Vue-Cli创建一个Vue项目 创建一个vue项目,只需要选择一些简单的配置即可。 安装ThreeJS...

  • VUE threejs birds 3DAnimation

    最近在学习前端开发,用的是入门超级简单的VUE。 因为对动画很感兴趣 所以仿写一个3d动画 原项目是threejs...

  • VUE系列教程目录

    VUE系列教程目录 vue入门之路篇系列教程: vue最简单的入门教程+实战+Vue2+VueRouter2+we...

  • VUE系列教程目录

    VUE系列教程目录 vue入门之路篇系列教程:vue最简单的入门教程+实战+Vue2+VueRouter2+web...

  • Vue.js状态管理工具Vuex快速上手

    Vue2简单入门 Vue.js再入门 Vue.js使用vue-router构建单页应用 Vue.js状态管理工具V...

  • Vue2简单入门

    Vue2简单入门 Vue.js再入门 Vue.js使用vue-router构建单页应用 Vue.js状态管理工具V...

  • Vue.js再入门

    Vue2简单入门 Vue.js再入门 Vue.js使用vue-router构建单页应用 Vue.js状态管理工具V...

  • Vue.js使用vue-router构建单页应用

    Vue2简单入门 Vue.js再入门 Vue.js使用vue-router构建单页应用 Vue.js状态管理工具V...

  • threejs入门

    引入 Three.js是基于原生WebGL封装运行的三维引擎,在所有WebGL引擎中,Three.js是国内文资料...

  • 【ThreeJs】06 - 纹理

    项目搭建小提示 创建vue项目 安装ThreeJs 导入ThreeJs到组件中可以使用全部到如之后使用 自定义名称...

网友评论

    本文标题:【ThreeJS】01 - 简单入门案例(Vue版)

    本文链接:https://www.haomeiwen.com/subject/waunkltx.html