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>
<div id="Stats-output">
</div>
<div id="WebGL-output">
</div>
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
console.log(scene.children.lengh)显示的是4?
我怎么不知道有lengh
我在没有dat.gui.js的情况下检测了一下,并没有你所说的问题。所以我觉得你的问题只会是three.js的版本问题或者是那个我不知道是什么的dat.gui.js出的问题。最后,camera是不用添加到scene中的,因为在render的时候会结合scene和camera。你可以下载最新版的three.js再试试