简体   繁体   English

Three.js不会在画布上渲染

[英]Three.js wont render on canvas

I loaded the script in index.html but when I open the page it shows the rendered but nothing inside, nothing renderer, I can't find the problem 我将脚本加载到index.html中,但是当我打开页面时,它显示了已渲染的内容,但内部没有任何内容,也没有渲染器,我找不到问题

Here is the code of external .js: 这是外部.js的代码:

var wdt     = $('.design').width();
var hgh     = $('.design').height();

var scene       = new THREE.Scene();
var camera      = new THREE.Camera(75, wdt/hgh, 0.1, 1000);

var renderer    = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(wdt, hgh);
document.getElementById('designer').appendChild(renderer.domElement);

var geometry = new THREE.CubeGeometry(1,1,1);
var material = new THREE.MeshBasicMaterial({color: 0x00ff00});
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);

camera.position.z = 5;

var render = function () {
requestAnimationFrame(render);

cube.rotation.x += 0.1;
cube.rotation.y += 0.1;

renderer.render(scene, camera);
};

render();

Can anybody help please? 有人可以帮忙吗?

Camera issues 相机问题

Here's a jsfiddle of a working version. 这是工作版本的jsfiddle

Camera in Three.js is abstract, so I picked the common one to construct: Three.js中的Camera是抽象的,因此我选择了常见的一个来构造:

var camera = new THREE.PerspectiveCamera (75, wdt / hgh, 0.1, 1000);

The camera's viewpoint doesn't line up by default, so explicit calls eliminate confusion: 摄像头的视点默认情况下不会对齐,因此显式调用可消除混乱:

camera.lookAt(cube.position);

And adding the camera to the scene: 并将摄像机添加到场景中:

scene.add(camera);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM