Home  >  Article  >  How to use Projector in Unity3D

How to use Projector in Unity3D

小老鼠
小老鼠Original
2024-03-01 09:32:221208browse

Usage: 1. Create Projector: Create an empty GameObject, and then add the Projector component from Component. You can also Import Package->Projector to import the Projector package.

How to use Projector in Unity3D

Projector principle

Projector can project a Material to all objects within the set frustum superior. Usually contains two map cookies and falloff. Cookie is the projected pattern, and the function of Falloff is mainly to determine the alpha of the projection, that is, light and dark, based on distance. The left side of Falloff is white, with an alpha value of 1, which corresponds to the brightest projection when the distance is closest. The right side is close to black, with an alpha value of 0, which means that as the projection distance becomes farther, the projection will gradually become closer to transparent or even invisible.

How to use Projector in Unity3D

【Achievable】

0 Character selection aperture

1 Ordinary dot shadow (blob shadow)

2 Fake Dynamic shadow

3 Light projection

4 Projector (according to the Material map, it can be a picture, video, or the scene seen by another camera)

5 The effect of 3D or 2D flashlight

Use steps

1. Create Projector.

can be created Empty GameObject, and then add the Projector component from Component as shown below

How to use Projector in Unity3D

You can also Import Package->Projector to import the Projector package. The package content is as follows

How to use Projector in Unity3D

2: Notes

1. Make sure Cookie Texture must be set to Clamp

2. In order to avoid projector bleeding, Cookie Texture enables the Border Mipmaps option, or directly disables Mipmap

How to use Projector in Unity3D

3.FallOff

whether it is orthographic or not.

If FallOff is not used, the cookies with the brightest alpha of 1 will be projected, and will be projected in both directions of the frustrum, causing the "double projection" effect that we do not want to see. If you use the Falloff map that comes with the system package. The shadow fades with distance.

【Example Demonstration】

Example 0: Aperture under the character’s feet

How to use Projector in Unity3D

How to use Projector in Unity3D

Used by projector The shader is as follows, plus a script to control the rotation of the projector.

Shader "Custom/Circle" {
 Properties {
    _ShadowTex ("Cookie", 2D) = "gray" { TexGen ObjectLinear }
 }
 Subshader {
    Tags { "RenderType"="Transparent-1" }
    Pass {
       ZWrite Off
       Fog { Color (1, 1, 1) }
       AlphaTest Greater 0
       ColorMask RGB
       Blend DstColor Zero
       SetTexture [_ShadowTex] {
          combine texture,texture
          Matrix [_Projector]
       }
    }
 }
}

或者用Vertex&Fragment

Shader "Custom/Circle" {
 Properties {
   _ShadowTex ("Cookie", 2D) "" }
 }
 Subshader {
    pass {
       ZWrite off
       Blend DstColor One
      CGPROGRAM
       #pragma vertex vert
       #pragma fragment frag
       #include "UnityCG.cginc"

       sampler2D _ShadowTex;
       float4x4 _Projector;

       struct v2f {
           float4 pos:SV_POSITION;
           float4 texc:TEXCOORD0;
       };
       v2f vert(appdata_base v)
       {
           v2f o;
           o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
           o.texc=mul(_Projector,v.vertex);
           return o;
       }
float4 frag(v2f i):COLOR
{
float4 c=tex2Dproj(_ShadowTex,i.texc);
return c;
}
ENDCG
}//endpass
}
}

Example 1: Ordinary dot shadow (blob shadow)

Add under the character, remember to add the character's layer to the 'Ignore Layer' under the projector settings That’s it.

How to use Projector in Unity3D

##Shader "Projector/Multiply" {


Properties {
_ShadowTex ("Cookie", 2D) = "gray" { TexGen ObjectLinear }
_FalloffTex ("FallOff", 2D) = "white" { TexGen ObjectLinear }
}

 Subshader {
    Tags { "RenderType"="Transparent-1" }
    Pass {
       ZWrite Off
       Fog { Color (1, 1, 1) }
       AlphaTest Greater 0
       ColorMask RGB
       Blend DstColor Zero
      Offset -1, -1
       SetTexture [_ShadowTex] {
          combine texture, ONE - texture
          Matrix [_Projector]
       }
SetTexture [_FalloffTex] {
constantColor (1,1,1,0)
combine previous lerp (texture) constant
Matrix [_ProjectorClip]
}
}
}
}

2 Fake Dynamic shadow (Fake Dynamic shadow)

is to use 3d Max or Maya or Unity first bakes the shadows corresponding to the animation and makes them into sequence frames. Then use the projector's material to create the corresponding frame animation.

3 Light projection

Basically replace the black blob cookie with a white cookie in the middle.

4 Projector (according to the Material map, it can be a picture, video, or the scene seen by another camera)

Another camera draws to RenderTexture, and then the projector's material can be used as RenderTexture

5 The effect of a 3D or 2D flashlight is that the direction and range settings of the projector are consistent with the flashlight. Just throw out the light color of the flashlight.

The above is the detailed content of How to use Projector in Unity3D. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn