Search Unity

CommandBuffer.CopyTexture with mipmaps?

Discussion in 'General Graphics' started by Alex_May, Apr 4, 2019.

  1. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    198
    Hey all! I'm trying to copy a texture from a temporary render texture in a command buffer to the final texture I want to use. Here's the code:

    Code (CSharp):
    1.             // draw a bunch of leaves on the leaves texture
    2.             int bigTexSize = 256;
    3.             leavesTexture = new Texture2D(bigTexSize, bigTexSize, TextureFormat.R8, false, false);
    4.             CommandBuffer cb = new CommandBuffer();
    5.             int rt = Shader.PropertyToID("rt");
    6.             RenderTextureDescriptor desc = new RenderTextureDescriptor();
    7.             desc.width = desc.height = bigTexSize;
    8.             desc.autoGenerateMips = false;
    9.             desc.colorFormat = RenderTextureFormat.R8;
    10.             desc.useMipMap = false;
    11.             desc.msaaSamples = 1;
    12.             desc.dimension = TextureDimension.Tex2D;
    13.             desc.depthBufferBits = 0;
    14.             desc.shadowSamplingMode = ShadowSamplingMode.None;
    15.             desc.volumeDepth = 1;
    16.             //RenderTexture rt2 = new RenderTexture(desc);
    17.             cb.GetTemporaryRT(rt, desc);
    18.             cb.SetRenderTarget(rt);
    19.             cb.ClearRenderTarget(true, true, Color.black, 0);
    20.             cb.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.Ortho(0, bigTexSize, 0, bigTexSize, 0, bigTexSize));
    21.             float leafMass = result.leafMass * result.leafLength * result.leafLength; // average about 7500*4*4 = 120,000
    22.             int instances = Mathf.Clamp(Mathf.FloorToInt(1000000.0f/leafMass), 1, 1000);
    23.             Material material = new Material(Shader.Find("Unlit/LeafDrawing"));
    24.             material.mainTexture = leafTexture;
    25.             for (int i = 0; i < instances; i++)
    26.             {
    27.                 float angle = Random.Range(0, 360);
    28.                 cb.DrawMesh(leafDrawingMesh, Matrix4x4.TRS(new Vector3(Random.Range(64,bigTexSize-64),Random.Range(64,bigTexSize-64), -bigTexSize/2), Quaternion.AngleAxis(angle, Vector3.forward), Vector3.one * Random.Range(0.9f, 1.3f) * result.leafLength * 5),material, 0);
    29.             }
    30.             for (int i = 0; i < leavesTexture.mipmapCount; i++)
    31.                 cb.CopyTexture(rt, 0, i, leavesTexture, 0, i);
    32.             cb.ReleaseTemporaryRT(rt);
    33.             Graphics.ExecuteCommandBuffer(cb);
    34.             Destroy(material);
    Now, this code works fine. But if you set the Texture2D to have mip maps, and the render texture to have mips, the result is just black.

    What am I doing wrong here? Anyone know?
     
  2. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    198
    Found out why it doesn't work:
     
  3. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    198
    (well, that doesn't explain why it works when there are no mipmaps involved)