Skip to content

Instantly share code, notes, and snippets.

@sugi-cho
Created May 26, 2019 15:10
Show Gist options
  • Save sugi-cho/0863fe97fe40fe0e80ac9f15131dbec1 to your computer and use it in GitHub Desktop.
Save sugi-cho/0863fe97fe40fe0e80ac9f15131dbec1 to your computer and use it in GitHub Desktop.
Texture2DArray from sequential images. script and shader
Shader "Unlit/Tex2DArrayPlayer"
{
Properties
{
_Prop ("fps, Slices", Vector) = (84, 30, 0,0)
}
SubShader
{
Tags {"Queue" = "Overlay" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
LOD 100
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
UNITY_DECLARE_TEX2DARRAY(_Tex);
half4 _Prop;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
half4 frag (v2f i) : SV_Target
{
int idx = (_Time.y * _Prop.x) % _Prop.y;
half4 col = UNITY_SAMPLE_TEX2DARRAY(_Tex, float3(i.uv, idx));
return col;
}
ENDCG
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Texture2DArrayGenerator : MonoBehaviour
{
public string tex2dsPath = "HitoGuidArray";
public Texture2DArray tex2dArray;
public Renderer[] targetRenderers;
public string propName = "_MainTex";
// Start is called before the first frame update
void Start()
{
var tex2ds = Resources.LoadAll<Texture2D>(tex2dsPath);
var w = tex2ds[0].width;
var h = tex2ds[0].height;
var d = tex2ds.Length;
var format = tex2ds[0].format;
tex2dArray = new Texture2DArray(w, h, d, format, 1 < tex2ds[0].mipmapCount);
tex2ds = tex2ds.OrderBy(tex => tex.name).ToArray();
for (var i = 0; i < d; i++)
Graphics.CopyTexture(tex2ds[i], 0, tex2dArray, i);
var mpb = new MaterialPropertyBlock();
foreach(var r in targetRenderers)
{
r.GetPropertyBlock(mpb);
mpb.SetTexture(propName, tex2dArray);
r.SetPropertyBlock(mpb);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment