// ----------------------------------------- // DumpGltfNode // ----------------------------------------- GLTF_Node DumpGltfNode(GameObject _go, List <GameObject> _nodes) { GLTF_Node result = new GLTF_Node(); // name result.name = _go.name; // NOTE: skinned mesh node will use identity matrix if (_go.GetComponent <SkinnedMeshRenderer>() != null) { // translation result.translation[0] = 0.0f; result.translation[1] = 0.0f; result.translation[2] = 0.0f; // rotation result.rotation[0] = 0.0f; result.rotation[1] = 0.0f; result.rotation[2] = 0.0f; result.rotation[3] = 1.0f; // scale result.scale[0] = 1.0f; result.scale[1] = 1.0f; result.scale[2] = 1.0f; } else { // translation // NOTE: convert LH to RH result.translation[0] = _go.transform.localPosition.x; result.translation[1] = _go.transform.localPosition.y; result.translation[2] = -_go.transform.localPosition.z; // rotation // NOTE: convert LH to RH result.rotation[0] = -_go.transform.localRotation.x; result.rotation[1] = -_go.transform.localRotation.y; result.rotation[2] = _go.transform.localRotation.z; result.rotation[3] = _go.transform.localRotation.w; // scale result.scale[0] = _go.transform.localScale.x; result.scale[1] = _go.transform.localScale.y; result.scale[2] = _go.transform.localScale.z; } // children result.children = new List <int>(); foreach (Transform child in _go.transform) { int idx = _nodes.IndexOf(child.gameObject); if (idx != -1) { result.children.Add(idx); } } return(result); }
// ----------------------------------------- // DumpModel // ----------------------------------------- void DumpModel(GameObject _prefab, GLTF _gltf, BufferInfo _bufInfo) { // get nodes List <GameObject> nodes = new List <GameObject>(); RecurseNode(_prefab, _go => { nodes.Add(_go); return(true); }); string assetPath = AssetDatabase.GetAssetPath(_prefab); var meshes = AssetDatabase.LoadAllAssetsAtPath(assetPath).OfType <Mesh>().ToList(); // get meshes // List<Mesh> meshes = new List<Mesh>(); // RecurseNode(_prefab, _go => { // MeshFilter meshFilter = _go.GetComponent<MeshFilter>(); // if (meshFilter && meshes.IndexOf(meshFilter.sharedMesh) == -1 ) { // meshes.Add(meshFilter.sharedMesh); // } // return true; // }); // dump nodes foreach (GameObject go in nodes) { GLTF_Node gltfNode = DumpGltfNode(go, nodes); MeshFilter meshFilter = go.GetComponent <MeshFilter>(); if (meshFilter != null) { gltfNode.mesh = meshes.IndexOf(meshFilter.sharedMesh); } _gltf.nodes.Add(gltfNode); } // dump meshes int accOffset = 0; foreach (Mesh mesh in meshes) { // dump mesh accOffset = _bufInfo.GetAccessorCount(); DumpMesh(mesh, _gltf, _bufInfo, accOffset); } }
// ----------------------------------------- // DumpSkinningModel // ----------------------------------------- void DumpSkinningModel(GameObject _prefab, GLTF _gltf, BufferInfo _bufInfo) { // get joints List <GameObject> joints = new List <GameObject>(); Utils.RecurseNode(_prefab, _go => { // this is not a joint if (_go.GetComponent <SkinnedMeshRenderer>() != null) { return(false); } joints.Add(_go); return(true); }); // get nodes List <GameObject> nodes = new List <GameObject>(); Utils.RecurseNode(_prefab, _go => { // this is a joint, skip it. if (_go.GetComponents <Component>().Length == 1) { return(false); } nodes.Add(_go); return(true); }); // get skins & meshes List <Mesh> meshes = new List <Mesh>(); List <SkinnedMeshRenderer> smrList = new List <SkinnedMeshRenderer>(); Utils.RecurseNode(_prefab, _go => { SkinnedMeshRenderer smr = _go.GetComponent <SkinnedMeshRenderer>(); if (smr != null) { meshes.Add(smr.sharedMesh); smrList.Add(smr); } return(true); }); // dump nodes foreach (GameObject go in nodes) { GLTF_Node gltfNode = DumpGltfNode(go, nodes); SkinnedMeshRenderer smr = go.GetComponent <SkinnedMeshRenderer>(); if (smr != null) { gltfNode.mesh = meshes.IndexOf(smr.sharedMesh); gltfNode.skin = smrList.IndexOf(smr); } _gltf.nodes.Add(gltfNode); } // dump joints foreach (GameObject go in joints) { GLTF_Node gltfNode = DumpGltfNode(go, joints); _gltf.joints.Add(gltfNode); } // dump meshes & skin int accOffset = 0; foreach (SkinnedMeshRenderer smr in smrList) { // dump mesh accOffset = _bufInfo.GetAccessorCount(); DumpMesh(smr.sharedMesh, _gltf, _bufInfo, accOffset); // dump skin int accBindposesIdx = _bufInfo.GetAccessorCount() - 1; GameObject rootBone = Utils.GetRootBone(smr, _prefab).gameObject; GLTF_Skin gltfSkin = DumpGtlfSkin(smr, joints, rootBone, accBindposesIdx); if (gltfSkin != null) { _gltf.skins.Add(gltfSkin); } } }