// ----------------------------------------- // 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); } }
// ----------------------------------------- // DumpAnims // ----------------------------------------- void DumpAnims(GameObject _animPrefab, GLTF _gltf, BufferInfo _bufInfo) { // get animations GameObject prefabInst = PrefabUtility.InstantiatePrefab(_animPrefab) as GameObject; List <AnimationClip> clips = Utils.GetAnimationClips(prefabInst); // get joints List <GameObject> joints = new List <GameObject>(); Utils.RecurseNode(prefabInst, _go => { // this is not a joint if (_go.GetComponent <SkinnedMeshRenderer>() != null) { return(false); } joints.Add(_go); return(true); }); // if (clips != null) { // process AnimationClip(s) foreach (AnimationClip clip in clips) { int accOffset = _bufInfo.GetAccessorCount(); AnimData animData = DumpAnimData(prefabInst, clip); DumpBufferInfoFromAnimData(animData, _bufInfo); GLTF_AnimationEx gltfAnim = DumpGltfAnimationEx(animData, joints, accOffset); _gltf.animations.Add(gltfAnim); } } Object.DestroyImmediate(prefabInst); }
// ----------------------------------------- // 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); } } }