Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Blit into Cubemap

Discussion in 'Shaders' started by flogelz, Jul 26, 2023.

  1. flogelz

    flogelz

    Joined:
    Aug 10, 2018
    Posts:
    141
    So I want to blend between two cubemaps manually with a blit and a custom shader.
    I tried this and soon found out, that you can't use the additional parameters in a blit for cubemap faces (see here https://docs.unity3d.com/ScriptReference/Graphics.Blit.html).
    Then I tried using a manual SetRenderTarget beforehand, where the target cubeface can be set, but it just result into nothing and all the time only one face of the cubemap gets drawn to.

    Other questions here on the forum always suggest using CopyTexture, which works, but doesnt allow me to use a custom material. (And I also know of the workaround of blending between sets of rendertextures and copying only in the final step, but blitting directly into the cubemap would be way more straightforward-)

    Does this functonality not exist or am I doing something obvious wrong?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    I believe I did this by blitting to a "flat" render target and then using copytexture to copy the cubemap face to and from the render texture.

    Alternatively you could do a "manual" blit rather than relying on the Blit() function which should respect the SetRenderTarget. Use a DrawProceedural() call with 3 vertices and set the UVs and vertex positions based on the SV_VertexID.
     
    flogelz likes this.
  3. flogelz

    flogelz

    Joined:
    Aug 10, 2018
    Posts:
    141
    @bgolus Thanks for the hints! I went with the custom blit route, since I can integrate it into the base pipeline. (I also cached the triangle before hand with the uvs, but the procedural approach should work too!)

    Just here for reference:
    Code (CSharp):
    1.     public static void Blit (this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier dest, CubemapFace destElement, int destMip, Material mat, int pass)
    2.     {
    3.         cmd.SetGlobalTexture("_MainTex", source);
    4.         cmd.SetRenderTarget(dest, destMip, destElement);
    5.         cmd.DrawMesh(FullScreenTriangle, Matrix4x4.identity, mat, 0, pass);
    6.     }
     
    bgolus likes this.