自学内容网 自学内容网

ThreeJs开发环境安装与首个DEMO

安装开发环境

我这边使用的JetBrain的WebStorm,咨询过很多其他开发从业者,普遍使用vscode的比较多。但是考虑到vscode涉及到不少插件安装和IDE配置,作为傻瓜式入门,我这边采用WebStorm。

下载地址:

WebStorm: The JavaScript and TypeScript IDE, by JetBrains

完成下载安装后,直接打开新建project

ThreeJs下载与部署

下载ThreeJs的整个包。

下载地址:点击download即可下载

Three.js – JavaScript 3D Library

下载的压缩包直接放在项目的同级目录下解压即可使用。

在./three.js-master/examples/ 下面有大量现成的样例,直接右键运行即可测试ThreeJs库是否可用。

首个DEMO

直接将teapot的example复制出来,就能作为demo了。需要注意导入包的相对路径是不一样的,需要修改。

创建第一个demo项目,firstScene.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>第一个基础场景</title>
    <script type="text/javascript" charset="utf-8"></script>
    <style type="text/css">
        body {
            margin: 0;
            overflow:hidden;
        }
    </style>
</head>
<body>
    <script type="importmap">
        {
            "imports": {
                "three": "./three.js-master/build/three.module.js",
                "three/addons/": "./three.js-master/examples/jsm/"
            }
        }
    </script>

    <div id="info">
        <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - the Utah Teapot<br />
        from <a href="https://www.udacity.com/course/interactive-3d-graphics--cs291" target="_blank" rel="noopener">Udacity Interactive 3D Graphics</a>
    </div>
    <script type="module">
        import * as THREE from 'three';

        import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
        import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';

        let camera, scene, renderer;
        let cameraControls;
        let effectController;
        const teapotSize = 300;
        let ambientLight, light;

        let tess = - 1;// force initialization
        let bBottom;
        let bLid;
        let bBody;
        let bFitLid;
        let bNonBlinn;
        let shading;

        let teapot, textureCube;
        const materials = {};

        init();
        render();

        function init() {

            const container = document.createElement( 'div' );
            document.body.appendChild( container );

            const canvasWidth = window.innerWidth;
            const canvasHeight = window.innerHeight;

            // CAMERA
            camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 80000 );
            camera.position.set( - 600, 550, 1300 );

            // LIGHTS
            ambientLight = new THREE.AmbientLight( 0x7c7c7c, 3.0 );

            light = new THREE.DirectionalLight( 0xFFFFFF, 3.0 );
            light.position.set( 0.32, 0.39, 0.7 );

            // RENDERER
            renderer = new THREE.WebGLRenderer( { antialias: true } );
            renderer.setPixelRatio( window.devicePixelRatio );
            renderer.setSize( canvasWidth, canvasHeight );
            container.appendChild( renderer.domElement );

            // EVENTS
            window.addEventListener( 'resize', onWindowResize );

            // CONTROLS
            cameraControls = new OrbitControls( camera, renderer.domElement );
            cameraControls.addEventListener( 'change', render );

            // TEXTURE MAP
            const textureMap = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
            textureMap.wrapS = textureMap.wrapT = THREE.RepeatWrapping;
            textureMap.anisotropy = 16;
            textureMap.colorSpace = THREE.SRGBColorSpace;

            // REFLECTION MAP
            const path = 'textures/cube/pisa/';
            const urls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];

            textureCube = new THREE.CubeTextureLoader().setPath( path ).load( urls );

            materials[ 'wireframe' ] = new THREE.MeshBasicMaterial( { wireframe: true } );
            materials[ 'flat' ] = new THREE.MeshPhongMaterial( { specular: 0x000000, flatShading: true, side: THREE.DoubleSide } );
            materials[ 'smooth' ] = new THREE.MeshLambertMaterial( { side: THREE.DoubleSide } );
            materials[ 'glossy' ] = new THREE.MeshPhongMaterial( { side: THREE.DoubleSide } );
            materials[ 'textured' ] = new THREE.MeshPhongMaterial( { map: textureMap, side: THREE.DoubleSide } );
            materials[ 'reflective' ] = new THREE.MeshPhongMaterial( { envMap: textureCube, side: THREE.DoubleSide } );

            // scene itself
            scene = new THREE.Scene();
            scene.background = new THREE.Color( 0xAAAAAA );

            scene.add( ambientLight );
            scene.add( light );

            // GUI
            setupGui();

        }

        // EVENT HANDLERS

        function onWindowResize() {

            const canvasWidth = window.innerWidth;
            const canvasHeight = window.innerHeight;

            renderer.setSize( canvasWidth, canvasHeight );

            camera.aspect = canvasWidth / canvasHeight;
            camera.updateProjectionMatrix();

            render();

        }

        function setupGui() {

            effectController = {
                newTess: 15,
                bottom: true,
                lid: true,
                body: true,
                fitLid: false,
                nonblinn: false,
                newShading: 'glossy'
            };

            const gui = new GUI();
            gui.add( effectController, 'newTess', [ 2, 3, 4, 5, 6, 8, 10, 15, 20, 30, 40, 50 ] ).name( 'Tessellation Level' ).onChange( render );
            gui.add( effectController, 'lid' ).name( 'display lid' ).onChange( render );
            gui.add( effectController, 'body' ).name( 'display body' ).onChange( render );
            gui.add( effectController, 'bottom' ).name( 'display bottom' ).onChange( render );
            gui.add( effectController, 'fitLid' ).name( 'snug lid' ).onChange( render );
            gui.add( effectController, 'nonblinn' ).name( 'original scale' ).onChange( render );
            gui.add( effectController, 'newShading', [ 'wireframe', 'flat', 'smooth', 'glossy', 'textured', 'reflective' ] ).name( 'Shading' ).onChange( render );

        }


        //

        function render() {

            if ( effectController.newTess !== tess ||
                effectController.bottom !== bBottom ||
                effectController.lid !== bLid ||
                effectController.body !== bBody ||
                effectController.fitLid !== bFitLid ||
                effectController.nonblinn !== bNonBlinn ||
                effectController.newShading !== shading ) {

                tess = effectController.newTess;
                bBottom = effectController.bottom;
                bLid = effectController.lid;
                bBody = effectController.body;
                bFitLid = effectController.fitLid;
                bNonBlinn = effectController.nonblinn;
                shading = effectController.newShading;

                createNewTeapot();

            }

            // skybox is rendered separately, so that it is always behind the teapot.
            if ( shading === 'reflective' ) {

                scene.background = textureCube;

            } else {

                scene.background = null;

            }

            renderer.render( scene, camera );

        }

        // Whenever the teapot changes, the scene is rebuilt from scratch (not much to it).
        function createNewTeapot() {

            if ( teapot !== undefined ) {

                teapot.geometry.dispose();
                scene.remove( teapot );

            }

            const geometry = new TeapotGeometry( teapotSize,
                tess,
                effectController.bottom,
                effectController.lid,
                effectController.body,
                effectController.fitLid,
                ! effectController.nonblinn );

            teapot = new THREE.Mesh( geometry, materials[ shading ] );

            scene.add( teapot );

        }
    </script>
</body>
</html>


原文地址:https://blog.csdn.net/u010595191/article/details/145121020

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!