Unity Shader Effect Learning

Source: Internet
Author: User
Tags mul

For image processing on unity, assume that you are using code alone. So unfortunately, the program basically will run to death, after all, directly to the pixel operation, read write is more CPU and memory.

So. This time because the project needs to achieve similar to the effect of the ha. Want to go, or think with unity shader better, after all, do not need the CPU to do anything, just use the GPU to be able.

The GPU is also very powerful.

Here is a brief talk about shader (in fact, I am also a novice, learning, reference http://blog.csdn.net/poem_qianmo/article/details/40723789

This is a Daniel, some of my basic knowledge is in the inside study.

This time I am to share a few shader, for the details of the principle of what, I will not talk about. Can not speak, just to roughly say. I am also studying.

1. Mirrors effect (enlarge)

Shader "Custom/maxface" {Properties {_maintex ("texture", 2D) = "White" {}_radius ("Radius", float) =0.2_centerx ("CenterX ", float) =0.5_centery (" CenterY ", float) =0.5}subshader {tags{" Queue "=" Transparent "" rendertype "=" Transparent "}pass{ Cgprogram#pragma vertex vert#pragma fragment Frag#include "Unitycg.cginc" Float4 _color;sampler2d _maintex;float _ Centerx;float _centery;struct v2f{float4 pos:sv_position;float2 uv:texcoord0;}; Float4 _maintex_st;v2f Vert (Appdata_base v) {v2f o;o.pos=mul (Unity_matrix_mvp,v.vertex); O.uv=transform_tex ( V.texcoord,_maintex); return o;} ; float newx=0;float newy=0;float _radius;half4 frag (v2f i): Color{float tx=i.uv.x-_centerx;float Ty=i.uv.y-_CenterY; Float distan=tx*tx+ty*ty;float real_radius=_radius/2;if (distan<_radius*_radius) {newx=tx/2;newy=ty/2;newx=newx * (sqrt (Distan)/real_radius) newy=newy* (sqrt (Distan)/real_radius); newx=newx+_centerx;newy=newy+_centery;} ELSE{NEWX=I.UV.X;NEWY=I.UV.Y;} float u_x=newx;float u_y=newy;float2 uv_earth=float2 (u_x,u_y); Half4 texcolor_earth=tex2d (_maintex,uv_earth);//return Texcolor_earth;} Endcg}}fallback "Diffuse"}

Here's an explanation, just take a look

Compile the correct words. Let's look at the effects in unity

Create a new plane and import a picture. Then drag it over to plane and select the shader we just created

To see, the following three parameters are the one we created in propertes. Pick the following number of references. Here are the effects

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Well, spoil the beauty.

The algorithm I changed from the Java inside, the details do not ask me. I am a code porter.

2, Mirrors reduction mode

I directly on the code, because basically did not change what

Shader "Custom/minface" {Properties {_maintex ("texture", 2D) = "White" {}_centerx ("CenterX", float) =0.5_centery (" CenterY ", float) =0.5}subshader {tags{" Queue "=" Transparent "" rendertype "=" Transparent "}pass{cgprogram#pragma vertex Vert#pragma fragment Frag#include "Unitycg.cginc" float4 _color;sampler2d _maintex;float _centerx;float _CenterY; struct V2F{FLOAT4 pos:sv_position;float2 uv:texcoord0;}; Float4 _maintex_st;v2f Vert (Appdata_base v) {v2f o;o.pos=mul (Unity_matrix_mvp,v.vertex); O.uv=transform_tex ( V.texcoord,_maintex); return o;} ; float newx=0;float newy=0;float _radius;float theta=0;half4 frag (v2f i): Color{float tx=i.uv.x-_centerx;float ty= I.uv.y-_centery;  theta = atan2 (Ty, TX);   Float RADIUS=SQRT (TX * tx+ ty * ty);           Float newr=sqrt (RADIUS) *0.5; Newx=_centerx+newr*cos (theta); Newy=_centery+newr*sin (theta); if (newx<0) {      newx=0;      } if (newx>1) {newx=1;      } if (newy>1) {newy=1;       } if (newy<0) {newx=0; } float2 Uv_earth=float2 (newx,newy); Half4 texcolor_earth=tex2d (_maintex,uv_earth); return Texcolor_earth;} Endcg}}fallback "Diffuse"}
Look at the effect (actually the second one is pretty cute)


Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

3. Another symmetrical one.

Shader "Custom/symmertyface" {//Symmetry effects properties {_maintex ("texture", 2D) = "White" {}}subshader {tags{"Queue" = " Transparent "" Rendertype "=" Transparent "}pass{cgprogram#pragma vertex vert#pragma fragment Frag#include" Unitycg.cginc "Float4 _color;sampler2d _maintex;float _centerx;float _centery;struct v2f{float4 pos:sv_position; Float2 uv:texcoord0;}; Float4 _maintex_st;v2f Vert (Appdata_base v) {v2f o;o.pos=mul (Unity_matrix_mvp,v.vertex); O.uv=transform_tex ( V.texcoord,_maintex); return o;} ; Half4 Frag (v2f i): Color{float uv_x;if (i.uv.x>0.5) {uv_x=1-i.uv.x;} else{uv_x=i.uv.x;}     Float2 Uv_earth=float2 (UV_X,I.UV.Y); Half4 texcolor_earth=tex2d (_maintex,uv_earth); return Texcolor_earth;} Endcg}}fallback "Diffuse"}
See Effect

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Well, even if the sister is symmetrical. It's still nice to watch (The flower crazy ing) attach the original image

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

That's all there is to it, and other special effects that are updated slowly.

Unity Shader Effect Learning

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.