#Attempting to copy 3 textures to 3 Texture2DArrays, all duplicates
1 messages · Page 1 of 1 (latest)
public static Texture2D ResizeReformatTexture(this Texture2D oldTexture, TextureFormat newFormat, Vector2Int TextureResolution)
{
// Create new empty Texture
Texture2D newTex = new Texture2D(oldTexture.width, oldTexture.height, newFormat, false);
// Copy old texture pixels into new one
newTex.SetPixels(oldTexture.GetPixels());
newTex.Apply();
// Resize texture
oldTexture.filterMode = FilterMode.Point;
RenderTexture rt = RenderTexture.GetTemporary(TextureResolution.x, TextureResolution.y);
rt.filterMode = FilterMode.Point;
// Copy from newTex to temporary RenderTexture
RenderTexture.active = rt;
Graphics.Blit(newTex, rt);
// Resize and copy back
newTex.Resize(TextureResolution.x, TextureResolution.y);
newTex.ReadPixels(new Rect(0, 0, TextureResolution.x, TextureResolution.y), 0, 0);
newTex.Apply();
// Release RenderTexture
RenderTexture.active = null;
RenderTexture.ReleaseTemporary(rt);
return newTex;
}```
I tried to remove the resize part
and just resized the textures myself, same issue
Changed it to an extension btw
Hmm okay, how are you defining the _DiffuseTextures / _SpecularTextures / _ReflectivityTextures then? Maybe they are set to the same texture array?
Oh my god
I'm so stupid
_DiffuseTextures = _SpecularTextures = _ReflectivityTextures = new Texture2DArray(TextureResolution.x,
TextureResolution.y, _rayTracingObjects.Count, TextureFormat.RGBAFloat, false);
🤦
So this is passed by reference not value when you do this?
I thought this was just a fast way to initialize all 3 as a new tex2darray
Most likely yeah
Thank you so much man, you're awesome 
Hah I'll make sure to keep this in mind for any future code
Do you need the data on the CPU?
Why not do all in a shader blit?
I know you're doing a blit now, but why all the read pixlels stuff? IDK the original question. But that read pixels get/set stuff is SLOOOOOW
That's a good point, could blit straight into each slice of a RenderTexture (with dimension Tex2DArray)
I'd assume they are only doing this setup in Start/Awake/OnEnable now though and not every frame so probably isn't that bad.
Yeah right now I'm doing this only when "Rebuilding" the scene with updated textures and mesh data
but thats a much more efficient idea, I'll try doing a blit for it instead just to save on some time
But actually speaking of preparing the compute shader, it takes an awfully long time for objects with over a few thousand tris, like up to 10 seconds
I'm actually running in a loop to assign the mesh's vertex data to my Tris buffer
I never bothered with making this slower as this was only being called when i press a button to do so, but I now am curious about how I'd optimize this
Not optimsation related but it's a little weird to apply the localToWorld matrix on the uvs