Unity3d using Mask in Ngui

Source: Internet
Author: User

Welcome to Unity Learning, unity training, Unity Enterprise training and education, there are many u3d resources, u3d training videos, u3d tutorials, u3d FAQs, u3d Project source code, "Dog Planing Learning Network" Unity's Ultimate Academy, dedicated to building the industry unity3d training, Learn the first brand.
The process is this: recently always wanted to do a face mask effect, and later found that the original unity mask needs to write with shader, online to find a lot of information, but also can be achieved, but most are using the render texture as the camera projection texture. Then take the image of the camera as a material, then create a texture in the Ngui and assign the material to texture. How to say that, although this way can be achieved, but must create a camera, for a large project is a bit unrealistic. An avatar with a mask how simple one thing, there must be a better way.
finally found a good way, this approach is closer to the idea of the method, the nonsense is not much to say, follow me step by step! first, we need to create a new shader. Then add the following code to him. in project attempt, right-click->create->shader, create a new Shader, name it casually.
  1. Shader "Unlit/transparent colored with mask" {
  2. Properties {
  3. _maintex ("Base (RGB), Alpha (A)", 2D) = "White" {}
  4. _alphatex ("Yeahyeah", 2D) = "White" {}
  5. }

  6. subshader{
  7. LOD 100

  8. tags{
  9. "Queue" = "Transparent"
  10. "Ignoreprojector" = "True"
  11. "Rendertype" = "Transparent"
  12. }

  13. Pass {
  14. Cull OFF
  15. Lighting OFF
  16. Zwrite OFF
  17. Fog {Mode Off}
  18. Offset-1,-1
  19. Colormask RGB
  20. Alphatest Greater. 01
  21. Blend Srcalpha Oneminussrcalpha
  22. Colormaterial Ambientanddiffuse

  23. SetTexture [_maintex] {
  24. Combine Texture * Primary
  25. }

  26. SetTexture [_alphatex] {
  27. Combine Previous, Texture
  28. }
  29. }
  30. }
  31. }
Copy Code
By the way, shader. Too official may not be good to understand, probably means that in the rendering time shader rules, these rules are often used to achieve some cool special effects, or to make some features more efficient. the properties in the code are the same as the public variables in the unity script, and when you assign shader to a material, the variables can be figured out, and you can assign them to the unity script basically. the Subshader in the code is a sub-renderer, where you can access the variables set in front of you, here is the main implementation of the shader function, of course, you can also define functions here, and so on, but the function is a language called CG/HLSL. Pass, which is the render channel. Here you can set some parameters to specify how to render. Well, basically, if you want to know shader, see Unity 's official explanation .
All right, let's do mask. If the shader code can understand, basic you will also do. It has done two things mainly:1, give two texture assignment, actually is two pictures. 2, the _alphatex in the opaque pixels and _maintex in the pixel output, in fact, quite in the Maintex key out a alphatex in the opaque part of the shape of the same tile.
actually at first I searched a lot of shader, many did not mention how to use, see belowactually very simple, click on the built shader in the Inspector panel you will see
By the way, shader. Too official may not be good to understand, probably means that in the rendering time shader rules, these rules are often used to achieve some cool special effects, or to make some features more efficient. the properties in the code are the same as the public variables in the unity script, and when you assign shader to a material, the variables can be figured out, and you can assign them to the unity script basically. the Subshader in the code is a sub-renderer, where you can access the variables set in front of you, here is the main implementation of the shader function, of course, you can also define functions here, and so on, but the function is a language called CG/HLSL. Pass, which is the render channel. Here you can set some parameters to specify how to render. Well, basically, if you want to know shader, see Unity 's official explanation .
All right, let's do mask. If the shader code can understand, basic you will also do. It has done two things mainly:1, give two texture assignment, actually is two pictures. 2, the _alphatex in the opaque pixels and _maintex in the pixel output, in fact, quite in the Maintex key out a alphatex in the opaque part of the shape of the same tile.
actually at first I searched a lot of shader, many did not mention how to use, see belowactually very simple, click on the built shader in the Inspector panel you will see

Drag the two pictures up from the project view. Maintex is the picture you want to showAlphatex is the shape you want to cutout, note that this picture must be transparent except for any color, and the size of the figure is as large as the Maintex picture. The last is to use, create a texture with Ngui, drag shader into the shader variable of uitexture script, you will find the excitement happened!! The final effect is as follows:
Disclaimer: This document is from the "Dog Planing Learning Network" community, is the Netizen self-published Unity3d study articles, if there is any content violated your relevant interests, please communicate with the official, we will deal with the real-time.
More Highlights:Www.gopedu.com

Unity3d using Mask in Ngui

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.