美文网首页
three.js基础结构

three.js基础结构

作者: adtk | 来源:发表于2019-04-29 17:16 被阅读0次

https://juejin.im/post/5b0ace63f265da0db479270a#heading-7


class ThreeDWorld {
    m_cube1 = {};
    m_cube2 = {};
    m_cube3 = {};

    constructor(canvasContainer) {
        // canvas容器
        this.container = canvasContainer || document.body;
        // 创建场景
        this.createScene();
        // 创建灯光
        this.createLights();
        // 性能监控插件
        // this.initStats();
        // 物体添加
        this.addObjs();
        // 轨道控制插件(鼠标拖拽视角、缩放等)
        // this.orbitControls = new THREE.OrbitControls(this.camera);
        // this.orbitControls.autoRotate = true;
        // 循环更新渲染场景
        this.update();
    }
    createScene() {
        this.HEIGHT = window.innerHeight;
        this.WIDTH = window.innerWidth;
        // 创建场景
        this.scene = new THREE.Scene();
        // 在场景中添加雾的效果,参数分别代表‘雾的颜色’、‘开始雾化的视线距离’、刚好雾化至看不见的视线距离’
        this.scene.fog = new THREE.Fog(0x090918, 1, 600);
        // 创建相机
        let aspectRatio = this.WIDTH / this.HEIGHT;
        let fieldOfView = 90;
        let nearPlane = 1;
        let farPlane = 10000;
        /**
         * PerspectiveCamera 透视相机
         * @param fieldOfView 视角
         * @param aspectRatio 纵横比
         * @param nearPlane 近平面
         * @param farPlane 远平面
         */
        this.camera = new THREE.PerspectiveCamera(
            fieldOfView,
            aspectRatio,
            nearPlane,
            farPlane
        );

        // 设置相机的位置
        this.camera.position.x = 0;
        this.camera.position.z = 150;
        this.camera.position.y = 0;
        // 创建渲染器
        this.renderer = new THREE.WebGLRenderer({
            // 在 css 中设置背景色透明显示渐变色
            alpha: true,
            // 开启抗锯齿
            antialias: true
        });
        // 渲染背景颜色同雾化的颜色
        this.renderer.setClearColor(this.scene.fog.color);
        // 定义渲染器的尺寸;在这里它会填满整个屏幕
        this.renderer.setSize(this.WIDTH, this.HEIGHT);

        // 打开渲染器的阴影地图
        this.renderer.shadowMap.enabled = true;
        // this.renderer.shadowMapSoft = true;
        this.renderer.shadowMap.type = THREE.PCFSoftShadowMap
        // 在 HTML 创建的容器中添加渲染器的 DOM 元素
        this.container.appendChild(this.renderer.domElement);
        // 监听屏幕,缩放屏幕更新相机和渲染器的尺寸
        // window.addEventListener('resize', this.handleWindowResize.bind(this), false);
    }
    createLights() {
        // 户外光源
        // 第一个参数是天空的颜色,第二个参数是地上的颜色,第三个参数是光源的强度
        this.hemisphereLight = new THREE.HemisphereLight(0xaaaaaa, 0x000000, .9);

        // 环境光源
        this.ambientLight = new THREE.AmbientLight(0xdc8874, .2);

        // 方向光是从一个特定的方向的照射
        // 类似太阳,即所有光源是平行的
        // 第一个参数是关系颜色,第二个参数是光源强度
        this.shadowLight = new THREE.DirectionalLight(0xffffff, .9);

        // 设置光源的位置方向
        this.shadowLight.position.set(50, 50, 50);

        // 开启光源投影
        this.shadowLight.castShadow = true;

        // 定义可见域的投射阴影
        this.shadowLight.shadow.camera.left = -400;
        this.shadowLight.shadow.camera.right = 400;
        this.shadowLight.shadow.camera.top = 400;
        this.shadowLight.shadow.camera.bottom = -400;
        this.shadowLight.shadow.camera.near = 1;
        this.shadowLight.shadow.camera.far = 1000;

        // 定义阴影的分辨率;虽然分辨率越高越好,但是需要付出更加昂贵的代价维持高性能的表现。
        this.shadowLight.shadow.mapSize.width = 2048;
        this.shadowLight.shadow.mapSize.height = 2048;

        // 为了使这些光源呈现效果,需要将它们添加到场景中
        this.scene.add(this.hemisphereLight);
        this.scene.add(this.shadowLight);
        this.scene.add(this.ambientLight);
    }

    addObjs() {
        // 使用基础网孔材料
        // let mat = new THREE.MeshBasicMaterial({
        //  color: 0xff0000,
        //  // 绘制为线框
        //  wireframe: true
        // });
        // 创建立方体几何模型
        // let cube1 = new THREE.BoxGeometry(10, 10, 10, 4, 4, 4);
        // // 混合模型与材质
        // this.m_cube1 = new THREE.Mesh(cube1, mat);
        // let cube2 = new THREE.BoxGeometry(20, 20, 20, 2, 2, 2);
        // this.m_cube2 = new THREE.Mesh(cube2, mat);
        // let cube3 = new THREE.BoxGeometry(10, 10, 10, 3, 3, 3);
        // this.m_cube3 = new THREE.Mesh(cube3, mat);

        let mat = new THREE.MeshBasicMaterial({
            color: 0xffff00,
            wireframe: true
        });
        let cube1 = new THREE.SphereGeometry(15, 16, 16);
        // 混合模型与材质
        this.m_cube1 = new THREE.Mesh(cube1, mat);
        let cube2 = new THREE.SphereGeometry(15, 16, 16);
        this.m_cube2 = new THREE.Mesh(cube2, mat);
        let cube3 = new THREE.SphereGeometry(25, 16, 16);
        this.m_cube3 = new THREE.Mesh(cube3, mat);


        this.m_cube1.position.x = -50;
        this.m_cube2.position.x = 0;
        this.m_cube3.position.x = 50;
        this.scene.add(this.m_cube1);
        this.scene.add(this.m_cube2);
        this.scene.add(this.m_cube3);
    }
    update() {
        // 动画插件
        // TWEEN.update();
        // // 性能监测插件
        // this.stats.update();
        // 渲染器执行渲染
        this.m_cube1.rotation.y += 0.01;
        this.m_cube2.rotation.y += 0.01;
        this.m_cube3.rotation.y += 0.01;
        this.scene.rotation.y += 0.005;
        this.renderer.render(this.scene, this.camera);
        // 循环调用
        requestAnimationFrame(() => {
            this.update()
        });
    }
}

new ThreeDWorld();

相关文章

  • threeJs基础文档

    three.js基础结构 目录 一个three.js项目至少需要的东西有—— [ ] scene:场景 [ ] c...

  • three.js基础结构

    https://juejin.im/post/5b0ace63f265da0db479270a#heading-7

  • THREE.js_的使用(例子)

    THREE.js 下载 THREE.js 官方文档 THREE.js 中文基础教程 THREE.MeshLine ...

  • Threejs基础环境搭建(二)基本文件配置

    接下来,需要构建基础的three.js环境 一,创建index.html文件 如上,采用cdn的three.js库...

  • three.js基础

    1.三大组建 2.渲染器renderer 3.场景 4.渲染器 5.渲染循环 渲染有两种方式:实时渲染和离线渲染 ...

  • three.js基础

    在three.js中,有两类几何体,基本几何体和buffer(缓存)几何体。 基本几何体的顶点位置,缩放,旋转角,...

  • Three.js Shader源码结构

    想要改写Three.js中的shader,就必须先了解其源码结构,本文梳理一下其源码结构,下一篇实现改写shade...

  • Three.js

    Three.js 1. 概述 1.1 什么是Three.js Three.js是一个3D javascript库。...

  • Go语言基础语法--注释、基础结构2

    章节 GO语言基础语法--注释、基础结构(重要) 1.GO语言基础语法---注释、基础结构 基础结构注意事项 源文...

  • Three.js 入门基础

    Three.js 1.Three.js 介绍 OpenGL(英语:Open Graphics Library,译名...

网友评论

      本文标题:three.js基础结构

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