Exemplo n.º 1
0
    public override Vector256 <float> Intersect(RayPacket256 rayPacket256)
    {
        var denom  = VectorPacket256.DotProduct(Norms, rayPacket256.Dirs);
        var dist   = Divide(Add(VectorPacket256.DotProduct(Norms, rayPacket256.Starts), Offsets), Subtract(Vector256 <float> .Zero, denom));
        var gtMask = Compare(denom, Vector256 <float> .Zero, FloatComparisonMode.OrderedGreaterThanNonSignaling);

        return(BlendVariable(dist, Intersections.NullDistance, gtMask));
    }
Exemplo n.º 2
0
    public override Vector256 <float> Intersect(RayPacket256 rayPacket256)
    {
        var eo               = Centers - rayPacket256.Starts;
        var v                = VectorPacket256.DotProduct(eo, rayPacket256.Dirs);
        var zero             = SetZeroVector256 <float>();
        var vLessZeroMask    = Compare(v, zero, FloatComparisonMode.LessThanOrderedNonSignaling);
        var discs            = Subtract(Multiply(Radiuses, Radiuses), Subtract(VectorPacket256.DotProduct(eo, eo), Multiply(v, v)));
        var discLessZeroMask = Compare(discs, zero, FloatComparisonMode.LessThanOrderedNonSignaling);
        var dists            = BlendVariable(Subtract(v, Sqrt(discs)), zero, Or(vLessZeroMask, discLessZeroMask));
        var isZero           = Compare(dists, zero, FloatComparisonMode.EqualOrderedNonSignaling);

        return(BlendVariable(dists, Intersections.NullDistance, isZero));
    }
Exemplo n.º 3
0
    private ColorPacket256 Shade(Intersections isect, RayPacket256 rays, Scene scene, int depth)
    {
        var ds          = rays.Dirs;
        var pos         = isect.Distances * ds + rays.Starts;
        var normals     = scene.Normals(isect.ThingIndices, pos);
        var reflectDirs = ds - (Multiply(VectorPacket256.DotProduct(normals, ds), Vector256.Create(2f)) * normals);
        var colors      = GetNaturalColor(isect.ThingIndices, pos, normals, reflectDirs, scene);

        if (depth >= MaxDepth)
        {
            return(colors + new ColorPacket256(.5f, .5f, .5f));
        }

        return(colors + GetReflectionColor(isect.ThingIndices, pos + (Vector256.Create(0.001f) * reflectDirs), normals, reflectDirs, scene, depth));
    }
Exemplo n.º 4
0
    private ColorPacket256 GetNaturalColor(Vector256 <int> things, VectorPacket256 pos, VectorPacket256 norms, VectorPacket256 rds, Scene scene)
    {
        var colors = ColorPacket256Helper.DefaultColor;

        for (int i = 0; i < scene.Lights.Length; i++)
        {
            var lights       = scene.Lights[i];
            var zero         = Vector256 <float> .Zero;
            var colorPacket  = lights.Colors;
            var ldis         = lights.Positions - pos;
            var livec        = ldis.Normalize();
            var neatIsectDis = TestRay(new RayPacket256(pos, livec), scene);

            // is in shadow?
            var mask1      = Compare(neatIsectDis, ldis.Lengths, FloatComparisonMode.LessThanOrEqualOrderedNonSignaling);
            var mask2      = Compare(neatIsectDis, zero, FloatComparisonMode.NotEqualOrderedNonSignaling);
            var isInShadow = And(mask1, mask2);

            Vector256 <float> illum = VectorPacket256.DotProduct(livec, norms);
            Vector256 <float> illumGraterThanZero = Compare(illum, zero, FloatComparisonMode.GreaterThanOrderedNonSignaling);
            var tmpColor1             = illum * colorPacket;
            var defaultRGB            = zero;
            Vector256 <float> lcolorR = BlendVariable(defaultRGB, tmpColor1.Xs, illumGraterThanZero);
            Vector256 <float> lcolorG = BlendVariable(defaultRGB, tmpColor1.Ys, illumGraterThanZero);
            Vector256 <float> lcolorB = BlendVariable(defaultRGB, tmpColor1.Zs, illumGraterThanZero);
            ColorPacket256    lcolor  = new ColorPacket256(lcolorR, lcolorG, lcolorB);

            Vector256 <float> specular = VectorPacket256.DotProduct(livec, rds.Normalize());
            Vector256 <float> specularGraterThanZero = Compare(specular, zero, FloatComparisonMode.GreaterThanOrderedNonSignaling);

            var difColor  = new ColorPacket256(1, 1, 1);
            var splColor  = new ColorPacket256(1, 1, 1);
            var roughness = Vector256.Create(1.0f);

            for (int j = 0; j < scene.Things.Length; j++)
            {
                Vector256 <float> thingMask = CompareEqual(things, Vector256.Create(j)).AsSingle();
                var rgh = Vector256.Create(scene.Things[j].Surface.Roughness);
                var dif = scene.Things[j].Surface.Diffuse(pos);
                var spl = scene.Things[j].Surface.Specular;

                roughness = BlendVariable(roughness, rgh, thingMask);

                difColor.Xs = BlendVariable(difColor.Xs, dif.Xs, thingMask);
                difColor.Ys = BlendVariable(difColor.Ys, dif.Ys, thingMask);
                difColor.Zs = BlendVariable(difColor.Zs, dif.Zs, thingMask);

                splColor.Xs = BlendVariable(splColor.Xs, spl.Xs, thingMask);
                splColor.Ys = BlendVariable(splColor.Ys, spl.Ys, thingMask);
                splColor.Zs = BlendVariable(splColor.Zs, spl.Zs, thingMask);
            }

            var tmpColor2             = VectorMath.Pow(specular, roughness) * colorPacket;
            Vector256 <float> scolorR = BlendVariable(defaultRGB, tmpColor2.Xs, specularGraterThanZero);
            Vector256 <float> scolorG = BlendVariable(defaultRGB, tmpColor2.Ys, specularGraterThanZero);
            Vector256 <float> scolorB = BlendVariable(defaultRGB, tmpColor2.Zs, specularGraterThanZero);
            ColorPacket256    scolor  = new ColorPacket256(scolorR, scolorG, scolorB);

            var oldColor = colors;

            colors = colors + ColorPacket256Helper.Times(difColor, lcolor) + ColorPacket256Helper.Times(splColor, scolor);

            colors = new ColorPacket256(BlendVariable(colors.Xs, oldColor.Xs, isInShadow), BlendVariable(colors.Ys, oldColor.Ys, isInShadow), BlendVariable(colors.Zs, oldColor.Zs, isInShadow));
        }
        return(colors);
    }