# How to distinguish outdoor and indoor areas in shader

This topic is 2420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everybody!

I'm working on a shader-based approach for a wet surfaces effect during rain in a PC game (S.T.A.L.K.E.R.). The game uses deferred shading/rendering with DirectX9 / HLSL and provides access to vertex and pixel shaders, but not material definitions or postprocess effects.

I already got a solution for faking wet terrain when it rains, however I have no idea how to apply it only to outdoor areas of the map, so that the effect won't be visible inside buildings, under bridges or large trees etc.

I've thought of something like checking whether an pixel's/vertexes normal line hits other pixel's/vertexes vertically above it, but I'm not sure if and how it would be possible to implement it, or if there would be any better approach for this.

Any ideas would be appreciated.

##### Share on other sites
You can do this via shadow-mapping techniques. Use any technique that works well for sun-shadows in large scenes (e.g. cascaded shadow maps), but instead of creating a shadow map from the sun's direction, create it from the direction that the rain is coming from.

Then in the shader, determine if you're in/out of the shadow like you would normally, but then use that value as a 'wet surface' value, instead of a shadow value.

##### Share on other sites
Thanks for the reply. The problem is that I have only access to the shaders themselves, not the host application as it would be the case in a XNA application, for example. So I need a way to do it only with the things that the game engine passes to the shaders, like the vertices / pixels and the default sampler states (position, color, normal map, light map, bump map, bloom etc.). Things like "d3dcreate..." won't work here, so I can't create my own shadow map.

##### Share on other sites
That makes it a bit harder then

##### Share on other sites
You could try to work with bounding volumes (boxes), i.e. for all indoor area render approximated boxes and set a stencil for indoor pixels or reject the pixel in the shader when the bounding box test failed. Overdraw should be limited when utilizing the stencil buffer.
When you don't have access to the stencil buffer either, try to utilize some other buffer attribute (i.e. alpha channel).

##### Share on other sites
As far as I understand the game setting (it's quite undocumented), I have and can alter the source code of the shaders of three rendering stages, which they call

1. The G-Stage (geometry stage), also refered to as the deferring stage - These might be the shaders that write to the g-buffer
2. The lighting stage - these shaders seem to read data from the G-Stage, process the lighting, and pass it over to the
3. Combine stage, which are mainly pixel shaders doing stuff like fake AA, motion blur, depth of field, contrast and saturation adjustments, and combine the result with the bloom sample from the buffer.

Does that make sense? Where would I start looking then?

##### Share on other sites
This link to a conference presentation (PowerPoint document) sort of describes the mentioned game engine architecture:

When looking into the shaders code, you see these sampler state definitions referencing the described stages/phases:

////////////////////////////////////////////////////////////////////////////////////////// // Geometry phase / deferring // uniform sampler2D s_base; // uniform sampler2D s_bump; // uniform sampler2D s_bumpX; // uniform sampler2D s_detail; // uniform sampler2D s_bumpD; // uniform sampler2D s_hemi; // uniform sampler2D s_mask; // uniform sampler2D s_dt_r; // uniform sampler2D s_dt_g; // uniform sampler2D s_dt_b; // uniform sampler2D s_dt_a; // uniform sampler2D s_dn_r; // uniform sampler2D s_dn_g; // uniform sampler2D s_dn_b; // uniform sampler2D s_dn_a; // ////////////////////////////////////////////////////////////////////////////////////////// // Lighting/shadowing phase // uniform sampler2D s_depth; // uniform sampler2D s_position; // uniform sampler2D s_normal; // uniform sampler s_lmap; // 2D/cube projector lightmap uniform sampler3D s_material; // uniform sampler1D s_attenuate; // ////////////////////////////////////////////////////////////////////////////////////////// // Combine phase // uniform sampler2D s_diffuse; // rgb.a = diffuse.gloss uniform sampler2D s_accumulator; // rgb.a = diffuse.specular uniform sampler2D s_generic; // uniform sampler2D s_bloom; // uniform sampler s_image; // used in various post-processing uniform sampler2D s_tonemap; // actually MidleGray / exp(Lw + eps)

Does that help?

##### Share on other sites

Does that help?

No, to be honest. You try to mod an existing game/engine which is on the other hand not so popular. The most popular engines are unity,udk and cryengine, but nevertheless, it will be always hard to implement a special feature in an existing engine. It depends heavily on the engine and not everything is possible and you need often a hack-solution to approximate your goal. But I fear that you choose an engine not many people know.

Best to take a look at the stalker forums, maybe there's a section about modding their game. With some luck a developer might give you some hints.

Did you consider to change the engine ?

##### Share on other sites
The PPT that you linked to describes the shadow-mapping technique that I posted earlier -- apparently after the G stage but before the lighting stage, they use this shadow map to modify the G-buffer for wet surfaces.

##### Share on other sites

Best to take a look at the stalker forums, maybe there's a section about modding their game. With some luck a developer might give you some hints.

Did you consider to change the engine ?

The xray engine might not be as popular as unity,udk and cryengine, but since I'm improving shaders on an existing game rather than developing a new one from scratch, changing the engine is not an option. And of course I took a look in the stalker forums, actually I'm a quite active user of the modding section there, but unfortunately there are very few stalker modders doing shader stuff. That's why I landed here after all in hopes of possibly getting better answers to my problems. But apparently it's not that easy

The PPT that you linked to describes the shadow-mapping technique that I posted earlier -- apparently after the G stage but before the lighting stage, they use this shadow map to modify the G-buffer for wet surfaces.

Uhm, I seem to have overrread that part, sorry. I will try to sort this out, although it's kinda hard for me to figure out how to implement your suggestion with what I got...

but instead of creating a shadow map from the sun's direction, create it from the direction that the rain is coming from.

Then in the shader, determine if you're in/out of the shadow like you would normally, but then use that value as a 'wet surface' value, instead of a shadow value.

Could you maybe give me some code example showing what you mean exactly? That might be helpful since I'm not a shader guru (yet )

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633294
• Total Posts
3011242
• ### Who's Online (See full list)

There are no registered users currently online

×