- Home /
Configure standard shader/material with Texture2Darray: how?
Is there a way to use a Texture2DArray with a material using the Standard Shader? If so, how?
I create a new Texture2DArray and then copy sub-textures from a standard Texture2D, using Graphics.CopyTexture. That seems to work.
But when I try to assign the new texture2darray to an existing material, I get the error " Error assigning texarray texture to 2D texture property '_MainTex': Dimensions must match UnityEngine.Material:set_mainTexture(Texture)".
In the code below, "original", "source", and "dimensions" are set in the Unity Editor.
I suppose I will have no choice but to use a custom shader. I found instructions on how to modify the standard shader to accept an index value and use a texture array, so I'll try that. Was just wondering if it's absolutely necessary.
void Start() {
copy = new Material(original);
if (copy != null) {
copy.mainTexture = CreateTextureArray();
GetComponent<MeshRenderer>().material = copy;
}
}
Texture2DArray CreateTextureArray() {
int w = source.width / dimensions.x;
int h = source.height / dimensions.y;
Texture2DArray ta = new Texture2DArray(w, h, dimensions.x * dimensions.y, source.format, true);
ta.filterMode = FilterMode.Point;
ta.wrapMode = TextureWrapMode.Repeat;
for (int i = 0; i < dimensions.y; i++) {
for (int j = 0; j < dimensions.x; j++) {
int index = i * dimensions.x + j;
int x = j * w;
int y = i * h;
Graphics.CopyTexture(source, 0, 0, x, y, w, h, ta, x, 0, 0, 0);
}
}
return ta;
}
Answer by bgulanowski · Nov 26, 2018 at 08:35 PM
Short answer is "It cannot be done."
The Standard Shader expects 2-component (Vector2) UVs. A Texture2DArray requires 3-componnent (Vector3) UVs.
This article details how to modify copies of standard shader files and make them work with Texture2DArray:
https://medium.com/@calebfaith/how-to-use-texture-arrays-in-unity-a830ae04c98b
The conversion is conceptually simple: find wherever the shader accesses the main texture using 2d coordinates, and replace with a 3d coordinate access. Then generate 3d UVs on your models. This probably requires procedurally generated content, but then, that's generally the situation when this requirement comes up.
Your answer
![](https://koobas.hobune.stream/wayback/20220612182812im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
!texture.texture error 0 Answers
Mesh Render Texture Blurry 1 Answer
assign texture to asset material via script 1 Answer
Get Texture 2D after all shader passes are applied 0 Answers
is there a way to invers multiply a black/white texture 0 Answers