In Unity3D, How to make the whole scene in dark, but some Area are not in dark -


i wan't make teaching levels ugui, need whole in dark, rectangular area not in dark.

i can make whole scene in dark adding gameobject image component, , control it's alpha dark.

but don't know how make rectangular area lighted, , rectangular change in different teaching levels. , different device different resolution change rec's bounder.

picutre3 wanted, , rectangular area ok.

picture1 p1

picture2

p2

picture3

p3

picture4 p4

i'll explain how can achieve simple shader , setup. i'm speaking memory if problems occur please ask.

so can is:

  • make new layer rendering masks. let's call layer masklayer.

  • duplicate main camera. call old main camera cama , new 1 camb.

  • set cama's culling mask include except masklayer.
  • set camb's culling mask include masklayer.
  • create new render texture (let's maskrt) , set render target camb.
  • now create game object. let's make sprite solid colour (white).
  • set layer of sprite masklayer.

at point should notice sprite no longer visible in game window or in cama preview. rest of scene visible. when select camb plane visible in preview. output camb represents "holes" be.

now continue this:

  • create new sprite large viewport.
  • ensure sprite has default tag.
  • create new shader , new material. call maskmaterial. default shader template created should enough. don't remember provides should have @ least texture input.
  • lets shader parameter called diffuse. in pixel shader, set output colour like:

    fixed4 frag (v2f i) : sv_target {     fixed4 texcol = tex2d (diffuse, i.uv);     fixed4 outputcolor = fixed4(0,0,0, 0.8-texcol.r);     return outputcolor ; } 

so here in our shader in plain english is: color solid black slight transparency (0.8) unless input texture has white color @ same position, in case more transparent (-texcol.r).

we use r component here can use color component.

now assign new material , shader full screen sprite , set maskrt render texture input texture diffuse mask shader.

if went (lol right) should have desired effect. moving mask easy moving objects in scene marked masklayer layer. can have many masks.

notes:

  • the sprite might make things difficult perhaps start out plane faces camera in case.

  • here docs on shaders in case need reading there:http://docs.unity3d.com/manual/shadertut2.html it's worth looking can away trial , error time.

    • in shader there color input default. can use in fragment shader instead of saying (0,0,0..) easier , more control on resulting mask. allow change masking appearance usual sliders of material. example:

    fixed4 outputcolor = fixed4(inputcolor.albedo, inputcolor.a-texcol)

edit: can have @ "using shader properties in cg code" section here:http://docs.unity3d.com/manual/shadertut2.html can copy paste entire piece , change fragment shader if struggling (sorry don't know level of experience have it).

edit: here shader requested. read following notes.

shader "tutorial/textured colored" {     properties {         _color ("main color", color) = (1,1,1,0.5)         _maintex ("texture", 2d) = "white" { }     }     subshader {         tags         {           "queue" = "transparent"         }          blend 1 oneminussrcalpha          pass {          cgprogram         #pragma vertex vert         #pragma fragment frag          #include "unitycg.cginc"          fixed4 _color;         sampler2d _maintex;          struct v2f {             float4 pos : sv_position;             float2 uv : texcoord0;         };          float4 _maintex_st;          v2f vert (appdata_base v)         {             v2f o;             o.pos = mul (unity_matrix_mvp, v.vertex);             o.uv = transform_tex (v.texcoord, _maintex);             return o;         }           fixed4 frag (v2f i) : sv_target         {             fixed4 texcol = tex2d (_maintex, i.uv);             fixed4 outputcolor = _color;             outputcolor.a -= texcol.r;             return outputcolor ;         }         endcg          }     } } 

notes:

  • this shader should go on full screen quad.
  • note difference in shader property names (instead of diffuse more clear _color , _maintex.
  • the logic remains same.
  • this code not tested.
  • there more efficient ways example writing masking object depth single channel rendertexture way easier learn think.
  • once semi-working there several improvements made. first change mask rectangles' material 1 unlit solid color shader.

update:

i left out crucial parts of shader sorry. please see tags section , blend operation added shader. please note full screen quad should rather use plane , not sprite shader not compatible sprites in state.

also note in example project here: https://drive.google.com/file/d/0bw8x8yat21aiyjryvkxjukhrevu/view?usp=sharing

that plane flipped both in x , z axes.

as ugui part missed earlier - solution might work. have not checked. if mask behind ui can try set "queue" tag in shader "overlay".


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -