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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Lower resolution for raymarching object, HoloLens

Discussion in 'General Graphics' started by Gaulois94, Jan 30, 2020.

  1. Gaulois94

    Gaulois94

    Joined:
    Jan 24, 2018
    Posts:
    12
    Hi all!

    I am doing a raymarching shader for Unity which works well but is slow. I am then trying to lower the resolution of the rendering ONLY for those objects (which is targeting a particular layer) to increase speed while not touching too much on the quality (the default screen resolution is high compared to my need).

    I then created a camera which has for parent Camera.main, and which I attached this code:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.XR;
    4. public class RenderTextureCamera : MonoBehaviour
    5. {
    6.     /// <summary>
    7.     /// The render texture use for volume rendering
    8.     /// </summary>
    9.     private RenderTexture m_volumeRenderingTexture = null;
    10.     public Material CopyTextureMaterial;
    11.     void Start()
    12.     {
    13.         //Create a render texture for the volume rendering camera. No depth texture, alpha component
    14.         m_volumeRenderingTexture = new RenderTexture((int)(Camera.main.pixelWidth*0.75), (int)(Camera.main.pixelHeight*0.75), 0, RenderTextureFormat.ARGB32);
    15.         m_volumeRenderingTexture.vrUsage = VRTextureUsage.TwoEyes;
    16.         Camera camera = GetComponent<Camera>();
    17.         int cullingMask = camera.cullingMask;
    18.         float depth     = camera.depth;
    19.         Color color     = camera.backgroundColor;
    20.         camera.CopyFrom(Camera.main); //Copy stereoscopy parameters
    21.         camera.cullingMask     = cullingMask; //Only my layer (in the editor)
    22.         camera.backgroundColor = color; //(0,0,0,0) in the editor
    23.         camera.depth = depth; //I do not know if this was important
    24.         camera.clearFlags = CameraClearFlags.SolidColor;
    25.         camera.targetTexture = m_volumeRenderingTexture;
    26.         camera.rect = new Rect(0, 0, 1, 1);
    27.     }
    28.    
    29.     private void OnPreRender()
    30.     {
    31.         Camera camera = GetComponent<Camera>();
    32.         camera.targetTexture = m_volumeRenderingTexture;
    33.     }
    34.     private void OnPostRender()
    35.     {
    36.         Camera camera = GetComponent<Camera>();
    37.         camera.targetTexture = null;
    38.         Graphics.Blit(m_volumeRenderingTexture, (RenderTexture)null); //Should go on screen, right?
    39.     }
    40.  
    41. }
    42.  
    However, for un unknown reason, this code does not work. I tried multiple things, and either my camera's rendering is not displayed, either the alpha component of my camera is not taken into account (I played with RenderTexture.active and camera.targetTexture parameters).

    Is there a way to blend two cameras on screen, one after the other?

    Best
     
  2. Gaulois94

    Gaulois94

    Joined:
    Jan 24, 2018
    Posts:
    12
    For people asking how to overlay cameras: the issue was on OnRenderImage which does not use, by default, a transparency-proof material.

    Here is my code:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.XR;
    4. public class RenderTextureCamera : MonoBehaviour
    5. {
    6.     /// <summary>
    7.     /// The render texture use for volume rendering
    8.     /// </summary>
    9.     private RenderTexture m_volumeRenderingTexture = null;
    10.  
    11.     public Material CopyTextureMaterial;
    12.  
    13.     private Material m_material;
    14.  
    15.     void Start()
    16.     {
    17.         //Create a render texture for the volume rendering camera. No depth texture, alpha component
    18.         m_volumeRenderingTexture = new RenderTexture(512, (int)(Camera.main.pixelHeight * 512.0f / Camera.main.pixelWidth), 16, RenderTextureFormat.ARGB32);
    19.         m_volumeRenderingTexture.vrUsage = VRTextureUsage.TwoEyes;
    20.  
    21.         m_material = new Material(CopyTextureMaterial);
    22.  
    23.         Camera camera = GetComponent<Camera>();
    24.         int cullingMask = camera.cullingMask;
    25.         float depth = camera.depth;
    26.         camera.CopyFrom(Camera.main);
    27.         camera.cullingMask = cullingMask;
    28.         camera.depth = depth;
    29.         camera.backgroundColor = new Color(0, 0, 0, 0);
    30.         camera.clearFlags = CameraClearFlags.SolidColor;
    31.         camera.targetTexture = m_volumeRenderingTexture;
    32.         camera.rect = new Rect(0, 0, 1, 1);
    33.     }
    34.  
    35.     private void OnPreRender()
    36.     {
    37.         Camera camera = GetComponent<Camera>();
    38.         camera.targetTexture = m_volumeRenderingTexture;
    39.     }
    40.  
    41.     private void OnRenderImage(RenderTexture source, RenderTexture destination)
    42.     {
    43.         Graphics.Blit(source, destination, m_material); //The culprit! m_material is a stupid shader that uses alpha blending
    44.     }
    45.  
    46.     private void OnPostRender()
    47.     {
    48.         Camera camera = GetComponent<Camera>();
    49.         camera.targetTexture = null; //I guess this force the final rendering to be on the screen, without it, the camera never overlay
    50.     }
    51. }
    52.  
     
  3. Gaulois94

    Gaulois94

    Joined:
    Jan 24, 2018
    Posts:
    12
    Well, that was what I thought, until I test with stereoscopic display… My lower resolution camera does not appear in full screen for an unknown Reason.

    My shader:

    Code (CSharp):
    1. Shader "Sereno/CopyTexture"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Main Texture", any) = "" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
    11.  
    12.         Lighting Off
    13.         Cull Off
    14.         ZWrite Off
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.         Fog {Mode Off}
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.  
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 fixed4 color  : COLOR;
    30.                 float2 uv     : TEXCOORD0;
    31.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    32.             };
    33.  
    34.             struct v2f
    35.             {
    36.                 float4 vertex : SV_POSITION;
    37.                 fixed4 color  : COLOR;
    38.                 float2 uv     : TEXCOORD0;
    39.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    40.                 UNITY_VERTEX_OUTPUT_STEREO
    41.             };
    42.  
    43.             /** The texture data*/
    44.             UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex);
    45.             //sampler2D _MainTex;
    46.             uniform float4 _MainTex_ST; //Scale--translation of that texture
    47.             uniform float2 _Scale = float2(1.0, 1.0);
    48.  
    49.             v2f vert(appdata v)
    50.             {
    51.                 v2f o;
    52.                 UNITY_SETUP_INSTANCE_ID(v);
    53.                 UNITY_TRANSFER_INSTANCE_ID(v, o);
    54.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    55.  
    56.                 o.vertex = UnityObjectToClipPos(v.vertex);
    57.                 o.color  = v.color;
    58.                 o.uv     = TRANSFORM_TEX(v.uv, _MainTex);
    59.                
    60.                 return o;
    61.             }
    62.  
    63.             fixed4  frag(v2f input) : COLOR
    64.             {
    65.                 UNITY_SETUP_INSTANCE_ID(input);
    66.                 fixed4 color = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, input.uv);
    67.                 //fixed4 color = tex2D(_MainTex, input.uv);
    68.  
    69.                 return color;
    70.             }
    71.             ENDCG
    72.         }
    73.     }
    74. }
    75.  
    and my C# camera code:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.XR;
    4. public class RenderTextureCamera : MonoBehaviour
    5. {
    6.     /// <summary>
    7.     /// The render texture use for volume rendering
    8.     /// </summary>
    9.     private RenderTexture m_volumeRenderingTexture = null;
    10.  
    11.     public Material CopyTextureMaterial;
    12.  
    13.     private Material m_material;
    14.  
    15.     void Start()
    16.     {
    17.         //Create a render texture for the volume rendering camera. No depth texture, alpha component
    18.         m_volumeRenderingTexture = new RenderTexture(512, (int)(Screen.height * 512.0f / ((XRSettings.isDeviceActive ? 2 : 1)*Screen.width)), 16, RenderTextureFormat.ARGB32);
    19.         m_volumeRenderingTexture.vrUsage = VRTextureUsage.TwoEyes;
    20.  
    21.         m_material = new Material(CopyTextureMaterial);
    22.  
    23.         Camera camera = GetComponent<Camera>();
    24.         int cullingMask = camera.cullingMask;
    25.         float depth = camera.depth;
    26.         camera.CopyFrom(Camera.main);
    27.         camera.cullingMask = cullingMask;
    28.         camera.depth = depth;
    29.         camera.backgroundColor = new Color(0, 0, 0, 0);
    30.         camera.clearFlags = CameraClearFlags.Depth;
    31.         camera.targetTexture = m_volumeRenderingTexture;
    32.         camera.depthTextureMode = DepthTextureMode.None;
    33.         camera.forceIntoRenderTexture = true;
    34.     }
    35.  
    36.     private void OnPreRender()
    37.     {
    38.         Camera camera = GetComponent<Camera>();
    39.         camera.targetTexture = m_volumeRenderingTexture;
    40.     }
    41.  
    42.     private void OnRenderImage(RenderTexture source, RenderTexture destination)
    43.     {
    44.         Graphics.Blit(source, destination, m_material);
    45.  
    46.         //Graphics.SetRenderTarget(destination);
    47.         //m_material.SetPass(0);
    48.         //m_material.SetTexture("_MainTex", source);
    49.  
    50.         /*GL.PushMatrix();
    51.         GL.LoadOrtho();
    52.        
    53.         GL.Begin(GL.QUADS);
    54.             GL.TexCoord2(0, 0);
    55.             GL.Vertex3(0, 0, 0);
    56.             GL.TexCoord2(0, 1);
    57.             GL.Vertex3(0, 1, 0);
    58.             GL.TexCoord2(1, 1);
    59.             GL.Vertex3(1, 1, 0);
    60.             GL.TexCoord2(1, 0);
    61.             GL.Vertex3(1, 0, 0);
    62.         GL.End();
    63.  
    64.         GL.PopMatrix();*/
    65.     }
    66.  
    67.     private void OnPostRender()
    68.     {
    69.         Camera camera = GetComponent<Camera>();
    70.         camera.targetTexture = null;
    71.         Graphics.Blit(m_volumeRenderingTexture, null as RenderTexture, m_material);
    72.     }
    73. }
    74.  
     

    Attached Files:

  4. Gaulois94

    Gaulois94

    Joined:
    Jan 24, 2018
    Posts:
    12