예제 #1
0
 public FaceTextureData(ImageBuffer textureToUse, Vector2Float vector2Float1, Vector2Float vector2Float2, Vector2Float vector2Float3)
 {
     this.image = textureToUse;
     this.uv0   = vector2Float1;
     this.uv1   = vector2Float2;
     this.uv2   = vector2Float3;
 }
예제 #2
0
        public static void PlaceTextureOnFaces(this Mesh mesh, IEnumerable <int> faces, ImageBuffer textureToUse, Matrix4X4 textureCoordinateMapping)
        {
            var faceTextures = new Dictionary <int, FaceTextureData>();

            var uvs = new Vector2Float[3];

            foreach (var face in faces)
            {
                int uvIndex = 0;
                foreach (int vertexIndex in new int[] { mesh.Faces[face].v0, mesh.Faces[face].v1, mesh.Faces[face].v2 })
                {
                    var edgeStartPosition = mesh.Vertices[vertexIndex];
                    var textureUv         = edgeStartPosition.Transform(textureCoordinateMapping);
                    uvs[uvIndex++] = new Vector2Float(textureUv);
                }

                faceTextures.Add(face, new FaceTextureData(textureToUse, uvs[0], uvs[1], uvs[2]));
            }

            // add in any existing face textures
            if (mesh.FaceTextures != null)
            {
                foreach (var kvp in mesh.FaceTextures)
                {
                    faceTextures[kvp.Key] = kvp.Value;
                }
            }

            mesh.FaceTextures = faceTextures;

            mesh.MarkAsChanged();
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        public override void Update()
        {
            releaseTime -= Time.DeltaTime;
            if (releaseTime < 0)
            {
                GameObject.Destroy(GameObject);
            }
            FPosition += Speed * Time.DeltaTime;
            //浮点溢出之后进行移动 相当于原来的Rigid
            int dx = 0, dy = 0;

            if (FPosition.X > 1)
            {
                FPosition.X -= 1;
                dx          += 1;
            }
            else if (FPosition.X < -1)
            {
                FPosition.X += 1;
                dx          -= 1;
            }

            if (FPosition.Y > 1)
            {
                FPosition.Y -= 1;
                dy          += 1;
            }
            else if (FPosition.Y < -1)
            {
                FPosition.Y += 1;
                dy          -= 1;
            }
            Position += new Vector2(dx, dy);
        }
예제 #4
0
 public TriangleShapeUv(Vector3Float vertex0, Vector3Float vertex1, Vector3Float vertex2,
                        Vector2Float uv0, Vector2Float uv1, Vector2Float uv2,
                        MaterialAbstract material)
     : base(vertex0.AsVector3(), vertex1.AsVector3(), vertex2.AsVector3(), material)
 {
     this.uv0 = uv0;
     this.uv1 = uv1;
     this.uv2 = uv2;
 }
예제 #5
0
 public TriangleShapeUv(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2,
                        Vector2 uv0, Vector2 uv1, Vector2 uv2,
                        MaterialAbstract material)
     : base(vertex0, vertex1, vertex2, material)
 {
     this.uv0 = new Vector2Float(uv0);
     this.uv1 = new Vector2Float(uv1);
     this.uv2 = new Vector2Float(uv2);
 }
예제 #6
0
        /*
         *  Solves the system [a] + [b]s =  [u] + [v]t
         *  [a - u] = [v]t - [b]s
         */
        private static void SolveLinearSystem2(Point2Float a, Vector2Float b, out float s, Point2Float u, Vector2Float v, out float t)
        {
            Vector2Float constants = a - u;
            double       det_denom = Vector2Float.Determinant(v, -b);
            double       det_v     = Vector2Float.Determinant(constants, -b);
            double       det_b     = Vector2Float.Determinant(v, constants);

            t = (float)(det_v / det_denom);
            s = (float)(det_b / det_denom);
        }
예제 #7
0
 public TriangleShapeUv(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2,
                        Vector2Float uv0, Vector2Float uv1, Vector2Float uv2,
                        MaterialAbstract material,
                        int index)
     : base(vertex0, vertex1, vertex2, material, index)
 {
     this.uv0 = uv0;
     this.uv1 = uv1;
     this.uv2 = uv2;
 }
예제 #8
0
        public static void PlaceTextureOnFace(this Mesh mesh, int face, ImageBuffer textureToUse, Matrix4X4 textureCoordinateMapping)
        {
            var uvs     = new Vector2Float[3];
            int uvIndex = 0;

            foreach (int vertexIndex in new int[] { mesh.Faces[face].v0, mesh.Faces[face].v1, mesh.Faces[face].v2 })
            {
                var edgeStartPosition = mesh.Vertices[vertexIndex];
                var textureUv         = edgeStartPosition.Transform(textureCoordinateMapping);
                uvs[uvIndex++] = new Vector2Float(textureUv);
            }

            mesh.FaceTextures.Add(face, new FaceTextureData(textureToUse, uvs[0], uvs[1], uvs[2]));

            mesh.MarkAsChanged();
        }
예제 #9
0
 public static void TexCoord2(Vector2Float uv)
 {
     TexCoord2(uv.X, uv.Y);
 }
예제 #10
0
        public static IObject3D Load(Stream fileStream, Action <double, string> reportProgress = null, IObject3D source = null)
        {
            IObject3D root = source ?? new Object3D();

            var time = Stopwatch.StartNew();

            // LOAD THE MESH DATA
            var objFile = new Obj();

            objFile.LoadObj(fileStream);

            IObject3D context = new Object3D();

            root.Children.Add(context);

            var mesh = new Mesh();

            context.SetMeshDirect(mesh);

            foreach (var vertex in objFile.VertexList)
            {
                mesh.Vertices.Add(new Vector3Float(vertex.X, vertex.Y, vertex.Z));
            }

            foreach (var face in objFile.FaceList)
            {
                for (int i = 0; i < face.VertexIndexList.Length; i++)
                {
                    if (face.VertexIndexList[i] >= objFile.TextureList.Count)
                    {
                        int a = 0;
                    }
                }

                mesh.Faces.Add(face.VertexIndexList[0] - 1, face.VertexIndexList[1] - 1, face.VertexIndexList[2] - 1, mesh.Vertices);
                if (face.VertexIndexList.Length == 4)
                {
                    // add the other side of the quad
                    mesh.Faces.Add(face.VertexIndexList[0] - 1, face.VertexIndexList[2] - 1, face.VertexIndexList[3] - 1, mesh.Vertices);
                }
            }

            // load and apply any texture
            if (objFile.Material != "")
            {
                // TODO: have consideration for this being in a shared zip file
                string pathToObj = Path.GetDirectoryName(((FileStream)fileStream).Name);
                //Try-catch block for when objFile.Material is not found
                try
                {
                    using (var materialsStream = File.OpenRead(Path.Combine(pathToObj, objFile.Material)))
                    {
                        var mtl = new Mtl();
                        mtl.LoadMtl(materialsStream);

                        foreach (var material in mtl.MaterialList)
                        {
                            if (!string.IsNullOrEmpty(material.DiffuseTextureFileName))
                            {
                                var pathToTexture = Path.Combine(pathToObj, material.DiffuseTextureFileName);
                                if (File.Exists(pathToTexture))
                                {
                                    var diffuseTexture = new ImageBuffer();

                                    // TODO: have consideration for this being in a shared zip file
                                    using (var imageStream = File.OpenRead(pathToTexture))
                                    {
                                        if (Path.GetExtension(material.DiffuseTextureFileName).ToLower() == ".tga")
                                        {
                                            ImageTgaIO.LoadImageData(diffuseTexture, imageStream, 32);
                                        }
                                        else
                                        {
                                            ImageIO.LoadImageData(imageStream, diffuseTexture);
                                        }
                                    }

                                    if (diffuseTexture.Width > 0 && diffuseTexture.Height > 0)
                                    {
                                        int meshFace = 0;
                                        for (int objFace = 0; objFace < objFile.FaceList.Count; objFace++, meshFace++)
                                        {
                                            var face = mesh.Faces[meshFace];

                                            var faceData = objFile.FaceList[objFace];

                                            int textureIndex0 = faceData.TextureVertexIndexList[0] - 1;
                                            var uv0           = new Vector2Float(objFile.TextureList[textureIndex0].X, objFile.TextureList[textureIndex0].Y);
                                            int textureIndex1 = faceData.TextureVertexIndexList[1] - 1;
                                            var uv1           = new Vector2Float(objFile.TextureList[textureIndex1].X, objFile.TextureList[textureIndex1].Y);
                                            int textureIndex2 = faceData.TextureVertexIndexList[2] - 1;
                                            var uv2           = new Vector2Float(objFile.TextureList[textureIndex2].X, objFile.TextureList[textureIndex2].Y);

                                            mesh.FaceTextures.Add(meshFace, new FaceTextureData(diffuseTexture, uv0, uv1, uv2));

                                            if (faceData.TextureVertexIndexList.Length == 4)
                                            {
                                                meshFace++;

                                                int textureIndex3 = faceData.TextureVertexIndexList[3] - 1;
                                                var uv3           = new Vector2Float(objFile.TextureList[textureIndex3].X, objFile.TextureList[textureIndex3].Y);

                                                mesh.FaceTextures.Add(meshFace, new FaceTextureData(diffuseTexture, uv0, uv2, uv3));
                                            }
                                        }

                                        context.Color = Color.White;
                                        root.Color    = Color.White;
                                    }
                                }
                            }
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                    // Just continue as if obj.Material == "" to show object
                }
            }

            time.Stop();
            Debug.WriteLine(string.Format("OBJ Load in {0:0.00}s", time.Elapsed.TotalSeconds));

            time.Restart();
            bool hasValidMesh = root.Children.Where(item => item.Mesh.Faces.Count > 0).Any();

            Debug.WriteLine("hasValidMesh: " + time.ElapsedMilliseconds);

            reportProgress?.Invoke(1, "");

            return(hasValidMesh ? root : null);
        }
예제 #11
0
 public Vector3 GetPositionVector(Vector2Float vector)
 {
     return(new Vector3(vector.X, 0, vector.Y));
 }
예제 #12
0
        public override void Render(EffectConfigToken parameters, RenderArgs dstArgs, RenderArgs srcArgs, Rectangle[] rois, int startIndex, int length)
        {
            BlendEffectConfigToken token = parameters as BlendEffectConfigToken;
            Point2Float            start = new Point2Float();
            Point2Float            end   = new Point2Float();
            Point2Float            p;
            Point2Float            q;

            if (token.blendDirection == BlendDirection.BL_TR)
            {
                start = new Point2Float(bounds.Left - 0.5f, bounds.Bottom + 0.5f);
                end   = new Point2Float(bounds.Right + 0.5f, bounds.Top - 0.5f);
            }
            else if (token.blendDirection == BlendDirection.BR_TL)
            {
                start = new Point2Float(bounds.Right, bounds.Bottom);
                end   = new Point2Float(bounds.Left, bounds.Top);
            }
            else if (token.blendDirection == BlendDirection.TL_BR)
            {
                start = new Point2Float(bounds.Left, bounds.Top);
                end   = new Point2Float(bounds.Right, bounds.Bottom);
            }
            else if (token.blendDirection == BlendDirection.TR_BL)
            {
                start = new Point2Float(bounds.Right, bounds.Top);
                end   = new Point2Float(bounds.Left, bounds.Bottom);
            }

            if (token.blendDirection == BlendDirection.BL_TR || token.blendDirection == BlendDirection.TR_BL)
            {
                p = new Point2Float(bounds.Right, bounds.Bottom);
                q = new Point2Float(bounds.Left, bounds.Top);
            }
            else
            {
                p = new Point2Float(bounds.Left, bounds.Bottom);
                q = new Point2Float(bounds.Right, bounds.Top);
            }

            Vector2Float lineDir = Vector2Float.Normalize(p - q);

            for (int i = startIndex; i < length; ++i)
            {
                Rectangle rect = rois[i];
                for (int y = rect.Top; y < rect.Bottom; ++y)
                {
                    for (int x = rect.Left; x < rect.Right; ++x)
                    {
                        Point2Float  current     = new Point2Float(x, y) + new Vector2Float(0.5f, 0.5f);
                        Vector2Float dir         = Vector2Float.Normalize(end - current);
                        float        distToCross = 0;
                        float        dist_ignore = 0;
                        SolveLinearSystem2(current, dir, out distToCross, p, Vector2Float.Normalize(q - p), out dist_ignore);

                        Point2Float  center       = new Point2Float(bounds.Left + bounds.Width / 2, bounds.Top + bounds.Height / 2);
                        Vector2Float distToCenter = current - center;
                        distToCenter.X /= bounds.Width / 2;
                        distToCenter.Y /= bounds.Height / 2;
                        Point2Float from2;

                        if (Math.Abs(distToCenter.X) > Math.Abs(distToCenter.Y))
                        {
                            from2 = new Point2Float(start.X, end.Y);
                        }
                        else
                        {
                            from2 = new Point2Float(end.X, start.Y);
                        }

                        float distToStart  = 0;
                        float dist_ignore2 = 0;
                        SolveLinearSystem2(current, -dir, out distToStart, from2, Vector2Float.Normalize(from2 - start), out dist_ignore2);


                        if (distToCross > 0)
                        {
                            float t = distToStart / (distToStart + distToCross);

                            ColorBgra pixle = srcArgs.Surface.GetPoint(x, y);
                            pixle.A = (byte)(t * pixle.A);
                            //pixle.R = (byte)(255 * t);
                            //pixle.G = 0; // (byte)(from2.Y);
                            //pixle.B = 0;
                            dstArgs.Surface.SetPoint(x, y, pixle);
                        }
                        else
                        {
                            dstArgs.Surface.SetPoint(x, y, srcArgs.Surface.GetPoint(x, y));
                        }
                    }
                }
            }
        }