TextureChange.shader
Shader "TextureChange" { Properties { _Blend ("Blend", Range (0, 1) ) = 0.5 _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Texture 1", 2D) = "white" {} _Texture2 ("Texture 2", 2D) = "" _BumpMap ("Normalmap", 2D) = "bump" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 300 Pass { SetTexture[_MainTex] SetTexture[_Texture2] { ConstantColor (0,0,0, [_Blend]) Combine texture Lerp(constant) previous } } CGPROGRAM #pragma surface surf Lambert sampler2D _MainTex; sampler2D _BumpMap; fixed4 _Color; sampler2D _Texture2; float _Blend; struct Input { float2 uv_MainTex; float2 uv_BumpMap; float2 uv_Texture2; }; void surf (Input IN, inout SurfaceOutput o) { fixed4 t1 = tex2D(_MainTex, IN.uv_MainTex) * _Color; fixed4 t2 = tex2D (_Texture2, IN.uv_MainTex) * _Color; o.Albedo = lerp(t1, t2, _Blend); o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); } ENDCG } FallBack "Diffuse" } |
object c# script
using UnityEngine; using System.Collections; public class Sh1 : MonoBehaviour { Texture tx1; Texture tx2; Texture newTx; bool triggerChange = false; float blendRange = 1.0f; void Start () { //texture load tx1 = Resources.Load("img") as Texture; print(tx1); tx2 = Resources.Load("backHD") as Texture; print(tx2); gameObject.renderer.material.SetFloat("_Blend" , blendRange);//initialize shader } public void changeTexture(int arg) { if(arg == 0) newTx = tx1; else if(arg ==1) newTx = tx2; gameObject.renderer.material.mainTexture = newTx; triggerChange = true; } private int testC = 0; void Update () { if(triggerChange == true) { blendRange = blendRange - 0.01f; //change speed gameObject.renderer.material.SetFloat("_Blend" , blendRange); if(blendRange <= 0) { triggerChange = false; blendRange = 1.0f; gameObject.renderer.material.SetTexture("_Texture2" , newTx); gameObject.renderer.material.SetFloat("_Blend" , 1); print("changeComplete"); } } if(Input.GetKeyDown(KeyCode.A)) { print("change!"); changeTexture(testC % 2); ++testC; } gameObject.transform.Rotate(Vector3.up , 1f); } } |
유니티에서 새로운 쉐이더를 에셋에 추가 하고 위의 쉐이더를 작성한 다음 해당 오브젝트로 드래그 한다.
그럼 적용 된다. 그다음 c# 본문 에서처럼 프로퍼티에 접근이 가능하다.
A 키를 누르면 번갈아서 텍스쳐가 교체된다.
출처 : http://www.sundh.com
'unity3d' 카테고리의 다른 글
웹캠의 활용, 캡처 뜨기 (0) | 2013.07.29 |
---|---|
Shader 의 적용부터 프로퍼티 접근까지 (2) | 2013.07.29 |
OGV 변환 하기 (3) | 2013.07.17 |
Custom Plane (3) | 2013.04.21 |
unity3d 의 다이나믹 텍스트 렌더링 방법 (1) | 2013.04.20 |
두개의 텍스쳐를 자연그럽게 교체하기 (0) | 2013.04.20 |