美文网首页Three.js我爱编程
09three.js-dat.GUI和stats.js库的使用

09three.js-dat.GUI和stats.js库的使用

作者: 狂暴机甲 | 来源:发表于2018-04-13 17:02 被阅读17次
dat.gui效果
dat.gui.png
stats库.png
1、stats.js库的使用。下载js文件,引入文件。
在body中添加一个div标签用于stats。初始化和使用见下面代码,记得要不断更新。
<div id="Stats-output"></div>
var stats = initStats();
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;
    }
function run() {
        stats.update();      
        requestAnimationFrame(run);
        renderer.render(scene,camera);
    }

function run() {
        stats.update();
        var allChildren = scene.children;
        for(var i=5;i<allChildren.length;i++){
            allChildren[i].rotation.y += 0.04;
        }
        camera.position.y = controls.hAngle;
        camera.position.x = 40*Math.sin(controls.sAngle/180*Math.PI);
        camera.position.z = 40*Math.cos(controls.sAngle/180*Math.PI);
        camera.lookAt(scene.position);
        requestAnimationFrame(run);
        renderer.render(scene,camera);
    }
    run();

2、 dat.GUI库的使用。

   var controls = new function () {
        this.sAngle = 45;
        this.hAngle = 40;
        this.numberOfObjects = scene.children.length;
        this.addCube = function () {
            var cubeSize = Math.ceil((Math.random()*3));
            var cubeGe = new THREE.BoxGeometry(cubeSize,cubeSize,cubeSize);
            var cubeMe = new THREE.MeshLambertMaterial({color:0xFFFFFF*Math.random()});
            var cube = new THREE.Mesh(cubeGe,cubeMe);
            cube.castShadow = true;
            cube.name = "cube-" + scene.children.length;
            cube.position.x = -25+Math.round(Math.random()*50);
            cube.position.y = Math.round(Math.random()*10);
            cube.position.z = -25+Math.round(Math.random()*50);

            scene.add(cube);
            this.numberOfObjects = scene.children.length;
        }

        this.removeCube = function () {
            var allChildren = scene.children;
            var lastObject = allChildren[allChildren.length-1];
            if(lastObject instanceof THREE.Mesh){
                scene.remove(lastObject);
                this.numberOfObjects = scene.children.length;
            }
        }

        this.outputObjects = function () {
            console.log(scene.children);
        }

    }
    var gui = new dat.GUI();
    gui.add(controls,'sAngle',-360,360,"相机角度");
    gui.add(controls,'hAngle',0,90,"相机高度");
    gui.add(controls,'addCube','添加立方体');
    gui.add(controls,'removeCube','删除立方体');
    gui.add(controls, 'outputObjects','打印场景中的物体');
    gui.add(controls,'numberOfObjects','场景对象数量').listen(); //为什么第一次输出的数量比实际少一个呢?

    function run() {
        stats.update();
        var allChildren = scene.children;
        for(var i=5;i<allChildren.length;i++){
            allChildren[i].rotation.y += 0.04;
        }
        camera.position.y = controls.hAngle;
        camera.position.x = 40*Math.sin(controls.sAngle/180*Math.PI);
        camera.position.z = 40*Math.cos(controls.sAngle/180*Math.PI);
        camera.lookAt(scene.position);
        requestAnimationFrame(run);
        renderer.render(scene,camera);
    }
    run();
image.png

相关文章

  • 09three.js-dat.GUI和stats.js库的使用

    dat.gui效果 2、 dat.GUI库的使用。

  • 08dat.GUI和stats.js库的使用1

    stats库,网络下载js文件。body中添加一个div。 js中代码var stats = initStats...

  • 02-Three.js 辅助工具

    stats.js JavaScript性能监控器,同样也可以测试webgl的渲染性能,在动画运行时,该库可以在一个...

  • 通过dlopen使用动态库

    动态库制作dlopen 动态加载Frameworks使用dlopen和dlsym方法动态加载库和调用函数动态库使用...

  • threejs 开发使用插件

    一、stats性能插件 stats.js用于对JavaScript进行性能检测。 我们创建一个createStat...

  • cmake的使用和库的使用

    最近在看高翔博士的《SLAM十四讲》,这本书真是太棒啦,以前对于cmake的使用和C++库的理解一直不是很清楚,看...

  • Linux动态库

    深入探讨Linux静态库与动态库的详解(一看就懂)Linux下动态库的生成和使用Linux下动态库生成和使用转-u...

  • Python学习笔记(一)【原创】

    基础安装及使用 pip命令:用于管理Python共享代码和库: 使用外部代码和库的工具。 exit(): 退出函数...

  • Spring使用JDBCTemplate

    使用JDBCTemplate更新数据库 使用sql语句和参数更新数据库(update) 批量更新数据库(batch...

  • Go导出的库给VS使用

    说明:go在windows下可以导出静态库和动态库,静态库是.a文件是不能给vs使用的,也没法转成vs使用的静态库...

网友评论

    本文标题:09three.js-dat.GUI和stats.js库的使用

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