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.
- Shader "Unlit/transparent colored with mask" {
- Properties {
- _maintex ("Base (RGB), Alpha (A)", 2D) = "White" {}
- _alphatex ("Yeahyeah", 2D) = "White" {}
- }
- subshader{
- LOD 100
- tags{
- "Queue" = "Transparent"
- "Ignoreprojector" = "True"
- "Rendertype" = "Transparent"
- }
- Pass {
- Cull OFF
- Lighting OFF
- Zwrite OFF
- Fog {Mode Off}
- Offset-1,-1
- Colormask RGB
- Alphatest Greater. 01
- Blend Srcalpha Oneminussrcalpha
- Colormaterial Ambientanddiffuse
- SetTexture [_maintex] {
- Combine Texture * Primary
- }
- SetTexture [_alphatex] {
- Combine Previous, Texture
- }
- }
- }
- }
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 |