|
|
|
|
|
|
// Else we have lightprobe for dynamic/moving entity. Either SH9 per object lightprobe or SH4 per pixel per object volume probe |
|
|
|
float3 SampleBakedGI(float3 positionWS, float3 normalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap) |
|
|
|
{ |
|
|
|
// If there is no lightmap, it assume lightprobe |
|
|
|
// If there is no lightmap, it assume lightprobe |
|
|
|
if (unity_ProbeVolumeParams.x == 0.0) |
|
|
|
{ |
|
|
|
// TODO: pass a tab of coefficient instead! |
|
|
|
float4 SHCoefficients[7]; |
|
|
|
SHCoefficients[0] = unity_SHAr; |
|
|
|
SHCoefficients[1] = unity_SHAg; |
|
|
|
SHCoefficients[2] = unity_SHAb; |
|
|
|
SHCoefficients[3] = unity_SHBr; |
|
|
|
SHCoefficients[4] = unity_SHBg; |
|
|
|
SHCoefficients[5] = unity_SHBb; |
|
|
|
SHCoefficients[6] = unity_SHC; |
|
|
|
if (unity_ProbeVolumeParams.x == 0.0) |
|
|
|
{ |
|
|
|
// TODO: pass a tab of coefficient instead! |
|
|
|
float4 SHCoefficients[7]; |
|
|
|
SHCoefficients[0] = unity_SHAr; |
|
|
|
SHCoefficients[1] = unity_SHAg; |
|
|
|
SHCoefficients[2] = unity_SHAb; |
|
|
|
SHCoefficients[3] = unity_SHBr; |
|
|
|
SHCoefficients[4] = unity_SHBg; |
|
|
|
SHCoefficients[5] = unity_SHBb; |
|
|
|
SHCoefficients[6] = unity_SHC; |
|
|
|
return SampleSH9(SHCoefficients, normalWS); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// TODO: Move all this to C++! |
|
|
|
float4x4 identity = 0; |
|
|
|
identity._m00_m11_m22_m33 = 1.0; |
|
|
|
float4x4 WorldToTexture = (unity_ProbeVolumeParams.y == 1.0f) ? unity_ProbeVolumeWorldToObject : identity; |
|
|
|
return SampleSH9(SHCoefficients, normalWS); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// TODO: Move all this to C++! |
|
|
|
float4x4 identity = 0; |
|
|
|
identity._m00_m11_m22_m33 = 1.0; |
|
|
|
float4x4 WorldToTexture = (unity_ProbeVolumeParams.y == 1.0f) ? unity_ProbeVolumeWorldToObject : identity; |
|
|
|
float4x4 translation = identity; |
|
|
|
translation._m30_m31_m32 = -unity_ProbeVolumeMin.xyz; |
|
|
|
float4x4 translation = identity; |
|
|
|
translation._m30_m31_m32 = -unity_ProbeVolumeMin.xyz; |
|
|
|
float4x4 scale = 0; |
|
|
|
scale._m00_m11_m22_m33 = float4(unity_ProbeVolumeSizeInv.xyz, 1.0); |
|
|
|
float4x4 scale = 0; |
|
|
|
scale._m00_m11_m22_m33 = float4(unity_ProbeVolumeSizeInv.xyz, 1.0); |
|
|
|
WorldToTexture = mul(mul(scale, translation), WorldToTexture); |
|
|
|
|
|
|
|
return SampleProbeVolumeSH4(TEXTURE3D_PASS(unity_ProbeVolumeSH, samplerunity_ProbeVolumeSH), positionWS, normalWS, WorldToTexture, unity_ProbeVolumeParams.z); |
|
|
|
} |
|
|
|
WorldToTexture = mul(mul(scale, translation), WorldToTexture); |
|
|
|
|
|
|
|
return SampleProbeVolumeSH4(TEXTURE3D_PASS(unity_ProbeVolumeSH, samplerunity_ProbeVolumeSH), positionWS, normalWS, WorldToTexture, unity_ProbeVolumeParams.z); |
|
|
|
} |
|
|
|
float3 bakeDiffuseLighting = float3(0.0, 0.0, 0.0); |
|
|
|
float3 bakeDiffuseLighting = float3(0.0, 0.0, 0.0); |
|
|
|
#ifdef LIGHTMAP_ON |
|
|
|
#ifdef DIRLIGHTMAP_COMBINED |
|
|
|
bakeDiffuseLighting += SampleDirectionalLightmap(TEXTURE2D_PASS(unity_Lightmap, samplerunity_Lightmap), |
|
|
|
TEXTURE2D_PASS(unity_LightmapInd, samplerunity_Lightmap), |
|
|
|
uvStaticLightmap, unity_LightmapST); |
|
|
|
#else |
|
|
|
bakeDiffuseLighting += SampleSingleLightmap(TEXTURE2D_PASS(unity_Lightmap, samplerunity_Lightmap), uvStaticLightmap, unity_LightmapST); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
#ifdef LIGHTMAP_ON |
|
|
|
#ifdef DIRLIGHTMAP_COMBINED |
|
|
|
bakeDiffuseLighting += SampleDirectionalLightmap(TEXTURE2D_PASS(unity_Lightmap, samplerunity_Lightmap), |
|
|
|
TEXTURE2D_PASS(unity_LightmapInd, samplerunity_Lightmap), |
|
|
|
uvStaticLightmap, unity_LightmapST); |
|
|
|
#else |
|
|
|
bakeDiffuseLighting += SampleSingleLightmap(TEXTURE2D_PASS(unity_Lightmap, samplerunity_Lightmap), uvStaticLightmap, unity_LightmapST); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
#ifdef DYNAMICLIGHTMAP_ON |
|
|
|
#ifdef DIRLIGHTMAP_COMBINED |
|
|
|
bakeDiffuseLighting += SampleDirectionalLightmap(TEXTURE2D_PASS(unity_DynamicLightmap, samplerunity_DynamicLightmap), |
|
|
|
TEXTURE2D_PASS(unity_DynamicDirectionality, samplerunity_DynamicLightmap), |
|
|
|
uvDynamicLightmap, unity_DynamicLightmapST); |
|
|
|
#else |
|
|
|
bakeDiffuseLighting += SampleSingleLightmap(TEXTURE2D_PASS(unity_DynamicLightmap, samplerunity_DynamicLightmap), uvDynamicLightmap, unity_DynamicLightmapST); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
#ifdef DYNAMICLIGHTMAP_ON |
|
|
|
#ifdef DIRLIGHTMAP_COMBINED |
|
|
|
bakeDiffuseLighting += SampleDirectionalLightmap(TEXTURE2D_PASS(unity_DynamicLightmap, samplerunity_DynamicLightmap), |
|
|
|
TEXTURE2D_PASS(unity_DynamicDirectionality, samplerunity_DynamicLightmap), |
|
|
|
uvDynamicLightmap, unity_DynamicLightmapST); |
|
|
|
#else |
|
|
|
bakeDiffuseLighting += SampleSingleLightmap(TEXTURE2D_PASS(unity_DynamicLightmap, samplerunity_DynamicLightmap), uvDynamicLightmap, unity_DynamicLightmapST); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
return bakeDiffuseLighting; |
|
|
|
return bakeDiffuseLighting; |
|
|
|
|
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
#endif |
|
|
|
surfaceData.metallic *= _Metallic; |
|
|
|
|
|
|
|
// TODO: think about using BC5 |
|
|
|
#ifdef _TANGENTMAP |
|
|
|
#ifdef _NORMALMAP_TANGENT_SPACE // Normal and tangent use same space |
|
|
|
float3 tangentTS = UnpackNormalAG(SAMPLE_TEXTURE2D(_TangentMap, sampler_TangentMap, input.texCoord0)); |
|
|
|
surfaceData.tangentWS = TransformTangentToWorld(tangentTS, input.tangentToWorld); |
|
|
|
#else // Object space (TODO: We need to apply the world rotation here! - Require to pass world transform) |
|
|
|
surfaceData.tangentWS = SAMPLE_TEXTURE2D(_TangentMap, sampler_TangentMap, input.texCoord0).rgb; |
|
|
|
#endif |
|
|
|
#else |
|
|
|
surfaceData.tangentWS = input.tangentToWorld[0].xyz; |
|
|
|
#endif |
|
|
|
// TODO: Is there anything todo regarding flip normal but for the tangent ? |
|
|
|
surfaceData.tangentWS = input.tangentToWorld[0].xyz; // TODO: do with tangent same as with normal, sample into texture etc... |
|
|
|
surfaceData.anisotropy = 0; |
|
|
|
#ifdef _ANISOTROPYMAP |
|
|
|
surfaceData.anisotropy = SAMPLE_TEXTURE2D(_AnisotropyMap, sampler_AnisotropyMap, input.texCoord0).r; |
|
|
|
#else |
|
|
|
surfaceData.anisotropy = 1.0; |
|
|
|
#endif |
|
|
|
surfaceData.anisotropy *= _Anisotropy; |
|
|
|
|
|
|
|
surfaceData.specular = 0.04; |
|
|
|
|
|
|
|
surfaceData.subSurfaceRadius = 1.0; |
|
|
|
|
|
|
// TODO: Sample lightmap/lightprobe/volume proxy |
|
|
|
// This should also handle projective lightmap |
|
|
|
// Note that data input above can be use to sample into lightmap (like normal) |
|
|
|
builtinData.bakeDiffuseLighting = SampleBakedGI(input.positionWS, surfaceData.normalWS, input.texCoord1, input.texCoord2); |
|
|
|
builtinData.bakeDiffuseLighting = SampleBakedGI(input.positionWS, surfaceData.normalWS, input.texCoord1, input.texCoord2); |
|
|
|
|
|
|
|
// If we chose an emissive color, we have a dedicated texture for it and don't use MaskMap |
|
|
|
#ifdef _EMISSIVE_COLOR |
|
|
|