1

我一直在用three.js做一些实验。但是,当我尝试做一个简单的测试时,我无法让它显示任何东西。这是我的代码。如果有人能告诉我我错过了什么,那将非常有帮助。

//Constant declaration
var RENDER_DIST = 1000,
    FOV = 75;

var WIDTH = window.innerWidth,
    HEIGHT= window.innerHeight;

//Create the scene
var scene = new THREE.Scene();

//Create the camera
var camera = new THREE.PerspectiveCamera(FOV, WIDTH / HEIGHT, 0.1, RENDER_DIST);

//Pull the camera back a bit
camera.z = 100;
//Then add it to the scene
scene.add(camera);

//Create a renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(WIDTH,HEIGHT);
renderer.domElement.className = "glMain";

//Container is a div
$("#container").html(renderer.domElement);

//Boilerplate done! Celebrate!
(function init() {
    var geometry = new THREE.SphereGeometry(50); 
    var material = new THREE.MeshBasicMaterial({color: 0xff0000}); 
    var sphere = new THREE.Mesh(geometry, material); 
    scene.add(sphere);

    //debugging
    console.log("Sphere at " + sphere.position.x + " " + sphere.position.y + " " + sphere.position.z);


})();

//Our main function
(function loopRun() {
    requestAnimationFrame(loopRun);

    console.log("rendered");
    renderer.render(scene, camera);
})();

在控制台日志上,我收到了所有"rendered"消息,但没有显示任何内容。我已经检查过画布的大小是否正确,所以我不知道出了什么问题。我尝试了几种不同的几何形状。

编辑:我正在使用 Chrome,在线 three.js 示例工作正常。控制台日志上没有错误。

4

2 回答 2

7
//Pull the camera back a bit
camera.position.z = 100;

//Container is a div
document.body.appendChild(renderer.domElement);
于 2012-12-31T19:58:24.587 回答
0

你也可以试试:

$("#container").append(renderer.viewElement);
于 2020-05-03T17:26:14.567 回答