threejs——GUI控制速度和弹跳速度
0 179

例子演示地址

效果图

Image

全部代码

<!DOCTYPE html>

<html>

<head>
    <title>GUI控制速度和弹跳速度</title>
    <script type="text/javascript" src="../libs/three.js"></script>
    <script type="text/javascript" src="../libs/stats.js"></script>
    <script type="text/javascript" src="../libs/dat.gui.js"></script>
    <style>
        body {
            /* set margin to 0 and overflow to hidden, to go fullscreen */
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>

<div id="Stats-output">
</div>
<!-- Div which will hold the Output -->
<div id="LapBin-output">
</div>

<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">

    // 加载所有内容后,我们将运行Three.js
    function init() {

        var stats = initStats();

         // 创建一个场景,该场景将包含所有元素,例如对象、摄影机和灯光。
         var scene = new THREE.Scene();

        // 创建一个摄像头,它定义了我们要看的范围。
        var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.2, 1000);

        // 创建渲染并设置大小
        var renderer = new THREE.WebGLRenderer();

        renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.shadowMapEnabled = true;

        // 创建平面
        var planeGeometry = new THREE.PlaneGeometry(60, 30);
        var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
        var plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.receiveShadow = true;

        // 旋转并定位平面
        plane.rotation.x = -0.5 * Math.PI;
        plane.position.x = 10;
        plane.position.y = 0;
        plane.position.z = -10;

        // 将平面添加到场景中
        scene.add(plane);

        // 创建一个立方体
        var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
        var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});
        var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
        cube.castShadow = true;

        // 定位立方体
        cube.position.x = -4;
        cube.position.y = 3;
        cube.position.z = -10;

        // 将立方体添加到场景中
        scene.add(cube);

        // 创建一个球体
        var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
        var sphereMaterial = new THREE.MeshLambertMaterial({color: 0xdd77ff});
        var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);

        // 定位球体
        sphere.position.x = 20;
        sphere.position.y = 4;
        sphere.position.z = -10;
        sphere.castShadow = true;

        // 将球体添加到场景中
        scene.add(sphere);

        // 将摄影机定位并指向场景的中心
        camera.position.x = -30;
        camera.position.y = 40;
        camera.position.z = 30;
        camera.lookAt(scene.position);

        // add subtle ambient lighting
        var ambientLight = new THREE.AmbientLight(0x0c0c0c);
        scene.add(ambientLight);

        // 为阴影添加聚光灯
        var spotLight = new THREE.SpotLight(0xffffff);
        spotLight.position.set(-40, 60, -10);
        spotLight.castShadow = true;
        scene.add(spotLight);

        // 将渲染器的输出添加到html元素
        document.getElementById("LapBin-output").appendChild(renderer.domElement);

        // 调用renderScene()方法
        var step = 0;

        var controls = new function () {
            this.rotationSpeed = 0.02;
            this.bouncingSpeed = 0.03;
        };

        var gui = new dat.GUI();
        gui.add(controls, 'rotationSpeed', 0, 0.5);
        gui.add(controls, 'bouncingSpeed', 0, 0.5);

        render();

        function render() {
            stats.update();
            // 围绕立方体的轴旋转立方体
            cube.rotation.x += controls.rotationSpeed;
            cube.rotation.y += controls.rotationSpeed;
            cube.rotation.z += controls.rotationSpeed;

            // 球体上下跳动
            step += controls.bouncingSpeed;
            sphere.position.x = 20 + ( 10 * (Math.cos(step)));
            sphere.position.y = 2 + ( 10 * Math.abs(Math.sin(step)));

            // 使用requestAnimationFrame渲染
            requestAnimationFrame(render);
            renderer.render(scene, camera);
        }

        function initStats() {

            var stats = new Stats();

            stats.setMode(0); // 0: fps, 1: ms

            // 位置调整到左上
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';

            document.getElementById("Stats-output").appendChild(stats.domElement);

            return stats;
        }
    }
    window.onload = init;

</script>
</body>
</html>
0 179
() 全部评论
所有回复 (0)

最新

  • 插件下载 124 0 1 发布

    使用方法:打开3dmax软件,把砖墙插件拖拽到 视图中。

    3dmax砖墙插件下载地址|点这里

  • 插件下载 176 0 1 发布

    适用版本:  3ds max 2016 - 2022

    使用方法:在3dmax桌面快捷图标,点击鼠标右键打开文件所在位置,找到scripts文件夹下的strapup文件夹,把开孔脚本拖拽到strapup文件夹内。

    3dmax开孔神器下载地址|点这里