private Color32 GetColor(Ray ray) { // Sphere HitRecord record = new HitRecord(); RayHit hit = new RayHit(); foreach (Hittable h in m_Hittables) { hit = h.DoRaycast(ray, 0f, 0f, ref record); } if (hit.IsValid()) { return(new Color32( (byte)Mathf.Clamp(Mathf.Abs(hit.normal.x) * 255f, 0, 255), (byte)Mathf.Clamp(Mathf.Abs(hit.normal.y) * 255f, 0, 255), (byte)Mathf.Clamp(Mathf.Abs(hit.normal.z) * 255f, 0, 255), (byte)255)); } // Sky float t = (ray.direction.y + (m_SceneHeight * .5f)) / m_SceneHeight; return(Color32.Lerp(COLOR_WHITE, COLOR_SKY_BLUE, t)); }
private void DoRays() { Ray ray = new Ray(Vector3.zero, new Vector3(0f, 0f, m_SceneLowerLeft.z)); int index = -1; if (m_EnableAA) { for (int y = 0; y < m_Height; y++) { for (int x = 0; x < m_Width; x++) { int r = 0, g = 0, b = 0; for (int n = 0; n < m_AASamples; n++) { float u = (x + (float)m_RandomDouble.NextDouble()) / (float)m_Width; float v = (y + (float)m_RandomDouble.NextDouble()) / (float)m_Height; ray.direction.x = m_SceneLowerLeft.x + (u * m_SceneWidth); ray.direction.y = m_SceneLowerLeft.y + (v * m_SceneHeight); Color32 res = GetColor(ray); r += res.r; g += res.g; b += res.b; } m_ColorBuffer[++index] = new Color32( (byte)Mathf.Clamp(r * m_InvAASamples, 0, 255), (byte)Mathf.Clamp(g * m_InvAASamples, 0, 255), (byte)Mathf.Clamp(b * m_InvAASamples, 0, 255), 255); } } } else { for (int y = 0; y < m_Height; y++) { for (int x = 0; x < m_Width; x++) { float u = x / (float)m_Width; float v = y / (float)m_Height; ray.direction.x = m_SceneLowerLeft.x + (u * m_SceneWidth); ray.direction.y = m_SceneLowerLeft.y + (v * m_SceneHeight); m_ColorBuffer[++index] = GetColor(ray); } } } }