Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the translucency of characters in Unity

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how Unity achieves translucency in characters. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1 processing of transparent Shader

It is necessary to deal with both transparency and culling of images, which cannot be handled by a pass in shader. The general practice is to render twice, once for translucent effects, and once for transparency culling.

First of all, to use AlphaTest for culling processing, you need to turn on the ZWrite option and render it again.

Pass {AlphaTest Greater [_ CutOff] ZWrite On ColorMask 0 SetTexture [_ MainTex] {ConstantColor [_ Color] Combine Texture * constant}}

Second, you need to turn off the ZWrite option when rendering translucently

Pass {ZWrite Off Blend SrcAlpha OneMinusSrcAlpha ZTest LEqual SetTexture [_ MainTex] {ConstantColor [_ Color] Combine Texture * constant}}

The final effect is that the ribbon is translucent and the skirt edge is the culling effect.

The model is a little rough, let's make do with it.

2 mesh merging of multi-parts of the model

In a 3D game, the change of the model is a very common function, there will be a simple binding of weapons and accessories. In this case, to achieve the translucent stealth effect, there will be the problem of interspersing between models.

The model supports the hair exchange function, the body and hair belong to two parts, transparent interspersed with each other, the effect is not very good

This occurs because after the translucency between the two parts, the depth relationship between the two parts is not known (the translucent effect is rendered in ZWrite mode off), and the perfect transparency effect can only be achieved by merging it into the same Mesh (grid).

Unityofficial mesh merge documents

Of course, relying solely on official documents is of no use. The consistent style of unity official documents, you know, we still have to write our own code, or we can also use off-the-shelf tools like Mesh Baker to achieve it. For the use of Mesh Baker, there are very detailed documents and examples. The mesh merge is accompanied by a material merge, and the code is given before the next section.

3 model multi-material merging

Multiple widgets are generally in different materials, so when rendering a 3D model, you need to deal with multiple shaders at the same time. When you open Unity, we will find that there is a drawcall for each shader you use. Merging multiple materials is also a way to optimize unity performance.

The batches without material merging is 4.

The batches of the merged material is 2

At the outermost layer of the model, I mounted a Model3D.cs script to handle the merging of models and materials, which also requires UV processing, which is already included in the code.

Void Combine () {List combineInstances = new List (); List materials = new List (); List bones = new List (); Transform [] transforms = GetComponentsInChildren (); List textures = new List (); int width = 0; int height = 0; int uvCount = 0; List uvList = new List () / / skinning model foreach (SkinnedMeshRenderer smr in GetComponentsInChildren ()) {if (_ material = = null) _ material = Instantiate (smr.sharedMaterial) as Material; for (int sub = 0; sub < smr.sharedMesh.subMeshCount; sub++) {CombineInstance ci = new CombineInstance (); ci.mesh = smr.sharedMesh Ci.subMeshIndex = sub; ci.transform = smr.transform.localToWorldMatrix; combineInstances.Add (ci);} uvList.Add (smr.sharedMesh.uv); uvCount + = smr.sharedMesh.uv.Length If (smr.material.mainTexture! = null) {/ / Save material materials.AddRange (smr.GetComponent () .texture); / / Save foreach (var mat in materials) {textures.Add (mat.mainTexture as Texture2D) }} / / Save bone information foreach (Transform bone in smr.bones) {bones.Add (bone);} Destroy (smr.gameObject);} SkinnedMeshRenderer r = GetComponent (); if (! r) r = gameObject.AddComponent () R.sharedMesh = new Mesh (); / / merge subgrid r.sharedMesh.CombineMeshes (combineInstances.ToArray (), true, false); r.bones = bones.ToArray (); r.material = _ material; Texture2D skinnedMeshAtlas = new Texture2D (width, height); Rect [] packingResult = skinnedMeshAtlas.PackTextures (textures.ToArray (), 0); Vector2 [] atlasUVs = new Vector2 [uvCount] / / merge materials and process uv int j = 0; for (int I = 0; I < uvList.Count; iTunes +) {foreach (Vector2 uv in uvList [I]) {atlasUVs [j] .x = Mathf.Lerp (packingResult [I] .xMin, packingResult [I] .xMax, uv.x) AtlasUVs [j] .y = Mathf.Lerp (packingResult [I] .yMin, packingResult [I] .yMax, uv.y); jacks;} r.material.mainTexture = skinnedMeshAtlas; r.sharedMesh.uv = atlasUVs;}

There is also a problem in the above code, that is, only the grid of SkinnedMeshRenderer type is merged. In unity, when the driven model FBX file is imported into the project, unity will be imported to SkinnedMeshRenderer type by default. But if the current FBX has no action (many weapons do not need action and rely directly on binding point actions), unity will be imported to the MeshRenderer type by default, and this code will not be able to merge the grid of the model.

SkinnedMeshRenderer (driving to include bone information)

MeshRenderer (no action, no bone information)

There are two solutions to this situation:

1. Waste the resources of a bone and add a bone to all parts without actions, so that when imported into unity, it will be imported to SkinnedMeshRenderer type by default, and there will be no problem of merging different types of grids. 2, in fact, SkinnedMeshRenderer and Mesh types can be merged, such as Mesh Baker can be achieved, the specific methods I do not have a specific study, interested friends can see for themselves.

After the operation effect of the merger, there is no piercing phenomenon.

Be careful

1. When rendering with this shader, if it is non-transparent, you need to adjust the value of _ cutoff to a value close to 1 and less than 1, such as 0.95, so that the display effect is correct. 2. When you modify the transparency to lower the alpha value of the color, you need to synchronously lower the _ cutoff value, which is slightly greater than the _ cutoff value, otherwise the model level will not be correct (rendering order).

Thank you for reading! This is the end of this article on "how to deal with characters translucently in Unity". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report