浏览代码

fibonacci + improving shader

Benjamin Guignabert 5 年之前
父节点
当前提交
0c6e8db13e
共有 2 个文件被更改,包括 312 次插入4 次删除
  1. 296 0
      src/Shaders/ShadersInclude/fibonacci.fx
  2. 16 4
      src/Shaders/subSurfaceScattering.fragment.fx

+ 296 - 0
src/Shaders/ShadersInclude/fibonacci.fx

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

+ 16 - 4
src/Shaders/subSurfaceScattering.fragment.fx

@@ -66,9 +66,21 @@ void main(void)
 
     if (!passedStencilTest) { return; }
 
-	int    profileIndex  = 0; //sssData.diffusionProfileIndex;
-	float  distScale     = 0.; //sssData.subsurfaceMask;
-	vec3 S             = vec3(1.); //_ShapeParamsAndMaxScatterDists[profileIndex].rgb; -> diffusion color
+
+    // SKIN DIFFUSION PROFILE
+    // profile:
+    //   name: Skin
+    //   scatteringDistance: {r: 0.7568628, g: 0.32156864, b: 0.20000002, a: 1}
+    //   transmissionTint: {r: 0.7568628, g: 0.32156864, b: 0.20000002, a: 1}
+    //   texturingMode: 0
+    //   transmissionMode: 0
+    //   thicknessRemap: {x: 0, y: 8.152544}
+    //   worldScale: 1
+    //   ior: 1.4
+    //   hash: 1075477546
+
+	float  distScale     = 1.; //sssData.subsurfaceMask;
+	vec3 S             = vec3(0.7568628, 0.32156864, 0.20000002); //_ShapeParamsAndMaxScatterDists[profileIndex].rgb; -> diffusion color
 	float  d             = 0.5; //_ShapeParamsAndMaxScatterDists[profileIndex].a; -> max scatter dist
 	float  metersPerUnit = 1.; //_WorldScalesAndFilterRadiiAndThicknessRemaps[profileIndex].x;
 	float  filterRadius  = 0.5; //_WorldScalesAndFilterRadiiAndThicknessRemaps[profileIndex].y; // In millimeters
@@ -145,7 +157,7 @@ void EvaluateSample(uint i, uint n, vec3 S, float d, vec3 centerPosVS, float mmP
     sinPhase = sin(phase);
     cosPhase = cos(phase);
 
-    vec2 bdp = SampleBurleyDiffusionProfile(i * scale + offset, d, );
+    vec2 bdp = SampleBurleyDiffusionProfile(i * scale + offset, d);
     float r = bdp.x;
     float rcpPdf = bdp.y;