Beispiel #1
0
		// Convert an AnatomyEngine mesh to a Godot mesh:
		public GodotMeshConverter(UVMesh mesh)
		{
			// Initialize the arrays that are needed:
			Godot.Collections.Array arrays = new Godot.Collections.Array();
			arrays.Resize((int)Mesh.ArrayType.Max);
			
			int vertexCount = mesh.VertexList.Count;
			
			Godot.Vector3[] normal_array = new Godot.Vector3[vertexCount];
			Godot.Vector3[] vertex_array = new Godot.Vector3[vertexCount];
			
			// Populate the arrays from the input mesh data:
			for (int i = 0; i < vertexCount; i++)
			{
				Numerics.Vector3 vertex = mesh.VertexList[i].Position;
				Numerics.Vector3 normal = mesh.VertexList[i].Normal;
				vertex_array[i] = new Godot.Vector3(vertex.X, vertex.Y, vertex.Z);
				normal_array[i] = new Godot.Vector3(normal.X, normal.Y, normal.Z);
			}
			
			// The index list doesn't need to be converted:
			int[] index_array = mesh.IndexList.ToArray();
			
			// Put the data arrays in a larger array for Godot to understand what the arrays represent:
			arrays[(int)Mesh.ArrayType.Vertex] = vertex_array;
			arrays[(int)Mesh.ArrayType.Normal] = normal_array;
			arrays[(int)Mesh.ArrayType.Index] = index_array;
			
			// Finally, upload the mesh to GPU:
			this.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, arrays);
		}
        public void CreateExampleJoint(Character character)
        {
            // Generate a simple cubic spline that will act as the radius of a long bone:
            SortedList <float, float> splinePoints = new SortedList <float, float>();

            splinePoints.Add(0.0f, 0.5f * 1.1f);
            splinePoints.Add(0.02f, 0.5f * 1.1f);
            splinePoints.Add(0.15f, 0.5f * 0.95f);
            splinePoints.Add(0.3f, 0.5f * 0.9f);
            splinePoints.Add(0.5f, 0.5f * 1.2f);
            splinePoints.Add(0.7f, 0.5f * 0.9f);
            splinePoints.Add(0.8f, 0.5f * 0.95f);
            splinePoints.Add(0.98f, 0.5f * 1.1f);
            splinePoints.Add(1.0f, 0.5f * 1.1f);

            CubicSpline1D jointSpline = new CubicSpline1D(splinePoints);

            // Define the center curve of the long bone:
            Line centerLine = new Line(new Numerics.Vector3(-0.6f, 0.0f, 3.0f),
                                       new Numerics.Vector3(0.6f, 0.0f, 3.0f));

            // Add a long bone to the character:
            character.joints.Add(new Anatomy.Joints.HingeJoint(centerLine, jointSpline));

            // Generate the geometry vertices of the first bone with resolution U=32 and resolution V=32:
            UVMesh mesh = character.joints[0].GetArticularSurface().GenerateMesh(64, 64);

            // Finally upload the mesh to Godot:
            MeshInstance newMesh = new MeshInstance();

            newMesh.Mesh = new GodotMeshConverter(mesh);

            AddChild(newMesh);
        }
Beispiel #3
0
        public void CreateExampleJoint(Anatomy.Skeleton skeleton)
        {
            // Generate a simple cubic spline that will act as the radius of a long bone:
            SortedList <float, float> splinePoints = new SortedList <float, float>();
            float radiusModifier = 0.6f;

            splinePoints.Add(-0.1f, radiusModifier * 1.1f);
            splinePoints.Add(0.0f, radiusModifier * 1.1f);
            splinePoints.Add(0.15f, radiusModifier * 0.95f);
            splinePoints.Add(0.3f, radiusModifier * 0.9f);
            splinePoints.Add(0.5f, radiusModifier * 1.15f);
            splinePoints.Add(0.7f, radiusModifier * 0.95f);
            splinePoints.Add(0.8f, radiusModifier * 0.95f);
            splinePoints.Add(1.0f, radiusModifier * 1.1f);

            QuadraticSpline1D jointSpline = new QuadraticSpline1D(splinePoints);

            // Define the center curve of the long bone:
            LineSegment centerLine = new LineSegment(new Numerics.Vector3(0.0f, -0.5f, 0.0f),
                                                     new Numerics.Vector3(0.0f, 1.5f, 0.5f));

            // Add a long bone to the character:
            skeleton.joints.Add(new Anatomy.Joints.HingeJoint(centerLine, jointSpline, 0.0f, 1.0f * (float)Math.PI));

            // Generate the geometry vertices of the first bone with resolution U=32 and resolution V=32:
            UVMesh mesh = skeleton.joints[0].GetArticularSurface().GenerateMesh(64, 64);

            // Finally upload the mesh to Godot:
            MeshInstance newMesh = new MeshInstance();

            newMesh.Mesh = new GodotMeshConverter(mesh);
            newMesh.SetSurfaceMaterial(0, (Material)GD.Load("res://JointMaterial.tres"));

            AddChild(newMesh);
        }
Beispiel #4
0
 public void Read(AssetStream stream)
 {
     UVMesh.Read(stream);
     TerrainDynamicUVST.Read(stream);
     TerrainChunkDynamicUVST.Read(stream);
     LightmapIndex        = stream.ReadUInt16();
     LightmapIndexDynamic = stream.ReadUInt16();
     LightmapST.Read(stream);
     LightmapSTDynamic.Read(stream);
 }
Beispiel #5
0
        public YAMLNode ExportYAML(IExportContainer container)
        {
            YAMLMappingNode node = new YAMLMappingNode();

            node.Add("uvMesh", UVMesh.ExportYAML(container));
            node.Add("terrainDynamicUVST", TerrainDynamicUVST.ExportYAML(container));
            node.Add("terrainChunkDynamicUVST", TerrainChunkDynamicUVST.ExportYAML(container));
            node.Add("lightmapIndex", LightmapIndex);
            node.Add("lightmapIndexDynamic", LightmapIndexDynamic);
            node.Add("lightmapST", LightmapST.ExportYAML(container));
            node.Add("lightmapSTDynamic", LightmapSTDynamic.ExportYAML(container));
            return(node);
        }
Beispiel #6
0
 public void Read(AssetReader reader)
 {
     UVMesh.Read(reader);
     TerrainDynamicUVST.Read(reader);
     TerrainChunkDynamicUVST.Read(reader);
     LightmapIndex        = reader.ReadUInt16();
     LightmapIndexDynamic = reader.ReadUInt16();
     LightmapST.Read(reader);
     LightmapSTDynamic.Read(reader);
     if (IsReadExplicitProbeSetHash(reader.Version))
     {
         ExplicitProbeSetHash.Read(reader);
     }
 }
Beispiel #7
0
        public YAMLNode ExportYAML(IExportContainer container)
        {
            YAMLMappingNode node = new YAMLMappingNode();

            node.Add(UvMeshName, UVMesh.ExportYAML(container));
            node.Add(TerrainDynamicUVSTName, TerrainDynamicUVST.ExportYAML(container));
            node.Add(TerrainChunkDynamicUVSTName, TerrainChunkDynamicUVST.ExportYAML(container));
            node.Add(LightmapIndexName, LightmapIndex);
            node.Add(LightmapIndexDynamicName, LightmapIndexDynamic);
            node.Add(LightmapSTName, LightmapST.ExportYAML(container));
            node.Add(LightmapSTDynamicName, LightmapSTDynamic.ExportYAML(container));
            if (IsReadExplicitProbeSetHash(container.ExportVersion))
            {
                node.Add(ExplicitProbeSetHashName, ExplicitProbeSetHash.ExportYAML(container));
            }
            return(node);
        }
        public void CreateExampleBone(Character character)
        {
            // Generate a simple cubic spline that will act as the radius of a long bone:
            SortedList <float, float> radiusPoints = new SortedList <float, float>();

            radiusPoints.Add(0.0f, 0.7f * 0.92f);
            radiusPoints.Add(0.02f, 0.7f * 0.92f);
            radiusPoints.Add(0.15f, 0.7f * 0.8f);
            radiusPoints.Add(0.5f, 0.7f * 0.7f);
            radiusPoints.Add(0.8f, 0.7f * 0.76f);
            radiusPoints.Add(0.98f, 0.7f * 0.8f);
            radiusPoints.Add(1.0f, 0.7f * 0.8f);

            CubicSpline1D boneRadius = new CubicSpline1D(radiusPoints);

            // Define the center curve of the long bone:
            SortedList <float, Numerics.Vector3> centerPoints = new SortedList <float, Numerics.Vector3>();

            centerPoints.Add(0.0f, new Numerics.Vector3(0.0f, 0.0f, 2.7f));
            centerPoints.Add(0.25f, new Numerics.Vector3(-0.3f, -0.5f, 1.0f));
            centerPoints.Add(0.5f, new Numerics.Vector3(0.3f, 1.0f, 0.0f));
            centerPoints.Add(0.75f, new Numerics.Vector3(0.8f, 1.0f, -1.0f));
            centerPoints.Add(1.0f, new Numerics.Vector3(0.6f, -0.5f, -0.9f));

            SpatialCubicSpline boneCenter = new SpatialCubicSpline(centerPoints);

            //Line centerLine = new Line(new Numerics.Vector3(0.0f, 0.0f, 1.7f),
            //						   new Numerics.Vector3(0f, 0.0f, -1.7f));

            // Add a long bone to the character:
            character.bones.Add(new Anatomy.Bones.LongBone(boneCenter, boneRadius));

            // Generate the geometry vertices of the first bone with resolution U=32 and resolution V=32:
            UVMesh mesh = character.bones[0].GetGeometry().GenerateMesh(64, 1024);

            // Finally upload the mesh to Godot:
            MeshInstance newMesh = new MeshInstance();

            newMesh.Mesh = new GodotMeshConverter(mesh);
            newMesh.SetSurfaceMaterial(0, (Material)GD.Load("res://gui/BoneMaterial.tres"));

            AddChild(newMesh);
        }
Beispiel #9
0
            public UVMesh(UVMesh uvMesh)
            {
                uvMeshVertices = new List <Vector3>(uvMesh.uvMeshVertices.Count);
                for (int i = 0; i < uvMesh.uvMeshVertices.Count; ++i)
                {
                    uvMeshVertices.Add(uvMesh.uvMeshVertices[i]);
                }

                uvMeshColors = new List <Color>(uvMesh.uvMeshColors.Count);
                for (int i = 0; i < uvMesh.uvMeshColors.Count; ++i)
                {
                    uvMeshColors.Add(uvMesh.uvMeshColors[i]);
                }

                uvMeshIndices = new List <int>(uvMesh.uvMeshIndices.Count);
                for (int i = 0; i < uvMesh.uvMeshIndices.Count; ++i)
                {
                    uvMeshIndices.Add(uvMesh.uvMeshIndices[i]);
                }
            }
Beispiel #10
0
 public IEnumerable <Object> FetchDependencies(ISerializedFile file, bool isLog = false)
 {
     yield return(UVMesh.FetchDependency(file, isLog, () => nameof(RendererData), "uvMesh"));
 }
Beispiel #11
0
        public void CreateExampleBones(Anatomy.Skeleton skeleton)
        {
            // Generate a simple cubic spline that will act as the radius of a long bone:
            SortedList <double, double> radiusPoints = new SortedList <double, double>();

            radiusPoints.Add(-3.5f, 0.7f * 1.2f);
            radiusPoints.Add(-1.0f, 0.7f * 1.2f);
            radiusPoints.Add(0.02f, 0.7f * 1.2f);
            radiusPoints.Add(0.15f, 0.7f * 1.0f);
            radiusPoints.Add(0.5f, 0.7f * 0.7f);
            radiusPoints.Add(0.8f, 0.7f * 0.76f);
            radiusPoints.Add(0.98f, 0.7f * 0.8f);
            radiusPoints.Add(4.5f, 0.7f * 0.8f);

            LinearSpline1D boneRadius = new LinearSpline1D(radiusPoints);

            // Define the center curve of the long bone:
            SortedList <double, dvec3> centerPoints = new SortedList <double, dvec3>();

            centerPoints.Add(0.0f, new dvec3(0.0f, 0.0f, 2.7f));
            centerPoints.Add(0.25f, new dvec3(-0.3f, -0.5f, 1.0f));
            centerPoints.Add(0.5f, new dvec3(0.3f, 1.0f, 0.0f));
            centerPoints.Add(0.75f, new dvec3(0.8f, 1.0f, -1.0f));
            centerPoints.Add(1.0f, new dvec3(0.6f, -0.5f, -0.9f));

            SpatialCubicSpline boneCenter = new SpatialCubicSpline(centerPoints);

            // Add first bone:
            LineSegment centerLine = new LineSegment(new dvec3(0.0f, 0.0f, 0.5f),
                                                     new dvec3(0.001f, 10.0f, 0.51f));

            var bone1 = new Anatomy.Bones.LongBone(centerLine, boneRadius);

            var jointInteraction = new Anatomy.Bone.JointDeformation(skeleton.joints[0], RayCastDirection.Outwards, 3.0f);

            bone1.InteractingJoints.Add(jointInteraction);
            skeleton.bones.Add(bone1);

            // Add second bone:
            //LineSegment centerLine2 = new LineSegment(new dvec3(0.0f, -10.0f, 0.5f),
            //						   new dvec3(0.001f, -1.0f, 0.51f));

            //var bone2 = new Anatomy.Bones.LongBone(centerLine2, 1.1f);
            //bone2.InteractingJoints.Add((skeleton.joints[0], RayCastDirection.Outwards, 3.0f));
            //skeleton.bones.Add(bone2);

            // Generate the geometry vertices of the first bone with resolution U=128 and resolution V=128:
            foreach (var bone in skeleton.bones)
            {
#if GODOT_HTML5
                UVMesh mesh = bone.GetGeometry().GenerateMesh(32, 32);
#else
                UVMesh mesh = bone.GetGeometry().GenerateMesh(256, 256);
#endif

                // Finally upload the mesh to Godot:
                MeshInstance newMesh = new MeshInstance();
                newMesh.Mesh = new GodotMeshConverter(mesh);

                // Give each mesh a random color:
                var boneMaterial = (SpatialMaterial)GD.Load("res://BoneMaterial.tres").Duplicate();
                boneMaterial.AlbedoColor = new Color(GD.Randf(), GD.Randf(), GD.Randf(), GD.Randf());
                newMesh.SetSurfaceMaterial(0, boneMaterial);

                AddChild(newMesh);
            }
        }
Beispiel #12
0
        private void CreateUVDataMesh(Mesh inputMesh, Vector3[] insertData, out Mesh[] outputMeshes)
        {
            // Get original mesh data.
            Vector2[] uvs          = inputMesh.uv;
            int[]     indices      = inputMesh.triangles;
            int       numTriangles = indices.Length / 3;

            // Create a uv mesh.
            // Note: the unity has limitation for vertex size (65535), so we need
            //       to create multiple meshes if needed.
            const int UNITY_MAX_VERTICES = 65535;
            int       numMeshes          = Mathf.CeilToInt((float)indices.Length / (float)UNITY_MAX_VERTICES);

            outputMeshes = new Mesh[numMeshes];

            List <UVMesh> meshList        = new List <UVMesh>();
            UVMesh        uvMesh          = new UVMesh();
            int           verticesInMesh  = 0;
            int           trianglesInMesh = 0;

            for (int triIndex = 0; triIndex < numTriangles; ++triIndex)
            {
                // If verticesInMesh == 0, create a new submesh.
                if (verticesInMesh == 0)
                {
                    uvMesh.Clear();
                }

                int index = 3 * triIndex;
                int tid0  = indices[index + 0];
                int tid1  = indices[index + 1];
                int tid2  = indices[index + 2];

                uvMesh.uvMeshVertices.Add(new Vector3(uvs[tid0].x, uvs[tid0].y, 1.0f));
                uvMesh.uvMeshVertices.Add(new Vector3(uvs[tid1].x, uvs[tid1].y, 1.0f));
                uvMesh.uvMeshVertices.Add(new Vector3(uvs[tid2].x, uvs[tid2].y, 1.0f));

                uvMesh.uvMeshColors.Add(new Color(insertData[tid0].x, insertData[tid0].y, insertData[tid0].z, 1.0f));
                uvMesh.uvMeshColors.Add(new Color(insertData[tid1].x, insertData[tid1].y, insertData[tid1].z, 1.0f));
                uvMesh.uvMeshColors.Add(new Color(insertData[tid2].x, insertData[tid2].y, insertData[tid2].z, 1.0f));

                uvMesh.uvMeshIndices.Add(3 * trianglesInMesh + 0);
                uvMesh.uvMeshIndices.Add(3 * trianglesInMesh + 1);
                uvMesh.uvMeshIndices.Add(3 * trianglesInMesh + 2);

                verticesInMesh += 3;
                trianglesInMesh++;

                // If trianglesInSubMesh == UNITY_MAX_VERTICES or there is no further triangles, push the submesh.
                if (verticesInMesh == UNITY_MAX_VERTICES || triIndex == numTriangles - 1)
                {
                    UVMesh newMesh = new UVMesh(uvMesh);
                    meshList.Add(newMesh);
                    verticesInMesh  = 0;
                    trianglesInMesh = 0;
                }
            }

            for (int i = 0; i < numMeshes; ++i)
            {
                outputMeshes[i]           = new Mesh();
                outputMeshes[i].vertices  = meshList[i].uvMeshVertices.ToArray();
                outputMeshes[i].colors    = meshList[i].uvMeshColors.ToArray();
                outputMeshes[i].triangles = meshList[i].uvMeshIndices.ToArray();
            }
        }