Exemple #1
0
        public static Vector3 GetPosFromProjSpaceBarycentricCoordinateZ(TriangleVertex tri, float pz, float a, float b, float c)
        {
            tri.triangle.InvZ();
            Vector3 ret = (tri.triangle.p1 * a * tri.triangle.p1.z + tri.triangle.p2 * b * tri.triangle.p2.z + tri.triangle.p3 * c * tri.triangle.p3.z) * pz;

            return(ret);
        }
        private void DebugVertexLog(TriangleVertex vertex)
        {
            var camera = Camera.main;

            if (camera != null)
            {
                var    p1  = camera.WorldToScreenPoint(vertex.triangle.p1);
                var    p2  = camera.WorldToScreenPoint(vertex.triangle.p2);
                var    p3  = camera.WorldToScreenPoint(vertex.triangle.p3);
                string ss1 = SoftCameraTest.GetVectorStr(p1);
                string ss2 = SoftCameraTest.GetVectorStr(p2);
                string ss3 = SoftCameraTest.GetVectorStr(p3);
                string ss4 = string.Format("【Camera】p1={0} p2={1} p3={2}", ss1, ss2, ss3);

                vertex.triangle.Trans(this.WorldToScreenPointEvt);

                // Debug.LogError(this.ViewProjLinkerScreenMatrix.ToString());

                string s1 = SoftCameraTest.GetVectorStr(vertex.triangle.p1);
                string s2 = SoftCameraTest.GetVectorStr(vertex.triangle.p2);
                string s3 = SoftCameraTest.GetVectorStr(vertex.triangle.p3);
                string s4 = string.Format("【SoftCamera】p1={0} p2={1} p3={2}", s1, s2, s3);

                Debug.Log(ss4 + s4);
            }
        }
Exemple #3
0
        public static Color GetColorFromProjSpaceBarycentricCoordinateAndZ(TriangleVertex tri, float pz, float a, float b, float c)
        {
            tri.triangle.InvZ();
            Color ret = (tri.cP1 * a * tri.triangle.p1.z + tri.cP2 * b * tri.triangle.p2.z + tri.cP3 * c * tri.triangle.p3.z) * pz;

            return(ret);
        }
Exemple #4
0
        public static Vector4 GetUV1FromProjSpaceBarycentricCoordinateAndZ(TriangleVertex tri, float pz, float a, float b, float c)
        {
            tri.triangle.InvZ();
            Vector4 ret = (tri.uv1_1 * a * tri.triangle.p1.z + tri.uv1_2 * b * tri.triangle.p2.z + tri.uv1_3 * c * tri.triangle.p3.z) * pz;

            return(ret);
        }
Exemple #5
0
 public void AddTriangle(TriangleVertex vertex)
 {
     if (m_List == null)
     {
         m_List = new NativeList <TriangleVertex>();
     }
     m_List.Add(vertex);
 }
Exemple #6
0
        public static float GetProjSpaceBarycentricCoordinateZ(TriangleVertex tri, Vector2 P, out float a, out float b, out float c)
        {
            Vector3 PP = new Vector3(P.x, P.y, 0f);

            GetBarycentricCoordinate(tri.triangle.p1, tri.triangle.p2, tri.triangle.p3, PP, out a, out b, out c);
            float ret = a * tri.triangle.p1.z + b * tri.triangle.p2.z + c * tri.triangle.p3.z;

            return(ret);
        }
Exemple #7
0
 public bool GetTrangle(int index, out TriangleVertex vertex)
 {
     if (m_List == null || index < 0 || index >= m_List.Count)
     {
         vertex = new TriangleVertex();
         return(false);
     }
     vertex = m_List[index];
     return(true);
 }
Exemple #8
0
        public static ObliqueTriangle ConstructFromVertexPositions(
            Point Pos1, Point Pos2, Point Pos3)
        {
            // setup the left most vertex, then the upper vertex and lower vertex.
            var   rv  = ObliqueTriangle.SplitLeftMostPoint(new Point[] { Pos1, Pos2, Pos3 });
            Point pt1 = rv.Item1; // pt1 is left most point

            var   rv2 = ObliqueTriangle.SplitTopMostPoint(rv.Item2);
            Point pt2 = rv2.Item1; // pt2 is top most point of pt2 and pt3.
            Point pt3 = rv2.Item2[0];

            // Create lines between the points. Line numbers match the number of the opposite
            // point.
            LineCoordinates line1 = new LineCoordinates(pt2, pt3);
            LineCoordinates line2 = new LineCoordinates(pt1, pt3);
            LineCoordinates line3 = new LineCoordinates(pt1, pt2);

            // the angle of each vertex.
            var ang1 = LineCoordinates.AngleBetween(line2, line3);
            var ang2 = LineCoordinates.AngleBetween(line1, line3);
            var ang3 = LineCoordinates.AngleBetween(line2, line1);

            var vertex1 = new TriangleVertex()
            {
                Angle    = ang1,
                Location = pt1,
                Line     = line1
            };

            var vertex2 = new TriangleVertex()
            {
                Angle    = ang2,
                Location = pt2,
                Line     = line2
            };

            var vertex3 = new TriangleVertex()
            {
                Angle    = ang3,
                Location = pt3,
                Line     = line3
            };

            var ot = new ObliqueTriangle()
            {
                Vertex1 = vertex1,
                Vertex2 = vertex2,
                Vertex3 = vertex3
            };

            return(ot);
        }
        private void FlipTriangle(TriangleVertex vertex, RenderPassMode passMode)
        {
            // 三角形转到屏幕坐标系
            RenderTarget target = this.Target;

            if (target != null)
            {
                if (IsShowVertexLog)
                {
                    DebugVertexLog(vertex);
                }

                // 这里是VertexShader的部分
                // 世界坐标系到屏幕坐标系
                if (passMode.vertexShader == null)
                {
                    // 默认的一个处理
                    //vertex.triangle.Trans(this.WorldToScreenPointEvt2, false);
                    vertex.triangle.MulMatrix(this.Shader_MVP_Matrix);
                }
                else
                {
                    InitPassMode(passMode);
                    passMode.vertexShader.Main(ref vertex);
                }

                // 做个三角形的判断
                if (vertex.IsAllZGreateOne)
                {
                    return;
                }

                // 这里做背面剔除
                if (SoftMath.Is_MVP_Culled(passMode.Cull, vertex.triangle))
                {
                    return;
                }

                vertex.triangle.MulMatrix(this.LinkerScreenMatrix);
#if _Use_FlipTrangle2
                target.FlipScreenTriangle2(this, vertex, passMode);
#else
                target.FlipScreenTriangle(this, vertex, passMode);
#endif
            }
        }
        private void CreateTriangle()
        {
            //Creates a triangle with position and color data
            var data = new TriangleVertex[] {
                new TriangleVertex(new Vector3(0, 1, 0), Color4.Blue),
                new TriangleVertex(new Vector3(1, -1, 0), Color4.Green),
                new TriangleVertex(new Vector3(-1, -1, 0), Color4.Red)
            };

            //Create the vertex buffer for olding the data in the device, passing data will feed the vertex buffer
            //with the provided array
            vertexBuffer = device.CreateVertexBuffer(data: data);

            //Create transformation matrices
            world      = Matrix.Identity;
            view       = Matrix.LookAt(new Vector3(0, 0, -1), new Vector3(0, 0, 1), Vector3.UnitY);
            projection = Matrix.PerspectiveFovLh((float)Width / (float)Height, Igneel.Numerics.PIover6, 1, 1000);
        }
        public void AddThickRectangle(RectangleF outer, RectangleF inner, ColorValue color)
        {
            var vertices = new TriangleVertex[8]
            {
                new TriangleVertex(outer.LeftTop, color),
                new TriangleVertex(inner.LeftTop, color),
                new TriangleVertex(outer.RightTop, color),
                new TriangleVertex(inner.RightTop, color),
                new TriangleVertex(outer.RightBottom, color),
                new TriangleVertex(inner.RightBottom, color),
                new TriangleVertex(outer.LeftBottom, color),
                new TriangleVertex(inner.LeftBottom, color)
            };

            var indices = new int[8 * 3]
            {
                0, 1, 2, 2, 1, 3,
                2, 3, 4, 4, 3, 5,
                4, 5, 6, 6, 5, 7,
                6, 7, 0, 0, 7, 1
            };

            AddTriangles(vertices, indices);
        }
 // 主函数,没有弄额外的结构,懒得弄,简单些
 public virtual void Main(ref TriangleVertex vertex)
 {
     vertex.triangle.MulMatrix(m_Owner.MVPMatrix);
 }
        private bool RenderSubMesh(SoftMesh mesh, SoftSubMesh subMesh, Matrix4x4 objToWorld, RenderPassMode passMode)
        {
            if (subMesh == null || passMode == null)
            {
                return(false);
            }
            var  indexes = subMesh.Indexes;
            var  vertexs = mesh.Vertexs;
            var  colors  = mesh.Colors;
            var  uv1s    = mesh.UV1s;
            bool ret     = false;

            bool    isColorEmpty = colors == null || colors.Count <= 0;
            bool    isUV1Empty   = uv1s == null || uv1s.Count <= 0;
            Color   c1           = Color.white;
            Color   c2           = Color.white;
            Color   c3           = Color.white;
            Vector4 uv1_1        = Vector4.zero;
            Vector4 uv1_2        = Vector4.zero;
            Vector4 uv1_3        = Vector4.zero;

            if (vertexs != null && (isColorEmpty || vertexs.Count == colors.Count) &&
                indexes != null && indexes.Count > 0)
            {
                int triangleCnt = ((int)indexes.Count / 3);
                for (int i = 0; i < triangleCnt; ++i)
                {
                    int     idx   = i * 3;
                    int     index = indexes[idx];
                    Vector3 p1    = vertexs[index];
                    if (!isColorEmpty)
                    {
                        c1 = colors[index];
                    }
                    if (!isUV1Empty)
                    {
                        uv1_1 = uv1s[index];
                    }
                    index = indexes[idx + 1];
                    Vector3 p2 = vertexs[index];
                    if (!isColorEmpty)
                    {
                        c2 = colors[index];
                    }
                    if (!isUV1Empty)
                    {
                        uv1_2 = uv1s[index];
                    }
                    index = indexes[idx + 2];
                    Vector3 p3 = vertexs[index];
                    if (!isColorEmpty)
                    {
                        c3 = colors[index];
                    }
                    if (!isUV1Empty)
                    {
                        uv1_3 = uv1s[index];
                    }
                    Triangle tri = new Triangle(p1, p2, p3);

                    // 三角形转到世界坐标系
                    tri.MulMatrix(objToWorld);
                    // 过CullMode 【注意】根据渲染管线VertexShader中可以任意改变三角形,所以要放到VS后面才行,也就是到MVP坐标系里判断
                    // 不在这里做摄影机剔除,移到VS后面
                    //   if (SoftMath.IsCulled(this, passMode.Cull, tri)) {
                    //       continue;
                    //   }
                    //----

                    TriangleVertex triV = new TriangleVertex(tri, c1, c2, c3, passMode.mainTex);
                    if (!isUV1Empty)
                    {
                        triV.uv1_1 = uv1_1;
                        triV.uv1_2 = uv1_2;
                        triV.uv1_3 = uv1_3;
                    }

                    // 进入VertexShader了, 做顶点变换等
                    m_TrianglesMgr.AddTriangle(triV);
                    ret = true;
                }
            }
            return(ret);
        }
Exemple #14
0
        public static float GetProjSpaceBarycentricCoordinateZ(TriangleVertex tri, Vector2 P)
        {
            float ret = GetProjSpaceBarycentricCoordinateZ(tri.triangle.p1, tri.triangle.p2, tri.triangle.p3, P);

            return(ret);
        }
Exemple #15
0
 public Triangle()
 {
     V1 = new TriangleVertex();
     V2 = new TriangleVertex();
     V3 = new TriangleVertex();
 }
Exemple #16
0
        /// <summary>
        /// Calc the point at which this line intersects with another line.
        /// </summary>
        /// <param name="Other"></param>
        /// <returns></returns>
        public static Point?IntersectPos(LineCoordinates Line1, LineCoordinates Line2)
        {
            // the x-axis range of this line does not intersect at all with the x-axis range
            // of the other line.
            if ((Line1.Start.X > Line2.End.X) || (Line1.End.X < Line2.Start.X))
            {
                return(null);
            }

            // the y-axis range of this line does not intersect at all with the y-axis range
            // of the other line.
            else if ((Line1.TopMost > Line2.BottomMost) || (Line1.BottomMost < Line2.TopMost))
            {
                return(null);
            }

            // lines are perpendicular. They intersect at the x-axis of the vertical line and
            // the y-axis of the horizontal line.
            else if (Line1.IsVertical && Line2.IsHorizontal)
            {
                return(new Point(Line1.Start.X, Line2.Start.Y));
            }
            else if (Line1.IsHorizontal && Line2.IsVertical)
            {
                return(new Point(Line1.Start.X, Line2.Start.Y));
            }

            // start pos of this line matches the start or end pos of the other line.
            else if ((Line1.Start.Equals(Line2.Start)) || (Line1.Start.Equals(Line2.End)))
            {
                return(Line1.Start);
            }

            // end pos of this line matches the start or end pos of the other line.
            else if ((Line1.End.Equals(Line2.Start)) || (Line1.End.Equals(Line2.End)))
            {
                return(Line2.End);
            }

            else
            {
                double i_x = 0.00;
                double i_y = 0.00;
//        char collisionDetected;

                double s1_x, s1_y, s2_x, s2_y;
                s1_x = Line1.End.X - Line1.Start.X;
                s1_y = Line1.End.Y - Line1.Start.Y;
                s2_x = Line2.End.X - Line2.Start.X;
                s2_y = Line2.End.Y - Line2.Start.Y;

                double s, t;
                s = (-s1_y * (Line1.Start.X - Line2.Start.X) + s1_x * (Line1.Start.Y - Line2.Start.Y))
                    / (-s2_x * s1_y + s1_x * s2_y);
                t = (s2_x * (Line1.Start.Y - Line2.Start.Y) - s2_y * (Line1.Start.X - Line2.Start.X))
                    / (-s2_x * s1_y + s1_x * s2_y);

                if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
                {
                    // Collision detected
                    i_x = Line1.Start.X + (t * s1_x);
                    i_y = Line1.Start.Y + (t * s1_y);
                    return(new Point(i_x, i_y));
                }
                else
                {
                    return(null);
                }
            }

#if skip
            var hi = LineCoordinates.HorizontalIntersect(this, Other);

            double         leftMostX = hi.Line1.Start.X + hi.Line1Ofs;
            TriangleVertex vertex1;
            TriangleVertex vertex2;

            // calc the location and angle of the vertex of the intersect triangle formed
            // by line1.
            {
                Point? pos;
                double angle;
                if (hi.Line1Ofs > 0)
                {
                    var adjLine    = new HorizontalLineCoordinates(hi.Line1.Start, hi.Line1Ofs);
                    var oppLine    = this.CalcOppositeSideLine(hi.Line1Ofs);
                    var rtTriangle = new RightTriangle()
                    {
                        AdjSide = adjLine,
                        OppSide = oppLine
                    };
                    pos = rtTriangle.OppVertex.Location;
                }
                else
                {
                    pos = hi.Line1.Start;
                }
                angle   = IntersectPos_CalcVertexAngle(hi.Line1.Angle);
                vertex1 = new TriangleVertex()
                {
                    Angle    = angle,
                    Location = pos.Value
                };
            }

            // calc the left side vertex of the intersect triangle formed by line2.
            {
                Point? pos;
                double angle;
                if (hi.Line2Ofs > 0)
                {
                    var adjLine    = new HorizontalLineCoordinates(hi.Line2.Start, hi.Line2Ofs);
                    var oppLine    = this.CalcOppositeSideLine(hi.Line2Ofs);
                    var rtTriangle = new RightTriangle()
                    {
                        AdjSide = adjLine,
                        OppSide = oppLine
                    };
                    pos = rtTriangle.OppVertex.Location;
                }
                else
                {
                    pos = hi.Line2.Start;
                }
                angle   = IntersectPos_CalcVertexAngle(hi.Line2.Angle);
                vertex2 = new TriangleVertex()
                {
                    Angle    = angle,
                    Location = pos.Value
                };
            }

            // build the oblique triangle that is formed by the intersection of the
            // two lines.
            var obTriangle = new ObliqueTriangle()
            {
                Vertex1 = vertex1,
                Vertex2 = vertex2
            };


            return(null);
        }