Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.4 KiB

  1. //
  2. // OvrAvatar Mobile Single Component Loading shader
  3. //
  4. // Cut-down single component version of the avatar shader to be used during combined mesh loading
  5. //
  6. // See OvrAvatarMaterialManager implementation notes
  7. //
  8. Shader "OvrAvatar/Avatar_Mobile_Loader"
  9. {
  10. Properties
  11. {
  12. [NoScaleOffset] _NormalMap("Normal Map", 2D) = "bump" {}
  13. _BaseColor("Color Tint", Color) = (1.0,1.0,1.0,1.0)
  14. _Dimmer("Dimmer", Range(0.0,1.0)) = 1.0
  15. _LoadingDimmer("Loading Dimmer", Range(0.0,1.0)) = 1.0
  16. _Alpha("Alpha", Range(0.0,1.0)) = 1.0
  17. _DiffuseIntensity("Diffuse Intensity", Range(0.0,1.0)) = 0.3
  18. _RimIntensity("Rim Intensity", Range(0.0,10.0)) = 5.0
  19. }
  20. SubShader
  21. {
  22. Tags { "LightMode" = "ForwardBase" "IgnoreProjector" = "True"}
  23. Pass
  24. {
  25. Blend One Zero
  26. Cull Back
  27. CGPROGRAM
  28. #pragma vertex vert
  29. #pragma fragment frag
  30. #pragma target 3.0
  31. #pragma fragmentoption ARB_precision_hint_fastest
  32. #include "UnityCG.cginc"
  33. #include "UnityLightingCommon.cginc"
  34. sampler2D _NormalMap;
  35. float4 _NormalMap_ST;
  36. float4 _BaseColor;
  37. float _Dimmer;
  38. float _LoadingDimmer;
  39. float _Alpha;
  40. float _DiffuseIntensity;
  41. float _RimIntensity;
  42. static const fixed MOUTH_ZSCALE = 0.5f;
  43. static const fixed MOUTH_DROPOFF = 0.01f;
  44. struct appdata
  45. {
  46. float4 vertex: POSITION;
  47. float3 normal: NORMAL;
  48. float4 tangent: TANGENT;
  49. float4 uv: TEXCOORD0;
  50. };
  51. struct v2f
  52. {
  53. float4 pos : SV_POSITION;
  54. float2 uv : TEXCOORD0;
  55. float4 posWorld: TEXCOORD1;
  56. float3 normalDir: TEXCOORD2;
  57. float3 tangentDir: TEXCOORD3;
  58. float3 bitangentDir: TEXCOORD4;
  59. };
  60. v2f vert(appdata v)
  61. {
  62. v2f o;
  63. // Calculate tangents for normal mapping
  64. o.normalDir = normalize(UnityObjectToWorldNormal(v.normal));
  65. o.tangentDir = normalize(mul(unity_ObjectToWorld, half4(v.tangent.xyz, 0.0)).xyz);
  66. o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
  67. o.posWorld = mul(unity_ObjectToWorld, v.vertex);
  68. o.pos = UnityObjectToClipPos(v.vertex);
  69. o.uv = v.uv;
  70. return o;
  71. }
  72. fixed4 frag(v2f i) : COLOR
  73. {
  74. // Process normal map
  75. #if (UNITY_VERSION >= 20171)
  76. float3 normalMap = UnpackNormal(tex2D(_NormalMap, i.uv));
  77. #else
  78. float3 normalMap = tex2D(_NormalMap, i.uv) * 2.0 - 1.0;
  79. #endif
  80. float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
  81. float3 normalDirection = normalize(mul(normalMap.rgb, tangentTransform));
  82. // Normal/Light/View calculations
  83. half3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  84. half VdotN = saturate(dot(viewDirection, normalDirection));
  85. half NdotL = saturate(dot(normalDirection, _WorldSpaceLightPos0.xyz));
  86. // Calculate color
  87. float4 albedoColor;
  88. #if !defined(UNITY_COLORSPACE_GAMMA)
  89. _BaseColor.rgb = LinearToGammaSpace(_BaseColor.rgb);
  90. #endif
  91. // Final base color including DiffuseIntensity and NdotL for lighting gradient
  92. _BaseColor.rgb += _DiffuseIntensity * NdotL * _LightColor0;
  93. // No diffuse texture in the loader shader
  94. albedoColor = _BaseColor;
  95. // Rim term
  96. albedoColor.rgb += pow(1.0 - VdotN, _RimIntensity) * NdotL;
  97. // Global dimmer
  98. albedoColor.rgb *= lerp(_Dimmer, _LoadingDimmer, step(_LoadingDimmer, _Dimmer));
  99. // Convert back to linear color space if we are in linear
  100. #if !defined(UNITY_COLORSPACE_GAMMA)
  101. albedoColor.rgb = GammaToLinearSpace(albedoColor.rgb);
  102. #endif
  103. albedoColor.rgb = saturate(albedoColor.rgb);
  104. // Set alpha
  105. albedoColor.a *= _Alpha;
  106. // Return clamped final color
  107. return albedoColor;
  108. }
  109. ENDCG
  110. }
  111. }
  112. }