-
-
Save smkplus/a4185a0f92727a269bf15e6aae81ddaf to your computer and use it in GitHub Desktop.
Texture2D Array example, see https://docs.unity3d.com/ScriptReference/Texture2DArray.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Used to generate Texture Array asset | |
// Menu button is available in GameObject > Create Texture Array | |
// See CHANGEME in the file | |
using UnityEngine; | |
using UnityEditor; | |
public class TextureArray : MonoBehaviour { | |
[MenuItem("GameObject/Create Texture Array")] | |
static void Create() | |
{ | |
// CHANGEME: Filepath must be under "Resources" and named appropriately. Extension is ignored. | |
// {0:000} means zero padding of 3 digits, i.e. 001, 002, 003 ... 010, 011, 012, ... | |
string filePattern = "Smoke/smoke_{0:000}"; | |
// CHANGEME: Number of textures you want to add in the array | |
int slices = 24; | |
// CHANGEME: TextureFormat.RGB24 is good for PNG files with no alpha channels. Use TextureFormat.RGB32 with alpha. | |
// See Texture2DArray in unity scripting API. | |
Texture2DArray textureArray = new Texture2DArray(256, 256, slices, TextureFormat.RGB24, false); | |
// CHANGEME: If your files start at 001, use i = 1. Otherwise change to what you got. | |
for (int i = 1; i <= slices; i++) | |
{ | |
string filename = string.Format(filePattern, i); | |
Debug.Log("Loading " + filename); | |
Texture2D tex = (Texture2D)Resources.Load(filename); | |
textureArray.SetPixels(tex.GetPixels(0), i, 0); | |
} | |
textureArray.Apply(); | |
// CHANGEME: Path where you want to save the texture array. It must end in .asset extension for Unity to recognise it. | |
string path = "Assets/Tasmania/Resources/SmokeTextureArray.asset"; | |
AssetDatabase.CreateAsset(textureArray, path); | |
Debug.Log("Saved asset to " + path); | |
} | |
} | |
// After this, you will have a Texture Array asset which you can assign to the shader's Tex attribute! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Directly taken from Unity doco | |
Shader "Example/Sample2DArrayTexture" | |
{ | |
Properties | |
{ | |
_MyArr("Tex", 2DArray) = "" {} | |
_SliceRange("Slices", Range(1,24)) = 0 | |
_UVScale("UVScale", Float) = 1.0 | |
} | |
SubShader | |
{ | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
// to use texture arrays we need to target DX10/OpenGLES3 which | |
// is shader model 3.5 minimum | |
#pragma target 3.5 | |
#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 = mul(UNITY_MATRIX_MVP, vertex); | |
o.uv.xy = (vertex.xy + 0.5) * _UVScale; | |
o.uv.z = (vertex.z + 0.5) * _SliceRange; | |
return o; | |
} | |
UNITY_DECLARE_TEX2DARRAY(_MyArr); | |
half4 frag(v2f i) : SV_Target | |
{ | |
return UNITY_SAMPLE_TEX2DARRAY(_MyArr, i.uv); | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment