This commit is contained in:
2021-06-13 10:28:03 +02:00
parent eb70603c85
commit df2d24cbd3
7487 changed files with 943244 additions and 0 deletions

View File

@@ -0,0 +1,306 @@
#ifndef UNITY_FIBONACCI_INCLUDED
#define UNITY_FIBONACCI_INCLUDED
#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3
#pragma warning (disable : 3205) // conversion of larger type to smaller
#endif
// Computes a point using the Fibonacci sequence of length N.
// Input: Fib[N - 1], Fib[N - 2], and the index 'i' of the point.
// Ref: Efficient Quadrature Rules for Illumination Integrals
real2 Fibonacci2dSeq(real fibN1, real fibN2, uint i)
{
// 3 cycles on GCN if 'fibN1' and 'fibN2' are known at compile time.
// N.b.: According to Swinbank and Pusser [SP06], the uniformity of the distribution
// can be slightly improved by introducing an offset of 1/N to the Z (or R) coordinates.
return real2(i / fibN1 + (0.5 / fibN1), frac(i * (fibN2 / fibN1)));
}
#define GOLDEN_RATIO 1.618033988749895
#define GOLDEN_ANGLE 2.399963229728653
// Replaces the Fibonacci sequence in Fibonacci2dSeq() with the Golden ratio.
real2 Golden2dSeq(uint i, real n)
{
// GoldenAngle = 2 * Pi * (1 - 1 / GoldenRatio).
// We can drop the "1 -" part since all it does is reverse the orientation.
return real2(i / n + (0.5 / n), frac(i * rcp(GOLDEN_RATIO)));
}
static const uint k_FibonacciSeq[] = {
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
};
static const real2 k_Fibonacci2dSeq21[] = {
real2(0.02380952, 0.00000000),
real2(0.07142857, 0.61904764),
real2(0.11904762, 0.23809528),
real2(0.16666667, 0.85714293),
real2(0.21428572, 0.47619057),
real2(0.26190478, 0.09523821),
real2(0.30952382, 0.71428585),
real2(0.35714287, 0.33333349),
real2(0.40476191, 0.95238113),
real2(0.45238096, 0.57142878),
real2(0.50000000, 0.19047642),
real2(0.54761904, 0.80952406),
real2(0.59523809, 0.42857170),
real2(0.64285713, 0.04761887),
real2(0.69047618, 0.66666698),
real2(0.73809522, 0.28571510),
real2(0.78571427, 0.90476227),
real2(0.83333331, 0.52380943),
real2(0.88095236, 0.14285755),
real2(0.92857140, 0.76190567),
real2(0.97619045, 0.38095284)
};
static const real2 k_Fibonacci2dSeq34[] = {
real2(0.01470588, 0.00000000),
real2(0.04411765, 0.61764705),
real2(0.07352941, 0.23529410),
real2(0.10294118, 0.85294116),
real2(0.13235295, 0.47058821),
real2(0.16176471, 0.08823538),
real2(0.19117647, 0.70588231),
real2(0.22058824, 0.32352924),
real2(0.25000000, 0.94117641),
real2(0.27941176, 0.55882359),
real2(0.30882353, 0.17647076),
real2(0.33823529, 0.79411745),
real2(0.36764705, 0.41176462),
real2(0.39705881, 0.02941132),
real2(0.42647058, 0.64705849),
real2(0.45588234, 0.26470566),
real2(0.48529410, 0.88235283),
real2(0.51470590, 0.50000000),
real2(0.54411763, 0.11764717),
real2(0.57352942, 0.73529434),
real2(0.60294116, 0.35294151),
real2(0.63235295, 0.97058773),
real2(0.66176468, 0.58823490),
real2(0.69117647, 0.20588207),
real2(0.72058821, 0.82352924),
real2(0.75000000, 0.44117641),
real2(0.77941179, 0.05882263),
real2(0.80882353, 0.67646980),
real2(0.83823532, 0.29411697),
real2(0.86764705, 0.91176414),
real2(0.89705884, 0.52941132),
real2(0.92647058, 0.14705849),
real2(0.95588237, 0.76470566),
real2(0.98529410, 0.38235283)
};
static const real2 k_Fibonacci2dSeq55[] = {
real2(0.00909091, 0.00000000),
real2(0.02727273, 0.61818182),
real2(0.04545455, 0.23636365),
real2(0.06363636, 0.85454547),
real2(0.08181818, 0.47272730),
real2(0.10000000, 0.09090900),
real2(0.11818182, 0.70909095),
real2(0.13636364, 0.32727289),
real2(0.15454546, 0.94545460),
real2(0.17272727, 0.56363630),
real2(0.19090909, 0.18181801),
real2(0.20909090, 0.80000019),
real2(0.22727273, 0.41818190),
real2(0.24545455, 0.03636360),
real2(0.26363635, 0.65454578),
real2(0.28181818, 0.27272701),
real2(0.30000001, 0.89090919),
real2(0.31818181, 0.50909138),
real2(0.33636364, 0.12727261),
real2(0.35454544, 0.74545479),
real2(0.37272727, 0.36363602),
real2(0.39090911, 0.98181820),
real2(0.40909091, 0.60000038),
real2(0.42727274, 0.21818161),
real2(0.44545454, 0.83636379),
real2(0.46363637, 0.45454597),
real2(0.48181817, 0.07272720),
real2(0.50000000, 0.69090843),
real2(0.51818180, 0.30909157),
real2(0.53636366, 0.92727280),
real2(0.55454546, 0.54545403),
real2(0.57272726, 0.16363716),
real2(0.59090906, 0.78181839),
real2(0.60909092, 0.39999962),
real2(0.62727273, 0.01818275),
real2(0.64545453, 0.63636398),
real2(0.66363639, 0.25454521),
real2(0.68181819, 0.87272835),
real2(0.69999999, 0.49090958),
real2(0.71818179, 0.10909081),
real2(0.73636365, 0.72727203),
real2(0.75454545, 0.34545517),
real2(0.77272725, 0.96363640),
real2(0.79090911, 0.58181763),
real2(0.80909091, 0.20000076),
real2(0.82727271, 0.81818199),
real2(0.84545457, 0.43636322),
real2(0.86363637, 0.05454636),
real2(0.88181818, 0.67272758),
real2(0.89999998, 0.29090881),
real2(0.91818184, 0.90909195),
real2(0.93636364, 0.52727318),
real2(0.95454544, 0.14545441),
real2(0.97272730, 0.76363754),
real2(0.99090910, 0.38181686)
};
static const real2 k_Fibonacci2dSeq89[] = {
real2(0.00561798, 0.00000000),
real2(0.01685393, 0.61797750),
real2(0.02808989, 0.23595500),
real2(0.03932584, 0.85393250),
real2(0.05056180, 0.47191000),
real2(0.06179775, 0.08988762),
real2(0.07303371, 0.70786500),
real2(0.08426967, 0.32584238),
real2(0.09550562, 0.94382000),
real2(0.10674157, 0.56179762),
real2(0.11797753, 0.17977524),
real2(0.12921348, 0.79775238),
real2(0.14044943, 0.41573000),
real2(0.15168539, 0.03370762),
real2(0.16292135, 0.65168476),
real2(0.17415731, 0.26966286),
real2(0.18539326, 0.88764000),
real2(0.19662921, 0.50561714),
real2(0.20786516, 0.12359524),
real2(0.21910113, 0.74157238),
real2(0.23033708, 0.35955048),
real2(0.24157304, 0.97752762),
real2(0.25280899, 0.59550476),
real2(0.26404494, 0.21348286),
real2(0.27528089, 0.83146000),
real2(0.28651685, 0.44943714),
real2(0.29775280, 0.06741524),
real2(0.30898875, 0.68539238),
real2(0.32022473, 0.30336952),
real2(0.33146068, 0.92134666),
real2(0.34269664, 0.53932571),
real2(0.35393259, 0.15730286),
real2(0.36516854, 0.77528000),
real2(0.37640449, 0.39325714),
real2(0.38764045, 0.01123428),
real2(0.39887640, 0.62921333),
real2(0.41011235, 0.24719048),
real2(0.42134830, 0.86516762),
real2(0.43258426, 0.48314476),
real2(0.44382024, 0.10112190),
real2(0.45505619, 0.71910095),
real2(0.46629214, 0.33707809),
real2(0.47752810, 0.95505524),
real2(0.48876405, 0.57303238),
real2(0.50000000, 0.19100952),
real2(0.51123595, 0.80898666),
real2(0.52247190, 0.42696571),
real2(0.53370786, 0.04494286),
real2(0.54494381, 0.66292000),
real2(0.55617976, 0.28089714),
real2(0.56741571, 0.89887428),
real2(0.57865167, 0.51685333),
real2(0.58988762, 0.13483047),
real2(0.60112357, 0.75280762),
real2(0.61235952, 0.37078476),
real2(0.62359548, 0.98876190),
real2(0.63483149, 0.60673904),
real2(0.64606744, 0.22471619),
real2(0.65730339, 0.84269333),
real2(0.66853935, 0.46067429),
real2(0.67977530, 0.07865143),
real2(0.69101125, 0.69662857),
real2(0.70224720, 0.31460571),
real2(0.71348315, 0.93258286),
real2(0.72471911, 0.55056000),
real2(0.73595506, 0.16853714),
real2(0.74719101, 0.78651428),
real2(0.75842696, 0.40449142),
real2(0.76966292, 0.02246857),
real2(0.78089887, 0.64044571),
real2(0.79213482, 0.25842667),
real2(0.80337077, 0.87640381),
real2(0.81460673, 0.49438095),
real2(0.82584268, 0.11235809),
real2(0.83707863, 0.73033524),
real2(0.84831458, 0.34831238),
real2(0.85955054, 0.96628952),
real2(0.87078649, 0.58426666),
real2(0.88202250, 0.20224380),
real2(0.89325845, 0.82022095),
real2(0.90449440, 0.43820190),
real2(0.91573036, 0.05617905),
real2(0.92696631, 0.67415619),
real2(0.93820226, 0.29213333),
real2(0.94943821, 0.91011047),
real2(0.96067417, 0.52808762),
real2(0.97191012, 0.14606476),
real2(0.98314607, 0.76404190),
real2(0.99438202, 0.38201904)
};
// Loads elements from one of the precomputed tables for sample counts of 21, 34, 55, and 89.
// Computes sample positions at runtime otherwise.
// Sample count must be a Fibonacci number (see 'k_FibonacciSeq').
real2 Fibonacci2d(uint i, uint sampleCount)
{
switch (sampleCount)
{
case 21: return k_Fibonacci2dSeq21[i];
case 34: return k_Fibonacci2dSeq34[i];
case 55: return k_Fibonacci2dSeq55[i];
case 89: return k_Fibonacci2dSeq89[i];
default:
{
uint fibN1 = sampleCount;
uint fibN2 = sampleCount;
// These are all constants, so this loop will be optimized away.
for (uint j = 1; j < 20; j++)
{
if (k_FibonacciSeq[j] == fibN1)
{
fibN2 = k_FibonacciSeq[j - 1];
}
}
return Fibonacci2dSeq(fibN1, fibN2, i);
}
}
}
real2 SampleDiskGolden(uint i, uint sampleCount)
{
real2 f = Golden2dSeq(i, sampleCount);
return real2(sqrt(f.x), TWO_PI * f.y);
}
// Returns the radius as the X coordinate, and the angle as the Y coordinate.
real2 SampleDiskFibonacci(uint i, uint sampleCount)
{
real2 f = Fibonacci2d(i, sampleCount);
return real2(sqrt(f.x), TWO_PI * f.y);
}
// Returns the zenith as the X coordinate, and the azimuthal angle as the Y coordinate.
real2 SampleHemisphereFibonacci(uint i, uint sampleCount)
{
real2 f = Fibonacci2d(i, sampleCount);
return real2(1 - f.x, TWO_PI * f.y);
}
// Returns the zenith as the X coordinate, and the azimuthal angle as the Y coordinate.
real2 SampleSphereFibonacci(uint i, uint sampleCount)
{
real2 f = Fibonacci2d(i, sampleCount);
return real2(1 - 2 * f.x, TWO_PI * f.y);
}
#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3
#pragma warning (enable : 3205) // conversion of larger type to smaller
#endif
#endif // UNITY_FIBONACCI_INCLUDED

View File

@@ -0,0 +1,431 @@
#ifndef UNITY_HAMMERSLEY_INCLUDED
#define UNITY_HAMMERSLEY_INCLUDED
#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3
#pragma warning (disable : 3205) // conversion of larger type to smaller
#endif
// Ref: http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
uint ReverseBits32(uint bits)
{
#if (SHADER_TARGET >= 45)
return reversebits(bits);
#else
bits = (bits << 16) | (bits >> 16);
bits = ((bits & 0x00ff00ff) << 8) | ((bits & 0xff00ff00) >> 8);
bits = ((bits & 0x0f0f0f0f) << 4) | ((bits & 0xf0f0f0f0) >> 4);
bits = ((bits & 0x33333333) << 2) | ((bits & 0xcccccccc) >> 2);
bits = ((bits & 0x55555555) << 1) | ((bits & 0xaaaaaaaa) >> 1);
return bits;
#endif
}
real VanDerCorputBase2(uint i)
{
return ReverseBits32(i) * rcp(4294967296.0); // 2^-32
}
real2 Hammersley2dSeq(uint i, uint sequenceLength)
{
return real2(real(i) / real(sequenceLength), VanDerCorputBase2(i));
}
static const real2 k_Hammersley2dSeq16[] = {
real2(0.00000000, 0.00000000),
real2(0.06250000, 0.50000000),
real2(0.12500000, 0.25000000),
real2(0.18750000, 0.75000000),
real2(0.25000000, 0.12500000),
real2(0.31250000, 0.62500000),
real2(0.37500000, 0.37500000),
real2(0.43750000, 0.87500000),
real2(0.50000000, 0.06250000),
real2(0.56250000, 0.56250000),
real2(0.62500000, 0.31250000),
real2(0.68750000, 0.81250000),
real2(0.75000000, 0.18750000),
real2(0.81250000, 0.68750000),
real2(0.87500000, 0.43750000),
real2(0.93750000, 0.93750000)
};
static const real2 k_Hammersley2dSeq32[] = {
real2(0.00000000, 0.00000000),
real2(0.03125000, 0.50000000),
real2(0.06250000, 0.25000000),
real2(0.09375000, 0.75000000),
real2(0.12500000, 0.12500000),
real2(0.15625000, 0.62500000),
real2(0.18750000, 0.37500000),
real2(0.21875000, 0.87500000),
real2(0.25000000, 0.06250000),
real2(0.28125000, 0.56250000),
real2(0.31250000, 0.31250000),
real2(0.34375000, 0.81250000),
real2(0.37500000, 0.18750000),
real2(0.40625000, 0.68750000),
real2(0.43750000, 0.43750000),
real2(0.46875000, 0.93750000),
real2(0.50000000, 0.03125000),
real2(0.53125000, 0.53125000),
real2(0.56250000, 0.28125000),
real2(0.59375000, 0.78125000),
real2(0.62500000, 0.15625000),
real2(0.65625000, 0.65625000),
real2(0.68750000, 0.40625000),
real2(0.71875000, 0.90625000),
real2(0.75000000, 0.09375000),
real2(0.78125000, 0.59375000),
real2(0.81250000, 0.34375000),
real2(0.84375000, 0.84375000),
real2(0.87500000, 0.21875000),
real2(0.90625000, 0.71875000),
real2(0.93750000, 0.46875000),
real2(0.96875000, 0.96875000)
};
static const real2 k_Hammersley2dSeq64[] = {
real2(0.00000000, 0.00000000),
real2(0.01562500, 0.50000000),
real2(0.03125000, 0.25000000),
real2(0.04687500, 0.75000000),
real2(0.06250000, 0.12500000),
real2(0.07812500, 0.62500000),
real2(0.09375000, 0.37500000),
real2(0.10937500, 0.87500000),
real2(0.12500000, 0.06250000),
real2(0.14062500, 0.56250000),
real2(0.15625000, 0.31250000),
real2(0.17187500, 0.81250000),
real2(0.18750000, 0.18750000),
real2(0.20312500, 0.68750000),
real2(0.21875000, 0.43750000),
real2(0.23437500, 0.93750000),
real2(0.25000000, 0.03125000),
real2(0.26562500, 0.53125000),
real2(0.28125000, 0.28125000),
real2(0.29687500, 0.78125000),
real2(0.31250000, 0.15625000),
real2(0.32812500, 0.65625000),
real2(0.34375000, 0.40625000),
real2(0.35937500, 0.90625000),
real2(0.37500000, 0.09375000),
real2(0.39062500, 0.59375000),
real2(0.40625000, 0.34375000),
real2(0.42187500, 0.84375000),
real2(0.43750000, 0.21875000),
real2(0.45312500, 0.71875000),
real2(0.46875000, 0.46875000),
real2(0.48437500, 0.96875000),
real2(0.50000000, 0.01562500),
real2(0.51562500, 0.51562500),
real2(0.53125000, 0.26562500),
real2(0.54687500, 0.76562500),
real2(0.56250000, 0.14062500),
real2(0.57812500, 0.64062500),
real2(0.59375000, 0.39062500),
real2(0.60937500, 0.89062500),
real2(0.62500000, 0.07812500),
real2(0.64062500, 0.57812500),
real2(0.65625000, 0.32812500),
real2(0.67187500, 0.82812500),
real2(0.68750000, 0.20312500),
real2(0.70312500, 0.70312500),
real2(0.71875000, 0.45312500),
real2(0.73437500, 0.95312500),
real2(0.75000000, 0.04687500),
real2(0.76562500, 0.54687500),
real2(0.78125000, 0.29687500),
real2(0.79687500, 0.79687500),
real2(0.81250000, 0.17187500),
real2(0.82812500, 0.67187500),
real2(0.84375000, 0.42187500),
real2(0.85937500, 0.92187500),
real2(0.87500000, 0.10937500),
real2(0.89062500, 0.60937500),
real2(0.90625000, 0.35937500),
real2(0.92187500, 0.85937500),
real2(0.93750000, 0.23437500),
real2(0.95312500, 0.73437500),
real2(0.96875000, 0.48437500),
real2(0.98437500, 0.98437500)
};
static const real2 k_Hammersley2dSeq256[] = {
real2(0.00000000, 0.00000000),
real2(0.00390625, 0.50000000),
real2(0.00781250, 0.25000000),
real2(0.01171875, 0.75000000),
real2(0.01562500, 0.12500000),
real2(0.01953125, 0.62500000),
real2(0.02343750, 0.37500000),
real2(0.02734375, 0.87500000),
real2(0.03125000, 0.06250000),
real2(0.03515625, 0.56250000),
real2(0.03906250, 0.31250000),
real2(0.04296875, 0.81250000),
real2(0.04687500, 0.18750000),
real2(0.05078125, 0.68750000),
real2(0.05468750, 0.43750000),
real2(0.05859375, 0.93750000),
real2(0.06250000, 0.03125000),
real2(0.06640625, 0.53125000),
real2(0.07031250, 0.28125000),
real2(0.07421875, 0.78125000),
real2(0.07812500, 0.15625000),
real2(0.08203125, 0.65625000),
real2(0.08593750, 0.40625000),
real2(0.08984375, 0.90625000),
real2(0.09375000, 0.09375000),
real2(0.09765625, 0.59375000),
real2(0.10156250, 0.34375000),
real2(0.10546875, 0.84375000),
real2(0.10937500, 0.21875000),
real2(0.11328125, 0.71875000),
real2(0.11718750, 0.46875000),
real2(0.12109375, 0.96875000),
real2(0.12500000, 0.01562500),
real2(0.12890625, 0.51562500),
real2(0.13281250, 0.26562500),
real2(0.13671875, 0.76562500),
real2(0.14062500, 0.14062500),
real2(0.14453125, 0.64062500),
real2(0.14843750, 0.39062500),
real2(0.15234375, 0.89062500),
real2(0.15625000, 0.07812500),
real2(0.16015625, 0.57812500),
real2(0.16406250, 0.32812500),
real2(0.16796875, 0.82812500),
real2(0.17187500, 0.20312500),
real2(0.17578125, 0.70312500),
real2(0.17968750, 0.45312500),
real2(0.18359375, 0.95312500),
real2(0.18750000, 0.04687500),
real2(0.19140625, 0.54687500),
real2(0.19531250, 0.29687500),
real2(0.19921875, 0.79687500),
real2(0.20312500, 0.17187500),
real2(0.20703125, 0.67187500),
real2(0.21093750, 0.42187500),
real2(0.21484375, 0.92187500),
real2(0.21875000, 0.10937500),
real2(0.22265625, 0.60937500),
real2(0.22656250, 0.35937500),
real2(0.23046875, 0.85937500),
real2(0.23437500, 0.23437500),
real2(0.23828125, 0.73437500),
real2(0.24218750, 0.48437500),
real2(0.24609375, 0.98437500),
real2(0.25000000, 0.00781250),
real2(0.25390625, 0.50781250),
real2(0.25781250, 0.25781250),
real2(0.26171875, 0.75781250),
real2(0.26562500, 0.13281250),
real2(0.26953125, 0.63281250),
real2(0.27343750, 0.38281250),
real2(0.27734375, 0.88281250),
real2(0.28125000, 0.07031250),
real2(0.28515625, 0.57031250),
real2(0.28906250, 0.32031250),
real2(0.29296875, 0.82031250),
real2(0.29687500, 0.19531250),
real2(0.30078125, 0.69531250),
real2(0.30468750, 0.44531250),
real2(0.30859375, 0.94531250),
real2(0.31250000, 0.03906250),
real2(0.31640625, 0.53906250),
real2(0.32031250, 0.28906250),
real2(0.32421875, 0.78906250),
real2(0.32812500, 0.16406250),
real2(0.33203125, 0.66406250),
real2(0.33593750, 0.41406250),
real2(0.33984375, 0.91406250),
real2(0.34375000, 0.10156250),
real2(0.34765625, 0.60156250),
real2(0.35156250, 0.35156250),
real2(0.35546875, 0.85156250),
real2(0.35937500, 0.22656250),
real2(0.36328125, 0.72656250),
real2(0.36718750, 0.47656250),
real2(0.37109375, 0.97656250),
real2(0.37500000, 0.02343750),
real2(0.37890625, 0.52343750),
real2(0.38281250, 0.27343750),
real2(0.38671875, 0.77343750),
real2(0.39062500, 0.14843750),
real2(0.39453125, 0.64843750),
real2(0.39843750, 0.39843750),
real2(0.40234375, 0.89843750),
real2(0.40625000, 0.08593750),
real2(0.41015625, 0.58593750),
real2(0.41406250, 0.33593750),
real2(0.41796875, 0.83593750),
real2(0.42187500, 0.21093750),
real2(0.42578125, 0.71093750),
real2(0.42968750, 0.46093750),
real2(0.43359375, 0.96093750),
real2(0.43750000, 0.05468750),
real2(0.44140625, 0.55468750),
real2(0.44531250, 0.30468750),
real2(0.44921875, 0.80468750),
real2(0.45312500, 0.17968750),
real2(0.45703125, 0.67968750),
real2(0.46093750, 0.42968750),
real2(0.46484375, 0.92968750),
real2(0.46875000, 0.11718750),
real2(0.47265625, 0.61718750),
real2(0.47656250, 0.36718750),
real2(0.48046875, 0.86718750),
real2(0.48437500, 0.24218750),
real2(0.48828125, 0.74218750),
real2(0.49218750, 0.49218750),
real2(0.49609375, 0.99218750),
real2(0.50000000, 0.00390625),
real2(0.50390625, 0.50390625),
real2(0.50781250, 0.25390625),
real2(0.51171875, 0.75390625),
real2(0.51562500, 0.12890625),
real2(0.51953125, 0.62890625),
real2(0.52343750, 0.37890625),
real2(0.52734375, 0.87890625),
real2(0.53125000, 0.06640625),
real2(0.53515625, 0.56640625),
real2(0.53906250, 0.31640625),
real2(0.54296875, 0.81640625),
real2(0.54687500, 0.19140625),
real2(0.55078125, 0.69140625),
real2(0.55468750, 0.44140625),
real2(0.55859375, 0.94140625),
real2(0.56250000, 0.03515625),
real2(0.56640625, 0.53515625),
real2(0.57031250, 0.28515625),
real2(0.57421875, 0.78515625),
real2(0.57812500, 0.16015625),
real2(0.58203125, 0.66015625),
real2(0.58593750, 0.41015625),
real2(0.58984375, 0.91015625),
real2(0.59375000, 0.09765625),
real2(0.59765625, 0.59765625),
real2(0.60156250, 0.34765625),
real2(0.60546875, 0.84765625),
real2(0.60937500, 0.22265625),
real2(0.61328125, 0.72265625),
real2(0.61718750, 0.47265625),
real2(0.62109375, 0.97265625),
real2(0.62500000, 0.01953125),
real2(0.62890625, 0.51953125),
real2(0.63281250, 0.26953125),
real2(0.63671875, 0.76953125),
real2(0.64062500, 0.14453125),
real2(0.64453125, 0.64453125),
real2(0.64843750, 0.39453125),
real2(0.65234375, 0.89453125),
real2(0.65625000, 0.08203125),
real2(0.66015625, 0.58203125),
real2(0.66406250, 0.33203125),
real2(0.66796875, 0.83203125),
real2(0.67187500, 0.20703125),
real2(0.67578125, 0.70703125),
real2(0.67968750, 0.45703125),
real2(0.68359375, 0.95703125),
real2(0.68750000, 0.05078125),
real2(0.69140625, 0.55078125),
real2(0.69531250, 0.30078125),
real2(0.69921875, 0.80078125),
real2(0.70312500, 0.17578125),
real2(0.70703125, 0.67578125),
real2(0.71093750, 0.42578125),
real2(0.71484375, 0.92578125),
real2(0.71875000, 0.11328125),
real2(0.72265625, 0.61328125),
real2(0.72656250, 0.36328125),
real2(0.73046875, 0.86328125),
real2(0.73437500, 0.23828125),
real2(0.73828125, 0.73828125),
real2(0.74218750, 0.48828125),
real2(0.74609375, 0.98828125),
real2(0.75000000, 0.01171875),
real2(0.75390625, 0.51171875),
real2(0.75781250, 0.26171875),
real2(0.76171875, 0.76171875),
real2(0.76562500, 0.13671875),
real2(0.76953125, 0.63671875),
real2(0.77343750, 0.38671875),
real2(0.77734375, 0.88671875),
real2(0.78125000, 0.07421875),
real2(0.78515625, 0.57421875),
real2(0.78906250, 0.32421875),
real2(0.79296875, 0.82421875),
real2(0.79687500, 0.19921875),
real2(0.80078125, 0.69921875),
real2(0.80468750, 0.44921875),
real2(0.80859375, 0.94921875),
real2(0.81250000, 0.04296875),
real2(0.81640625, 0.54296875),
real2(0.82031250, 0.29296875),
real2(0.82421875, 0.79296875),
real2(0.82812500, 0.16796875),
real2(0.83203125, 0.66796875),
real2(0.83593750, 0.41796875),
real2(0.83984375, 0.91796875),
real2(0.84375000, 0.10546875),
real2(0.84765625, 0.60546875),
real2(0.85156250, 0.35546875),
real2(0.85546875, 0.85546875),
real2(0.85937500, 0.23046875),
real2(0.86328125, 0.73046875),
real2(0.86718750, 0.48046875),
real2(0.87109375, 0.98046875),
real2(0.87500000, 0.02734375),
real2(0.87890625, 0.52734375),
real2(0.88281250, 0.27734375),
real2(0.88671875, 0.77734375),
real2(0.89062500, 0.15234375),
real2(0.89453125, 0.65234375),
real2(0.89843750, 0.40234375),
real2(0.90234375, 0.90234375),
real2(0.90625000, 0.08984375),
real2(0.91015625, 0.58984375),
real2(0.91406250, 0.33984375),
real2(0.91796875, 0.83984375),
real2(0.92187500, 0.21484375),
real2(0.92578125, 0.71484375),
real2(0.92968750, 0.46484375),
real2(0.93359375, 0.96484375),
real2(0.93750000, 0.05859375),
real2(0.94140625, 0.55859375),
real2(0.94531250, 0.30859375),
real2(0.94921875, 0.80859375),
real2(0.95312500, 0.18359375),
real2(0.95703125, 0.68359375),
real2(0.96093750, 0.43359375),
real2(0.96484375, 0.93359375),
real2(0.96875000, 0.12109375),
real2(0.97265625, 0.62109375),
real2(0.97656250, 0.37109375),
real2(0.98046875, 0.87109375),
real2(0.98437500, 0.24609375),
real2(0.98828125, 0.74609375),
real2(0.99218750, 0.49609375),
real2(0.99609375, 0.99609375)
};
// Loads elements from one of the precomputed tables for sample counts of 16, 32, 64, 256.
// Computes sample positions at runtime otherwise.
real2 Hammersley2d(uint i, uint sampleCount)
{
switch (sampleCount)
{
case 16: return k_Hammersley2dSeq16[i];
case 32: return k_Hammersley2dSeq32[i];
case 64: return k_Hammersley2dSeq64[i];
case 256: return k_Hammersley2dSeq256[i];
default: return Hammersley2dSeq(i, sampleCount);
}
}
#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3
#pragma warning (enable : 3205) // conversion of larger type to smaller
#endif
#endif // UNITY_HAMMERSLEY_INCLUDED

View File

@@ -0,0 +1,75 @@
// This structure abstract uv mapping inside one struct.
// It represent a mapping of any uv (with its associated tangent space for derivative if SurfaceGradient mode) - UVSet0 to 4, planar, triplanar
#ifndef __SAMPLEUVMAPPING_HLSL__
#define __SAMPLEUVMAPPING_HLSL__
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/NormalSurfaceGradient.hlsl"
#define UV_MAPPING_UVSET 0
#define UV_MAPPING_PLANAR 1
#define UV_MAPPING_TRIPLANAR 2
struct UVMapping
{
int mappingType;
float2 uv; // Current uv or planar uv
// Triplanar specific
float2 uvZY;
float2 uvXZ;
float2 uvXY;
float3 normalWS; // vertex normal
float3 triplanarWeights;
#ifdef SURFACE_GRADIENT
// tangent basis to use when mappingType is UV_MAPPING_UVSET
// these are vertex level in world space
float3 tangentWS;
float3 bitangentWS;
// TODO: store also object normal map for object triplanar
#endif
};
// Multiple includes of the file to handle all variations of textures sampling for regular, lod and bias
// Regular sampling functions
#define ADD_FUNC_SUFFIX(Name) Name
#define SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping, unused) SAMPLE_TEXTURE2D(textureName, samplerName, uvMapping)
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Sampling/SampleUVMappingInternal.hlsl"
#undef ADD_FUNC_SUFFIX
#undef SAMPLE_TEXTURE_FUNC
// Lod sampling functions
#define ADD_FUNC_SUFFIX(Name) MERGE_NAME(Name, Lod)
#define SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping, lod) SAMPLE_TEXTURE2D_LOD(textureName, samplerName, uvMapping, lod)
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Sampling/SampleUVMappingInternal.hlsl"
#undef ADD_FUNC_SUFFIX
#undef SAMPLE_TEXTURE_FUNC
// Bias sampling functions
#define ADD_FUNC_SUFFIX(Name) MERGE_NAME(Name, Bias)
#define SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping, bias) SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, uvMapping, bias)
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Sampling/SampleUVMappingInternal.hlsl"
#undef ADD_FUNC_SUFFIX
#undef SAMPLE_TEXTURE_FUNC
// Macro to improve readibility of surface data
#define SAMPLE_UVMAPPING_TEXTURE2D(textureName, samplerName, uvMapping) SampleUVMapping(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, 0.0) // Last 0.0 is unused
#define SAMPLE_UVMAPPING_TEXTURE2D_LOD(textureName, samplerName, uvMapping, lod) SampleUVMappingLod(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, lod)
#define SAMPLE_UVMAPPING_TEXTURE2D_BIAS(textureName, samplerName, uvMapping, bias) SampleUVMappingBias(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, bias)
#define SAMPLE_UVMAPPING_NORMALMAP(textureName, samplerName, uvMapping, scale) SampleUVMappingNormal(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, scale, 0.0)
#define SAMPLE_UVMAPPING_NORMALMAP_LOD(textureName, samplerName, uvMapping, scale, lod) SampleUVMappingNormalLod(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, scale, lod)
#define SAMPLE_UVMAPPING_NORMALMAP_BIAS(textureName, samplerName, uvMapping, scale, bias) SampleUVMappingNormalBias(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, scale, bias)
#define SAMPLE_UVMAPPING_NORMALMAP_AG(textureName, samplerName, uvMapping, scale) SampleUVMappingNormalAG(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, scale, 0.0)
#define SAMPLE_UVMAPPING_NORMALMAP_AG_LOD(textureName, samplerName, uvMapping, scale, lod) SampleUVMappingNormalAGLod(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, scale, lod)
#define SAMPLE_UVMAPPING_NORMALMAP_AG_BIAS(textureName, samplerName, uvMapping, scale, bias) SampleUVMappingNormalAGBias(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, scale, bias)
#define SAMPLE_UVMAPPING_NORMALMAP_RGB(textureName, samplerName, uvMapping, scale) SampleUVMappingNormalRGB(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, scale, 0.0)
#define SAMPLE_UVMAPPING_NORMALMAP_RGB_LOD(textureName, samplerName, uvMapping, scale, lod) SampleUVMappingNormalRGBLod(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, scale, lod)
#define SAMPLE_UVMAPPING_NORMALMAP_RGB_BIAS(textureName, samplerName, uvMapping, scale, bias) SampleUVMappingNormalRGBBias(TEXTURE2D_ARGS(textureName, samplerName), uvMapping, scale, bias)
#endif //__SAMPLEUVMAPPING_HLSL__

View File

@@ -0,0 +1,64 @@
// These functions are use to hide the handling of triplanar mapping
// Normal need a specific treatment as they use special encoding for both base and detail map
// Also we use multiple inclusion to handle the various variation for lod and bias
// param can be unused, lod or bias
real4 ADD_FUNC_SUFFIX(SampleUVMapping)(TEXTURE2D_PARAM(textureName, samplerName), UVMapping uvMapping, real param)
{
if (uvMapping.mappingType == UV_MAPPING_TRIPLANAR)
{
real3 triplanarWeights = uvMapping.triplanarWeights;
real4 val = real4(0.0, 0.0, 0.0, 0.0);
if (triplanarWeights.x > 0.0)
val += triplanarWeights.x * SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvZY, param);
if (triplanarWeights.y > 0.0)
val += triplanarWeights.y * SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXZ, param);
if (triplanarWeights.z > 0.0)
val += triplanarWeights.z * SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXY, param);
return val;
}
else // UV_MAPPING_UVSET / UV_MAPPING_PLANAR
{
return SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uv, param);
}
}
// Nested multiple includes of the file to handle all variations of normal map (AG, RG or RGB)
// This version is use for the base normal map (BC5 or DXT5nm)
#define ADD_NORMAL_FUNC_SUFFIX(Name) Name
#if defined(UNITY_ASTC_NORMALMAP_ENCODING)
#define UNPACK_NORMAL_FUNC UnpackNormalAG
#define UNPACK_DERIVATIVE_FUNC UnpackDerivativeNormalAG
#elif defined(UNITY_NO_DXT5nm)
#define UNPACK_NORMAL_FUNC UnpackNormalRGB
#define UNPACK_DERIVATIVE_FUNC UnpackDerivativeNormalRGB
#else
#define UNPACK_NORMAL_FUNC UnpackNormalmapRGorAG
#define UNPACK_DERIVATIVE_FUNC UnpackDerivativeNormalRGorAG
#endif
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Sampling/SampleUVMappingNormalInternal.hlsl"
#undef ADD_NORMAL_FUNC_SUFFIX
#undef UNPACK_NORMAL_FUNC
#undef UNPACK_DERIVATIVE_FUNC
// This version is for normalmap with AG encoding only. Use with details map encoded with others properties (like smoothness).
#define ADD_NORMAL_FUNC_SUFFIX(Name) MERGE_NAME(Name, AG)
#define UNPACK_NORMAL_FUNC UnpackNormalAG
#define UNPACK_DERIVATIVE_FUNC UnpackDerivativeNormalAG
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Sampling/SampleUVMappingNormalInternal.hlsl"
#undef ADD_NORMAL_FUNC_SUFFIX
#undef UNPACK_NORMAL_FUNC
#undef UNPACK_DERIVATIVE_FUNC
// This version is for normalmap with RGB encoding only, i.e uncompress or BC7.
#define ADD_NORMAL_FUNC_SUFFIX(Name) MERGE_NAME(Name, RGB)
#define UNPACK_NORMAL_FUNC UnpackNormalRGB
#define UNPACK_DERIVATIVE_FUNC UnpackDerivativeNormalRGB
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Sampling/SampleUVMappingNormalInternal.hlsl"
#undef ADD_NORMAL_FUNC_SUFFIX
#undef UNPACK_NORMAL_FUNC
#undef UNPACK_DERIVATIVE_FUNC

View File

@@ -0,0 +1,57 @@
real3 ADD_FUNC_SUFFIX(ADD_NORMAL_FUNC_SUFFIX(SampleUVMappingNormal))(TEXTURE2D_PARAM(textureName, samplerName), UVMapping uvMapping, real scale, real param)
{
if (uvMapping.mappingType == UV_MAPPING_TRIPLANAR)
{
real3 triplanarWeights = uvMapping.triplanarWeights;
#ifdef SURFACE_GRADIENT
// Height map gradient. Basically, it encodes height map slopes along S and T axes.
real2 derivXplane;
real2 derivYPlane;
real2 derivZPlane;
derivXplane = derivYPlane = derivZPlane = real2(0.0, 0.0);
if (triplanarWeights.x > 0.0)
derivXplane = triplanarWeights.x * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvZY, param), scale);
if (triplanarWeights.y > 0.0)
derivYPlane = triplanarWeights.y * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXZ, param), scale);
if (triplanarWeights.z > 0.0)
derivZPlane = triplanarWeights.z * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXY, param), scale);
// Assume derivXplane, derivYPlane and derivZPlane sampled using (z,y), (z,x) and (x,y) respectively.
// TODO: Check with morten convention! Do it follow ours ?
real3 volumeGrad = real3(derivZPlane.x + derivYPlane.x, derivZPlane.y + derivXplane.y, derivXplane.x + derivYPlane.y);
return SurfaceGradientFromVolumeGradient(uvMapping.normalWS, volumeGrad);
#else
real3 val = real3(0.0, 0.0, 0.0);
if (triplanarWeights.x > 0.0)
val += triplanarWeights.x * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvZY, param), scale);
if (triplanarWeights.y > 0.0)
val += triplanarWeights.y * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXZ, param), scale);
if (triplanarWeights.z > 0.0)
val += triplanarWeights.z * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXY, param), scale);
return normalize(val);
#endif
}
#ifdef SURFACE_GRADIENT
else if (uvMapping.mappingType == UV_MAPPING_PLANAR)
{
// Note: Planar is on uv coordinate (and not uvXZ)
real2 derivYPlane = UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uv, param), scale);
// See comment above
real3 volumeGrad = real3(derivYPlane.x, 0.0, derivYPlane.y);
return SurfaceGradientFromVolumeGradient(uvMapping.normalWS, volumeGrad);
}
#endif
else
{
#ifdef SURFACE_GRADIENT
real2 deriv = UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uv, param), scale);
return SurfaceGradientFromTBN(deriv, uvMapping.tangentWS, uvMapping.bitangentWS);
#else
return UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uv, param), scale);
#endif
}
}

View File

@@ -0,0 +1,316 @@
#ifndef UNITY_SAMPLING_INCLUDED
#define UNITY_SAMPLING_INCLUDED
#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3
#pragma warning (disable : 3205) // conversion of larger type to smaller
#endif
//-----------------------------------------------------------------------------
// Sample generator
//-----------------------------------------------------------------------------
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Sampling/Fibonacci.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Sampling/Hammersley.hlsl"
//-----------------------------------------------------------------------------
// Coordinate system conversion
//-----------------------------------------------------------------------------
// Transforms the unit vector from the spherical to the Cartesian (right-handed, Z up) coordinate.
real3 SphericalToCartesian(real cosPhi, real sinPhi, real cosTheta)
{
real sinTheta = SinFromCos(cosTheta);
return real3(real2(cosPhi, sinPhi) * sinTheta, cosTheta);
}
real3 SphericalToCartesian(real phi, real cosTheta)
{
real sinPhi, cosPhi;
sincos(phi, sinPhi, cosPhi);
return SphericalToCartesian(cosPhi, sinPhi, cosTheta);
}
// Converts Cartesian coordinates given in the right-handed coordinate system
// with Z pointing upwards (OpenGL style) to the coordinates in the left-handed
// coordinate system with Y pointing up and Z facing forward (DirectX style).
real3 TransformGLtoDX(real3 v)
{
return v.xzy;
}
// Performs conversion from equiareal map coordinates to Cartesian (DirectX cubemap) ones.
real3 ConvertEquiarealToCubemap(real u, real v)
{
real phi = TWO_PI - TWO_PI * u;
real cosTheta = 1.0 - 2.0 * v;
return TransformGLtoDX(SphericalToCartesian(phi, cosTheta));
}
// Convert a texel position into normalized position [-1..1]x[-1..1]
real2 CubemapTexelToNVC(uint2 unPositionTXS, uint cubemapSize)
{
return 2.0 * real2(unPositionTXS) / real(max(cubemapSize - 1, 1)) - 1.0;
}
// Map cubemap face to world vector basis
static const real3 CUBEMAP_FACE_BASIS_MAPPING[6][3] =
{
//XPOS face
{
real3(0.0, 0.0, -1.0),
real3(0.0, -1.0, 0.0),
real3(1.0, 0.0, 0.0)
},
//XNEG face
{
real3(0.0, 0.0, 1.0),
real3(0.0, -1.0, 0.0),
real3(-1.0, 0.0, 0.0)
},
//YPOS face
{
real3(1.0, 0.0, 0.0),
real3(0.0, 0.0, 1.0),
real3(0.0, 1.0, 0.0)
},
//YNEG face
{
real3(1.0, 0.0, 0.0),
real3(0.0, 0.0, -1.0),
real3(0.0, -1.0, 0.0)
},
//ZPOS face
{
real3(1.0, 0.0, 0.0),
real3(0.0, -1.0, 0.0),
real3(0.0, 0.0, 1.0)
},
//ZNEG face
{
real3(-1.0, 0.0, 0.0),
real3(0.0, -1.0, 0.0),
real3(0.0, 0.0, -1.0)
}
};
// Convert a normalized cubemap face position into a direction
real3 CubemapTexelToDirection(real2 positionNVC, uint faceId)
{
real3 dir = CUBEMAP_FACE_BASIS_MAPPING[faceId][0] * positionNVC.x
+ CUBEMAP_FACE_BASIS_MAPPING[faceId][1] * positionNVC.y
+ CUBEMAP_FACE_BASIS_MAPPING[faceId][2];
return normalize(dir);
}
//-----------------------------------------------------------------------------
// Sampling function
// Reference : http://www.cs.virginia.edu/~jdl/bib/globillum/mis/shirley96.pdf + PBRT
//-----------------------------------------------------------------------------
// Performs uniform sampling of the unit disk.
// Ref: PBRT v3, p. 777.
real2 SampleDiskUniform(real u1, real u2)
{
real r = sqrt(u1);
real phi = TWO_PI * u2;
real sinPhi, cosPhi;
sincos(phi, sinPhi, cosPhi);
return r * real2(cosPhi, sinPhi);
}
// Performs cubic sampling of the unit disk.
real2 SampleDiskCubic(real u1, real u2)
{
real r = u1;
real phi = TWO_PI * u2;
real sinPhi, cosPhi;
sincos(phi, sinPhi, cosPhi);
return r * real2(cosPhi, sinPhi);
}
real3 SampleConeUniform(real u1, real u2, real cos_theta)
{
float r0 = cos_theta + u1 * (1.0f - cos_theta);
float r = sqrt(max(0.0, 1.0 - r0 * r0));
float phi = TWO_PI * u2;
return float3(r * cos(phi), r * sin(phi), r0);
}
real3 SampleSphereUniform(real u1, real u2)
{
real phi = TWO_PI * u2;
real cosTheta = 1.0 - 2.0 * u1;
return SphericalToCartesian(phi, cosTheta);
}
// Performs cosine-weighted sampling of the hemisphere.
// Ref: PBRT v3, p. 780.
real3 SampleHemisphereCosine(real u1, real u2)
{
real3 localL;
// Since we don't really care about the area distortion,
// we substitute uniform disk sampling for the concentric one.
localL.xy = SampleDiskUniform(u1, u2);
// Project the point from the disk onto the hemisphere.
localL.z = sqrt(1.0 - u1);
return localL;
}
// Cosine-weighted sampling without the tangent frame.
// Ref: http://www.amietia.com/lambertnotangent.html
real3 SampleHemisphereCosine(real u1, real u2, real3 normal)
{
// This function needs to used safenormalize because there is a probability
// that the generated direction is the exact opposite of the normal and that would lead
// to a nan vector otheriwse.
real3 pointOnSphere = SampleSphereUniform(u1, u2);
return SafeNormalize(normal + pointOnSphere);
}
real3 SampleHemisphereUniform(real u1, real u2)
{
real phi = TWO_PI * u2;
real cosTheta = 1.0 - u1;
return SphericalToCartesian(phi, cosTheta);
}
void SampleSphere(real2 u,
real4x4 localToWorld,
real radius,
out real lightPdf,
out real3 P,
out real3 Ns)
{
real u1 = u.x;
real u2 = u.y;
Ns = SampleSphereUniform(u1, u2);
// Transform from unit sphere to world space
P = radius * Ns + localToWorld[3].xyz;
// pdf is inverse of area
lightPdf = 1.0 / (FOUR_PI * radius * radius);
}
void SampleHemisphere(real2 u,
real4x4 localToWorld,
real radius,
out real lightPdf,
out real3 P,
out real3 Ns)
{
real u1 = u.x;
real u2 = u.y;
// Random point at hemisphere surface
Ns = -SampleHemisphereUniform(u1, u2); // We want the y down hemisphere
P = radius * Ns;
// Transform to world space
P = mul(real4(P, 1.0), localToWorld).xyz;
Ns = mul(Ns, (real3x3)(localToWorld));
// pdf is inverse of area
lightPdf = 1.0 / (TWO_PI * radius * radius);
}
// Note: The cylinder has no end caps (i.e. no disk on the side)
void SampleCylinder(real2 u,
real4x4 localToWorld,
real radius,
real width,
out real lightPdf,
out real3 P,
out real3 Ns)
{
real u1 = u.x;
real u2 = u.y;
// Random point at cylinder surface
real t = (u1 - 0.5) * width;
real theta = 2.0 * PI * u2;
real cosTheta = cos(theta);
real sinTheta = sin(theta);
// Cylinder are align on the right axis
P = real3(t, radius * cosTheta, radius * sinTheta);
Ns = normalize(real3(0.0, cosTheta, sinTheta));
// Transform to world space
P = mul(real4(P, 1.0), localToWorld).xyz;
Ns = mul(Ns, (real3x3)(localToWorld));
// pdf is inverse of area
lightPdf = 1.0 / (TWO_PI * radius * width);
}
void SampleRectangle(real2 u,
real4x4 localToWorld,
real width,
real height,
out real lightPdf,
out real3 P,
out real3 Ns)
{
// Random point at rectangle surface
P = real3((u.x - 0.5) * width, (u.y - 0.5) * height, 0);
Ns = real3(0, 0, -1); // Light down (-Z)
// Transform to world space
P = mul(real4(P, 1.0), localToWorld).xyz;
Ns = mul(Ns, (real3x3)(localToWorld));
// pdf is inverse of area
lightPdf = 1.0 / (width * height);
}
void SampleDisk(real2 u,
real4x4 localToWorld,
real radius,
out real lightPdf,
out real3 P,
out real3 Ns)
{
// Random point at disk surface
P = real3(radius * SampleDiskUniform(u.x, u.y), 0);
Ns = real3(0.0, 0.0, -1.0); // Light down (-Z)
// Transform to world space
P = mul(real4(P, 1.0), localToWorld).xyz;
Ns = mul(Ns, (real3x3)(localToWorld));
// pdf is inverse of area
lightPdf = 1.0 / (PI * radius * radius);
}
// Solid angle cone sampling.
// Takes the cosine of the aperture as an input.
void SampleCone(real2 u, real cosHalfAngle,
out real3 dir, out real rcpPdf)
{
real cosTheta = lerp(1, cosHalfAngle, u.x);
real phi = TWO_PI * u.y;
dir = SphericalToCartesian(phi, cosTheta);
rcpPdf = TWO_PI * (1 - cosHalfAngle);
}
#if SHADER_API_MOBILE || SHADER_API_GLES || SHADER_API_GLES3
#pragma warning (enable : 3205) // conversion of larger type to smaller
#endif
#endif // UNITY_SAMPLING_INCLUDED