PSSMShadow on terrain and setting shadowmode

My first attempt att playing with PSSM Shadows and terrain quad didn’t go so well. Either nothing happened or the entire scene turned black. I thought it might be the sky casting shadows so to eliminate that I hacked a test case based on TerrainTest (see below).

This sets the shadow mode on the terrain quad and it is acting really strange. I map the ‘F’ key to change shadow mode on the terrain. This is what happens



Switching (from off) to Cast: Nothing changes (looks like the unmodified TerrainTest).

Switching to Recieve: Everything gets darker, like I’ve turned down the lights.

Switching to CastAndRecieve: I get PSSM Shadows (looking really blocky and sort of flickering when moving the camera, but still, shadows).

Switching to Off: Like expected, back to ordinary TerrainTest

Switching to Cast: same as off

Switching to Recieve: Now there are shadows drawn, like they were left over from the C&R (same buffer maybe and nothing updates it). Sometimes when I get close to the ground they change.



Question: Is this expected that when receiving and nothing is there to cast shadows strange things happen? Did I goof something?



Running windowed mode (1280x720 24bpp) Mac OS X Lion 10.7.4, AMD Radeon HD 6770M 1024 MB





[java]

/*

  • Copyright © 2009-2010 jMonkeyEngine
  • All rights reserved.

    *
  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions are
  • met:

    *
    • Redistributions of source code must retain the above copyright
  • notice, this list of conditions and the following disclaimer.

    *
    • Redistributions in binary form must reproduce the above copyright
  • notice, this list of conditions and the following disclaimer in the
  • documentation and/or other materials provided with the distribution.

    *
    • Neither the name of ‘jMonkeyEngine’ nor the names of its contributors
  • may be used to endorse or promote products derived from this software
  • without specific prior written permission.

    *
  • THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  • "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  • TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  • PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  • CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  • EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  • PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  • PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  • LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  • NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  • SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    */

    package jme3test.terrain;



    import com.jme3.app.SimpleApplication;

    import com.jme3.font.BitmapText;

    import com.jme3.input.KeyInput;

    import com.jme3.input.controls.ActionListener;

    import com.jme3.input.controls.KeyTrigger;

    import com.jme3.light.AmbientLight;

    import com.jme3.light.DirectionalLight;

    import com.jme3.light.PointLight;

    import com.jme3.material.Material;

    import com.jme3.math.ColorRGBA;

    import com.jme3.math.Vector3f;

    import com.jme3.renderer.queue.RenderQueue.ShadowMode;

    import com.jme3.scene.Geometry;

    import com.jme3.shadow.PssmShadowRenderer;

    import com.jme3.terrain.geomipmap.TerrainLodControl;

    import com.jme3.terrain.geomipmap.TerrainQuad;

    import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;

    import com.jme3.terrain.heightmap.AbstractHeightMap;

    import com.jme3.terrain.heightmap.ImageBasedHeightMap;

    import com.jme3.texture.Texture;

    import com.jme3.texture.Texture.WrapMode;



    /**
  • Demonstrates how to use terrain.
  • The base terrain class it uses is TerrainQuad, which is a quad tree of actual
  • meshes called TerainPatches.
  • There are a couple options for the terrain in this test:
  • The first is wireframe mode. Here you can see the underlying trianglestrip structure.
  • You will notice some off lines; these are degenerate triangles and are part of the
  • trianglestrip. They are only noticeable in wireframe mode.
  • Second is Tri-Planar texture mode. Here the textures are rendered on all 3 axes and
  • then blended together to reduce distortion and stretching.
  • Third, which you have to modify the code to see, is Entropy LOD calculations.
  • In the constructor for the TerrainQuad, un-comment the final parameter that is
  • the LodPerspectiveCalculatorFactory. Then you will see the terrain flicker to start
  • while it calculates the entropies. Once it is done, it will pick the best LOD value
  • based on entropy. This method reduces "popping" of terrain greatly when LOD levels
  • change. It is highly suggested you use it in your app.

    *
  • @author bowens

    /

    public class TerrainTest extends SimpleApplication {



    private TerrainQuad terrain;

    Material matRock;

    Material matWire;

    boolean wireframe = false;

    boolean triPlanar = false;

    protected BitmapText hintText;

    PointLight pl;

    Geometry lightMdl;

    private float grassScale = 64;

    private float dirtScale = 16;

    private float rockScale = 128;



    public static void main(String[] args) {

    TerrainTest app = new TerrainTest();

    app.start();

    }



    @Override

    public void initialize() {

    super.initialize();



    loadHintText();

    }



    @Override

    public void simpleInitApp() {

    setupKeys();



    // First, we load up our textures and the heightmap texture for the terrain



    // TERRAIN TEXTURE material

    matRock = new Material(assetManager, "Common/MatDefs/Terrain/Terrain.j3md");

    matRock.setBoolean("useTriPlanarMapping", false);



    // ALPHA map (for splat textures)

    matRock.setTexture("Alpha", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));



    // HEIGHTMAP image (for the terrain heightmap)

    Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");



    // GRASS texture

    Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");

    grass.setWrap(WrapMode.Repeat);

    matRock.setTexture("Tex1", grass);

    matRock.setFloat("Tex1Scale", grassScale);



    // DIRT texture

    Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");

    dirt.setWrap(WrapMode.Repeat);

    matRock.setTexture("Tex2", dirt);

    matRock.setFloat("Tex2Scale", dirtScale);



    // ROCK texture

    Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");

    rock.setWrap(WrapMode.Repeat);

    matRock.setTexture("Tex3", rock);

    matRock.setFloat("Tex3Scale", rockScale);



    // WIREFRAME material

    matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    matWire.getAdditionalRenderState().setWireframe(true);

    matWire.setColor("Color", ColorRGBA.Green);



    // CREATE HEIGHTMAP

    AbstractHeightMap heightmap = null;

    try {

    //heightmap = new HillHeightMap(1025, 1000, 50, 100, (byte) 3);



    heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 1f);

    heightmap.load();



    } catch (Exception e) {

    e.printStackTrace();

    }



    /

  • Here we create the actual terrain. The tiles will be 65x65, and the total size of the
  • terrain will be 513x513. It uses the heightmap we created to generate the height values.

    */

    /**
  • Optimal terrain patch size is 65 (64x64).
  • The total size is up to you. At 1025 it ran fine for me (200+FPS), however at
  • size=2049, it got really slow. But that is a jump from 2 million to 8 million triangles…

    */

    terrain = new TerrainQuad(“terrain”, 65, 513, heightmap.getHeightMap());

    TerrainLodControl control = new TerrainLodControl(terrain, getCamera());

    control.setLodCalculator(new DistanceLodCalculator(65, 2.7f)); // patch size, and a multiplier

    terrain.addControl(control);

    terrain.setMaterial(matRock);

    terrain.setLocalTranslation(0, -100, 0);

    terrain.setLocalScale(2f, 1f, 2f);

    rootNode.attachChild(terrain);



    DirectionalLight light = new DirectionalLight();

    light.setDirection((new Vector3f(-0.5f, -1f, -0.5f)).normalize());

    light.setColor(new ColorRGBA(0.7f, 0.7f, 0.7f, 1.0f));

    rootNode.addLight(light);

    AmbientLight ambientLight = new AmbientLight();

    ambientLight.setColor(new ColorRGBA(0.1f, 0.1f, 0.1f, 1.0f));



    PssmShadowRenderer shadowRenderer = new PssmShadowRenderer(assetManager, 512, 2);

    shadowRenderer.setDirection(light.getDirection());

    viewPort.addProcessor(shadowRenderer);



    cam.setLocation(new Vector3f(0, 10, -10));

    cam.lookAtDirection(new Vector3f(0, -1.5f, -1).normalizeLocal(), Vector3f.UNIT_Y);

    }



    public void loadHintText() {

    hintText = new BitmapText(guiFont, false);

    hintText.setSize(guiFont.getCharSet().getRenderedSize());

    hintText.setLocalTranslation(0, getCamera().getHeight(), 0);

    hintText.setText(“Hit T to switch to wireframe, P to switch to tri-planar texturing, F to switch shadow mode”);

    guiNode.attachChild(hintText);

    }



    private void setupKeys() {

    flyCam.setMoveSpeed(50);

    inputManager.addMapping(“wireframe”, new KeyTrigger(KeyInput.KEY_T));

    inputManager.addListener(actionListener, “wireframe”);

    inputManager.addMapping(“triPlanar”, new KeyTrigger(KeyInput.KEY_P));

    inputManager.addListener(actionListener, “triPlanar”);

    inputManager.addMapping(“shadowMode”, new KeyTrigger(KeyInput.KEY_F));

    inputManager.addListener(actionListener, “shadowMode”);

    }

    private ActionListener actionListener = new ActionListener() {



    public void onAction(String name, boolean pressed, float tpf) {

    if (name.equals(“wireframe”) && !pressed) {

    wireframe = !wireframe;

    if (!wireframe) {

    terrain.setMaterial(matWire);

    } else {

    terrain.setMaterial(matRock);

    }

    } else if (name.equals(“triPlanar”) && !pressed) {

    triPlanar = !triPlanar;

    if (triPlanar) {

    matRock.setBoolean(“useTriPlanarMapping”, true);

    // planar textures don’t use the mesh’s texture coordinates but real world coordinates,

    // so we need to convert these texture coordinate scales into real world scales so it looks

    // the same when we switch to/from tr-planar mode

    matRock.setFloat(“Tex1Scale”, 1f / (float) (512f / grassScale));

    matRock.setFloat(“Tex2Scale”, 1f / (float) (512f / dirtScale));

    matRock.setFloat(“Tex3Scale”, 1f / (float) (512f / rockScale));

    } else {

    matRock.setBoolean(“useTriPlanarMapping”, false);

    matRock.setFloat(“Tex1Scale”, grassScale);

    matRock.setFloat(“Tex2Scale”, dirtScale);

    matRock.setFloat(“Tex3Scale”, rockScale);

    }

    } else if (name.equals(“shadowMode”) && !pressed) {

    ShadowMode shadowMode = terrain.getShadowMode();

    switch (shadowMode) {

    case Off :

    terrain.setShadowMode(ShadowMode.Cast);

    hintText.setText(“Cast”);

    break ;

    case Cast :

    terrain.setShadowMode(ShadowMode.Receive);

    hintText.setText(“Receive”);

    break ;

    case Receive :

    terrain.setShadowMode(ShadowMode.CastAndReceive);

    hintText.setText(“CastAndReceive”);

    break ;

    case CastAndReceive :

    terrain.setShadowMode(ShadowMode.Off);

    hintText.setText(“Off”);

    break ;

    }

    }

    }

    };

    }

    [/java]
2 Likes

Nightly or stable? I know theres some issues in nightly atm.

It’s the terrain shaders fault, hasn’t been updated to the new pssm shadow system. At least I think so.



EDIT: Didn’t see Normen’s post. Guess I’m wrong then.



EDIT2: Hmm still no pssm shadow techniques in the terrain mat tho, maybe there is more then one problem.

PSSM shadows used to work with terrain, I used them in the hovertank test.

Yeah, sorry, I forgot to mention I’m on nightly.

Okay, I guess this test case might be helpful anyway to get this resolved, thanks!


It's the terrain shaders fault, hasn't been updated to the new pssm shadow system. At least I think so.

my bad. you might be right
I'll look into it, thanks for the test case @jmaasing
1 Like

ok… so actually everything looks normal, and the terrain material is fine.



You have to understand how shadows casting and receiving objects are handled :

There are 2 shadow queues casting and receiving. objects are queued considering the shadowMode you set on them.



The shadows rendering process is done in 2 rendering pass :

  • pre shadow that generates the shadow maps and only rendering objects that are in the shadows CAST queue. (only depth is rendered to shadowmaps).
  • Post shadow that occur after the scene rendering and that re-render objects that are in the shadow RECEIVE queue with the shadow technique.



    There is only the terrain in your scene.

    To answer your questions :


Switching (from off) to Cast: Nothing changes (looks like the unmodified TerrainTest).

That's because there is nothing in the Receive queue so the post shadow pass is never rendered.

Switching to Recieve: Everything gets darker, like I’ve turned down the lights.

the post shadow is done on your terrain, but there is nothing in the Cast queue so the pre shadow is skipped and the post shadows is done with whatever there was in the graphic memory for the shadow maps.

Switching to CastAndRecieve: I get PSSM Shadows (looking really blocky and sort of flickering when moving the camera, but still, shadows).

Everything is normal here. The blockyness is due to the small size and counts of shadow maps (1024 x 4) woudl give better results. Also PCF filtering for edges would make it better too.
Also the terrain is not smoothed so it's very blocky itself and shadow maps suffer a lot from that.

Switching to Off: Like expected, back to ordinary TerrainTest

ok

Switching to Cast: same as off

same as before

Switching to Recieve: Now there are shadows drawn, like they were left over from the C&R (same buffer maybe and nothing updates it).

Same as before, but what was left in the shadow maps memory is the render from the castAndReceive mode

Sometimes when I get close to the ground they change.

can be due to what ever happens in graphic memory

for the terrain to cast shadows on itself you have no choice but to put it in the CastAndReceive queue.
3 Likes

Ty very good info.



I saw no shadow techniques etc. in Terrain.j3md , and when you helped with the transparency stuff I had to add a bunch of techniques to the materials, but that’s just for transparent cast/recieve. I forgot…



Maybe it is useful info as well - if you are going to use transparent casters or recievers you have to use materials with shadow techniques. Lighting.j3md has it, and is a great example.

@nehon said:
ok... so actually everything looks normal, and the terrain material is fine.
...
for the terrain to cast shadows on itself you have no choice but to put it in the CastAndReceive queue.


So basically: "Is this expected that when receiving and nothing is there to cast shadows strange things happen?" - Answer is yes, although perhaps not that strange :)

Thanks a lot for your time and the in-depth explanation. I'm learning so much from these forums and everyone here. Big thanks.