# Terrrain LOD \[UPDATE: dynamic shadows !!\]  huge open world for all (glsl shader)

**URL:** <https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014>\
**Category:** Game Engine Resources\
**Created:** [June 1, 2012, 1:21pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014 "2012-06-01T13:21:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![benj](https://blenderartists.org/user_avatar/blenderartists.org/benj/32/5683_2.png) [@benj](https://blenderartists.org/u/benj)\
**Post date:** [June 1, 2012, 1:21pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/1 "2012-06-01T13:21:59Z")

</div>

Hello!  
Here is my “LODShader”

This shader handle the terrain LOD. It uses real-time displacement in order to display more polygons near the camera, and less polygons far way. This allow you to make huge open world for your games! Or at least, big background for your games.  
It’s still in developpement, but I hope it already could be useful 🙂

EDIT: Update 2013/02/28 ============================

There is now a fully working physic system that allow on-foot gameplay (FPS, TPS, race game, vehicles, etc.).  
Video:

================================================)

 ![](https://blenderartists.org/uploads/default/original/3X/6/1/611963a0bf73772d95de535d93903f3b73b6ebea.jpg)  
  ![](https://blenderartists.org/uploads/default/original/3X/6/2/62918f55b311bd23f2ab60778719f922231c4cdb.jpg)  
  ![](https://blenderartists.org/uploads/default/original/3X/3/a/3a69ba7c911cabf9642ec5318024a49055e756c0.jpg)  

_Current features:_

- fast and efficient LOD via real-time displacement.
- unobtrusive and easy to setup, highly ajustable.
- multilayer shading with 4 materials
- colormap, normalmap and specularmap for each of these 4 materials
- stencil maps to mix different materials
- 2-steps fog (2 colors and 2 distance)
- baked shadows map support

_What it can’t do:_  
_- cast and receive dynamic shadows (you have to bake them)_

- have collision (but you can use an invisible collision mesh)  
  
UPDATE : dynamic shadow and collisions are now possible !

_In what kind of game I can use it:_

- All of them, except a game where the collision with the player and the ground are too visible. This is the big weakness, for now.

_Where to download it:_  
[\>HERE\<](http://benjamin.vedrenne.free.fr/STOCKAGE/LODShader.py)

_How to use it:_  
As I said before, it’s easy to setup. Easy, but not obvious… that’s why I made this little tutorial…

---

<div class="post-metadata">

**Author:** ![benj](https://blenderartists.org/user_avatar/blenderartists.org/benj/32/5683_2.png) [@benj](https://blenderartists.org/u/benj)\
**Post date:** [June 1, 2012, 1:25pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/2 "2012-06-01T13:25:54Z")

</div>

_ **How to use it** _

_ **0.** _ Import the shader in your blend file.

_ **1.** _ Create a plane. Unwrap the UVs, then subdivide the plane. Subdivide the center many times to get something like this:

 ![](https://blenderartists.org/uploads/default/original/3X/7/b/7bf0b1d226b2b0c417079b5c1653e350893f8987.jpg)  

_ **2.** _ Create the following games properties on this object:  
They will make the LOD to work properly

![](https://blenderartists.org/uploads/default/original/3X/8/c/8c05c00219a97edc2a5b9609a3552fbb1f0f5735.jpg)

- uvX and uvY : leave them to zero
- height : define the height of the terrain.
- cell size and cell number: What I call a “cell” is one of the biggest quads of your mesh, in the lower density area (look at the picture below). The cells number is the number of cells in the width of you terrain. The cell size is the width of one cell, in blender unit, and have to be an integer.  
(If your cells width is not an exact number of blender units, scale your mesh to make this match to an integer value… For example : my mesh have 64 cells in the width, I put the dimensions to 128… The cell size is 2, and cell number is 64! easy)
- LODcenter: simply put the name of the object that the LOD will follow. By default put the active camera name.

 ![](https://blenderartists.org/uploads/default/original/3X/2/1/214a38ca25a96ece02acc858e526535ac00c66c9.jpg)

---

<div class="post-metadata">

**Author:** ![benj](https://blenderartists.org/user_avatar/blenderartists.org/benj/32/5683_2.png) [@benj](https://blenderartists.org/u/benj)\
**Post date:** [June 1, 2012, 1:27pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/3 "2012-06-01T13:27:26Z")

</div>

_ **3.** _ Setup the logic like this (it will call the shader):

![](https://blenderartists.org/uploads/default/original/3X/f/8/f86d4cd781c0297d5219ea67006fb8c0e6780985.jpg)

_ **4.** _ Setup the textures.

First create your textures. You can use these textures:

- height map of the terrain (needed, in gray shade)
- normal map of the terrain (needed)
- lightmap of the therrain (optional, use pure white instead)  
And for each the 4 materials:
- color (optional, use any solid color instead)
- normal map (optionnal, use solid color instead with R=127 G=127 B=255, or R=0.5 G=0.5 B=1.0)
- specular map (gray shade, optionnal, use pure black instead)
- stencil map (for 3 materials only, gray shade)

As you can see, you don’t have to use all of them, but if you don’t, you have to provide something instead to the shader, otherwise it won’t work at all.

Now you have your textures, we have to give them to the shader. This is the tricky part, but don’t panic, it’s very easy. Custom glsl shader does not accept a lot of textures. In order to make this shader the most compatible possible (with GPUs), we put all these textures in 4 files.

Create a new material, and place your files in the texture panel in this exact order:

![](https://blenderartists.org/uploads/default/original/3X/c/1/c13e3d2b8b0dd04adeeaa44b900bd396c7dcefa5.jpg)

- height map : this file contains the normal map of the terrain in the R,G,B channels and the heightmap in the Alpha channel.

![](https://blenderartists.org/uploads/default/original/3X/7/1/713b3e4d6b4d336ab51665881e48959f04383793.jpg)

---

<div class="post-metadata">

**Author:** ![benj](https://blenderartists.org/user_avatar/blenderartists.org/benj/32/5683_2.png) [@benj](https://blenderartists.org/u/benj)\
**Post date:** [June 1, 2012, 1:29pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/4 "2012-06-01T13:29:08Z")

</div>

- textures: this file contains color, specular and normal of each material, they’re placed like this on a 4\*4 pattern:

![](https://blenderartists.org/uploads/default/original/3X/c/4/c481fd51a54ff3cc68a8277728a28733fc811ded.jpg)

- stencils: the three stencils are placed in different channls of the same picture:

- material 2 stencil on the BLUE channel
- material 3 stencil on the RED channel
- material 4 stencil on the GREEN channel  
(be aware that material1 is below others, material2 is over material1, material3 is over these, and material 4 is over all)

 ![](https://blenderartists.org/uploads/default/original/3X/f/5/f5b6f03787d45af86415c84d8464b0c58f0534c7.jpg)  

- lightmap: put baked shadows and/or ambient occlusion here. Take care that non-shadowed areas are pure white otherwise everything can be too darkenned.  
 ![](https://blenderartists.org/uploads/default/original/3X/a/d/adbdcfd598f00057f00ed9ff11842ba656ee8948.jpg)

---

<div class="post-metadata">

**Author:** ![benj](https://blenderartists.org/user_avatar/blenderartists.org/benj/32/5683_2.png) [@benj](https://blenderartists.org/u/benj)\
**Post date:** [June 1, 2012, 1:35pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/5 "2012-06-01T13:35:21Z")

</div>

_ **5.** _ Adjsut the material in the shader.  
Go to the text windows, open the shader, find the “variables to tweak” area.

 ![](https://blenderartists.org/uploads/default/original/3X/9/4/945757f9ad6f8d6e98cab256ab3bd9dcb05d8e6b.jpg)  

Most of the parameters speak by themself. If some are unclear, change values and you will see the changes. All the values are float so take care to write “4.0” and not “4”.  
Only the “margin” value may not be obvious. Because all the material textures are placed on the same file, tiling artifact may appear. To avoid this, place margin around your textures, like this:

 ![](https://blenderartists.org/uploads/default/original/3X/5/2/52386d57fc38d11e866f168d9c1a84d273c75181.jpg)  

In this example, I choose 0.6 as a value, but you can basically scale your texture like you want.  
I suggest you first not to make any margin around your texture, and then change the “margin” value directly in the shader (from 1.0, decreasing step by step) until artifacts disappear. Then scale your textures acording to the value you found.

_ **6.** _ Run the game and enjoy 🙂

---

<div class="post-metadata">

**Author:** ![benj](https://blenderartists.org/user_avatar/blenderartists.org/benj/32/5683_2.png) [@benj](https://blenderartists.org/u/benj)\
**Post date:** [June 1, 2012, 1:44pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/6 "2012-06-01T13:44:49Z")

</div>

Please feel free to ask if I am unclear!

Feel free to use the shader, share it, improve it !

And please let me know and see if you use it for an actual game :]

_Some tips:_

- Use only square textures, power of 2 scaled.
- Once you have your heightmap, create a mesh from it using the “displace” modifier. From this mesh you can bake normal map of the terrain, and lightmap. You can even paint on it to create your stencils maps.
- You can get the material textures easily from [cgtextures.com](http://cgtextures.com)
- to put some material only on top of your terrain (for example), use your heigtmap as a stencil, and change the gray shade using curves.
- to put one material only on the steep areas, use the blue channel of the terrain normal map as a stencil.
- Use this same mesh to create the collision mesh. You can use the “decimate” modifier to decrease the polygon number of the collision mesh.
- I suggest you to subdivide the first plane accordingly to the size of your heightmap… To be the most optimized, you should have one vertex per pixel in the center area.
- for optmization, choose the texture size smartly. Having big texture slow down the shader… Height/normal texture is the biggest because it really needs details. But lightmap can be much smaller. Stencils map can also be a lot smaller. Do some tests…

---

<div class="post-metadata">

**Author:** ![benj](https://blenderartists.org/user_avatar/blenderartists.org/benj/32/5683_2.png) [@benj](https://blenderartists.org/u/benj)\
**Post date:** [June 1, 2012, 2:07pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/7 "2012-06-01T14:07:13Z")

</div>

Okey, then the .blend demo file… [HERE](http://benjamin.vedrenne.free.fr/STOCKAGE/LODShader_packed.blend) (26 Mo, tell me if I forgot to pack something)

Laser blaster also made a version that support multilights: [HERE](http://www.mediafire.com/?coy6l41avb4u4af)(you may have to tweak some little things in order to make averything works properly, look at posts #41 and #46 if you have some problems with this version)

Let me know if it works for you (what fps?), if not what’s your GPU specs…

---

<div class="post-metadata">

**Author:** ![superflip](https://blenderartists.org/user_avatar/blenderartists.org/superflip/32/14855_2.png) [@superflip](https://blenderartists.org/u/superflip)\
**Post date:** [June 1, 2012, 4:43pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/8 "2012-06-01T16:43:35Z")

</div>

I get this

 ![](https://blenderartists.org/uploads/default/original/3X/a/1/a176d8e2f4002b4117650e47a31bf6101594e677.jpg)  

 ![](https://blenderartists.org/uploads/default/original/3X/7/2/723cb0f37c7314ed5f609789d8285b651ad1da08.jpg)  

on my older system…but the lod works perfectly and wow at the high steady framerates even on my older system. I want to say over 112+ fps at most points 😃

ATI RADEON 3650 HD  
DUAL 2.4 Xeon processors

---

<div class="post-metadata">

**Author:** ![laser\_blaster](https://blenderartists.org/user_avatar/blenderartists.org/laser_blaster/32/9456_2.png) [@laser\_blaster](https://blenderartists.org/u/laser_blaster)\
**Post date:** [June 1, 2012, 7:25pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/9 "2012-06-01T19:25:59Z")

</div>

The shader doesn’t work for me at all(everything is transparent). But I’m sure its fixable. It’s probably some miniscule error in your shader code that my gpu driver doesn’t like. I’ve had this problem before. I’m currently going though the code, seeing if I can find any problems.

Thanks for posting this, though.

@ Superflip: Hmmm… I wonder if that’s simply a precision problem with your gpu, or if there’s some sort of compatibility issue at hand.

EDIT: I managed to get the shader to work properly without BGE throwing any errors. However, my poor old relic of a computer just can’t handle the strain. Instead of 30 frames per second, my framerate is more like 30 **seconds per frame**. It also crashed my computer after a while… I’ll see if I can change anything to improve the situation.

EDIT 2: Okay, I don’t think there are any errors in your code. The problem is my ancient gpu. It’s an Nvidia GeForce 6800, and has limited support for vertex texture fetches. I did a bit of research: **(For vertex texture fetch)Gf 6 supports only GL\_TEXTURE\_2D of format GL\_LUMINANCE32F\_ARB and GL\_RGBA32F\_ARB. It doesn’t support any of the other floating point formats or fixed point formats. There is no floating point compressed format.All other formats besides GL\_LUMINANCE32F\_ARB and GL\_RGBA32F\_ARB cause the VS to run in software mode.**

I assume Blender doesn’t use either of those texture formats, and that’s causing the vertex shader to run in software, thus the massive slowdown.

This is a very nice shader you’ve created. It’s a shame I can’t really use it.

---

<div class="post-metadata">

**Author:** ![benj](https://blenderartists.org/user_avatar/blenderartists.org/benj/32/5683_2.png) [@benj](https://blenderartists.org/u/benj)\
**Post date:** [June 1, 2012, 10:59pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/10 "2012-06-01T22:59:21Z")

</div>

Well I hope it will work at least for someone…  
(yes 30 seconds per frame means that CPU is doing the job…😕 )

It seems that Blender uses GL\_ARB\_multitexture… (which is already deprecated).

The only thing you can do is to update your GPU driver, but I guess you already did.

@ super flip: nice glitch… I have no idea why ! Maybe code should be changed for ATI card ? Like laserblaster said, its looks like everything works fine except some precision 😕

---

<div class="post-metadata">

**Author:** ![1808](https://blenderartists.org/user_avatar/blenderartists.org/1808/32/21199_2.png) [@1808](https://blenderartists.org/u/1808)\
**Post date:** [June 2, 2012, 1:38am UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/11 "2012-06-02T01:38:52Z")

</div>

Does it work on 2.49?

---

<div class="post-metadata">

**Author:** ![superflip](https://blenderartists.org/user_avatar/blenderartists.org/superflip/32/14855_2.png) [@superflip](https://blenderartists.org/u/superflip)\
**Post date:** [June 2, 2012, 7:06am UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/12 "2012-06-02T07:06:20Z")

</div>

> super flip: nice glitch… I have no idea why ! Maybe code should be changed for ATI card ? Like laserblaster said, its looks like everything works fine except some precision 😕

I checked my video drivers to make sure they were up-to-date, To my surprise they were not. After upgrading drivers it works perfectly without the glitches. And a smooth 100 + Framerate at any zoom, orientation level even on my ancient machine. Nice Job.  
😃

---

<div class="post-metadata">

**Author:** ![laser\_blaster](https://blenderartists.org/user_avatar/blenderartists.org/laser_blaster/32/9456_2.png) [@laser\_blaster](https://blenderartists.org/u/laser_blaster)\
**Post date:** [June 2, 2012, 12:11pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/13 "2012-06-02T12:11:05Z")

</div>

> [@1808](#):
>
> Does it work on 2.49?

No. If you want the shader to work in 2.49, you’d need to modify the Python script to be compatible.

And if you want the demo file to work, you’ll need a version of Blender that has Bmesh(2.63 and above, I believe).

---

<div class="post-metadata">

**Author:** ![benj](https://blenderartists.org/user_avatar/blenderartists.org/benj/32/5683_2.png) [@benj](https://blenderartists.org/u/benj)\
**Post date:** [June 2, 2012, 2:18pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/14 "2012-06-02T14:18:50Z")

</div>

@superflip : hey im happy to know it works well for you too ! Whats your gpu spec?

@laserblaster : actually the python part of the script is very small, it should be very easy to change for 2.49… I should make a 2.49 compatible file…

Does it work for someone else?

---

<div class="post-metadata">

**Author:** ![1808](https://blenderartists.org/user_avatar/blenderartists.org/1808/32/21199_2.png) [@1808](https://blenderartists.org/u/1808)\
**Post date:** [June 3, 2012, 1:10am UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/15 "2012-06-03T01:10:49Z")

</div>

It’s quite beautifull by the way, and it would look even cooler with the day/night cycle skydome.

---

<div class="post-metadata">

**Author:** ![superflip](https://blenderartists.org/user_avatar/blenderartists.org/superflip/32/14855_2.png) [@superflip](https://blenderartists.org/u/superflip)\
**Post date:** [June 3, 2012, 6:38am UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/16 "2012-06-03T06:38:41Z")

</div>

> @superflip : hey im happy to know it works well for you too ! Whats your gpu spec?

its only a ATI RADEON 3650 HD AGP (512), quite a few years old

---

<div class="post-metadata">

**Author:** ![leonnn](https://blenderartists.org/user_avatar/blenderartists.org/leonnn/32/9115_2.png) [@leonnn](https://blenderartists.org/u/leonnn)\
**Post date:** [June 3, 2012, 11:10pm UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/17 "2012-06-03T23:10:50Z")

</div>

For me its working very well! I just cant move forward, but that is ok

---

<div class="post-metadata">

**Author:** ![Elfaz\_Blend](https://blenderartists.org/user_avatar/blenderartists.org/elfaz_blend/32/10759_2.png) [@Elfaz\_Blend](https://blenderartists.org/u/Elfaz_Blend)\
**Post date:** [June 4, 2012, 9:16am UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/18 "2012-06-04T09:16:32Z")

</div>

> [@leonnn](#):
>
> For me its working very well! I just cant move forward, but that is ok

he did not use A, S, W, D to move the camera, try changing its logic brick

---

<div class="post-metadata">

**Author:** ![Scrible](https://blenderartists.org/user_avatar/blenderartists.org/scrible/32/17952_2.png) [@Scrible](https://blenderartists.org/u/Scrible)\
**Post date:** [June 4, 2012, 9:26am UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/19 "2012-06-04T09:26:28Z")

</div>

Is the demo-blend supposed to look like this?

 ![](https://blenderartists.org/uploads/default/original/3X/b/d/bd8ff78ae8dc1a653c472a82e04d197b03e4dd86.JPG)  

**edit: Ah, I just realized it doesn’t work when just pressing “p”. Using the standalone-player it works fine.**

---

<div class="post-metadata">

**Author:** ![klauser](https://blenderartists.org/user_avatar/blenderartists.org/klauser/32/18092_2.png) [@klauser](https://blenderartists.org/u/klauser)\
**Post date:** [June 4, 2012, 11:26am UTC](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014/20 "2012-06-04T11:26:30Z")

</div>

> [@Scrible](#):
>
> Is the demo-blend supposed to look like this?
> 
> **edit: Ah, I just realized it doesn’t work when just pressing “p”. Using the standalone-player it works fine.**

No, it works fine when you press ‘p’, you just have to make sure your viewport shading is set to ‘Texture’. This is seriously impressive benj. I get a nice steady 60fps with this, even with martinish’s DOF shader running alongside it. Fantastic work. Are there many more improvements to be made with this, or is this almost done?

 ![](https://blenderartists.org/uploads/default/original/3X/3/7/37cce74e4769dfad97c44665ccb72437174ef7b3.jpg)

[Next page](https://blenderartists.org/t/terrrain-lod-update-dynamic-shadows-huge-open-world-for-all-glsl-shader/543014.md?page=2)
