public Color ShadeHit(IntersectionData intersectionData, int remaining = 5) { var color = Color.Black; foreach (var light in Lights) { var isShadowed = IsShadowed(intersectionData.OverPoint, light); var surface = intersectionData.Object.Material.Lighting(light, intersectionData.Object, intersectionData.OverPoint, intersectionData.EyeVector, intersectionData.Normal, isShadowed); var reflected = ReflectedColor(intersectionData, remaining); var refracted = RefractedColor(intersectionData, remaining); var material = intersectionData.Object.Material; if (material.Reflective > 0 && material.Transparency > 0) { var reflectance = intersectionData.Schlick(); color += surface + reflected * reflectance + refracted * (1 - reflectance); } else { color += surface + reflected + refracted; } } return(color); }
public unsafe Color ShadeHit(IntersectionData intersectionData, int remaining = 5) { double *x = stackalloc double[ILight.MAX_SAMPLE]; double *y = stackalloc double[ILight.MAX_SAMPLE]; double *z = stackalloc double[ILight.MAX_SAMPLE]; var overPoint = intersectionData.OverPoint; var eyeVector = intersectionData.EyeVector; var normal = intersectionData.Normal; var material = intersectionData.Object.Material; var shapeColor = material.Pattern.GetColorAtShape(intersectionData.Object, ref overPoint); var surface = Color.Black; for (var i = 0; i < Lights.Count; i++) { var light = Lights[i]; int nbSamples = light.GetPositions(x, y, z); double lightIntensity = 0; var lightColor = Color.Black; for (int j = 0; j < nbSamples; j++) { var sampleLightColor = light.GetIntensityAt(x[j], y[j], z[j], ref overPoint); lightColor += sampleLightColor; if (lightColor.Equals(Color.Black)) { continue; } bool isShadowed = IsShadowed(overPoint, x[j], y[j], z[j]); lightIntensity += isShadowed ? 0 : 1; } lightIntensity /= nbSamples; lightColor /= nbSamples; surface += material.Lighting(nbSamples, x, y, z, ref overPoint, ref eyeVector, ref normal, lightIntensity, shapeColor, lightColor); } var reflected = ReflectedColor(intersectionData, remaining); var refracted = RefractedColor(intersectionData, remaining); if (material.Reflective > 0 && material.Transparency > 0) { var reflectance = intersectionData.Schlick(); var color = surface + reflected * reflectance + refracted * (1 - reflectance); return(color); } else { var color = surface + reflected + refracted; return(color); } }