美文网首页
第一个场景

第一个场景

作者: Codifier | 来源:发表于2019-11-06 14:57 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My First Scene</title>
    <script src="../../three-part/threejs/three.js"></script>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
    init();
    function init(){
        // Scenes allow you to set up what and where is to be rendered by three.js.
        // This is where you place objects, lights and cameras.
        let scene = new THREE.Scene();
        /**
         * Camera that uses perspective projection
         * PerspectiveCamera( fov : Number, aspect : Number, near : Number, far : Number )
         * fov — Camera frustum vertical field of view.
         * aspect — Camera frustum aspect ratio.
         * near — Camera frustum near plane.
         * far — Camera frustum far plane.
         */
        let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);

        // The WebGL renderer displays your beautifully crafted scenes using WebGL.
        let render = new THREE.WebGLRenderer();
        // Sets the clear color and opacity.
        render.setClearColor(new THREE.Color(0x000000));
        render.setSize(window.innerWidth, window.innerHeight);
        /**
         * An axis object to visualize the 3 axes in a simple way.
         * The X axis is red. The Y axis is green. The Z axis is blue.
         * AxesHelper( size : Number )
         * size -- (optional) size of the lines representing the axes. Default is 1.
         */
        let axes = new THREE.AxesHelper(20);
        scene.add(axes);

        // create a plane
        /**
         * PlaneGeometry(width : Float, height : Float, widthSegments : Integer, heightSegments : Integer)
         * width — Width along the X axis. Default is 1.
         * height — Height along the Y axis. Default is 1.
         * widthSegments — Optional. Default is 1.
         * heightSegments — Optional. Default is 1.
         * @type {PlaneGeometry}
         */
        let planeGeometry = new THREE.PlaneGeometry(60, 20, 4, 4);
        // A material for drawing geometries in a simple shaded (flat or wireframe) way.
        let planeMaterial = new THREE.MeshBasicMaterial({
            color : 0xaaaaa
        });
        /**
         * Representing triangular polygon mesh based objects
         * Mesh( geometry : Geometry, material : Material )
         * geometry — (optional) an instance of Geometry or BufferGeometry. Default is a new BufferGeometry.
         * material — (optional) a single or an array of Material. Default is a new MeshBasicMaterial
         * @type {Raycaster.params.Mesh|Mesh}
         */
        let plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.rotation.x = -0.5 * Math.PI;
        plane.position.set(15, 0, 0);
        scene.add(plane);

        // create a cube
        /**
         * BoxGeometry is a geometry class for a rectangular cuboid with a given 'width', 'height', and 'depth'.
         * On creation, the cuboid is centred on the origin, with each edge parallel to one of the axes.
         * BoxGeometry(width : Float, height : Float, depth : Float, widthSegments : Integer, heightSegments : Integer, depthSegments : Integer)
         * width — Width; that is, the length of the edges parallel to the X axis. Optional; defaults to 1.
         * height — Height; that is, the length of the edges parallel to the Y axis. Optional; defaults to 1.
         * depth — Depth; that is, the length of the edges parallel to the Z axis. Optional; defaults to 1.
         * widthSegments — Number of segmented rectangular faces along the width of the sides. Optional; defaults to 1.
         * heightSegments — Number of segmented rectangular faces along the height of the sides. Optional; defaults to 1.
         * depthSegments — Number of segmented rectangular faces along the depth of the sides. Optional; defaults to 1.
         * @type {BoxGeometry}
         */
        let cubeGeometry = new THREE.BoxGeometry(6, 6, 6, 3, 3, 3);
        let cubeMatrial = new THREE.MeshBasicMaterial({
            color : 0xff0000,
            wireframe : true
        });
        let cube = new THREE.Mesh(cubeGeometry, cubeMatrial);
        cube.position.set(0, 3, 0);
        scene.add(cube);

        // create a sphere
        /**
         * A class for generating sphere geometries
         * SphereGeometry(radius : Float, widthSegments : Integer, heightSegments : Integer, phiStart : Float, phiLength : Float, thetaStart : Float, thetaLength : Float)
         * radius — sphere radius. Default is 1.
         * widthSegments — number of horizontal segments. Minimum value is 3, and the default is 8.
         * heightSegments — number of vertical segments. Minimum value is 2, and the default is 6.
         * phiStart — specify horizontal starting angle. Default is 0.
         * phiLength — specify horizontal sweep angle size. Default is Math.PI * 2.
         * thetaStart — specify vertical starting angle. Default is 0.
         * thetaLength — specify vertical sweep angle size. Default is Math.PI.
         * @type {SphereGeometry}
         */
        let sphereGeometry = new THREE.SphereGeometry(4, 20, 20, 0, Math.PI, 0, Math.PI / 2);
        let sphereMatrial = new THREE.MeshBasicMaterial({
            color : 0x7777ff,
            wireframe : true
        });
        let sphere = new THREE.Mesh(sphereGeometry, sphereMatrial);
        sphere.position.set(20, 4, 2);
        scene.add(sphere);

        camera.position.set(-30, 40, 30);
        camera.lookAt(scene.position);

        document.getElementById("container").appendChild(render.domElement);

        render.render(scene, camera);
    }
</script>
</body>
</html>

运行结果:


相关文章

  • cocos2dx-lua切换场景

    场景切换的方式 创建场景 runWithScene() 用于开始游戏,只用来加载第一个场景。 replaceSce...

  • 第一个场景

    运行结果:

  • kotlin 中let 和apply的区别

    场景一 场景一中由于DataBindingUtil.inflate第一个参数需要非空参数,所以不能直接使用let{...

  • 2018年1月14日观看电影心得

    【怒海潜将】观看后心得 7个场景: 第一个场景:乡下耕田。 第二个场景:海军舰艇。 第三个场景:海军潜水学院,新泽...

  • 2018-01-22

    第一个场景:所有的事情都陷入困境 第一个链接:人处在困境时总会有希望 第二个场景:秘密就是吸引力 第三个链接:只要...

  • Unity 2019.4 脚本生命周期

    脚本生命周期流程图 加载第一个场景 场景开始时将调用以下函数(为场景中的每个对象调用一次)。 Awake:始终在任...

  • Eevee框架8——场景切换

    和对象池一样,本人场景切换用到的非常少(基本单场景搞定,因为个人感觉Unity自己加载第一个场景的速度,要远快于动...

  • Game1硬币收集 - 第1部分 玩家场景 - Godot引擎游

    第1部分 - 玩家场景 您将要制作的第一个场景定义了Player对象。创建单独的玩家场景的好处之一是,您可以独立测...

  • RxSwift实现对textField内容监听改变按钮状态

    在iOS开发中,经常遇到按钮的响应状态跟随输入框内容的变化而变化,以下面两个场景为例子: 第一个场景:单输入框场景...

  • 前任三观后感

    有几个触动我的场景: 第一个场景是余飞和丁点分手归还东西的场景!两人在一起那么久,东西可以还,回忆还不了。真是说...

网友评论

      本文标题:第一个场景

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