Texture Change

µÎÀåÀÇ ÅؽºÃĸ¦ ºÎµå·´°Ô ¹Ù²ãº¸ÀÚ.

¼ÎÀÌ´õ ÄÚµå

µÎÀåÀÇ ÅؽºÃÄ _Texture1, _Texture2¸¦ _Blend °ªÀ» ÀÌ¿ëÇØ  Ä®¶ó°ªÀ» º¸°£ ÇÏ´Â ÄÚµåÀÌ´Ù.

Shader "Custom/TextureChange" {
Properties {
    _Blend ("Blend", Range (0, 1) ) = 0.5
    _Texture1 ("Texture 1", 2D) = "white" {}
    _Texture2 ("Texture 2", 2D) = ""
}

SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 300

        CGPROGRAM
        #pragma surface surf Lambert
       
        float _Blend;
        sampler2D _Texture1;
        sampler2D _Texture2;
           
        struct Input {
            float2 uv_Texture1;
            float2 uv_Texture2;        
        };
       
        void surf (Input IN, inout SurfaceOutput o) {
            fixed4 t1 = tex2D(_Texture1, IN.uv_Texture1);
            fixed4 t2 = tex2D (_Texture2, IN.uv_Texture2);
           
            o.Albedo = lerp(t1, t2, _Blend);
        }
       
        ENDCG 
    } //SubShader
    
    FallBack "Diffuse"
}

µÎÀåÀÇ ÅؽºÃĸ¦ lerp ÇÔ¼ö·Î Ä®¶ó °ªÀ» Á¶Á¤ÇÑ´Ù.

o.Albedo = lerp(t1, t2, _Blend)
float4 color = lerp(º£À̽º»ö»ó,  µ¡Ä¥»ö»ó, °¡ÁßÄ¡)

°¡ÁßÄ¡ 0¿¡ °¡±î¿ì¸é º£À̽º»ö»ó¿¡ °¡±õ°í 1¿¡ °¡±î¿ì¸é µ¡Ä¥»ý»ó¿¡ °¡±õ´Ù.

½ºÅ©¸³Æ® ÄÚµå

½ºÅ©¸³Æ®¿¡¼­ µÎÀåÀÇ ÅؽºÃĸ¦ ·ÎµùÇؼ­ º¯°æÇÏ°í ÀÖ´Ù.

using UnityEngine;
using System.Collections;

public class CylinderScript : MonoBehaviour {
    
    Texture tx1;
    Texture tx2;
    Texture newTx;
    bool triggerChange = false;
    float blendRange = 1f;
    
    void Start () {
        //texture load
        tx1 = Resources.Load("img") as Texture;
        print(tx1);
        
        tx2 = Resources.Load("backHD") as Texture;
        print(tx2);

        gameObject.GetComponent<Renderer>().material.SetFloat("_Blend" , blendRange);//initialize shader
    }
    
    public void changeTexture(int arg)
    {
        if(arg == 0)
            newTx = tx1;
        else if(arg ==1)
            newTx = tx2;
        
        //gameObject.GetComponent<Renderer>().material.mainTexture = newTx; ÀÌ°ÍÇÏ¸é ¾ÈµÊ..XXX
        gameObject.GetComponent<Renderer>().material.SetTexture("_Texture1" , newTx);
        triggerChange = true;
    }
    
    
    private int testC = 0;
    void Update () {
        if(triggerChange == true)
        {
            blendRange = blendRange - 0.01f; //change speed
            gameObject.GetComponent<Renderer>().material.SetFloat("_Blend" , blendRange);
            
            if(blendRange <= 0)
            {
                triggerChange = false;
                blendRange = 1f;
                gameObject.GetComponent<Renderer>().material.SetTexture("_Texture2" , newTx);
                gameObject.GetComponent<Renderer>().material.SetFloat("_Blend", 1f);
                
                print("changeComplete");
            }
        }
        
        
        if(Input.GetKeyDown(KeyCode.A))
        {
            print("change!");
            changeTexture(testC % 2);
            
            ++testC;
        }
        gameObject.transform.Rotate(Vector3.up , 1f);
    }
}

material.SetTexture("_Texture1" , newTx)·Î ÅؽºÃĸ¦ ÃʱâÈ­ ÇÑ´Ù.

'A'Å°¸¦ ´©¸£¸é ÅؽºÃĸ¦ º¯°æÇÑ´Ù.

±âÁ¸ÀÇ ÅؽºÃÄ´Â _Texture2¿¡, »õ·Î¿î ÅؽºÃÄ´Â _Texture1¿¡ ³Ö´Â´Ù.
material.SetFloat("_Blend" , blendRange)·Î ºí·£µù °ªÀ» Á¶Á¤ÇÑ´Ù.
ºí·£µù °ªÀÌ 1¿¡¼­ 0±îÁö °¨¼ÒÇϸé _Texture2¿¡¼­ _Texture1 »ö»óÀ¸·Î ¹Ù²î°Ô µÈ´Ù.

[ÂüÁ¶]
http://www.scripter.co.kr/234