Search Unity

[Solved] Rendering to UV space (with SRP)

Discussion in 'Shaders' started by tomtom070, Nov 28, 2018.

  1. tomtom070

    tomtom070

    Joined:
    Jul 20, 2017
    Posts:
    7
    Hello,
    I want to render the mesh of a sphere to uv space. I do that to save information about the sphere in a rendertexture later in my project. So my vertex shader looks like this right now:

    Code (CSharp):
    1.  
    2. v2f vert (appdata v){
    3.         v2f o;
    4.         v.vertex = float4((v.uv.xy-0.5)*2, 0, 1.0);  
    5.         v.vertex = mul(UNITY_MATRIX_V, v.vertex);
    6.         v.vertex = mul(UNITY_MATRIX_P, v.vertex);
    7.         o.vertex = float4(v.vertex.xy, 0, 1);
    8.         o.uv = v.uv;
    9.         return o;
    10. }
    And this is working, but only if I set the camera to orthographic, size 1, and position and rotation to 0. But even with that it still doesnt get rendered perfectly. I still get a a few wide pixel black stripe on the right.



    My question now is: Is there a better way to do that? Im using a scriptable render pipeline. Can I do something there?

    Thanks in advance for any help.
    Tim

    PS:
    This is my SRP, if that helps:
    Code (CSharp):
    1.  
    2. public void Render(ScriptableRenderContext context, Camera camera) {
    3.     //culling
    4.     ScriptableCullingParameters cullingParameters;
    5.     if (!CullResults.GetCullingParameters(camera, out cullingParameters)) { return; }
    6.     CullResults.Cull(ref cullingParameters, context, ref cull);
    7.  
    8.     //___Setup camera_____________________________________________________________________
    9.     bool isCamOrth = camera.orthographic;
    10.     float camNear = camera.nearClipPlane;
    11.     float camSize = camera.orthographicSize;
    12.     Vector3 camPos = camera.transform.position;
    13.     Quaternion camRot = camera.transform.rotation;
    14.     camera.orthographic = true;
    15.     camera.nearClipPlane = 0;
    16.     camera.orthographicSize = 1.0f;
    17.     camera.transform.SetPositionAndRotation(Vector3.zero, Quaternion.EulerRotation(0, 0, 0));
    18.  
    19.     context.SetupCameraProperties(camera);
    20.     cameraBuffer.Clear();
    21.     //_____________________________________________________________________________________
    22.  
    23.  
    24.     //clearing
    25.     cameraBuffer.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
    26.  
    27.     context.ExecuteCommandBuffer(cameraBuffer);
    28.     cameraBuffer.Clear();
    29.  
    30.     //drawing
    31.     var drawSettings = new DrawRendererSettings(camera, new ShaderPassName("SRPDefaultUnlit"));
    32.     drawSettings.sorting.flags = SortFlags.CommonOpaque;
    33.     var filterSettings = new FilterRenderersSettings(true);
    34.     context.DrawRenderers(cull.visibleRenderers, ref drawSettings, filterSettings);
    35.  
    36.     context.Submit();
    37.  
    38.     //___reset camera_____________________________________________________________________
    39.     camera.orthographic = isCamOrth;
    40.     camera.nearClipPlane = camNear;
    41.     camera.orthographicSize = camSize;
    42.     camera.transform.SetPositionAndRotation(camPos, camRot);
    43.  
    44. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You don't need 3/4ths of that, the first line is really all you need. That's already positioning the uvs into clip space.

    o.vertex = float4(v.uv.xy * 2.0 - 1.0, 0.5, 1.0);
     
    tomtom070 likes this.
  3. tomtom070

    tomtom070

    Joined:
    Jul 20, 2017
    Posts:
    7
    Sorry for the late answer, I seem to have missed the notification.

    Interessting. I tried that already, but it didn't work in my main project. Now I tried it again in the reduced version of my project to isolate this issue, and there it works.

    The problem only seems to occure when I render to a render texture (again -.- ). I will look into that later when I have time.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Make sure your shader is using

    Cull Off

    Render textures in D3D does some weird stuff, like flip the Y axis. So you may also need to do:

    o.vertex.y = 1 - o.vertex.y;
     
    tomtom070 likes this.
  5. tomtom070

    tomtom070

    Joined:
    Jul 20, 2017
    Posts:
    7
    Hm. That work. Thank your for helping me again. I really appreciate your help.