<!DOCTYPE html>
<html lang="en">
<head>
<title>3d cylinder</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no,minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<script src="three.min.js" type="text/javascript"></script>
<script src="Stats.js" type="text/javascript"></script>
<script>
var container, stats;
var camera, scene, renderer;
var cylinder;
var targetRotation = 0;
var targetRotationOnMouseDown = 0;
var mouseX = 0;
var mouseXOnMouseDown = 0;
var windowHalfX = window.innerWidth/2;
var windowHalfY = window.innerHeight/2;
init();
function init() {
container = document.createElement('div');
document.body.appendChild(container);
var info = document.createElement('div');
info.style.position = 'absolute';
info.style.top = '10px';
info.style.width = '100%';
info.style.textAlign = 'center';
container.appendChild(info);
camera = new THREE.PerspectiveCamera(70, window.innerWidth/window.innerHeight, 1, 1000);
camera.position.y = 150;
camera.position.z = 500;
scene = new THREE.Scene();
// cylinder
var materials = [];
for (var i = 0; i < 6; i ++) {
materials.push(new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff }));
}
cylinder = new THREE.Mesh(new THREE.CylinderGeometry(100, 100, 400, 50, 50, false), materials);
cylinder.overdraw = true;
scene.add(cylinder);
renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild(stats.domElement);
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
windowHalfX = window.innerWidth/2;
windowHalfY = window.innerHeight/2;
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
</script>
</body>
</html>
我想通過以下代碼繪製使用HTML5和three.js的3d柱面。我正在使用Canvas渲染並在Google Chrome瀏覽器上對其進行測試。在控制檯上,它正在發出「Three.CanvasRenderer 50」。可能是什麼原因,這個消息是什麼意思?HTML 5和three.js代碼不會在網頁上顯示任何東西
是否必須添加動畫在three.js所的場景? – chako
不,但我認爲你需要至少調用一次render,如:renderer.render(scene,camera); – sq2
我添加了'renderer.render(scene,camera);'最後在init()中,但它仍然不起作用。 – chako