Unity3d Development Diary (1)-Mobile platform real-time shadow scheme

Source: Internet
Author: User

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
  1. Shader "Luoyinan/projector/shadowcast"
  2. {
  3. Properties
  4. {
  5. _shadowcolor ("Main Color", color) = (0.5, 0.5, 0.5, 1)
  6. }
  7. Subshader
  8. {
  9. Pass
  10. {
  11. Color [_shadowcolor]
  12. }
  13. }
  14. }


2. Receive shader:

[CSharp]View PlainCopy
  1. Shader "Luoyinan/projector/shadowreceive"
  2. {
  3. Properties
  4. {
  5. _color ("Main Color", color) = (1,1,1,1)
  6. _shadowtex ("Cookie", 2D) = "" "{}
  7. _masktex ("Mask", 2D) = "" "{}
  8. }
  9. Subshader
  10. {
  11. Tags {"Queue" ="Transparent"}
  12. Pass
  13. {
  14. Zwrite OFF
  15. Fog {Mode Off}
  16. Colormask RGB
  17. Blend Dstcolor Zero
  18. Offset-1,-1
  19. Cgprogram
  20. #pragma vertex vert
  21. #pragma fragment Frag
  22. #include "Unitycg.cginc"
  23. struct v2f
  24. {
  25. FLOAT4 uvshadow:texcoord0;
  26. FLOAT4 pos:sv_position;
  27. };
  28. float4x4 _projector;
  29. v2f Vert (float4 vertex:position)
  30. {
  31. v2f o;
  32. O.pos = Mul (UNITY_MATRIX_MVP, vertex);
  33. O.uvshadow = Mul (_projector, vertex);
  34. return o;
  35. }
  36. Sampler2d _shadowtex;
  37. Sampler2d _masktex;
  38. Fixed4 Frag (v2f i): Sv_target
  39. {
  40. Fixed4 TEXM = tex2dproj (_masktex, Unity_proj_coord (I.uvshadow));
  41. Fixed4 Texs = tex2dproj (_shadowtex, Unity_proj_coord (I.uvshadow));
  42. Texs.rgb + = Texm.rgb;
  43. return texs;
  44. }
  45. Endcg
  46. }
  47. }
  48. }


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

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.