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

What is the use of LineRenderer and TrailRenderer in Unity

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail what is the use of LineRenderer and TrailRenderer in Unity. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

LineRender and TrailRender are two good things, and they are used by many Unity trailing effects. Some brief introductions can be found in the official API documentation.

Explore their specific rendering methods here, and then give some Shader to better control them.

In the end, we can achieve an effect like this:

Next, let's look at LineRenderer. The TailRenderer situation is similar later.

Create LineRender

LineRender is a strip that controls the size and position of the rendering by vertices. We can completely control the number of vertices and the specific coordinates.

First we need to create a GameObject, then add the LineRenderer component, and then fill in the following parameters:

It is actually a set of vertices extending in the X direction. Then create a new material and drag it into Materials.

Since you are currently using the default material, you can only get the following look:

It's just a rectangle.

Modify LineRenderer

First, we need a resource picture, something like this:

This image is made using PSD, where the white part represents the part that will be revealed in the strip in the future, and the black part is the transparent part. Only black and white will be fine. Note that you need to add a channel, that is, create a new transparent channel in PSD and paste the black-and-white image into it. Finally, make your passageway look like this:

Why only black and white pictures, not colors? Explain:

We use this material to form the main outline of the strip, and the color displayed is controlled by other parameters, so all we need here is a black-and-white diagram.

Start writing Shader

In the attribute part, just a map is fine.

Properties {_ MainTex ("Base (RGB)", 2D) = "white" {}}

In the Tags part, we need to set up the translucent rendering queue and set the Alpha blending mode, which is the most common Alpha blending mode.

Tags {"RenderType" = "Transparent"IgnoreProjector" = "True"Queue" = "Transparent"} LOD 200 Blend SrcAlpha OneMinusSrcAlpha

Since we do not need light and do not want to be affected by light, we write a lighting equation that returns directly to the primary color to avoid the default lighting treatment. At the same time, in order to avoid generating the Pass of the lighting part, add the noforwardadd parameter, so that our rendering only needs a Pass.

CGPROGRAM # pragma surface surf NoLight vertex:vert alpha noforwardadd / / Lighting equation The name is Lighting followed by # pragma suface the name of the illumination equation / / lightDir: unit vector from vertex to light source / / viewDir: unit vector from vertex to camera / / atten: attenuated attenuation coefficient float4 LightingNoLight (SurfaceOutput s, float3 lightDir,half3 viewDir, half atten) {float4 c C.rgb = s. Albedo; c. A = s. Alpha; return c;}

Next is the vertex shader code, where we just need to pass the vertex colors and UV coordinates of the system and store them so that they can be input to the surface shader.

Sampler2D _ MainTex; fixed4 _ SelfCol; struct Input {float2 uv_MainTex; float4 vertColor;}; void vert (inout appdata_full v, out Input o) {o.vertColor = v. Color; o.uv_MainTex = v. text;}

Finally, the most important part, the surface shader code:

Void surf (Input IN, inout SurfaceOutput o) {half4 c = tex2D (_ MainTex, IN.uv_MainTex); o.Alpha = c.a * IN.vertColor.a; o.Albedo = IN.vertColor.rgb;} ENDCG

In fact, it is also very simple:

Here we just extract the [pixel color transparency] X [vertex color transparency] on the map as the final transparency. The purpose of this is to extract the outline on the map, and the transparency is also affected by the vertex color.

Use [Vertex Color] as the final color.

This allows us to set the color and transparency of the entire stripe through LineRenderer's StartColor and EndColor.

Use the previous map and this Shader to modify the previous material. In the end, the strip looks like this:

Complete ShaderShader "AndrewBox/LineRenderer" {Properties {_ MainTex ("Base (RGB)", 2D) = "white" {} SubShader {Tags {"RenderType" = "Transparent"IgnoreProjector" = "True"Queue" = "Transparent"} LOD 200 Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM # pragma surface surf NoLight vertex:vert alpha noforwardadd / / Illumination equation The name is Lighting followed by # pragma suface the name of the illumination equation / / lightDir: unit vector from vertex to light source / / viewDir: unit vector from vertex to camera / / atten: attenuated attenuation coefficient float4 LightingNoLight (SurfaceOutput s, float3 lightDir,half3 viewDir, half atten) {float4 c C.rgb = s.Albedo; c.a = s.Alpha; return c;} sampler2D _ MainTex; fixed4 _ SelfCol; struct Input {float2 uv_MainTex; float4 vertColor;}; void vert (inout appdata_full v, out Input o) {o.vertColor = v.color O.uv_MainTex = v.texcoord.} void surf (Input IN, inout SurfaceOutput o) {half4 c = tex2D (_ MainTex, IN.uv_MainTex); o.Alpha = c.a * IN.vertColor.a; o.Albedo = IN.vertColor.rgb;} ENDCG} FallBack "Diffuse"} TrailRenderer

TrailRenderer and LineRenderer are very similar, but somewhat different. The differences are:

First of all, its vertex composition is dynamic, and each frame you need to move the GameObject that produces the TrailRenderer to a different location, so that it will be automatically connected into a track.

Second, it controls the color change of the entire strip by five color values, but unlike LineRenderer, you can't modify these color values at run time, because there is no way to access these five colors in its API, but we can still change them through Shader.

The most important thing to remember is that, like LineRenderer, it writes a set of color values to the vertex color, and we just need to read the vertex color.

In order to control the overall color display of the wake, we add a main control color and modify the Shader above.

Properties {_ MainTex ("Base (RGB)", 2D) = "white" {} _ MainCol ("Self Color Value", Color) = (1mem1min1)}

Finally, make corresponding modifications in the surface shader

Void surf (Input IN, inout SurfaceOutput o) {half4 c = tex2D (_ MainTex, IN.uv_MainTex) / / c.an is the transparency on the map, using it to make up the graphic outline of the stripe / / _ MainCol.a the transparency of the main color, used to control the transparency of the entire stripe / / IN.vertColor.an is obtained by vertex color and set by the Colors array Used to control the change of transparency at different locations of the stripe o.Alpha = c.a * _ MainCol.a* IN.vertColor.a / / _ MainCol.rgb the color value of the main color, which is used to display the color value of the main color part / / IN.vertColor.rgb is the vertex color. It is set by the Colors array and is used to control the color changes in different positions of the stripe o.Albedo = _ MainCol.rgb*IN.vertColor.rgb;}.

In this way, we can control the color change of the overall wake over time by mainly controlling the color change. On the other hand, the change of color in different positions on the strip is controlled by those five color values. When we only need to display a solid color strip, set all five colors to white and variable transparency.

Finally, the display effect of TrailRender is as follows:

Of course, you need a script to control the rotation of the GameObject where TrailRender is located, and the code is relatively simple. (the implementation of the BaseBehavior class can be found in my other articles.)

Public class TrailRendererGen: BaseBehavior {[SerializeField] [Tooltip ("rotation angular velocity")] protected float massively 360; protected override void OnInitFirst () {} protected override void OnInitSecond () {} protected override void OnUpdate () {m_transform.Rotate (Vector3.forward, m_angle*Time.deltaTime) }} this is the end of the article on "what is the use of LineRenderer and TrailRenderer 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, please 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