Hi everyone,
I stuck currently to create Texture2Darray with a compresses format.
The reason is
Texture2DArray array = new Texture2DArray(textures[0].width, textures[0].height, textures.Length, textures[0].format, true);
Array. provide only SetPixels and SetPixels32 and these both requires RGB and RGBA format only. But not BC7/DXT5 or the newer compression typ.
Does any know how to build a compressed Texture2DArray and provide some lines of code please?!
Thanks a lot!
With this small example you’re able to select a brunch of textures and save them as Texture2DArray asset to the same directory in Unity. Drag and drop the file was created to the Tex2DArray shader slot myArr.
using UnityEngine;
using UnityEditor;
using System.IO;
public class EditorExportAsset {
[MenuItem("Assets/Build : Texture2DArray Asset From Selection")]
static void ExportResource() {
// Selection is unsorted and does not reflect the sort of the displayed filenames of unity
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
if (selection.Length > 0) {
// Redirect to the Path of the first selected object
string pathfull = new FileInfo(AssetDatabase.GetAssetPath(selection[0])).FullName;
// FileInfo reversed IBM format back to Unity
pathfull = pathfull.Replace("\\", "/");
string name = selection[0].name.ToLower();
var pathfull_l = pathfull.ToLower();
pathfull = pathfull.Substring(0, pathfull_l.LastIndexOf("/"+name));
// Debug.Log(pathfull + " " + pathfull_l.LastIndexOf("/" + name) + " " +name) ;
// Bring up save panel
pathfull = EditorUtility.SaveFilePanel("Save Resource Texture2DArray", pathfull, "Texture2DArray", "asset");
// Create relative Path for AssetDatabase.Save
pathfull_l = pathfull.ToLower();
pathfull = pathfull.Substring(pathfull_l.IndexOf("/assets/") + 1);
if (pathfull.Length != 0) {
// Build the resource file from the active selection.
Texture2D[] textures = new Texture2D[selection.Length];
//Debug.Log(textures[0].format);
for (int i = 0; i < textures.Length; i++) {
textures[i] = (Texture2D)selection[i];
}
Texture2DArray array = new Texture2DArray(textures[0].width, textures[0].height, textures.Length, textures[0].format, true);
// Graphics.CopyTexture() ??
for (int i = 0; i < textures.Length; i++)
array.SetPixels32(textures[i].GetPixels32(), i);
array.Apply();
AssetDatabase.CreateAsset(array, pathfull);
}
} else {
Debug.LogWarning("# Build Texture2DArray Asset From Selection : No Selection ");
}
}
}
Shader "Example/TextureArrayShader" {
Properties{
_Color("Color", Color) = (1,1,1,1)
_MyArr("Tex", 2DArray) = "" {}
_Index("Texture Index", Int) = 0
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
}
SubShader{
Tags{ "RenderType" = "Opapue" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.5 target, to get
#pragma target 3.5
UNITY_DECLARE_TEX2DARRAY(_MyArr);
struct Input {
float2 uv_MyArr;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
float _Index;
void surf(Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
// 2nd attribute is float3 !!! uvs and index
fixed4 c = UNITY_SAMPLE_TEX2DARRAY(_MyArr, float3(IN.uv_MyArr, _Index)) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
