Draw Grid on Terrain

Hi, i want to draw a grid on the terrain. (I dont want to display the mesh)

I changed the frag file so only some special pixels are colored:

http://img6.imagebanana.com/img/hri675nt/ugly.jpg



But as you can see the lines far away looks ugly. Because of this I want all lines in the grid to have a 1 pixel width, like in this picture:

http://img6.imagebanana.com/img/8jnfdkfv/dowant.jpg



I have no idea how to realize this.

1 Like

does this help?



https://wiki.jmonkeyengine.org/legacy/doku.php/jme2:wireframestate?s[]=wireframe

@kbender88 said:
does this help?

https://wiki.jmonkeyengine.org/legacy/doku.php/jme2:wireframestate?s[]=wireframe


That's for jME2, although jME3 also has a wireframe capability.

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:debugging

Do you just want it to look nicer, or the lines to be more noticeable farther away?

If you just want it to look nicer, add the anti aliasing filter to your scene to smooth out the jaggedness of the lines.

as i good know terrain is based on Tris not Quads only. so Wireframe will show Tris then? am i wrong?

@oxplay2 said:
as i good know terrain is based on Tris not Quads only. so Wireframe will show Tris then? am i wrong?

yes you do not want to render wireframe to get the effect he wants.
Do you just want it to look nicer, or the lines to be more noticeable farther away?

I want the lines to be more noticeable farther away and I dont want to make the lines bigger.

@Sploreg: i dont want create new topic so i write here. Is there way to export alphamaps and heightmap from j3o terrain scene?

this SDK terrain editor is awesome, but i need to have terrain data in images.

No, but a plugin that does that would be a 20 line one-classer.

@oxplay2 said:
@Sploreg: i dont want create new topic so i write here. Is there way to export alphamaps and heightmap from j3o terrain scene?
this SDK terrain editor is awesome, but i need to have terrain data in images.

The alpha maps are stored under assets/Textures/terrain-alpha and have the name of your scene and the terrain name.
No way to export the heightmap as an image. But that will be slower for loading the terrain anyways that just using the j3o.
If you really need the heightmap as an image, then you will have to write it. It wouldn't be hard. Terrain.getHeightMap() gives you all height values, you just need to export that to an image. If you write the thing, I can put it in the SDK as a button.
1 Like
@ogerlord said:
I want the lines to be more noticeable farther away and I dont want to make the lines bigger.

I see. Well, the solution to this is beyond my shader skills. @nehon might be able to help you with ideas on how to achieve it. I know he has done something similar, but I'm not certain he had the same use case as you.

yep, the lines of my grid even scale up as the camera goes away so the lines always have the same thickness (almost)



this code assume that the terrain texture coordinates are linear, so this might not work with triplanar mapping.

Also this creates a black grid, having it of different color may require some adaptation.



in your vert shader declare a float varying called dist and do this in the main function



dist = -(g_WorldViewMatrix * vec4(inPosition, 1.0)).z;





declare the varying also in the frag shader and add this at the end of the main function



float gridValue = clamp(step(dist0.003,fract(texCoord1.x * float(m_GridSize))) * step(dist0.003,fract(texCoord1.y * float(m_GridSize))),0.4,1.0);



gl_FragColor = color * vec4(gridValue);





m_GridSize is the number of cells you want in your grid.

dist is the distance to the cam that you computed in the vert shader. The 0.003 arbitrary constant is what you have to tweak to change the line thickness



the result look like this, enable anti aliasing and it should look better.

http://i.imgur.com/puxzq.jpg

1 Like

Hey @nehon if I change the Grid class to do this job, or it’s a bad idea? I think I would have to create multiple lines for the Grid mesh, one for each point with different height (or angle, to improve), instead of the current implementaiton, right? With the shader, would this be better (faster, easier)? I have no idea how to do this with shaders. Need to watch that tutorail by @wezrule

Shader is always the way, faster for sure…not always easier, but here it’s very easy.



just copy the unshaded material (or lighting), rename it to what ever name suits you, and apply the changes I explained in the post before.



I recomand @wezrule tutorial suite.

Did what you said, but when the quad is loading the material, a thousand boxes shows up in the SDK, but I can only see two of those boxes, as they are overlapped and the SDK freezes.



RendererException: Cannot recompile shader source.

and

OpenGLException: Invalid Operation (1282)



I think I missed something here, like sending the dist value from one shader to the other (vert to frag).



Edit: Ok, it was a compile error. Neither “color” or “texCoord1” exists, replaced with “gl_FragColor” and “texCoord”, but I think I did it wrong, as the whole terrain is just darker.



Edit2: Ok, it was a problem trying to pass the values to the shader. Now I got it workin (sort of)