Skip to content

Strange ShadowCamera position #25

@bartolomiew

Description

@bartolomiew

While trying to port this demo : http://stemkoski.github.io/Three.js/Shadow.html We can see that shadow camera position and orientation for spotlight looks wrong. Fog doesn't work as expected.

import java.util.Arrays;
import java.util.List;

import thothbot.parallax.core.client.AnimatedScene;
import thothbot.parallax.core.client.controls.OrbitControls;
import thothbot.parallax.core.client.gl2.enums.TextureWrapMode;
import thothbot.parallax.core.client.renderers.ShadowMap;
import thothbot.parallax.core.client.textures.Texture;
import thothbot.parallax.core.shared.cameras.PerspectiveCamera;
import thothbot.parallax.core.shared.core.Object3D;
import thothbot.parallax.core.shared.geometries.BoxGeometry;
import thothbot.parallax.core.shared.geometries.PlaneGeometry;
import thothbot.parallax.core.shared.geometries.SphereGeometry;
import thothbot.parallax.core.shared.lights.PointLight;
import thothbot.parallax.core.shared.lights.SpotLight;
import thothbot.parallax.core.shared.materials.Material;
import thothbot.parallax.core.shared.materials.Material.SIDE;
import thothbot.parallax.core.shared.materials.MeshBasicMaterial;
import thothbot.parallax.core.shared.materials.MeshLambertMaterial;
import thothbot.parallax.core.shared.math.Color;
import thothbot.parallax.core.shared.objects.Mesh;
import thothbot.parallax.core.shared.scenes.FogExp2;
import thothbot.parallax.core.shared.utils.SceneUtils;

public class Sample16Shadow extends AnimatedScene {

    // field of view
    private static final double VIEW_ANGLE = 45d;

    // near
    private static final double NEAR = 0.1d;

    // far
    private static final double FAR = 20000d;

    // camera
    private PerspectiveCamera camera;

    // control
    OrbitControls controls;

    private static final String texture_floor = "./static/textures/checkerboard.jpg";

    @Override
    protected void onStart() {

        // CAMERA
        camera = new PerspectiveCamera(VIEW_ANGLE, getRenderer()
                .getAbsoluteAspectRation(), NEAR, FAR);
        getScene().add(camera);
        camera.getPosition().set(0, 150, 400);
        camera.lookAt(getScene().getPosition());

        // CONTROLS
        controls = new OrbitControls(camera, getCanvas());

        // LIGHT
        PointLight light = new PointLight(0xffffff);
        light.getPosition().set(0,250,0);
        getScene().add(light);

        // SKYBOX/FOG
        BoxGeometry skyBoxGeometry = new BoxGeometry(10000, 10000, 10000);
        MeshBasicMaterial skyBoxMaterial = new MeshBasicMaterial();
        skyBoxMaterial.setColor(new Color(0x9999ff));
        skyBoxMaterial.setSide(SIDE.BACK);
        Mesh skyBox = new Mesh(skyBoxGeometry, skyBoxMaterial);
//      getScene().add(skyBox);                                 // <---     
        getScene().setFog(new FogExp2( 0x9999ff, 0.00025 ));    // <---

        ////////////
        // CUSTOM //
        ////////////

        // must enable shadows on the renderer 
        getRenderer().addPlugin(new ShadowMap(getRenderer(), getScene()));

        // "shadow cameras" show the light source and direction

        // spotlight #1 -- yellow, dark shadow
        SpotLight spotlight = new SpotLight(0xffff00);
        spotlight.getPosition().set(-60,150,-30);
        spotlight.setShadowCameraVisible(true);
        spotlight.setShadowDarkness(0.95);
        spotlight.setIntensity(2);
        // must enable shadow casting ability for the light
        spotlight.setCastShadow(true);
        getScene().add(spotlight);

        // spotlight #2 -- red, light shadow
        SpotLight spotlight2 = new SpotLight(0xff0000);
        spotlight2.getPosition().set(60,150,-60);
        getScene().add(spotlight2);
        spotlight2.setShadowCameraVisible(true);
        spotlight2.setShadowDarkness(0.70);
        spotlight2.setIntensity(2);
        spotlight2.setCastShadow(true);

        // spotlight #3
        SpotLight spotlight3 = new SpotLight(0x0000ff);
        spotlight3.getPosition().set(150,80,-100);
        spotlight3.setShadowCameraVisible(true);
        spotlight3.setShadowDarkness(0.95);
        spotlight3.setIntensity(2);
        spotlight3.setCastShadow(true);
        getScene().add(spotlight3);
        // change the direction this spotlight is facing
        Object3D lightTarget = new Object3D();
        lightTarget.getPosition().set(150,10,-100);
        getScene().add(lightTarget);
        spotlight3.setTarget(lightTarget);

        // cube: mesh to cast shadows
        BoxGeometry cubeGeometry = new BoxGeometry( 50, 50, 50 );
        MeshLambertMaterial cubeMaterial = new MeshLambertMaterial();
        cubeMaterial.setColor(new Color(0x888888));
        Mesh cube = new Mesh( cubeGeometry, cubeMaterial );
        cube.getPosition().set(0,50,0);
        // Note that the mesh is flagged to cast shadows
        cube.setCastShadow(true);
        getScene().add(cube);

        // floor: mesh to receive shadows
        Texture floorTexture = new Texture( texture_floor );
        floorTexture.setWrapS(TextureWrapMode.REPEAT);
        floorTexture.setWrapT(TextureWrapMode.REPEAT);
        floorTexture.getRepeat().set( 10, 10 );
        // Note the change to Lambert material.
        MeshLambertMaterial floorMaterial = new MeshLambertMaterial();
        floorMaterial.setMap(floorTexture);
        floorMaterial.setSide(SIDE.DOUBLE);
        PlaneGeometry floorGeometry = new PlaneGeometry(1000, 1000, 100, 100);
        Mesh floor = new Mesh(floorGeometry, floorMaterial);
        floor.getPosition().setY(-0.5);
        floor.getRotation().setX(Math.PI / 2);
        // Note the mesh is flagged to receive shadows
        floor.setReceiveShadow(true);
        getScene().add(floor);

        // create "light-ball" meshes
        SphereGeometry sphereGeometry = new SphereGeometry( 10, 16, 8 );

        MeshBasicMaterial darkMaterial = new MeshBasicMaterial();
        darkMaterial.setColor(new Color(0x000000));

        MeshBasicMaterial wireframeMaterial = new MeshBasicMaterial();
        wireframeMaterial.setColor(new Color(0xffff00));
        wireframeMaterial.setWireframe(true);
        wireframeMaterial.setTransparent(true);
        List<? extends Material> multiMaterial = Arrays.asList(darkMaterial, wireframeMaterial);
        Object3D shape = SceneUtils.createMultiMaterialObject(sphereGeometry.clone(), multiMaterial );
        shape.setPosition(spotlight.getPosition());
        getScene().add( shape );

        wireframeMaterial = new MeshBasicMaterial(); 
        wireframeMaterial.setColor(new Color(0xff0000));
        wireframeMaterial.setWireframe(true);
        wireframeMaterial.setTransparent(true);
        multiMaterial = Arrays.asList(darkMaterial, wireframeMaterial);
        shape = SceneUtils.createMultiMaterialObject(sphereGeometry.clone(), multiMaterial );
        shape.setPosition(spotlight2.getPosition());
        getScene().add( shape );

        wireframeMaterial = new MeshBasicMaterial(); 
        wireframeMaterial.setColor(new Color(0x0000ff));
        wireframeMaterial.setWireframe(true);
        wireframeMaterial.setTransparent(true);
        multiMaterial = Arrays.asList(darkMaterial, wireframeMaterial);
        shape = SceneUtils.createMultiMaterialObject(sphereGeometry.clone(), multiMaterial );
        shape.setPosition(spotlight3.getPosition());
        getScene().add( shape );
    }

    @Override
    protected void onUpdate(double duration) {
        // Called when the animation should be updated.
        controls.update();
        getRenderer().render(getScene(), camera);
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions