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.

215 lines
9.3 KiB

  1. //
  2. // OvrAvatar PC single component expressive face shader
  3. // For use on expressive face meshes
  4. //
  5. // Unity Surface Shader implementation
  6. // Mobile vertex/fragment shader is recommended for use on mobile platforms for performance.
  7. //
  8. // Uses transparent queue for fade effects
  9. //
  10. // Color and appearance of the facial regions controlled via G&B channels in roughness texture
  11. // Pupil size controlled by manipulating UV coordinates
  12. //
  13. Shader "OvrAvatar/Avatar_PC_SingleComponentExpressive"
  14. {
  15. Properties
  16. {
  17. [NoScaleOffset] _MainTex("Color (RGB)", 2D) = "white" {}
  18. [NoScaleOffset] _NormalMap("Normal Map", 2D) = "bump" {}
  19. [NoScaleOffset] _RoughnessMap("Roughness Map", 2D) = "black" {}
  20. _BaseColor("Color Tint", Color) = (1.0,1.0,1.0,1.0)
  21. _Dimmer("Dimmer", Range(0.0,1.0)) = 1.0
  22. _Alpha("Alpha", Range(0.0,1.0)) = 1.0
  23. _DiffuseIntensity("Diffuse Intensity", Range(0.0,1.0)) = 0.3
  24. _SmoothnessMultiplier("Smoothness Multiplier", Range(0.0,1.0)) = 1.0
  25. _MetallicMultiplier("Metallic Multiplier", Range(0.0,1.0)) = 0.3
  26. _RimIntensity("Rim Intensity", Range(0.0,10.0)) = 5.0
  27. _PupilSize("Pupil Size", Range(-1, 2)) = 0
  28. _LipSmoothness("Lip Smoothness", Range(0, 1)) = 0
  29. _MaskColorIris("Iris Color", Color) = (0.0,0.0,0.0,1.0)
  30. _MaskColorLips("Lips Color", Color) = (0.0,0.0,0.0,1.0)
  31. _MaskColorBrows("Brows Color", Color) = (0.0,0.0,0.0,1.0)
  32. _MaskColorLashes("Lashes Color", Color) = (0.0,0.0,0.0,1.0)
  33. _MaskColorSclera("Sclera Color", Color) = (0.0,0.0,0.0,1.0)
  34. _MaskColorGums("Gums Color", Color) = (0.0,0.0,0.0,1.0)
  35. _MaskColorTeeth("Teeth Color", Color) = (0.0,0.0,0.0,1.0)
  36. [HideInInspector] _SrcBlend("", Float) = 1
  37. [HideInInspector] _DstBlend("", Float) = 0
  38. }
  39. SubShader
  40. {
  41. Blend [_SrcBlend] [_DstBlend]
  42. Cull Back
  43. CGPROGRAM
  44. #pragma surface surf Standard keepalpha fullforwardshadows
  45. #pragma target 3.0
  46. #pragma fragmentoption ARB_precision_hint_fastest
  47. #include "UnityCG.cginc"
  48. sampler2D _MainTex;
  49. sampler2D _NormalMap;
  50. sampler2D _RoughnessMap;
  51. half4 _BaseColor;
  52. half _Dimmer;
  53. half _Alpha;
  54. half _DiffuseIntensity;
  55. half _SmoothnessMultiplier;
  56. half _SmoothnessMultiplierLips;
  57. half _MetallicMultiplier;
  58. half _RimIntensity;
  59. half _PupilSize;
  60. half _LipSmoothness;
  61. fixed4 _MaskColorIris;
  62. fixed4 _MaskColorLips;
  63. fixed4 _MaskColorBrows;
  64. fixed4 _MaskColorLashes;
  65. fixed4 _MaskColorLashesEnd;
  66. fixed4 _MaskColorSclera;
  67. fixed4 _MaskColorGums;
  68. fixed4 _MaskColorTeeth;
  69. static const int ONE = 1;
  70. static const fixed ALPHA_CLIP_THRESHOLD = 0.7;
  71. static const int IRIS_BRIGHTNESS_MODIFIER = 2;
  72. static const fixed SCLERA_BRIGHTNESS_MODIFIER = 1.2;
  73. static const fixed LIP_SMOOTHNESS_MULTIPLIER = 0.5;
  74. static const fixed LIP_SMOOTHNESS_MIN_NDOTL = 0.3;
  75. static const fixed BROWS_LASHES_DIFFUSEINTENSITY = ONE - 0.25;
  76. static const int COLOR_MULTIPLIER = 255;
  77. static const half2 PUPIL_CENTER_UV = half2(0.127, 0.1175);
  78. static const half DILATION_ENVELOPE = 0.024;
  79. static const half2 EYE_REGION_UV = PUPIL_CENTER_UV + DILATION_ENVELOPE;
  80. static const int MASK_SLICE_SIZE = 17;
  81. static const half MASK_SLICE_THRESHOLD = MASK_SLICE_SIZE * 0.5f;
  82. static const int MASK_INDEX_IRIS = 255;
  83. static const int MASK_INDEX_SCLERA = 238;
  84. static const int MASK_INDEX_LASHES = 221;
  85. static const int MASK_INDEX_LIPS = 204;
  86. static const int MASK_INDEX_GUMS = 187;
  87. static const int MASK_INDEX_TEETH = 170;
  88. static const int MASK_INDEX_BROWS = 153;
  89. struct Input
  90. {
  91. float2 uv_MainTex;
  92. float2 uv_NormalMap;
  93. float2 uv_RoughnessMap;
  94. float3 viewDir;
  95. float3 worldNormal; INTERNAL_DATA
  96. };
  97. void surf(Input IN, inout SurfaceOutputStandard o)
  98. {
  99. // Pupil size offsets uv coords
  100. if (all(IN.uv_MainTex < EYE_REGION_UV))
  101. {
  102. IN.uv_MainTex -= PUPIL_CENTER_UV;
  103. half pupil = saturate(length(IN.uv_MainTex) / DILATION_ENVELOPE);
  104. IN.uv_MainTex *= lerp(ONE, pupil, _PupilSize);
  105. IN.uv_MainTex += PUPIL_CENTER_UV;
  106. }
  107. // Diffuse texture sample
  108. half4 albedoColor = tex2D(_MainTex, IN.uv_MainTex);
  109. // Unpack normal map
  110. #if (UNITY_VERSION >= 20171)
  111. o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
  112. #else
  113. o.Normal = tex2D(_NormalMap, IN.uv_MainTex) * 2.0 - ONE;
  114. #endif
  115. // Roughness contains metallic in r, smoothness in a, mask region in b and mask control in g
  116. half4 roughnessTex = tex2D(_RoughnessMap, IN.uv_MainTex);
  117. // Normal/Light/View calculations
  118. half NdotL = saturate(dot(WorldNormalVector(IN, o.Normal), _WorldSpaceLightPos0.xyz));
  119. half VdotN = saturate(dot(normalize(IN.viewDir), o.Normal));
  120. // Color space conversions if we are in linear
  121. #ifndef UNITY_COLORSPACE_GAMMA
  122. _BaseColor.rgb = LinearToGammaSpace(_BaseColor.rgb);
  123. _MaskColorIris.rgb = LinearToGammaSpace(_MaskColorIris.rgb);
  124. _MaskColorLips.rgb = LinearToGammaSpace(_MaskColorLips.rgb);
  125. _MaskColorBrows.rgb = LinearToGammaSpace(_MaskColorBrows.rgb);
  126. _MaskColorLashes.rgb = LinearToGammaSpace(_MaskColorLashes.rgb);
  127. _MaskColorLashesEnd.rgb = LinearToGammaSpace(_MaskColorLashesEnd.rgb);
  128. _MaskColorSclera.rgb = LinearToGammaSpace(_MaskColorSclera.rgb);
  129. _MaskColorGums.rgb = LinearToGammaSpace(_MaskColorGums.rgb);
  130. _MaskColorTeeth.rgb = LinearToGammaSpace(_MaskColorTeeth.rgb);
  131. #endif
  132. // Mask regions and colors
  133. half irisScalar = abs(roughnessTex.b * COLOR_MULTIPLIER - MASK_INDEX_IRIS) <= MASK_SLICE_THRESHOLD ? roughnessTex.g : 0.0f;
  134. half lipsScalar = abs(roughnessTex.b * COLOR_MULTIPLIER - MASK_INDEX_LIPS) <= MASK_SLICE_THRESHOLD ? roughnessTex.g : 0.0f;
  135. half browsScalar = abs(roughnessTex.b * COLOR_MULTIPLIER - MASK_INDEX_BROWS) <= MASK_SLICE_THRESHOLD ? roughnessTex.g : 0.0f;;
  136. half lashesScalar = abs(roughnessTex.b * COLOR_MULTIPLIER - MASK_INDEX_LASHES) <= MASK_SLICE_THRESHOLD ? roughnessTex.g : 0.0f;
  137. half scleraScalar = abs(roughnessTex.b * COLOR_MULTIPLIER - MASK_INDEX_SCLERA) <= MASK_SLICE_THRESHOLD ? roughnessTex.g : 0.0f;
  138. half teethScalar = abs(roughnessTex.b * COLOR_MULTIPLIER - MASK_INDEX_TEETH) <= MASK_SLICE_THRESHOLD ? roughnessTex.g : 0.0f;;
  139. half gumsScalar = abs(roughnessTex.b * COLOR_MULTIPLIER - MASK_INDEX_GUMS) <= MASK_SLICE_THRESHOLD ? roughnessTex.g : 0.0f;
  140. half3 maskIris = irisScalar * (_MaskColorIris.rgb * IRIS_BRIGHTNESS_MODIFIER - _BaseColor.rgb);
  141. half3 maskBrows = browsScalar * (_MaskColorBrows.rgb - _BaseColor.rgb);
  142. half3 maskLashes = lashesScalar * (_MaskColorLashes.rgb - _BaseColor.rgb);
  143. half3 maskSclera = scleraScalar * (_MaskColorSclera.rgb * SCLERA_BRIGHTNESS_MODIFIER - _BaseColor.rgb);
  144. half3 maskTeeth = teethScalar * (_MaskColorTeeth.rgb - _BaseColor.rgb);
  145. half3 maskGums = gumsScalar * (_MaskColorGums.rgb - _BaseColor.rgb);
  146. // Lip tint excluded from color mask as it lerps with texture color
  147. half3 colorMask = maskIris + maskBrows + maskLashes + maskSclera + maskTeeth + maskGums;
  148. // Set smoothness
  149. o.Smoothness = roughnessTex.a * _SmoothnessMultiplier;
  150. // Force no smoothness on gums & teeth
  151. o.Smoothness *= ONE - saturate(teethScalar + gumsScalar);
  152. // Use global smoothness or lip smoothness modifier
  153. o.Smoothness += (_LipSmoothness * LIP_SMOOTHNESS_MULTIPLIER) * lipsScalar;
  154. // Set metallic with global modifier
  155. o.Metallic = roughnessTex.r * _MetallicMultiplier;
  156. // Brows and lashes modify DiffuseIntensity
  157. _DiffuseIntensity *= ONE - (saturate(browsScalar + lashesScalar) * BROWS_LASHES_DIFFUSEINTENSITY);
  158. // Modify base color with DiffuseIntensity * NdotL for lighting gradient
  159. _BaseColor.rgb += _DiffuseIntensity * NdotL;
  160. // Add in color mask
  161. _BaseColor.rgb += colorMask;
  162. // Multiply texture with base color with special case for lips
  163. o.Albedo.rgb = lerp(albedoColor.rgb * _BaseColor.rgb, _MaskColorLips.rgb, lipsScalar * _MaskColorLips.a);
  164. // Rim term
  165. o.Albedo += pow(ONE - VdotN, _RimIntensity) * NdotL;
  166. // Global dimmer
  167. o.Albedo *= _Dimmer;
  168. // Convert back to linear color space if we are in linear
  169. #if !defined(UNITY_COLORSPACE_GAMMA)
  170. o.Albedo = GammaToLinearSpace(o.Albedo);
  171. #endif
  172. o.Albedo = saturate(o.Albedo);
  173. // Set alpha, with special case for lashes
  174. o.Alpha = saturate(albedoColor.a * lerp(ONE, _Alpha, ONE - lashesScalar) * _Alpha);
  175. // Clip fragments in the lash region for clean lash transparency
  176. clip(o.Alpha - lerp(0.0, ALPHA_CLIP_THRESHOLD, lashesScalar));
  177. }
  178. ENDCG
  179. }
  180. Fallback "Diffuse"
  181. }