[OpenGL] Shader instance analysis (7)-Snowflake falling effect, shader instance analysis

Source: Internet
Author: User

[OpenGL] Shader instance analysis (7)-Snowflake falling effect, shader instance analysis

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

After studying the effect of falling snow, I feel very good. I will share it with you. The effect is as follows:


The Code is as follows:

Shader "shadertoy/Flakes" {// https://www.shadertoy.com/view/4d2xzcpropertiesjavasimouse ("Mouse Pos", Vector) = (100,100,) iChannel0 ("iChannel0", 2D) = "white" {} iChannelResolution0 ("iChannelResolution0", Vector) = (100,100,)} CGINCLUDE # include "UnityCG. cginc "# pragma target 3.0 # pragma glsl # define vec2 float2 # define vec3 float3 # define vec4 float4 # define mat2 float2x2 # define iGlobalTime _ Time. y # define mod fmod # define mix lerp # define atan atan2 # define fract frac # define texture2D tex2D // screen size # define iResolution _ ScreenParams // coordinates on the screen, in pixel # define gl_FragCoord (_ iParam. srcPos. xy/_ iParam. srcPos. w) * _ ScreenParams. xy) # define PI2 6.28318530718 # define pi 3.14159265358979 # define halfpi (pi * 0.5) # define oneoverpi (1.0/pi) fixed4 iMouse; sampler2D iChannel0; fixed4 iChannelResolution0; struct v2f {float4 pos: SV_POSITION; float4 srcPos: TEXCOORD0 ;}; // precision highp float; v2f vert (appdata_base v) {v2f o; o. pos = mul (UNITY_MATRIX_MVP, v. vertex); o. srcPos = ComputeScreenPos (o. pos); return o;} vec4 main (v2f _ iParam); fixed4 frag (v2f _ iParam): COLOR0 {return main (_ iParam);} vec4 main (v2f _ iParam) {vec2 p = gl_FragCoord.xy/iResolution. xy; vec3 col = vec3 (150, 0); float dd =; for (int I = 0; I <dd; I ++) {float an = 6.2831 * float (I)/dd; vec2 of = vec2 (cos (an), sin ()) * (1.0 + 0.6 * cos (7.0 * an + iGlobalTime) + vec2 (0.0, iGlobalTime); col = max (col, texture2D (iChannel0, p + 20 * of/iResolution. xy ). xyz); col = max (col, texture2D (iChannel0, p + 5.0 * of/iResolution. xy ). xyz);} col = pow (col, vec3 (1.0, 2.0, 3.0) * pow (4.0 * p. y * (1.0-p. y), 0.2); return vec4 (col, 1.0);} ENDCG SubShader {Pass {CGPROGRAM # pragma vertex vert # pragma fragment frag # pragma fragmentoption implements ENDCG} FallBack Off}
Code Analysis:
1) The algorithm for drawing the Pentagon snowflakeThe Code is as follows:

float dd = 150;for( int i=0; i<dd; i++ ){float an = 6.2831*float(i)/dd;vec2  of = vec2( cos(an), sin(an) ) * (1.0+0.6*cos(7.0*an+iGlobalTime)) + vec2( 0.0, iGlobalTime );col = max( col, texture2D( iChannel0, p + 20*of/iResolution.xy ).xyz );col = max( col, texture2D( iChannel0, p +  5.0*of/iResolution.xy ).xyz );}
Before understanding this code, first understand how to draw a circle. The Code is as follows:

float dd = 30;for( int i=0; i<dd; i++ ){float an = 6.2831*float(i)/dd;vec2  of = vec2( cos(an), sin(an) );col = max( col, texture2D( iChannel0, p + 20*of/iResolution.xy ).xyz );}
Then prepare a texture with a white pixel in the middle and black surrounding it.


The effect is as follows:


This code is in the fragment shader, which means that the above algorithm is performed at each point on the screen. The details are as follows: traverse the points around the point in the texture (the above Code is the point on the circle 20 units from the point), and use the brightest of the surrounding points as the color of the point. The above texture is a bit special. Only one point is white and the rest is black. Then, only a point of 20 units away from this point will become bright, and the remaining points will be black, as shown in the result. Summarize the effects of the above algorithm in one sentence:A specific image is generated around each "relative highlight" in the texture. The brightness of the image depends on the brightness of the point.. For the effect, refer to the image at the end of the article.

Next we will understand this Code:

float dd = 150;for( int i=0; i<dd; i++ ){float an = 6.2831*float(i)/dd;vec2  of = vec2( cos(an), sin(an) ) * (1.0+0.7*cos(7.0*an));col = max( col, texture2D( iChannel0, p + 20*of/iResolution.xy ).xyz );//col = max( col, texture2D( iChannel0, p +  5.0*of/iResolution.xy ).xyz );}
The output result is as follows:


A) the image of 1.0 + 0.7 * cos (7.0 * an) is as follows:


B) In the algorithm, the path of the vector is:


The results are clear. In fact, the algorithm here is similar to the algorithm used to draw a Heart shape in [OpenGL] Shader instance analysis (II)-Heart.

The animation can be implemented by adding time:

vec2  of = vec2( cos(an), sin(an) ) * (1.0+0.6*cos(7.0*an+iGlobalTime)) + vec2( 0.0, iGlobalTime );
The first iGlobalTime is used to control the rotation of the snowflake, and the second iGlobalTime is used to make the snowflake fall.
2) Subsequent Color Processing

It can be understood as a kind of postEffect processing, specifically the effect contributed by the following code:

col = pow( col, vec3(1.0,2.0,3.0) ) * pow( 4.0*p.y*(1.0-p.y), 0.2);

A) pow (col, vec3 (1.0, 2.0, 3.0) makes the color warm. The col value ranges from 0 to 1. The pow operation is performed on decimal places. The higher the number of times, the smaller the value. For example, the power of 0.5 is 0.5, the power of 2 is 0.25, and the power of 3 is 0.125. So the role of this sentence is obvious: the red component remains unchanged, and the green value becomes smaller, blue is smaller. The effect, so that the overall color will be warm.

B) pow (4.0 * p. y * (1.0-p. y), 0.2) dimmed both sides of the screen.


Finally, attach the texture used in the shader:


After the program is processed, the following result is obtained:


The article is complete. Welcome to discuss it.


RenderMonkey 182 opengl tutorial. Now I want to write shader and do some 3D effects.

Find this book Shaders. for. Game. Programmers. and. Artists, which is available online.

What is the difference between OpenGL 21 and Shader Moder 41? Is it true that all graphics cards support both of these technologies?

The shader model 4.1 refers to the 4.1 version of the shadow. Only the graphics card that supports Microsoft's graphic interface DirectX10.1 supports the 4.1 version of the player. The HD3000 and 4000 Series of ATI are supported, but the GeForce series are not yet supported, because the current games basically only use the 4.0 version of the coloring tool, it only supports DX10.0, therefore, it is not necessary to support DX10.1. OpenGL (fully write Open Graphics Library) is a specification that defines a cross-programming language and cross-platform programming interface, it is used for three-dimensional images (two-dimensional images can also be used ). OpenGL is a professional graphical program interface and is a powerful underlying graphics library with convenient calling. OpenGL's predecessor was iris gl developed by SGI for its graphics workstation. Iris gl is an industrial standard 3D graphics software interface, which is powerful but portable, so SGI developed OpenGL Based on iris gl. The full name of OpenGL is "Open Graphics Library". As the name suggests, OpenGL is "Open graphical program interface ".
DirectX leads the market in the home market, but OpenGL cannot be replaced in the field of professional and high-end graphics.
The OpenGL function depends on your daily needs ~~ Of course there should be something better than none ~

Related Article

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.