void HandleLevelOfDetail(OpenFlight.Node node, Scene.Node parent) { var flt_lod = Parser.ParseLevelOfDetail(node.Record); var lod = new Scene.LOD(); lod.Name = flt_lod.ID; lod.SwitchInDistance = flt_lod.SwitchInDistance; lod.SwitchOutDistance = flt_lod.SwitchOutDistance; parent.AddChild(lod); foreach (var child in node.Children) { HandleNode(child, lod); } }
void HandleObject(OpenFlight.Node node, Scene.Node parent) { var flt_object = Parser.ParseObject(node.Record); var group = new Scene.Group(); group.Name = flt_object.ID; parent.AddChild(group); foreach (var child in node.Children) { HandleNode(child, group); } if (WorkingGroup != null) { CloseWorkingGroup(); } }
void HandleMesh(OpenFlight.Node node, Scene.Node parent) { var flt_mesh = Parser.ParseMesh(node.Record); OpenFlight.LocalVertexPool local_vertex_pool = null; OpenFlight.MeshPrimitive mesh_primitive = null; foreach (var record in node.Records) { switch (record.Opcode) { case OpenFlight.Opcode.LocalVertexPool: local_vertex_pool = Parser.ParseLocalVertexPool(record); break; case OpenFlight.Opcode.MeshPrimitive: mesh_primitive = Parser.ParseMeshPrimitive(record); break; } } if (local_vertex_pool == null) { return; } if (mesh_primitive == null) { return; } if (!local_vertex_pool.HasPosition) { return; } if (mesh_primitive.Type == 4) { UnityEngine.Debug.LogFormat("{0}: indexed poly is not currently supported.", node.Record.ToString()); return; } var mesh = new Scene.Mesh(); parent.AddChild(mesh); var material = MaterialForMesh(flt_mesh); mesh.MaterialIndex = Scene.Materials.IndexOf(material); mesh.Name = flt_mesh.ID; var count = local_vertex_pool.Coordinates.Length; mesh.Vertices = new UnityEngine.Vector3[count]; for (int i = 0; i < count; ++i) { // TODO: transform mesh.Vertices[i].x = (float)local_vertex_pool.Coordinates[i].X; mesh.Vertices[i].y = (float)local_vertex_pool.Coordinates[i].Z; mesh.Vertices[i].z = (float)local_vertex_pool.Coordinates[i].Y; } if (local_vertex_pool.HasNormal) { mesh.Normals = new UnityEngine.Vector3[count]; for (int i = 0; i < count; ++i) { mesh.Normals[i].x = local_vertex_pool.Normals[i].I; mesh.Normals[i].y = local_vertex_pool.Normals[i].K; mesh.Normals[i].z = local_vertex_pool.Normals[i].J; } } if (local_vertex_pool.HasBaseUV) { mesh.UVs1 = new UnityEngine.Vector2[count]; for (int i = 0; i < count; ++i) { mesh.UVs1[i].x = local_vertex_pool.UVBase[i].U; mesh.UVs1[i].y = local_vertex_pool.UVBase[i].V; } } if (local_vertex_pool.HasRGBAColor) { mesh.Colors = new UnityEngine.Color32[count]; for (int i = 0; i < count; ++i) { mesh.Colors[i].r = local_vertex_pool.Colors[i].R; mesh.Colors[i].g = local_vertex_pool.Colors[i].G; mesh.Colors[i].b = local_vertex_pool.Colors[i].B; mesh.Colors[i].a = local_vertex_pool.Colors[i].A; } } var index_count = mesh_primitive.Indices.Length; if (mesh_primitive.Type == 1) // triangle strip { mesh.Triangles = new int[(index_count - 2) * 3]; for (int i = 0; i + 2 < index_count; ++i) { if (i % 2 == 0) { mesh.Triangles[(i * 3) + 0] = mesh_primitive.Indices[i + 1]; mesh.Triangles[(i * 3) + 1] = mesh_primitive.Indices[i + 0]; mesh.Triangles[(i * 3) + 2] = mesh_primitive.Indices[i + 2]; } else { mesh.Triangles[(i * 3) + 0] = mesh_primitive.Indices[i + 0]; mesh.Triangles[(i * 3) + 1] = mesh_primitive.Indices[i + 1]; mesh.Triangles[(i * 3) + 2] = mesh_primitive.Indices[i + 2]; } } } if (mesh_primitive.Type == 2) // triangle fan { mesh.Triangles = new int[(index_count - 2) * 3]; for (int i = 0; i + 2 < index_count; ++i) { mesh.Triangles[(i * 3) + 0] = mesh_primitive.Indices[0]; mesh.Triangles[(i * 3) + 1] = mesh_primitive.Indices[i + 1]; mesh.Triangles[(i * 3) + 2] = mesh_primitive.Indices[i + 2]; } } if (mesh_primitive.Type == 3) // quad strip { mesh.Triangles = new int[((index_count / 2) - 1) * 6]; for (int i = 0; i + 1 < index_count; i += 2) { mesh.Triangles[(i * 6) + 0] = mesh_primitive.Indices[i + 0]; mesh.Triangles[(i * 6) + 1] = mesh_primitive.Indices[i + 1]; mesh.Triangles[(i * 6) + 2] = mesh_primitive.Indices[i + 2]; mesh.Triangles[(i * 6) + 3] = mesh_primitive.Indices[i + 2]; mesh.Triangles[(i * 6) + 4] = mesh_primitive.Indices[i + 1]; mesh.Triangles[(i * 6) + 5] = mesh_primitive.Indices[i + 3]; } } }