0

实际上,我是新手webGl,特别是在学习http://stemkoski.github.com/Three.js/three.js的示例时,我正在尝试学习它。

现在我的问题是:我建立了一个小 3D 场景。一个立方体、一个球体、一个地板和一个点光源。现在我想要点光源来创建阴影。我把这个命令“castShadow = true;” 立方体,点光源和球体,然后“receiveShadow = true;” 到地板上。但那里没有影子。

希望有人可以帮助我。

其他问题,主要目标是建造一个有窗户和像太阳一样的动画光的房子。这意味着每个网格都必须投射阴影并接收阴影。那可能吗?

我的代码:

    <html>
    <head>
        <style>
            #container {
                background: #225;
                width: 400px;
                height: 300px;
            }
        </style>
    </head>
    <body>

        <div id="container">


        </div>

    </body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script src="js/Three.js"></script>

    <script type="text/javascript">
// set the scene size
var WIDTH = 400,
  HEIGHT = 400;

// set some camera attributes
var VIEW_ANGLE = 45,
  ASPECT = WIDTH / HEIGHT,
  NEAR = 0.1,
  FAR = 10000;


var $container = $('#container');

// create a WebGL renderer, camera
// and a scene
var renderer = new THREE.WebGLRenderer();
var camera =
  new THREE.PerspectiveCamera(
    VIEW_ANGLE,
    ASPECT,
    NEAR,
    FAR);

var scene = new THREE.Scene();

///////////////////////// Camera /////////////////////

scene.add(camera);
camera.position.set(180,-180,300);
camera.lookAt(scene.position);

///////////////////////// Floor /////////////////////
var floorGeometry = new THREE.PlaneGeometry(200, 200);
    var floorMaterial = new THREE.MeshLambertMaterial( { color: 0x008800 } );
    var floor = new THREE.Mesh(floorGeometry, floorMaterial);
    floor.position.set(0,0,-25);
    floor.rotation.set(0,0,0);
    floor.doubleSided = true;
    floor.receiveShadow = true;
    scene.add(floor);


///////////////////////// Cube /////////////////////
var cubeGeometry = new THREE.CubeGeometry( 50, 50, 50 );
    var cubeMaterial = new THREE.MeshLambertMaterial( { color: 0x000088 } );
    cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
    cube.position.set(0,0,0);
    cube.castShadow = true;
    scene.add(cube);


///////////////////////// Sphere /////////////////////

var sphereGeometry = new THREE.SphereGeometry(50, 16, 16);
    var sphereMaterial = new THREE.MeshLambertMaterial( { color: 0xCC0000 });
    sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
    sphere.position.set(100,100,0);
    sphere.castShadow = true;
    scene.add(sphere);




///////////////////////// Point Light /////////////////////
var pointLight = new THREE.PointLight(0xFFFFFF);
    pointLight.position.set(50,50,130);
    pointLight.shadowCameraVisible = true;
    pointLight.shadowDarkness = 1;
    pointLight.intensity = 2;
    pointLight.castShadow = true;
    scene.add(pointLight);




renderer.setSize(WIDTH, HEIGHT);
$container.append(renderer.domElement);
renderer.render(scene, camera);

    </script>
</html>
4

1 回答 1

3

您是否在渲染器上启用了阴影?

renderer.shadowMapEnabled = true;
于 2013-02-20T16:28:16.320 回答