Distant surfaces rendering in front of near surfaces

I have a problem that I know what is happening but I don’t know how to get Xenko to solve it. I am hoping someone may be able to point me in the right direction, it might requiring me pinging @xen2 or @Kryptos :rofl:

I am in the middle of trying to generate terrain for an enhanced flowing water demo. To create the plane I did a literal copy / paste of GeometricPrimitive.Plane.New from the engine’s code itself, with the only change is that I am using the height map to set a Y-Coordinate of the vertex instead of 0.0f, and I calculated my own normals (though I tried using UnitY before posting this).

When the game first loaded I was excited because I got what I expected; a mesh that looks like the heightmap I used, along with simplistic height based texturing.

But then I move around the world and noticed that this is happening:

The water way on the other side is showing through. You can see shadows cast from hills close to the camera, but not the hills themselves. It’s basically rendering far things first, or far things are replacing close things, etc.

I implemented the terrain by:

  1. Creating an empty entity with a script attached to it,
  2. The script creates the terrain primitive, an empty model, and a model component.
  3. Calls primitive.ToMeshDraw() and adds the mesh to the model, and adds the model to the model component.
  4. Lastly, adds my texture shader to the model, and adds the model to the entity that the script was attached to.

I was hoping that adding it to an entity already in the scene would let me make use of default mesh rendering to handle this sort of thing. I had tried playing with my own custom effect before going this route and was having a hard time getting that working. Now I am sort of lost and frustrated.

Any help would be greatly appreciated!

Actually, I just updated to Xenko 3.0.0.2 and this appears to be working as I was expecting now!

Before you were using 2.x right? (nothing changed between 3.0.0.1 and 3.0.0.2)

Nope I had created the project after upgrading to v3. Odd that nothing changed then. It did tell me the project had to be converted to 3.0.0.2 after I installed it, perhaps there was some pointer to something, some subtle setting, etc, that got corrected?

4 posts were split to a new topic: Xenko versioning (editor behavior & PackageReference dependencies)

@xen2 I figured out what actually fixed this. I had a MaterialTransparencyBlendFeature on the material I created in code when I was getting the issue. I had that disabled when I thought it was fixed by the update. Now, even though I had one attached, it was set to Alpha = 1.0, and the shader was returning alpha = 1.0 as well so I wasn’t expecting it to render distance surfaces. Not sure if that is a misunderstanding on my part, I’m still new to working with shaders in general, or if the depth logic needs to take alpha at 1.0 into consideration.

I have had a couple PMs for the code to generate the terrain in the screenshot. The project was lost in a hard drive crash but I was able to get most of it off the disk. I plan on finishing the sample eventually but I have a gist of the main logic that can be viewed here:

This is the state of the file before the drive died (plus an example added to the top), it might (probably) have errors or something else missing but the concept should be there. The GetNormal() and GetHeight() methods are simple but represent the bulk of the technique to creating the geometry.

2 Likes

Thank you for everything !

I started rebuilding this project and came across a piece of code that might cause confusion because it’s an extension method, and I myself had to do a bit of searching to remind myself what it could be. It’s essentially the method ColorModel.getRgb() from Java.

namespace XenkoTerrain.Extensions
{
  public static class ColorExtensions
  {
    public static float ToRgb(this Color self)
    {
      return
        (self.A << 24)
        | (self.R << 16)
        | (self.G << 8)
        | (self.B << 0);
    }
  }
}