Unity Spin warp image effects

Source: Internet
Author: User

Spin-twist effects are those that distort the rendered image within a circular area, and the degree of rotation of other pixels varies with distance. This can be achieved by modifying the shader.

Original picture

Distort picture

/*==================================================== screen Twist effect shader ====================================

    ==================*/Shader "Hidden/twirleffects" {Properties {_maintex ("Texture", 2D) = "White" {}
            } subshader {//No culling or depth cull off zwrite off ZTest always Pass { Cgprogram #pragma vertex vert #pragma fragment frag #include "unitycg.cginc
            "Uniform sampler2d _maintex;
            Uniform FLOAT4 _maintex_texelsize;

            Half4 _maintex_st;
            Rotating Twisted Center uniform float4 _centerradius;

            The rotation matrix is passed into uniform float4x4 _rotationmatrix;
                struct AppData {float4 vertex:position;
            FLOAT2 uv:texcoord0;

            };
                struct V2F {float2 uv:texcoord0; FLOAT4 Vertex:sv_position;

            };
                v2f Vert (AppData v) {v2f o;
                O.vertex = Mul (UNITY_MATRIX_MVP, V.vertex);
                Transform UV coordinates into the center coordinate system O.UV = V.UV-_centerradius.xy;
            return o;
                } fixed4 Frag (v2f i): sv_target {float2 offest = I.UV;

                Rotate UV Float2 Distortedoffset = MULTIPLYUV (_rotationmatrix,offest.xy) with rotation matrix;
                Calculates the position of the UV point in the rotating circle float2 tmp = offest/_centerradius.zw;

                float t = min (1,length (TMP));

                Offest =lerp (DISTORTEDOFFSET,OFFEST,T) based on the position of the UV point in the circle where the UV moves are interpolated; 

                The UV coordinates are returned in the original coordinate system offest + = _centerradius.xy;

                Fixed4 col = tex2d (_maintex, Unitystereoscreenspaceuvadjust (Offest, _maintex_st));
            return col; } ENDCG}}}

This rotation effect is mainly to offset the UV value of the image, the key code

                Float2 offest = I.UV;
                Rotate UV
                Float2 distortedoffset = MULTIPLYUV (_rotationmatrix,offest.xy) with rotation matrix;

                Calculates the position of the UV point in the rotating circle
                float2 tmp = offest/_centerradius.zw;
                Float  t = min (1,length (TMP));
                offest =lerp (distortedoffset,offest,t) based on the position of the UV point in the circle where the UV moves are interpolated;

                Returns the UV coordinates in the original coordinate system
                

The image is distorted based on the position of the UV point.

Here is the source code of the script

using system.collections; using System.Collections.Generic; using Unityengine; 

    public class Twirlscripts:monobehaviour {[Executeineditmode] public Vector2 radius = new Vector2 (0.3f, 0.3f);

    Public Vector2 Center = new Vector2 (0.5f, 0.5f);

    [Range (0.0f, 360.0f)] public float angle = 0.0f;

    Public Material Material; private void Onrenderimage (rendertexture source, rendertexture destination) {matrix4x4 Rotationmatrix = Matr ix4x4.

        TRS (Vector3.zero, quaternion.euler (0, 0, Angle), vector3.one); Material.
        SetMatrix ("_rotationmatrix", Rotationmatrix); Material.

        Setvector ("_centerradius", New Vector4 (Center.x, Center.y, Radius.x, Radius.y));

    Graphics.blit (source, destination, material); }

}

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.