private static Mesh BuildPopulationMesh(PopulationData data) { var count = Math.Min(8192, data.Data.Count); var colors = new Vector3Buffer(8 * count); var meshBuilder = new MeshBuilder(); var matrix = Matrix.Identity; for (int i = 0; i < count; i++) { var location = data.Data[i]; var height = location.Size * 0.8f; var width = 0.0025f; matrix.LoadIdentity(); matrix.Rotate(Quaternion.CreateFromYawPitchRoll(MathF.Deg2Rad * (location.Lng), 0, MathF.Deg2Rad * (90 - location.Lat))); matrix.Translate(new Vector3(-0.5f * width, 0.5f + 0.5f * height, -0.5f * width)); matrix.Scale(new Vector3(width, height, width)); meshBuilder.AddBox(matrix); var color = Vector3.Lerp(new Vector3(0, 1, 0), new Vector3(1, 0.7f, 0), 3 * location.Size / data.Max); for (int k = 0; k < 8; k++) { colors[k + i * 8] = color; } } var datamesh = meshBuilder.GetMesh(); datamesh.Colors = colors; return(datamesh); }
private static void CreateVertexNormalBuffer(Mesh mesh) { var vertexCount = Slices * Stacks; var vertices = new Vector3Buffer((int)vertexCount); var normals = new Vector3Buffer((int)vertexCount); int count = 0; float ds = 1.0f / Slices; float dt = 1.0f / Stacks; // The upper bounds in these loops are tweaked to reduce the // chance of precision error causing an incorrect # of iterations. for (float s = 0; s < 1 - ds / 2; s += ds) { for (float t = 0; t < 1 - dt / 2; t += dt) { const float E = 0.01f; var p = EvaluateTrefoil(s, t); var u = EvaluateTrefoil(s + E, t) - p; var v = EvaluateTrefoil(s, t + E) - p; var n = Vector3.Normalize(Vector3.Cross(u, v)); vertices[count] = p; normals[count] = n; count++; } } mesh.Vertices = vertices; mesh.Normals = normals; }
public static Mesh Create1(int gridCount, float width) { var result = new Mesh(); var vertices = new Vector3Buffer(gridCount * gridCount); var texCoords = new Vector2Buffer(gridCount * gridCount); result.Type = PrimitiveType.Lines; result.TexCoords = texCoords; result.Vertices = vertices; result.Normals = Enumerable .Repeat(new Vector3(0, 1, 0), gridCount * gridCount) .ToArray(); for (int i = 0; i < gridCount; i++) { for (int j = 0; j < gridCount; j++) { vertices[i * gridCount + j] = 0.5f * width * new Vector3(i / (gridCount - 1f) - 0.5f, 0, j / (gridCount - 1f) - 0.5f); } } for (int i = 0; i < gridCount; i++) { for (int j = 0; j < gridCount; j++) { texCoords[i * gridCount + j] = new Vector2(i / (gridCount - 1f), j / (gridCount - 1f)); } } int index = 0; var indices = new UInt32Buffer(4 * gridCount * (gridCount - 1)); for (int j = 0; j < gridCount; j++) { for (int i = 0; i < gridCount - 1; i++) { indices[index++] = (UInt32)(i + j * gridCount); indices[index++] = (UInt32)(i + 1 + j * gridCount); } } for (int j = 0; j < gridCount; j++) { for (int i = 0; i < gridCount - 1; i++) { indices[index++] = (UInt32)(j + i * gridCount); indices[index++] = (UInt32)(j + (i + 1) * gridCount); } } result.Indices = indices; result.CalculateBounds(); return(result); }
public static Mesh Create(Quaternion rotation, Vector2 radiuses, int segments = 20) { var vertices = new Vector3Buffer(segments); var normals = new Vector3Buffer(segments); var indices = new UInt32Buffer(2 * segments); for (int i = 0; i < segments; i++) { var angle = MathF.PI * 2 * (i / (float)segments); var normal = Vector3.Transform(new Vector3(MathF.Sin(angle), MathF.Cos(angle), 0), rotation); var point = Vector3.Transform((Vector3)(new Vector2(MathF.Sin(angle), MathF.Cos(angle)) * radiuses), rotation); vertices[i] = point; indices[2 * i] = (UInt32)i; indices[2 * i + 1] = (UInt32)((i + 1) % segments); normals[i] = normal; } return new Mesh() { Normals = normals, Vertices = vertices, Indices = indices, Type = PrimitiveType.Lines }; }
public static Mesh Create(int gridCount, float width, bool generateTexCoords = false) { var result = new Mesh(); var vertices = new Vector3Buffer(gridCount * 4); result.Type = PrimitiveType.Lines; result.Vertices = vertices; result.Normals = Enumerable .Repeat(new Vector3(0, 1, 0), gridCount * 4) .ToArray(); for (int i = 0; i < gridCount; i++) { var offset = i / (gridCount - 1f) - 0.5f; vertices[i * 2] = new Vector3(offset * width, 0, -width / 2f); vertices[i * 2 + 1] = new Vector3(offset * width, 0, width / 2f); vertices[i * 2 + gridCount * 2] = new Vector3(-width / 2f, 0, offset * width); vertices[i * 2 + 1 + gridCount * 2] = new Vector3(width / 2f, 0, offset * width); } if (generateTexCoords) { var texCoords = new Vector2Buffer(gridCount * 4); for (int i = 0; i < gridCount; i++) { var offset = i / (gridCount - 1f); texCoords[i * 2] = new Vector2(offset, 0); texCoords[i * 2 + 1] = new Vector2(offset, 1); texCoords[i * 2 + gridCount * 2] = new Vector2(0, offset); texCoords[i * 2 + 1 + gridCount * 2] = new Vector2(1, offset); } result.TexCoords = texCoords; } result.CalculateBounds(); return(result); }
public static Mesh Create(CloudShape shape, float diameter = 1, int vertexCount = 300000) { var vertices = new Vector3Buffer(vertexCount); var normals = new Vector3Buffer(vertexCount); var texCoords = new Vector2Buffer(vertices.Length); for (int i = 0; i < vertices.Length; i++) { Vector3 point; switch (shape) { case CloudShape.Sphere: point = RandomF.InsideUnitSphere(); break; case CloudShape.Cube: point = RandomF.InsideUnitCube(); break; default: throw new InvalidOperationException(); } vertices[i] = point * diameter; normals[i] = point.Normalized; texCoords[i] = new Vector2(vertices[i].X + 0.5f, -vertices[i].Y + 0.5f); } var result = new Mesh() { Vertices = vertices, TexCoords = texCoords, Normals = normals, Type = PrimitiveType.Points }; result.CalculateBounds(); return(result); }
public static Mesh CreateLines(Vector3 size) { var result = new Mesh(); var halfSize = size * 0.5f; var vertices = new Vector3Buffer(8); vertices[0] = halfSize * new Vector3(-1, -1, -1); vertices[1] = halfSize * new Vector3(-1, 1, -1); vertices[2] = halfSize * new Vector3(1, 1, -1); vertices[3] = halfSize * new Vector3(1, -1, -1); vertices[4] = halfSize * new Vector3(-1, -1, 1); vertices[5] = halfSize * new Vector3(-1, 1, 1); vertices[6] = halfSize * new Vector3(1, 1, 1); vertices[7] = halfSize * new Vector3(1, -1, 1); UInt32Buffer indices = new UInt32[24] { 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 }; result.Vertices = vertices; result.Indices = indices; result.Type = PrimitiveType.Lines; result.CalculateBounds(); return(result); }
public ParticleRenderer(SceneTime time) { if (time == null) { throw new ArgumentNullException("time"); } MaxParticles = 1000; _particleCount = MaxParticles; _time = time; _glContext = GL.GetCurrent(true); _particlePositions = new Vector3Buffer(MaxParticles); _particleColors = new Vector4Buffer(MaxParticles); _particleData1 = new Vector4Buffer(MaxParticles); _particleData2 = new Vector4Buffer(MaxParticles); _particleData3 = new Vector4Buffer(MaxParticles); _computeData = new DataBuffer(); _computeData.Usage = BufferUsage.DynamicDraw; InitBufferValues(); _vertexBufferArray = new VertexArrayObject(); _positionsBuffer = _vertexBufferArray.SetAttributeData(StandardAttribute.Position, _particlePositions, false); _colorsBuffer = _vertexBufferArray.SetAttributeData(StandardAttribute.Color, _particleColors, false); _dataBuffer1 = _vertexBufferArray.SetAttributeData(StandardAttribute.Data1, _particleData1, false); _dataBuffer2 = _vertexBufferArray.SetAttributeData(StandardAttribute.Data2, _particleData2, false); _dataBuffer3 = _vertexBufferArray.SetAttributeData(StandardAttribute.Data3, _particleData3, false); _computeDataBuffer = new BufferObject(BufferObjectTypes.ShaderStorage); _computeShader = ShaderProgram.CreateCompute(Resources.Particles_comp); _computeDataBuffer.BufferData(_computeData); _prevTime = _time.CurrentFloat; _boundSphere = new Sphere(Vector3.Zero, 0.5f); }
protected override void OnStart() { _control = Scene.Control; _control.MouseDown += Control_MouseDown; var material = new Material(MaterialType.FlatColor); material.Color = new Vector4(1, 1, 0, 1); _renderer = SceneObject.AddComponent <MeshRenderable>(); _renderer.Material = material; _renderer.IsEnabled = false; var mesh = new Mesh(); var buffer3 = new Vector3Buffer(2); buffer3.Usage = BufferUsage.DynamicDraw; mesh.Vertices = buffer3; mesh.Type = PrimitiveType.Lines; _renderer.Mesh = mesh; var hitPointSo = new Node(Scene); var sphere = Icosahedron.Create(0.01f, 1); _hitPointRenderer = hitPointSo.AddComponent <MeshRenderable>(); _hitPointRenderer.IsEnabled = false; _hitPointRenderer.Mesh = sphere; _hitPointRenderer.SceneObject.Layer = 2; _hitPointRenderer.Material = material; _hitPointTransform = hitPointSo.Transform; _selectedMaterial = new Material(MaterialType.DiffuseColor); _selectedMaterial.Color = new Vector4(1, 0, 0, 1); }
void Start() { buffer = new Vector3Buffer(f, v => transform.localScale = v, 2f); }
public static Mesh Create(float majorRadius, float minorRadius, int torusPrecision = 48) { var numVertices = (torusPrecision + 1) * (torusPrecision + 1); var numIndices = 2 * torusPrecision * torusPrecision * 3; var vertices = new Vector3Buffer(numVertices); var normals = new Vector3Buffer(numVertices); for (int i = 0; i < torusPrecision + 1; i++) { var sTangent = new Vector3(0.0f, 0.0f, -1.0f); var tTangent = (new Vector3(0.0f, -1.0f, 0.0f)).GetRotatedZ(i * 360.0f / torusPrecision); var position = (new Vector3(minorRadius, 0.0f, 0.0f)).GetRotatedZ(i * 360.0f / torusPrecision) + new Vector3(majorRadius, 0.0f, 0.0f); var normal = Vector3.Cross(tTangent, sTangent); vertices[i] = position; normals[i] = normal; //vertices[i].s = 0.0f; //vertices[i].t = (float)i / torusPrecision; } for (int ring = 1; ring < torusPrecision + 1; ring++) { for (int i = 0; i < torusPrecision + 1; i++) { vertices[ring * (torusPrecision + 1) + i] = vertices[i].GetRotatedY(ring * 360.0f / torusPrecision); normals[ring * (torusPrecision + 1) + i] = normals[i].GetRotatedY(ring * 360.0f / torusPrecision); //vertices[ring * (torusPrecision + 1) + i].s = 2.0f * ring / torusPrecision; //vertices[ring * (torusPrecision + 1) + i].t = vertices[i].t; //vertices[ring * (torusPrecision + 1) + i].sTangent = // vertices[i].sTangent.GetRotatedY(ring * 360.0f / torusPrecision); //vertices[ring * (torusPrecision + 1) + i].tTangent = // vertices[i].tTangent.GetRotatedY(ring * 360.0f / torusPrecision); } } var indices = new UInt32Buffer(numIndices); // Calculate the indices for (int ring = 0; ring < torusPrecision; ring++) { for (int i = 0; i < torusPrecision; i++) { indices[((ring * torusPrecision + i) * 2) * 3 + 0] = (UInt32)(ring * (torusPrecision + 1) + i); indices[((ring * torusPrecision + i) * 2) * 3 + 1] = (UInt32)((ring + 1) * (torusPrecision + 1) + i); indices[((ring * torusPrecision + i) * 2) * 3 + 2] = (UInt32)(ring * (torusPrecision + 1) + i + 1); indices[((ring * torusPrecision + i) * 2 + 1) * 3 + 0] = (UInt32)(ring * (torusPrecision + 1) + i + 1); indices[((ring * torusPrecision + i) * 2 + 1) * 3 + 1] = (UInt32)((ring + 1) * (torusPrecision + 1) + i); indices[((ring * torusPrecision + i) * 2 + 1) * 3 + 2] = (UInt16)((ring + 1) * (torusPrecision + 1) + i + 1); } } var result = new Mesh(); result.Indices = indices; result.Vertices = vertices; result.Normals = normals; result.CalculateBounds(); return(result); }
public static Mesh Create(float width, float height) { var vertices = new Vector3Buffer(8); vertices[0] = new Vector3(-width, -height, 0) / 2; vertices[1] = new Vector3(-width, height, 0) / 2; vertices[2] = new Vector3(width, height, 0) / 2; vertices[3] = new Vector3(width, -height, 0) / 2; vertices[4] = new Vector3(-width, -height, 0) / 2; vertices[5] = new Vector3(-width, height, 0) / 2; vertices[6] = new Vector3(width, height, 0) / 2; vertices[7] = new Vector3(width, -height, 0) / 2; var normals = new Vector3Buffer(8); normals[0] = new Vector3(0, 0, 1); normals[1] = new Vector3(0, 0, 1); normals[2] = new Vector3(0, 0, 1); normals[3] = new Vector3(0, 0, 1); normals[4] = new Vector3(0, 0, -1); normals[5] = new Vector3(0, 0, -1); normals[6] = new Vector3(0, 0, -1); normals[7] = new Vector3(0, 0, -1); var tangents = new Vector3Buffer(8); tangents[0] = new Vector3(0, 0, 1); tangents[1] = new Vector3(0, 0, 1); tangents[2] = new Vector3(0, 0, 1); tangents[3] = new Vector3(0, 0, 1); tangents[4] = new Vector3(0, 0, -1); tangents[5] = new Vector3(0, 0, -1); tangents[6] = new Vector3(0, 0, -1); tangents[7] = new Vector3(0, 0, -1); var texCoords = new Vector2Buffer(8); texCoords[0] = Vector2.Zero; texCoords[1] = new Vector2(0, 1); texCoords[2] = Vector2.One; texCoords[3] = new Vector2(1, 0); texCoords[4] = Vector2.Zero; texCoords[5] = new Vector2(0, 1); texCoords[6] = Vector2.One; texCoords[7] = new Vector2(1, 0); UInt32Buffer indices = new UInt32[] { 2, 1, 0, 3, 2, 0, 4, 5, 6, 4, 6, 7 }; var mesh = new Mesh(); mesh.Vertices = vertices; mesh.Normals = normals; mesh.TexCoords = texCoords; mesh.Indices = indices; mesh.Tangents = Enumerable.Repeat(new Vector3(1, 0, 0), 8).ToArray(); mesh.CalculateBounds(); return(mesh); }
public Mesh LoadStream(Stream stream) { var reader = new StreamReader(stream); using (reader) { var vertices = new List <Vector3>(); var normals = new List <Vector3>(); var texCoords = new List <Vector2>(); var points = new List <Point>(); string line; char[] splitChars = { ' ' }; while ((line = reader.ReadLine()) != null) { line = line.Trim(splitChars); line = line.Replace(" ", " "); var parameters = line.Split(splitChars); switch (parameters[0]) { case "p": // Point break; case "v": // Vertex float x = ParseFloat(parameters[1]); float y = ParseFloat(parameters[2]); float z = ParseFloat(parameters[3]); vertices.Add(new Vector3(x, y, z)); break; case "vt": // TexCoord float u = ParseFloat(parameters[1]); float v = ParseFloat(parameters[2]); texCoords.Add(new Vector2(u, v)); break; case "vn": // Normal float nx = ParseFloat(parameters[1]); float ny = ParseFloat(parameters[2]); float nz = ParseFloat(parameters[3]); normals.Add(new Vector3(nx, ny, nz)); break; case "f": // Face points.AddRange(ParseFace(parameters)); break; } } var vertexBuffer = new Vector3Buffer(points.Count); var normalBuffer = new Vector3Buffer(points.Count); var textCoordBuffer = new Vector2Buffer(points.Count); for (int i = 0; i < points.Count; i++) { var p = points[i]; vertexBuffer[i] = vertices[p.Vertex]; normalBuffer[i] = normals[p.Normal]; textCoordBuffer[i] = texCoords[p.Texture]; } var result = new Mesh(); result.Vertices = vertexBuffer; result.TexCoords = textCoordBuffer; result.CalculateBounds(); if (normalBuffer.Length == 0) { result.CalculateNormals(); } else { result.Normals = normalBuffer; } return(result); } }