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); }
// Helpers! private static void ComputeRippleAtOffset(int x, int y, int width, int height, Action <byte> action) { var fx = x - width * 0.5f; var fy = y - height * 0.5f; var d = DeviceFunction.Sqrt(fx * fx + fy * fy); var v = (byte)(128f + 127f * DeviceFunction.Cos(d / 10f) / (d / 10f + 1f)); action(v); }
public static Matrix Phansalkar(Matrix input, int radius) { var width = input.Width; var height = input.Height; var data = (float[])input; var result = (float[])new Matrix(width, height); var p = 2.5f; var q = 10; var k = 0.15; var r = 0.4; var dimension = GetKernelDimension(height); Gpu.Default.Launch(KernelBuilder(tid => { var index = tid * width; for (var x = 0; x < width; x++) { var sum = 0f; var count = 0; for (var ky = -radius; ky <= radius; ky++) { var py = tid + ky; var pindex = py * width; for (var kx = -radius; kx <= radius; kx++) { var px = x + kx; if (px >= 0 && px < width && py >= 0 && py < height) { sum += data[pindex + px] / byte.MaxValue; count++; } } } var mean = sum / count; var variance = DeviceFunction.Abs(sum / count - mean); var deviation = DeviceFunction.Sqrt(variance / count); var threshold = mean * (1f + p * DeviceFunction.Exp(-q * mean) + k * (deviation / r - 1)); result[index + x] = data[index + x] / byte.MaxValue > threshold ? byte.MaxValue : 0; } }, height), new LaunchParam(dimension, dimension)); return(new Matrix(width, height, result)); }
private static void ComputeRippleAtOffset(deviceptr <byte> result, int index, int width, int height) { var x = index % width; var y = index / width; var offset = ColorComponents * index; if (offset < 3 * width * height) { var fx = x - width * 0.5f; var fy = y - height * 0.5f; var d = DeviceFunction.Sqrt(fx * fx + fy * fy); var v = (byte)(128f + 127f * DeviceFunction.Cos(d / 10f) / (d / 10f + 1f)); result[offset + 0] = v; result[offset + 1] = v; result[offset + 2] = v; } }
public static float L(float3 p) { return(DeviceFunction.Sqrt(p.x * p.x + p.y * p.y + p.z * p.z)); }
public static double VectorLength(double x1, double y1, double x2, double y2) { return(DeviceFunction.Sqrt((DeviceFunction.Abs(x2 - x1) * DeviceFunction.Abs(x2 - x1)) + (DeviceFunction.Abs(y2 - y1) * DeviceFunction.Abs(y2 - y1)))); }