Skip to content

Commit 27cbbd5

Browse files
Merge pull request #40 from Unity-Technologies/Create-MaterialUtilities
Create material utilities
2 parents a797aa7 + fc2b822 commit 27cbbd5

File tree

3 files changed

+96
-87
lines changed

3 files changed

+96
-87
lines changed

Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitData.hlsl

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,7 @@
1-
21
//-------------------------------------------------------------------------------------
32
// Fill SurfaceData/Builtin data function
43
//-------------------------------------------------------------------------------------
5-
6-
// In unity we can have a mix of fully baked lightmap (static lightmap) + enlighten realtime lightmap (dynamic lightmap)
7-
// for each case we can have directional lightmap or not.
8-
// Else we have lightprobe for dynamic/moving entity. Either SH9 per object lightprobe or SH4 per pixel per object volume probe
9-
float3 SampleBakedGI(float3 positionWS, float3 normalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap)
10-
{
11-
// If there is no lightmap, it assume lightprobe
12-
#if !defined(LIGHTMAP_ON) && !defined(DYNAMICLIGHTMAP_ON)
13-
14-
// TODO: Confirm with Ionut but it seems that UNITY_LIGHT_PROBE_PROXY_VOLUME is always define for high end and
15-
// unity_ProbeVolumeParams always bind.
16-
if (unity_ProbeVolumeParams.x == 0.0)
17-
{
18-
// TODO: pass a tab of coefficient instead!
19-
float4 SHCoefficients[7];
20-
SHCoefficients[0] = unity_SHAr;
21-
SHCoefficients[1] = unity_SHAg;
22-
SHCoefficients[2] = unity_SHAb;
23-
SHCoefficients[3] = unity_SHBr;
24-
SHCoefficients[4] = unity_SHBg;
25-
SHCoefficients[5] = unity_SHBb;
26-
SHCoefficients[6] = unity_SHC;
27-
28-
return SampleSH9(SHCoefficients, normalWS);
29-
}
30-
else
31-
{
32-
// TODO: Move all this to C++!
33-
float4x4 identity = 0;
34-
identity._m00_m11_m22_m33 = 1.0;
35-
float4x4 WorldToTexture = (unity_ProbeVolumeParams.y == 1.0f) ? unity_ProbeVolumeWorldToObject : identity;
36-
37-
float4x4 translation = identity;
38-
translation._m30_m31_m32 = -unity_ProbeVolumeMin.xyz;
39-
40-
float4x4 scale = 0;
41-
scale._m00_m11_m22_m33 = float4(unity_ProbeVolumeSizeInv.xyz, 1.0);
42-
43-
WorldToTexture = mul(mul(scale, translation), WorldToTexture);
44-
45-
return SampleProbeVolumeSH4(TEXTURE3D_PARAM(unity_ProbeVolumeSH, samplerunity_ProbeVolumeSH), positionWS, normalWS, WorldToTexture, unity_ProbeVolumeParams.z);
46-
}
47-
48-
#else
49-
50-
float3 bakeDiffuseLighting = float3(0.0, 0.0, 0.0);
51-
52-
#ifdef LIGHTMAP_ON
53-
#ifdef DIRLIGHTMAP_COMBINED
54-
bakeDiffuseLighting += SampleDirectionalLightmap(TEXTURE2D_PARAM(unity_Lightmap, samplerunity_Lightmap),
55-
TEXTURE2D_PARAM(unity_LightmapInd, samplerunity_Lightmap),
56-
uvStaticLightmap, unity_LightmapST, normalWS);
57-
#else
58-
bakeDiffuseLighting += SampleSingleLightmap(TEXTURE2D_PARAM(unity_Lightmap, samplerunity_Lightmap), uvStaticLightmap, unity_LightmapST);
59-
#endif
60-
#endif
61-
62-
#ifdef DYNAMICLIGHTMAP_ON
63-
#ifdef DIRLIGHTMAP_COMBINED
64-
bakeDiffuseLighting += SampleDirectionalLightmap(TEXTURE2D_PARAM(unity_DynamicLightmap, samplerunity_DynamicLightmap),
65-
TEXTURE2D_PARAM(unity_DynamicDirectionality, samplerunity_DynamicLightmap),
66-
uvDynamicLightmap, unity_DynamicLightmapST, normalWS);
67-
#else
68-
bakeDiffuseLighting += SampleSingleLightmap(TEXTURE2D_PARAM(unity_DynamicLightmap, samplerunity_DynamicLightmap), uvDynamicLightmap, unity_DynamicLightmapST);
69-
#endif
70-
#endif
71-
72-
return bakeDiffuseLighting;
73-
74-
#endif
75-
}
76-
77-
float2 CalculateVelocity(float4 positionCS, float4 previousPositionCS)
78-
{
79-
// This test on define is required to remove warning of divide by 0 when initializing empty struct
80-
// TODO: Add forward opaque MRT case...
81-
#if (SHADERPASS == SHADERPASS_VELOCITY) || (SHADERPASS == SHADERPASS_GBUFFER && SHADEROPTIONS_VELOCITY_IN_GBUFFER)
82-
// Encode velocity
83-
positionCS.xy = positionCS.xy / positionCS.w;
84-
previousPositionCS.xy = previousPositionCS.xy / previousPositionCS.w;
85-
86-
return (positionCS.xy - previousPositionCS.xy) * _ForceNoMotion;
87-
#else
88-
return float2(0.0, 0.0);
89-
#endif
90-
}
4+
#include "../MaterialUtilities.hlsl"
915

926
void GetBuiltinData(FragInput input, SurfaceData surfaceData, float alpha, out BuiltinData builtinData)
937
{
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// In unity we can have a mix of fully baked lightmap (static lightmap) + enlighten realtime lightmap (dynamic lightmap)
2+
// for each case we can have directional lightmap or not.
3+
// Else we have lightprobe for dynamic/moving entity. Either SH9 per object lightprobe or SH4 per pixel per object volume probe
4+
float3 SampleBakedGI(float3 positionWS, float3 normalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap)
5+
{
6+
// If there is no lightmap, it assume lightprobe
7+
#if !defined(LIGHTMAP_ON) && !defined(DYNAMICLIGHTMAP_ON)
8+
9+
// TODO: Confirm with Ionut but it seems that UNITY_LIGHT_PROBE_PROXY_VOLUME is always define for high end and
10+
// unity_ProbeVolumeParams always bind.
11+
if (unity_ProbeVolumeParams.x == 0.0)
12+
{
13+
// TODO: pass a tab of coefficient instead!
14+
float4 SHCoefficients[7];
15+
SHCoefficients[0] = unity_SHAr;
16+
SHCoefficients[1] = unity_SHAg;
17+
SHCoefficients[2] = unity_SHAb;
18+
SHCoefficients[3] = unity_SHBr;
19+
SHCoefficients[4] = unity_SHBg;
20+
SHCoefficients[5] = unity_SHBb;
21+
SHCoefficients[6] = unity_SHC;
22+
23+
return SampleSH9(SHCoefficients, normalWS);
24+
}
25+
else
26+
{
27+
// TODO: Move all this to C++!
28+
float4x4 identity = 0;
29+
identity._m00_m11_m22_m33 = 1.0;
30+
float4x4 WorldToTexture = (unity_ProbeVolumeParams.y == 1.0f) ? unity_ProbeVolumeWorldToObject : identity;
31+
32+
float4x4 translation = identity;
33+
translation._m30_m31_m32 = -unity_ProbeVolumeMin.xyz;
34+
35+
float4x4 scale = 0;
36+
scale._m00_m11_m22_m33 = float4(unity_ProbeVolumeSizeInv.xyz, 1.0);
37+
38+
WorldToTexture = mul(mul(scale, translation), WorldToTexture);
39+
40+
return SampleProbeVolumeSH4(TEXTURE3D_PARAM(unity_ProbeVolumeSH, samplerunity_ProbeVolumeSH), positionWS, normalWS, WorldToTexture, unity_ProbeVolumeParams.z);
41+
}
42+
43+
#else
44+
45+
float3 bakeDiffuseLighting = float3(0.0, 0.0, 0.0);
46+
47+
#ifdef LIGHTMAP_ON
48+
#ifdef DIRLIGHTMAP_COMBINED
49+
bakeDiffuseLighting += SampleDirectionalLightmap(TEXTURE2D_PARAM(unity_Lightmap, samplerunity_Lightmap),
50+
TEXTURE2D_PARAM(unity_LightmapInd, samplerunity_Lightmap),
51+
uvStaticLightmap, unity_LightmapST, normalWS);
52+
#else
53+
bakeDiffuseLighting += SampleSingleLightmap(TEXTURE2D_PARAM(unity_Lightmap, samplerunity_Lightmap), uvStaticLightmap, unity_LightmapST);
54+
#endif
55+
#endif
56+
57+
#ifdef DYNAMICLIGHTMAP_ON
58+
#ifdef DIRLIGHTMAP_COMBINED
59+
bakeDiffuseLighting += SampleDirectionalLightmap(TEXTURE2D_PARAM(unity_DynamicLightmap, samplerunity_DynamicLightmap),
60+
TEXTURE2D_PARAM(unity_DynamicDirectionality, samplerunity_DynamicLightmap),
61+
uvDynamicLightmap, unity_DynamicLightmapST, normalWS);
62+
#else
63+
bakeDiffuseLighting += SampleSingleLightmap(TEXTURE2D_PARAM(unity_DynamicLightmap, samplerunity_DynamicLightmap), uvDynamicLightmap, unity_DynamicLightmapST);
64+
#endif
65+
#endif
66+
67+
return bakeDiffuseLighting;
68+
69+
#endif
70+
}
71+
72+
float2 CalculateVelocity(float4 positionCS, float4 previousPositionCS)
73+
{
74+
// This test on define is required to remove warning of divide by 0 when initializing empty struct
75+
// TODO: Add forward opaque MRT case...
76+
#if (SHADERPASS == SHADERPASS_VELOCITY) || (SHADERPASS == SHADERPASS_GBUFFER && SHADEROPTIONS_VELOCITY_IN_GBUFFER)
77+
// Encode velocity
78+
positionCS.xy = positionCS.xy / positionCS.w;
79+
previousPositionCS.xy = previousPositionCS.xy / previousPositionCS.w;
80+
81+
return (positionCS.xy - previousPositionCS.xy) * _ForceNoMotion;
82+
#else
83+
return float2(0.0, 0.0);
84+
#endif
85+
}

Assets/ScriptableRenderLoop/HDRenderLoop/Material/MaterialUtilities.hlsl.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)