コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
ファイル: Plane.cs プロジェクト: xorza/NetGL
        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);
        }
コード例 #5
0
ファイル: ObjImporter.cs プロジェクト: xorza/NetGL
        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);
            }
        }