public SolidTexture(ColorIntensity color) { this.color = color; }
private void PhotonShade(HitInfo hit, Line ray, int threadId, int depth, bool inside, ColorIntensity lightCol, Random rnd, double distance, double area) { if (depth > maxrecurse) return; if (lightCol.GreyScale() < minratio) return; RealHitInfo realHit = hit.GetReal(ray); Material surf = realHit.HitStuff; ColorIntensity pigment = realHit.Pigment.GetTexture(realHit.Normal, 0); double dist = realHit.Normal.Start.LineTo(ray.Start).Length; distance += dist; if (surf.Attenutive && inside) { lightCol.R *= Math.Exp(-(1.0 - surf.Attenuation[0]) * dist / surf.AttenuationDistance); lightCol.G *= Math.Exp(-(1.0 - surf.Attenuation[1]) * dist / surf.AttenuationDistance); lightCol.B *= Math.Exp(-(1.0 - surf.Attenuation[2]) * dist / surf.AttenuationDistance); } #if DEBUG Photon currentParent = PhotonParents[threadId]; #endif if ((depth > 0 || allLightsArePhotons)) { Photon photon = new Photon(); photon.HitPos.X = (float)realHit.Normal.Start.X; photon.HitPos.Y = (float)realHit.Normal.Start.Y; photon.HitPos.Z = (float)realHit.Normal.Start.Z; photon.TravelDir.Dx = (float)ray.Direct.Dx; photon.TravelDir.Dy = (float)ray.Direct.Dy; photon.TravelDir.Dz = (float)ray.Direct.Dz; ColorIntensity photonColor = lightCol; if (realLighting || distance == double.PositiveInfinity) { photonColor.R *= area / PhotonCount; photonColor.G *= area / PhotonCount; photonColor.B *= area / PhotonCount; } else { photonColor.R *= distance * distance * area / PhotonCount; photonColor.G *= distance * distance * area / PhotonCount; photonColor.B *= distance * distance * area / PhotonCount; } photon.PhotonColorPower.R = (float)photonColor.R; photon.PhotonColorPower.G = (float)photonColor.G; photon.PhotonColorPower.B = (float)photonColor.B; #if DEBUG photon.parent = currentParent; PhotonParents[threadId] = photon; #endif Map.AddPhoton(photon, threadId); } ColorIntensity reflectance = new ColorIntensity(); ColorIntensity transmitance = new ColorIntensity(); Line refractRay = new Line(); Line reflectRay = new Line(); if (surf.Refractive || surf.Reflective) { ray.Direct = ray.Direct.Scale(1 / ray.Direct.Length); reflectance.R = surf.Reflective ? surf.Reflectance[0] : 0.0; reflectance.G = surf.Reflective ? surf.Reflectance[1] : 0.0; reflectance.B = surf.Reflective ? surf.Reflectance[2] : 0.0; if (surf.Refractive) { double ni = inside ? surf.RefractIndex : 1.0; double nt = (!inside) ? surf.RefractIndex : 1.0; double cratio = ni / nt; double ct1 = -ray.Direct.Dot(realHit.Normal.Direct); double ct2sqrd = 1 - cratio * cratio * (1 - ct1 * ct1); if (ct2sqrd <= 0) { reflectance.R = 1; reflectance.G = 1; reflectance.B = 1; } else { double ct2 = Math.Sqrt(ct2sqrd); // fresnel equations for reflectance perp and parallel. double rperp = (ni * ct1 - nt * ct2) / (ni * ct1 + nt * ct2); double rpll = (nt * ct1 - ni * ct2) / (ni * ct2 + nt + ct1); // assume unpolarised light always - better then tracing 2 // rays for both sides of every interface. double reflectanceval = (rperp * rperp + rpll * rpll) / 2; reflectance.R = Math.Min(1.0, reflectance.R + reflectanceval); reflectance.G = Math.Min(1.0, reflectance.G + reflectanceval); reflectance.B = Math.Min(1.0, reflectance.B + reflectanceval); transmitance.R = 1 - reflectance.R; transmitance.G = 1 - reflectance.G; transmitance.B = 1 - reflectance.B; refractRay.Direct = ray.Direct.Scale(cratio); refractRay.Direct.Add(realHit.Normal.Direct.Scale(cratio * (ct1) - ct2)); refractRay.Start = realHit.Normal.Start.MoveBy(refractRay.Direct.Scale(EPSILON * 10)); } } reflectRay.Direct = new Vector(ray.Direct.Dx, ray.Direct.Dy, ray.Direct.Dz); reflectRay.Direct.Add(realHit.Normal.Direct.Scale(-2 * ray.Direct.Dot(realHit.Normal.Direct))); reflectRay.Start = realHit.Normal.Start.MoveBy(reflectRay.Direct.Scale(EPSILON * 10)); } bool doReflect = false; bool doRefract = false; bool doDifuse = false; double avr = reflectance.R + reflectance.G + reflectance.B; avr /= 3; double avt = transmitance.R + transmitance.G + transmitance.B; avt /= 3; double reflectWeight = Math.Max(avr, 0.0); double refractWeight = Math.Max(avt, 0.0); double diffuseWeight = Math.Max(pigment.GreyScale() * surf.Diffuse, 0.0); double specularity = 1.0; double diffusivity = 1.0; if (surf.Refractive) { specularity = surf.Specularity; diffusivity = 1.0 - specularity; reflectWeight *= surf.Specularity; refractWeight *= surf.Specularity; diffuseWeight *= 1.0-surf.Specularity; } double choice = rnd.NextDouble(); if (choice < diffuseWeight) doDifuse = true; else if (choice < diffuseWeight+reflectWeight) doReflect = true; else if (choice < diffuseWeight+reflectWeight+refractWeight) doRefract = true; if (doRefract && avt > minratio) { SetIn(ref refractRay.Start, threadId); HitInfo hitnew = scene.Intersect(refractRay, threadId); if (!(hitnew.HitDist == -1)) { ColorIntensity lightCol2 = new ColorIntensity(); lightCol2.R = lightCol.R * transmitance.R *specularity/ refractWeight; lightCol2.G = lightCol.G * transmitance.G * specularity / refractWeight; lightCol2.B = lightCol.B * transmitance.B * specularity / refractWeight; PhotonShade(hitnew, refractRay, threadId, depth + 1, !inside, lightCol2, rnd, distance, area); } } if (doReflect && avr > minratio) { SetIn(ref reflectRay.Start, threadId); HitInfo hitnew2 = scene.Intersect(reflectRay, threadId); if (!(hitnew2.HitDist == -1)) { ColorIntensity lightCol2 = new ColorIntensity(); lightCol2.R = lightCol.R * reflectance.R * specularity / reflectWeight; lightCol2.G = lightCol.G * reflectance.G * specularity / reflectWeight; lightCol2.B = lightCol.B * reflectance.B * specularity / reflectWeight; PhotonShade(hitnew2, reflectRay, threadId, depth + 1, inside, lightCol2, rnd, distance, area); } } if (doDifuse) { double z = rnd.NextDouble(); double theta = rnd.NextDouble() * 2.0 * Math.PI; double r = Math.Sqrt(1 - z * z); double x = Math.Cos(theta) * r; double y = Math.Sin(theta) * r; Line diffuseRay = new Line(); Vector a; Vector b; Vector basis = realHit.Normal.Direct.Scale(1.0/realHit.Normal.Direct.Length); GetPerp(basis, out a, out b); diffuseRay.Direct = basis.Scale(z); diffuseRay.Direct.Add(a.Scale(x/a.Length)); diffuseRay.Direct.Add(b.Scale(y/b.Length)); diffuseRay.Start = realHit.Normal.Start.MoveBy(diffuseRay.Direct.Scale(EPSILON * 10)); SetIn(ref diffuseRay.Start, threadId); HitInfo hitnew2 = scene.Intersect(diffuseRay, threadId); if (!(hitnew2.HitDist == -1)) { ColorIntensity lightCol2 = new ColorIntensity(); lightCol2.R = lightCol.R * surf.Diffuse * pigment.R *diffusivity/ diffuseWeight; lightCol2.G = lightCol.G * surf.Diffuse * pigment.G * diffusivity / diffuseWeight; lightCol2.B = lightCol.B * surf.Diffuse * pigment.B * diffusivity / diffuseWeight; PhotonShade(hitnew2, diffuseRay, threadId, depth + 1, inside, lightCol2, rnd, distance, area); } } #if DEBUG PhotonParents[threadId] = currentParent; #endif }
private ColorIntensity Shade(RealHitInfo hit, Line by, int recurse, double ratio, bool inside, int threadId) { #if DEBUG if (Gathering) { CurrentGather.Weight = ratio; CurrentGather.RealInfo = hit; } #endif ColorIntensity colors, shadowcolors; Material surf = hit.HitStuff; ColorIntensity pigment = hit.Pigment.GetTexture(hit.Normal, 0); ColorIntensity atten = new ColorIntensity(); if (surf.Attenutive && inside) { double dist = hit.Normal.Start.LineTo(by.Start).Length; atten.R = Math.Exp(-(1.0 - surf.Attenuation[0]) * dist / surf.AttenuationDistance); atten.G = Math.Exp(-(1.0 - surf.Attenuation[1]) * dist / surf.AttenuationDistance); atten.B = Math.Exp(-(1.0 - surf.Attenuation[2]) * dist / surf.AttenuationDistance); ratio = ratio * (atten.R + atten.G + atten.B) / 3; } double rI = 0; double gI = 0; double bI = 0; Vector lightdir; Line newray = new Line(); HitInfo hitnewray = new HitInfo(); hit.Normal.Direct = hit.Normal.Direct.Scale(1 / hit.Normal.Direct.Length); Vector eye = by.Direct.Scale(-1 / by.Direct.Length); int lightCount = (allLightsArePhotons && photons) ? 0 : lights.Count; //lightCount = 0; for (int i = 0; i < lightCount; i++) { double lightdist = double.PositiveInfinity; switch (lights[i].LightType) { case LightType.Ambient: rI += lights[i].Color.R * surf.Ambient[0]; gI += lights[i].Color.G * surf.Ambient[1]; bI += lights[i].Color.B * surf.Ambient[2]; continue; case LightType.Directional: lightdir = lights[i].Direction.Direct.Scale(-1); break; case LightType.Point: lightdir = hit.Normal.Start.LineTo(lights[i].Direction.Start); lightdist = lightdir.Length; break; default: continue; } // Add double illuminate flag or something to turn this off. if (true) { if (lightdir.Dot(hit.Normal.Direct) <= 0.0) continue; } newray.Direct = lightdir.Scale(1.0/lightdir.Length); if (newray.Direct.Dot(hit.Normal.Direct) >= 0) newray.Start = hit.Normal.Start.MoveBy(newray.Direct.Scale(EPSILON * 10)); else newray.Start = hit.Normal.Start.MoveBy(newray.Direct.Scale(-EPSILON * 10)); SetIn(ref newray.Start, threadId); HitInfo info = scene.Intersect(newray, false, threadId); if (!(info.HitDist == -1 || info.HitDist > lightdist)) continue; shadowcolors.R = 1.0; shadowcolors.G = 1.0; shadowcolors.B = 1.0; // Don't need to setIn again, seperate intersect cache for shadow vs non-shadow rays. hitnewray = scene.Intersect(newray, threadId); if (!photons) { if (!(hitnewray.HitDist == -1 || hitnewray.HitDist > lightdist)) { #if DEBUG GatherInfo last = null; if (Gathering) { last = CurrentGather; CurrentGather = new GatherInfo(); } #endif shadowcolors = ShadowShade(hitnewray.GetReal(newray), newray, recurse + 1, ratio, inside, threadId, lightdist); #if DEBUG if (Gathering) { last.LightChildren.Add(CurrentGather); CurrentGather = last; } #endif } } else { if (!(hitnewray.HitDist == -1 || hitnewray.HitDist > lightdist)) continue; } shadowcolors.R *= lights[i].Color.R; shadowcolors.G *= lights[i].Color.G; shadowcolors.B *= lights[i].Color.B; if (realLighting && lightdist < double.PositiveInfinity) { double lightdistSq = lightdist * lightdist; shadowcolors.R /= lightdistSq; shadowcolors.G /= lightdistSq; shadowcolors.B /= lightdistSq; } CalculateLightContrib(hit.Normal.Direct, shadowcolors, surf, pigment, ref rI, ref gI, ref bI, lightdir, eye); } if (photons) { double scaleRadSq; List<Photon> closePhotons = Map.GetClosest(hit.Normal.Start, hit.Normal.Direct, out scaleRadSq, threadId); double photoR = 0.0; double photoG = 0.0; double photoB = 0.0; foreach (Photon photon in closePhotons) { lightdir.Dx = -photon.TravelDir.Dx; lightdir.Dy = -photon.TravelDir.Dy; lightdir.Dz = -photon.TravelDir.Dz; if (true) { if (lightdir.Dot(hit.Normal.Direct) <= 0.0) continue; } ColorIntensity photonColor; photonColor.R = photon.PhotonColorPower.R; photonColor.G = photon.PhotonColorPower.G; photonColor.B = photon.PhotonColorPower.B; CalculateLightContrib(hit.Normal.Direct, photonColor, surf, pigment, ref photoR, ref photoG, ref photoB, lightdir, eye); #if DEBUG if (Gathering) { CurrentGather.GatheredPhotons.Add(photon); } #endif } if (closePhotons.Count > 0) { rI += photoR / Math.PI / scaleRadSq; if (rI > 3) { int breakpoint = 3; breakpoint *= 34; } gI += photoG / Math.PI / scaleRadSq; if (gI > 3) { int breakpoint = 3; breakpoint *= 34; } bI += photoB / Math.PI / scaleRadSq; if (bI > 3) { int breakpoint = 3; breakpoint *= 34; } } } if ((surf.Refractive || surf.Reflective) && recurse < maxrecurse) { by.Direct = by.Direct.Scale(1 / by.Direct.Length); ColorIntensity reflectance = new ColorIntensity(); reflectance.R = surf.Reflective ? surf.Reflectance[0] : 0.0; reflectance.G = surf.Reflective ? surf.Reflectance[1] : 0.0; reflectance.B = surf.Reflective ? surf.Reflectance[2] : 0.0; if (surf.Refractive) { double ni = inside ? surf.RefractIndex : 1.0; double nt = (!inside) ? surf.RefractIndex : 1.0; double cratio = ni / nt; double ct1 = -by.Direct.Dot(hit.Normal.Direct); double ct2sqrd = 1 - cratio * cratio * (1 - ct1 * ct1); if (ct2sqrd <= 0) { reflectance.R = 1; reflectance.G = 1; reflectance.B = 1; } else { double ct2 = Math.Sqrt(ct2sqrd); // fresnel equations for reflectance perp and parallel. double rperp = (ni * ct1 - nt * ct2) / (ni * ct1 + nt * ct2); double rpll = (nt * ct1 - ni * ct2) / (ni * ct2 + nt + ct1); // assume unpolarised light always - better then tracing 2 // rays for both sides of every interface. double reflectanceval = (rperp * rperp + rpll * rpll) / 2; reflectance.R = Math.Min(1.0, reflectance.R + reflectanceval); reflectance.G = Math.Min(1.0, reflectance.G + reflectanceval); reflectance.B = Math.Min(1.0, reflectance.B + reflectanceval); ColorIntensity transmitance = new ColorIntensity(); transmitance.R = 1 - reflectance.R; transmitance.G = 1 - reflectance.G; transmitance.B = 1 - reflectance.B; double avt = transmitance.R + transmitance.G + transmitance.B; avt /= 3; if (avt * ratio*surf.Specularity > minratio) { Line newray2 = new Line(); newray2.Direct = by.Direct.Scale(cratio); newray2.Direct.Add(hit.Normal.Direct.Scale(cratio * (ct1) - ct2)); newray2.Start = hit.Normal.Start.MoveBy(newray2.Direct.Scale(EPSILON * 10)); SetIn(ref newray2.Start, threadId); HitInfo hitnew = scene.Intersect(newray2, threadId); if (!(hitnew.HitDist == -1)) { #if DEBUG GatherInfo last2 = null; if (Gathering) { last2 = CurrentGather; CurrentGather = new GatherInfo(); } #endif ColorIntensity cl = Shade(hitnew.GetReal(newray2), newray2, recurse + 1, avt * ratio * surf.Specularity, !inside, threadId); #if DEBUG if (Gathering) { last2.Children.Add(CurrentGather); CurrentGather = last2; } #endif rI = rI + ((double)transmitance.R * cl.R) * surf.Specularity; gI = gI + ((double)transmitance.G * cl.G) * surf.Specularity; bI = bI + ((double)transmitance.B * cl.B) * surf.Specularity; } } } } double avr = reflectance.R + reflectance.G + reflectance.B; avr /= 3; double specularity = surf.Refractive ? surf.Specularity : 1.0; if (avr * ratio*specularity > minratio) { Line newray2 = new Line(); newray2.Direct = new Vector(by.Direct.Dx, by.Direct.Dy, by.Direct.Dz); newray2.Direct.Add(hit.Normal.Direct.Scale(-2 * by.Direct.Dot(hit.Normal.Direct))); newray2.Start = hit.Normal.Start.MoveBy(newray2.Direct.Scale(EPSILON * 10)); SetIn(ref newray2.Start, threadId); HitInfo hitnew2 = scene.Intersect(newray2, threadId); if (!(hitnew2.HitDist == -1)) { #if DEBUG GatherInfo last3 = null; if (Gathering) { last3 = CurrentGather; CurrentGather = new GatherInfo(); } #endif ColorIntensity cl2 = Shade(hitnew2.GetReal(newray2), newray2, recurse + 1, avr * ratio * specularity, inside, threadId); #if DEBUG if (Gathering) { last3.Children.Add(CurrentGather); CurrentGather = last3; } #endif rI = rI + ((double)reflectance.R * cl2.R) * specularity; gI = gI + ((double)reflectance.G * cl2.G) * specularity; bI = bI + ((double)reflectance.B * cl2.B) * specularity; } } } else { if (recurse >= maxrecurse) { // Recurse limit reached. int i = 0; i = i * 3; } } if (surf.Attenutive && inside) { rI *= atten.R; gI *= atten.G; bI *= atten.B; } colors.R = rI; colors.G = gI; colors.B = bI; return colors; }
private void PhotonShade(HitInfo hit, Line ray, int threadId, ColorIntensity lightCol, Random rnd, double distance, double area) { PhotonShade(hit, ray, threadId, 0, false, lightCol, rnd, distance, area); }
private void GeneratePhotons(int threadId, int threadCount) { Random rnd = new Random(); if (!photons) return; foreach (Light light in Lights) { if (light.LightType == LightType.Point) { for (int i = 0; i < PhotonCount/threadCount; i++) { double z = rnd.NextDouble()*2-1; double theta = rnd.NextDouble()*2.0*Math.PI; double r = Math.Sqrt(1-z*z); double x = Math.Cos(theta)*r; double y = Math.Sin(theta)*r; Line ray = new Line(); ray.Direct = new Vector(x, y, z); ray.Start = light.Direction.Start; SetIn(ref ray.Start, threadId); HitInfo hit = scene.Intersect(ray, threadId); ColorIntensity lightCol = new ColorIntensity(); lightCol.R = light.Color.R; lightCol.G = light.Color.G; lightCol.B = light.Color.B; if (hit.HitDist != -1) { PhotonShade(hit, ray, threadId, lightCol, rnd, 0.0, Math.PI*4); } if (i % 100000 == 0) GC.Collect(); } } else if (light.LightType == LightType.Directional) { Point centre; double radius; scene.Bounds.GetSphere(out centre, out radius); Point eye = centre.MoveBy(light.Direction.Direct.Scale(-radius * 2)); Vector a; Vector b; GetPerp(light.Direction.Direct.Scale(1.0/light.Direction.Direct.Length), out a, out b); for (int i = 0; i < PhotonCount/threadCount; i++) { double x = 0.0; double y= 0.0; do { x = rnd.NextDouble() * 2 - 1; y = rnd.NextDouble() * 2 - 1; } while (x * x + y * y > 1.0); x = x * radius; y = y * radius; Line ray = new Line(); ray.Direct = light.Direction.Direct; ray.Start = eye.MoveBy(a.Scale(x/a.Length)).MoveBy(b.Scale(y/b.Length)); SetIn(ref ray.Start, threadId); HitInfo hit = scene.Intersect(ray, threadId); ColorIntensity lightCol = new ColorIntensity(); lightCol.R = light.Color.R; lightCol.G = light.Color.G; lightCol.B = light.Color.B; if (hit.HitDist != -1) { PhotonShade(hit, ray, threadId, lightCol, rnd, double.PositiveInfinity, Math.PI*radius*radius); } if (i % 100000 == 0) GC.Collect(); } } } }
private bool Decide(byte[] screen, int screenwidth, ColorIntensity current) { // Todo: new decide function. return false; }
private void CalculateLightContrib(Vector normal, ColorIntensity lightColor, Material surf, ColorIntensity pigment, ref double rI, ref double gI, ref double bI, Vector lightdir, Vector eye) { double diffusivity = 1.0-surf.Specularity; if (surf.Phong > 0.0) { Vector reflection = eye; reflection.Add(normal.Scale(-2.0 * eye.Dot(normal))); double phong = -lightdir.Scale(1.0 / lightdir.Length).Dot(reflection); if (phong > 0.0) { double phongpowd = Math.Pow(phong, surf.Exponent); rI += surf.Phong * phongpowd * lightColor.R*diffusivity; gI += surf.Phong * phongpowd * lightColor.G * diffusivity; bI += surf.Phong * phongpowd * lightColor.B * diffusivity; } } if (surf.Specular > 0.0) { Vector half = lightdir.Scale(1.0 / lightdir.Length); half.Add(eye); half.ScaleSelf(0.5); double spec = half.Dot(normal) / half.Length; if (spec > 0.0) { double specpowd = Math.Pow(spec, 1.0 / surf.Roughness); rI += surf.Specular * specpowd * lightColor.R * diffusivity; gI += surf.Specular * specpowd * lightColor.G * diffusivity; bI += surf.Specular * specpowd * lightColor.B * diffusivity; } } if (surf.Iridescence > 0.0) { double cosaoi = lightdir.Dot(normal)/lightdir.Length; double interference = 4.0 * Math.PI * surf.FilmThickness * cosaoi; double intensity = cosaoi * surf.Iridescence; double rwl = 0.25; double gwl = 0.18; double bwl = 0.14; /* Modify color by phase offset for each wavelength. */ rI += surf.Iridescence * (intensity * (1.0 - 0.5 * Math.Cos(interference / rwl))) * diffusivity; gI += surf.Iridescence * (intensity * (1.0 - 0.5 * Math.Cos(interference / gwl))) * diffusivity; bI += surf.Iridescence * (intensity * (1.0 - 0.5 * Math.Cos(interference / bwl))) * diffusivity; } double lam = lambert2(lightdir, normal, surf.Brilliance); rI += surf.Diffuse * pigment.R * lam * lightColor.R * diffusivity; gI += surf.Diffuse * pigment.G * lam * lightColor.G * diffusivity; bI += surf.Diffuse * pigment.B * lam * lightColor.B * diffusivity; }
public IntensityMap(int width, int height) { this.width = width; this.height = height; pix = new ColorIntensity[width * height]; }