123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using CommonUI.Display;
- using UnityEngine;
- using CommonLang;
- namespace CommonUI_Unity3D.Impl
- {
- public struct UnityVertexBuffer : VertexBuffer
- {
- private int mTopology;
- private List<int> mIndices;
- private List<Vector3> mPos;
- private List<Vector2> mUVs;
- public UnityVertexBuffer(int capacity)
- {
- this.mTopology = GL.QUADS;
- this.mIndices = ListObjectPool<int>.AllocAutoRelease(capacity);
- this.mPos = ListObjectPool<Vector3>.AllocAutoRelease(capacity);
- this.mUVs = ListObjectPool<Vector2>.AllocAutoRelease(capacity);
- }
- public void Dispose()
- {
- ListObjectPool<int>.Release(mIndices);
- ListObjectPool<Vector3>.Release(mPos);
- ListObjectPool<Vector2>.Release(mUVs);
- mIndices = null;
- mPos = null;
- mUVs = null;
- }
-
-
-
- public int Count
- {
- get { return mPos.Count; }
- set
- {
- CUtils.SetListSize<Vector3>(mPos, value);
- CUtils.SetListSize<Vector2>(mUVs, value);
- }
- }
- public int IndicesCount
- {
- get
- {
- return mIndices.Count;
- }
- }
-
-
-
-
- public void Append(VertexBuffer data)
- {
- mPos.AddRange(((UnityVertexBuffer)data).mPos);
- mUVs.AddRange(((UnityVertexBuffer)data).mUVs);
- }
- public void Append(float x, float y, uint color, float u, float v)
- {
- mPos.Add(new Vector3(x, y, 0));
- mUVs.Add(new Vector2(u, 1f - v));
- }
- public void Append(VertexPoint vertex)
- {
- mPos.Add(new Vector3(vertex.X, vertex.Y, 0));
- mUVs.Add(new Vector2(vertex.U, 1f - vertex.V));
- }
-
-
-
-
-
- public void SetVertex(int index, VertexPoint vertex)
- {
- this.mPos[index] = new Vector3(vertex.X, vertex.Y, 0);
- this.mUVs[index] = new Vector2(vertex.U, 1f - vertex.V);
- }
-
-
-
-
-
- public void GetVertex(int index, out VertexPoint vertex)
- {
- VertexPoint v = new VertexPoint();
- v.X = mPos[index].x;
- v.Y = mPos[index].y;
- v.U = mUVs[index].x;
- v.V = mUVs[index].y;
- vertex = v;
- }
-
-
-
-
-
-
- public void SetPosition(int index, float x, float y)
- {
- this.mPos[index] = new Vector3(x, y, 0);
- }
-
-
-
-
-
- public void SetColor(int index, uint color)
- {
- }
-
-
-
-
-
-
- public void SetTexCoords(int index, float u, float v)
- {
- this.mUVs[index] = new Vector2(u, 1f - v);
- }
- public void SetIndices(int[] indices, VertexTopology topology)
- {
- if (indices == null)
- {
- throw new Exception("Indices Can not be set to NULL !");
- }
- for (int i = 0; i < indices.Length; i++)
- {
- if (indices[i] < 0 && indices[i] >= mPos.Count)
- {
- throw new Exception("Indices Value Must In Vertex Length range !");
- }
- }
- mIndices.Clear();
- mIndices.AddRange(indices);
- switch (topology)
- {
- case VertexTopology.QUADS:
- mTopology = GL.QUADS;
- break;
- case VertexTopology.LINES:
- mTopology = GL.LINES;
- break;
- case VertexTopology.TRIANGLE_STRIP:
- mTopology = GL.TRIANGLE_STRIP;
- break;
- case VertexTopology.TRIANGLES:
- mTopology = GL.TRIANGLES;
- break;
- }
- }
- public void SetIndices(int index, int[] indices)
- {
- for (int i = 0; i < indices.Length; i++)
- {
- mIndices[i + index] = indices[i];
- }
- }
- public void AddIndicesQuard(int a, int b, int c, int d)
- {
- mIndices.Add(a);
- mIndices.Add(b);
- mIndices.Add(c);
- mIndices.Add(d);
- }
- public void Optimize()
- {
- }
- public object Clone()
- {
- UnityVertexBuffer ret = new UnityVertexBuffer(this.Count);
- ret.Append(this);
- return ret;
- }
- public void Clear()
- {
- this.mUVs.Clear();
- this.mPos.Clear();
- }
- internal void Draw()
- {
- GL.Begin(mTopology);
- for (int i = 0; i < mIndices.Count; i++)
- {
- Vector2 uv = mUVs[mIndices[i]];
- GL.TexCoord2(uv.x, uv.y);
- GL.Vertex(mPos[mIndices[i]]);
- }
- GL.End();
- }
- internal void DrawVertex(int[] indices, int mode)
- {
- GL.Begin(mode);
- for (int i = 0; i < indices.Length; i++)
- {
- Vector2 uv = mUVs[indices[i]];
- GL.TexCoord2(uv.x, uv.y);
- GL.Vertex(mPos[indices[i]]);
- }
- GL.End();
- }
- internal void DrawSequence(int mode)
- {
- GL.Begin(mode);
- for (int i = 0; i < mPos.Count; i++)
- {
- Vector2 uv = mUVs[i];
- GL.TexCoord2(uv.x, uv.y);
- GL.Vertex(mPos[i]);
- }
- GL.End();
- }
- public void translate(float x, float y)
- {
- throw new NotImplementedException();
- }
- public void rotate(float angle)
- {
- throw new NotImplementedException();
- }
- public void scale(float sx, float sy)
- {
- throw new NotImplementedException();
- }
- public void pushTransform()
- {
- throw new NotImplementedException();
- }
- public void popTransform()
- {
- throw new NotImplementedException();
- }
- }
- public struct UnityQuards2D
- {
- public IUnityImageInterface src;
- public float u0, v0, u1, v1;
- public float x0, y0, x1, y1, x2, y2, x3, y3;
- public static UnityQuards2D DrawImage(IUnityImageInterface src, float x, float y)
- {
- UnityQuards2D q = new UnityQuards2D();
- q.src = src;
- q.u0 = 0;
- q.v0 = 1f;
- q.u1 = src.MaxU;
- q.v1 = 1f - src.MaxV;
- q.x0 = x;
- q.y0 = y;
- q.x1 = x + src.Width;
- q.y1 = y;
- q.x2 = x + src.Width;
- q.y2 = y + src.Height;
- q.x3 = x;
- q.y3 = y + src.Height;
- return q;
- }
- public static UnityQuards2D DrawImageZoom(IUnityImageInterface src, float x, float y, float w, float h)
- {
- UnityQuards2D q = new UnityQuards2D();
- q.src = src;
- q.u0 = 0;
- q.v0 = 1f;
- q.u1 = src.MaxU;
- q.v1 = 1f - src.MaxV;
- q.x0 = x;
- q.y0 = y;
- q.x1 = x + w;
- q.y1 = y;
- q.x2 = x + w;
- q.y2 = y + h;
- q.x3 = x;
- q.y3 = y + h;
- return q;
- }
- public static UnityQuards2D DrawImageTrans(IUnityImageInterface src, float x, float y, CommonUI.Display.Trans trans)
- {
- return DrawImageRegion(src, 0, 0, src.Width, src.Height, x, y, src.Width, src.Height, trans);
- }
- public static UnityQuards2D DrawImageRegion(
- IUnityImageInterface src,
- float sx, float sy, float sw, float sh,
- float dx, float dy, float dw, float dh,
- Trans trans)
- {
- float sx2 = sx + sw;
- float sy2 = sy + sh;
- float dx2 = dx + dw;
- float dy2 = dy + dh;
- VertexUtils.TransformTrans(ref sx, ref sy, ref sx2, ref sy2, trans, ref dx, ref dy, ref dx2, ref dy2);
- UnityQuards2D q = new UnityQuards2D();
- q.src = src;
- q.u0 = (sx) * src.MaxU / src.Width;
- q.v0 = 1f - (sy) * src.MaxV / src.Height;
- q.u1 = (sx2) * src.MaxU / src.Width;
- q.v1 = 1f - (sy2) * src.MaxV / src.Height;
- q.x0 = dx;
- q.y0 = dy;
- q.x1 = dx2;
- q.y1 = dy;
- q.x2 = dx2;
- q.y2 = dy2;
- q.x3 = dx;
- q.y3 = dy2;
-
- return q;
- }
- internal void Draw()
- {
- GL.TexCoord2(u0, v0);
- GL.Vertex3(x0, y0, 0);
- GL.TexCoord2(u1, v0);
- GL.Vertex3(x1, y1, 0);
- GL.TexCoord2(u1, v1);
- GL.Vertex3(x2, y2, 0);
- GL.TexCoord2(u0, v1);
- GL.Vertex3(x3, y3, 0);
- }
- }
- }
|