본문 바로가기

unity3d

[Shader] Circle Transition + MousePosition



* 마우스를 클릭한 지점부터 Spread 됨



b2 (2).zip




c#

using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour {

    public Material circle_transition;
    public GameObject car;
    public Texture main_tex;
    public Texture sub_tex;

    private Texture currentTex;

	void Start () {
        circle_transition.SetTexture("_MainTex", main_tex);
        circle_transition.SetTexture("_SubTex", sub_tex);
        QualitySettings.SetQualityLevel(5);
	}

    float Progress = 2.74f;
    float Fuzzy = -1.99f;
    float center_x = 0.5f;
    float center_y = 0.85f;
    void OnGUI()
    {
        RangeComm("Progress", 0, ref Progress, 2.74f, 4f);
        RangeComm("Fuzzy", 30, ref Fuzzy, -1.99f, -1.5f);
        RangeComm("center_x", 60, ref center_x, 0, 1);
        RangeComm("center_y", 90, ref center_y, 0, 1);
    }

    Vector4 position = new Vector4();
    Vector4 ray_position = new Vector4();
	void Update () {

        car.transform.Rotate(0, 0, 0.1f);

        position.x = center_x;
        position.y = center_y;
        circle_transition.SetFloat("_Progress", Progress);
        circle_transition.SetFloat("_FuzzyAmount", Fuzzy);
        //circle_transition.SetVector ("_Center", position);
        circle_transition.SetFloat("_CircleSize", -6.87f);

        if(Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit ;
            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                ray_position.x = hit.textureCoord.x;
                ray_position.y = hit.textureCoord.y;
                circle_transition.SetVector("_Center", ray_position);

                StartProgressive();
            }
        }

	}

    private iTween prevTween = null;
    private void StartProgressive()
    {
        if (prevTween != null)
            prevTween.dispose();

        prevTween = XTween.To(
            2.74f,
            4.0f,
            3.0f,
            0.0f,
            iTween.EaseType.linear,
            delegate(float v)
            {
                circle_transition.SetFloat("_Progress", v);
            }, 
            delegate
            {
                ChageTextures();
            },
            delegate {
                Debug.Log("dispose!");
                ChageTextures();
            }
            );
    }

    private void ChageTextures()
    {
        Texture2D getMainTex = circle_transition.GetTexture("_MainTex") as Texture2D;
        Texture2D getSubTex = circle_transition.GetTexture("_SubTex") as Texture2D;
        circle_transition.SetTexture("_MainTex", getSubTex);
        circle_transition.SetTexture("_SubTex", getMainTex);
    }

    Texture2D TextureClone(Texture2D org_tex)
    {
        Texture2D cloneTex = new Texture2D(org_tex.width, org_tex.height);
        var color = org_tex.GetPixels(0, 0, org_tex.width, org_tex.height);
        cloneTex.SetPixels(color);
        cloneTex.Apply();
        return cloneTex;
    }

    private float RangeComm(string name , float position_y , ref float v ,float min = 0.0f, float max = 1.0f)
    {
        GUI.Label(new Rect(0, position_y, 40, 20), name);
        v = GUI.HorizontalSlider(new Rect(40, position_y, 160, 20), v, min, max);
        return v;
    }
}







shader 


 

Shader "Custom/Circle_Transition_Position" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_SubTex("sub tex" , 2D) = "white"{}
		_Progress("progress" , float) = 0
		_FuzzyAmount("fuzzy" , float) = 0
		_CircleSize("circle size" , float) = 1
		_Center("center xy" , vector) = (1,1,1,1)
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;
		sampler2D _SubTex;
		float _Progress;
		float _FuzzyAmount;
		float _CircleSize;
		float4 _Center;
		struct Input {
			float2 uv_MainTex;
			float4 Color;
			float3 worldPos;
		};

		void surf (Input IN, inout SurfaceOutput o) {
					
			float radius = - _FuzzyAmount + _Progress + (_CircleSize + 2.0 + _FuzzyAmount);
			float fromCenter = length(IN.uv_MainTex - _Center);//distance(IN.uv_MainTex , _Center);
			float distFromCircle = fromCenter - radius;
			float4 c1 = tex2D(_MainTex , IN.uv_MainTex);
			float4 c2 = tex2D(_SubTex , IN.uv_MainTex);
			float p = saturate((distFromCircle +  _FuzzyAmount)/(2.0 + _FuzzyAmount));
			
			float4 result = lerp(c2 , c1 , p);
			
			half4 c = result;//(1,1,1,1);//tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}