Rumah  >  Soal Jawab  >  teks badan

javascript - threejs scene中children的个数问题

<html>

<head>

<title>Example 02.01 - Basic Scene</title>
<script type="text/javascript" src="/javascripts/three.js"></script>
<script type="text/javascript" src="/javascripts/dat.gui.js"></script>
<style>
    body {
        /* set margin to 0 and overflow to hidden, to go fullscreen */
        margin: 0;
        overflow: hidden;
    }
</style>

</head>
<body>

<p id="Stats-output">
</p>

<p id="WebGL-output">
</p>


<script type="text/javascript">

// once everything is loaded, we run our Three.js stuff.
function init() {
    // 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);
    scene.add(camera);
    // create a render and set the size
    var renderer = new THREE.WebGLRenderer();
    renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMapEnabled = true;
    // create the ground plane
    var planeGeometry = new THREE.PlaneGeometry(60, 20, 1, 1);
    var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
    var plane = new THREE.Mesh(planeGeometry, planeMaterial);
    plane.receiveShadow = true;
    // rotate and position the plane
    plane.rotation.x = -0.5 * Math.PI;
    plane.position.x = 15;
    plane.position.y = 0;
    plane.position.z = 0;
    // add the plane to the scene
    scene.add(plane);
    // position and point the camera to the center of the scene
    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);
    // add spotlight for the shadows
    var spotLight = new THREE.SpotLight(0xffffff);
    spotLight.position.set(-40, 60, -10);
    spotLight.castShadow = true;
    scene.add(spotLight);
    // add the output of the renderer to the html element
    document.getElementById("WebGL-output").appendChild(renderer.domElement);
    // call the render function
    ***console.log(scene.children.length);
    console.log(scene.children);***
    render();
    function render() {
        // render using requestAnimationFrame
        requestAnimationFrame(render);
        renderer.render(scene, camera);
    }
}
window.onload = init

</script>
</body>
</html>
代码如上所示,主要问题是有两个:
1:我在scene中add了四个元素,camera,plane,ambient,spotlight。console.log(scene.children.lengh)显示的是4,这没有问题,但console.log(scene.children)的结果如下图所示

它的length显示的是5?这是什么情况
2:如console.log(scene.children)显示的,我只添加了一个camera,为什么有两个perspectiveCamera?

PHP中文网PHP中文网2749 hari yang lalu444

membalas semua(2)saya akan balas

  • 伊谢尔伦

    伊谢尔伦2017-04-10 15:42:33

    console.log(scene.children.lengh)显示的是4?
    我怎么不知道有lengh

    balas
    0
  • 高洛峰

    高洛峰2017-04-10 15:42:33

    我在没有dat.gui.js的情况下检测了一下,并没有你所说的问题。所以我觉得你的问题只会是three.js的版本问题或者是那个我不知道是什么的dat.gui.js出的问题。最后,camera是不用添加到scene中的,因为在render的时候会结合scene和camera。你可以下载最新版的three.js再试试

    balas
    0
  • Batalbalas