void OnGenerateVisualContent(MeshGenerationContext mgc) { Rect r = contentRect; if (r.width < 0.01f || r.height < 0.01f) { return; // Skip rendering when too small. } Color color = resolvedStyle.color; k_Vertices[0].tint = Color.black; k_Vertices[1].tint = Color.black; k_Vertices[2].tint = color; k_Vertices[3].tint = color; float left = 0; float right = r.width; float top = 0; float bottom = r.height; k_Vertices[0].position = new Vector3(left, bottom, Vertex.nearZ); k_Vertices[1].position = new Vector3(left, top, Vertex.nearZ); k_Vertices[2].position = new Vector3(right, top, Vertex.nearZ); k_Vertices[3].position = new Vector3(right, bottom, Vertex.nearZ); MeshWriteData mwd = mgc.Allocate(k_Vertices.Length, k_Indices.Length); mwd.SetAllVertices(k_Vertices); mwd.SetAllIndices(k_Indices); }
private void DrawPreviewImage(MeshGenerationContext mgc) { if (m_PreviewImage == null) { return; } ComputePreviewImageHalfSizeAndOffset(out Vector2 halfSize, out Vector2 offset); var meshWriteData = mgc.Allocate(4, 6, m_PreviewImage, m_PreviewMaterial, MeshGenerationContext.MeshFlags.None); var vertices = new Vertex[4]; vertices[0].position = new Vector3(-halfSize.x, -halfSize.y, Vertex.nearZ); vertices[0].tint = Color.white; vertices[1].position = new Vector3(halfSize.x, -halfSize.y, Vertex.nearZ); vertices[1].tint = Color.white; vertices[2].position = new Vector3(halfSize.x, halfSize.y, Vertex.nearZ); vertices[2].tint = Color.white; vertices[3].position = new Vector3(-halfSize.x, halfSize.y, Vertex.nearZ); vertices[3].tint = Color.white; SimulatorUtilities.SetTextureCoordinates(TargetOrientation, vertices); SimulatorUtilities.TransformVertices(m_Rotation, offset, vertices); meshWriteData.SetAllVertices(vertices); var indices = new ushort[] { 0, 3, 1, 1, 3, 2 }; meshWriteData.SetAllIndices(indices); }
void OnGenerateVisualContent(MeshGenerationContext mgc) { Rect r = contentRect; if (r.width < 0.01f || r.height < 0.01f) { return; // Skip rendering when too small. } float left = 0; float right = r.width; float top = 0; float bottom = r.height; k_Vertices[0].position = new Vector3(left, bottom, Vertex.nearZ); k_Vertices[1].position = new Vector3(left, top, Vertex.nearZ); k_Vertices[2].position = new Vector3(right, top, Vertex.nearZ); k_Vertices[3].position = new Vector3(right, bottom, Vertex.nearZ); MeshWriteData mwd = mgc.Allocate(k_Vertices.Length, k_Indices.Length, m_Texture); // Remap 0..1 to the uv region. Rect uvs = mwd.uvRegion; k_Vertices[0].uv = new Vector2(uvs.xMin, uvs.yMin); k_Vertices[1].uv = new Vector2(uvs.xMin, uvs.yMax); k_Vertices[2].uv = new Vector2(uvs.xMax, uvs.yMax); k_Vertices[3].uv = new Vector2(uvs.xMax, uvs.yMin); mwd.SetAllVertices(k_Vertices); mwd.SetAllIndices(k_Indices); }
void OnGenerateVisualContent(MeshGenerationContext mgc) { Rect r = contentRect; if (r.width < 0.01f || r.height < 0.01f) { return; // Skip rendering when too small. } float radiusX = r.width / 2; float radiusY = r.height / 2; k_Vertices[0].position = new Vector3(radiusX, radiusY, Vertex.nearZ); float angle = 0; for (int i = 1; i < 7; ++i) { k_Vertices[i].position = new Vector3( radiusX + radiusX * Mathf.Cos(angle), radiusY - radiusY * Mathf.Sin(angle), Vertex.nearZ); angle += 2f * Mathf.PI / 6; } MeshWriteData mwd = mgc.Allocate(k_Vertices.Length, k_Indices.Length); mwd.SetAllVertices(k_Vertices); mwd.SetAllIndices(k_Indices); }
private void DrawLine(MeshGenerationContext ctx, Vector2 startPos, Vector2 endPos, Color color, float thickness) { var mesh = ctx.Allocate(6, 6); var dir = (endPos - startPos).normalized; var startCenter = new Vector3(startPos.x, startPos.y, Vertex.nearZ); var endCenter = new Vector3(endPos.x, endPos.y, Vertex.nearZ); var normal = new Vector3(-dir.y, dir.x, 0f); var halfThickness = thickness * 0.5f; var vertices = new Vertex[6]; vertices[0].position = startCenter + normal * halfThickness; vertices[1].position = startCenter - normal * halfThickness; vertices[2].position = endCenter + normal * halfThickness; vertices[3].position = startCenter - normal * halfThickness; vertices[4].position = endCenter - normal * halfThickness; vertices[5].position = endCenter + normal * halfThickness; vertices[0].tint = color; vertices[1].tint = color; vertices[2].tint = color; vertices[3].tint = color; vertices[4].tint = color; vertices[5].tint = color; mesh.SetAllVertices(vertices); mesh.SetAllIndices(new ushort[] { 0, 1, 2, 3, 4, 5 }); }
private void DrawTriangle(MeshGenerationContext ctx, Vector2 startPos, Vector2 endPos, Color color, float thickness) { var mesh = ctx.Allocate(3, 3); var length = thickness * 4f; var width = thickness * 3f; var distAlongLine = 1f; var dir = (endPos - startPos).normalized; var dist = (endPos - startPos).magnitude; var center = new Vector3(startPos.x + dir.x * (dist * distAlongLine - length), startPos.y + dir.y * (dist * distAlongLine - length), Vertex.nearZ); var normal = new Vector3(-dir.y, dir.x, 0f); var vertices = new Vertex[3]; vertices[0].position = center + normal * width; vertices[1].position = center - normal * width; vertices[2].position = new Vector3(center.x + dir.x * length, center.y + dir.y * length, center.z); vertices[0].tint = color; vertices[1].tint = color; vertices[2].tint = color; mesh.SetAllVertices(vertices); mesh.SetAllIndices(new ushort[] { 0, 1, 2 }); }
public void GenerateTickHeadContent(MeshGenerationContext context) { const float tipY = 20f; const float height = 6f; const float width = 11f; const float middle = 6f; Color color; if (EditorGUIUtility.isProSkin) { color = m_DebugPlayhead ? ColorUtility.FromHtmlString("#234A6C") : Color.white; } else { color = m_DebugPlayhead ? ColorUtility.FromHtmlString("#2E5B8D") : Color.white; } MeshWriteData mesh = context.Allocate(3, 3); Vertex[] vertices = new Vertex[3]; vertices[0].position = new Vector3(middle, tipY, Vertex.nearZ); vertices[1].position = new Vector3(width, tipY - height, Vertex.nearZ); vertices[2].position = new Vector3(0, tipY - height, Vertex.nearZ); vertices[0].tint = color; vertices[1].tint = color; vertices[2].tint = color; mesh.SetAllVertices(vertices); mesh.SetAllIndices(new ushort[] { 0, 2, 1 }); }
private void DrawHighlightSafeArea(MeshGenerationContext mgc) { if (!m_HighlightSafeArea) { return; } var safeAreaInScreen = GetSafeAreaInScreen(); var halfImageWidth = safeAreaInScreen.width / 2; var halfImageHeight = safeAreaInScreen.height / 2; const int vertexCount = 8, indexCount = 24; var meshWriteData = mgc.Allocate(vertexCount, indexCount); var vertices = new Vertex[vertexCount]; var highlightColor = Color.green; const int highlightLineWidth = 2; vertices[0].position = new Vector3(-halfImageWidth, -halfImageHeight, Vertex.nearZ); vertices[0].tint = highlightColor; vertices[1].position = new Vector3(halfImageWidth, -halfImageHeight, Vertex.nearZ); vertices[1].tint = highlightColor; vertices[2].position = new Vector3(halfImageWidth, halfImageHeight, Vertex.nearZ); vertices[2].tint = highlightColor; vertices[3].position = new Vector3(-halfImageWidth, halfImageHeight, Vertex.nearZ); vertices[3].tint = highlightColor; vertices[4].position = new Vector3(-halfImageWidth + highlightLineWidth, -halfImageHeight + highlightLineWidth, Vertex.nearZ); vertices[4].tint = highlightColor; vertices[5].position = new Vector3(halfImageWidth - highlightLineWidth, -halfImageHeight + highlightLineWidth, Vertex.nearZ); vertices[5].tint = highlightColor; vertices[6].position = new Vector3(halfImageWidth - highlightLineWidth, halfImageHeight - highlightLineWidth, Vertex.nearZ); vertices[6].tint = highlightColor; vertices[7].position = new Vector3(-halfImageWidth + highlightLineWidth, halfImageHeight - highlightLineWidth, Vertex.nearZ); vertices[7].tint = highlightColor; var offset = new Vector2(m_Offset.x + safeAreaInScreen.x / 2, m_Offset.y + safeAreaInScreen.y / 2); offset = ComputeOffsetForScreenMode(offset, true); SimulatorUtilities.TransformVertices(ComputeRotationForHighlightSafeArea(), offset, vertices); meshWriteData.SetAllVertices(vertices); var indices = new ushort[] { 0, 4, 1, 1, 4, 5, 1, 5, 6, 1, 6, 2, 6, 7, 2, 7, 3, 2, 0, 3, 4, 3, 7, 4 }; meshWriteData.SetAllIndices(indices); }
private void DrawEdge(MeshGenerationContext cxt) { if (state == State.None) { return; } uint vertexCount = RENDER_POINT_COUNT * 2; uint indexCount = (vertexCount - 2) * 3; var mesh = cxt.Allocate((int)vertexCount, (int)indexCount, null); Vector3 normal = default; for (int i = 0; i < RENDER_POINT_COUNT; i++) { if (i < RENDER_POINT_COUNT - 1) { if (i > 0) { normal = (renderPoints[i + 1].position - renderPoints[i - 1].position).normalized * actualHalfWidth; } else { normal = (renderPoints[i + 1].position - renderPoints[i].position).normalized * actualHalfWidth; } var tmp = normal.x; normal.x = normal.y; normal.y = -tmp; } var p = renderPoints[i]; var v0 = GetVertex(p.position + normal, p.color); var v1 = GetVertex(p.position - normal, p.color); mesh.SetNextVertex(v0); mesh.SetNextVertex(v1); } for (int i = 0; i < RENDER_POINT_COUNT - 1; i++) { mesh.SetNextIndex((ushort)(0 + i * 2)); mesh.SetNextIndex((ushort)(2 + i * 2)); mesh.SetNextIndex((ushort)(1 + i * 2)); mesh.SetNextIndex((ushort)(1 + i * 2)); mesh.SetNextIndex((ushort)(2 + i * 2)); mesh.SetNextIndex((ushort)(3 + i * 2)); } }
private void DrawDeviceImage(MeshGenerationContext mgc) { if (m_DeviceInfo.Meta.overlayImage == null) { DrawDeviceBorder(mgc); return; } var scale = m_Scale / 100f; var halfImageWidth = scale * m_DeviceInfo.Screens[0].width / 2; var halfImageHeight = scale * m_DeviceInfo.Screens[0].height / 2; var leftWidth = halfImageWidth + scale * m_DeviceInfo.Meta.overlayOffset.x; var topWidth = halfImageHeight + scale * m_DeviceInfo.Meta.overlayOffset.y; var rightWidth = halfImageWidth + scale * m_DeviceInfo.Meta.overlayOffset.z; var bottomWidth = halfImageHeight + scale * m_DeviceInfo.Meta.overlayOffset.w; var meshWriteData = mgc.Allocate(4, 6, m_DeviceInfo.Meta.overlayImage); var vertices = new Vertex[4]; vertices[0].position = new Vector3(-leftWidth, -topWidth, Vertex.nearZ); vertices[0].tint = Color.white; vertices[0].uv = new Vector2(0, 1); vertices[1].position = new Vector3(rightWidth, -topWidth, Vertex.nearZ); vertices[1].tint = Color.white; vertices[1].uv = new Vector2(1, 1); vertices[2].position = new Vector3(rightWidth, bottomWidth, Vertex.nearZ); vertices[2].tint = Color.white; vertices[2].uv = new Vector2(1, 0); vertices[3].position = new Vector3(-leftWidth, bottomWidth, Vertex.nearZ); vertices[3].tint = Color.white; vertices[3].uv = new Vector2(0, 0); SimulatorUtilities.TransformVertices(m_Rotation, m_Offset, vertices); meshWriteData.SetAllVertices(vertices); var indices = new ushort[] { 0, 3, 1, 1, 3, 2 }; meshWriteData.SetAllIndices(indices); }
void Quad(Vector2 pos, Vector2 size, Color color, Texture2D texture2D, MeshGenerationContext context) { var mesh = context.Allocate(4, 6, texture2D); var x0 = pos.x; var y0 = pos.y; var x1 = pos.x + size.x; var y1 = pos.y + size.y; mesh.SetNextVertex(new Vertex() { position = new Vector3(x0, y0, Vertex.nearZ), tint = color, uv = new Vector2(0, 0) }); mesh.SetNextVertex(new Vertex() { position = new Vector3(x1, y0, Vertex.nearZ), tint = color, uv = new Vector2(1, 0) }); mesh.SetNextVertex(new Vertex() { position = new Vector3(x0, y1, Vertex.nearZ), tint = color, uv = new Vector2(0, 1) }); mesh.SetNextVertex(new Vertex() { position = new Vector3(x1, y1, Vertex.nearZ), tint = color, uv = new Vector2(1, 1) }); mesh.SetNextIndex(0); mesh.SetNextIndex(1); mesh.SetNextIndex(2); mesh.SetNextIndex(1); mesh.SetNextIndex(3); mesh.SetNextIndex(2); }
public static void Circle(Vector2 pos, float radius, float startAngle, float endAngle, Color color, MeshGenerationContext context) { var segments = 50; var mesh = context.Allocate(segments + 1, segments * 3); mesh.SetNextVertex(new Vertex() { position = new Vector3(pos.x, pos.y, -1), tint = color }); var angle = startAngle; var range = endAngle - startAngle; var step = range / (segments - 1); // store off the first position Vector3 offset = new Vector3(radius * Mathf.Cos(angle), radius * Mathf.Sin(angle)); mesh.SetNextVertex(new Vertex() { position = new Vector3(pos.x, pos.y, -1) + offset, tint = color }); // calculate the rest of the arc/circle for (var i = 0; i < segments - 1; i++) { angle += step; offset = new Vector3(radius * Mathf.Cos(angle), radius * Mathf.Sin(angle)); mesh.SetNextVertex(new Vertex() { position = new Vector3(pos.x, pos.y, -1) + offset, tint = color }); } for (ushort i = 1; i < segments; i++) { mesh.SetNextIndex(0); mesh.SetNextIndex((ushort)(i + 1)); mesh.SetNextIndex(i); } }
void Quad(Vector2 pos, Vector2 size, Color color, Texture2D texture2D, MeshGenerationContext context) { var mesh = context.Allocate(4, 6, texture2D); var x0 = pos.x; var y0 = pos.y; var x1 = pos.x + size.x; var y1 = pos.y + size.y; mesh.SetNextVertex(new Vertex() { position = new Vector3(x0, y0, Vertex.nearZ), tint = color, uv = new Vector2(0, 1) * mesh.uvRegion.size + mesh.uvRegion.position }); mesh.SetNextVertex(new Vertex() { position = new Vector3(x1, y0, Vertex.nearZ), tint = color, uv = new Vector2(1, 1) * mesh.uvRegion.size + mesh.uvRegion.position }); mesh.SetNextVertex(new Vertex() { position = new Vector3(x0, y1, Vertex.nearZ), tint = color, uv = new Vector2(0, 0) * mesh.uvRegion.size + mesh.uvRegion.position }); mesh.SetNextVertex(new Vertex() { position = new Vector3(x1, y1, Vertex.nearZ), tint = color, uv = new Vector2(1, 0) * mesh.uvRegion.size + mesh.uvRegion.position }); mesh.SetAllIndices(k_Indices); }
void DrawLine(MeshGenerationContext mgc, Vector2 v0, Vector2 v1, Color wireColor, float alpha) { var v = (v1 - v0); var leftPerp = new Vector2(v.y, -v.x).normalized * 0.5f; var p0 = v0 + leftPerp; var p1 = v1 + leftPerp; var p2 = v1 - leftPerp; var p3 = v0 - leftPerp; wireColor.a = alpha; var mesh = mgc.Allocate(4, 6); mesh.SetNextVertex(new Vertex() { position = new Vector3(p0.x, p0.y, Vertex.nearZ), tint = wireColor }); mesh.SetNextVertex(new Vertex() { position = new Vector3(p1.x, p1.y, Vertex.nearZ), tint = wireColor }); mesh.SetNextVertex(new Vertex() { position = new Vector3(p2.x, p2.y, Vertex.nearZ), tint = wireColor }); mesh.SetNextVertex(new Vertex() { position = new Vector3(p3.x, p3.y, Vertex.nearZ), tint = wireColor }); mesh.SetNextIndex(0); mesh.SetNextIndex(1); mesh.SetNextIndex(2); mesh.SetNextIndex(0); mesh.SetNextIndex(2); mesh.SetNextIndex(3); }
private void OnGenerateVisualContent(MeshGenerationContext context) { var center = (Vector3)contentRect.center; var zoom = _canvas.Zoom; var hwidth = _canvas.ImageWidth * 0.5f * zoom; var hheight = _canvas.ImageHeight * 0.5f * zoom; var uvmax = Vector2.zero; var uvmin = Vector2.zero; var tl = center + new Vector3(-hwidth, hheight, 0); var tr = center + new Vector3(hwidth, hheight, 0); var br = center + new Vector3(hwidth, -hheight, 0); var bl = center + new Vector3(-hwidth, -hheight, 0); if (ShowCheckerboard) { var gridScale = 16.0f / zoom; uvmax = new Vector2( (int)(_canvas.ImageWidth / gridScale), (int)(_canvas.ImageHeight / gridScale)); uvmax = new Vector2( Mathf.NextPowerOfTwo((int)(uvmax.x - uvmax.x * 0.5f)), Mathf.NextPowerOfTwo((int)(uvmax.y - uvmax.y * 0.5f))); uvmax = Vector2.Max(Vector2.one, uvmax); } else { uvmin = uvmax = new Vector2(0.75f, 0.25f); } // Draw the background grid var mesh = context.Allocate(4, 6, _background); mesh.SetAllVertices(new Vertex[] { new Vertex { position = tl, uv = uvmin, tint = Color.white }, new Vertex { position = tr, uv = new Vector2(uvmax.x, uvmin.y), tint = Color.white }, new Vertex { position = br, uv = uvmax, tint = Color.white }, new Vertex { position = bl, uv = new Vector2(uvmin.x, uvmax.y), tint = Color.white } }); mesh.SetAllIndices(new ushort[] { 2, 1, 0, 3, 2, 0 }); if (_canvas.File == null) { return; } // Draw each layer foreach (var petexture in _canvas.File.images.Where(t => t.frame == _canvas.ActiveFrame).OrderBy(t => t.layer.order)) { if (!petexture.layer.visible) { continue; } var tint = Color.white.MultiplyAlpha(petexture.layer.opacity); mesh = context.Allocate(4, 6, petexture.texture); mesh.SetAllVertices(new Vertex[] { new Vertex { position = tl, uv = Vector2.zero, tint = tint }, new Vertex { position = tr, uv = new Vector2(1, 0), tint = tint }, new Vertex { position = br, uv = Vector2.one, tint = tint }, new Vertex { position = bl, uv = new Vector2(0, 1), tint = tint } }); mesh.SetAllIndices(new ushort[] { 2, 1, 0, 3, 2, 0 }); } }
public static MeshWriteData AllocateMeshWriteData(MeshGenerationContext mgc, int vertexCount, int indexCount) { return(mgc.Allocate(vertexCount, indexCount, null, null, MeshGenerationContext.MeshFlags.UVisDisplacement)); }
protected override void OnGenerateVisualContent(MeshGenerationContext cxt) { var mp = new MeshParts(); List <int3> curTri = new List <int3>(); Color[] clrByDepth = new Color[_subDiv + 1]; for (int iClr = 0; iClr < clrByDepth.Length; iClr++) { float t = iClr / (float)(clrByDepth.Length); clrByDepth[iClr] = Color.HSVToRGB(t, 1, 1); } for (int iTriVtx = 0; iTriVtx < 3; iTriVtx++) { Vertex vtx = new Vertex(); vtx.position = (Vector2)T_LsFromNs(TriangleMat[iTriVtx]); vtx.tint = clrByDepth[_subDiv]; // it circular mp.vertices.Add(vtx); } curTri.Add(new int3(2, 1, 0)); for (int iDiv = 0; iDiv < _subDiv; iDiv++) { List <int3> nxtTri = new List <int3>(); foreach (var outerTri in curTri) { int vtxOffset = mp.vertices.Count; for (int iTriVtx = 0; iTriVtx < 3; iTriVtx++) { var pntLS = Vector3.zero; for (int iEdge = 0; iEdge < 3; iEdge++) { pntLS += (iEdge == iTriVtx) ? Vector3.zero: mp.vertices[outerTri[iEdge]].position; } pntLS /= 2; Vertex vtx = new Vertex(); vtx.position = pntLS; vtx.tint = clrByDepth[iDiv]; mp.vertices.Add(vtx); } for (int iTriVtx = 0; iTriVtx < 3; iTriVtx++) { // Broad cast it to xyz; int3 newTri = outerTri[iTriVtx]; if (iTriVtx == 0) { newTri.yz = vtxOffset + new int2(2, 1); } else if (iTriVtx == 1) { newTri.xz = vtxOffset + new int2(2, 0); } else if (iTriVtx == 2) { newTri.xy = vtxOffset + new int2(1, 0); } nxtTri.Add(newTri); } nxtTri.Add(vtxOffset + new int3(0, 1, 2)); } curTri = nxtTri; } foreach (var tri in curTri) { mp.triangles.Add(tri); //indices.AddRange(new ushort[] { (ushort)tri.z, (ushort)tri.y, (ushort)tri.x }); } if (GeneBankManager.Inst && GeneBankManager.Inst.GenomeCount > 0) { ApplyGenomeColors(mp.vertices); } AddPoint(mp, T_NsFromBs(_value_bs), math.cmin(layout.size / 30), Color.white); MeshWriteData meshData = cxt.Allocate(mp.vertices.Count, mp.triangles.Count * 3); meshData.SetAllVertices(mp.vertices.ToArray()); meshData.SetAllIndices(mp.GetIndices()); }
void DrawGraph(MeshGenerationContext context) { float step = 1 / context.visualElement.contentRect.width; List <Vector3> points = new List <Vector3>(); for (float i = 0; i <= 1; i += step) { points.Add(new Vector3(i, ResponseCurve.Evaluate(i, responseCurve), Vertex.nearZ)); } float thickness = 2f; Color color = Color.red; List <Vertex> vertices = new List <Vertex>(); List <ushort> indices = new List <ushort>(); for (int i = 0; i < points.Count - 1; i++) { var pointA = points[i]; var pointB = points[i + 1]; pointA.x = pointA.x * context.visualElement.contentRect.width; pointA.y = context.visualElement.contentRect.height - pointA.y * context.visualElement.contentRect.height; pointB.x = pointB.x * context.visualElement.contentRect.width; pointB.y = context.visualElement.contentRect.height - pointB.y * context.visualElement.contentRect.height; float angle = Mathf.Atan2(pointB.y - pointA.y, pointB.x - pointA.x); float offsetX = thickness / 2 * Mathf.Sin(angle); float offsetY = thickness / 2 * Mathf.Cos(angle); vertices.Add(new Vertex() { position = new Vector3(pointA.x + offsetX, pointA.y - offsetY, Vertex.nearZ), tint = color }); vertices.Add(new Vertex() { position = new Vector3(pointB.x + offsetX, pointB.y - offsetY, Vertex.nearZ), tint = color }); vertices.Add(new Vertex() { position = new Vector3(pointB.x - offsetX, pointB.y + offsetY, Vertex.nearZ), tint = color }); vertices.Add(new Vertex() { position = new Vector3(pointB.x - offsetX, pointB.y + offsetY, Vertex.nearZ), tint = color }); vertices.Add(new Vertex() { position = new Vector3(pointA.x - offsetX, pointA.y + offsetY, Vertex.nearZ), tint = color }); vertices.Add(new Vertex() { position = new Vector3(pointA.x + offsetX, pointA.y - offsetY, Vertex.nearZ), tint = color }); ushort indexOffset(int value) => (ushort)(value + (i * 6)); indices.Add(indexOffset(0)); indices.Add(indexOffset(1)); indices.Add(indexOffset(2)); indices.Add(indexOffset(3)); indices.Add(indexOffset(4)); indices.Add(indexOffset(5)); } var mesh = context.Allocate(vertices.Count, indices.Count); mesh.SetAllVertices(vertices.ToArray()); mesh.SetAllIndices(indices.ToArray()); }
private void DrawDeviceBorder(MeshGenerationContext mgc) { // For now, we draw device as borders. We can draw device image in the future. var scale = m_Scale / 100f; var halfImageWidth = scale * m_DeviceInfo.Screens[0].width / 2; var halfImageHeight = scale * m_DeviceInfo.Screens[0].height / 2; var leftOuterWidth = halfImageWidth + scale * m_DeviceInfo.Meta.overlayOffset.x; var topOuterWidth = halfImageHeight + scale * m_DeviceInfo.Meta.overlayOffset.y; var rightOuterWidth = halfImageWidth + scale * m_DeviceInfo.Meta.overlayOffset.z; var bottomOuterWidth = halfImageHeight + scale * m_DeviceInfo.Meta.overlayOffset.w; var padding = 20 * scale; var leftInnerWidth = leftOuterWidth - padding; var topInnerWidth = topOuterWidth - padding; var rightInnerWidth = rightOuterWidth - padding; var bottomInnerWidth = bottomOuterWidth - padding; var outerColor = EditorGUIUtility.isProSkin ? new Color(217f / 255, 217f / 255, 217f / 255) : new Color(100f / 255, 100f / 255, 100f / 255); var innerColor = new Color(41f / 255, 41f / 255, 41f / 255); const int vertexCount = 16, indexCount = 48; var meshWriteData = mgc.Allocate(vertexCount, indexCount); var vertices = new Vertex[vertexCount]; // Outer border. vertices[0].position = new Vector3(-leftOuterWidth, -topOuterWidth, Vertex.nearZ); vertices[0].tint = outerColor; vertices[1].position = new Vector3(rightOuterWidth, -topOuterWidth, Vertex.nearZ); vertices[1].tint = outerColor; vertices[2].position = new Vector3(rightOuterWidth, bottomOuterWidth, Vertex.nearZ); vertices[2].tint = outerColor; vertices[3].position = new Vector3(-leftOuterWidth, bottomOuterWidth, Vertex.nearZ); vertices[3].tint = outerColor; vertices[4].position = new Vector3(-leftInnerWidth, -topInnerWidth, Vertex.nearZ); vertices[4].tint = outerColor; vertices[5].position = new Vector3(rightInnerWidth, -topInnerWidth, Vertex.nearZ); vertices[5].tint = outerColor; vertices[6].position = new Vector3(rightInnerWidth, bottomInnerWidth, Vertex.nearZ); vertices[6].tint = outerColor; vertices[7].position = new Vector3(-leftInnerWidth, bottomInnerWidth, Vertex.nearZ); vertices[7].tint = outerColor; //Inner border. vertices[8].position = vertices[4].position; vertices[8].tint = innerColor; vertices[9].position = vertices[5].position; vertices[9].tint = innerColor; vertices[10].position = vertices[6].position; vertices[10].tint = innerColor; vertices[11].position = vertices[7].position; vertices[11].tint = innerColor; vertices[12].position = new Vector3(-halfImageWidth, -halfImageHeight, Vertex.nearZ); vertices[12].tint = innerColor; vertices[13].position = new Vector3(halfImageWidth, -halfImageHeight, Vertex.nearZ); vertices[13].tint = innerColor; vertices[14].position = new Vector3(halfImageWidth, halfImageHeight, Vertex.nearZ); vertices[14].tint = innerColor; vertices[15].position = new Vector3(-halfImageWidth, halfImageHeight, Vertex.nearZ); vertices[15].tint = innerColor; SimulatorUtilities.TransformVertices(m_Rotation, m_Offset, vertices); meshWriteData.SetAllVertices(vertices); var indices = new ushort[] { 0, 4, 1, 1, 4, 5, 1, 5, 6, 1, 6, 2, 6, 7, 2, 7, 3, 2, 0, 3, 4, 3, 7, 4, // Outer 8, 12, 9, 9, 12, 13, 9, 13, 14, 9, 14, 10, 14, 15, 10, 15, 11, 10, 8, 11, 12, 11, 15, 12 // Inner }; meshWriteData.SetAllIndices(indices); }
private void DrawEaseFunction(MeshGenerationContext context) { if (_first) { _first = false; CaculationPoints(); } var list = new List <Vertex>(); var path = new List <LineMeshGenerator.Vertex>(); for (int i = 0; i < _points.Count; i++) { path.Add(new LineMeshGenerator.Vertex(_points[i].x, _points[i].y)); } var mesh = LineMeshGenerator.Generate(path); for (int i = 0; i < mesh.Count; i++) { var vert = new Vertex(); vert.tint = Color.white; vert.position = new Vector3(mesh[i].x, mesh[i].y, Vertex.nearZ); list.Add(vert); } var indexList = new List <ushort>(); for (int i = 0; i < _points.Count - 1; i++) { var vertIndex = (i * 4); indexList.Add((ushort)vertIndex); indexList.Add((ushort)(vertIndex + 3)); indexList.Add((ushort)(vertIndex + 1)); indexList.Add((ushort)(vertIndex)); indexList.Add((ushort)(vertIndex + 2)); indexList.Add((ushort)(vertIndex + 3)); } //for( int i = 0; i < _points.Count - 1; i++ ) //{ // var dir = _points[i + 1] - _points[i]; // dir = dir.normalized; // var vertical = NormalVector( dir ); // var vert1 = new Vertex(); // vert1.tint = Color.white; // vert1.position = new Vector3( _points[i].x, _points[i].y, Vertex.nearZ ); // var vert2 = new Vertex(); // vert2.tint = Color.white; // vert2.position = new Vector3( _points[i].x + vertical.x, _points[i].y + vertical.y, Vertex.nearZ ); // var vert3 = new Vertex(); // vert3.tint = Color.white; // vert3.position = new Vector3( _points[i + 1].x, _points[i + 1].y, Vertex.nearZ ); // var vert4 = new Vertex(); // vert4.tint = Color.white; // vert4.position = new Vector3( _points[i + 1].x + vertical.x, _points[i + 1].y + vertical.y, Vertex.nearZ ); // list.Add( vert1 ); // list.Add( vert2 ); // list.Add( vert3 ); // list.Add( vert4 ); //} //var indexList = new List<ushort>(); //for( int i = 0; i < _points.Count - 1; i++ ) //{ // var vertIndex = ( i * 4 ); // indexList.Add( (ushort)vertIndex ); // indexList.Add( (ushort)( vertIndex + 3 ) ); // indexList.Add( (ushort)( vertIndex + 1 ) ); // indexList.Add( (ushort)( vertIndex ) ); // indexList.Add( (ushort)( vertIndex + 2 ) ); // indexList.Add( (ushort)( vertIndex + 3 ) ); //} var data = context.Allocate(list.Count, indexList.Count); data.SetAllVertices(list.ToArray()); data.SetAllIndices(indexList.ToArray()); }
private void OnGenerateVisualContent(MeshGenerationContext cxt) { MeshParts mp = new MeshParts(); MultiLayerPerception mlp = _TestMLP; AddRect(mp, 0, 1, Color.gray); if (mlp != null) { using (IWorker oneshotSyncWorker = WorkerFactory.CreateWorker(_testMLP.model, _extraLayers, WorkerFactory.Device.GPU)) { using (Tensor obsTensor = new Tensor(new TensorShape(1, mlp._shape.inputSize))) { if (_observe.Length < mlp._shape.inputSize) { _observe = new float[mlp._shape.inputSize]; } for (int iINode = 0; iINode < mlp._shape.inputSize; iINode++) { obsTensor[iINode] = _observe[iINode]; } oneshotSyncWorker.Execute(obsTensor).FlushSchedule(); } for (int iINode = 0; iINode < mlp._shape.inputSize; iINode++) { AddRect(mp, GetNodePos(0, iINode), NodeSize, ActNodeColor(_observe[iINode])); } using (Tensor hvr = oneshotSyncWorker.PeekOutput(MultiLayerPerception.LayerNames.Hidden)) { using (Tensor hva = oneshotSyncWorker.PeekOutput(MultiLayerPerception.LayerNames.HiddenActive)) { for (int iHNode = 0; iHNode < mlp._shape.hiddenSize; iHNode++) { AddRect(mp, GetNodePos(1, iHNode), NodeSize, RawNodeColor(hvr[iHNode])); AddRect(mp, GetNodePos(1, iHNode) + new float2(0.5f, 0) * NodeSize, new float2(0.5f, 1) * NodeSize, ActNodeColor(hva[iHNode])); } } } using (Tensor ovr = oneshotSyncWorker.PeekOutput(MultiLayerPerception.LayerNames.Output)) { using (Tensor ova = oneshotSyncWorker.PeekOutput()) { for (int iONode = 0; iONode < mlp._shape.outputSize; iONode++) { AddRect(mp, GetNodePos(2, iONode), NodeSize, RawNodeColor(ovr[iONode])); AddRect(mp, GetNodePos(2, iONode) + new float2(0.5f, 0) * NodeSize, new float2(0.5f, 1) * NodeSize, ActNodeColor(ova[iONode])); } } } } string[] layerNames = new string[] { MultiLayerPerception.LayerNames.Hidden, MultiLayerPerception.LayerNames.Output }; float2 xBuf = NodeSize / 2; xBuf.y = 0; int prvLayer = 0; int curLayer = 1; foreach (string layerName in layerNames) { TensorShape tShape = _testMLP.GetLayerShape(layerName); for (int iPNode = 0; iPNode < tShape.flatHeight; iPNode++) { for (int iCNode = 0; iCNode < tShape.flatWidth; iCNode++) { float2 posI = GetNodePos(prvLayer, iPNode) + NodeSize / 2; float2 posW = GetNodePos(curLayer, iCNode) + NodeSize / 2; float t = 0.5f + mlp.GetWeight(layerName, iPNode, iCNode); DrawLine(mp, posI + xBuf, posW - xBuf, 0.025f, TurboColorMap.Map(t)); } } prvLayer = curLayer; curLayer++; } } MeshWriteData meshData = cxt.Allocate(mp.vertices.Count, mp.IndicesCount); if (meshData.vertexCount > 0) { meshData.SetAllVertices(mp.vertices.ToArray()); meshData.SetAllIndices(mp.GetIndices()); } }
void DrawEdge(MeshGenerationContext mgc) { if (edgeWidth <= 0) { return; } UpdateRenderPoints(); if (m_RenderPoints.Count == 0) { return; // Don't draw anything } Color inColor = this.inputColor; Color outColor = this.outputColor; inColor *= UIElementsUtility.editorPlayModeTintColor; outColor *= UIElementsUtility.editorPlayModeTintColor; uint cpt = (uint)m_RenderPoints.Count; uint wantedLength = (cpt) * 2; uint indexCount = (wantedLength - 2) * 3; var md = mgc.Allocate((int)wantedLength, (int)indexCount, null, null, MeshGenerationContext.MeshFlags.UVisDisplacement); if (md.vertexCount == 0) { return; } float polyLineLength = 0; for (int i = 1; i < cpt; ++i) { polyLineLength += (m_RenderPoints[i - 1] - m_RenderPoints[i]).sqrMagnitude; } float halfWidth = edgeWidth * 0.5f; float currentLength = 0; Color32 flags = new Color32(0, 0, 0, (byte)VertexFlags.LastType); Vector2 unitPreviousSegment = Vector2.zero; for (int i = 0; i < cpt; ++i) { Vector2 dir; Vector2 unitNextSegment = Vector2.zero; Vector2 nextSegment = Vector2.zero; if (i < cpt - 1) { nextSegment = (m_RenderPoints[i + 1] - m_RenderPoints[i]); unitNextSegment = nextSegment.normalized; } if (i > 0 && i < cpt - 1) { dir = unitPreviousSegment + unitNextSegment; dir.Normalize(); } else if (i > 0) { dir = unitPreviousSegment; } else { dir = unitNextSegment; } Vector2 pos = m_RenderPoints[i]; Vector2 uv = new Vector2(dir.y * halfWidth, -dir.x * halfWidth); // Normal scaled by half width Color32 tint = Color.LerpUnclamped(outColor, inColor, currentLength / polyLineLength); md.SetNextVertex(new Vertex() { position = new Vector3(pos.x, pos.y, 1), uv = uv, tint = tint, idsFlags = flags }); md.SetNextVertex(new Vertex() { position = new Vector3(pos.x, pos.y, -1), uv = uv, tint = tint, idsFlags = flags }); if (i < cpt - 2) { currentLength += nextSegment.sqrMagnitude; } else { currentLength = polyLineLength; } unitPreviousSegment = unitNextSegment; } // Fill triangle indices as it is a triangle strip for (uint i = 0; i < wantedLength - 2; ++i) { if ((i & 0x01) == 0) { md.SetNextIndex((UInt16)i); md.SetNextIndex((UInt16)(i + 1)); md.SetNextIndex((UInt16)(i + 2)); } else { md.SetNextIndex((UInt16)(i + 1)); md.SetNextIndex((UInt16)i); md.SetNextIndex((UInt16)(i + 2)); } } }
void TiledQuads(Vector2 pos, Vector2 size, Color color, float scale, Texture2D texture2D, MeshGenerationContext context) { var tW = texture2D.width * scale; var tH = texture2D.height * scale; var xRepeat = size.x / tW; var yRepeat = size.y / tH; var xTiles = (int)xRepeat; var xTaper = xRepeat - xTiles; if (xTaper <= 0) { xTaper = 1; } else { ++xTiles; } var yTiles = (int)yRepeat; var yTaper = yRepeat - yTiles; if (yTaper <= 0) { yTaper = 1; } else { ++yTiles; } var totalTiles = xTiles * yTiles; var uvOffset = new Vector2(0, 0); var lastRowUvOffset = new Vector2(0, 1 - yTaper); // if more quads are needed than ushort allows for, split the mesh. const int k_VertextCountPerQuad = 4; const int k_IndexCountPerQuad = 6; int quadIndexToSplitMeshAt = totalTiles; if (totalTiles * k_IndexCountPerQuad > ushort.MaxValue) { quadIndexToSplitMeshAt = short.MaxValue / k_IndexCountPerQuad; } var mesh = context.Allocate(k_VertextCountPerQuad * quadIndexToSplitMeshAt, k_IndexCountPerQuad * quadIndexToSplitMeshAt, texture2D); ushort vertextOffset = 0; for (int q = 0; q < totalTiles; q++) { var x = q % xTiles; var y = q / xTiles; var x0 = pos.x + tW * x; var y0 = pos.y + tH * y; var applicableXTaper = (x == xTiles - 1 ? xTaper : 1); var applicableYTaper = (y == yTiles - 1 ? yTaper : 1); QuadA(new Vector2(x0, y0), new Vector2(tW * applicableXTaper, tH * applicableYTaper), new Vector2(applicableXTaper, 1), (y == yTiles - 1 ? lastRowUvOffset : uvOffset), color, mesh, vertextOffset); vertextOffset += k_VertextCountPerQuad; // Check if the current mesh is full and the next one is needed if (q >= quadIndexToSplitMeshAt && q + 1 < totalTiles) { // calculate next split var remainingQuads = totalTiles - q; quadIndexToSplitMeshAt = remainingQuads; if (remainingQuads * k_IndexCountPerQuad > ushort.MaxValue) { quadIndexToSplitMeshAt = short.MaxValue / k_IndexCountPerQuad; } vertextOffset = 0; mesh = context.Allocate(k_VertextCountPerQuad * quadIndexToSplitMeshAt, k_IndexCountPerQuad * quadIndexToSplitMeshAt, texture2D); } } }