[Unity Shaders] map and character occlusion processing

Source: Internet
Author: User
[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.