public static void ApplyColor(GameObject rootModel, AppearData appearData) { SkinnedMeshRenderer targetSmr = GetTargetSmr(rootModel); if (targetSmr == null) { return; } for (int i = 0; i < targetSmr.sharedMaterials.Length; i++) { SetColor(targetSmr.sharedMaterials[i], appearData); } }
// Public functions public static IEnumerator BuildAsync(GameObject rootModel, AppearData appearData, IEnumerable <string> strPartInfos) { //Profiler.BeginSample ("Prepare"); List <CustomPartInfo> partInfos = new List <CustomPartInfo>(); Dictionary <string, Transform> targetBones = new Dictionary <string, Transform> (); SkinnedMeshRenderer targetSmr = GetTargetSmr(rootModel); GetPartInfos(strPartInfos, partInfos); GetTargetBones(rootModel, targetBones); //Profiler.EndSample (); //Profiler.BeginSample ("BuildMesh"); BuildMesh(partInfos, targetBones, targetSmr, appearData); //Profiler.EndSample (); yield return(0); for (int i = 0; i < targetSmr.sharedMaterials.Length; i++) { GameObject.Destroy(targetSmr.sharedMaterials[i]); } List <Material> materials = new List <Material>(); Material mat; CustomPartInfo partInfo; int nParts = partInfos.Count; for (int i = 0; i < nParts; i++) { partInfo = partInfos[i]; //Profiler.BeginSample("MatN"); int nMats = partInfo.Smr.sharedMaterials.Length; //Profiler.EndSample(); yield return(0); for (int j = 0; j < nMats; j++) { //Profiler.BeginSample("MatAdd"); mat = GameObject.Instantiate(partInfo.Smr.sharedMaterials[j]); SetColor(mat, appearData); materials.Add(mat); //Profiler.EndSample(); yield return(0); } } targetSmr.materials = materials.ToArray(); ////Profiler.BeginSample ("BuildMat"); //BuildMat(partInfos, targetSmr, appearData); ////Profiler.EndSample (); }
public static SkinnedMeshRenderer Build(GameObject rootModel, AppearData appearData, IEnumerable <string> strPartInfos) { List <CustomPartInfo> partInfos = new List <CustomPartInfo>(); Dictionary <string, Transform> targetBones = new Dictionary <string, Transform> (); SkinnedMeshRenderer targetSmr = GetTargetSmr(rootModel); GetPartInfos(strPartInfos, partInfos); GetTargetBones(rootModel, targetBones); //Profiler.BeginSample ("BuildMesh"); BuildMesh(partInfos, targetBones, targetSmr, appearData); //Profiler.EndSample (); //Profiler.BeginSample ("BuildMat"); BuildMat(partInfos, targetSmr, appearData); //Profiler.EndSample (); return(targetSmr); }
static void SetColor(Material pMat, AppearData appearData) { if (pMat.name.Contains("eye")) { pMat.SetColor("_SkinColor", appearData.mEyeColor); } else if (pMat.name.Contains("hair")) { pMat.SetColor("_Color", appearData.mHairColor); } else if (pMat.name.Contains("head")) { pMat.SetColor("_Color", appearData.skinColor); } else if (!pMat.name.Contains("Helmet")) { pMat.SetColor("_SkinColor", appearData.skinColor); } else if (pMat.name.Contains("Helmet_10A")) // Special code for Gold helmet { /* * pMat.SetFloat("_Mode", 2); * pMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); * pMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); * pMat.SetInt("_ZWrite", 0); * pMat.DisableKeyword("_ALPHATEST_ON"); * pMat.EnableKeyword("_ALPHABLEND_ON"); * pMat.DisableKeyword("_ALPHAPREMULTIPLY_ON"); * pMat.renderQueue = 3000; */ pMat.SetFloat("_Mode", 3); pMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); pMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); pMat.SetInt("_ZWrite", 0); pMat.DisableKeyword("_ALPHATEST_ON"); pMat.DisableKeyword("_ALPHABLEND_ON"); pMat.EnableKeyword("_ALPHAPREMULTIPLY_ON"); pMat.renderQueue = 3000; } }
static Mesh BakeToMesh(CustomPartInfo partInfo, AppearData appearData) { SkinnedMeshRenderer smr = partInfo.Smr; if (string.IsNullOrEmpty(partInfo.ModelName) || smr.sharedMesh.blendShapeCount == 0) { return(smr.sharedMesh); } EMorphItem indexStart = EMorphItem.Max; EMorphItem indexEnd = EMorphItem.Max; if (partInfo.ModelName.Contains("head")) { indexStart = EMorphItem.MinFace; indexEnd = EMorphItem.MaxFace; } else if (partInfo.ModelName.Contains("torso")) { indexStart = EMorphItem.MinUpperbody; indexEnd = EMorphItem.MaxUpperbody; } else if (partInfo.ModelName.Contains("legs")) { indexStart = EMorphItem.MinLowerbody; indexEnd = EMorphItem.MaxLowerbody; } else if (partInfo.ModelName.Contains("feet")) { indexStart = EMorphItem.MinFoot; indexEnd = EMorphItem.MaxFoot; } else if (partInfo.ModelName.Contains("hand")) { indexStart = EMorphItem.MinHand; indexEnd = EMorphItem.MaxHand; } if (indexStart != indexEnd) { _tmpWeightIdxs.Clear(); _tmpWeightVals.Clear(); for (int i = (int)indexStart; i < (int)indexEnd; i++) { float w = appearData.GetWeight((EMorphItem)i) * 100f; int index = i - (int)indexStart; if (w > 0f) { index = index * 2; } else { w = -w; index = index * 2 + 1; } if (smr.sharedMesh.blendShapeCount > index) { _tmpWeightIdxs.Add(index); _tmpWeightVals.Add(smr.GetBlendShapeWeight(index)); smr.SetBlendShapeWeight(index, w); } } Mesh msh = PeekTmpMesh(); smr.BakeMesh(msh); msh.boneWeights = smr.sharedMesh.boneWeights; msh.bindposes = smr.sharedMesh.bindposes; if (_tmpWeightIdxs.Count > 0) { for (int i = 0; i < _tmpWeightIdxs.Count; i++) { smr.SetBlendShapeWeight(_tmpWeightIdxs[i], _tmpWeightVals[i]); } } return(msh); } return(smr.sharedMesh); }
static void BuildMat(List <CustomPartInfo> partInfos, SkinnedMeshRenderer targetSmr, AppearData appearData) { for (int i = 0; i < targetSmr.sharedMaterials.Length; i++) { GameObject.Destroy(targetSmr.sharedMaterials[i]); } List <Material> materials = new List <Material>(); Material mat; CustomPartInfo partInfo; int nParts = partInfos.Count; for (int i = 0; i < nParts; i++) { partInfo = partInfos[i]; //Profiler.BeginSample("MatAdd"); for (int j = 0; j < partInfo.Smr.sharedMaterials.Length; j++) { mat = GameObject.Instantiate(partInfo.Smr.sharedMaterials[j]); SetColor(mat, appearData); materials.Add(mat); } //Profiler.EndSample(); } targetSmr.materials = materials.ToArray(); }
// Build Mesh/Mat static void BuildMesh(List <CustomPartInfo> partInfos, Dictionary <string, Transform> targetBones, SkinnedMeshRenderer targetSmr, AppearData appearData) { targetSmr.sharedMesh.Clear(false); CustomPartInfo partInfo; Mesh mesh; ResetTmpMeshes(); _tmpOutBones.Clear(); _tmpCombineInstances.Clear(); //Profiler.BeginSample ("PartInfo"); int nParts = partInfos.Count; for (int i = 0; i < nParts; i++) { partInfo = partInfos[i]; mesh = BakeToMesh(partInfo, appearData); for (int sub = 0; sub < mesh.subMeshCount; sub++) { CombineInstance ci = new CombineInstance(); ci.mesh = mesh; ci.subMeshIndex = sub; _tmpCombineInstances.Add(ci); //all sub mesh must find bones //Profiler.BeginSample("FindBones"); CopyBonesByName(partInfo.Smr.bones, targetBones, _tmpOutBones); //Profiler.EndSample(); } } //Profiler.EndSample (); //Profiler.BeginSample ("Combine"); targetSmr.sharedMesh.CombineMeshes(_tmpCombineInstances.ToArray(), false, false); //Profiler.EndSample (); targetSmr.bones = _tmpOutBones.ToArray(); }
void SetAppearData(AppearData data) { mAppearData = data; }