Пример #1
0
 public Matrix4(Vec4 newX, Vec4 newY, Vec4 newZ, Vec4 newW)
 {
     x = newX;
     y = newY;
     z = newZ;
     w = newW;
 }
Пример #2
0
 public Matrix4(float scalar)
 {
     x = new Vec4(scalar, 0, 0, 0);
     y = new Vec4(0, scalar, 0, 0);
     z = new Vec4(0, 0, scalar, 0);
     w = new Vec4(0, 0, 0, scalar);
 }
Пример #3
0
        public void Invert()
        {
            float determinant = 1 / this.Determinant();

            float m00 = (this.y.y * this.z.z * this.w.w + this.y.z * this.z.w * this.w.y + this.y.w * this.z.y * this.w.z -
                         this.y.y * this.z.w * this.w.z - this.y.z * this.z.y * this.w.w - this.y.w * this.z.z * this.w.y) * determinant;
            float m01 = (this.x.y * this.z.w * this.w.z + this.x.z * this.z.y * this.w.w + this.x.w * this.z.z * this.w.y -
                         this.x.y * this.z.z * this.w.w - this.x.z * this.z.w * this.w.y - this.x.w * this.z.y * this.w.z) * determinant;
            float m02 = (this.x.y * this.y.z * this.w.w + this.x.z * this.y.w * this.w.y + this.x.w * this.y.y * this.w.z -
                         this.x.y * this.y.w * this.w.z - this.x.z * this.y.y * this.w.w - this.x.w * this.y.z * this.w.y) * determinant;
            float m03 = (this.x.y * this.y.w * this.z.z + this.x.z * this.y.y * this.z.w + this.x.w * this.y.z * this.z.y -
                         this.x.y * this.y.z * this.z.w - this.x.z * this.y.w * this.z.y - this.x.w * this.y.y * this.z.z) * determinant;

            float m10 = (this.y.x * this.z.w * this.w.z + this.y.z * this.z.x * this.w.w + this.y.w * this.z.z * this.w.x -
                         this.y.x * this.z.z * this.w.w - this.y.z * this.z.w * this.w.x - this.y.w * this.z.x * this.w.z) * determinant;
            float m11 = (this.x.x * this.z.z * this.w.w + this.x.z * this.z.w * this.w.x + this.x.w * this.z.x * this.w.z -
                         this.x.x * this.z.w * this.w.z - this.x.z * this.z.x * this.w.w - this.x.w * this.z.z * this.w.x) * determinant;
            float m12 = (this.x.x * this.y.w * this.w.z + this.x.z * this.y.x * this.w.w + this.x.w * this.y.z * this.w.x -
                         this.x.x * this.y.z * this.w.w - this.x.z * this.y.w * this.w.x - this.x.w * this.y.x * this.w.z) * determinant;
            float m13 = (this.x.x * this.y.z * this.z.w + this.x.z * this.y.w * this.z.x + this.x.w * this.y.x * this.z.z -
                         this.x.x * this.y.w * this.z.z - this.x.z * this.y.x * this.z.w - this.x.w * this.y.z * this.z.x) * determinant;

            float m20 = (this.y.x * this.z.y * this.w.w + this.y.y * this.z.w * this.w.x + this.y.w * this.z.x * this.w.y -
                         this.y.x * this.z.w * this.w.y - this.y.y * this.z.x * this.w.w - this.y.w * this.z.y * this.w.x) * determinant;
            float m21 = (this.x.x * this.z.w * this.w.y + this.x.y * this.z.x * this.w.w + this.x.w * this.z.y * this.w.x -
                         this.x.x * this.z.y * this.w.w - this.x.y * this.z.w * this.w.x - this.x.w * this.z.x * this.w.y) * determinant;
            float m22 = (this.x.x * this.y.y * this.w.w + this.x.y * this.y.w * this.w.x + this.x.w * this.y.x * this.w.y -
                         this.x.x * this.y.w * this.w.y - this.x.y * this.y.x * this.w.w - this.x.w * this.y.y * this.w.x) * determinant;
            float m23 = (this.x.x * this.y.w * this.z.y + this.x.y * this.y.x * this.z.w + this.x.w * this.y.y * this.z.x -
                         this.x.x * this.y.y * this.z.w - this.x.y * this.y.w * this.z.x - this.x.w * this.y.x * this.z.y) * determinant;

            float m30 = (this.y.x * this.z.z * this.w.y + this.y.y * this.z.x * this.w.z + this.y.z * this.z.y * this.w.x -
                         this.y.x * this.z.y * this.w.z - this.y.y * this.z.z * this.w.x - this.y.z * this.z.x * this.w.y) * determinant;
            float m31 = (this.x.x * this.z.y * this.w.z + this.x.y * this.z.z * this.w.x + this.x.z * this.z.x * this.w.y -
                         this.x.x * this.z.z * this.w.y - this.x.y * this.z.x * this.w.z - this.x.z * this.z.y * this.w.x) * determinant;
            float m32 = (this.x.x * this.y.z * this.w.y + this.x.y * this.y.x * this.w.z + this.x.z * this.y.y * this.w.x -
                         this.x.x * this.y.y * this.w.z - this.x.y * this.y.z * this.w.x - this.x.z * this.y.x * this.w.y) * determinant;
            float m33 = (this.x.x * this.y.y * this.z.z + this.x.y * this.y.z * this.z.x + this.x.z * this.y.x * this.z.y -
                         this.x.x * this.y.z * this.z.y - this.x.y * this.y.x * this.z.z - this.x.z * this.y.y * this.z.x) * determinant;

            this.x.x = m00;
            this.x.y = m01;
            this.x.z = m02;
            this.x.w = m03;
            this.y.x = m10;
            this.y.y = m11;
            this.y.z = m12;
            this.y.w = m13;
            this.z.x = m20;
            this.z.y = m21;
            this.z.z = m22;
            this.z.w = m23;
            this.w.x = m30;
            this.w.y = m31;
            this.w.z = m32;
            this.w.w = m33;
        }
Пример #4
0
        public BarycentricTriangle(Point p00, Point p10, Point p01)
        {
            this.p00 = new Vec4(p00.X, p00.Y, 0);
            this.p10 = new Vec4(p10.X, p10.Y, 0);
            this.p01 = new Vec4(p01.X, p01.Y, 0);

            q1 = this.p10.subtract3(this.p00);
            q3 = this.p01.subtract3(this.p00);
        }
Пример #5
0
        public BarycentricTriangle(LatLon p00, LatLon p10, LatLon p01)
        {
            this.p00 = new Vec4(p00.getLongitude().getRadians(), p00.getLatitude().getRadians(), 0);
            this.p10 = new Vec4(p01.getLongitude().getRadians(), p01.getLatitude().getRadians(), 0);
            this.p01 = new Vec4(p10.getLongitude().getRadians(), p10.getLatitude().getRadians(), 0);

            q1 = this.p10.subtract3(this.p00);
            q3 = this.p01.subtract3(this.p00);
        }
Пример #6
0
        public void Vector4Test()
        {
            Vec4 vect = Vec4.Zero;

            vect.Add(new Vec4(1f, 1f, 2f, 2f)); //  1  1 2 2
            vect.Sub(new Vec4(3f, 2f, 1f, 2f)); // -2 -1 1 0

            Assert.AreEqual(vect, new Vec4(-2f, -1f, 1f, 0f));
        }
        public void render(DrawContext dc, Annotation annotation, Vec4 annotationPoint, Layer layer)
        {
            if (!isAnnotationValid(annotation, false))
            {
                return;
            }

            this.drawOne(dc, annotation, annotationPoint, layer);
        }
Пример #8
0
        public BarycentricTriangle(Vec4 p00, Vec4 p10, Vec4 p01)
        {
            this.p00 = p00;
            this.p10 = p10;
            this.p01 = p01;

            q1 = p10.subtract3(p00);
            q3 = p01.subtract3(p00);
        }
Пример #9
0
        /**
         * Determines if and where a ray intersects the geometry.
         *
         * @param line the <code>Line</code> for which an intersection is to be found.
         *
         * @return the <Vec4> point closest to the ray origin where an intersection has been found or null if no
         *         intersection was found.
         */
        public Intersection[] intersect(Line line)
        {
            if (line == null)
            {
                String msg = Logging.getMessage("nullValue.LineIsNull");
                Logging.logger().severe(msg);
                throw new ArgumentException(msg);
            }

            List <SectorGeometry> sglist = new List <SectorGeometry>(this);

            Intersection[]      hits;
            List <Intersection> list = new List <Intersection>();

            foreach (SectorGeometry sg in sglist)
            {
                if (sg.getExtent().intersects(line))
                {
                    if ((hits = sg.intersect(line)) != null)
                    {
                        list.AddRange(hits);
                    }
                }
            }

            int numHits = list.Count;

            if (numHits == 0)
            {
                return(null);
            }

            Vec4 origin = line.getOrigin();

            list.Sort((i1, i2) => {
                if (i1 == null && i2 == null)
                {
                    return(0);
                }
                if (i2 == null)
                {
                    return(-1);
                }
                if (i1 == null)
                {
                    return(1);
                }

                Vec4 v1   = i1.getIntersectionPoint();
                Vec4 v2   = i2.getIntersectionPoint();
                double d1 = origin.distanceTo3(v1);
                double d2 = origin.distanceTo3(v2);
                return(d1.CompareTo(d2));
            });

            return(list.ToArray());
        }
Пример #10
0
 public static void SetVec4(this Hashtable ht, string key, Vec4 v)
 {
     float[] al = new float[3];
     al[0]   = v.x;
     al[1]   = v.y;
     al[2]   = v.z;
     al[3]   = v.w;
     ht[key] = al;
 }
Пример #11
0
        public Position getCenterPosition()
        {
            Vec4 eyePoint = this.getEyePoint();

            Intersection[] intersection = this.globe.intersect(new Line(eyePoint, this.getForwardVector()), 0);
            Position       pos          = this.globe.computePositionFromPoint(intersection[0].getIntersectionPoint());

            return(pos);
        }
Пример #12
0
 public FluidData(float am, Vec4 col, float flam, string spr = "", float h = 0.0f, float trans = 0.7f)
 {
     this.amount      = am;
     this.color       = col;
     this.flammable   = flam;
     this.sprite      = spr;
     this.heat        = h;
     this.transparent = trans;
 }
Пример #13
0
        /**
         * Create a new globe, and set the position of the globe's center. The globe will be tessellated using tessellator
         * defined by the {@link AVKey#TESSELLATOR_CLASS_NAME} configuration parameter.
         *
         * @param equatorialRadius Radius of the globe at the equator.
         * @param polarRadius      Radius of the globe at the poles.
         * @param es               Square of the globe's eccentricity.
         * @param em               Elevation model. May be null.
         * @param center           Cartesian coordinates of the globe's center point.
         */

        public EllipsoidalGlobe(double equatorialRadius, double polarRadius, double es, ElevationModel em, Vec4 center)
        {
            this.equatorialRadius = equatorialRadius;
            this.polarRadius      = polarRadius;
            this.es              = es; // assume it's consistent with the two radii
            this.center          = center;
            this._elevationModel = em;
            this._tessellator    = (Tessellator)WorldWind.createConfigurationComponent(AVKey.TESSELLATOR_CLASS_NAME);
        }
Пример #14
0
 public void RotToPlaneTest()
 {
     E    x       = E.Num("x");
     E    y       = E.Num("y");
     E    z       = E.Num("z");
     Vec4 v       = new Vec4(x, y, z, E.Zero);
     Mat4 rotToXY = Mat4.RotToXY(v);
     Mat4 r       = rotToXY.Evaluate(new EvalSettings(false));
 }
Пример #15
0
 /// <summary>
 /// Read array of Vector4.
 /// </summary>
 /// <param name="count">Amount of Vector4s to read.</param>
 /// <returns>The read array of Vector4s.</returns>
 public Vec4[] ReadVec4s(int count)
 {
     Vec4[] data = new Vec4[count];
     for (int i = 0; i < count; i++)
     {
         data[i] = ReadVec4();
     }
     return(data);
 }
Пример #16
0
        public LatLon interpolate(double t, double s)
        {
            Vec4 top      = this.northEdge.getPointAt(s);
            Vec4 bot      = this.southEdge.getPointAt(s);
            Line topToBot = Line.fromSegment(bot, top);
            Vec4 point    = topToBot.getPointAt(t);

            return(LatLon.fromDegrees(point.y(), point.x()));
        }
        public void pick(DrawContext dc, Annotation annotation, Vec4 annotationPoint, java.awt.Point pickPoint, Layer layer)
        {
            if (!isAnnotationValid(annotation, false))
            {
                return;
            }

            this.drawOne(dc, annotation, annotationPoint, layer);
        }
Пример #18
0
        protected override void OnMaterialRender( uint passId, Material material, ref bool skipPass )
        {
            base.OnMaterialRender( passId, material, ref skipPass );

            if( passId == 700 || passId == 701 )
            {
                bool horizontal = passId == 700;

                Vec2[] sampleOffsets = new Vec2[ 15 ];
                Vec4[] sampleWeights = new Vec4[ 15 ];

                // calculate gaussian texture offsets & weights
                Vec2I textureSize = Owner.DimensionsInPixels.Size;
                float texelSize = 1.0f / (float)( horizontal ? textureSize.X : textureSize.Y );

                texelSize *= fuzziness;

                // central sample, no offset
                sampleOffsets[ 0 ] = Vec2.Zero;
                {
                    float distribution = GaussianDistribution( 0, 0, 3 );
                    sampleWeights[ 0 ] = new Vec4( distribution, distribution, distribution, 0 );
                }

                // 'pre' samples
                for( int n = 1; n < 8; n++ )
                {
                    float distribution = GaussianDistribution( n, 0, 3 );
                    sampleWeights[ n ] = new Vec4( distribution, distribution, distribution, 1 );

                    if( horizontal )
                        sampleOffsets[ n ] = new Vec2( (float)n * texelSize, 0 );
                    else
                        sampleOffsets[ n ] = new Vec2( 0, (float)n * texelSize );
                }
                // 'post' samples
                for( int n = 8; n < 15; n++ )
                {
                    sampleWeights[ n ] = sampleWeights[ n - 7 ];
                    sampleOffsets[ n ] = -sampleOffsets[ n - 7 ];
                }

                //convert to Vec4 array
                Vec4[] vec4Offsets = new Vec4[ 15 ];
                for( int n = 0; n < 15; n++ )
                {
                    Vec2 offset = sampleOffsets[ n ];
                    vec4Offsets[ n ] = new Vec4( offset.X, offset.Y, 0, 0 );
                }

                GpuProgramParameters parameters = material.GetBestTechnique().
                    Passes[ 0 ].FragmentProgramParameters;
                parameters.SetNamedConstant( "sampleOffsets", vec4Offsets );
                parameters.SetNamedConstant( "sampleWeights", sampleWeights );
            }
        }
Пример #19
0
        public static Vec4 NormalizeSafe(Vec4 v)
        {
            float dis = MathUtils.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);

            if (dis == 0f)
            {
                return(new Vec4());
            }
            return(v * (1 / dis));
        }
Пример #20
0
        protected Position computeEyePositionFromModelview()
        {
            if (this.globe != null)
            {
                Vec4 eyePoint = Vec4.UNIT_W.transformBy4(this.modelviewInv);
                return(this.globe.computePositionFromPoint(eyePoint));
            }

            return(Position.ZERO);
        }
Пример #21
0
        public static Vec4 PxQuatToVec4(Quaternion pxquat)
        {
            Vec4 vec4 = new Vec4();

            vec4.X = pxquat.x;
            vec4.Y = pxquat.y;
            vec4.Z = pxquat.z;
            vec4.W = pxquat.w;
            return(vec4);
        }
Пример #22
0
//==========================================================

    public Vec4 readVec4(BinaryReader br)
    {
        Vec4 v = new Vec4();

        v.X = br.ReadSingle();
        v.Y = br.ReadSingle();
        v.Z = br.ReadSingle();
        v.W = br.ReadSingle();
        return(v);
    }
Пример #23
0
        protected override void OnUpdate(double deltaTime)
        {
            var time = clock.ElapsedTime.AsSeconds();

            rect.Position = position;
            Vec4[] iMouse = new Vec4[] { new Vec4(-parallax.X, parallax.Y, 0, 0) };

            shader.SetUniform("iTime", time);
            shader.SetUniformArray("iMouse", iMouse);
        }
Пример #24
0
 public Vec4 Transform(Vec4 v)
 {
     return(new Vec4
            (
                v.x * this.x.x + v.y * this.y.x + v.z * this.z.x + v.w * this.w.x,
                v.x * this.x.y + v.y * this.y.y + v.z * this.z.y + v.w * this.w.y,
                v.x * this.x.z + v.y * this.y.z + v.z * this.z.z + v.w * this.w.z,
                v.x * this.x.w + v.y * this.y.w + v.z * this.z.w + v.w * this.w.w
            ));
 }
Пример #25
0
            public Vec4 Blur5(Sampler2D image, Vec2 uv, Vec2 resolution, Vec2 direction)
            {
                var color = new Vec4(0.0f, 0.0f, 0.0f, 0.0f);
                var off1  = new Vec2(1.3333333333333333f) * direction;

                color += image.Sample(uv) * 0.29411764705882354f;
                color += image.Sample(uv + (off1 / resolution)) * 0.35294117647058826f;
                color += image.Sample(uv - (off1 / resolution)) * 0.35294117647058826f;
                return(color);
            }
Пример #26
0
        public static Quaternion Vec4ToPxQuat(Vec4 vec4)
        {
            Quaternion pxquat = new Quaternion();

            pxquat.x = vec4.X;
            pxquat.y = vec4.Y;
            pxquat.z = vec4.Z;
            pxquat.w = vec4.W;
            return(pxquat);
        }
Пример #27
0
        /**
         * Return this cylinder's center point.
         *
         * @return this cylinder's center point.
         */
        public Vec4 getCenter()
        {
            Vec4 b = this.bottomCenter;
            Vec4 t = this.topCenter;

            return(new Vec4(
                       (b.x() + t.x()) / 2.0,
                       (b.y() + t.y()) / 2.0,
                       (b.z() + t.z()) / 2.0));
        }
Пример #28
0
        public Material(System.IO.BinaryReader br)
        {
            m_ambientColor = new Vec4(br);
            m_diffuseColor = new Vec4(br);
            m_name         = br.ReadString();
            bool isOpacityMapNull = br.ReadBoolean();

            if (isOpacityMapNull)
            {
                this.m_opacityMap = null;
            }
            else
            {
                this.m_opacityMap = MaterialMap.Load(br);
            }

            this.m_shininess     = (float)br.ReadDouble();
            this.m_specularColor = new Vec4(br);
            int count = br.ReadInt32();

            if (count > 0)
            {
                for (int i = 0; i < count; ++i)
                {
                    AddMaterialMap(MaterialMap.Load(br));
                }
            }

            this.Transparency = (float)br.ReadDouble();
            this.m_twoSided   = br.ReadBoolean();
            this.m_scale      = (float)br.ReadDouble();
            if (this.m_scale != 1)
            {
                this.hasTexTransform = true;
            }
            if (br.ReadBoolean())
            {
                remapUV    = new Vec2[2];
                remapUV[0] = Vec2.Load(br);
                remapUV[1] = Vec2.Load(br);
            }
            else
            {
                remapUV = null;
            }
            if (br.PeekChar() == '~')
            {
                br.ReadChar();
                count = br.ReadInt32();
                for (int i = 0; i < count; ++i)
                {
                    AddShader(Shader.Load(br));
                }
            }
        }
Пример #29
0
        protected Intersection[] intersect(Line line, double equRadius, double polRadius)
        {
            if (line == null)
            {
                return(null);
            }

            // Taken from Lengyel, 2Ed., Section 5.2.3, page 148.

            double m  = equRadius / polRadius; // "ratio of the x semi-axis length to the y semi-axis length"
            double n  = 1d;                    // "ratio of the x semi-axis length to the z semi-axis length"
            double m2 = m * m;
            double n2 = n * n;
            double r2 = equRadius * equRadius; // nominal radius squared //equRadius * polRadius;

            double vx = line.getDirection().getX();
            double vy = line.getDirection().getY();
            double vz = line.getDirection().getZ();
            double sx = line.getOrigin().getX();
            double sy = line.getOrigin().getY();
            double sz = line.getOrigin().getZ();

            double a = vx * vx + m2 * vy * vy + n2 * vz * vz;
            double b = 2d * (sx * vx + m2 * sy * vy + n2 * sz * vz);
            double c = sx * sx + m2 * sy * sy + n2 * sz * sz - r2;

            double discriminant = Discriminant(a, b, c);

            if (discriminant < 0)
            {
                return(null);
            }

            double discriminantRoot = Math.Sqrt(discriminant);

            if (discriminant == 0)
            {
                Vec4 p = line.getPointAt((-b - discriminantRoot) / (2 * a));
                return(new Intersection[] { new Intersection(p, true) });
            }
            else // (discriminant > 0)
            {
                Vec4 near = line.getPointAt((-b - discriminantRoot) / (2 * a));
                Vec4 far  = line.getPointAt((-b + discriminantRoot) / (2 * a));
                if (c >= 0) // Line originates outside the Globe.
                {
                    return new Intersection[] { new Intersection(near, false), new Intersection(far, false) }
                }
                ;
                else // Line originates inside the Globe.
                {
                    return new Intersection[] { new Intersection(far, false) }
                };
            }
        }
Пример #30
0
        protected void drawOutline(DrawContext dc, Vec4 a, Vec4 b, Vec4 c, Vec4 d)
        {
            GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility.

            gl.glBegin(GL2.GL_LINE_LOOP);
            gl.glVertex3d(a.x, a.y, a.z);
            gl.glVertex3d(b.x, b.y, b.z);
            gl.glVertex3d(c.x, c.y, c.z);
            gl.glVertex3d(d.x, d.y, d.z);
            gl.glEnd();
        }
Пример #31
0
        public Position computePositionFromPoint(Vec4 point)
        {
            if (point == null)
            {
                string message = Logging.getMessage("nullValue.PointIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }

            return(cartesianToGeodetic(point));
        }
Пример #32
0
        public Position computePositionFromEllipsoidalPoint(Vec4 ellipsoidalPoint)
        {
            if (ellipsoidalPoint == null)
            {
                string message = Logging.getMessage("nullValue.PointIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }

            return(this.ellipsoidalToGeodetic(ellipsoidalPoint));
        }
Пример #33
0
        // overload operator *
        public static Vec4 operator *(Mat4 l, Vec4 r)
        {
            Vec4 Vr = new Vec4();

            for (int i = 0; i < Size; i++)
            {
                Vr[i] = (new Vec4(l.cols[0][i], l.cols[1][i], l.cols[2][i], l.cols[3][i]) * r).SumXYZW();
            }

            return(Vr);
        }
        protected override void OnMaterialRender( uint passId, Material material, ref bool skipPass )
        {
            base.OnMaterialRender( passId, material, ref skipPass );

            if( passId == 500 )
            {
                Vec4 multiplier = new Vec4( Red, Green, Blue, 1 );

                GpuProgramParameters parameters = material.Techniques[ 0 ].Passes[ 0 ].FragmentProgramParameters;
                parameters.SetNamedConstant( "multiplier", multiplier );
            }
        }
Пример #35
0
        public static VertexBufferResource Create(int n)
        {
            var color = new Vec4(0.5f, 0.5f, 0.5f, 1.0f);
            var vertices = new List<Vertex>();

            var minX = -n;
            var maxX = n;
            for (int z = -n; z <= n; ++z)
            {
                vertices.Add(new Vertex
                {
                    Position = new Vec4(minX, 0, z, 1.0f),
                    Color = color,
                });
                vertices.Add(new Vertex
                {
                    Position = new Vec4(maxX, 0, z, 1.0f),
                    Color = color,
                });
            }

            var minZ = -n;
            var maxZ = n;
            for (int x = -n; x <= n; ++x)
            {
                vertices.Add(new Vertex
                {
                    Position = new Vec4(x, 0, minZ, 1.0f),
                    Color = color,
                });
                vertices.Add(new Vertex
                {
                    Position = new Vec4(x, 0, maxZ, 1.0f),
                    Color = color,
                });
            }
            var grid = VertexBufferResource.Create(vertices.Select(x => x.ToArray()));

            grid.Topology = VertexBufferTopology.Lines;
            return grid;
        }
        protected override void OnMaterialRender(uint passId, Material material, ref bool skipPass)
        {
            base.OnMaterialRender(passId, material, ref skipPass);

            //update material scheme
            {
                string materialScheme = RendererWorld.Instance.DefaultViewport.MaterialScheme;

                foreach (CompositionTechnique technique in Compositor.Techniques)
                {
                    foreach (CompositionTargetPass pass in technique.TargetPasses)
                        pass.MaterialScheme = materialScheme;
                    if (technique.OutputTargetPass != null)
                        technique.OutputTargetPass.MaterialScheme = materialScheme;
                }
            }

            const int rt_luminance0 = 994;
            const int rt_luminance1 = 993;
            const int rt_luminance2 = 992;
            const int rt_luminance3 = 991;
            const int rt_luminance4 = 990;
            const int rt_brightPass = 800;
            const int rt_bloomBlur = 700;
            const int rt_bloomHorizontal = 701;
            const int rt_bloomVertical = 702;
            const int rt_adaptedLuminance = 500;
            const int rt_targetOutput = 600;

            //Skip adaptation passes if adaptation switched off.
            if (!Adaptation)
            {
                if (passId == rt_luminance0 || passId == rt_luminance1 || passId == rt_luminance2
                    || passId == rt_luminance3 || passId == rt_luminance4)
                {
                    skipPass = true;
                    return;
                }
            }

            //Skip bloom passes if bloom switched off
            if (BloomScale == 0)
            {
                if (passId == rt_brightPass || passId == rt_bloomBlur || passId == rt_bloomHorizontal ||
                    passId == rt_bloomVertical)
                {
                    skipPass = true;
                    return;
                }
            }

            // Prepare the fragment params offsets
            switch (passId)
            {
                case rt_luminance0:
                    {
                        Vec2[] sampleOffsets = new Vec2[9];

                        // Initialize the sample offsets for the initial luminance pass.
                        int textureSize = Technique.GetTextureDefinition("rt_luminance0").Size.X;

                        float tu = 1.0f / (3.0f * textureSize);

                        int index = 0;
                        for (int x = -1; x <= 1; x++)
                        {
                            for (int y = -1; y <= 1; y++)
                            {
                                sampleOffsets[index] = new Vec2(x, y) * tu;
                                index++;
                            }
                        }

                        GpuProgramParameters parameters = material.GetBestTechnique().
                            Passes[0].FragmentProgramParameters;

                        //convert to Vec4 array
                        Vec4[] vec4Offsets = new Vec4[9];
                        for (int n = 0; n < 9; n++)
                        {
                            Vec2 offset = sampleOffsets[n];
                            vec4Offsets[n] = new Vec4(offset[0], offset[1], 0, 0);
                        }

                        parameters.SetNamedConstant("sampleOffsets", vec4Offsets);
                    }
                    break;

                case rt_luminance1:
                case rt_luminance2:
                case rt_luminance3:
                case rt_luminance4:
                    {
                        Vec2[] sampleOffsets = new Vec2[16];

                        string textureSizeFrom = null;
                        switch (passId)
                        {
                            case rt_luminance1: textureSizeFrom = "rt_luminance0"; break;
                            case rt_luminance2: textureSizeFrom = "rt_luminance1"; break;
                            case rt_luminance3: textureSizeFrom = "rt_luminance2"; break;
                            case rt_luminance4: textureSizeFrom = "rt_luminance3"; break;
                            default: Trace.Assert(false); break;
                        }
                        Vec2I textureSize = Technique.GetTextureDefinition(textureSizeFrom).Size;
                        CalculateDownScale4x4SampleOffsets(textureSize, sampleOffsets);

                        //convert to Vec4 array
                        Vec4[] vec4Offsets = new Vec4[16];
                        for (int n = 0; n < 16; n++)
                        {
                            Vec2 offset = sampleOffsets[n];
                            vec4Offsets[n] = new Vec4(offset[0], offset[1], 0, 0);
                        }

                        GpuProgramParameters parameters = material.GetBestTechnique().
                            Passes[0].FragmentProgramParameters;
                        parameters.SetNamedConstant("sampleOffsets", vec4Offsets);
                    }
                    break;

                //BrightPass
                case rt_brightPass:
                    {
                        Vec2[] sampleOffsets = new Vec2[16];

                        Vec2I textureSize = Owner.DimensionsInPixels.Size;
                        CalculateDownScale4x4SampleOffsets(textureSize, sampleOffsets);

                        //convert to Vec4 array
                        Vec4[] vec4Offsets = new Vec4[16];
                        for (int n = 0; n < 16; n++)
                        {
                            Vec2 offset = sampleOffsets[n];
                            vec4Offsets[n] = new Vec4(offset[0], offset[1], 0, 0);
                        }

                        GpuProgramParameters parameters = material.GetBestTechnique().
                            Passes[0].FragmentProgramParameters;
                        parameters.SetNamedConstant("brightThreshold", BloomBrightThreshold);
                        parameters.SetNamedConstant("sampleOffsets", vec4Offsets);
                    }
                    break;

                case rt_bloomBlur:
                    {
                        Vec2[] sampleOffsets = new Vec2[13];
                        Vec4[] sampleWeights = new Vec4[13];

                        Vec2I textureSize = brightPassTextureSize;
                        CalculateGaussianBlur5x5SampleOffsets(textureSize, sampleOffsets, sampleWeights, 1);

                        //convert to Vec4 array
                        Vec4[] vec4Offsets = new Vec4[13];
                        for (int n = 0; n < 13; n++)
                        {
                            Vec2 offset = sampleOffsets[n];
                            vec4Offsets[n] = new Vec4(offset.X, offset.Y, 0, 0);
                        }

                        GpuProgramParameters parameters = material.GetBestTechnique().
                            Passes[0].FragmentProgramParameters;
                        parameters.SetNamedConstant("sampleOffsets", vec4Offsets);
                        parameters.SetNamedConstant("sampleWeights", sampleWeights);
                    }
                    break;

                case rt_bloomHorizontal:
                case rt_bloomVertical:
                    {
                        // horizontal and vertical bloom
                        bool horizontal = passId == rt_bloomHorizontal;

                        float[] sampleOffsets = new float[15];
                        Vec4[] sampleWeights = new Vec4[15];

                        Vec2I textureSize = bloomTextureSize;
                        CalculateBloomSampleOffsets(horizontal ? textureSize.X : textureSize.Y,
                            sampleOffsets, sampleWeights, 3, 2);

                        //convert to Vec4 array
                        Vec4[] vec4Offsets = new Vec4[15];
                        for (int n = 0; n < 15; n++)
                        {
                            float offset = sampleOffsets[n];

                            if (horizontal)
                                vec4Offsets[n] = new Vec4(offset, 0, 0, 0);
                            else
                                vec4Offsets[n] = new Vec4(0, offset, 0, 0);
                        }

                        GpuProgramParameters parameters = material.GetBestTechnique().
                            Passes[0].FragmentProgramParameters;
                        parameters.SetNamedConstant("sampleOffsets", vec4Offsets);
                        parameters.SetNamedConstant("sampleWeights", sampleWeights);
                    }
                    break;

                case rt_adaptedLuminance:
                    {
                        float elapsedTime;
                        {
                            float currentTime = RendererWorld.Instance.FrameRenderTime;
                            if (lastLuminanceCalculationTime != -1)
                                elapsedTime = currentTime - lastLuminanceCalculationTime;
                            else
                                elapsedTime = 1000;
                            lastLuminanceCalculationTime = currentTime;
                        }

                        GpuProgramParameters parameters = material.GetBestTechnique().
                            Passes[0].FragmentProgramParameters;
                        parameters.SetNamedConstant("adaptationMinimum", Adaptation ? AdaptationMinimum : 1);
                        parameters.SetNamedConstant("adaptationMaximum", Adaptation ? AdaptationMaximum : 1);
                        parameters.SetNamedConstant("adaptationVelocity", AdaptationVelocity);
                        parameters.SetNamedConstant("elapsedTime", elapsedTime);
                    }
                    break;

                //Final pass
                case rt_targetOutput:
                    {
                        GpuProgramParameters parameters = material.GetBestTechnique().
                            Passes[0].FragmentProgramParameters;
                        parameters.SetNamedConstant("adaptationMiddleBrightness",
                            Adaptation ? AdaptationMiddleBrightness : 1);
                        parameters.SetNamedConstant("bloomScale", BloomScale);
                    }
                    break;
            }
        }
Пример #37
0
 public static float Sqr(Vec4 v)
 {
     return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
 }
        void SetCustomGpuParameter( GpuParameters parameter, Vec4 value )
        {
            for( int nMaterial = 0; nMaterial < 2; nMaterial++ )
            {
                Material material;
                if( nMaterial == 0 )
                {
                    material = BaseMaterial;
                }
                else
                {
                    material = BaseMaterial.ShadowTextureCasterMaterial;
                    if( material == null )
                        continue;
                }

                foreach( Technique technique in material.Techniques )
                {
                    foreach( Pass pass in technique.Passes )
                    {
                        if( pass.VertexProgramParameters != null ||
                            pass.FragmentProgramParameters != null )
                        {
                            if( pass.VertexProgramParameters != null )
                            {
                                if( !pass.IsCustomGpuParameterInitialized( (int)parameter ) )
                                {
                                    pass.VertexProgramParameters.SetNamedAutoConstant( parameter.ToString(),
                                        GpuProgramParameters.AutoConstantType.Custom, (int)parameter );
                                }
                            }

                            if( pass.FragmentProgramParameters != null )
                            {
                                if( !pass.IsCustomGpuParameterInitialized( (int)parameter ) )
                                {
                                    pass.FragmentProgramParameters.SetNamedAutoConstant( parameter.ToString(),
                                        GpuProgramParameters.AutoConstantType.Custom, (int)parameter );
                                }
                            }

                            pass.SetCustomGpuParameter( (int)parameter, value );
                        }
                    }
                }
            }
        }
Пример #39
0
        void TrackMeshObject_AddToRenderQueue( MovableObject sender,
            Camera camera, bool onlyShadowCasters, ref bool allowRender)
        {
            //tracks animation

            if( camera != RendererWorld.Instance.DefaultCamera )
                return;
            if( leftTrack.meshObject == null )
                return;

            //!!!!!need speed for each track
            float speed = GetTracksSpeed();

            Track track = leftTrack.meshObject == sender ? leftTrack : rightTrack;

            Vec2 value = track.materialScrollValue + Type.TracksAnimationMultiplier *
                ( speed * RendererWorld.Instance.FrameRenderTimeStep );

            while( value.X < 0 ) value.X++;
            while( value.X >= 1 ) value.X--;
            while( value.Y < 0 ) value.Y++;
            while( value.Y >= 1 ) value.Y--;

            track.materialScrollValue = value;

            Vec4 programValue;
            if( EntitySystemWorld.Instance.Simulation )
                programValue = new Vec4( track.materialScrollValue.X, track.materialScrollValue.Y, 0, 0 );
            else
                programValue = Vec4.Zero;

            foreach( MeshObject.SubObject subObject in track.meshObject.SubObjects )
            {
                //update SubObject dynamic gpu parameter
                subObject.SetCustomGpuParameter(
                    (int)ShaderBaseMaterial.GpuParameters.diffuse1MapTransformAdd, programValue );
            }
        }
        protected override void OnMaterialRender( uint passId, Material material, ref bool skipPass )
        {
            base.OnMaterialRender( passId, material, ref skipPass );

            //update material scheme
            {
                string materialScheme = RendererWorld.Instance.DefaultViewport.MaterialScheme;

                foreach( CompositionTechnique technique in Compositor.Techniques )
                {
                    foreach( CompositionTargetPass pass in technique.TargetPasses )
                        pass.MaterialScheme = materialScheme;
                    if( technique.OutputTargetPass != null )
                        technique.OutputTargetPass.MaterialScheme = materialScheme;
                }
            }

            const int rt_brightPass = 800;
            const int rt_bloomBlur = 700;
            const int rt_bloomHorizontal = 701;
            const int rt_bloomVertical = 702;
            const int rt_targetOutput = 600;

            //Skip bloom passes if bloom switched off
            if( BloomScale == 0 )
            {
                if( passId == rt_brightPass || passId == rt_bloomBlur || passId == rt_bloomHorizontal ||
                    passId == rt_bloomVertical )
                {
                    skipPass = true;
                    return;
                }
            }

            // Prepare the fragment params offsets
            switch( passId )
            {
            //BrightPass
            case rt_brightPass:
                {
                    Vec2[] sampleOffsets = new Vec2[ 16 ];

                    Vec2i textureSize = Owner.DimensionsInPixels.Size;
                    CalculateDownScale4x4SampleOffsets( textureSize, sampleOffsets );

                    //convert to Vec4 array
                    Vec4[] vec4Offsets = new Vec4[ 16 ];
                    for( int n = 0; n < 16; n++ )
                    {
                        Vec2 offset = sampleOffsets[ n ];
                        vec4Offsets[ n ] = new Vec4( offset[ 0 ], offset[ 1 ], 0, 0 );
                    }

                    GpuProgramParameters parameters = material.GetBestTechnique().
                        Passes[ 0 ].FragmentProgramParameters;
                    parameters.SetNamedConstant( "brightThreshold", BloomBrightThreshold );
                    parameters.SetNamedConstant( "sampleOffsets", vec4Offsets );
                }
                break;

            case rt_bloomBlur:
                {
                    Vec2[] sampleOffsets = new Vec2[ 13 ];
                    Vec4[] sampleWeights = new Vec4[ 13 ];

                    Vec2i textureSize = brightPassTextureSize;
                    CalculateGaussianBlur5x5SampleOffsets( textureSize, sampleOffsets, sampleWeights, 1 );

                    //convert to Vec4 array
                    Vec4[] vec4Offsets = new Vec4[ 13 ];
                    for( int n = 0; n < 13; n++ )
                    {
                        Vec2 offset = sampleOffsets[ n ];
                        vec4Offsets[ n ] = new Vec4( offset.X, offset.Y, 0, 0 );
                    }

                    GpuProgramParameters parameters = material.GetBestTechnique().
                        Passes[ 0 ].FragmentProgramParameters;
                    parameters.SetNamedConstant( "sampleOffsets", vec4Offsets );
                    parameters.SetNamedConstant( "sampleWeights", sampleWeights );
                }
                break;

            case rt_bloomHorizontal:
            case rt_bloomVertical:
                {
                    // horizontal and vertical bloom
                    bool horizontal = passId == rt_bloomHorizontal;

                    float[] sampleOffsets = new float[ 15 ];
                    Vec4[] sampleWeights = new Vec4[ 15 ];

                    Vec2i textureSize = bloomTextureSize;
                    CalculateBloomSampleOffsets( horizontal ? textureSize.X : textureSize.Y,
                        sampleOffsets, sampleWeights, 3, 2 );

                    //convert to Vec4 array
                    Vec4[] vec4Offsets = new Vec4[ 15 ];
                    for( int n = 0; n < 15; n++ )
                    {
                        float offset = sampleOffsets[ n ];

                        if( horizontal )
                            vec4Offsets[ n ] = new Vec4( offset, 0, 0, 0 );
                        else
                            vec4Offsets[ n ] = new Vec4( 0, offset, 0, 0 );
                    }

                    GpuProgramParameters parameters = material.GetBestTechnique().
                        Passes[ 0 ].FragmentProgramParameters;
                    parameters.SetNamedConstant( "sampleOffsets", vec4Offsets );
                    parameters.SetNamedConstant( "sampleWeights", sampleWeights );
                }
                break;

            //Final pass
            case rt_targetOutput:
                {
                    GpuProgramParameters parameters = material.GetBestTechnique().
                        Passes[ 0 ].FragmentProgramParameters;
                    parameters.SetNamedConstant( "bloomScale", BloomScale );
                }
                break;
            }
        }
Пример #41
0
        void UpdateReflectionBoxParallaxCorrectedCubemapsGpuParameters( Vec4 cubemapPosition, Vec3 rotationScale0,
			Vec3 rotationScale1, Vec3 rotationScale2, Vec3 position )
        {
            if( reflectionBoxParallaxCorrectedCubemaps )
            {
                if( ( ReflectionColor != new ColorValue( 0, 0, 0 ) && ReflectionPower != 0 ) || reflectionScaleDynamic )
                {
                    SetCustomGpuParameter( GpuParameters.reflectionParallax_cubemapPosition, cubemapPosition, true, true, false );
                    SetCustomGpuParameter( GpuParameters.reflectionParallax_rotationScale0, new Vec4( rotationScale0, 0 ), true, true, false );
                    SetCustomGpuParameter( GpuParameters.reflectionParallax_rotationScale1, new Vec4( rotationScale1, 0 ), true, true, false );
                    SetCustomGpuParameter( GpuParameters.reflectionParallax_rotationScale2, new Vec4( rotationScale2, 0 ), true, true, false );
                    SetCustomGpuParameter( GpuParameters.reflectionParallax_position, new Vec4( position, 0 ), true, true, false );
                }
            }
        }
Пример #42
0
 public static float Dot(Vec4 u, Vec4 v)
 {
     return u.x * v.x + u.y * v.y + u.z * v.z + u.w * v.w;
 }
        static void CalculateBloomSampleOffsets( int textureSize, float[] sampleOffsets,
			Vec4[] sampleWeights, float deviation, float multiplier )
        {
            int n;
            float weight;
            float tu = 1.0f / (float)textureSize;

            // Fill the center texel
            weight = multiplier * GaussianDistribution( 0, 0, deviation );
            sampleOffsets[ 0 ] = 0.0f;
            sampleWeights[ 0 ] = new Vec4( weight, weight, weight, 1.0f );

            // Fill the first half
            for( n = 1; n < 8; n++ )
            {
                // Get the Gaussian intensity for this offset
                weight = multiplier * GaussianDistribution( n, 0, deviation );
                sampleOffsets[ n ] = n * tu;
                sampleWeights[ n ] = new Vec4( weight, weight, weight, 1.0f );
            }

            // Mirror to the second half
            for( n = 8; n < 15; n++ )
            {
                sampleOffsets[ n ] = -sampleOffsets[ n - 7 ];
                sampleWeights[ n ] = sampleWeights[ n - 7 ];
            }
        }
 void init()
 {
     VertexConstatints = new List<VertexConstraint> ();
     VertexOld = new List<Vec3> ();
     Force = new Vec4 (0, 0, 0, 1);
     Mass = 0;
     foreach (var v in VertexNow)
         VertexOld.Add (v * 1);
     GenerateVertexConstraintsFromFaces ();
 }
Пример #45
0
        void SetCustomGpuParameter( VegetationGpuParameters parameter, Vec4 value )
        {
            for( int nMaterial = 0; nMaterial < 4; nMaterial++ )
            {
                Material material = null;

                switch( nMaterial )
                {
                case 0:
                    material = BaseMaterial;
                    break;
                case 1:
                    material = BaseMaterial.GetShadowTextureCasterMaterial( RenderLightType.Point );
                    break;
                case 2:
                    material = BaseMaterial.GetShadowTextureCasterMaterial( RenderLightType.Directional );
                    break;
                case 3:
                    material = BaseMaterial.GetShadowTextureCasterMaterial( RenderLightType.Spot );
                    break;
                }

                if( material == null )
                    continue;

                foreach( Technique technique in material.Techniques )
                {
                    foreach( Pass pass in technique.Passes )
                    {
                        if( pass.VertexProgramParameters != null || pass.FragmentProgramParameters != null )
                        {
                            if( pass.VertexProgramParameters != null )
                            {
                                if( !pass.IsCustomGpuParameterInitialized( (int)parameter ) )
                                {
                                    pass.VertexProgramParameters.SetNamedAutoConstant( parameter.ToString(),
                                        GpuProgramParameters.AutoConstantType.Custom, (int)parameter );
                                }
                            }

                            if( pass.FragmentProgramParameters != null )
                            {
                                if( !pass.IsCustomGpuParameterInitialized( (int)parameter ) )
                                {
                                    pass.FragmentProgramParameters.SetNamedAutoConstant( parameter.ToString(),
                                        GpuProgramParameters.AutoConstantType.Custom, (int)parameter );
                                }
                            }

                            pass.SetCustomGpuParameter( (int)parameter, value );
                        }
                    }
                }
            }
        }
 public void ApplyForce(Vec4 force)
 {
     Force = Force + force;
     Console.WriteLine ("Applied force" + Force.ToString ());
 }
Пример #47
0
        void SetGpuNamedConstant( string name, Vec4 value )
        {
            foreach( Technique technique in BaseMaterial.Techniques )
            {
                foreach( Pass pass in technique.Passes )
                {
                    GpuProgramParameters parameters;

                    parameters = pass.VertexProgramParameters;
                    if( parameters != null )
                        parameters.SetNamedConstant( name, value );

                    parameters = pass.FragmentProgramParameters;
                    if( parameters != null )
                        parameters.SetNamedConstant( name, value );
                }
            }
        }
Пример #48
0
        void UpdateReflectionCubemap( TextureUnitState textureUnitState, Vec3 objectWorldPosition )
        {
            string textureName = "";

            Vec4 reflectionParallax_cubemapPosition = Vec4.Zero;
            Vec3 reflectionParallax_rotationScale0 = Vec3.Zero;
            Vec3 reflectionParallax_rotationScale1 = Vec3.Zero;
            Vec3 reflectionParallax_rotationScale2 = Vec3.Zero;
            Vec3 reflectionParallax_position = Vec3.Zero;

            //get cubemap from CubemapZone's
            if( Map.Instance != null )
            {
                CubemapZone zone = CubemapZone.GetZoneForPoint( objectWorldPosition, true );
                if( zone != null )
                {
                    textureName = zone.GetTextureName();
                    reflectionParallax_cubemapPosition = new Vec4( zone.Position, 0 );
                }

                CubemapZone_BoxParallaxCorrectedZone zone2 = CubemapZone_BoxParallaxCorrectedZone.GetZoneForPoint( objectWorldPosition, 1 );
                if( zone2 != null )
                {
                    Mat3 mat = zone2.GetCachedRotationScaleInverseMatrix();
                    //Mat3 mat = zone2.Rotation.ToMat3() * Mat3.FromScale( zone2.Scale * .5f );
                    //mat = mat.GetInverse();

                    mat.Transpose();
                    reflectionParallax_rotationScale0 = mat[ 0 ];
                    reflectionParallax_rotationScale1 = mat[ 1 ];
                    reflectionParallax_rotationScale2 = mat[ 2 ];
                    reflectionParallax_position = zone2.Position;

                    //enable parallax correction
                    reflectionParallax_cubemapPosition.W = 1;
                }
            }

            UpdateReflectionBoxParallaxCorrectedCubemapsGpuParameters( reflectionParallax_cubemapPosition,
                reflectionParallax_rotationScale0, reflectionParallax_rotationScale1, reflectionParallax_rotationScale2,
                reflectionParallax_position );

            //get cubemap from SkyBox
            if( string.IsNullOrEmpty( textureName ) )
                textureName = SceneManager.Instance.GetSkyBoxTextureName();

            //update state
            textureUnitState.SetCubicTextureName( textureName, true );
        }
		static void CalculateBlurSampleOffsets( int textureSize, float[] sampleOffsets,
			Vec4[] sampleWeights, float deviation, float multiplier )
		{
			float tu = 1.0f / (float)textureSize;

			// Fill the center texel
			{
				float weight = multiplier * GaussianDistribution( 0, 0, deviation );
				sampleOffsets[ 0 ] = 0.0f;
				sampleWeights[ 0 ] = new Vec4( weight, weight, weight, 1.0f );
			}

			// Fill the first half
			for( int n = 1; n < 8; n++ )
			{
				// Get the Gaussian intensity for this offset
				float weight = multiplier * GaussianDistribution( n, 0, deviation );
				sampleOffsets[ n ] = n * tu;
				sampleWeights[ n ] = new Vec4( weight, weight, weight, 1.0f );
			}

			// Mirror to the second half
			for( int n = 8; n < 15; n++ )
			{
				sampleOffsets[ n ] = -sampleOffsets[ n - 7 ];
				sampleWeights[ n ] = sampleWeights[ n - 7 ];
			}

			//normalize weights (fix epsilon errors)
			{
				Vec4 total = Vec4.Zero;
				for( int n = 0; n < sampleWeights.Length; n++ )
					total += sampleWeights[ n ];
				for( int n = 0; n < sampleWeights.Length; n++ )
					sampleWeights[ n ] = ( sampleWeights[ n ] / total ) * multiplier;
			}
		}
Пример #50
0
        protected override void OnMaterialRender( uint passId, Material material, ref bool skipPass )
        {
            base.OnMaterialRender( passId, material, ref skipPass );

            const int rt_downscale = 100;
            const int rt_blurHorizontal = 200;
            const int rt_blurVertical = 300;
            const int rt_autoFocus1 = 400;
            const int rt_autoFocus2 = 401;
            const int rt_autoFocus3 = 402;
            const int rt_autoFocusFinal = 403;
            const int rt_autoFocusCurrent = 404;
            //const int rt_blurFactors = 500;
            const int rt_targetOutput = 600;

            //Skip auto focus passes if no auto focus is enabled
            if( !autoFocus )
            {
                if( passId == rt_autoFocus1 || passId == rt_autoFocus2 || passId == rt_autoFocus3 ||
                    passId == rt_autoFocusFinal || passId == rt_autoFocusCurrent )
                {
                    skipPass = true;
                    return;
                }
            }

            // Prepare the fragment params offsets
            switch( passId )
            {
            case rt_downscale:
                {
                    Vec2[] sampleOffsets = new Vec2[ 16 ];

                    CalculateDownScale4x4SampleOffsets( Owner.DimensionsInPixels.Size, sampleOffsets );

                    //convert to Vec4 array
                    Vec4[] vec4Offsets = new Vec4[ 16 ];
                    for( int n = 0; n < 16; n++ )
                    {
                        Vec2 offset = sampleOffsets[ n ];
                        vec4Offsets[ n ] = new Vec4( offset[ 0 ], offset[ 1 ], 0, 0 );
                    }

                    GpuProgramParameters parameters = material.GetBestTechnique().
                        Passes[ 0 ].FragmentProgramParameters;
                    parameters.SetNamedConstant( "sampleOffsets", vec4Offsets );
                }
                break;

            case rt_blurHorizontal:
            case rt_blurVertical:
                {
                    // horizontal and vertical blur
                    bool horizontal = passId == rt_blurHorizontal;

                    float[] sampleOffsets = new float[ 15 ];
                    Vec4[] sampleWeights = new Vec4[ 15 ];

                    CalculateBlurSampleOffsets( horizontal ? downscaleTextureSize.X : downscaleTextureSize.Y,
                        sampleOffsets, sampleWeights, 3, 1 );

                    //convert to Vec4 array
                    Vec4[] vec4Offsets = new Vec4[ 15 ];
                    for( int n = 0; n < 15; n++ )
                    {
                        float offset = sampleOffsets[ n ] * blurSpread;

                        if( horizontal )
                            vec4Offsets[ n ] = new Vec4( offset, 0, 0, 0 );
                        else
                            vec4Offsets[ n ] = new Vec4( 0, offset, 0, 0 );
                    }

                    GpuProgramParameters parameters = material.GetBestTechnique().
                        Passes[ 0 ].FragmentProgramParameters;
                    parameters.SetNamedConstant( "sampleOffsets", vec4Offsets );
                    parameters.SetNamedConstant( "sampleWeights", sampleWeights );
                }
                break;

            case rt_autoFocus1:
                {
                    GpuProgramParameters parameters = material.GetBestTechnique().
                        Passes[ 0 ].FragmentProgramParameters;
                    parameters.SetNamedAutoConstant( "farClipDistance",
                        GpuProgramParameters.AutoConstantType.FarClipDistance );
                }
                break;

            case rt_autoFocus2:
            case rt_autoFocus3:
                {
                    Vec2[] sampleOffsets = new Vec2[ 16 ];

                    string textureSizeFrom = null;
                    switch( passId )
                    {
                    case rt_autoFocus2: textureSizeFrom = "rt_autoFocus1"; break;
                    case rt_autoFocus3: textureSizeFrom = "rt_autoFocus2"; break;
                    default: Trace.Assert( false ); break;
                    }
                    Vec2I sourceTextureSize = Technique.GetTextureDefinition( textureSizeFrom ).Size;
                    CalculateDownScale4x4SampleOffsets( sourceTextureSize, sampleOffsets );

                    //convert to Vec4 array
                    Vec4[] vec4Offsets = new Vec4[ 16 ];
                    for( int n = 0; n < 16; n++ )
                    {
                        Vec2 offset = sampleOffsets[ n ];
                        vec4Offsets[ n ] = new Vec4( offset[ 0 ], offset[ 1 ], 0, 0 );
                    }

                    GpuProgramParameters parameters = material.GetBestTechnique().
                        Passes[ 0 ].FragmentProgramParameters;
                    parameters.SetNamedConstant( "sampleOffsets", vec4Offsets );
                }
                break;

            case rt_autoFocusFinal:
                {
                    GpuProgramParameters parameters = material.GetBestTechnique().
                        Passes[ 0 ].FragmentProgramParameters;

                    Vec4 properties = Vec4.Zero;
                    properties.X = autoFocusRange.Minimum;
                    properties.Y = autoFocusRange.Maximum;
                    properties.Z = RendererWorld.Instance.FrameRenderTimeStep * autoFocusTransitionSpeed;
                    parameters.SetNamedConstant( "properties", properties );
                }
                break;

            case rt_autoFocusCurrent:
                break;

            //case rt_blurFactors:
            //   {
            //      GpuProgramParameters parameters = material.GetBestTechnique().
            //         Passes[ 0 ].FragmentProgramParameters;
            //      parameters.SetNamedAutoConstant( "farClipDistance",
            //         GpuProgramParameters.AutoConstantType.FarClipDistance );

            //      Vec4 properties = Vec4.Zero;
            //      properties.X = autoFocus ? -1.0f : focalDistance;
            //      properties.Y = focalSize;
            //      properties.Z = backgroundTransitionLength;
            //      properties.W = blurForeground ? foregroundTransitionLength : -1;
            //      parameters.SetNamedConstant( "properties", properties );
            //   }
            //   break;

            //Final pass
            case rt_targetOutput:
                {
                    GpuProgramParameters parameters = material.GetBestTechnique().
                        Passes[ 0 ].FragmentProgramParameters;

                    parameters.SetNamedAutoConstant( "farClipDistance",
                        GpuProgramParameters.AutoConstantType.FarClipDistance );

                    Vec4 properties = Vec4.Zero;
                    properties.X = autoFocus ? -1.0f : focalDistance;
                    properties.Y = focalSize;
                    properties.Z = backgroundTransitionLength;
                    properties.W = blurForeground ? foregroundTransitionLength : -1;
                    parameters.SetNamedConstant( "properties", properties );

                    //Vec2[] sampleOffsets = new Vec2[ 49 ];
                    //Vec2 textureSize = Owner.DimensionsInPixels.Size.ToVec2();
                    //for( int y = -3; y <= 3; y++ )
                    //   for( int x = -3; x <= 3; x++ )
                    //      sampleOffsets[ ( y + 3 ) * 7 + ( x + 3 ) ] = new Vec2( x, y ) / textureSize;

                    ////convert to Vec4 array
                    //Vec4[] vec4Offsets = new Vec4[ 49 ];
                    //for( int n = 0; n < 49; n++ )
                    //{
                    //   Vec2 offset = sampleOffsets[ n ];
                    //   vec4Offsets[ n ] = new Vec4( offset[ 0 ], offset[ 1 ], 0, 0 );
                    //}
                    //parameters.SetNamedConstant( "sampleOffsets", vec4Offsets );
                }
                break;
            }
        }
		protected override void OnMaterialRender( uint passId, Material material, ref bool skipPass )
		{
			base.OnMaterialRender( passId, material, ref skipPass );

			const int rt_depthDownsample1x1 = 100;
			const int rt_depthDownsample2x2 = 101;
			const int rt_depthDownsample3x3 = 102;
			const int rt_occlusion = 200;
			const int rt_blurHorizontal = 300;
			const int rt_blurVertical = 400;
			const int rt_targetOutput = 500;

			switch( passId )
			{
			case rt_depthDownsample1x1:
			case rt_depthDownsample2x2:
			case rt_depthDownsample3x3:
				{
					if( downsampling == 1 && passId != rt_depthDownsample1x1 )
					{
						skipPass = true;
						return;
					}
					if( downsampling == 2 && passId != rt_depthDownsample2x2 )
					{
						skipPass = true;
						return;
					}
					if( downsampling == 3 && passId != rt_depthDownsample3x3 )
					{
						skipPass = true;
						return;
					}

					GpuProgramParameters parameters = material.Techniques[ 0 ].Passes[ 0 ].FragmentProgramParameters;
					if( parameters != null )
					{
						parameters.SetNamedAutoConstant( "viewportSize", GpuProgramParameters.AutoConstantType.ViewportSize );

						Vec4 v = new Vec4( EngineApp.Instance.IsKeyPressed( EKeys.Z ) ? 1 : -1, 0, 0, 0 );
						parameters.SetNamedConstant( "temp", v );
					}
				}
				break;

			case rt_occlusion:
				{
					GpuProgramParameters parameters = material.Techniques[ 0 ].Passes[ 0 ].FragmentProgramParameters;
					if( parameters != null )
					{
						parameters.SetNamedAutoConstant( "farClipDistance",
							GpuProgramParameters.AutoConstantType.FarClipDistance );
						parameters.SetNamedAutoConstant( "viewportSize",
							GpuProgramParameters.AutoConstantType.ViewportSize );
						parameters.SetNamedAutoConstant( "fov",
							GpuProgramParameters.AutoConstantType.FOV );

						parameters.SetNamedConstant( "downscaleTextureSize",
							new Vec4( downscaleTextureSize.X, downscaleTextureSize.Y,
							1.0f / (float)downscaleTextureSize.X, 1.0f / (float)downscaleTextureSize.Y ) );

						parameters.SetNamedConstant( "sampleLength", sampleLength );
						parameters.SetNamedConstant( "offsetScale", offsetScale );
						parameters.SetNamedConstant( "defaultAccessibility", defaultAccessibility );

						//parameters.SetNamedConstant( "parameters",
						//   new Vec4( sampleLength, offsetScale, defaultAccessibility, 0 ) );

						Range range = new Range( maxDistance, maxDistance * 1.2f );
						parameters.SetNamedConstant( "fadingByDistanceRange",
							new Vec4( range.Minimum, 1.0f / ( range.Maximum - range.Minimum ), 0, 0 ) );
					}
				}
				break;

			case rt_blurHorizontal:
			case rt_blurVertical:
				{
					// horizontal and vertical blur
					bool horizontal = passId == rt_blurHorizontal;

					float[] sampleOffsets = new float[ 15 ];
					Vec4[] sampleWeights = new Vec4[ 15 ];

					Vec2I textureSize = Owner.DimensionsInPixels.Size;
					CalculateBlurSampleOffsets( horizontal ? textureSize.X : textureSize.Y, sampleOffsets, sampleWeights, 3, 1 );

					//convert to Vec4 array
					Vec4[] vec4Offsets = new Vec4[ 15 ];
					for( int n = 0; n < 15; n++ )
					{
						float offset = sampleOffsets[ n ] * blurSpread;

						if( horizontal )
							vec4Offsets[ n ] = new Vec4( offset, 0, 0, 0 );
						else
							vec4Offsets[ n ] = new Vec4( 0, offset, 0, 0 );
					}

					GpuProgramParameters parameters = material.GetBestTechnique().
						Passes[ 0 ].FragmentProgramParameters;
					parameters.SetNamedAutoConstant( "farClipDistance",
						GpuProgramParameters.AutoConstantType.FarClipDistance );
					parameters.SetNamedConstant( "sampleOffsets", vec4Offsets );
					parameters.SetNamedConstant( "sampleWeights", sampleWeights );

					//parameters.SetNamedConstant( "horizontal", passId == rt_blurHorizontal ? 1.0f : -1.0f );
				}
				break;

			case rt_targetOutput:
				{
					GpuProgramParameters parameters = material.GetBestTechnique().
						Passes[ 0 ].FragmentProgramParameters;
					parameters.SetNamedConstant( "intensity", intensity );

					//parameters.SetNamedConstant( "fixEdges", fixEdges ? 1.0f : -1.0f );
					parameters.SetNamedConstant( "showAO", showAO ? 1.0f : -1.0f );

					parameters.SetNamedConstant( "downscaleTextureSize",
						new Vec4( downscaleTextureSize.X, downscaleTextureSize.Y,
							1.0f / (float)downscaleTextureSize.X, 1.0f / (float)downscaleTextureSize.Y ) );
				}
				break;
			}
		}
Пример #52
0
        void UpdateTracksTextureAnimation()
        {
            for( int nTrack = 0; nTrack < 2; nTrack++ )
            {
                Track track = nTrack == 0 ? leftTrack : rightTrack;

                if( track.meshSubObject == null )
                    continue;

                //update value
                if( EntitySystemWorld.Instance.Simulation &&
                    !EntitySystemWorld.Instance.SystemPauseOfSimulation )
                {
                    Vec2 value = track.materialScrollValue + Type.TracksAnimationMultiplier *
                        ( track.speed * RendererWorld.Instance.FrameRenderTimeStep );

                    while( value.X < 0 ) value.X++;
                    while( value.X >= 1 ) value.X--;
                    while( value.Y < 0 ) value.Y++;
                    while( value.Y >= 1 ) value.Y--;

                    track.materialScrollValue = value;
                }

                //update gpu program parameters
                {
                    Vec4 value = new Vec4( track.materialScrollValue.X, track.materialScrollValue.Y,
                        0, 0 );

                    track.meshSubObject.SetCustomGpuParameter(
                        (int)ShaderBaseMaterial.GpuParameters.diffuse1MapTransformAdd, value );
                    track.meshSubObject.SetCustomGpuParameter(
                        (int)ShaderBaseMaterial.GpuParameters.specularMapTransformAdd, value );
                    track.meshSubObject.SetCustomGpuParameter(
                        (int)ShaderBaseMaterial.GpuParameters.normalMapTransformAdd, value );
                }

                //update parameters for fixed pipeline material
                if( track.clonedMaterialForFixedPipeline != null )
                {
                    track.clonedMaterialForFixedPipeline.Diffuse1Map.Transform.Scroll = track.materialScrollValue;
                    track.clonedMaterialForFixedPipeline.SpecularMap.Transform.Scroll = track.materialScrollValue;
                }
            }
        }
        static void CalculateGaussianBlur5x5SampleOffsets( Vec2i textureSize,
			Vec2[] sampleOffsets, Vec4[] sampleWeights, float multiplier )
        {
            float tu = 1.0f / (float)textureSize.X;
            float tv = 1.0f / (float)textureSize.Y;

            Vec4 white = new Vec4( 1, 1, 1, 1 );

            float totalWeight = 0.0f;
            int index = 0;
            for( int x = -2; x <= 2; x++ )
            {
                for( int y = -2; y <= 2; y++ )
                {
                    // Exclude pixels with a block distance greater than 2. This will
                    // create a kernel which approximates a 5x5 kernel using only 13
                    // sample points instead of 25; this is necessary since 2.0 shaders
                    // only support 16 texture grabs.
                    if( Math.Abs( x ) + Math.Abs( y ) > 2 )
                        continue;

                    // Get the unscaled Gaussian intensity for this offset
                    sampleOffsets[ index ] = new Vec2( x * tu, y * tv );
                    sampleWeights[ index ] = white * GaussianDistribution( (float)x, (float)y, 1 );
                    totalWeight += sampleWeights[ index ].X;

                    index++;
                }
            }

            // Divide the current weight by the total weight of all the samples; Gaussian
            // blur kernels add to 1.0f to ensure that the intensity of the image isn't
            // changed when the blur occurs. An optional multiplier variable is used to
            // add or remove image intensity during the blur.
            for( int i = 0; i < index; i++ )
            {
                sampleWeights[ i ] /= totalWeight;
                sampleWeights[ i ] *= multiplier;
            }
        }
Пример #54
0
 public Vertex(Vec3 position, Vec3 normal, Vec2 texCoord, Vec4 tangent)
 {
     this.position = position;
     this.normal = normal;
     this.texCoord = texCoord;
     this.tangent = tangent;
 }
Пример #55
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var vector4 = (Vector4)value;
            var vec4 = new Vec4() {
                x = vector4.x,
                y = vector4.y,
                z = vector4.z,
                w = vector4.w
            };

            serializer.Serialize(writer, vec4);
        }
Пример #56
0
        void SetCustomGpuParameter( GpuParameters parameter, Vec4 value, bool vertex, bool fragment,
			bool needForShadowCasterMaterial )
        {
            string parameterAsString = null;

            int materialCount = needForShadowCasterMaterial ? 4 : 1;
            for( int nMaterial = 0; nMaterial < materialCount; nMaterial++ )
            {
                Material material = null;

                switch( nMaterial )
                {
                case 0:
                    material = BaseMaterial;
                    break;
                case 1:
                    material = BaseMaterial.GetShadowTextureCasterMaterial( RenderLightType.Point );
                    break;
                case 2:
                    material = BaseMaterial.GetShadowTextureCasterMaterial( RenderLightType.Directional );
                    break;
                case 3:
                    material = BaseMaterial.GetShadowTextureCasterMaterial( RenderLightType.Spot );
                    break;
                }

                if( material == null )
                    continue;

                foreach( Technique technique in material.Techniques )
                {
                    foreach( Pass pass in technique.Passes )
                    {
                        GpuProgramParameters vertexParameters = pass.VertexProgramParameters;
                        GpuProgramParameters fragmentParameters = pass.FragmentProgramParameters;

                        if( vertexParameters != null || fragmentParameters != null )
                        {
                            if( vertex && vertexParameters != null )
                            {
                                if( !pass.IsCustomGpuParameterInitialized( (int)parameter ) )
                                {
                                    if( parameterAsString == null )
                                        parameterAsString = parameter.ToString();
                                    vertexParameters.SetNamedAutoConstant( parameterAsString,
                                        GpuProgramParameters.AutoConstantType.Custom, (int)parameter );
                                }
                            }

                            if( fragment && fragmentParameters != null )
                            {
                                if( !pass.IsCustomGpuParameterInitialized( (int)parameter ) )
                                {
                                    if( parameterAsString == null )
                                        parameterAsString = parameter.ToString();
                                    fragmentParameters.SetNamedAutoConstant( parameterAsString,
                                        GpuProgramParameters.AutoConstantType.Custom, (int)parameter );
                                }
                            }

                            pass.SetCustomGpuParameter( (int)parameter, value );
                        }
                    }
                }
            }
        }