Search Unity

Reverse normals on GLSL

Discussion in 'Shaders' started by Neogene, Apr 24, 2018.

  1. Neogene

    Neogene

    Joined:
    Dec 29, 2010
    Posts:
    95
    Hi,

    I'm trying to modify the Google Carboard VR Unity shader to flip rendered texture inside the mesh, so I should invert normals (I don't want to modify the mesh), I added a checkbox/float variable but I'm not able to modify the vertex/main() body to do the operation, I added also the .ifdef area, anyone could help ? thank you.

    Code (CSharp):
    1. Shader "GoogleVR/Video Unlit Shader" {
    2.   Properties {
    3.     _Gamma ("Video gamma", Range(0.01,3.0)) = 1.0
    4.     _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     [KeywordEnum(None, TopBottom, LeftRight)] _StereoMode ("Stereo mode", Float) = 0
    6.     [Toggle(FLIP_X)] _FlipX ("Flip X", Float) = 0
    7.     [Toggle(FLIP_NORMALS)] _FlipNormals ("Flip Normals", Float) = 0
    8.   }
    9.  
    10.   SubShader {
    11.     Pass {
    12.       Tags { "RenderType" = "Opaque" }
    13.  
    14.       Lighting Off
    15.       Cull Off
    16.  
    17.       GLSLPROGRAM
    18.         #pragma only_renderers gles gles3
    19.         #extension GL_OES_EGL_image_external : require
    20.         #extension GL_OES_EGL_image_external_essl3 : enable
    21.  
    22.         #pragma multi_compile ___ _STEREOMODE_TOPBOTTOM _STEREOMODE_LEFTRIGHT
    23.         #pragma multi_compile ___ FLIP_X
    24.         #pragma multi_compile ___ FLIP_NORMALS
    25.  
    26.         precision mediump int;
    27.         precision mediump float;
    28.  
    29.         #ifdef VERTEX
    30.           uniform mat4 video_matrix;
    31.           uniform int unity_StereoEyeIndex;
    32.           varying vec2 uv;
    33.  
    34.           void main() {
    35.             gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    36.             vec4 untransformedUV = gl_MultiTexCoord0;
    37.  
    38.            #ifdef FLIP_NORMALS
    39.               *************************** what to do here ? *********
    40.             #endif  // END
    41.  
    42.             #ifdef FLIP_X
    43.               untransformedUV.x = 1.0 - untransformedUV.x;
    44.             #endif  // FLIP_X
    45.             #ifdef _STEREOMODE_TOPBOTTOM
    46.               untransformedUV.y *= 0.5;
    47.               if (unity_StereoEyeIndex == 0) {
    48.                 untransformedUV.y += 0.5;
    49.               }
    50.             #endif  // _STEREOMODE_TOPBOTTOM
    51.             #ifdef _STEREOMODE_LEFTRIGHT
    52.               untransformedUV.x *= 0.5;
    53.               if (unity_StereoEyeIndex != 0) {
    54.                 untransformedUV.x += 0.5;
    55.               }
    56.             #endif  // _STEREOMODE_LEFTRIGHT
    57.  
    58.             uv = (video_matrix * untransformedUV).xy;
    59.           }
    60.         #endif  // VERTEX
    61.  
    62.         #ifdef FRAGMENT
    63.           vec3 gammaCorrect(vec3 v, float gamma) {
    64.             return pow(v, vec3(1.0/gamma));
    65.           }
    66.  
    67.           // Apply the gamma correction.  One possible optimization that could
    68.           // be applied is if _Gamma == 2.0, then use gammaCorrectApprox since sqrt will be faster.
    69.           // Also, if _Gamma == 1.0, then there is no effect, so this call could be skipped all together.
    70.           vec4 gammaCorrect(vec4 v, float gamma) {
    71.             return vec4(gammaCorrect(v.xyz, gamma), v.w);
    72.           }
    73.  
    74.           uniform float _Gamma;
    75.           uniform samplerExternalOES _MainTex;
    76.           varying vec2 uv;
    77.  
    78.           void main() {
    79.             gl_FragColor = gammaCorrect(texture2D(_MainTex, uv), _Gamma);
    80.           }
    81.         #endif  // FRAGMENT
    82.       ENDGLSL
    83.     }
    84.   }
    85.   Fallback "Unlit/Texture"
    86. }