Пример #1
0
    // Use this for initialization
    void Start()
    {
        Color32 color = new Color32(255, 0, 0, 255);

        //color = new Color32();
        UIVertex[] data = new UIVertex[] {
            new UIVertex()
            {
                position = new Vector3(0f, 0f, 0f), color = color
            },
            new UIVertex()
            {
                position = new Vector3(0f, 50f, 0f), color = color
            },
            new UIVertex()
            {
                position = new Vector3(50f, 50f, 50f), color = color
            },
            new UIVertex()
            {
                position = new Vector3(50f, 0f, 50f), color = color
            }
        };

        CanvasRenderer canvasRender = GetComponent <CanvasRenderer>();

        canvasRender.Clear();
        Material defaultMaterial = new Material(Shader.Find("UI/Default"));

        canvasRender.SetMaterial(defaultMaterial, null);
        canvasRender.SetVertices(data, data.Length);
        Debug.Log("start");
    }
Пример #2
0
 public void Draw()
 {
     canvasRender.Clear();
     canvasRender.SetMaterial(wallMaterial, null);
     canvasRender.GetMaterial().color = Color.white;
     canvasRender.SetVertices(vertlist);
     //Debug.Log("cout: " +  list.Count);
 }
 public void DrawSprite(UIVertex[] uiVertices, int spriteCount)
 {
     if (m_inlineGraphicCanvasRenderer == null)
     {
         m_inlineGraphicCanvasRenderer = m_inlineGraphic.GetComponent <CanvasRenderer>();
     }
     m_inlineGraphicCanvasRenderer.SetVertices(uiVertices, spriteCount * 4);
     m_inlineGraphic.UpdateMaterial();
 }
Пример #4
0
    protected override void _drawAxisBackground(List <UIVertex> pVertexList)
    {
        Material pMat = new Material(AxisMaterial);

        pMat.SetColor("_TintColor", MarginBackgroundColor);

        CanvasRenderer pCanvasRenderer = NGraphUtils.AddCanvasRenderer(mAxesBackgroundGo);

        pCanvasRenderer.Clear();
        pCanvasRenderer.SetMaterial(pMat, null);
        pCanvasRenderer.SetVertices(pVertexList);
    }
Пример #5
0
    void MeshRenderTest()
    {
        CanvasRenderer cr   = meshTest.AddComponent <CanvasRenderer>();
        Mesh           mesh = new Mesh();

        mesh.vertices = new Vector3[] {
            new Vector3(0, 0, 0),
            new Vector3(0, 100, 0),
            new Vector3(100, 100, 0),
            new Vector3(100, 0, 0)
        };


        mesh.triangles = new int[] { 0, 1, 2, 2, 3, 0 };
//		mesh.colors = new Color[]{
//			Color.black,
//			Color.blue,
//			Color.cyan,
//			Color.gray
//		};
        cr.Clear();
        cr.SetMaterial(material, null);
//		cr.SetMesh (mesh);
        Vector3[] vertices  = mesh.vertices;
        int[]     triangles = mesh.triangles;
        Vector3[] normals   = mesh.normals;
        Vector2[] uv        = mesh.uv;
        UIVertex  vertex;
//		Debug.Log (uv.Length);
        List <UIVertex> vertexList = new List <UIVertex> ();

        for (int i = 0; i < triangles.Length; i++)
        {
            vertex = new UIVertex();
            int triangle = triangles[i];

            vertex.position = ((vertices[triangle] - mesh.bounds.center) * 1);
            vertex.uv0      = Vector2.one;
            vertex.normal   = Vector3.forward;

            vertexList.Add(vertex);

            if (i % 3 == 0)
            {
                vertexList.Add(vertex);
            }
        }
        cr.SetVertices(vertexList);
    }
Пример #6
0
    public void AddValue(float val)
    {
        if (m_NormalizedGraphValues == null || m_NormalizedGraphValues.Length < m_Resolution)
        {
            m_NormalizedGraphValues = new float[m_Resolution];
        }

        for (int p = m_NormalizedGraphValues.Length - 1; p > 0; p--)
        {
            m_NormalizedGraphValues[p] = m_NormalizedGraphValues[p - 1];
        }

        m_NormalizedGraphValues[0] = val;

        UpdateVerts();

        pCanvasRenderer.Clear();
        pCanvasRenderer.SetVertices(pVertexList);
    }
Пример #7
0
    protected void drawMarker(Vector2 pDataPoint, int dataPointIndex)
    {
        Mesh           pMesh           = null;
        GameObject     pMarkerGo       = NGraphUtils.AddGameObject(mGameObject, NGraph.LEVEL_STEP, "Marker - " + dataPointIndex);
        CanvasRenderer pCanvasRenderer = null;

        if (mGraph.UnityGui)
        {
            pCanvasRenderer = NGraphUtils.AddCanvasRenderer(pMarkerGo);
            Material pMat = new Material(PlotMaterial);
            pMat.SetColor("_TintColor", mMarkerColor);
            pMat.SetVector("_Clipping", new Vector4(mClipping.x, mClipping.y, mClipping.z, mClipping.w));

            pCanvasRenderer.SetMaterial(pMat, null);
        }
        else
        {
            NGraphUtils.AddMesh(pMarkerGo, out mMeshRenderer, out pMesh);
            mMeshRenderer.material = PlotMaterial;
            mMeshRenderer.material.SetColor("_TintColor", mMarkerColor);
            mMeshRenderer.material.SetVector("_Clipping", new Vector4(mClipping.x, mClipping.y, mClipping.z, mClipping.w));
        }
        mMarkerGos.Add(pMarkerGo);

        List <Vector3> pVertices  = new List <Vector3>();
        List <Vector2> pUvs       = new List <Vector2>();
        List <int>     pTriangles = new List <int>();

        switch (MarkersStyle)
        {
        case MarkerStyle.Box:
        {
            /*
             * c      d
             *
             *
             * b      a
             */

            pVertices.Add(new Vector3(mMarkerWeight / 2 + pDataPoint.x, -mMarkerWeight / 2 + pDataPoint.y, 0));
            pVertices.Add(new Vector3(-mMarkerWeight / 2 + pDataPoint.x, -mMarkerWeight / 2 + pDataPoint.y, 0));
            pVertices.Add(new Vector3(-mMarkerWeight / 2 + pDataPoint.x, mMarkerWeight / 2 + pDataPoint.y, 0));
            pVertices.Add(new Vector3(mMarkerWeight / 2 + pDataPoint.x, mMarkerWeight / 2 + pDataPoint.y, 0));
            pUvs.Add(new Vector2(0, 0));
            pUvs.Add(new Vector2(1, 0));
            pUvs.Add(new Vector2(0, 1));
            pUvs.Add(new Vector2(1, 1));

            pTriangles.Add(0);
            pTriangles.Add(1);
            pTriangles.Add(2);

            pTriangles.Add(2);
            pTriangles.Add(3);
            pTriangles.Add(0);

            break;
        }

        case MarkerStyle.Triangle:
        {
            /*
             *    c
             *
             *
             * a      b
             */

            pVertices.Add(new Vector3(pDataPoint.x, pDataPoint.y + mMarkerWeight / 2, 0));
            pVertices.Add(new Vector3(mMarkerWeight / 2 + pDataPoint.x, -mMarkerWeight / 2 + pDataPoint.y, 0));
            pVertices.Add(new Vector3(-mMarkerWeight / 2 + pDataPoint.x, -mMarkerWeight / 2 + pDataPoint.y, 0));
            pUvs.Add(new Vector2(0, 0));
            pUvs.Add(new Vector2(1, 0));
            pUvs.Add(new Vector2(0.5f, 1));
            if (mGraph.UnityGui)
            {
                pVertices.Add(new Vector3(-mMarkerWeight / 2 + pDataPoint.x, -mMarkerWeight / 2 + pDataPoint.y, 0));
                pUvs.Add(new Vector2(0.5f, 1));
            }

            pTriangles.Add(0);
            pTriangles.Add(1);
            pTriangles.Add(2);

            break;
        }
        }

        if (mGraph.UnityGui)
        {
            List <UIVertex> vertices = new List <UIVertex>(pVertices.Count);
            for (int i = 0; i < pVertices.Count; i++)
            {
                UIVertex pVertex = new UIVertex();
                pVertex.position = pVertices[i];
                pVertex.uv0      = pUvs[i];
                vertices.Add(pVertex);
            }
            pCanvasRenderer.SetVertices(vertices);
        }
        else
        {
            pMesh.vertices  = pVertices.ToArray();
            pMesh.uv        = pUvs.ToArray();
            pMesh.triangles = pTriangles.ToArray();
        }
    }
Пример #8
0
        public void ApplyPolygonCustom(List <UIVertex> vbo)
        {
            if (customVectex == null || customVectex.Length < 2)
            {
                return;
            }
            if (abilityValues == null || abilityValues.Length < 2)
            {
                return;
            }
            if (customVectex.Length != abilityValues.Length)
            {
                return;
            }

            UIVertex vert = UIVertex.simpleVert;

            vert.color = (Color32)color;
            UIVertex center = UIVertex.simpleVert;

            center.position = Vector2.zero;
            center.uv0      = new Vector2(0.5f, 0.5f);
            center.color    = (Color32)color;

            UIVertex vt0 = center;
            UIVertex vt1 = vert;
            UIVertex vt2 = vert;
            UIVertex vt3 = vert;

            if (indexPositionArray.Length != abilityValues.Length)
            {
                indexPositionArray = new Vector2[abilityValues.Length];
            }

            for (int i = 0; i < abilityValues.Length; i += 2)
            {
                int tmpIndex = i;

                Vector2 normal    = customVectex[tmpIndex].anchoredPosition - originPoint.anchoredPosition;
                float   tmpRadius = normal.magnitude;
                vert.position = originPoint.anchoredPosition + normal.normalized * abilityValues[tmpIndex] * tmpRadius / maxValue;
                vt1           = vert;
                indexPositionArray[tmpIndex] = vert.position;

                tmpIndex      = (i + 1) % abilityValues.Length;
                normal        = customVectex[tmpIndex].anchoredPosition - originPoint.anchoredPosition;
                tmpRadius     = normal.magnitude;
                vert.position = originPoint.anchoredPosition + normal.normalized * abilityValues[tmpIndex] * tmpRadius / maxValue;
                vt2           = vert;
                indexPositionArray[tmpIndex] = vert.position;

                bool lastTriangleOverflow = false;
                if (i + 2 > abilityValues.Length)
                {
                    lastTriangleOverflow = true;
                    vt3 = center;
                }
                else
                {
                    tmpIndex      = (i + 2) % abilityValues.Length;
                    normal        = customVectex[tmpIndex].anchoredPosition - originPoint.anchoredPosition;
                    tmpRadius     = normal.magnitude;
                    vert.position = originPoint.anchoredPosition + normal.normalized * abilityValues[tmpIndex] * tmpRadius / maxValue;
                    vt3           = vert;
                    indexPositionArray[tmpIndex] = vert.position;
                }

                vbo.Add(vt0);
                vbo.Add(vt1);
                vbo.Add(vt2);
#if UNITY_5 || UNITY_5_3_OR_NEWER
                if (!lastTriangleOverflow)
                {
                    vbo.Add(vt2);
                    vbo.Add(vt3);
                    vbo.Add(vt0);
                }
#else
                vbo.Add(vt3);
#endif
            }

            if (Application.isPlaying && isUseAA)
            {
                int idx = 0;
                for (int i = 0; i < indexPositionArray.Length; i++)
                {
                    var p1 = Vector3.zero;
                    var p2 = Vector3.zero;
                    var v1 = Vector3.zero;
                    var px = Vector3.zero;

                    p1.x = indexPositionArray[i].x;
                    p1.y = indexPositionArray[i].y;
                    p2.x = indexPositionArray[(i + 1) % indexPositionArray.Length].x;
                    p2.y = indexPositionArray[(i + 1) % indexPositionArray.Length].y;
                    px.x = p2.y - p1.y; px.y = p1.x - p2.x;
                    float normalizedDistance = (1.0f / (float)System.Math.Sqrt((px.x * px.x) + (px.y * px.y)));
                    px *= normalizedDistance * borderTex.height / 2;

                    borderUIVertexs[idx].position.x     = p1.x - px.x;
                    borderUIVertexs[idx].position.y     = p1.y - px.y;
                    borderUIVertexs[idx + 3].position.x = p1.x + px.x;
                    borderUIVertexs[idx + 3].position.y = p1.y + px.y;
                    borderUIVertexs[idx + 2].position.x = p2.x + px.x;
                    borderUIVertexs[idx + 2].position.y = p2.y + px.y;
                    borderUIVertexs[idx + 1].position.x = p2.x - px.x;
                    borderUIVertexs[idx + 1].position.y = p2.y - px.y;
                    idx += 4;
                }

                borderCanvasRenderer.SetColor(color);
#if UNITY_5 || UNITY_5_3_OR_NEWER
                Mesh           mesh      = new Mesh();
                List <Vector3> vertices  = new List <Vector3>();
                List <int>     triangles = new List <int>();
                List <Color32> colors    = new List <Color32>();
                List <Vector2> uvs       = new List <Vector2>();
                for (int i = 0; i < abilityValues.Length; i++)
                {
                    vt0 = borderUIVertexs[i * 4 + 0];
                    vt1 = borderUIVertexs[i * 4 + 1];
                    vt2 = borderUIVertexs[i * 4 + 2];
                    vt3 = borderUIVertexs[i * 4 + 3];
                    // position
                    vertices.Add(vt0.position);
                    vertices.Add(vt1.position);
                    vertices.Add(vt2.position);
                    vertices.Add(vt3.position);
                    // triangle
                    triangles.Add(i * 4 + 0);
                    triangles.Add(i * 4 + 1);
                    triangles.Add(i * 4 + 2);
                    triangles.Add(i * 4 + 2);
                    triangles.Add(i * 4 + 3);
                    triangles.Add(i * 4 + 0);
                    // colors
                    colors.Add(vt0.color);
                    colors.Add(vt1.color);
                    colors.Add(vt2.color);
                    colors.Add(vt3.color);
                    // uvs
                    uvs.Add(vt0.uv0);
                    uvs.Add(vt1.uv0);
                    uvs.Add(vt2.uv0);
                    uvs.Add(vt3.uv0);
                }

                mesh.vertices  = vertices.ToArray();
                mesh.triangles = triangles.ToArray();
                mesh.RecalculateNormals();
                // 必须设置color32,设置color是无效的
                mesh.colors32 = colors.ToArray();
                mesh.uv       = uvs.ToArray();
                borderCanvasRenderer.SetMesh(mesh);
#else
                borderCanvasRenderer.SetVertices(borderUIVertexs, abilityValues.Length * 4);
#endif
            }
        }
Пример #9
0
    public static void DrawRect(Rect pRect, CanvasRenderer pCanvasRenderer)
    {
        // don't try to draw zero width of zero height meshes
        if (pRect.xMin == pRect.xMax || pRect.yMin == pRect.yMax)
        {
            return;
        }

        List <UIVertex> pVertexList = new List <UIVertex>(4);
        UIVertex        pUIVertex   = new UIVertex();

        Vector3 a = new Vector3(pRect.xMin, pRect.yMin, 0);
        Vector3 b = new Vector3(pRect.xMax, pRect.yMin, 0);
        Vector3 c = new Vector3(pRect.xMax, pRect.yMax, 0);
        Vector3 d = new Vector3(pRect.xMin, pRect.yMax, 0);

        /* d     c          a      b
         *             |
         *            OR
         *             |
         * a     b          d      c
         */
        if (a.y > d.y)
        {
            pUIVertex.position = a;
        }
        else
        {
            pUIVertex.position = d;
        }
        pUIVertex.uv0 = new Vector2(0, 0);
        pVertexList.Add(pUIVertex);

        if (a.y > d.y)
        {
            pUIVertex.position = b;
        }
        else
        {
            pUIVertex.position = c;
        }
        pUIVertex.uv0 = new Vector2(1, 0);
        pVertexList.Add(pUIVertex);

        if (a.y > d.y)
        {
            pUIVertex.position = c;
        }
        else
        {
            pUIVertex.position = b;
        }
        pUIVertex.uv0 = new Vector2(0, 1);
        pVertexList.Add(pUIVertex);

        if (a.y > d.y)
        {
            pUIVertex.position = d;
        }
        else
        {
            pUIVertex.position = a;
        }
        pUIVertex.uv0 = new Vector2(1, 1);
        pVertexList.Add(pUIVertex);

        pCanvasRenderer.SetVertices(pVertexList);
    }
Пример #10
0
    public static void DrawRect(Rect pRect, CanvasRenderer pCanvasRenderer)
    {
        List <UIVertex> pVertexList = new List <UIVertex>(4);
        UIVertex        pUIVertex   = new UIVertex();

        Vector3 a = new Vector3(pRect.xMin, pRect.yMin, 0);
        Vector3 b = new Vector3(pRect.xMax, pRect.yMin, 0);
        Vector3 c = new Vector3(pRect.xMax, pRect.yMax, 0);
        Vector3 d = new Vector3(pRect.xMin, pRect.yMax, 0);

        /* d     c          a      b
         *             |
         *            OR
         *             |
         * a     b          d      c
         */
        if (a.y > d.y)
        {
            pUIVertex.position = a;
        }
        else
        {
            pUIVertex.position = d;
        }
        pUIVertex.uv0 = new Vector2(0, 0);
        pVertexList.Add(pUIVertex);

        if (a.y > d.y)
        {
            pUIVertex.position = b;
        }
        else
        {
            pUIVertex.position = c;
        }
        pUIVertex.uv0 = new Vector2(1, 0);
        pVertexList.Add(pUIVertex);

        if (a.y > d.y)
        {
            pUIVertex.position = c;
        }
        else
        {
            pUIVertex.position = b;
        }
        pUIVertex.uv0 = new Vector2(0, 1);
        pVertexList.Add(pUIVertex);

        if (a.y > d.y)
        {
            pUIVertex.position = d;
        }
        else
        {
            pUIVertex.position = a;
        }
        pUIVertex.uv0 = new Vector2(1, 1);
        pVertexList.Add(pUIVertex);

        pCanvasRenderer.SetVertices(pVertexList);
    }