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.

199 lines
7.7 KiB

  1. //
  2. // OvrAvatar Mobile single component shader
  3. // For use on non-expressive face meshes and other components
  4. //
  5. // Unity vertex-fragnment implementation
  6. // Simplified lighting model recommended for use on mobile supporting one directional light
  7. // Surface shader recommended on PC
  8. //
  9. // Uses transparent queue for fade effects
  10. //
  11. // Simple mouth animation with speech done with vertex perturbation
  12. //
  13. // Shader keywords:
  14. // - SECONDARY_LIGHT_ON SECONDARY_LIGHT_OFF
  15. // Enable SECONDARY_LIGHT_ON for a second "light" comprised of _SecondaryLightDirection and
  16. // _SecondaryLightColor This will influence the rim effect providing a lit contour to the avatar
  17. //
  18. Shader "OvrAvatar/Avatar_Mobile_SingleComponent"
  19. {
  20. Properties
  21. {
  22. [NoScaleOffset] _MainTex("Main Texture", 2D) = "white" {}
  23. [NoScaleOffset] _NormalMap("Normal Map", 2D) = "bump" {}
  24. [NoScaleOffset] _RoughnessMap("Roughness Map", 2D) = "black" {}
  25. _BaseColor("Color Tint", Color) = (1.0,1.0,1.0,1.0)
  26. _Dimmer("Dimmer", Range(0.0,1.0)) = 1.0
  27. _Alpha("Alpha", Range(0.0,1.0)) = 1.0
  28. _DiffuseIntensity("Diffuse Intensity", Range(0.0,1.0)) = 0.3
  29. _RimIntensity("Rim Intensity", Range(0.0,10.0)) = 5.0
  30. _ReflectionIntensity("Reflection Intensity", Range(0.0,1.0)) = 0.0
  31. _Voice("Voice", Range(0.0,1.0)) = 0.0
  32. [HideInInspector] _MouthPosition("Mouth position", Vector) = (0,0,0,1)
  33. [HideInInspector] _MouthDirection("Mouth direction", Vector) = (0,0,0,1)
  34. [HideInInspector] _MouthEffectDistance("Mouth Effect Distance", Float) = 0.03
  35. [HideInInspector] _MouthEffectScale("Mouth Effect Scaler", Float) = 1
  36. [HideInInspector] _SrcBlend("", Float) = 1
  37. [HideInInspector] _DstBlend("", Float) = 0
  38. }
  39. SubShader
  40. {
  41. Tags { "LightMode" = "ForwardBase" "IgnoreProjector" = "True"}
  42. Pass
  43. {
  44. Blend [_SrcBlend] [_DstBlend]
  45. Cull Back
  46. CGPROGRAM
  47. #pragma vertex vert
  48. #pragma fragment frag
  49. #pragma target 3.0
  50. #pragma fragmentoption ARB_precision_hint_fastest
  51. #pragma multi_compile SECONDARY_LIGHT_OFF SECONDARY_LIGHT_ON
  52. #include "UnityCG.cginc"
  53. #include "UnityLightingCommon.cginc"
  54. sampler2D _MainTex;
  55. sampler2D _NormalMap;
  56. float4 _NormalMap_ST;
  57. sampler2D _RoughnessMap;
  58. half4 _BaseColor;
  59. half _Dimmer;
  60. half _Alpha;
  61. half _DiffuseIntensity;
  62. half _RimIntensity;
  63. half _ReflectionIntensity;
  64. half3 _SecondaryLightDirection;
  65. half4 _SecondaryLightColor;
  66. half _Voice;
  67. half4 _MouthPosition;
  68. half4 _MouthDirection;
  69. half _MouthEffectDistance;
  70. half _MouthEffectScale;
  71. static const fixed MOUTH_ZSCALE = 0.5f;
  72. static const fixed MOUTH_DROPOFF = 0.01f;
  73. struct appdata
  74. {
  75. float4 vertex: POSITION;
  76. float3 normal: NORMAL;
  77. float4 tangent: TANGENT;
  78. float4 uv: TEXCOORD0;
  79. };
  80. struct v2f
  81. {
  82. float4 pos : SV_POSITION;
  83. float2 uv : TEXCOORD0;
  84. float4 posWorld: TEXCOORD1;
  85. float3 normalDir: TEXCOORD2;
  86. float3 tangentDir: TEXCOORD3;
  87. float3 bitangentDir: TEXCOORD4;
  88. };
  89. v2f vert(appdata v)
  90. {
  91. v2f o;
  92. // Mouth vertex animation with voice
  93. float4 worldVert = mul(unity_ObjectToWorld, v.vertex);
  94. float3 delta = _MouthPosition - worldVert;
  95. delta.z *= MOUTH_ZSCALE;
  96. half dist = length(delta);
  97. half scaledMouthDropoff = _MouthEffectScale * MOUTH_DROPOFF;
  98. half scaledMouthEffect = _MouthEffectScale * _MouthEffectDistance;
  99. half displacement = _Voice * smoothstep(scaledMouthEffect + scaledMouthDropoff, scaledMouthEffect, dist);
  100. worldVert.xyz -= _MouthDirection * displacement;
  101. v.vertex = mul(unity_WorldToObject, worldVert);
  102. // Calculate tangents for normal mapping
  103. o.normalDir = normalize(UnityObjectToWorldNormal(v.normal));
  104. o.tangentDir = normalize(mul(unity_ObjectToWorld, half4(v.tangent.xyz, 0.0)).xyz);
  105. o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
  106. o.posWorld = worldVert;
  107. o.pos = UnityObjectToClipPos(v.vertex);
  108. o.uv = v.uv;
  109. return o;
  110. }
  111. fixed4 frag(v2f i) : COLOR
  112. {
  113. // Diffuse texture sample
  114. half4 albedoColor = tex2D(_MainTex, i.uv);
  115. // Process normal map
  116. #if (UNITY_VERSION >= 20171)
  117. float3 normalMap = UnpackNormal(tex2D(_NormalMap, i.uv));
  118. #else
  119. float3 normalMap = tex2D(_NormalMap, i.uv) * 2.0 - ONE;
  120. #endif
  121. float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
  122. float3 normalDirection = normalize(mul(normalMap.rgb, tangentTransform));
  123. // Roughness contains metallic in r, smoothness in a, mask region in b and mask control in g
  124. half4 roughnessTex = tex2D(_RoughnessMap, i.uv);
  125. // Normal/Light/View calculations
  126. half3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  127. half VdotN = saturate(dot(viewDirection, normalDirection));
  128. half NdotL = saturate(dot(normalDirection, _WorldSpaceLightPos0.xyz));
  129. // Sample the default reflection cubemap using the reflection vector
  130. float3 worldReflection = reflect(-viewDirection, normalDirection);
  131. half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, worldReflection);
  132. // Decode cubemap data into actual color
  133. half3 reflectionColor = DecodeHDR(skyData, unity_SpecCube0_HDR);
  134. #ifndef UNITY_COLORSPACE_GAMMA
  135. _BaseColor.rgb = LinearToGammaSpace(_BaseColor.rgb);
  136. #endif
  137. // Multiply in base color
  138. albedoColor.rgb *= _BaseColor.rgb;
  139. // Lerp diffuseIntensity with roughness map
  140. _DiffuseIntensity = lerp(_DiffuseIntensity, 1.0, roughnessTex.a);
  141. // Apply main light with a lerp between DiffuseIntensity and 1 based on the roughness
  142. albedoColor.rgb += _DiffuseIntensity * NdotL * _LightColor0;
  143. // Rim term
  144. #ifdef SECONDARY_LIGHT_ON
  145. // Secondary light proxy (direction and color) passed into the rim term
  146. NdotL = saturate(dot(normalDirection, _SecondaryLightDirection));
  147. albedoColor.rgb += pow(1.0 - VdotN, _RimIntensity) * NdotL * _SecondaryLightColor;
  148. #else
  149. albedoColor.rgb += pow(1.0 - VdotN, _RimIntensity) * NdotL;
  150. #endif
  151. // Reflection from cubemap
  152. albedoColor.rgb += reflectionColor * (roughnessTex.a * _ReflectionIntensity) * NdotL;
  153. // Global dimmer
  154. albedoColor.rgb *= _Dimmer;
  155. // Convert back to linear color space if we are in linear
  156. #if !defined(UNITY_COLORSPACE_GAMMA)
  157. albedoColor.rgb = GammaToLinearSpace(albedoColor.rgb);
  158. #endif
  159. albedoColor.rgb = saturate(albedoColor.rgb);
  160. // Set alpha
  161. albedoColor.a *= _Alpha;
  162. // Return clamped final color
  163. return albedoColor;
  164. }
  165. ENDCG
  166. }
  167. }
  168. Fallback "Diffuse"
  169. }