Пример #1
0
        public override Vector3 EvalMaterial(Intersection intersec, Material mat)
        {
            if (mat.Textured)
            {
                double x = Math.Floor(intersec.Point.X);
                double y = Math.Floor(intersec.Point.Z);

                if (x % 2 == 0 ^ y % 2 == 0)
                    return mat.Color.ToVector() * 0.5;
            }
            return mat.Color.ToVector();
        }
Пример #2
0
        public override Vector3 EvalMaterial(Intersection intersec, Material mat)
        {
            if (mat.Textured)
            {
                //double scale = 255.0;
                //int u = (int)Math.Floor(intersec.BaryCoords.X * scale);
                //int v = (int)Math.Floor(intersec.BaryCoords.Y * scale);

                //double fac = (u ^ v) * 0.75 / scale + 0.25;
                //col *= fac;

                Vector2 bary = intersec.BaryCoords;
                double v0contrib = 1 - bary.X - bary.Y;
                double v1contrib = 1 - bary.X;
                double v2contrib = 1 - bary.Y;
                Vector2 texCoord = Vertex0.TexCoord + (Vertex1.TexCoord - Vertex0.TexCoord) * v1contrib + (Vertex2.TexCoord - Vertex0.TexCoord) * v2contrib;
                texCoord.X = 1 - texCoord.X;

                return mat.GetTextureColor(texCoord);
            }
            return mat.Color.ToVector();
        }
Пример #3
0
        public override Vector3 EvalMaterial(Intersection intersec, Material mat)
        {
            if (mat.Textured)
            {
                double xtex = (1 + Math.Atan2(intersec.Normal.Z, intersec.Normal.X) / Math.PI) * 0.5;
                double ytex = Math.Acos(intersec.Normal.Y) / Math.PI;
                double scale = 4;

                bool pattern = ((xtex * scale) % 1.0 > 0.5) ^ ((ytex * scale) % 1.0 > 0.5);
                if (pattern)
                    return Vector3.Zero;

                //int x = (int)(Math.Abs(point.X) * 255.0);
                //int y = (int)(Math.Abs(point.Y) * 255.0);
                //int z = (int)(Math.Abs(point.Z) * 255.0);
                //int c = x ^ y ^ z;

                //col = Extensions.ColorFromHSV(c / 255.0 * 360.0, 0.8, 0.7).ToVector();
            }

            return mat.Color.ToVector();
        }
Пример #4
0
 public override Vector3 EvalMaterial(Intersection intersec, Material mat)
 {
     return mat.Color.ToVector();
 }
Пример #5
0
        public override Vector3 EvalMaterial(Intersection intersec, Material mat)
        {
            Vector2 bary = intersec.BaryCoords;

            if (bary.X + bary.Y > 1.0)
            {
                intersec.BaryCoords.X = 1.0 - intersec.BaryCoords.X;
                intersec.BaryCoords.Y = 1.0 - intersec.BaryCoords.Y;
                return T2.EvalMaterial(intersec, mat);
            }
            return T1.EvalMaterial(intersec, mat);
        }
Пример #6
0
 public abstract Vector3 EvalMaterial(Intersection intersec, Material mat);
Пример #7
0
        private double GetAmbientOcclusion(Intersection res, int rays)
        {
            int notblocked = 0;
            for (int i = 0; i < rays; i++)
            {
                //Vector2 sample = pdisk_64[i];
                double theta = 2 * Math.PI * MwcRng.GetUniform();
                double phi = Math.Acos(MwcRng.GetUniform() * 2 - 1);

                Vector3 rand = new Vector3(
                    Math.Cos(theta) * Math.Sin(phi),
                    Math.Sin(theta) * Math.Sin(phi),
                    Math.Cos(phi)
                    );

                if (Vector3.DotProduct(rand, res.Normal) < 0.0)
                    rand.Negate();

                Ray newray = new Ray(res.Point, rand);
                Intersection res2 = IntersectSceneExcept(newray, res.Object);
                if (!res2.Intersects)
                    notblocked++;
            }

            return notblocked * 1.0 / rays;
        }
Пример #8
0
        private double EvalLights(Intersection res)
        {
            double lambertAmount = 0;
            foreach (var light in lights)
            {
                double contribution = 0;
                if (light is PointLight)
                {
                    if (!IsLightVisible(res.Point, light))
                        continue;
                    contribution = Vector3.DotProduct(((light as PointLight).Position - res.Point).Normalized, res.Normal);
                }
                if (light is AreaLight)
                {
                    AreaLight alight = light as AreaLight;
                    double lambert = Vector3.DotProduct((alight.Position - res.Point).Normalized, res.Normal).Clamp(0, 1);

                    int tests = pdisk_32.Samples.Count;
                    int obstructed = 0;
                    for (int i = 0; i < tests; i++)
                    {
                        Vector2 uv = pdisk_32[i] * 2.0 - Vector2.One;

                        Vector3 diff = res.Point - alight.Position;
                        Vector3 normDiff = diff.Normalized;

                        Vector3 right = Vector3.CrossProduct(normDiff, camera.GlobalUp).Normalized;
                        Vector3 up = Vector3.CrossProduct(right, normDiff).Normalized;
                        right *= uv.X * alight.Size;
                        up *= uv.Y * alight.Size;

                        Vector3 apoint = alight.Position + right + up;

                        if (PointObstructed(res.Point, apoint))
                            obstructed++;
                    }

                    double visibility = (tests - obstructed) / (double)tests;

                    contribution = visibility * lambert;
                }

                lambertAmount += contribution * light.Strength;
            }

            return Math.Min(1, lambertAmount);
        }
Пример #9
0
        public Bitmap RenderSSAO(int width, int height)
        {
            Intersection[] intersecmap = new Intersection[width * height];
            camera.SetResolution(width, height);

            Parallel.For(0, width, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, (x) =>
            {
                for (int y = 0; y < height; y++)
                {
                    intersecmap[x + y * width] = IntersectScene(camera.CastRay(x, y));
                }
            });

            Bitmap bmp;
            BitmapData data;
            byte[] pixels = CreateAndLockBitmap(width, height, PixelFormat.Format24bppRgb, out bmp, out data);

            Vector2 filterRadius = new Vector2(10.0 / width, 10.0 / height);
            double distanceThreshold = 1.0;

            Parallel.For(0, width, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, (x) =>
            {
                for (int y = 0; y < height; y++)
                {
                    Vector3 pos = intersecmap[x + y * width].Point;
                    Vector3 normal = intersecmap[x + y * width].Normal;

                    double ambientOcclusion = 0;

                    for (int i = 0; i < poissonSamples.Length; i++)
                    {
                        Vector2 coord = new Vector2(x, y) + poissonSamples[i] * filterRadius;

                        int coordX = Math.Floor(coord.X).ClampToInt(0, width);
                        int coordY = Math.Floor(coord.Y).ClampToInt(0, height);

                        double depth = intersecmap[(coordX + 0) + (coordY + 0) * width].Distance;
                        int depthSamples = 1;
                        if (coordX < width - 1)
                        {
                            depth += intersecmap[(coordX + 1) + (coordY + 0) * width].Distance;
                            depthSamples++;
                        }
                        if (coordY < height - 1)
                        {
                            depth += intersecmap[(coordX + 0) + (coordY + 1) * width].Distance;
                            depthSamples++;
                        }
                        if (coordX < width - 1 && coordY < height - 1)
                        {
                            depth += intersecmap[(coordX + 1) + (coordY + 1) * width].Distance;
                            depthSamples++;
                        }
                        depth /= depthSamples;

                        Vector3 samplePos = camera.CalcPos(coord.X, coord.Y, depth);
                        Vector3 sampleDir = (samplePos - pos).Normalized;

                        double dot = Math.Max(Vector3.DotProduct(sampleDir, normal), 0);
                        double dist = (samplePos - pos).Length;

                        double a = 1.0 - Utility.SmoothStep(distanceThreshold, distanceThreshold * 2.0, dist);

                        ambientOcclusion += a * dot;
                    }

                    double ao = 255 * (1.0 - ambientOcclusion / poissonSamples.Length);
                    byte col = (byte)ao.ClampToInt(0, 255);
                    pixels[y * data.Stride + x * 3 + 0] = col;
                    pixels[y * data.Stride + x * 3 + 1] = col;
                    pixels[y * data.Stride + x * 3 + 2] = col;
                }
                ProgressReport.Report(1);
            });

            CopyAndUnLock(bmp, data, pixels);

            return bmp;
        }