[Unity Shaders] map and character occlusion processing
The first method:
Methods of radiographic Inspection
Idea: Make a ray between the main camera and the character, if the wall on the map obscures the character model, this ray must be
Walls intersect. The wall's material is set to a transparent material after the intersection is detected. Implements the display of masked characters.
Not much nonsense to say on the picture:
When doing the ray, the ray is very easy and the character intersects, therefore must add a judgment the tags,
Release the code below
The/** Ray detects the colliding body between the camera and the player, setting the transparent material */using System.Collections;
Using System.Collections.Generic;
Using Unityengine;
public class Raytest:monobehaviour {public Camera playercamera;
Public Material Blendmat;
Public Material Commonmat;
Private Transform Playerpos {get {return gameobject.getcomponent<transform> ();
}} private Gameobject Blendwall;
Use the For initialization void Start () {}//update is called once per frame void update () {
var direction = playerpos.position-playercamera.transform.position;
Raycasthit hit; if (Physics.raycast (PlayerCamera.transform.position, vector3.normalize (direction), out hit, Vector3.magnitude ( direction)) {if (Hit.collider.tag = = "Wall") {Debug.drawline (playercame
Ra.transform.position, Hit.point, color.red);
Blendwall = Hit.collider.gameObject; Blendwall.getcomponent<renderer> (). material = Blendmat;
Return
}} if (Blendwall! = null) blendwall.getcomponent<renderer> (). material = Commonmat; }
}
Result screenshot
* Second Method *
Methods for drawing using Ztest
Ideas:
Modify the rendering order of the people and place the rendering of the characters behind the wall.
Tags {"Queue" = "transparent+5"}
No occlusion part zwrite on ZTest lequal
Partially occluded ZTest Greater Lighting off Zwrite off
If the wall is transparent, you must ensure that the wall's depth cache is written.
Pass
{
zwrite on
colormask 0
}
Also make a pass to write the depth cache
Release part of the code:
Pass
{
offset-50,0
ZTest Greater
Lighting off
zwrite off
Blend srcalpha Oneminussrcalpha
cgprogram
#pragma vertex vertblend
#pragma fragment fragblend
ENDCG
}
Pass
{
Zwrite on
ZTest lequal
cgprogram
#pragma vertex vert
#pragma fragment frag
ENDCG
}
My second method is mainly referring to this article