unlimited planar reflections & refraction (update)

(JohnnyBlack) #221

It is nice, but quite hungry on a pretty good system. Gets around 90 fps sometimes…

(RealTimeBrush) #222

This is amazing stuff this is a diamond mine of ready cut gems. Fantastic work guys.

(martin.hedin) #223

I have noticed that the reflection texture is of the backfaces of all meshes. So if you use dynamic lighting, the light is inverted, since you are seeing a reflection of the backfaces. Any idea how to get reflections of the front faces?

line 79 to 90 in watertexture.py is an example how this is supposed to be fixed, but still the light is inverted.

#culling front faces as the camera is scaled to -1

#plane equation
normal = own.getAxisVect((0.0, 0.0, 1.0)) #plane normals Z=front

D = -own.position.project(normal).magnitude #closest distance from center to plane
V = (activecam.position-own.position).normalized().dot(normal) #VdotN to get frontface/backface

#invert normals when backface
if V<0:
    normal = -normal

(redbaron5) #224

Is there any way to make an object exempt from the reflection part of the watertexture script? Im working on a flying game and in order to ensure the landscape is continually reflected, I need to set the clipping offset to a very high amount. I was wondering if I could make it so the actual flying object is NOT reflected.

(paleos) #225

I have made a contribution to the water shader,

i have made it so that the water surface when viewed from the above is tinted with the water color,
when viewed from below it is not.

to apply my patch do the following:

replace the following

vec3 Transmittance = mix(refraction,refraction*waterColor,waterDensity);

vec3 color = mix(mix(Transmittance,scatterColor,lightScatter),reflection,clamp(fresnel,0.0,1.0));

gl_FragColor = vec4(color+specular,1.0);</i>

with this

vec3 OverwaterTransmittance = mix(refraction,refraction*waterColor,waterDensity);
vec3 UnderwaterTransmittance = refraction;

vec3 Overwatercolor = mix(mix(OverwaterTransmittance,scatterColor,lightScatter),reflection,clamp(fresnel,0.0,1.0));
vec3 Underwatercolor = mix(mix(UnderwaterTransmittance,scatterColor,lightScatter),reflection,clamp(fresnel,0.0,1.0));
//Checks if the water surface plane is frontfacing or backfacing
if (gl_FrontFacing) {
gl_FragColor = vec4(Overwatercolor+specular,1.0);
gl_FragColor = vec4(Underwatercolor+specular,1.0);

Also do not forget to disable Backface Culling for the water surface.

As a Bonus I attached a water surface normal map of higher quality than the voronoi one that comes with the demo .blend to this post,
I generated it by baking a mesh generated by the ocean modifier against a plane.

As a further Bonus I also attached a displacement map for parallax mapping, parallax occlusion mapping, OpenGL 4 Tessellation, Etc. of the water surface.
I baked it the same way i baked the normal map except displacement instead of normals.


(martinsh) #226

thanks for feedback and suggestions, this keeps the thread alive :slight_smile:
paleos: yep using “gl_FrontFacing” is a much cleaner solution, thanks, in future versions I will change it.


I demoed this version in Blender Conference

download link to be added

(3d solar system builder) #227

This is awesome.It would be cool to have this in a videogame.Does this beat the water and sky in the crisis videogame.I never played crisis.

(Rubbernuke) #228

This is fantastic, and just gets better and better! My only crit with video #3 is the palm trees and islands look too small and make the waves /water ripples look too large and a bit fake.

But this is easily as good as Crysis, and the foam 'bit’s near the shoreline are just lovely (and the sparkles…pretty!)

Out of interest, how did the conference go?

(martinsh) #229

thanks guys,
Rubbernuke - yup the water wave formation is something I am focusing next. The texture approach was good for water plane without displacement, but now when I want to have varying wave frequencies and amplitudes for coastline behaviors and buoyancy physics, this approach is not the best. I have been looking into procedural approaches like gerstner waves and they seem to be reasonably easy and light to implement.
So the next update should have breaking waves and objects floating and reacting to waves.
Blender Conference was great, it was my first time there and I will try to attend next ones.

martin.hedin - shore detection is made by using a displacement texture I baked from terrain to water surface. I cannot think of better way of doing this.
Yes the stars will move with Sun when I post the download link :slight_smile:

Really nice video, this gives me some ideas for the sky setup.

(Rubbernuke) #230

If you can implement that would be amazing- Blender will actually have bleeding edge, commercial grade seas! So will we have a seaside track in your racing game :wink: ?

Something I just thought of:

Do you think in rough seas you can have sea spray, especially on the apex of rough waves, or have waves react to objects (bow waves etc)?

(Jackii) #231

My god… Displaced waves and ripples…! You sir, are amazing.

(draw) #232

Awesome!! :slight_smile: :slight_smile: :slight_smile:

(carlo697) #233

edited edited

(reaperhell) #234

Sir Can you please provide the tutorials for the same.Its really awesome,
but didn getting from ready blend…

(Jsewill) #235

@martinsh (or anyone who wants to respond): In the reflection shaders 1.0 blend, whenever I have two objects with either the refract_ob shader or the stained glass(1 or 2) present in the scene, one of them goes a bit buggy and gives a bunch of feedback. Here’s a screenshot:

These screenshots were taken on my macbook pro which has the Nvidia 320m. I’m about to test it on other hardware. If it isn’t just the hardware, what do you think it could be?


I just confirmed the same results on a Radeon HD 7850. I’d love to track down the issue myself, fix it, and post a blend…provided I have time.


I realized how silly I was to miss what was causing these results. It was just the need for a new instance of the texture.Texture (bge.texture.Texture) class and a non-linked material (though the textures can be linked, just need a new material block) for each additional object. No blend needed I guess.

Sorry for the interruption. We now return to our regularly scheduled program.

(Mathes) #236

I am so impressed…But i also want to understand it. As i can guess , the material setting isn´t important right ? I can change the diffuse to be multiplied but that won´t change anything. So the code defines the shader and the material is just for the texture slots right ?

I marked the code that i guess its the right part. So above it what it should do (how it looks) and at the bottom its the link to the material and texture code ? Lets say i want to have the diffuse changed to be blend add, i have to do it in the code ? How to do so and how is it connected together ?

Greetings, Mathes

(Jsewill) #237

Yes, the generated blender shader is being replaced with a custom shader program via python. This means that your assumption that the material settings are being ignored, is mostly (, if not totally,) correct. The screenshot you posted shows part of the fragment shader, which is where we’d do what you’re wanting to do, in this case.

Line 74 is where all the texels get combined to make the final fragment color. Instead of having the diffuse variable multiplied against the refract variable, you could add it to the result of the mix function, like so:

vec3 color = diffuse+mix(refract,reflect,clamp(fresnel,0.0,1.0));

This may not have your desired effect, but strictly speaking, the diffuse would be added to the result of the refraction and reflection textures. Mess around with it.

FYI, the blender game engine currently supports glsl 1.2–which is the standard that those shaders in that file are written against. I believe there are some volunteer devs putting time into upgrading this, and I know blender proper is getting or has already got an opengl upgrade, so you may see newer language standards supported by the blender game engine in the near future.

(Mathes) #238

Thanks so much ! Now i think i understand it even further as i expected it :slight_smile:

(Mathes) #239

Another question, the variable refraction and reflection is a named variable or is it a function ?
I searched around a bit, but did not find a complete documentation on blender and GLSL. Is there a list of functions available besides the documentation here ? http://en.wikibooks.org/wiki/GLSL_Programming/Blender

I am curious how to develop shader like MartinSH does. :slight_smile:

(Jsewill) #240

That’s a good place to start.

Those are both variables defined within the fragment shader. GLSL is a tiny bit like C or C++, certainly in some of its syntax.

What martinsh has done here (and in much of his GLSL related work published on these forums) is used external input as shader uniforms and samplers (some of which is populated by a second render camera). That’s an important factor in developing GLSL shaders, creatively using external (, that is external to the shader program) data to get a desired result.

Unfortunately, besides directing you to the official GLSL documentation (https://www.opengl.org/documentation/glsl/), I don’t have many other resources for you. I’ve spent much time searching through these forums and digging into many of the awesome shaders and material node setups people have created, I’d recommend it. martinsh has some really great ones.