简体   繁体   English

Three.js 不显示任何东西

[英]Three.js not displaying anything

I have been doing some experiments with three.js.我一直在用three.js做一些实验。 However, when I try to do a simple test, I can't get it to display anything.但是,当我尝试做一个简单的测试时,我无法让它显示任何内容。 Here's my code.这是我的代码。 If someone could tell me what I'm missing it would be really helpful.如果有人能告诉我我错过了什么,那将非常有帮助。

//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);
})();

On the console log I am getting all the "rendered" messages but nothing is displaying.在控制台日志上,我收到了所有"rendered"消息,但没有显示任何内容。 I have checked that the canvas is the right size and it is, so I have no idea what is wrong.我已经检查过画布的大小是否正确,所以我不知道出了什么问题。 I have tried a few different geometries.我尝试了几种不同的几何形状。

EDIT: I am using Chrome, and online three.js examples work fine.编辑:我正在使用 Chrome,并且在线three.js 示例工作正常。 There are no errors on the console log.控制台日志上没有错误。

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

//Container is a div
document.body.appendChild(renderer.domElement);

你也可以尝试:

$("#container").append(renderer.viewElement);

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

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