Unity3d role shadow Creation

Source: Internet
Author: User

This article will focus on two mainstream shadow production technologies on mobile platforms. It will also briefly introduce the relatively advanced dynamic shadow generation methods on the two mobile platforms.

Currently, the mainstream unity3.x does not support dynamic shadow generation on mobile platforms. Currently, the most popular and cost-effective method for generating shadows is the following two:

 

1. Simple texture Method

The so-called simple texture method is to directly append a shadow translucent texture to the bottom of the role and let it move along with the role. Generally, this shadow object is a subobject of the role model, the shadow generation effect is as follows:

 

The Inspector view of shadow is as follows:

 

Shadowcomponent is a script used to control shadow. It has nothing to do with shadow generation, so it is not described here. There are two main parts that affect the application generation. One is the shadow mesh, and the other is the "No Name" material required for rendering ". It should be noted that the shadow mesh is a plane, but it is not recommended to use the plane generated by Unity itself. because the number of faces generated by unity is large, you can use modeling tools such as 3dmax for row modeling, as follows:

 

For materials, the most important thing is the shader's writing. I will list it below for your convenience:

 

[CSHARP]View plaincopy
  1. Shader "iPhone/simpleshadow"
  2. {
  3. Properties
  4. {
  5. _ Maintex ("maintex", 2d) = ""{}
  6. }
  7. Subshader
  8. {
  9. Tags {"queue" = "Transparent "}
  10. Pass
  11. {
  12. Blend srcalpha oneminussrcalpha
  13. Color [_ clrbase]
  14. Cull off
  15. Lighting off
  16. Settexture [_ maintex] {combine texture, one-texture}
  17. }
  18. }
  19. }

 

You can use the above settings to generate the simplest shadow effect. You can set your own scripts to control the movement and change of shadows.

 

However, the method for generating the shadow has an obvious "Hard damage", that is, the shadow can only be applied to a flat ground. Once the ground is uneven or there is a block, the effect of "wearing" will appear, as shown in, the shadow generated by this method has no effect on the cube at the foot. To solve this problem, the projection method emerged.

 

 

2. Projection Generation Method

This method is essentially a decal technique, that is, to set a projector and then project the shadow to the place where you want to display the shadow, the advantage of this method is that the projection effect does not depend on the geometric shape of the projected area. That is, the projected area can have any concave and convex surfaces or handle various obstacles. The Shadow generated by this method is as follows:

 

The following describes how to generate a shadow:

(1) Create an empty object using "gameobject-> Create empty" and name it "shadow projector ".

 

(2) Add the projector component to the empty object through "component-> effects-> projector", and achieve the following effects by translating, rotating, and adjusting parameters:

 

 

(3) drag the prepared material into the material option to project the shadow. The effect is as follows:

 

We can see that not only shadow is generated in the scenario, but also the role's body is "black", because the projector's "ignore layers" is set to "nothing, when we set the negligible layer as the role's "Player", the role can no longer be projected. The effect is as follows:

 

(4) The final Inspector view of the Shadow projector is as follows:

 

The shader used by the material is:

[CSHARP]View plaincopy
  1. Shader "projector/multiply "{
  2. Properties {
  3. _ Shadowtex ("cookie", 2d) = "gray" {texgen objectlinear}
  4. _ Fallofftex ("falloff", 2d) = "white" {texgen objectlinear}
  5. }
  6. Subshader {
  7. Tags {"rendertype" = "Transparent-1 "}
  8. Pass {
  9. Zwrite off
  10. Fog {color (1, 1, 1 )}
  11. Alphatest greater 0
  12. Colormask RGB
  13. Blend dstcolor zero
  14. Offset-1,-1
  15. Settexture [_ shadowtex] {
  16. Combine texture, one-texture
  17. Matrix [_ projector]
  18. }
  19. Settexture [_ fallofftex] {
  20. Constantcolor (1, 1, 0)
  21. Combine previous lerp (texture) Constant
  22. Matrix [_ projectorclip]
  23. }
  24. }
  25. }
  26. }

 

Through the above steps, we can implement the projection shadow generation method. Like the first method, you can set some specific scripts to control the movement and changes of the shadow. In addition, it should be noted that the setting of the "ignore layers" option is to remove the layers that do not need to be projected as much as possible to reduce unnecessary computing workload.

The above two methods are the mainstream shadow generation methods on mobile platforms. The advantage is that the generation is simple, easy to use, and the calculation workload is small, but the disadvantage is also prominent, that is, the shadow is false, it is not a real object shadow, so it is not realistic. The following describes two real-time dynamic shadow generation methods that can be used on mobile platforms. However, in this article, we will only introduce the shadow effect, rather than the specific implementation technology and generation method, I will explain it in the advanced tutorial later.

 

3. Shadow Map Method

Although Unity does not currently support generating dynamic shadows on mobile devices, you can also use rendertexture to generate a simple shadow map, as shown below:

 

We can see that the generated shadow has a sawtooth image. This is because the shadow map resolution is not enough. You can increase the resolution of rendertexture to reduce the effect of the Sawtooth image, as shown in, of course, this will also bring about a certain amount of rendering consumption.

 

4. Environment occlusion method

This method is taken from the game shadow gun and generates dynamic shadows in real time by analyzing the approximate environmental occlusion information of objects, as shown in:

The generation method is roughly as follows:

First, a shadow grid is generated at the foot of the role.

 

Second, three balls are generated based on the Cross-step and cross-leg of the role respectively. The ambient occlusion information of the bottom is calculated using the three balls.

 

Finally, the grid is dynamically subdivided based on the calculated AO information, so that the final AO shadow can be generated.

 

Summary

To sum up, this article has provided two basic rendering Methods and Their shader implementation for the role shadow on the mobile platform. Here I have not analyzed the principle of each rendering effect, the simple implementation method is provided directly based on the actual situation. If you want to have a deep understanding of the shadow generation method, you can search for its principles on Google. For the last two real dynamic shadow generation methods, I will update them in detail in subsequent blogs.

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.