IEnumerator DoLight(SpriteRenderer sprite, Color color) { float h; float s; float v; ColorManipulator.ColorToHSV(color, out h, out s, out v); var wait = new WaitForSeconds(0.25f); while (true) { if (!this.ShouldBeLit) { yield return(null); continue; } v = Mathf.Lerp(0, 1, this.LightLevel); sprite.color = ColorManipulator.ColorFromHSV(h, s, v); yield return(wait); } }
/// <summary> /// Most scene objects use the same procedure for calculating color at a /// particular point, however sometimes this must be overridden to first /// adjust the object normal /// </summary> public virtual Color PointColor(Scene scene, Vector3 intersectionPoint, Vector3 intersectionNormal, Vector3 rayDirection, int reflections) { Color pointColor = Color.FromArgb(0, 0, 0); // Determine intensity contribution from each light source foreach (ISceneLight light in scene.lights) { double diffuse = light.Diffuse(scene, intersectionPoint, intersectionNormal); double specular = light.Specular(scene, intersectionPoint, intersectionNormal, rayDirection); specular = specularReflectivity * Math.Pow(specular, specularFalloff); // Diffuse lighting takes the color of the object Color temp = ColorManipulator.Multiply(color, diffuse); pointColor = ColorManipulator.Add(pointColor, temp); // Specular highlights take the color of the light (currently fixed to white) temp = ColorManipulator.Multiply(Color.White, specular); pointColor = ColorManipulator.Add(pointColor, temp); } // Transparency Intersection intersection = scene.ClosestIntersection(intersectionPoint, rayDirection); if (intersection.DidIntersect) { // TODO refraction var intersectionPointNew = intersectionPoint + (float)(intersection.Position) * rayDirection; Color objColor = intersection.IntersectedObject.PointColor(scene, intersectionPointNew, intersection.Normal, rayDirection, reflections); pointColor = ColorManipulator.Add( ColorManipulator.Multiply(pointColor, 1 - transparency), ColorManipulator.Multiply(objColor, transparency)); } // Color contribution from reflections if (reflections > 0) { // Reflect view ray across intersection normal and fire new ray in this direction Vector3 viewReflection = rayDirection - 2 * intersectionNormal * Vector3.Dot(rayDirection, intersectionNormal); intersection = scene.ClosestIntersection(intersectionPoint, viewReflection); // No reflection if (!intersection.DidIntersect) { return(pointColor); } // Blend object color with reflected color according to object reflectivity Vector3 intersectionPointNew = intersectionPoint + (float)(intersection.Position) * viewReflection; Color objColor = intersection.IntersectedObject.PointColor(scene, intersectionPointNew, intersection.Normal, viewReflection, reflections - 1); pointColor = ColorManipulator.Add(ColorManipulator.Multiply(objColor, reflectivity), ColorManipulator.Multiply(pointColor, (1 - reflectivity))); } return(pointColor); }