텍스처를 매번 올리는 작업을 Texture2DArray 를 만들어 여러장의 택스처를 한번에 전송을 할수 있는 기능.
CPU의 부담도 줄어 준다고 합니다.
최고 512 개까지 저장하는 구조체
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Sample2DArrayTexture : MonoBehaviour
{
public Texture2D texture2D_1;
public Texture2D texture2D_2;
public Texture2D texture2D_3;
public Texture2D texture2D_4;
public Texture2DArray texture2DArray;
public Material material;
public float slices = 10;
// Start is called before the first frame update
void Start()
{
texture2DArray = new Texture2DArray(512, 512, 4, TextureFormat.RGB24, false);
var colors = texture2D_1.GetPixels();
Debug.Log($" {colors.Length}");
}
// Update is called once per frame
void Update()
{
slices += Time.smoothDeltaTime;
material.SetFloat("_SliceRange", slices);
if (slices > 5)
slices = 0;
}
public void OnClick1()
{
texture2DArray.SetPixels(texture2D_1.GetPixels(0), 0, 0);
texture2DArray.SetPixels(texture2D_2.GetPixels(0), 1, 0);
texture2DArray.SetPixels(texture2D_3.GetPixels(0), 2, 0);
texture2DArray.SetPixels(texture2D_4.GetPixels(0), 3, 0);
texture2DArray.Apply();
material.SetTexture("_MyArr", texture2DArray);
}
public void OnClick2()
{
texture2DArray.SetPixels(texture2D_4.GetPixels(0), 0, 0);
texture2DArray.SetPixels(texture2D_3.GetPixels(0), 1, 0);
texture2DArray.SetPixels(texture2D_2.GetPixels(0), 2, 0);
texture2DArray.SetPixels(texture2D_1.GetPixels(0), 3, 0);
texture2DArray.Apply();
material.SetTexture("_MyArr", texture2DArray);
}
}
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Example/Sample2DArrayTexture"
{
Properties
{
_MyArr ("Tex", 2DArray) = "" {}
_SliceRange ("Slices", Range(0,5)) = 0
_UVScale ("UVScale", Float) = 1.0
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma require 2darray
#include "UnityCG.cginc"
struct v2f
{
float3 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float _SliceRange;
float _UVScale;
v2f vert (float4 vertex : POSITION)
{
v2f o;
o.vertex = UnityObjectToClipPos(vertex);
o.uv.xy = (vertex.xy + 0.5) * _UVScale;
o.uv.z = _SliceRange - 0.5;
return o;
}
UNITY_DECLARE_TEX2DARRAY(_MyArr);
half4 frag (v2f i) : SV_Target
{
return UNITY_SAMPLE_TEX2DARRAY(_MyArr, i.uv);
}
ENDCG
}
}
}
'Shader > Shader 함수들' 카테고리의 다른 글
기본 정리중. (0) | 2020.05.01 |
---|