如何在Three.js中啓用陰影?如何在Three.js中啓用陰影
不同的例子表明它應該像指定THREE.FlatShading一樣簡單,但是當我這樣做時,我的形狀根本沒有陰影。
我的JS:
CANVAS_WIDTH = 200,
CANVAS_HEIGHT = 200;
var camera, scene, renderer;
var mesh;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera(70, CANVAS_WIDTH/CANVAS_HEIGHT, 1, 1000);
camera.position.z = 400;
scene = new THREE.Scene();
var geometry = new THREE.BoxBufferGeometry(200, 200, 200);
var material = new THREE.MeshBasicMaterial({color: 0xff0000});
material.shading = THREE.FlatShading;
for(var i=0; i<material.length; i++){
material[i].shading = THREE.FlatShading;
}
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
// light
//scene.add(new THREE.AmbientLight(0x404040));
light = new THREE.DirectionalLight(0xffffff);
light.position.set(400, 400, 400);
scene.add(light);
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
container = document.getElementById('accelgyro_canvas');
container.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
console.log('animate')
requestAnimationFrame(animate);
mesh.rotation.x += 0.005;
mesh.rotation.y += 0.01;
renderer.render(scene, camera);
}
這使得由立方體爲:
我試圖調整光的角度,添加/移除的環境光,改變哪個對象予加FlatShading和似乎沒有任何幫助。
我該如何解決這個問題?
你是在陰影興趣(如在http://threejs.org/examples/#webgl_shadowmap_viewer),或在着色(http://threejs.org/例子/#canvas_materials_normal)?問題的標題與內容不匹配 –