Added the project.

This commit is contained in:
Robert Cupisz
2016-11-01 13:25:56 +01:00
parent 93760b331c
commit 947b6de3a5
137 changed files with 33276 additions and 0 deletions

View File

@ -0,0 +1,224 @@
// Based on 'Real-Time Polygonal-Light Shading with Linearly Transformed Cosines'
// https://labs.unity.com/article/real-time-polygonal-light-shading-linearly-transformed-cosines
#if AREA_LIGHT_ENABLE_DIFFUSE
sampler2D _TransformInv_Diffuse;
#endif
sampler2D _TransformInv_Specular;
sampler2D _AmpDiffAmpSpecFresnel;
float4x4 _LightVerts;
half IntegrateEdge(half3 v1, half3 v2)
{
half d = dot(v1,v2);
half theta = acos(max(-0.9999, dot(v1,v2)));
half theta_sintheta = theta / sin(theta);
return theta_sintheta * (v1.x*v2.y - v1.y*v2.x);
}
// Baum's equation
// Expects non-normalized vertex positions
half PolygonRadiance(half4x3 L)
{
// detect clipping config
uint config = 0;
if (L[0].z > 0) config += 1;
if (L[1].z > 0) config += 2;
if (L[2].z > 0) config += 4;
if (L[3].z > 0) config += 8;
// The fifth vertex for cases when clipping cuts off one corner.
// Due to a compiler bug, copying L into a vector array with 5 rows
// messes something up, so we need to stick with the matrix + the L4 vertex.
half3 L4 = L[3];
// This switch is surprisingly fast. Tried replacing it with a lookup array of vertices.
// Even though that replaced the switch with just some indexing and no branches, it became
// way, way slower - mem fetch stalls?
// clip
uint n = 0;
switch(config)
{
case 0: // clip all
break;
case 1: // V1 clip V2 V3 V4
n = 3;
L[1] = -L[1].z * L[0] + L[0].z * L[1];
L[2] = -L[3].z * L[0] + L[0].z * L[3];
break;
case 2: // V2 clip V1 V3 V4
n = 3;
L[0] = -L[0].z * L[1] + L[1].z * L[0];
L[2] = -L[2].z * L[1] + L[1].z * L[2];
break;
case 3: // V1 V2 clip V3 V4
n = 4;
L[2] = -L[2].z * L[1] + L[1].z * L[2];
L[3] = -L[3].z * L[0] + L[0].z * L[3];
break;
case 4: // V3 clip V1 V2 V4
n = 3;
L[0] = -L[3].z * L[2] + L[2].z * L[3];
L[1] = -L[1].z * L[2] + L[2].z * L[1];
break;
case 5: // V1 V3 clip V2 V4: impossible
break;
case 6: // V2 V3 clip V1 V4
n = 4;
L[0] = -L[0].z * L[1] + L[1].z * L[0];
L[3] = -L[3].z * L[2] + L[2].z * L[3];
break;
case 7: // V1 V2 V3 clip V4
n = 5;
L4 = -L[3].z * L[0] + L[0].z * L[3];
L[3] = -L[3].z * L[2] + L[2].z * L[3];
break;
case 8: // V4 clip V1 V2 V3
n = 3;
L[0] = -L[0].z * L[3] + L[3].z * L[0];
L[1] = -L[2].z * L[3] + L[3].z * L[2];
L[2] = L[3];
break;
case 9: // V1 V4 clip V2 V3
n = 4;
L[1] = -L[1].z * L[0] + L[0].z * L[1];
L[2] = -L[2].z * L[3] + L[3].z * L[2];
break;
case 10: // V2 V4 clip V1 V3: impossible
break;
case 11: // V1 V2 V4 clip V3
n = 5;
L[3] = -L[2].z * L[3] + L[3].z * L[2];
L[2] = -L[2].z * L[1] + L[1].z * L[2];
break;
case 12: // V3 V4 clip V1 V2
n = 4;
L[1] = -L[1].z * L[2] + L[2].z * L[1];
L[0] = -L[0].z * L[3] + L[3].z * L[0];
break;
case 13: // V1 V3 V4 clip V2
n = 5;
L[3] = L[2];
L[2] = -L[1].z * L[2] + L[2].z * L[1];
L[1] = -L[1].z * L[0] + L[0].z * L[1];
break;
case 14: // V2 V3 V4 clip V1
n = 5;
L4 = -L[0].z * L[3] + L[3].z * L[0];
L[0] = -L[0].z * L[1] + L[1].z * L[0];
break;
case 15: // V1 V2 V3 V4
n = 4;
break;
}
if (n == 0)
return 0;
// normalize
L[0] = normalize(L[0]);
L[1] = normalize(L[1]);
L[2] = normalize(L[2]);
if(n == 3)
L[3] = L[0];
else
{
L[3] = normalize(L[3]);
if (n == 4)
L4 = L[0];
else
L4 = normalize(L4);
}
// integrate
half sum = 0;
sum += IntegrateEdge(L[0], L[1]);
sum += IntegrateEdge(L[1], L[2]);
sum += IntegrateEdge(L[2], L[3]);
if(n >= 4)
sum += IntegrateEdge(L[3], L4);
if(n == 5)
sum += IntegrateEdge(L4, L[0]);
sum *= 0.15915; // 1/2pi
return max(0, sum);
}
half TransformedPolygonRadiance(half4x3 L, half2 uv, sampler2D transformInv, half amplitude)
{
// Get the inverse LTC matrix M
half3x3 Minv = 0;
Minv._m22 = 1;
Minv._m00_m02_m11_m20 = tex2D(transformInv, uv);
// Transform light vertices into diffuse configuration
half4x3 LTransformed = mul(L, Minv);
// Polygon radiance in transformed configuration - specular
return PolygonRadiance(LTransformed) * amplitude;
}
half3 CalculateLight (half3 position, half3 diffColor, half3 specColor, half oneMinusRoughness, half3 N,
half3 lightPos, half3 lightColor)
{
#if AREA_LIGHT_SHADOWS
half shadow = Shadow(position);
if (shadow == 0.0)
return 0;
#endif
// TODO: larger and smaller values cause artifacts - why?
oneMinusRoughness = clamp(oneMinusRoughness, 0.01, 0.93);
half roughness = 1 - oneMinusRoughness;
half3 V = normalize(_WorldSpaceCameraPos - position);
// Construct orthonormal basis around N, aligned with V
half3x3 basis;
basis[0] = normalize(V - N * dot(V, N));
basis[1] = normalize(cross(N, basis[0]));
basis[2] = N;
// Transform light vertices into that space
half4x3 L;
L = _LightVerts - half4x3(position, position, position, position);
L = mul(L, transpose(basis));
// UVs for sampling the LUTs
half theta = acos(dot(V, N));
half2 uv = half2(roughness, theta/1.57);
half3 AmpDiffAmpSpecFresnel = tex2D(_AmpDiffAmpSpecFresnel, uv).rgb;
half3 result = 0;
#if AREA_LIGHT_ENABLE_DIFFUSE
half diffuseTerm = TransformedPolygonRadiance(L, uv, _TransformInv_Diffuse, AmpDiffAmpSpecFresnel.x);
result = diffuseTerm * diffColor;
#endif
half specularTerm = TransformedPolygonRadiance(L, uv, _TransformInv_Specular, AmpDiffAmpSpecFresnel.y);
half fresnelTerm = specColor + (1.0 - specColor) * AmpDiffAmpSpecFresnel.z;
result += specularTerm * fresnelTerm * UNITY_PI;
#if AREA_LIGHT_SHADOWS
result *= shadow;
#endif
return result * lightColor;
}

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: fd7e771f2b4bdf14da1d4e099b8bd629
timeCreated: 1453121090
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,110 @@
Shader "Hidden/AreaLight" {
SubShader {
Tags { "Queue"="Geometry-1" }
CGINCLUDE
#include "UnityCG.cginc"
#include "UnityPBSLighting.cginc"
#include "UnityDeferredLibrary.cginc"
#define AREA_LIGHT_ENABLE_DIFFUSE 1
#if AREA_LIGHT_SHADOWS
#include "AreaLightShadows.cginc"
#endif
#include "AreaLight.cginc"
sampler2D _CameraGBufferTexture0;
sampler2D _CameraGBufferTexture1;
sampler2D _CameraGBufferTexture2;
void DeferredCalculateLightParams (
unity_v2f_deferred i,
out float3 outWorldPos,
out float2 outUV)
{
i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
float2 uv = i.uv.xy / i.uv.w;
// read depth and reconstruct world position
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv);
depth = Linear01Depth (depth);
float4 vpos = float4(i.ray * depth,1);
float3 wpos = mul (unity_CameraToWorld, vpos).xyz;
outWorldPos = wpos;
outUV = uv;
}
half4 CalculateLightDeferred (unity_v2f_deferred i)
{
float3 worldPos;
float2 uv;
DeferredCalculateLightParams (i, worldPos, uv);
half4 gbuffer0 = tex2D (_CameraGBufferTexture0, uv);
half4 gbuffer1 = tex2D (_CameraGBufferTexture1, uv);
half4 gbuffer2 = tex2D (_CameraGBufferTexture2, uv);
half3 baseColor = gbuffer0.rgb;
half3 specColor = gbuffer1.rgb;
half oneMinusRoughness = gbuffer1.a;
half3 normalWorld = gbuffer2.rgb * 2 - 1;
normalWorld = normalize(normalWorld);
return CalculateLight (worldPos, baseColor, specColor, oneMinusRoughness, normalWorld,
_LightPos.xyz, _LightColor.xyz).rgbb;
}
ENDCG
// shadows
Pass
{
Fog { Mode Off }
ZWrite Off
Blend One One
Cull Front
ZTest Always
CGPROGRAM
#pragma target 3.0
#pragma vertex vert_deferred
#pragma fragment frag
#pragma exclude_renderers nomrt
// only one option, so it will always be set and before any includes
#pragma multi_compile AREA_LIGHT_SHADOWS
fixed4 frag (unity_v2f_deferred i) : SV_Target
{
return CalculateLightDeferred(i);
}
ENDCG
}
// no shadows
Pass
{
Fog { Mode Off }
ZWrite Off
Blend One One
Cull Front
ZTest Always
CGPROGRAM
#pragma target 3.0
#pragma vertex vert_deferred
#pragma fragment frag
#pragma exclude_renderers nomrt
fixed4 frag (unity_v2f_deferred i) : SV_Target
{
return CalculateLightDeferred(i);
}
ENDCG
}
}
Fallback Off
}

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 2b28be3523d190a41910088d1ee68ef7
timeCreated: 1453120999
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,103 @@
cbuffer POISSON_DISKS {
static half2 poisson[40] = {
half2(0.02971195f, 0.8905211f),
half2(0.2495298f, 0.732075f),
half2(-0.3469206f, 0.6437836f),
half2(-0.01878909f, 0.4827394f),
half2(-0.2725213f, 0.896188f),
half2(-0.6814336f, 0.6480481f),
half2(0.4152045f, 0.2794172f),
half2(0.1310554f, 0.2675925f),
half2(0.5344744f, 0.5624411f),
half2(0.8385689f, 0.5137348f),
half2(0.6045052f, 0.08393857f),
half2(0.4643163f, 0.8684642f),
half2(0.335507f, -0.110113f),
half2(0.03007669f, -0.0007075319f),
half2(0.8077537f, 0.2551664f),
half2(-0.1521498f, 0.2429521f),
half2(-0.2997617f, 0.0234927f),
half2(0.2587779f, -0.4226915f),
half2(-0.01448214f, -0.2720358f),
half2(-0.3937779f, -0.228529f),
half2(-0.7833176f, 0.1737299f),
half2(-0.4447537f, 0.2582748f),
half2(-0.9030743f, 0.406874f),
half2(-0.729588f, -0.2115215f),
half2(-0.5383645f, -0.6681151f),
half2(-0.07709587f, -0.5395499f),
half2(-0.3402214f, -0.4782109f),
half2(-0.5580465f, 0.01399586f),
half2(-0.105644f, -0.9191031f),
half2(-0.8343651f, -0.4750755f),
half2(-0.9959937f, -0.0540134f),
half2(0.1747736f, -0.936202f),
half2(-0.3642297f, -0.926432f),
half2(0.1719682f, -0.6798802f),
half2(0.4424475f, -0.7744268f),
half2(0.6849481f, -0.3031401f),
half2(0.5453879f, -0.5152272f),
half2(0.9634013f, -0.2050581f),
half2(0.9907925f, 0.08320642f),
half2(0.8386722f, -0.5428791f)
};
};
Texture2D _Shadowmap;
SamplerComparisonState sampler_Shadowmap;
// To get a sampler, which doesn't do comparison
Texture2D _ShadowmapDummy;
SamplerState sampler_ShadowmapDummy;
half _ShadowReceiverWidth;
half _ShadowReceiverDistanceScale;
half2 _ShadowLightWidth;
half _ShadowBias;
float4x4 _ShadowProjectionMatrix;
half EdgeSmooth(half2 xy)
{
// Magic tweaks to the shape
float corner = 0.4;
float outset = 1.0;
float smooth = 0.5;
float d = length(max(abs(xy) - 1 + corner*outset, 0.0)) - corner;
return saturate(1 - smoothstep(-smooth, 0, d));
}
half Shadow(half3 position)
{
half4 pClip = mul(_ShadowProjectionMatrix, half4(position, 1));
half3 p = pClip.xyz/pClip.w;
if (any(step(1.0, abs(p.xy))))
return 0;
// The texture contains just 0. But we need to sample it somewhere for Unity to initialize the corresponding sampler.
float dist = _ShadowmapDummy.Sample(sampler_ShadowmapDummy, 0).a;
half edgeSmooth = EdgeSmooth(p.xy);
p = p * 0.5 + 0.5;
for(int j = 0; j < 10; ++j)
{
half2 offset = poisson[j + 24] * _ShadowReceiverWidth;
float depth = _Shadowmap.SampleLevel(sampler_ShadowmapDummy, p.xy + offset, 0).r;
dist += max(0.0, p.z - depth);
}
dist *= _ShadowReceiverDistanceScale;
p.z -= _ShadowBias/pClip.w;
half shadow = 0;
for(int i = 0; i < 32; ++i)
{
half lightWidth = lerp(_ShadowLightWidth.x, _ShadowLightWidth.y, min(1.0, dist));
const half2 offset = poisson[i] * lightWidth;
shadow += _Shadowmap.SampleCmpLevelZero(sampler_Shadowmap, p.xy + offset, p.z);
}
return shadow * edgeSmooth / 32.0;
}

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: eeb2994f509cea043bc495c62bc72a29
timeCreated: 1456842264
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,184 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: AreaLightSource
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords: _EMISSION
m_LightmapFlags: 1
m_CustomRenderQueue: -1
stringTagMap: {}
m_SavedProperties:
serializedVersion: 2
m_TexEnvs:
- first:
name: _BumpMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _DetailAlbedoMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _DetailMask
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _DetailNormalMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _EmissionMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _MainTex
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _MetallicGlossMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _OcclusionMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _ParallaxMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _SpecGlossMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- first:
name: _BumpScale
second: 1
- first:
name: _CullMode
second: 2
- first:
name: _Cutoff
second: 0.5
- first:
name: _DetailMode
second: 0
- first:
name: _DetailNormalMapScale
second: 1
- first:
name: _DstBlend
second: 0
- first:
name: _GlossMapScale
second: 1
- first:
name: _Glossiness
second: 0
- first:
name: _GlossyReflections
second: 1
- first:
name: _MetaDiffuseScale
second: 1
- first:
name: _MetaEmissionScale
second: 1
- first:
name: _MetaMetallicSoup
second: 1
- first:
name: _MetaSpecularScale
second: 1
- first:
name: _MetaUseCustom
second: 0
- first:
name: _Metallic
second: 0
- first:
name: _Mode
second: 0
- first:
name: _OcclusionStrength
second: 1
- first:
name: _Parallax
second: 0.02
- first:
name: _SmoothnessTextureChannel
second: 0
- first:
name: _SpecularHighlights
second: 1
- first:
name: _SrcBlend
second: 1
- first:
name: _Translucency
second: 0
- first:
name: _UVDetailMask
second: 0
- first:
name: _UVSec
second: 0
- first:
name: _VertexOcclusionPower
second: 1
- first:
name: _ZWrite
second: 1
m_Colors:
- first:
name: _Color
second: {r: 0, g: 0, b: 0, a: 1}
- first:
name: _EmissionColor
second: {r: 1, g: 1, b: 1, a: 1}
- first:
name: _MetaDiffuseAdd
second: {r: 0, g: 0, b: 0, a: 1}
- first:
name: _MetaDiffuseTint
second: {r: 1, g: 1, b: 1, a: 1}
- first:
name: _MetaEmissionAdd
second: {r: 0, g: 0, b: 0, a: 1}
- first:
name: _MetaEmissionTint
second: {r: 1, g: 1, b: 1, a: 1}
- first:
name: _MetaSpecularAdd
second: {r: 0, g: 0, b: 0, a: 1}
- first:
name: _MetaSpecularTint
second: {r: 1, g: 1, b: 1, a: 1}
- first:
name: _SpecColor
second: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1eb056d70e23cc34a9e926d8c8c887a5
timeCreated: 1450296949
licenseType: Pro
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant: