Get normal vector from terrain

Hi everyone,



I try to find out how to get get normal vector from certain point of terrain. So I have x,y z of terrain, how can I get normal vector?



I tried getSurfaceNormal(), but TerrainQuad do not support it, I believe (.



Thanks

Cast a ray straight down to that point, that will return you a collision result and that has the normal vector.

[java]

CollisionResults results = new CollisionResults();

Ray ray = new Ray();

Vector3f pos = new Vector3f(x,100,z);

Vector3f dir = new Vector3f(x,99,z);

dir.subtractLocal(pos).normalizeLocal();

ray.setOrigin(pos);

ray.setDirection(dir);

node.collideWith(ray, results);

CollisionResult result = results.getClosestCollision();

return result.getContactNormal();

[/java]

2 Likes

Hi, I need terrain normals as well, and saw this post. I tried using getTerrainNormal(Vector2f xz), but that didn’t work. I use the same position vector that I use for height (and the heights are always correct), but there are index out of bounds exceptions occationally with the getNormal method. Seems like it could be from the interpolation process, the sample coords not being clamped or something.



Is getTerrainNormal for internal use, and is it better to do ray casting instead?



Grateful for answers, thx.

Do you mean terrain.getNormal()?

That one works, I just tested it and it throws no errors and always returns a valid normal.

hmm, yea maybe that could cause it to fail. I will add in the -1 and see how it goes :slight_smile:

1 Like

Yeah. It fails sometimes. I looked it up, thinking maybe because it uses the term “totalSize/2” in the normalization formulas, while getHeight use “(totalSize - 1) / 2”.



I take lots of normals randomly, sometimes on the edges of the mesh. Position works every time tho. (EDIT: Height, I mean. Not position).

@Sploreg

Nice, thanks a lot. Clamping manually before sending the coords to getNormal seems to be working fine.



I’m passing those normals to grass geometry btw, and using the same ones for both grass and terrain. This makes everything blend. Works prefectly, so now I can start using grass with the advanced terrain shader. :smiley:



http://www.jamtlandoutdoors.se/images/dist/terrain_grass_blending.png

2 Likes

That looks amazing!



I committed that fix btw. See if it helps with the issue.

3 Likes

Wow, that looks incredible in the screenshot. Any chance of a video?

Yeah, looks very life-like. Very big difference from before. There are some material issues left but when those are fixed, and the foliage shader is finished, I think Forester vegetation will be able to match the advanced terrain in detail level etc. Then it’s time to do some serious editor-stuff. And terrain grid stuff.



@zarch

No I can’t video capture now, unfortunately. It will be available in the next build, and there’s gonna be a test in the demo. Not sure when that will be tho, sometimes next month i hope.

1 Like

Regarding TerrainGrid, I have extended the TerrainQuad API so you are not tied to having to use a quad if you want to tile quads together. TerrainGrid works and all, but it forces you to use the quad tree paradigm. I will make a forum post explaining what I have changed.

1 Like

@androlo dude, you have to stop posting pictures of your back yard…this is a serious 3D engine forum here…

1 Like

@nehon

Hehe. I agree, it looks a lot better then I imagined. With a little blur it would almost like an oil painting or something. This will surely inspire people.

1 Like

:open_mouth:

Hmm, I just had a thought…



Since the vegetation is actually modelled as blades etc…how much overhead would it be to “trample it down” - in other words as people/animals/whatever walk over it then they get crushed down/sideways/etc (just scrunch the vertices)… could even have it push them over in the walk direction… could even move some and then have it “spring back” as you move on.



Don’t try and remember the trampling of course, so if they leave the area and come back it will have recovered…but I think it would add a lot of realism to the feel of walking through it if you saw the vegetation respond to things moving through it.

@zarch

I don’t have the slightest idea tbh. I haven’t read anything about it. Probably not that harsh tho, considering it’s been around in games for some time. Maybe they just use some crappy animation, no idea.

@androlo said:
@zarch
I don't have the slightest idea tbh. I haven't read anything about it. Probably not that harsh tho, considering it's been around in games for some time. Maybe they just use some crappy animation, no idea.


I remember seing somewhere that this is what they did (for a game, can't remember wich). Overhead to calculate the curvature was too much for them.

Yeah, I think it’s one of those things that really makes the world seem alive though - when things respond to each other…

Damn! Now I get an error again with the terrain normals. Gonna go through this now line for line. Seems there was a reason for not using that -1 maybe.

Seems it does a “x -= 0.5f” and “z -= 0.5f” in the next method call in the chain.



EDIT: Still works, but I have to clamp it even more, to - ( terrainSize + 2) , terrainSize - 2. I used to have +/- 1. Makes sense tho. Gonna look more into it but after work.