为什么我不能在three.js中通过亮点获得阴影?

时间:2017-02-27 13:04:42

标签: javascript three.js shadow

我想要阴影,我设置如下,真的想知道问题是什么?我有一个网格,立方体和球体作为它的孩子,我也为它们设置了castShadow和Receive阴影,但它没有阴影效果。 我的代码的某些部分:

var camera, scene, renderer, dice, dice1;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(35,window.innerWidth/window.innerHeight, 0.1, 1000);
// Z is up for objects intended to be 3D printed.
camera.up.set(0, 0, 1);
scene.add(camera);
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setClearColor(0x999999);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
var light = new THREE.PointLight(0x000000, 1, 1000);
light.position.set(10, 10,10);
light.castShadow = true; // default false
scene.add(light);
//Set up shadow properties for the light;
light.shadow.mapSize.width = 1024; // default
light.shadow.mapSize.height = 1024; // default
light.shadow.camera.near = 1; // default
light.shadow.camera.far = 1000 // default
var grid = new THREE.GridHelper(50, 50, 0xffffff, 0x555555);
grid.colorGrid = 0x00ff00;
grid.rotateOnAxis(new THREE.Vector3(1, 0, 0), 90 * (Math.PI / 180));
scene.add(grid);
objects.push(grid); // add to the array for DragControls
grid.receiveShadow=true;
//Create a sphere that cast shadows (but does not receive them)
var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.castShadow = true; //default is false
sphere.receiveShadow = false; //default
sphere.position.set(10, 15, 10);
scene.add(sphere);
//initializing the color cubic
var material = new THREE.MeshBasicMaterial({
color: 0xff0000});
dice = new THREE.Mesh(new THREE.BoxGeometry(5, 5, 5, 1, 1, 1), material);
dice.position.set(10, 2.5, 10);
dice.castShadow = true;
grid.add(dice);

1 个答案:

答案 0 :(得分:0)

您无法在网格上投射阴影。它只是线条。 在代码中添加它以查看阴影。

var plane = new THREE.Mesh(new THREE.PlaneGeometry(50,50), new THREE.MeshStandardMaterial( {color: 0x00ff00 }));
plane.castShadow = false;
plane.receiveShadow = true;
plane.position.set(0, 0, -1);
scene.add(plane);