How should I approach lighting a specific area ?

Hey,

I’m working on a turned based games with ships on sea. I’m using the basic water mesh in the standard assets as my playfield. I scaled it 200, 0, 200 so it’s pretty big. Of course I don’t want the game to play in that entire area I just want a portion of it, preferably where the camera doesn’t see the edges of the round mesh. I can limit my camera and ship movements within boundaries just fine. Then I got thinking about how will I visualize the game area to my players. I first thought to use some sort of transparent texture with a dotted boundary line pasted onto the water mesh. However then I started thinking that the shader on the water might do weird stuff with that. Also there doesn’t seem to be something like pasting textures on a mesh like a decal or whatever that stuff is called (I’m a programmer taking his first steps in game programming and not an artist).

So I dropped that idea and I came up with how about I light everything that is out of bounds really dark and the play area really bright.

I’m just not sure how to approach this, I currently have one directional light in my scene lighting everything. I’ve been experimenting with spotlight but not with much luck. I have two challenges, first the lighted area needs to match my bounds. To make it easy the center of the square is 0,0,0 the boundaries are (-10,0,-10),(-10,0,10),(10,0,-10),(10,0,10). So an unnormalized 10 by 10 grid. I probably need two lights but how to configure them is puzzling me. Can anyone put me on the right direction ?

Write a surface shader for water material, and make albedo black for areas outside of your target range.

If this doesn’t work (objects will probably remain reflective at edges even with black albedo), do the same thing using “finalColor”. See “Custom Fog with Final Color modifier” in the linked manual page.

Eeeep shaders … I’ll give it a shot when I get home from work. I have to do a lot of reading though. I currently understand nothing about the shader syntax, how it runs, etc…

Currently I’m using the shader for the water that is in the standard assets pack. Your suggestions does sound like it’s the way to go though without much of a doubt. Just wished it was as simple as adding some lights to the scene :frowning:

Well… there are also light cookies you could use to “mask” the light.

You could try those. It should be similar to setting a “projector filter” on the light, so if you point the global directional light directly down, it might work the way you want. However, I think that water will remain reflective (meaning skybox will be visible) outside of cookie’s range. IMO “final color” adjustment shader is your best bet. The link I posted earlier has examples, and those shouldn’t be too complicated.

So I have been reading up on shaders here: A gentle introduction to shaders in Unity - Shader tutorial

This thought me a little, then I looked at the vertex shader topic and saw that the water shader in the standard assets is a vertex shader. So I think I know what has to happen but I bumped on some roadblocks. This is the current standard water shader (I’m not gonna pretend I know exactly how he works cause I don’t but I have a pretty good idea what I could do)

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "FX/Water (Basic)" {
    Properties {
        _horizonColor("Horizon color", COLOR) = (.172, .463, .435, 0)
        _WaveScale("Wave scale", Range(0.02, 0.15)) = .07[NoScaleOffset] _ColorControl("Reflective color (RGB) fresnel (A) ", 2 D) = "" {}
            [NoScaleOffset] _BumpMap("Waves Normalmap ", 2 D) = "" {}
        WaveSpeed("Wave speed (map1 x,y; map2 x,y)", Vector) = (19, 9, -16, -7)
    }

    CGINCLUDE

    # include "UnityCG.cginc"

    uniform float4 _horizonColor;

    uniform float4 WaveSpeed;
    uniform float _WaveScale;
    uniform float4 _WaveOffset;

    struct appdata {
        float4 vertex: POSITION;
        float3 normal: NORMAL;
    };

    struct v2f {
        float4 pos: SV_POSITION;
        float2 bumpuv[2]: TEXCOORD0;
        float3 viewDir: TEXCOORD2;
        UNITY_FOG_COORDS(3)
    };

    v2f vert(appdata v) {
        v2f o;
        float4 s;

        o.pos = UnityObjectToClipPos(v.vertex);

        // scroll bump waves
        float4 temp;
        float4 wpos = mul(unity_ObjectToWorld, v.vertex);
        temp.xyzw = wpos.xzxz * _WaveScale + _WaveOffset;
        o.bumpuv[0] = temp.xy * float2(.4, .45);
        o.bumpuv[1] = temp.wz;

        // object space view direction
        o.viewDir.xzy = normalize(WorldSpaceViewDir(v.vertex));

        UNITY_TRANSFER_FOG(o, o.pos);
        return o;
    }

    ENDCG

    Subshader {
        Tags {
            "RenderType" = "Opaque"
        }
        Pass {

            CGPROGRAM# pragma vertex vert# pragma fragment frag# pragma multi_compile_fog

            sampler2D _BumpMap;
            sampler2D _ColorControl;

            half4 frag(v2f i): COLOR {
                half3 bump1 = UnpackNormal(tex2D(_BumpMap, i.bumpuv[0])).rgb;
                half3 bump2 = UnpackNormal(tex2D(_BumpMap, i.bumpuv[1])).rgb;
                half3 bump = (bump1 + bump2) * 0.5;

                half fresnel = dot(i.viewDir, bump);
                half4 water = tex2D(_ColorControl, float2(fresnel, fresnel));

                half4 col;
                col.rgb = lerp(water.rgb, _horizonColor.rgb, water.a);
                col.a = _horizonColor.a;

                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

in the frag function I could do something like

                if(xlocationworld > 10 || xlocationworld < -10 || ylocationworld > 10 || ylocationworld < -10){
                    return half4(1.0, 0.0, 0.0, 1.0);
                } else { return col; }

What I do not understand is how do I get those locations. I tried adding something to the v2f struct but each time I do the entire mesh turns pink, so I believe that means there is some kind of error I do not know how to debug. I believe I can get the world location with

o.wPos = mul(_Object2World, v.vertex).xyz;

In the vert function I just don’t know how to store it in the v2f to use it in the frag function. How can I extend the v2f struct if at all and if not how can I store the worldcoordinates somewhere ?

When the mesh turns pink you have an error message printed to console/output pane which is visible in editor. You can read the message and see what shader compiler doesn’t like. If you double-click the message, unity will open monodevelop or visualstudio at the line that has a problem.

To store world position in v2d, simply add something along the lines of:

float3 worldPos: TEXCOORD4;

To v2f structure, then initialize it in “vert” function, for example:

float4 wpos = mul(unity_ObjectToWorld, v.vertex);
o.worldPos = wpos.xyz;

And then you can access acces it from within “frag” function.

Also…:

Unity manual provides articles about shaders and basic examples that should be trivial enough to follow. I already provided a link to them:

1 Like

Thanks neginfinity,

I got it to work with your explaination on how to get the world coords into the struct. The tutorial I linked explained shaders far better to me then the manual to be real honest. First of I’m not native english now while that shouldn’t be an excuse it makes it easier when someone explains a functionality in easy to understand words and sentence structure then the pure technical speech a manual provides. Thanks for pointing me in the right directions though and show me how to expand the struct cause I tried several things giving me that pink mesh and I didn’t notice the error in the error pane.

This is my final shader code should others need to do something similar so here it is

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "FX/Water (Basic)" {
    Properties {
        _horizonColor ("Horizon color", COLOR)  = ( .172 , .463 , .435 , 0)
        _WaveScale ("Wave scale", Range (0.02,0.15)) = .07
        [NoScaleOffset] _ColorControl ("Reflective color (RGB) fresnel (A) ", 2D) = "" { }
        [NoScaleOffset] _BumpMap ("Waves Normalmap ", 2D) = "" { }
        WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
    }

    CGINCLUDE

    #include "UnityCG.cginc"

    uniform float4 _horizonColor;

    uniform float4 WaveSpeed;
    uniform float _WaveScale;
    uniform float4 _WaveOffset;

    struct appdata {
        float4 vertex : POSITION;
        float3 normal : NORMAL;
    };

    struct v2f {
        float3 worldPos: TEXCOORD4;
        float4 pos : SV_POSITION;
        float2 bumpuv[2] : TEXCOORD0;
        float3 viewDir : TEXCOORD2;
        UNITY_FOG_COORDS(3)
    };

    v2f vert(appdata v)
    {
        v2f o;
        float4 s;

        o.pos = UnityObjectToClipPos(v.vertex);

        // scroll bump waves
        float4 temp;
        float4 wpos = mul (unity_ObjectToWorld, v.vertex);
        o.worldPos = wpos.xyz;
        temp.xyzw = wpos.xzxz * _WaveScale + _WaveOffset;
        o.bumpuv[0] = temp.xy * float2(.4, .45);
        o.bumpuv[1] = temp.wz;

        // object space view direction
        o.viewDir.xzy = normalize( WorldSpaceViewDir(v.vertex) );

        UNITY_TRANSFER_FOG(o,o.pos);
        return o;
    }

    ENDCG

    Subshader {
        Tags { "RenderType"="Opaque" }
        Pass {
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_fog

                sampler2D _BumpMap;
                sampler2D _ColorControl;

                half4 frag( v2f i ) : COLOR
                {
                    half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv[0] )).rgb;
                    half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv[1] )).rgb;
                    half3 bump = (bump1 + bump2) * 0.5;
 
                    half fresnel = dot( i.viewDir, bump );
                    half4 water = tex2D( _ColorControl, float2(fresnel,fresnel) );
 
                    half4 col;
                    col.rgb = lerp( water.rgb, _horizonColor.rgb, water.a );
                    col.a = _horizonColor.a;

                    UNITY_APPLY_FOG(i.fogCoord, col);
                    if(i.worldPos.xy.x > 40 || i.worldPos.xy.x < -40 || i.worldPos.xyz.z > 40 || i.worldPos.xyz.z < -40){
                        return col/3;
                    }
                    return col;
                }
            ENDCG
        }
    }
}