Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save babon/e027af971d04e8b4d15d148f18826e7a to your computer and use it in GitHub Desktop.
Save babon/e027af971d04e8b4d15d148f18826e7a to your computer and use it in GitHub Desktop.

Revisions

  1. babon created this gist Aug 27, 2023.
    12 changes: 12 additions & 0 deletions Unity efficiently convert RenderTexture to Texture3D.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    //input RenderTexture 'rt3D'

    Texture3D output = new Texture3D(width, height, depth, rt3D.graphicsFormat, TextureCreationFlags.None);
    //assuming rt3D format to be 8 bits
    var a = new NativeArray<byte>(width * height * depth, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
    AsyncGPUReadback.RequestIntoNativeArray(ref a, rt3D, 0, (_) =>
    {
    output.SetPixelData(a, 0);
    output.Apply(updateMipmaps: false, makeNoLongerReadable: true);
    AssetDatabase.CreateAsset(output, "Assets/3dtex.asset");
    a.Dispose();
    });