Search Unity

PostProcess Outline Shader GVR alignment

Discussion in 'AR/VR (XR) Discussion' started by Reahreic, Aug 11, 2017.

  1. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    I've been working on implementing a post processing outline shader that i can use on the Google Daydream (GVR) Platform (among others) and have run into a problem that i can't seem to resolve.

    My outlined objects display correctly in the editor, however once deployed to a daydream compatible phone, the outline is rendered offset along the z axis about halfway between the camera and target object.

    Project Settings:
    • Multi Pass Stereo Rendering
    • Forward Rendering
    Outline Shader
    Code (CSharp):
    1. Shader "Hidden/Test"
    2. {
    3.      Properties
    4.     {
    5.         _MainTex ("Main Texture",2D) = "black"{}
    6.         _SceneTex("Scene Texture",2D) = "black"{}
    7.     }
    8.     SubShader
    9.     {
    10.         // No culling or depth
    11.         //Cull Off ZWrite Off ZTest Always
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.            
    19.             #include "UnityCG.cginc"
    20.  
    21.             sampler2D _MainTex ;
    22.             sampler2D _SceneTex;
    23.  
    24.             float2 _MainTex_TexelSize;
    25.  
    26.             struct v2f{
    27.                 float4 pos : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.             };
    30.  
    31.             v2f vert (appdata_base v){
    32.                 v2f o;
    33.                 o.pos = UnityObjectToClipPos(v.vertex);
    34.                 o.uv = o.pos.xy / 2 + 0.5;
    35.                 return o;
    36.             }
    37.  
    38.             half4 frag (v2f i) : COLOR{
    39.  
    40.                 if(tex2D(_MainTex , i.uv.xy).r > 0){
    41.                     return tex2D(_SceneTex , i.uv.xy);
    42.                 }
    43.  
    44.                 float colorInt = 0;
    45.                 int thickness = 8;
    46.  
    47.                 for(int k = 0; k < thickness; k++){
    48.                     for(int j = 0; j < thickness; j++){
    49.                         colorInt += tex2D( _MainTex , i.uv.xy + float2(( k - thickness / 2) * _MainTex_TexelSize.x, (j - thickness / 2) * _MainTex_TexelSize.y)).r;
    50.                     }
    51.                 }
    52.                 return colorInt * half4(0,1,1,1) + tex2D(_SceneTex , i.uv.xy);
    53.             }
    54.             ENDCG
    55.         }
    56.     }
    57. }
    58.  
    OutlinePostEffect
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using UnityEngine.Rendering;
    4. using UnityEngine.VR;
    5.  
    6. public class OutlinePostEffect :MonoBehaviour {
    7.  
    8.     public AppManager appManager;
    9.     public Camera bufferCamera;
    10.  
    11.     [Space(20)]
    12.     public Shader outlineShader;
    13.     public Shader outlineBufferShader;
    14.     public LayerMask layerMask;
    15.  
    16.     private Material outlineMaterial;
    17.     private RenderTexture renderTexture;
    18.  
    19.    
    20.  
    21.     private bool isReady = false;
    22.  
    23.     void Start() {
    24.         //Configure bufferCam rendering path on outline camera
    25.         bufferCamera.cullingMask = layerMask.value;
    26.         bufferCamera.renderingPath = RenderingPath.Forward;
    27.  
    28.         MakeRT();
    29.  
    30.         outlineMaterial = new Material(outlineShader);
    31.  
    32.         isReady = true;
    33.     }
    34.  
    35.     private void MakeRT() {
    36.         if(VRSettings.eyeTextureWidth <= 0) {
    37.             renderTexture = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.R8);
    38.         } else {
    39.             renderTexture = new RenderTexture(VRSettings.eyeTextureWidth, VRSettings.eyeTextureHeight, 0, RenderTextureFormat.R8);
    40.         }
    41.         renderTexture.antiAliasing = 2;
    42.         renderTexture.anisoLevel = 0;
    43.         renderTexture.depth = 0;
    44.         renderTexture.useMipMap = false;
    45.         renderTexture.filterMode = FilterMode.Bilinear;
    46.  
    47.         //put it to video memory
    48.         renderTexture.Create();
    49.     }
    50.  
    51.     void OnRenderImage(RenderTexture source, RenderTexture destination) {
    52.         if(!isReady) { return; }
    53.        
    54.         //set the camera's target texture when rendering
    55.         bufferCamera.targetTexture = renderTexture;
    56.  
    57.         outlineMaterial.SetTexture("_SceneTex", source);
    58.  
    59.         //render all objects this camera can render, but with our custom shader.
    60.         bufferCamera.RenderWithShader(outlineBufferShader, "");
    61.  
    62.         //copy the RT to the final image
    63.         Graphics.Blit(renderTexture, destination, outlineMaterial);
    64.  
    65.         //release the temporary RT
    66.         //renderTexture.Release();
    67.  
    68.     }
    69. }
    70.  
     
  2. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254