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 Shader implementation of Unity inkjet effect?

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

Share

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

What is the Shader implementation of Unity inkjet effect? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

For similar inkjet effects used in games, bullets often used in shooting games, such as bullets fired by players, will have a similar inkjet effect on the wall, as shown below:

By default, the entire Alpha channel of the screen is black, and the Alpha channel that has been splashed into the area will not turn white until the player starts spraying ink. Then the image effect is that the original color is mixed with the grayscale.

The implementation is as follows:

As you can see from the image above, use Projector to paint the paint onto the surface of the object and create a color mask. Each Projector uses programmatic dynamic generation to initialize when a bullet (a white spot in flight) touches a surface. The Projector comes with a cartridge collision body, and when the bullet lands on the Projector, it does not initialize the new Projector, but makes the original Projector larger. This increases the amount of paint, while the number of Projector in the scene remains the same.

By default, the Unity standard shader writes 1 for the Alpha channel of all opaque objects. So let's replace the Unity standard shader with a custom shader. Create a new standard surface shader and replace its surface function with the following:

Void surf (Input IN, inout SurfaceOutputStandard o) {/ / Albedo comes from the colored texture fixed4 c=tex2D (_ MainTex, IN.uv_MainTex) * _ Color; o.Albedo = c.rgb; o.Metallic = _ Metallic; o.Smoothness = _ Glossiness; o.Alpha = 0; / / add this line only}

The following line is important to prevent Unity from changing the custom alpha value. Change the line of # pragma to the following:

CGPROGRAM # pragma surface surf Standard fullforwardshadows keepalphs

Note that this technique is not available for deferred rendering pipelines in Unity because it overrides the Alpha channel in G-Buffer to store mask data.

When a bullet hits a surface, it dynamically generates Unity Projector at the point of impact. These Projector come with custom materials and custom shaders. The texture of the material is a splash shape with Alpha channel. The texture used in this example is as follows:

Note that Wrap Mode should be set to "Clamp" instead of "Repeat" in the texture import settings. The shader for the Projector material is modified from the ProjectorLight provided by Unity, and the code is as follows:

Shader "Projector/ProjectAlpha" {Properties {_ ShadowTex ("Cookie", 2D) = "gray" {} Subshader {Tags {"Queue" = "Transparent"} Pass {ZWrite Off Blend Zero One,One One Offset-1 -1 CGPROGRAM # pragma vertex vert # pragma fragment frag # pragma multi_compile_fog # include "UnityCG.cginc" struct Input {float4 vertex: LagPosition Float3 normal: NORMAL;}; struct v2f {float4 uvShadow: TEXCOORD0; UNITY_FOG_COORDS (2) float4 pos: SV_POSITION; fixed nv: COLOR0;}; float4x4 unity_Projector Float4x4 unity_ProjectorClip; v2f vert (Input v) {v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.uvShadow = mul (unity_Projector, v.vertex); UNITY_TRANSFER_FOG (o, o.pos) Float3 normView = normalize (float3 (unity_Projector [2] [0], unity_Projector [2] [1], unity_Projector [2] [2])); float nv = dot (v.normal, normView); o.nv = nv < 0? 1: 0; return o;} sampler2D _ ShadowTex Sampler2D _ FalloffTex; fixed4 frag (v2f I): COLOR {fixed4 texS = tex2DProj (_ ShadowTex, UNITY_PROJ_COORD (i.uvShadow)); fixed4 res = fixed4 UNITY_APPLY_FOG_COLOR (i.fogCoord, res, fixed4); return res;} EDNCG}}

Again, the part about mixing:

When a shader calculates the color of a pixel, the color must act on the pixel color that already exists at that point on the screen. By default, the new pixel completely covers the original pixel, but the new pixel can also be mixed with the original pixel. Mixing is usually used to make objects transparent or translucent, but it can also achieve many other cool special effects.

The keyword Blend can be included in a Subshader or Pass tag, or even mixed with different Pass of the same shader. After you add the Blend keyword, you must write the mixing factor. The mixing factors are as follows:

Src points to the color used by the shader to calculate. Dst points to the pixel color that is already on the screen. The color used by the shader to calculate is multiplied by the first factor, while the color already on the screen is multiplied by the second factor. Add the two results to the final color written to the screen.

So "Blend SrcAlpha One" multiplies its alpha value by the color calculated by the current shader, and the color on the screen remains unchanged. Then add the result of the screen color calculation to the former. You can also use a comma to separate two sets of factors, the blending option before the comma is used to calculate the color, and the blending option after the comma calculates only the Alpha channel. You can check out the Unity documentation to learn more about mixing.

The shader for Projector is "Blend Zero One, One One". "Zero One" removes the color of the spatter texture and uses the surface color on which the bullet is spattered. "One One" adds the alpha value of the splash to the surface alpha value.

Now that you use the shaders and materials above to generate Projector, you should set the Alpha channel of the scene view to white.

Now you can modify the Alpha channel at will, but the final effect has not yet been achieved. Let's use the Alpha mask to create the image effects needed for the game.

First, create a shader that you want to use image effects. Create a new default Image Effect Shader in Unity and replace the snippet code with the following:

Fixed4 frag (v2f I): SV_Target {fixed4 col = tex2D (_ MainTex, i.uv); fixed3 bnw = dot (col.rgb, float3); col.rgb = lerp (bnw, col.rgb, col.a); return col;}

You can change the bnw (Black&White) variable at will to achieve the desired blending effect. Finally, you need to create a new script to run the image effect. The script is very simple and the code is as follows:

Using System.Collections; using System.Collections.Generic; using UnityStandardAssets.ImageEffectBase; [ExecuteInEditMode] [ImageEffectAllowedInSceneView] public class ALphaColorSwitch: ImageEffectBase {void OnRenderImage (RenderTexture source, RenderTexture destination) {Graphics.Blit (source, destination, material);}}

Note that ImageEffectBase is used here, and this resource is in the Unity standard repository (Post Processing Stack repository is recommended for Unity 5.5 and above instead of ImageEffects). After importing the standard asset library, bind the script to the camera (make sure the camera's rendering mode is set to Forward) and set the common shader variable to the shader mentioned earlier.

At this point, you can spray ink into the scene!

The answer to the question about the realization of Unity inkjet effect Shader is shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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