我在此html / css代码中使用了示例代码,并使用了旨在使用three.js创建3D立方体的脚本。
当我打开HTML文件时,我得到了一个镀铬的空白页,上面只有“画布{宽度:100%;高度:100%}”
这是我的代码:
<html>
<head>
<title>My first Three.js app</title> <style>canvas { width: 100%;
height: 100% }</style>
</head>
<body>
<script src="js/three.min.js"></script>
<script>
var scene = new THREE.Scene();
var camera = new
THREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight, 1,10000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,
window.innerHeight);document.body.appendChild(renderer.domElement);
var geometry = new THREE.BoxGeometry(700, 700, 700, 10, 10, 10);
var material = new THREE.MeshBasicMaterial({color: 0xfffff,
wireframe: true});
var cube = new THREE.Mesh(geometry, material); scene.add(cube);
function render() {
requestAnimationFrame(render);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
render();
</script>
</body>
</html>
答案 0 :(得分:1)
您的相机可能是在立方体内部启动的。
尝试添加:
camera.position.set(0,0,1000)
也许:
camera.lookAt(新的THREE.Vector3(0,0,0))
创建相机后。
另一种选择是只运行以下代码段,然后检查其线索:
var renderer = new THREE.WebGLRenderer();
var w = 300;
var h = 200;
renderer.setSize( w,h );
document.body.appendChild( renderer.domElement );
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(
45, // Field of view
w/h, // Aspect ratio
0.1, // Near
10000 // Far
);
camera.position.set( 15, 10, 15 );
camera.lookAt( scene.position );
controls = new THREE.OrbitControls(camera, renderer.domElement);
var light = new THREE.PointLight( 0xFFFF00 );
light.position.set( 20, 20, 20 );
scene.add( light );
var light1 = new THREE.AmbientLight( 0x808080 );
light1.position.set( 20, 20, 20 );
scene.add( light1 );
var light2 = new THREE.PointLight( 0x00FFFF );
light2.position.set( -20, 20, -20 );
scene.add( light2 );
var light3 = new THREE.PointLight( 0xFF00FF );
light3.position.set( -20, -20, -20 );
scene.add( light3 );
var sphereGeom = new THREE.SphereGeometry(5,16,16);
for(var i=0;i<sphereGeom.vertices.length;i++){
var v = sphereGeom.vertices[i];
if(v.y<0) v.y=0;
}
sphereGeom.verticesNeedUpdate = true;
sphereGeom.computeFaceNormals();
sphereGeom.computeVertexNormals();
var material = new THREE.MeshLambertMaterial( { color: 0x808080 } );
var mesh = new THREE.Mesh( sphereGeom, material );
scene.add( mesh );
renderer.setClearColor( 0xdddddd, 1);
(function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
})();
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/controls/OrbitControls.js"></script>
答案 1 :(得分:0)
您的摘录显示您在<style>
开头有错字:
<title>My first Three.js app</title> <style>canvas { width: 100%; height: 100% }</style>
应该是:
<title>My first Three.js app</title> <style>canvas { width: 100%; height: 100% }</style>
我怀疑这就是为什么您看到页面上显示的文本。
因此,画布可能会显示为0x0,因此您看不到多维数据集。
调整样式标签,看看它如何进行?
编辑:实际上,我认为body标签也需要设置为100%,否则文档将没有任何高度。也许尝试以下样式标签:
<style>
html, body { height: 100%; width: 100%; }
canvas { width: 100%; height: 100% }
</style>
答案 2 :(得分:0)
我发现自己做错了:我错误地调用了three.js文件。
是:
<script src="js/three.min.js"></script>
但应该是:
<script src="./three.js"></script>