polylist Triangles2Polylist(triangles triangles) { polylist poly = new polylist(); poly.count = triangles.count; poly.input = triangles.input; poly.material = triangles.material; poly.name = triangles.name; poly.p = triangles.p; string str = "3 "; System.Text.StringBuilder builder = new System.Text.StringBuilder(str.Length * (int)poly.count); for (int i = 0; i < (int)poly.count; i++) { builder.Append(str); } poly.vcount = builder.ToString(); return(poly); }
public static polylist MakePolyList(ulong id_num, uint[] indices, ref uint largest_offset) { polylist plist = new polylist(); plist.material = "MaterialInstance_" + id_num; plist.input = new InputLocalOffset[] { makeInputOffset("vertices", "VERTEX", 0, false, 0), makeInputOffset("texCoord_source", "TEXCOORD", 1, true, 0), makeInputOffset("color_source", "COLOR", 2, true, 0) }; string p = "", vcount = ""; long len = indices.LongLength; uint largest = largest_offset; for (long i = 0; i < len; i++) { if (i % 3 == 0) { vcount += "3 "; } uint index = indices[i] + largest_offset; largest = Math.Max(largest, indices[i] + largest_offset); p += index.ToString() + " " + index.ToString() + " " + index.ToString(); if (i < len - 1) { p += " "; } } largest_offset = largest + 1; plist.vcount = vcount; plist.count = (ulong)(indices.LongLength / 3); plist.p = p; return(plist); }
public static void AddSkinnedModelWithAnimations(ref Skin SkinChunk, ref SkeletonCTTR SkeletonChunk, ref Shader[] ShaderChunks)//, ref Animation[] AnimChunks) { List <uint> IndexList = new List <uint>(); cachedGeoms.geometry = new geometry[SkinChunk.NumPrimGroups]; for (int primgroups = 0; primgroups < SkinChunk.NumPrimGroups; primgroups++) { cachedGeoms.geometry[primgroups] = new geometry(); cachedGeoms.geometry[primgroups].id = "geometry" + primgroups; cachedGeoms.geometry[primgroups].name = "geometry" + primgroups; mesh newMesh = new Collada141.mesh(); newMesh.vertices = new vertices(); newMesh.vertices.id = "geometry" + primgroups + "-vertices"; newMesh.vertices.name = "geometry" + primgroups + "-vertices"; newMesh.vertices.input = new InputLocal[1]; newMesh.vertices.input[0] = new InputLocal(); newMesh.vertices.input[0].semantic = "POSITION"; newMesh.vertices.input[0].source = "#geometry" + primgroups + "-positions"; //newMesh.vertices.input[1] = new InputLocal(); //newMesh.vertices.input[1].semantic = "TEXCOORD"; //newMesh.vertices.input[1].source = "#geometry" + primgroups + "-texcoords"; //newMesh.vertices.input[2].semantic = "COLOR"; //newMesh.vertices.input[2].source = "#geometry0-colors"; newMesh.source = new source[3]; newMesh.source[0] = new source(); newMesh.source[0].id = "geometry" + primgroups + "-positions"; float_array geom_positions = new float_array(); geom_positions.id = "geometry" + primgroups + "-positions-array"; PositionList posList = SkinChunk.GetChildren <PrimitiveGroupCTTR>()[primgroups].GetChildren <PositionList>()[0]; geom_positions.count = (ulong)posList.Positions.Length * 3; geom_positions.Values = new double[geom_positions.count]; for (ulong i = 0; i < geom_positions.count / 3; i++) { if (i % 3 == 0) { geom_positions.Values[i] = posList.Positions[i / 3].X; } else if (i % 3 == 1) { geom_positions.Values[i] = posList.Positions[i / 3].Y; } else { geom_positions.Values[i] = posList.Positions[i / 3].Z; } } newMesh.source[0].Item = geom_positions; newMesh.source[0].technique_common = new sourceTechnique_common(); newMesh.source[0].technique_common.accessor = new accessor(); newMesh.source[0].technique_common.accessor.source = "#" + geom_positions.id; newMesh.source[0].technique_common.accessor.count = geom_positions.count / 3; newMesh.source[0].technique_common.accessor.stride = 3; newMesh.source[0].technique_common.accessor.param = new param[3] { new param(), new param(), new param(), }; newMesh.source[0].technique_common.accessor.param[0].name = "X"; newMesh.source[0].technique_common.accessor.param[0].type = "float"; newMesh.source[0].technique_common.accessor.param[1].name = "Y"; newMesh.source[0].technique_common.accessor.param[1].type = "float"; newMesh.source[0].technique_common.accessor.param[2].name = "Z"; newMesh.source[0].technique_common.accessor.param[2].type = "float"; newMesh.source[1] = new source(); newMesh.source[1].id = "geometry" + primgroups + "-mesh-normals"; float_array geom_normals = new float_array(); geom_normals.id = "geometry" + primgroups + "-mesh-map-array"; NormalList normals = SkinChunk.GetChildren <PrimitiveGroupCTTR>()[primgroups].GetChildren <NormalList>()[0]; geom_normals.count = (ulong)normals.Normals.Length * 3; geom_normals.Values = new double[geom_normals.count]; for (ulong i = 0; i < geom_normals.count; i++) { if (i % 3 == 0) { geom_normals.Values[i] = normals.Normals[i / 3].X; } else if (i % 3 == 1) { geom_normals.Values[i] = normals.Normals[i / 3].Y; } else { geom_normals.Values[i] = normals.Normals[i / 3].Z; } } newMesh.source[1].Item = geom_normals; newMesh.source[1].technique_common = new sourceTechnique_common(); newMesh.source[1].technique_common.accessor = new accessor(); newMesh.source[1].technique_common.accessor.source = "#" + geom_normals.id; newMesh.source[1].technique_common.accessor.count = geom_positions.count / 3; newMesh.source[1].technique_common.accessor.stride = 3; newMesh.source[1].technique_common.accessor.param = new param[3] { new param(), new param(), new param(), }; newMesh.source[1].technique_common.accessor.param[0].name = "X"; newMesh.source[1].technique_common.accessor.param[0].type = "float"; newMesh.source[1].technique_common.accessor.param[1].name = "Y"; newMesh.source[1].technique_common.accessor.param[1].type = "float"; newMesh.source[1].technique_common.accessor.param[2].name = "Z"; newMesh.source[1].technique_common.accessor.param[2].type = "float"; newMesh.source[2] = new source(); newMesh.source[2].id = "geometry" + primgroups + "-mesh-map-0"; float_array geom_texcoords = new float_array(); geom_texcoords.id = "geometry" + primgroups + "-mesh-map-array"; UVList UV = SkinChunk.GetChildren <PrimitiveGroupCTTR>()[primgroups].GetChildren <UVList>()[0]; geom_texcoords.count = (ulong)UV.UVs.Length * 2; geom_texcoords.Values = new double[geom_texcoords.count]; for (ulong i = 0; i < geom_texcoords.count; i++) { ulong pos = i / 2; geom_texcoords.Values[i] = UV.UVs[pos].X; i++; geom_texcoords.Values[i] = UV.UVs[pos].Y; } newMesh.source[2].Item = geom_texcoords; newMesh.source[2].technique_common = new sourceTechnique_common(); newMesh.source[2].technique_common.accessor = new accessor(); newMesh.source[2].technique_common.accessor.source = "#" + geom_texcoords.id; newMesh.source[2].technique_common.accessor.count = geom_texcoords.count / 2; newMesh.source[2].technique_common.accessor.stride = 2; newMesh.source[2].technique_common.accessor.param = new param[2] { new param(), new param(), }; newMesh.source[2].technique_common.accessor.param[0].name = "S"; newMesh.source[2].technique_common.accessor.param[0].type = "float"; newMesh.source[2].technique_common.accessor.param[1].name = "T"; newMesh.source[2].technique_common.accessor.param[1].type = "float"; /* * newMesh.source[3].id = "geometry" + primgroups + "-colors-Col"; * float_array geom_colors = new float_array(); * geom_colors.id = "geometry" + primgroups + "-colors-Col-array"; * ColourList colors = SkinChunk.GetChildren<PrimitiveGroupCTTR>()[primgroups].GetChildren<ColourList>()[0]; * geom_colors.count = (ulong)colors.Colours.Length * 3; * geom_colors.Values = new double[geom_colors.count]; * for (ulong i = 0; i < geom_colors.count / 3; i++) * { * if (i % 3 == 0) * { * geom_colors.Values[i] = colors.Colours[i]; * } * else if (i % 3 == 1) * { * geom_colors.Values[i] = colors.Colours[i / 3]; * } * else * { * geom_colors.Values[i] = colors.Colours[i / 3]; * } * } * * newMesh.source[3].Item = geom_colors; * newMesh.source[3].technique_common = new sourceTechnique_common(); * newMesh.source[3].technique_common.accessor = new accessor(); * newMesh.source[3].technique_common.accessor.source = geom_colors.id; * newMesh.source[3].technique_common.accessor.count = geom_colors.count / 3; * newMesh.source[3].technique_common.accessor.stride = 3; * newMesh.source[3].technique_common.accessor.param = new param[3]; * newMesh.source[3].technique_common.accessor.param[0].name = "R"; * newMesh.source[3].technique_common.accessor.param[0].type = "float"; * newMesh.source[3].technique_common.accessor.param[1].name = "G"; * newMesh.source[3].technique_common.accessor.param[1].type = "float"; * newMesh.source[3].technique_common.accessor.param[2].name = "B"; * newMesh.source[3].technique_common.accessor.param[2].type = "float"; */ polylist meshTriangles = new polylist(); meshTriangles.material = ""; meshTriangles.count = (ulong)SkinChunk.GetChildren <PrimitiveGroupCTTR>()[primgroups].GetChildren <IndexList>()[0].Indices.Length / 4; meshTriangles.input = new InputLocalOffset[3]; meshTriangles.input[0] = new InputLocalOffset(); meshTriangles.input[0].semantic = "VERTEX"; meshTriangles.input[0].source = "#" + newMesh.vertices.id; meshTriangles.input[0].offset = 0; meshTriangles.input[1] = new InputLocalOffset(); meshTriangles.input[1].semantic = "NORMAL"; meshTriangles.input[1].source = "#" + "geometry" + primgroups + "-mesh-normals"; meshTriangles.input[1].offset = 1; meshTriangles.input[2] = new InputLocalOffset(); meshTriangles.input[2].semantic = "TEXCOORD"; meshTriangles.input[2].source = "#" + "geometry" + primgroups + "-mesh-map-0"; meshTriangles.input[2].offset = 2; meshTriangles.input[2].set = 0; string packed_primitives = ""; for (int i = 0; i < SkinChunk.GetChildren <PrimitiveGroupCTTR>()[primgroups].GetChildren <IndexList>()[0].Indices.Length; i++) { packed_primitives += SkinChunk.GetChildren <PrimitiveGroupCTTR>()[primgroups].GetChildren <IndexList>()[0].Indices[i]; packed_primitives += " "; } string packed_vcount = ""; for (int i = 0; i < SkinChunk.GetChildren <PrimitiveGroupCTTR>()[primgroups].GetChildren <IndexList>()[0].Indices.Length / 3; i++) { packed_vcount += "3 "; } meshTriangles.vcount = packed_vcount; meshTriangles.p = packed_primitives; newMesh.Items = new object[1]; newMesh.Items[0] = meshTriangles; cachedGeoms.geometry[primgroups].Item = newMesh; for (int i = 0; i < SkinChunk.GetChildren <PrimitiveGroupCTTR>()[primgroups].GetChildren <IndexList>()[0].Indices.Length; i++) { IndexList.Add(SkinChunk.GetChildren <PrimitiveGroupCTTR>()[primgroups].GetChildren <IndexList>()[0].Indices[i]); } } cachedMats.material = new material[ShaderChunks.Length]; List <string> shader_tex = new List <string>(); for (int mat = 0; mat < ShaderChunks.Length; mat++) { cachedMats.material[mat] = new material(); cachedMats.material[mat].id = "mat" + ShaderChunks[mat].Name; cachedMats.material[mat].name = ShaderChunks[mat].Name; cachedMats.material[mat].instance_effect = new instance_effect(); cachedMats.material[mat].instance_effect.url = "#" + ShaderChunks[mat].Name; //effect name if (ShaderChunks[mat].GetChildren <ShaderTextureParam>().Length > 0) { if (!shader_tex.Contains(ShaderChunks[mat].GetChildren <ShaderTextureParam>()[0].Value)) { shader_tex.Add(ShaderChunks[mat].GetChildren <ShaderTextureParam>()[0].Value); } } } cachedImages.image = new image[shader_tex.Count]; for (int i = 0; i < cachedImages.image.Length; i++) { cachedImages.image[i] = new image(); cachedImages.image[i].id = "image" + shader_tex[i]; //fx_surface_init_from_common init_from = new fx_surface_init_from_common(); //init_from.Value = shader_tex[i]; //cachedImages.image[i].Item = init_from; } cachedEffects.effect = new effect[ShaderChunks.Length]; for (int mat = 0; mat < ShaderChunks.Length; mat++) { cachedEffects.effect[mat] = new effect(); cachedEffects.effect[mat].id = ShaderChunks[mat].Name; cachedEffects.effect[mat].name = ShaderChunks[mat].Name; cachedEffects.effect[mat].newparam = new fx_newparam_common[2]; cachedEffects.effect[mat].newparam[0] = new fx_newparam_common(); cachedEffects.effect[mat].newparam[0].sid = "Image-surface"; cachedEffects.effect[mat].newparam[0].surface = new fx_surface_common(); cachedEffects.effect[mat].newparam[0].surface.type = fx_surface_type_enum.Item2D; cachedEffects.effect[mat].newparam[0].surface.init_from = new fx_surface_init_from_common[1]; cachedEffects.effect[mat].newparam[0].surface.init_from[0] = new fx_surface_init_from_common(); cachedEffects.effect[mat].newparam[0].surface.init_from[0].Value = ShaderChunks[mat].Name; cachedEffects.effect[mat].newparam[0].surface.format = "A8R8G8B8"; cachedEffects.effect[mat].newparam[1] = new fx_newparam_common(); cachedEffects.effect[mat].newparam[1].sid = "Image-sampler"; cachedEffects.effect[mat].newparam[1].sampler2D = new fx_sampler2D_common(); cachedEffects.effect[mat].newparam[1].sampler2D.source = "Image-surface"; cachedEffects.effect[mat].newparam[1].sampler2D.wrap_s = fx_sampler_wrap_common.CLAMP; cachedEffects.effect[mat].newparam[1].sampler2D.wrap_t = fx_sampler_wrap_common.CLAMP; cachedEffects.effect[mat].newparam[1].sampler2D.minfilter = fx_sampler_filter_common.NEAREST; cachedEffects.effect[mat].newparam[1].sampler2D.magfilter = fx_sampler_filter_common.NEAREST; cachedEffects.effect[mat].newparam[1].sampler2D.mipfilter = fx_sampler_filter_common.NEAREST; cachedEffects.effect[mat].Items = new effectFx_profile_abstractProfile_COMMON[1]; cachedEffects.effect[mat].Items[0] = new effectFx_profile_abstractProfile_COMMON(); cachedEffects.effect[mat].Items[0].technique = new effectFx_profile_abstractProfile_COMMONTechnique(); cachedEffects.effect[mat].Items[0].technique.sid = "common"; effectFx_profile_abstractProfile_COMMONTechniquePhong common_phong = new effectFx_profile_abstractProfile_COMMONTechniquePhong(); common_color_or_texture_typeTexture textureType = new common_color_or_texture_typeTexture(); textureType.texture = "Image-sampler"; textureType.texcoord = "tc"; common_phong.diffuse = new common_color_or_texture_type(); common_phong.diffuse.Item = textureType; common_phong.transparent = new common_transparent_type(); common_phong.transparent.Item = textureType; cachedEffects.effect[mat].Items[0].technique.Item = common_phong; } cachedControllers.controller = new controller[SkinChunk.NumPrimGroups]; for (int a = 0; a < SkinChunk.NumPrimGroups; a++) { cachedControllers.controller[a] = new controller(); cachedControllers.controller[a].id = SkinChunk.Name + a; cachedControllers.controller[a].name = SkinChunk.Name + a; skin ControllerSkin = new skin(); ControllerSkin.source1 = "#geometry" + a; ControllerSkin.source = new source[3]; ControllerSkin.source[0] = new source(); ControllerSkin.source[0].id = "controller" + a + "-joints"; Name_array Joints_Names = new Name_array(); Joints_Names.id = "controller" + a + "-joints-array"; Joints_Names.count = (ulong)SkeletonChunk.GetChildren <SkeletonJointCTTR>().Length; Joints_Names.Values = new string[Joints_Names.count]; for (ulong i = 0; i < Joints_Names.count; i++) { Joints_Names.Values[i] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].Name; } ControllerSkin.source[0].Item = Joints_Names; ControllerSkin.source[0].technique_common = new sourceTechnique_common(); ControllerSkin.source[0].technique_common.accessor = new accessor(); ControllerSkin.source[0].technique_common.accessor.source = "#" + Joints_Names.id; ControllerSkin.source[0].technique_common.accessor.count = Joints_Names.count; ControllerSkin.source[0].technique_common.accessor.param = new param[1] { new param() }; ControllerSkin.source[0].technique_common.accessor.param[0].name = "JOINT"; ControllerSkin.source[0].technique_common.accessor.param[0].type = "Name"; ControllerSkin.source[1] = new source(); ControllerSkin.source[1].id = "controller" + a + "-bind_poses"; float_array Bind_Poses = new float_array(); Bind_Poses.id = "controller" + a + "-bind_poses-array"; Bind_Poses.count = (ulong)SkeletonChunk.GetChildren <SkeletonJointCTTR>().Length * 16; Bind_Poses.Values = new double[Bind_Poses.count]; int bind_pose_pos = 0; for (ulong i = 0; i < Joints_Names.count; i++) { Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M11; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M12; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M13; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M14; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M21; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M22; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M23; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M24; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M31; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M32; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M33; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M34; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M41; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M42; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M43; bind_pose_pos++; Bind_Poses.Values[bind_pose_pos] = SkeletonChunk.GetChildren <SkeletonJointCTTR>()[i].RestPose.M44; bind_pose_pos++; } ControllerSkin.source[1].Item = Bind_Poses; ControllerSkin.source[1].technique_common = new sourceTechnique_common(); ControllerSkin.source[1].technique_common.accessor = new accessor(); ControllerSkin.source[1].technique_common.accessor.source = "#" + Bind_Poses.id; ControllerSkin.source[1].technique_common.accessor.count = Bind_Poses.count; ControllerSkin.source[1].technique_common.accessor.param = new param[1] { new param() }; ControllerSkin.source[1].technique_common.accessor.param[0].name = "TRANSFORM"; ControllerSkin.source[1].technique_common.accessor.param[0].type = "float4x4"; ControllerSkin.source[2] = new source(); ControllerSkin.source[2].id = "controller" + a + "-weights"; float_array Weights_Array = new float_array(); Weights_Array.id = "controller" + a + "-weights-array"; Weights_Array.count = Joints_Names.count; Weights_Array.Values = new double[Joints_Names.count]; for (int i = 0; i < Weights_Array.Values.Length; i++) { Weights_Array.Values[i] = 1; } ControllerSkin.source[2].Item = Weights_Array; ControllerSkin.source[2].technique_common = new sourceTechnique_common(); ControllerSkin.source[2].technique_common.accessor = new accessor(); ControllerSkin.source[2].technique_common.accessor.source = "#" + Weights_Array.id; ControllerSkin.source[2].technique_common.accessor.count = Weights_Array.count; ControllerSkin.source[2].technique_common.accessor.param = new param[1] { new param() }; ControllerSkin.source[2].technique_common.accessor.param[0].name = "WEIGHT"; ControllerSkin.source[2].technique_common.accessor.param[0].type = "float"; ControllerSkin.joints = new skinJoints(); ControllerSkin.joints.input = new InputLocal[2] { new InputLocal(), new InputLocal() }; ControllerSkin.joints.input[0].semantic = "JOINT"; ControllerSkin.joints.input[0].source = "#" + ControllerSkin.source[0].id; ControllerSkin.joints.input[1].semantic = "INV_BIND_MATRIX"; ControllerSkin.joints.input[1].source = "#" + ControllerSkin.source[1].id; ControllerSkin.vertex_weights = new skinVertex_weights(); ControllerSkin.vertex_weights.count = (ulong)IndexList.Count; ControllerSkin.vertex_weights.input = new InputLocalOffset[2] { new InputLocalOffset(), new InputLocalOffset() }; ControllerSkin.vertex_weights.input[0].semantic = "JOINT"; ControllerSkin.vertex_weights.input[0].source = "#" + ControllerSkin.source[0].id; ControllerSkin.vertex_weights.input[0].offset = 0; ControllerSkin.vertex_weights.input[1].semantic = "WEIGHT"; ControllerSkin.vertex_weights.input[1].source = "#" + ControllerSkin.source[2].id; ControllerSkin.vertex_weights.input[1].offset = 1; string vertex_vcount = ""; for (int i = 0; i < IndexList.Count; i++) { vertex_vcount += "1 "; } ControllerSkin.vertex_weights.vcount = vertex_vcount; string index_list_str = ""; for (int i = 0; i < IndexList.Count; i++) { index_list_str += IndexList[i] + " "; } ControllerSkin.vertex_weights.v = index_list_str; cachedControllers.controller[a].Item = ControllerSkin; } cachedVscenes.visual_scene = new visual_scene[1] { new visual_scene() }; cachedVscenes.visual_scene[0].id = "Scene0"; cachedVscenes.visual_scene[0].name = "Scene0"; cachedVscenes.visual_scene[0].node = new node[SkinChunk.NumPrimGroups + 1]; cachedVscenes.visual_scene[0].node[0] = new node(); //cachedVscenes.visual_scene[0].node[0].Items = new object[1]; //cachedVscenes.visual_scene[0].node[0].Items[0] = new matrix(); for (int i = 0; i < SkeletonChunk.Children.Count; i++) { SkeletonJointCTTR joint = (SkeletonJointCTTR)SkeletonChunk.Children[i]; if (joint.SkeletonParent == 0) { //cachedVscenes.visual_scene[0].node[0].node1 = new node[10]; } } cachedVscenes.visual_scene[0].node[0].id = SkeletonChunk.Name; cachedVscenes.visual_scene[0].node[0].type = NodeType.JOINT; //todo: joints node tree // SkeletonParent - 0 means root, the rest is chunk child index // SkeletonJointCTTR.BindPose matrix as transform matrix? for (int i = 1; i < SkinChunk.NumPrimGroups + 1; i++) { cachedVscenes.visual_scene[0].node[i] = new node(); cachedVscenes.visual_scene[0].node[i].id = "node" + i; cachedVscenes.visual_scene[0].node[i].name = "polygon" + i; cachedVscenes.visual_scene[0].node[i].type = NodeType.NODE; cachedVscenes.visual_scene[0].node[i].instance_controller = new instance_controller[1] { new instance_controller() }; cachedVscenes.visual_scene[0].node[i].instance_controller[0].url = "#" + cachedControllers.controller[i - 1].id; cachedVscenes.visual_scene[0].node[i].instance_controller[0].skeleton = new string[1] { "#" + cachedVscenes.visual_scene[0].node[0].id }; cachedVscenes.visual_scene[0].node[i].instance_controller[0].bind_material = new bind_material(); cachedVscenes.visual_scene[0].node[i].instance_controller[0].bind_material.technique_common = new instance_material[1] { new instance_material() }; cachedVscenes.visual_scene[0].node[i].instance_controller[0].bind_material.technique_common[0].symbol = "#" + cachedMats.material[0].id; cachedVscenes.visual_scene[0].node[i].instance_controller[0].bind_material.technique_common[0].target = "#" + cachedMats.material[0].id; cachedVscenes.visual_scene[0].node[i].instance_controller[0].bind_material.technique_common[0].bind_vertex_input = new instance_materialBind_vertex_input[1] { new instance_materialBind_vertex_input() }; cachedVscenes.visual_scene[0].node[i].instance_controller[0].bind_material.technique_common[0].bind_vertex_input[0].semantic = "tc"; cachedVscenes.visual_scene[0].node[i].instance_controller[0].bind_material.technique_common[0].bind_vertex_input[0].input_semantic = "TEXCOORD"; } //not working: // - vertex weights error out // - mesh comes out wrong //animclips //anims }
void AddFacesFromPolyList(polylist list, mesh mesh, ModelPrim prim, Matrix4 transform) { string material = list.material; source posSrc = null; source normalSrc = null; source uvSrc = null; ulong stride = 0; int posOffset = -1; int norOffset = -1; int uvOffset = -1; foreach (var inp in list.input) { stride = Math.Max(stride, inp.offset); switch (inp.semantic) { case "VERTEX": posSrc = FindSource(mesh.source, mesh.vertices.input[0].source); posOffset = (int)inp.offset; break; case "NORMAL": normalSrc = FindSource(mesh.source, inp.source); norOffset = (int)inp.offset; break; case "TEXCOORD": uvSrc = FindSource(mesh.source, inp.source); uvOffset = (int)inp.offset; break; } } stride += 1; if (posSrc == null) { return; } var vcount = StrToArray(list.vcount); var idx = StrToArray(list.p); Vector3[] normals = null; if (normalSrc != null) { var norVal = ((float_array)normalSrc.Item).Values; normals = new Vector3[norVal.Length / 3]; for (int i = 0; i < normals.Length; i++) { normals[i] = new Vector3((float)norVal[i * 3 + 0], (float)norVal[i * 3 + 1], (float)norVal[i * 3 + 2]); normals[i] = Vector3.TransformNormal(normals[i], transform); normals[i].Normalize(); } } Vector2[] uvs = null; if (uvSrc != null) { var uvVal = ((float_array)uvSrc.Item).Values; uvs = new Vector2[uvVal.Length / 2]; for (int i = 0; i < uvs.Length; i++) { uvs[i] = new Vector2((float)uvVal[i * 2 + 0], (float)uvVal[i * 2 + 1]); } } ModelFace face = new ModelFace { MaterialID = list.material }; if (face.MaterialID != null) { if (MatSymTarget.ContainsKey(list.material)) { ModelMaterial mat = Materials.Find(m => m.ID == MatSymTarget[list.material]); if (mat != null) { face.Material = mat; } } } int curIdx = 0; foreach (var nvert in vcount) { if (nvert < 3 || nvert > 4) { throw new InvalidDataException("Only triangles and quads supported"); } Vertex[] verts = new Vertex[nvert]; for (int j = 0; j < nvert; j++) { verts[j].Position = prim.Positions[idx[curIdx + posOffset + (int)stride * j]]; if (normals != null) { verts[j].Normal = normals[idx[curIdx + norOffset + (int)stride * j]]; } if (uvs != null) { verts[j].TexCoord = uvs[idx[curIdx + uvOffset + (int)stride * j]]; } } switch (nvert) { case 3: face.AddVertex(verts[0]); face.AddVertex(verts[1]); face.AddVertex(verts[2]); break; case 4: face.AddVertex(verts[0]); face.AddVertex(verts[1]); face.AddVertex(verts[2]); face.AddVertex(verts[0]); face.AddVertex(verts[2]); face.AddVertex(verts[3]); break; } curIdx += (int)stride * nvert; } prim.Faces.Add(face); }
internal DAEGeometry(DAELoaderNode loader, geometry geo) { if (geo.Item == null || !(geo.Item is mesh)) { // empty or not supported geometry return; } mesh m = geo.Item as mesh; // load sources Dictionary <string, MeshSource> sources = new Dictionary <string, MeshSource>(); foreach (source src in m.source) { if (src.Item is float_array) { float_array values = src.Item as float_array; int stride = (int)src.technique_common.accessor.stride; sources.Add(src.id, new MeshSource(values.Values, stride)); } } // load positions foreach (InputLocal input in m.vertices.input) { if (input.semantic.Equals("POSITION")) { sources.Add(m.vertices.id, sources[DAEUtils.GetUrl(input.source).Id]); break; } } // load primitives foreach (var item in m.Items) { // load triangles if (item is triangles) { triangles tris = item as triangles; DAEVertexDescription[] vertices = new DAEVertexDescription[tris.count * 3]; int[] p = DAEUtils.StringToIntArray(tris.p); int stepSize = p.Length / ((int)tris.count * 3); foreach (InputLocalOffset input in tris.input) { SetVertices(input, p, stepSize, sources, vertices); } if (!ContainsTangents(tris.input)) { GenerateMeshTangents(vertices); } _triangles.Add(new DAETriangles(loader, vertices, tris.material)); } else if (item is polylist) { polylist polys = item as polylist; int[] polyVertexCounts = DAEUtils.StringToIntArray(polys.vcount); int vertexCount = 0; int triCount = 0; for (int i = 0; i < polyVertexCounts.Length; i++) { vertexCount += polyVertexCounts[i]; triCount += polyVertexCounts[i] - 2; } DAEVertexDescription[] polyVertices = new DAEVertexDescription[vertexCount]; int[] p = DAEUtils.StringToIntArray(polys.p); int stepSize = p.Length / vertexCount; foreach (InputLocalOffset input in polys.input) { SetVertices(input, p, stepSize, sources, polyVertices); } // triangulation DAEVertexDescription[] triVertices = new DAEVertexDescription[triCount * 3]; int triVertexIdx = 0; int polyVertexIdx = 0; for (int i = 0; i < polyVertexCounts.Length; i++) { int triVertex = 0; for (int k = 0; k < polyVertexCounts[i]; k++) { if (triVertex == 3) { triVertex = 1; k--; // repeat first vertex triVertices[triVertexIdx++] = polyVertices[polyVertexIdx]; } triVertices[triVertexIdx++] = polyVertices[polyVertexIdx + k]; triVertex++; } // skip to next polygon polyVertexIdx += polyVertexCounts[i]; } if (!ContainsTangents(polys.input)) { GenerateMeshTangents(triVertices); } _triangles.Add(new DAETriangles(loader, triVertices, polys.material)); } } }
private void LoadGeometryFromCollada(CollisionGroupNode parent, geometry geo) { mesh m = geo.Item as mesh; // For safety, read the model's definition of where the position data is // and grab it from there. We could just do a search for "position" in the // source list names, but this makes sure there are no errors. InputLocal pos_input = Array.Find(m.vertices.input, x => x.semantic == "POSITION"); source pos_src = Array.Find(m.source, x => x.id == pos_input.source.Trim('#')); float_array pos_arr = pos_src.Item as float_array; // For some reason Maya puts a leading space in the face index data, // so we need to trim that out before trying to parse the index string. string[] indices; int stride; if (m.Items[0].GetType() == typeof(triangles)) { triangles tris = m.Items[0] as triangles; indices = tris.p.Trim(' ').Split(' '); stride = tris.input.Length; // Make sure this tool can support meshes with multiple vertex attributes. } else if (m.Items[0].GetType() == typeof(polylist)) { polylist polys = m.Items[0] as polylist; indices = polys.p.Trim(' ').Split(' '); stride = polys.input.Length; // Make sure this tool can support meshes with multiple vertex attributes. } else { throw new Exception($"Unsupported polygon type for mesh {geo.name}: {m.Items[0].GetType()}"); } for (int i = 0; i < indices.Length; i += stride * 3) { int vec1_index = Convert.ToInt32(indices[i]); int vec2_index = Convert.ToInt32(indices[i + stride]); int vec3_index = Convert.ToInt32(indices[i + (stride * 2)]); Vector3 vec1 = new Vector3((float)pos_arr.Values[vec1_index * 3], (float)pos_arr.Values[(vec1_index * 3) + 1], (float)pos_arr.Values[(vec1_index * 3) + 2]); Vector3 vec2 = new Vector3((float)pos_arr.Values[vec2_index * 3], (float)pos_arr.Values[(vec2_index * 3) + 1], (float)pos_arr.Values[(vec2_index * 3) + 2]); Vector3 vec3 = new Vector3((float)pos_arr.Values[vec3_index * 3], (float)pos_arr.Values[(vec3_index * 3) + 1], (float)pos_arr.Values[(vec3_index * 3) + 2]); // The benefit of using this library is that we easily got the up-axis // info from the file. If the up-axis was defined as Z-up, we need to // swap the Y and Z components of our vectors so the mesh isn't sideways. // (The Wind Waker is Y-up.) if (m_UpAxis == UpAxisType.Z_UP) { vec1 = SwapYZ(vec1); vec2 = SwapYZ(vec2); vec3 = SwapYZ(vec3); } CollisionTriangle new_tri = new CollisionTriangle(vec1, vec2, vec3, parent); parent.Triangles.Add(new_tri); Triangles.Add(new_tri); } }