SkinnedMeshCombiner

Description


This script will combine skinned meshes whilst retaining their animation and texture properties. Currently it's not tested with multiple materials, as that was not required for what we needed it for.

Use


Add this script to a master GO, with numerous skinned mesh child GOs. Run scene. Profit.

Requirements


Your child GOs must have the skinned mesh, on a separate GO, for example the hierarchy should look like this: GO - This script on here.

CHILD GO - A gameobject that serves as a container for the other components, can have animation data, or, misc components on that do not affect skinned meshes.
SMC GO - A gameobject with -just- the skinned mesh renderer on it, I mean, you can put other things on here, but they will get destroyed when the script deletes this object.
Another GO - Misc stuff, this GO will remain intact/functional after you run the scene/simulation.
CHILD GO
CHILD GO


SkinnedMeshCombiner.cs

using UnityEngine;
using System.Collections.Generic;

public class SkinnedMeshCombiner : MonoBehaviour {

void Start() { 
SkinnedMeshRenderer[] smRenderers = GetComponentsInChildren();
List bones = new List(); 
List boneWeights = new List(); 
List combineInstances = new List();
List textures = new List();
int numSubs = 0;

foreach(SkinnedMeshRenderer smr in smRenderers)
numSubs += smr.sharedMesh.subMeshCount;

int[] meshIndex = new int[numSubs];
int boneOffset = 0;
for( int s = 0; s < smRenderers.Length; s++ ) {
SkinnedMeshRenderer smr = smRenderers[s]; 

BoneWeight[] meshBoneweight = smr.sharedMesh.boneWeights;

// May want to modify this if the renderer shares bones as unnecessary bones will get added.
foreach( BoneWeight bw in meshBoneweight ) {
BoneWeight bWeight = bw;

bWeight.boneIndex0 += boneOffset;
bWeight.boneIndex1 += boneOffset;
bWeight.boneIndex2 += boneOffset;
bWeight.boneIndex3 += boneOffset; 

boneWeights.Add( bWeight );
}
boneOffset += smr.bones.Length;

Transform[] meshBones = smr.bones;
foreach( Transform bone in meshBones )
bones.Add( bone );

if( smr.material.mainTexture != null )
textures.Add( smr.renderer.material.mainTexture as Texture2D );

CombineInstance ci = new CombineInstance();
ci.mesh = smr.sharedMesh;
meshIndex[s] = ci.mesh.vertexCount;
ci.transform = smr.transform.localToWorldMatrix;
combineInstances.Add( ci );

Object.Destroy( smr.gameObject );
}

List bindposes = new List();

for( int b = 0; b < bones.Count; b++ ) {
bindposes.Add( bones[b].worldToLocalMatrix * transform.worldToLocalMatrix );
}

SkinnedMeshRenderer r = gameObject.AddComponent ();
r.sharedMesh = new Mesh();
r.sharedMesh.CombineMeshes( combineInstances.ToArray(), true, true );

Texture2D skinnedMeshAtlas = new Texture2D( 128, 128 );
Rect[] packingResult = skinnedMeshAtlas.PackTextures( textures.ToArray(), 0 );
Vector2[] originalUVs = r.sharedMesh.uv;
Vector2[] atlasUVs = new Vector2[originalUVs.Length];

int rectIndex = 0;
int vertTracker = 0;
for( int i = 0; i < atlasUVs.Length; i++ ) {
atlasUVs[i].x = Mathf.Lerp( packingResult[rectIndex].xMin, packingResult[rectIndex].xMax, originalUVs[i].x );
atlasUVs[i].y = Mathf.Lerp( packingResult[rectIndex].yMin, packingResult[rectIndex].yMax, originalUVs[i].y ); 

if( i >= meshIndex[rectIndex] + vertTracker ) { 
vertTracker += meshIndex[rectIndex];
rectIndex++; 
}
}

Material combinedMat = new Material( Shader.Find( "Diffuse" ) );
combinedMat.mainTexture = skinnedMeshAtlas;
r.sharedMesh.uv = atlasUVs;
r.sharedMaterial = combinedMat;

r.bones = bones.ToArray();
r.sharedMesh.boneWeights = boneWeights.ToArray();
r.sharedMesh.bindposes = bindposes.ToArray();
r.sharedMesh.RecalculateBounds();
}
}

原链接: http://wiki.unity3d.com/index.php?title=SkinnedMeshCombiner
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值