public static float3 MinSpace(float3 a, float b) { a.x = DeviceFunction.Min(a.x, b); a.y = DeviceFunction.Min(a.y, b); a.z = DeviceFunction.Min(a.z, b); return(a); }
public static float NewSoftShadow(float3 p, float3 d, float shadowStrength, int iterations, float side, float3 seed, float3 shift, float minDist, float maxDist, float minAngle) { float darkness = 1; float prevDist = float.MaxValue; float dist = minDist / 100; float angle = 1; float totalDist = minDist; float oldNewIntDist = 0; float legLength = 0; while (totalDist < maxDist) { dist = ScaledDE(A(p, M(d, totalDist)), iterations, side, seed, shift); oldNewIntDist = dist * dist / (2 * prevDist); legLength = DeviceFunction.Sqrt(dist * dist - oldNewIntDist * oldNewIntDist); angle = shadowStrength * legLength / DeviceFunction.Max(0, totalDist - oldNewIntDist); darkness = DeviceFunction.Min(darkness, angle); prevDist = dist; totalDist += dist; if (dist < 0) { return(0); } if (darkness < minAngle) { return(0); } } return(darkness); }
public static float OldSoftShadow(float3 p, float3 d, float shadowStrength, int iterations, float side, float3 seed, float3 shift, float minDist, float maxDist, float minAngle) { float k = 1; float dist = minDist; float angle = 1; float totalDist = minDist / 100; float3 marchedPoint = p; while (totalDist < maxDist) { dist = ScaledDE(marchedPoint, iterations, side, seed, shift); if (dist == 0) { dist = minDist; } marchedPoint = A(marchedPoint, M(d, dist)); totalDist += dist; angle = shadowStrength * dist / totalDist; k = DeviceFunction.Min(k, angle); if (dist < 0) { return(0); } if (k < minAngle) { return(0); } } return(k); }
public static void MarchRay(deviceptr <float3> directions, deviceptr <byte> pixelValues, float3 camera, float3 light, float2 cols, float minDist, float maxDist, int maxstep, int bytes, int width, int iterations, float side, float3 seed, float3 shift, float shadowStrength, float ambientOccStrength) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; int h = Index(i, j, width); float3 p = camera; int stepnum = 0; float dist = minDist + 1; float totalDist = 0; while (totalDist < maxDist && dist > minDist) { dist = ScaledDE(p, iterations, side, seed, shift); p = A(p, M(directions[h], dist)); totalDist += dist; stepnum++; if (stepnum == maxstep) { break; } } float brightness = 0; if (DeviceFunction.Abs(dist) <= minDist) { float3 off = S(light, p); float lightVectorLength = L(off); off = D(off, lightVectorLength); float shadow = 1; float diffuseCalculated = 0; float normalAngle = O(off, Normal(p, iterations, side, seed, shift, minDist)); if (normalAngle > 0) { shadow = NewSoftShadow(p, off, shadowStrength, iterations, side, seed, shift, minDist, lightVectorLength, 0.01f); diffuseCalculated = DeviceFunction.Max(cols.y * shadow * normalAngle, 0); } brightness += diffuseCalculated + cols.x / (1 + stepnum * ambientOccStrength); brightness = DeviceFunction.Min(DeviceFunction.Max(brightness, 0), 1); float col = Orbit(p, iterations, side, seed, shift); pixelValues[h * 3] = (byte)(Blue(col) * brightness * byte.MaxValue); pixelValues[h * 3 + 1] = (byte)(Green(col) * brightness * byte.MaxValue); pixelValues[h * 3 + 2] = (byte)(Red(col) * brightness * byte.MaxValue); } else { pixelValues[h * 3] = 0; pixelValues[h * 3 + 1] = 0; pixelValues[h * 3 + 2] = 0; } }
public static float3 FoldMenger(float3 z) { float a = DeviceFunction.Min(z.x - z.y, 0); z.x -= a; z.y += a; a = DeviceFunction.Min(z.x - z.z, 0); z.x -= a; z.z += a; a = DeviceFunction.Min(z.y - z.z, 0); z.y -= a; z.z += a; return(z); }
public static float TrapezoidWave(float loc) { return(DeviceFunction.Min(DeviceFunction.Max(DeviceFunction.Abs(loc - 3), 0) - 1, 1)); }