public override MeshBase CreateMesh(GridderSource source) { PointGridderSource src = (PointGridderSource)source; PointPositionBuffer positions = new PointPositionBuffer(); PointRadiusBuffer radiusBuffer = null; int dimSize = src.DimenSize; // setup positions unsafe { positions.AllocMem(dimSize); Vertex *cells = (Vertex *)positions.Data; for (int gridIndex = 0; gridIndex < dimSize; gridIndex++) { Vertex p = src.Transform * src.Positions[gridIndex]; cells[gridIndex] = p; } } radiusBuffer = this.CreateRadiusBufferData(src, src.Radius); PointMeshGeometry3D mesh = new PointMeshGeometry3D(positions, radiusBuffer, dimSize); mesh.Max = src.TransformedActiveBounds.Max; mesh.Min = src.TransformedActiveBounds.Min; return(mesh); }
void UpdateBuffers() { unsafe { VertexData vertexData = staticMeshObject.VertexData; HardwareVertexBuffer vertexBuffer = vertexData.VertexBufferBinding.GetBuffer(0); Vertex *buffer = (Vertex *)vertexBuffer.Lock( HardwareBuffer.LockOptions.Normal).ToPointer(); foreach (Vertex vertex in vertices) { *buffer = vertex; buffer++; } vertexBuffer.Unlock(); } unsafe { HardwareIndexBuffer indexBuffer = staticMeshObject.IndexData.IndexBuffer; ushort * buffer = (ushort *)indexBuffer.Lock( HardwareBuffer.LockOptions.Normal).ToPointer(); foreach (int index in indices) { *buffer = (ushort)index; buffer++; } indexBuffer.Unlock(); } }
void Insert(float2 *points, int start, int amount, Entity cid, float4x4 ltw) { Vertex *lastVert = null; var end = start + amount; Vertex *point = null; for (var i = start; i < end; i++) { var c = Math.Mul2D(ltw, points[i]); Assert.IsTrue(_verticesSeq.Length < 5 || Contains(c), PointOutsideNavmeshMessage); var vert = InsertPoint(c); Assert.IsTrue(vert != null); if (i == start) { ++vert->ConstraintHandles; _constraints[cid] = (IntPtr)vert; point = vert; } if (lastVert != null && vert != lastVert) { InsertSegment(lastVert, vert, cid); point = null; } lastVert = vert; } if (point != null) { ++point->PointConstraints; } }
internal EdgeEnumerator(Vertex *v) { Assert.IsTrue(v != null); _start = v->Edge; _started = false; Current = null; }
Edge *RemoveVertex(Vertex *vert) { Assert.IsTrue(vert->Edge != null); Assert.IsTrue(vert->Edge->Org == vert); var remaining = vert->Edge->LNext; InfiniteLoopDetection.Reset(); while (true) { InfiniteLoopDetection.Register(1000, "RemoveVertex"); var e = vert->Edge; if (e == null) { break; } RemoveEdge(e); } V.Remove((IntPtr)vert); Assert.IsTrue(vert->Edge == null); _qt.Remove(vert); var delPos = vert->SeqPos; ((Vertex *)_verticesSeq[_verticesSeq.Length - 1])->SeqPos = delPos; _verticesSeq.RemoveAtSwapBack(delPos); _vertices.Recycle(vert); return(remaining); }
private unsafe void PopulateVertex( Sprite *sprite, Texture texture, Vertex *tl, Vertex *tr, Vertex *bl, Vertex *br) { var w = (sprite->Size.X > 0 ? sprite->Size.X : texture.Width) * sprite->Scale.X; var h = (sprite->Size.Y > 0 ? sprite->Size.Y : texture.Height) * sprite->Scale.Y; var x = sprite->Position.X - sprite->Origin.X * w; var y = sprite->Position.Y - sprite->Origin.Y * h; tl->Position.X = x; tl->Position.Y = y; tl->Color = sprite->Color.Bgra; tl->TextureCoordinate.X = texture.Left; tl->TextureCoordinate.Y = texture.Top; tr->Position.X = x + w; tr->Position.Y = y; tr->Color = sprite->Color.Bgra; tr->TextureCoordinate.X = texture.Right; tr->TextureCoordinate.Y = texture.Top; bl->Position.X = x; bl->Position.Y = y + h; bl->Color = sprite->Color.Bgra; bl->TextureCoordinate.X = texture.Left; bl->TextureCoordinate.Y = texture.Bottom; br->Position.X = x + w; br->Position.Y = y + h; br->Color = sprite->Color.Bgra; br->TextureCoordinate.X = texture.Right; br->TextureCoordinate.Y = texture.Bottom; }
public unsafe void AddParticle(Vector2 position, Vector2 size, Color color) { fixed(Vertex *ptr = &this.vertecies[elementCount * 4]) { Vertex *ptr2 = ptr; ptr2->Position = new Vector2(position.X, position.Y); ptr2->Tint = color; ptr2++; ptr2->Position = new Vector2(position.X + size.X, position.Y); ptr2->Tint = color; ptr2++; ptr2->Position = new Vector2(position.X + size.X, position.Y + size.Y); ptr2->Tint = color; ptr2++; ptr2->Position = new Vector2(position.X, position.Y + size.Y); ptr2->Tint = color; } this.elementCount++; if (elementCount > maxSize) { throw new ArgumentException(); } }
void UpdateMeshVertices() { SubMesh subMesh = mesh.SubMeshes[0]; Vec2 cellSize = 1.0f / Type.GridSize.ToVec2(); HardwareVertexBuffer vertexBuffer = subMesh.VertexData.VertexBufferBinding.GetBuffer(0); unsafe { Vertex *buffer = (Vertex *)vertexBuffer.Lock( HardwareBuffer.LockOptions.Normal).ToPointer(); subMesh.VertexData.VertexCount = (Type.GridSize.X + 1) * (Type.GridSize.Y + 1); for (int y = 0; y < Type.GridSize.Y + 1; y++) { for (int x = 0; x < Type.GridSize.X + 1; x++) { Vertex vertex = new Vertex(); vertex.position = new Vec3(0, (float)(x - Type.GridSize.X / 2) * cellSize.X, (float)(y - Type.GridSize.Y / 2) * cellSize.Y); vertex.normal = new Vec3(1, 0, 0); vertex.texCoord = new Vec2((float)x / (float)Type.GridSize.X, 1.0f - (float)y / (float)Type.GridSize.Y); *buffer = vertex; buffer++; } } vertexBuffer.Unlock(); } }
internal static Vertex *nk_draw_vertex(Vertex *dst, Vector2 pos, Vector2 uv, Colorf color) { dst->Position = new Vector2(pos.X, pos.Y); dst->Color = color.ToColor(); dst->TextureCoordinate = new Vector2(uv.X, uv.Y); return(dst + 1); }
/// <summary> /// RAM pointer setup /// </summary> public SimpleMesh(MaterialLib materials, Vertex *vertices, int *indices, int vertexCount, int indexCount) { Materials = materials; Vertices = vertices; Indices = indices; VertexCount = vertexCount; IndexCount = indexCount; }
public Step(Vertex *vertex, int stepId, float g, float h, int previous, Edge *edge) { Vertex = vertex; Id = stepId; G = g; _gPlusH = g + h; Previous = previous; Edge = edge; }
unsafe static Vertex InterpolateVertexInTriangleEdge(Vertex *vt, int e0, int e1, float t) { Vertex iv = vt[0]; iv.position.x = vt[e0].position.x + t * (vt[e1].position.x - vt[e0].position.x); iv.position.y = vt[e0].position.y + t * (vt[e1].position.y - vt[e0].position.y); iv.tint = Color.LerpUnclamped(vt[e0].tint, vt[e1].tint, t); iv.uv = Vector2.LerpUnclamped(vt[e0].uv, vt[e1].uv, t); return(iv); }
private unsafe static Vertex InterpolateVertexInTriangle(Vertex *vt, float x, float y, Vector3 uvw) { Vertex result = *vt; result.position.x = x; result.position.y = y; result.tint = vt->tint * uvw.x + vt[1].tint * uvw.y + vt[2].tint * uvw.z; result.uv = vt->uv * uvw.x + vt[1].uv * uvw.y + vt[2].uv * uvw.z; return(result); }
private unsafe static Vertex InterpolateVertexInTriangleEdge(Vertex *vt, int e0, int e1, float t) { Vertex result = *vt; result.position.x = vt[e0].position.x + t * (vt[e1].position.x - vt[e0].position.x); result.position.y = vt[e0].position.y + t * (vt[e1].position.y - vt[e0].position.y); result.tint = Color.LerpUnclamped(vt[e0].tint, vt[e1].tint, t); result.uv = Vector2.LerpUnclamped(vt[e0].uv, vt[e1].uv, t); return(result); }
unsafe static Vertex InterpolateVertexInTriangle(Vertex *vt, float x, float y, Vector3 uvw) { Vertex iv = vt[0]; iv.position.x = x; iv.position.y = y; iv.tint = (Color)vt[0].tint * uvw.x + (Color)vt[1].tint * uvw.y + (Color)vt[2].tint * uvw.z; iv.uv = vt[0].uv * uvw.x + vt[1].uv * uvw.y + vt[2].uv * uvw.z; return(iv); }
static unsafe void Remap_Internal(Vertex *vertices, int vertexCount, float maxDimension, ref float3 dimensionFlips, ref int3 result) { float3 minimum = vertices[0].Position; float3 maximum = vertices[0].Position; for (int i = 1; i < vertexCount; i++) { float3 v = vertices[i].Position; minimum = min(v, minimum); maximum = max(v, maximum); } float3 size = maximum - minimum; float scale = maxDimension / cmax(size); result = new int3( Mathf.NextPowerOfTwo((int)(size.x * scale)), Mathf.NextPowerOfTwo((int)(size.y * scale)), Mathf.NextPowerOfTwo((int)(size.z * scale)) ); for (int i = 0; i < vertexCount; i++) { (vertices + i)->Position = (vertices[i].Position - minimum) * scale; } float3 flipScales = result; if (dimensionFlips.x < 1f) { for (int i = 0; i < vertexCount; i++) { float3 *v = &(vertices + i)->Position; v->x = flipScales.x - v->x; } } if (dimensionFlips.y < 1f) { for (int i = 0; i < vertexCount; i++) { float3 *v = &(vertices + i)->Position; v->y = flipScales.y - v->y; } } if (dimensionFlips.z < 1f) { for (int i = 0; i < vertexCount; i++) { float3 *v = &(vertices + i)->Position; v->z = flipScales.z - v->z; } } }
public void Insert(Vertex *v) { var p = v->Point; var node = _root; while (!node->IsLeaf) { node = node->GetChild(p); } while (node->Count == _bucketSize) { var hs = node->HalfSize / 2; var o = node->Origin; node->BL = _nodes.Set(new QuadTreeNode(o - hs, hs, node->Data, node)); node->TL = _nodes.Set(new QuadTreeNode(o + new float2(-hs, hs), hs, GetChunk(), node)); node->BR = _nodes.Set(new QuadTreeNode(o + new float2(hs, -hs), hs, GetChunk(), node)); node->TR = _nodes.Set(new QuadTreeNode(o + hs, hs, GetChunk(), node)); node->Count = 0; for (int i = 0; i < _bucketSize; i++) { var pt = node->Data[i]->Point; if (pt.x < o.x) { if (pt.y < o.y) { node->BL->Data[node->BL->Count++] = node->BL->Data[i]; } else { node->TL->Data[node->TL->Count++] = node->BL->Data[i]; } } else { if (pt.y < o.y) { node->BR->Data[node->BR->Count++] = node->BL->Data[i]; } else { node->TR->Data[node->TR->Count++] = node->BL->Data[i]; } } } node = node->GetChild(p); } node->Data[node->Count++] = v; }
static Edge *GetConnection(Vertex *a, Vertex *b) { var e = a->GetEdgeEnumerator(); while (e.MoveNext()) { if (e.Current->Dest == b) { return(e.Current); } } return(null); }
void RemoveSemiCollinear(Vertex *v, Edge *e1, Edge *e2) { var crep = GetCrep(e1->QuadEdge->Crep); var a = e1->Dest; var b = e2->Dest; e1->QuadEdge->Crep.Clear(); e2->QuadEdge->Crep.Clear(); _flipStack.Push(e1); _flipStack.Push(e2); FlipQuad(); var face1 = RemoveVertex(v); RetriangulateFace(face1); InsertSegmentNoConstraints(a, b, crep); }
private unsafe void PopulateVertexWithRotationAndTransform( Sprite *sprite, Texture texture, Vertex *tl, Vertex *tr, Vertex *bl, Vertex *br, Matrix3x2 transform) { var w = (sprite->Size.X > 0 ? sprite->Size.X : texture.Width) * sprite->Scale.X; var h = (sprite->Size.Y > 0 ? sprite->Size.Y : texture.Height) * sprite->Scale.Y; var x = sprite->Position.X; var y = sprite->Position.Y; var dx = -sprite->Origin.X * w; var dy = -sprite->Origin.Y * h; var radius = MathHelper.ToRadius(sprite->Rotation); var cos = Math.Cos(radius); var sin = Math.Sin(radius); tl->Position.X = (float)(x + dx * cos - dy * sin); tl->Position.Y = (float)(y + dx * sin + dy * cos); tl->Color = sprite->Color.Bgra; tl->TextureCoordinate.X = texture.Left; tl->TextureCoordinate.Y = texture.Top; tr->Position.X = (float)(x + (dx + w) * cos - dy * sin); tr->Position.Y = (float)(y + (dx + w) * sin + dy * cos); tr->Color = sprite->Color.Bgra; tr->TextureCoordinate.X = texture.Right; tr->TextureCoordinate.Y = texture.Top; bl->Position.X = (float)(x + dx * cos - (dy + h) * sin); bl->Position.Y = (float)(y + dx * sin + (dy + h) * cos); bl->Color = sprite->Color.Bgra; bl->TextureCoordinate.X = texture.Left; bl->TextureCoordinate.Y = texture.Bottom; br->Position.X = (float)(x + (dx + w) * cos - (dy + h) * sin); br->Position.Y = (float)(y + (dx + w) * sin + (dy + h) * cos); br->Color = sprite->Color.Bgra; br->TextureCoordinate.X = texture.Right; br->TextureCoordinate.Y = texture.Bottom; tl->Position = Vector2.Transform(tl->Position, transform); tr->Position = Vector2.Transform(tr->Position, transform); bl->Position = Vector2.Transform(bl->Position, transform); br->Position = Vector2.Transform(br->Position, transform); }
private NativeArray <Vertex> InitializeVertexBuffer() { Vector3[] normal = sampleMesh.normals; Vector4[] tangent = sampleMesh.tangents; NativeArray <Vertex> verts = new NativeArray <Vertex>(normal.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); Vertex *ptr = verts.Ptr(); int seed = Guid.NewGuid().GetHashCode(); Random rand = new Random(*(uint *)UnsafeUtility.AddressOf(ref seed)); for (int i = 0; i < verts.Length; ++i) { ref Vertex vert = ref ptr[i]; vert.tangent = normalize((Vector3)tangent[i]); vert.normal = normalize(normal[i]); vert.p = rand.NextUInt2(); vert.binormal = normalize(cross(vert.normal, vert.tangent) * tangent[i].w); }
public void Update() { if ((VBOSize == Vertices.Count) && (Vertices.Count <= 0)) { return; } GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit); try { GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOID); if (VBOSize != Vertices.Count) // need to create new buffer { unsafe { Vertex[] Mem = Vertices.ToArray(); fixed(Vertex *MemPtr = Mem) { GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(Vertices.Count * Vertex.StructSize), (IntPtr)MemPtr, BufferUsageHint.StreamCopy); } } RenderingException.FromGLError(); VBOSize = Vertices.Count; } else // size matches, just update the items (no need to create new buffer) { IntPtr VMemIntPtr = GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly); RenderingException.FromGLError(); unsafe { Vertex *VMemPtr = (Vertex *)VMemIntPtr.ToPointer(); for (int i = 0; i < Vertices.Count; i++) { (*(VMemPtr + i)) = Vertices[i]; } } GL.UnmapBuffer(BufferTarget.ArrayBuffer); } } finally { GL.PopClientAttrib(); } }
bool TriDisturbed(Edge *tri, out Vertex *vRef) { var edge = tri; if (!edge->Constrained) { if (RhsDisturbed(edge, out vRef)) { return(true); } if (LhsDisturbed(edge->Sym, out vRef)) { return(true); } } edge = tri->LNext; if (!edge->Constrained) { if (RhsDisturbed(edge, out vRef)) { return(true); } if (LhsDisturbed(edge->Sym, out vRef)) { return(true); } } edge = tri->LNext->LNext; if (!edge->Constrained) { if (RhsDisturbed(edge, out vRef)) { return(true); } if (LhsDisturbed(edge->Sym, out vRef)) { return(true); } } vRef = default; return(false); }
void Connect(Vertex *a, Vertex *b, UnsafeList crep) { var connection = GetConnection(a, b); if (connection == null) { V.TryAdd((IntPtr)a); V.TryAdd((IntPtr)b); connection = Connect(a, b); RetriangulateFace(connection); RetriangulateFace(connection->Sym); } connection->QuadEdge->Crep = crep; ResetClearance(connection); C.TryAdd((IntPtr)connection); }
bool RhsDisturbed(Edge *edge, out Vertex *vRef) { edge->ClearanceRight = -1; if (!edge->ONext->Constrained && !edge->DNext->Constrained) { var a = edge->ONext->Dest->Point; var b = edge->Org->Point; var c = edge->Dest->Point; if (CheckTraversal(a, b, c, edge, false, out var disturbance)) { vRef = InsertPointInEdge(disturbance.PRef, disturbance.Edge); return(true); } } vRef = default; return(false); }
Edge *CreateEdge(Vertex *a, Vertex *b) { var q = _quadEdges.Set(new QuadEdge { Crep = GetCrep(), Id = NextEdgeId }); q->Edge0 = new Edge(q, 0); q->Edge1 = new Edge(q, 1); q->Edge2 = new Edge(q, 2); q->Edge3 = new Edge(q, 3); q->Edge0.Next = &q->Edge0; q->Edge1.Next = &q->Edge3; q->Edge2.Next = &q->Edge2; q->Edge3.Next = &q->Edge1; SetEndPoints(&q->Edge0, a, b); return(&q->Edge0); }
public Vertex *FindClosest(float2 p, float rangeSq = float.MaxValue) { var node = _root; while (!node->IsLeaf) { node = node->GetChild(p); } Vertex *closest = null; var dist = rangeSq; for (int i = 0; i < node->Count; i++) { var v = node->Data[i]; var d = math.lengthsq(v->Point - p); if (d < dist) { closest = v; dist = d; } } if (node->Parent != null) { var dd = math.sqrt(dist) / 2; var min = p - dd; var max = p + dd; if (math.any(min < node->Origin - node->HalfSize) || math.any(max >= node->Origin + node->HalfSize)) { do { node = node->Parent; }while (node->Parent != null && (math.any(min < node->Origin - node->HalfSize) || math.any(max >= node->Origin + node->HalfSize))); FindClosest(node, p, min, max, ref dist, ref closest); } } return(closest); }
public void Remove(Vertex *v) { var p = v->Point; var node = _root; while (!node->IsLeaf) { node = node->GetChild(p); } for (int i = 0; i < node->Count; i++) { if (node->Data[i] == v) { node->Data[i] = node->Data[--node->Count]; break; } Assert.IsTrue(i != node->Count - 1); } var parent = node->Parent; while (node->Count == 0 && parent != null && parent->BL->IsLeaf && parent->BL->Count == 0 && parent->TL->IsLeaf && parent->TL->Count == 0 && parent->BR->IsLeaf && parent->BR->Count == 0 && parent->TR->IsLeaf && parent->TR->Count == 0) { parent->Data = parent->BL->Data; _nodes.Recycle(parent->BL); parent->BL = null; _chunks.Push((IntPtr)parent->TL->Data); _nodes.Recycle(parent->TL); _chunks.Push((IntPtr)parent->BR->Data); _nodes.Recycle(parent->BR); _chunks.Push((IntPtr)parent->TR->Data); _nodes.Recycle(parent->TR); node = parent; parent = node->Parent; } }
public override MeshBase CreateMesh(GridderSource source) { PointGridderSource src = (PointGridderSource)source; Vertex minVertex = new Vertex(); Vertex maxVertex = new Vertex(); bool isSet = false; PointPositionBuffer positions = new PointPositionBuffer(); PointRadiusBuffer radiusBuffer = null; int dimSize = src.DimenSize; Random random = new Random(); // setup positions unsafe { positions.AllocMem(dimSize); Vertex *cells = (Vertex *)positions.Data; for (int gridIndex = 0; gridIndex < dimSize; gridIndex++) { Vertex p = src.Positions[gridIndex]; cells[gridIndex] = p; if (!isSet) { minVertex = p; maxVertex = p; isSet = true; } if (src.IsActiveBlock(gridIndex)) { minVertex = SimLab.SimGrid.helper.VertexHelper.MinVertex(minVertex, p); maxVertex = SimLab.SimGrid.helper.VertexHelper.MaxVertex(maxVertex, p); } } } radiusBuffer = this.CreateRadiusBufferData(src, src.Radius); PointMeshGeometry3D mesh = new PointMeshGeometry3D(positions, radiusBuffer, dimSize); mesh.Max = maxVertex; mesh.Min = minVertex; return(mesh); }
void InsertSegmentNoConstraints(Vertex *a, Vertex *b, Entity id) { var c = GetConnection(a, b); if (c != null) { C.TryAdd((IntPtr)c); if (!c->IsConstrainedBy(id)) { c->AddConstraint(id); } ResetClearance(c); return; } var e = GetLeftEdge(a, b->Point); InfiniteLoopDetection.Reset(); while (e->Dest != b) { InfiniteLoopDetection.Register(1000, "InsertSegmentNoConstraints"); var d = Math.TriArea(a->Point, b->Point, e->Dest->Point); var next = d > 0 ? e->RPrev : e->ONext; if (d < 0) { Assert.IsTrue(!e->Constrained); RemoveEdge(e); } else if (d == 0 && e->Dest != a) { var t = e->Dest; Connect(a, t, id); a = t; } e = next; } Connect(a, b, id); }