public static bool TraceStep(ref Ray3f ray, Scene scene, ref Random rng, NativeArray <float3> fibs, ref float3 color) { HitRecord hit; const float tMin = 0f; const float tMax = 1000f; bool hitSomething = Intersect.Scene(scene, ray, tMin, tMax, out hit); if (hitSomething) { color *= BRDF(hit); Ray3f nextRay; bool scattered = Scatter(ray, hit, ref rng, fibs, out nextRay); if (scattered) { ray = nextRay; return(true); } return(false); } else { float t = 0.5f * (ray.direction.y + 1f); float3 light = (1f - t) * new float3(1f) + t * scene.LightColor; color = light * color; return(false); } }
public static float3 TraceRecursive(Ray3f ray, Scene scene, ref Random rng, NativeArray <float3> fibs, int depth, int maxDepth, ref ushort rayCount) { HitRecord hit; const float tMin = 0f; const float tMax = 1000f; bool hitSomething = Intersect.Scene(scene, ray, tMin, tMax, out hit); ++rayCount; float3 light = new float3(0); if (hitSomething) { Ray3f nextRay; bool scattered = Scatter(ray, hit, ref rng, fibs, out nextRay); if (scattered && depth < maxDepth) { light = TraceRecursive(nextRay, scene, ref rng, fibs, depth + 1, maxDepth, ref rayCount); } light = BRDF(hit) * light; } else { float t = 0.5f * (ray.direction.y + 1f); light = (1f - t) * new float3(1f) + t * scene.LightColor; } return(light); }
private void OnDrawGizmos() { if (!Application.isPlaying || !_drawDebugRays) { return; } var rng = new Unity.Mathematics.Random(14387); var screenPos = new float2(0); float3 pixel = new float3(0f); Gizmos.color = new Color(1f, 1f, 1f, 0.5f); for (int s = 0; s < _scene.Spheres.Length; s++) { Gizmos.DrawSphere(_scene.Spheres[s].Center, _scene.Spheres[s].Radius); } for (int r = 0; r < 16; r++) { Gizmos.color = Color.HSVToRGB(r / 8f, 0.7f, 0.5f); float2 jitter = new float2(rng.NextFloat(), rng.NextFloat() / 16f); float2 p = (screenPos + jitter); var ray = _camera.GetRay(p, ref rng); for (int t = 0; t < 8; t++) { const float tMin = 0f; const float tMax = 1000f; Gizmos.DrawSphere(ray.origin, 0.01f); HitRecord hit; bool hitSomething = Intersect.Scene(_scene, ray, tMin, tMax, out hit); if (hitSomething) { Gizmos.DrawLine(ray.origin, hit.point); Ray3f subRay; if (!Trace.Scatter(ray, hit, ref rng, _fibs, out subRay)) { break; } ray = subRay; } else { Gizmos.DrawRay(ray.origin, math.normalize(ray.direction)); break; } } } }