Search Unity

Arm Guide to unity developers - Optimizing graphics

Discussion in 'Shaders' started by Megalithic, Oct 20, 2017.

  1. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    Hi,

    I have been trying to implement the cubemap reflection techniques that are available in the "ARM guide to unity developer" documentation. In page 102 of that doc they mention how to implement their shader. I tried it but I don't get the desired effect. I cannot tell what is it that I am doing wrong. Here is the shader.

    Code (CSharp):
    1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    2. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    3. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    4.  
    5. Shader "Arm/Reflection/Testing" {
    6.     Properties {
    7.         _MainTex ("Base (RGB)", 2D) = "white" { }
    8.         _Cube("Reflection Map", Cube) = "" {}
    9.         _AmbientColor("Ambient Color", Color) = (1, 1, 1, 1)
    10.         _ReflAmount("Reflection Amount", Float) = 0.5
    11.     }
    12.     SubShader{
    13.         Pass {
    14.             CGPROGRAM
    15.  
    16.             #pragma glsl
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    20.  
    21.             // User-specified uniforms
    22.             uniform sampler2D _MainTex;
    23.             uniform samplerCUBE _Cube;
    24.             uniform float4 _AmbientColor;
    25.             uniform float _ReflAmount;
    26.             uniform float _ToggleLocalCorrection;
    27.  
    28.             // ----Passed from script InfoRoReflmaterial.cs --------
    29.             uniform float3 _BBoxMin;
    30.             uniform float3 _BBoxMax;
    31.             uniform float3 _EnviCubeMapPos;
    32.  
    33.             struct vertexInput {
    34.                 float4 vertex : POSITION;
    35.                 float3 normal : NORMAL;
    36.                 float4 texcoord : TEXCOORD0;
    37.             };
    38.  
    39.             struct vertexOutput    {
    40.                 float4 pos : SV_POSITION;
    41.                 float4 tex : TEXCOORD0;
    42.                 float3 vertexInWorld : TEXCOORD1;
    43.                 float3 viewDirInWorld : TEXCOORD2;
    44.                 float3 normalInWorld : TEXCOORD3;
    45.             };
    46.  
    47.             //Vertex Functions
    48.             vertexOutput vert(vertexInput input){
    49.                 vertexOutput output;
    50.                 output.tex = input.texcoord;
    51.                 // Transform vertex coordinates from local to world.
    52.                 float4 vertexWorld = mul(unity_ObjectToWorld, input.vertex);
    53.                 // Transform normal to world coordinates.
    54.                 float4 normalWorld = mul(float4(input.normal,0.0), unity_WorldToObject);
    55.                 // Final vertex output position.
    56.                 output.pos = UnityObjectToClipPos(input.vertex);
    57.                
    58.                 // ----------- Local correction ------------
    59.                 output.vertexInWorld = vertexWorld.xyz;
    60.                 output.viewDirInWorld = vertexWorld.xyz - _WorldSpaceCameraPos;
    61.                 output.normalInWorld = normalWorld.xyz;
    62.                
    63.                 return output;  
    64.                
    65.             }
    66.  
    67.             //Fragment Functions
    68.             float4 frag(vertexOutput input) : COLOR    {
    69.                 float4 reflColor = float4(1, 1, 0, 0);
    70.                 // Find reflected vector in WS.
    71.                 float3 viewDirWS = normalize(input.viewDirInWorld);
    72.                 float3 normalWS = normalize(input.normalInWorld);
    73.                 float3 reflDirWS = reflect(viewDirWS, normalWS);
    74.                 // Working in World Coordinate System.
    75.                 float3 localPosWS = input.vertexInWorld;
    76.                 float3 intersectMaxPointPlanes = (_BBoxMax - localPosWS) / reflDirWS;
    77.                 float3 intersectMinPointPlanes = (_BBoxMin - localPosWS) / reflDirWS;
    78.                 // Looking only for intersections in the forward direction of the ray.
    79.                 float3 largestParams = max(intersectMaxPointPlanes, intersectMinPointPlanes);
    80.                 // Smallest value of the ray parameters gives us the intersection.
    81.                 float distToIntersect = min(min(largestParams.x, largestParams.y), largestParams.z);
    82.                 // Find the position of the intersection point.
    83.                 float3 intersectPositionWS = localPosWS + reflDirWS * distToIntersect;
    84.                 // Get local corrected reflection vector.
    85.                 float3 localCorrReflDirWS = intersectPositionWS - _EnviCubeMapPos;
    86.                 // Lookup the environment reflection texture with the right vector.
    87.                 reflColor = texCUBE(_Cube, localCorrReflDirWS);
    88.                 // Lookup the texture color.
    89.                 float4 texColor = tex2D(_MainTex, float4(input.tex));
    90.                 return _AmbientColor + texColor * _ReflAmount * reflColor;
    91.             }
    92.  
    93.             ENDCG
    94.         }
    95.     }
    96. }
    I have attached the faulty reflection that I get.

    It would be great if anyone could help me. I am relatively new to shaders.
     

    Attached Files:

  2. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
  3. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    Can a shader guru help me out please? If you need more info then please let me know.