- Home /
Is it possible to combine two textures from a texture array in a shader?
I would like to create a shader based on an existing one where it can display xz and y from a texture array. However, currently the shader in question only allows for xz. Y is very stretched, which doesn't work for what I'm trying to do.
In order to fix the issue, I would like to add a y texture as well, also sampling from the array, however shaders are not my best area. Is this possible? If so, what do I need to change in the shader as it is to allow it?
Shader "Custom/TerrainTextures" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Terrain Texture", 2DArray) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.5
UNITY_DECLARE_TEX2DARRAY(_MainTex);
struct Input {
float4 color : COLOR;
float3 worldPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
float2 uv = IN.worldPos.xz * 0.20;
fixed4 c = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(uv, 0));
o.Albedo = c.rgb * _Color;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Answer by Eno-Khaon · Jan 16, 2019 at 03:31 AM
Maybe I'm misunderstanding what you're trying to do exactly, but would a Texture3D be more suitable for your needs in this case? I'm not really clear what you mean by the Y-axis being stretched.
When applying a Texture2D vs. a Texture3D vs. a Texture2DArray:
Texture2D uses 2D coordinates, held in a 0-1 range, to define what part of the texture to draw.
Texture3D uses 3D coordinates, also held in a 0-1 range, to define what part of the texture to draw.
Texture2DArray uses 2D coordinates, plus an integer depth, to define what part of *which* texture to draw.
In other words, to grab a pixel from the same depth in a Texture3D and a (basically) identical Texture2Darray, each of 8 Texture2Ds' depth, you might take a sample at 0.5625 depth from the Texture3D (0.5 + half a pixel) or at 4 depth (base 0, 5th texture) from the Texture2DArray). Furthermore, Texture2DArrays are always clamped, whereas Texture3D can repeat around the Z-axis.
As a result of that, if you simply use the same coordinate system (world position) for each axis of a Texture2DArray, then X/Y would repeat every 1 unit of world space, whereas Z would only increment 1 pixel (from each Texture component) per 1 unit of world space.
Your answer
![](https://koobas.hobune.stream/wayback/20220612185832im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
ES2.0 Diffuse Shader? 0 Answers
clipping shader for OpenGL quad 0 Answers
Unity3D sprite mask shader 0 Answers
normal maps on iOS with a custom shader.. Works, but not the way I need it to 0 Answers
GLSL shader adding UV offset 1 Answer