Example #1
0
        public CircleGeometry(float radius=1,int segments=8,float thetaStart = 0, float thetaLength = Mathf.Tau)
        {
            segments = Mathf.Max(3, segments);
            var uvs = new List<Vector2>();
            
            vertices.Add(Vector3.Zero);
            uvs.Add(Vector2.Half);

            for(var i = 0f; i <= segments; i ++ ) 
            {
                
		        var segment = thetaStart + i / segments * thetaLength;

                var v = new Vector3(Mathf.Cos( segment ),radius * Mathf.Sin( segment ),0);
                v.Multiply(radius);
                vertices.Add(v);
                uvs.Add(new Vector2(( v.x / radius + 1 ) / 2, ( v.y / radius + 1 ) / 2 ));
            }

            var n = Vector3.UnitZ;

            for (var i = 1; i <= segments; i++)
            {
                faces.Add(new Face3(i, i + 1, 0, n));
                var faceSet = new UVFaceSet(uvs[i], uvs[i + 1], Vector2.Half);
                faceVertexUvs[0].Add(faceSet);
            }

            ComputeNormals();
            BoundingSphere = new Sphere(Vector3.Zero,radius );
        }
Example #2
0
        public PolyhedronGeometry(Vector3[] intialVertices, int[] indices, float radius = 1, int detail = 0)
        {
            var vertexSet = new List<VertexInfo>();

            foreach (var v in intialVertices) Prepare(v, vertexSet);

            var initialFaces = new List<Face3>();
            for (int i = 0; i < indices.Length; i += 3)
            {
                var i1 = indices[i];
                var i2 = indices[i + 1];
                var i3 = indices[i + 2];
                var v1 = vertexSet[i1].Position;
                var v2 = vertexSet[i2].Position;
                var v3 = vertexSet[i3].Position;
                initialFaces.Add(new Face3(i1, i2, i3, v1, v2, v3));
            }

            var centroid = Vector3.Zero;

            foreach(var f in initialFaces) Subdivide(vertexSet, f, detail);
            

            // Handle case when face straddles the seam
            foreach (var uvs in faceVertexUvs[0])
            {
                var x0 = uvs.A.x;
                var x1 = uvs.B.x;
                var x2 = uvs.C.x;

                var max = Mathf.Max(x0, Mathf.Max(x1, x2));
                var min = Mathf.Min(x0, Mathf.Min(x1, x2));

                if (max > 0.9f && min < 0.1f)
                { // 0.9 is somewhat arbitrary
                    if (x0 < 0.2f) uvs.A.x += 1;
                    if (x1 < 0.2f) uvs.B.x += 1;
                    if (x2 < 0.2f) uvs.C.x += 1;
                }
            }


            // Apply radius
            for (var i = 0; i < vertexSet.Count; i++)
            {
                var v = vertexSet[i].Position;
                v.Multiply(radius);
                vertices.Add(v);

                //vertices[i] = v;
            }

            MergeVertices();
            ComputeNormals();
            BoundingSphere = new Sphere(Vector3.Zero, radius);

        }
Example #3
0
        private bool IntersectsSphere(Sphere sphere)
        {
            var center = sphere.Center;
            var negRadius = -sphere.Radius;

            foreach(var plane in planes)
            {
                var distance = plane.DistanceToPoint(center);
                if (distance < negRadius) return false;
            }

            return true;

        }
Example #4
0
 public void ComputeBoundingSphere()
 {
     BoundingSphere = Sphere.FromPoints(vertices);
 }
Example #5
0
        public SphereGeometry(float radius = 1, int widthSegments = 8, int heightSegments = 6, float phiStart = 0, float phiLength = Mathf.Tau, float thetaStart = 0, float thetaLength = Mathf.Pi)
        {
            widthSegments = Mathf.Max(3, widthSegments);
            heightSegments = Mathf.Max(2, heightSegments);
            var uv = faceVertexUvs[0];

            var uvs = new List<List<Vector2>>();
            var verticiesIndicies = new List<List<int>>();
            for (var y = 0; y <= heightSegments; y ++ ) 
            {
                var verticesIndexRow = new List<int>();
		        var uvsRow = new List<Vector2>();

		for (var x = 0; x <= widthSegments; x ++ ) 
        {
			var u = x / (float)widthSegments;
			var v = y / (float)heightSegments;

			var vX = - radius * Mathf.Cos( phiStart + u * phiLength ) * Mathf.Sin( thetaStart + v * thetaLength );
			var vY = radius * Mathf.Cos( thetaStart + v * thetaLength );
			var vZ = radius * Mathf.Sin( phiStart + u * phiLength ) * Mathf.Sin( thetaStart + v * thetaLength );
            vertices.Add( new Vector3(vX,vY,vZ));

			verticesIndexRow.Add( vertices.Count - 1 );
			uvsRow.Add( new Vector2( u, 1 - v ) );
		}

		verticiesIndicies.Add( verticesIndexRow );
		uvs.Add( uvsRow );
            }

            for (var y = 0; y < heightSegments; y++)
            {
                for (var x = 0; x < widthSegments; x++)
                {
                    var v1 = verticiesIndicies[y][x + 1];
                    var v2 = verticiesIndicies[y][x];
                    var v3 = verticiesIndicies[y + 1][x];
                    var v4 = verticiesIndicies[y + 1][x + 1];

                    var n1 = vertices[v1].Normalized();
                    var n2 = vertices[v2].Normalized();
                    var n3 = vertices[v3].Normalized();
                    var n4 = vertices[v4].Normalized();

                    var uv1 = uvs[y][x + 1];
                    var uv2 = uvs[y][x];
                    var uv3 = uvs[y + 1][x];
                    var uv4 = uvs[y + 1][x + 1];

                    if (Mathf.Abs(vertices[v1].y) == radius)
                    {
                        uv1.x = (uv1.x + uv2.x) / 2;
                        faces.Add(new Face3(v1, v3, v4, n1,n3,n4));
                        uv.Add(new UVFaceSet(uv1, uv3, uv4));
                    }
                    else if (Mathf.Abs(vertices[v3].y) == radius)
                    {
                        uv3.x = (uv3.x + uv4.x) / 2;
                        faces.Add(new Face3(v1, v2, v3, n1,n2,n3));
                        uv.Add(new UVFaceSet(uv1,uv2,uv3));
                    }
                    else
                    {
                        faces.Add(new Face3(v1, v2, v4, n1,n2,n4));
                        uv.Add(new UVFaceSet(uv1,uv2,uv4));
                        faces.Add(new Face3(v2, v3, v4, n2,n3,n4));
                        uv.Add(new UVFaceSet(uv2,uv3,uv4));
                    }
                }
            }

            ComputeNormals();
	        BoundingSphere = new Sphere( Vector3.Zero, radius );
        }