[OpenGL] Shader instance analysis (9)-main character injury effect in AngryBots

Source: Internet
Author: User

Keep forwarding address: http://blog.csdn.net/stalendp/article/details/40859441

AngryBots is a great official example of Unity and has great research value. In previous studies, due to its rich content, I didn't know where to start writing and analyzing articles at a time. The shader technology has been studied more recently. Let's start with this aspect of shader. First, analyze one of the screen effects: the full screen effect (postScreenEffect) that will appear when the protagonist is attacked. The effect is as follows:

  

In fact, this is a Bloom effect. The relevant files include MobileBloom. js and MobileBloom. shader. For details about how to view these two files, see:


The related code is as follows:

MobileBloom. js code:

Function OnRenderImage (source: RenderTexture, destination: RenderTexture) {# if UNITY_EDITORFindShaders (); CheckSupport (); CreateMaterials (); # endifagonyTint = Mathf. clamp01 (agonyTint-Time. deltaTime * 2.75f); var tempRtLowA: RenderTexture = RenderTexture. getTemporary (source. width/4, source. height/4, rtFormat); var tempRtLowB: RenderTexture = RenderTexture. getTemporary (source. width/4, s Ource. height/4, rtFormat); // prepare dataapply. setColor ("_ ColorMix", colorMix); apply. setVector ("_ Parameter", Vector4 (colorMixBlend * 0.25f, 0.0f, 0.0f, 1.0f-intensity-agonyTint); // downsample & blurGraphics. bits (source, tempRtLowA, apply, agonyTint <0.5f? 1: 5); Graphics. bits (tempRtLowA, tempRtLowB, apply, 2); Graphics. BWB (tempRtLowB, tempRtLowA, apply, 3); // applyapply. setTexture ("_ Bloom", tempRtLowA); Graphics. bation (source, destination, apply, QualityManager. quality> Quality. medium? 4: 0); RenderTexture. ReleaseTemporary (tempRtLowA); RenderTexture. ReleaseTemporary (tempRtLowB );}

Knowledge Point preparation:

1) OnRenderImage function

This is a callback function that is part of the life cycle of MonoBehaviour. Each frame is called. When this function is called, all 3d rendering is complete, used to process 3d rendering results. The effect described in this article is implemented in this function.The script of this function is usually bound to Camera. This function is only available in Unity Pro.

2) Graphics. BICS function

Static void bid (Texture source, RenderTexture dest); static void Blit (Texture source, RenderTexture dest, Material mat, int pass =-1); static void Blit (Texture source, material mat, int pass =-1 );
This function is like a filter. The source image is converted into a dest image after processing, and the mat material object is responsible for processing. (More accurate description: it is implemented by binding to the shader on the mat. This shader can have multiple passes and can specify specific shader through the pass parameter, -1 indicates executing all the passes on the shader).

3) RenderTexture. GetTemporary function and RenderTexture. ReleaseTemporary function

GetTemporary gets the temporary RenderTexture (rendering result of the current frame ). ReleaseTemporary is used to release the specified RenderTexture;

In unity, the RenderTextures is pooled, so the GetTemporary function will return quickly (it is likely that the existing RenderTexture is obtained). After processing, use ReleaseTemporary to release references to this RenderTexture to reuse RenderTexture and improve performance.

With three knowledge points, the functions of the above code are very clear:

  • A) obtain the tempRtLowA and tempRtLowB pasters.(The length and width are 1/4 of the source image to accelerate rendering)
  • B) set Shader parameters in Mat.
  • C) use Mat to process the texture and render it to the destination texture for display.
  • D) release a temporary texture.

Here we will first explain a and c;

[Step a] reduces the texture size by 1/16 (both the length and width are reduced to 1/4 of the original size) to save GPU memory and increase the rendering speed. The reason for doing so is, we need to blur the processing of images, but the image requirements are not very high.

Step c](Note: Call the Bader function to filter textures. The last numeric parameter is used to refer to the shader's pass)

Pass1 or pass5: extract the brightest part of the color; pass2 blur the highlighted image horizontally; pass3 blur the highlighted image vertically; pass0 or pass4; and then overlay the blurred image to the original image.

A two-point fuzzy process is desired, and then a vertical fuzzy process is performed, as shown in (that is, an algorithm that spreads a point to the surrounding area ):


Next, we will explain the algorithm used to process textures in the Shader.

MobileBloom. shader code:

Shader "Hidden/MobileBloom" {Properties {_ MainTex ("Base (RGB)", 2D) = "white" {} _ Bloom ("Bloom (RGB)", 2D) = "black" {}} CGINCLUDE # include "UnityCG. cginc "sampler2D _ MainTex; sampler2D _ Bloom; uniform fixed4 _ ColorMix; uniform half4 _ MainTex_TexelSize; uniform fixed4 _ Parameter; # define ONE_MINUS_INTENSITY _ Parameter. wstruct v2f_simple {half4 pos: SV_POSITION; half4 uv: TEXCOORD0 ;}; struct detail {half4 pos: SV_POSITION; half2 uv: TEXCOORD0; half2 uv2 [4]: TEXCOORD1 ;}; struct v2f_withBlurCoords {half4 pos: SV_POSITION; half2 uv2 [4]: TEXCOORD0 ;}; v2f_simple vertBloom (appdata_img v) {v2f_simple o; o. pos = mul (UNITY_MATRIX_MVP, v. vertex); o. uv = v. texcoord. xyxy; # if SHADER_API_D3D9 if (_ MainTex_TexelSize.y <0.0) o. uv. w = 1.0-o. uv. w; # endifreturn o;} v2f_withMaxCoords vertMax (appdata_img v) {v2f_withMaxCoords o; o. pos = mul (UNITY_MATRIX_MVP, v. vertex); o. uv = v. texcoord; o. uv2 [0] = v. texcoord + _ MainTex_TexelSize.xy * half2 (1.5, 1.5); o. uv2 [1] = v. texcoord + _ MainTex_TexelSize.xy * half2 (-1.5, 1.5); o. uv2 [2] = v. texcoord + _ MainTex_TexelSize.xy * half2 (-1.5,-1.5); o. uv2 [3] = v. texcoord + _ MainTex_TexelSize.xy * half2 (1.5,-1.5); return o;} v2f_withBlurCoords vertBlurVertical (appdata_img v) {v2f_withBlurCoords o; o. pos = mul (UNITY_MATRIX_MVP, v. vertex); o. uv2 [0] = v. texcoord + _ MainTex_TexelSize.xy * half2 (0.0,-1.5); o. uv2 [1] = v. texcoord + _ MainTex_TexelSize.xy * half2 (0.0,-0.5); o. uv2 [2] = v. texcoord + _ MainTex_TexelSize.xy * half2 (0.0, 0.5); o. uv2 [3] = v. texcoord + _ MainTex_TexelSize.xy * half2 (0.0, 1.5); return o;} v2f_withBlurCoords vertBlurHorizontal (appdata_img v) {v2f_withBlurCoords o; o. pos = mul (UNITY_MATRIX_MVP, v. vertex); o. uv2 [0] = v. texcoord + _ MainTex_TexelSize.xy * half2 (-1.5, 0.0); o. uv2 [1] = v. texcoord + _ MainTex_TexelSize.xy * half2 (-0.5, 0.0); o. uv2 [2] = v. texcoord + _ MainTex_TexelSize.xy * half2 (0.5, 0.0); o. uv2 [3] = v. texcoord + _ MainTex_TexelSize.xy * half2 (1.5, 0.0); return o;} fixed4 fragBloom (v2f_simple I): COLOR {fixed4 color = tex2D (_ MainTex, I. uv. xy); return color + tex2D (_ Bloom, I. uv. zw);} fixed4 fragBloomWithColorMix (v2f_simple I): COLOR {fixed4 color = tex2D (_ MainTex, I. uv. xy); half colorDistance = Luminance (abs (color. rgb-_ ColorMix. rgb); color = lerp (color, _ ColorMix, (_ Parameter. x * colorDistance); color + = tex2D (_ Bloom, I. uv. zw); return color;} fixed4 fragMaxWithPain (v2f_withMaxCoords I): COLOR {fixed4 color = tex2D (_ MainTex, I. uv. xy); color = max (color, tex2D (_ MainTex, I. uv2 [0]); color = max (color, tex2D (_ MainTex, I. uv2 [1]); color = max (color, tex2D (_ MainTex, I. uv2 [2]); color = max (color, tex2D (_ MainTex, I. uv2 [3]); return saturate (color + half4 (0.25, 0, 0)-ONE_MINUS_INTENSITY);} fixed4 fragMax (v2f_withMaxCoords I ): COLOR {fixed4 color = tex2D (_ MainTex, I. uv. xy); color = max (color, tex2D (_ MainTex, I. uv2 [0]); color = max (color, tex2D (_ MainTex, I. uv2 [1]); color = max (color, tex2D (_ MainTex, I. uv2 [2]); color = max (color, tex2D (_ MainTex, I. uv2 [3]); return saturate (color-ONE_MINUS_INTENSITY);} fixed4 fragBlurForFlares (v2f_withBlurCoords I): COLOR {fixed4 color = tex2D (_ MainTex, I. uv2 [0]); color + = tex2D (_ MainTex, I. uv2 [1]); color + = tex2D (_ MainTex, I. uv2 [2]); color + = tex2D (_ MainTex, I. uv2 [3]); return color * 0.25 ;} else {ZTest Always Cull Off ZWrite Off Blend Off Fog {Mode off} // 0 Pass {CGPROGRAM # pragma vertex vertBloom # pragma fragment fragBloom # pragma fragmentoption implements ENDCG}/1 Pass {CGPROGRAM # pragma vertex vertMax # pragma fragment fragMax # pragma vertex ENDCG} // 2 Pass {CGPROGRAM # pragma vertex #pragma fragment #pragma vertex ENDCG}/3 Pass {CGPROGRAM # pragma vertex variable # pragma fragment variable # pragma vertex ENDCG} // 4 Pass {CGPROGRAM # pragma vertex vertBloom # pragma fragment variable # pragma vertex ENDCG}/5 Pass {CGPROGRAM # pragma vertex vertMax # pragma fragment fragMaxWithPain # pragma fragmentoption ARB_precision_hint_fastest ENDCG} FallBack Off}



Official example AngryBots URL: http://u3d.as/content/unity-technologies/angry-bots/5CF

Unit Shaders and Effects Cookbook:

Chapter 10 Screen Effects with Unity Render Textures

Chapter 11 Gameplay and Screen Effects

[GPU Gems] Real-Time Glow: http://http.developer.nvidia.com/GPUGems/gpugems_ch21.html

[OpenGL] Shader instance analysis (9)-main character injury effect in AngryBots

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.