这个three.js考试的在线CDN和背景颜色控制?

时间:2018-01-11 12:58:10

标签: three.js vrml

这可能是What is the URL for three.js to include it online?的延续,我猜......

我在网上找到了这个例子:

要让它从一个文件(包含在下面)运行,所以它在线下载所有的JS库,我不得不改变这一部分:

<script type="text/javascript" src="../libs/three.js"></script>
<script type="text/javascript" src="../libs/VRMLLoader.js"></script>
<script type="text/javascript" src="../libs/stats.js"></script>
<script type="text/javascript" src="../libs/dat.gui.js"></script>
<script type="text/javascript" src="../libs/OrbitControls.js"></script>

进入这个:

<script type="text/javascript" src="https://threejs.org/build/three.js"></script>
<script type="text/javascript" src="https://threejs.org/examples/js/loaders/VRMLLoader.js"></script>
<script type="text/javascript" src="https://threejs.org/examples/js/libs/stats.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script type="text/javascript" src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>

问题1:VRMLLoader.js和类似的库是否有更合适的CDN,所以此示例从单个.html文件运行?我不想点three.js - 然后再次,如果我输入一个虚假的链接,如https://threejs.org/examples/js/libs/dat.gui.js,我会得到一个404&#34;阅读完整文档,了解有关使用GitHub页面的更多信息。 &#34;,也许它现在好吗?

无论如何,当我在Ubuntu 14.04上运行Firefox 57.0.4中的文件时,我得到了这个:

three.js

如您所见,背景为荧光绿,可能是0x00FF00的RGB;但是,代码说:

    webGLRenderer.setClearColor(new THREE.Color(0x000, 1.0));

...所以我应该在这里获得黑色背景,而不是?如果我在0xFFF中设置0x000而不是.setClearColor,那么我会得到一个黄色背景?!

问题2:在这个three.js示例中,如何指定背景颜色并正确渲染?

这是文件threejs-16-load-vrml.html - 只需将其保存在本地并在浏览​​器中打开:

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<!-- https://raw.githubusercontent.com/josdirksen/learning-threejs/master/chapter-08/16-load-vrml.html
 -->

<html>

<head>
    <title>Example 08.16 - Load vrml model </title>

<!--     <script type="text/javascript" src="../libs/three.js"></script>
    <script type="text/javascript" src="../libs/VRMLLoader.js"></script>
    <script type="text/javascript" src="../libs/stats.js"></script>
    <script type="text/javascript" src="../libs/dat.gui.js"></script>
    <script type="text/javascript" src="../libs/OrbitControls.js"></script>
 -->
<!--     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.js"></script>
 -->
    <script type="text/javascript" src="https://threejs.org/build/three.js"></script>
    <script type="text/javascript" src="https://threejs.org/examples/js/loaders/VRMLLoader.js"></script>
    <script type="text/javascript" src="https://threejs.org/examples/js/libs/stats.min.js"></script>
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
    <script type="text/javascript" src="https://threejs.org/examples/js/controls/OrbitControls.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="WebGL-output">
</div>

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

    // once everything is loaded, we run our Three.js stuff.
    function init() {

        var stats = initStats();


        // create a scene, that will hold all our elements such as objects, cameras and lights.
        var scene = new THREE.Scene();

        // create a camera, which defines where we're looking at.
        var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);

        // create a render and set the size
        var webGLRenderer = new THREE.WebGLRenderer();
        webGLRenderer.setClearColor(new THREE.Color(0xFFF, 1.0));
        webGLRenderer.setSize(window.innerWidth, window.innerHeight);
        webGLRenderer.shadowMapEnabled = true;

        // position and point the camera to the center of the scene
        camera.position.x = 30;
        camera.position.y = 30;
        camera.position.z = 30;
        camera.lookAt(new THREE.Vector3(0, 0, 0));

        var orbit = new THREE.OrbitControls(camera);

        var dir1 = new THREE.DirectionalLight(0.4);
        dir1.position.set(-30, 30, -30);
        scene.add(dir1);

        var dir2 = new THREE.DirectionalLight(0.4);
        dir2.position.set(-30, 30, 30);
        scene.add(dir2);

        var dir3 = new THREE.DirectionalLight(0.4);
        dir3.position.set(30, 30, -30);
        scene.add(dir3);

        // add spotlight for the shadows
        var spotLight = new THREE.SpotLight(0xffffff);
        spotLight.position.set(30, 30, 30);
        scene.add(spotLight);

        // add the output of the renderer to the html element
        document.getElementById("WebGL-output").appendChild(webGLRenderer.domElement);

        // call the render function
        var step = 0;


        // setup the control gui
        var controls = new function () {
            // we need the first child, since it's a multimaterial


        };

        var group;
        var gui = new dat.GUI();


        var loader = new THREE.VRMLLoader();
        var group = new THREE.Object3D();
//~         loader.load("../assets/models/vrml/tree.wrl", function (model) {
        loader.load("https://raw.githubusercontent.com/josdirksen/learning-threejs/master/assets/models/vrml/tree.wrl", function (model) {

            console.log(model);

            model.traverse(function (child) {
                if (child instanceof THREE.Mesh) {
//                    child.material = new THREE.MeshLambertMaterial({color:0xaaaaaa});
                    console.log(child.geometry);
                }
            });

            model.scale.set(10, 10, 10);

            scene.add(model);

        });


        render();


        function render() {
            stats.update();

            orbit.update();

            if (group) {
                group.rotation.y += 0.006;
                // group.rotation.x+=0.006;
            }

            // render using requestAnimationFrame
            requestAnimationFrame(render);
            webGLRenderer.render(scene, camera);
        }

        function initStats() {

            var stats = new Stats();
            stats.setMode(0); // 0: fps, 1: ms

            // Align top-left
            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>

1 个答案:

答案 0 :(得分:0)

  1. dat.gui.js - &gt; dat.gui.min.js
  2. 仔细阅读THREE.Color() here
  3. var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
    camera.position.set(0, 0, 10);
    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setClearColor('#fff');
    document.body.appendChild(renderer.domElement);
    
    var sphere = new THREE.Mesh(new THREE.SphereGeometry(), new THREE.MeshBasicMaterial({color: "blue", wireframe: true}));
    scene.add(sphere);
    
    var gui = new dat.GUI();
    gui.add(sphere.scale, "x", .5, 2);
    
    render();
    function render(){
      requestAnimationFrame(render);
      renderer.render(scene, camera);
    }
    body{
      overflow: hidden;
      margin: 0;
    }
    <script src="https://threejs.org/build/three.min.js"></script>
    <script src="https://threejs.org/examples/js/libs/dat.gui.min.js"></script>