The shadow of this thing, said the word long, many years ago people began to study a variety of shadow technology, but there are all kinds of flaws and problems, until recently in the PSSM, that is, CE3 CSM, shadow technology is a relatively perfect solution. Unity comes with real-time shading, and it's also a technology that's very mature on the computer. I also witnessed the development of shadow technology, I have also written some of the shadow, I thought that no longer worry about the shadow, the results of mobile games, found that the situation is not so optimistic. This depth-based shadow map technique Support for floating-point textures is required, but some mobile platforms do not support floating-point textures, which is especially costly even if supported. So we need to rethink what kind of shadow scheme is appropriate for mobile platforms.
I divided the common real-time shadow technology into 3 large categories, one-by-two analysis.
1. Template-based volumetric shading
Based on the edge of the model, the model needs to be handled specifically because it is based on the edge of the model and is at the vertex level rather than the pixel level, resulting in the inability to support alpha test, such as the protagonist's wings. Give up
2. Shadow map based on the projection projector
Projection-based, projected textures, requires rendertexture, supports alpha test,
But this technique also has a lot of problems, such as reverse projection (with a fall of texture resolution), multiple projections (like Bridge and bridge), etc.
Shadow quality and rendertexture texture size close Very large, if you want a rendertexture texture to render many characters, texture will be larger, to deal with the shadow Edge,
But to handle a single role, you can avoid a lot of problems. can be considered.
3. Shadow map based on depth depth
Unity comes with this, which requires a floating-point texture, but some phones don't support it, and it's expensive, regardless.
Through the above analysis, combined with the actual situation of their own game, 45-degree perspective game, my final plan is:
Medium quality:
1. Only the protagonist uses the shadow map based on the projection projector
2. Other characters, with the simplest shadow patches tied to the foot. (If the mob is empty, hide the patch and display it on the floor to prevent wear)
The advantage is that both real-time shadows and efficiency are ensured.
High image quality:
1. All roles in the field of view shadow map based on projection projector
Implementation principle:
It is simple to render the character to Rendertexture and then project it to the ground with projector.
1. Projecting shader:
[CSharp]View PlainCopy
- Shader "Luoyinan/projector/shadowcast"
- {
- Properties
- {
- _shadowcolor ("Main Color", color) = (0.5, 0.5, 0.5, 1)
- }
- Subshader
- {
- Pass
- {
- Color [_shadowcolor]
- }
- }
- }
2. Receive shader:
[CSharp]View PlainCopy
- Shader "Luoyinan/projector/shadowreceive"
- {
- Properties
- {
- _color ("Main Color", color) = (1,1,1,1)
- _shadowtex ("Cookie", 2D) = "" "{}
- _masktex ("Mask", 2D) = "" "{}
- }
- Subshader
- {
- Tags {"Queue" ="Transparent"}
- Pass
- {
- Zwrite OFF
- Fog {Mode Off}
- Colormask RGB
- Blend Dstcolor Zero
- Offset-1,-1
- Cgprogram
- #pragma vertex vert
- #pragma fragment Frag
- #include "Unitycg.cginc"
- struct v2f
- {
- FLOAT4 uvshadow:texcoord0;
- FLOAT4 pos:sv_position;
- };
- float4x4 _projector;
- v2f Vert (float4 vertex:position)
- {
- v2f o;
- O.pos = Mul (UNITY_MATRIX_MVP, vertex);
- O.uvshadow = Mul (_projector, vertex);
- return o;
- }
- Sampler2d _shadowtex;
- Sampler2d _masktex;
- Fixed4 Frag (v2f i): Sv_target
- {
- Fixed4 TEXM = tex2dproj (_masktex, Unity_proj_coord (I.uvshadow));
- Fixed4 Texs = tex2dproj (_shadowtex, Unity_proj_coord (I.uvshadow));
- Texs.rgb + = Texm.rgb;
- return texs;
- }
- Endcg
- }
- }
- }
Optimization:
1. Rendertexture as simple as possible, the size is best not more than 512, the format with no alpha channel 565, turn off anti-aliasing, because only a single color, can turn off the depth,
2. Shadow cast shader as simple as possible, for efficiency, my shader does not use the master texture, so the alpha test is not supported. If you want to support it, you can modify the shader yourself.
3. The shadow camera sees more objects than the game's main camera, only the objects seen by the main camera produce shadows. You can render several characters less. Use onbecamevisible to achieve.
4. In order to avoid reverse projection wearing help, the shadow direction as far as possible not too oblique.
5. The distant shadow edge decays with a mask texture:
As a result, you can see the natural attenuation of the shadows in the distance, and can be projected onto the slopes:
Unity3d Development Diary (1)-Mobile platform real-time shadow scheme