protected void BuildCombineInstances()
        {
            SkinnedMeshCombiner.CombineInstance combineInstance;

            //Since BuildCombineInstances is called within a renderer loop, use a variable to keep track of the materialIndex per renderer
            int rendererMaterialIndex = 0;

            for (int materialIndex = 0; materialIndex < umaData.generatedMaterials.materials.Count; materialIndex++)
            {
                UMARendererAsset rendererAsset = umaData.GetRendererAsset(currentRendererIndex);
                var generatedMaterial          = umaData.generatedMaterials.materials[materialIndex];
                if (generatedMaterial.rendererAsset != rendererAsset)
                {
                    continue;
                }
                combinedMaterialList.Add(generatedMaterial);
                generatedMaterial.materialIndex = materialIndex;

                for (int materialDefinitionIndex = 0; materialDefinitionIndex < generatedMaterial.materialFragments.Count; materialDefinitionIndex++)
                {
                    var materialDefinition = generatedMaterial.materialFragments[materialDefinitionIndex];
                    var slotData           = materialDefinition.slotData;
                    combineInstance          = new SkinnedMeshCombiner.CombineInstance();
                    combineInstance.meshData = slotData.asset.meshData;

                    //New MeshHiding
                    if (slotData.meshHideMask != null)
                    {
                        combineInstance.triangleMask = slotData.meshHideMask;
                    }

                    combineInstance.targetSubmeshIndices = new int[combineInstance.meshData.subMeshCount];
                    for (int i = 0; i < combineInstance.meshData.subMeshCount; i++)
                    {
                        combineInstance.targetSubmeshIndices[i] = -1;
                    }
                    combineInstance.targetSubmeshIndices[slotData.asset.subMeshIndex] = rendererMaterialIndex;
                    combinedMeshList.Add(combineInstance);

                    if (slotData.asset.SlotAtlassed != null)
                    {
                        slotData.asset.SlotAtlassed.Invoke(umaData, slotData, generatedMaterial.material, materialDefinition.atlasRegion);
                    }
                    if (rendererAsset != null && rendererAsset.ClothProperties != null)
                    {
                        clothProperties = rendererAsset.ClothProperties;
                    }
                }
                rendererMaterialIndex++;
            }
        }
예제 #2
0
        /// <summary>
        /// Updates the UMA mesh and skeleton to match current slots.
        /// </summary>
        /// <param name="updatedAtlas">If set to <c>true</c> atlas has changed.</param>
        /// <param name="umaData">UMA data.</param>
        /// <param name="atlasResolution">Atlas resolution.</param>
        public override void UpdateUMAMesh(bool updatedAtlas, UMAData umaData, int atlasResolution)
        {
            this.umaData         = umaData;
            this.atlasResolution = atlasResolution;

            combinedMeshList     = new List <SkinnedMeshCombiner.CombineInstance>(umaData.umaRecipe.slotDataList.Length);
            combinedMaterialList = new List <Material>();

            EnsureUMADataSetup(umaData);
            umaData.skeleton.BeginSkeletonUpdate();

            for (currentRendererIndex = 0; currentRendererIndex < umaData.generatedMaterials.rendererCount; currentRendererIndex++)
            {
                //Move umaMesh creation to with in the renderer loops
                //May want to make sure to set all it's buffers to null instead of creating a new UMAMeshData
                UMAMeshData umaMesh = new UMAMeshData();
                umaMesh.ClaimSharedBuffers();

                umaMesh.subMeshCount = 0;
                umaMesh.vertexCount  = 0;

                combinedMeshList.Clear();
                combinedMaterialList.Clear();
                clothProperties = null;

                BuildCombineInstances();

                if (combinedMeshList.Count == 1)
                {
                    // fast track
                    var tempMesh = SkinnedMeshCombiner.ShallowInstanceMesh(combinedMeshList[0].meshData);
                    tempMesh.ApplyDataToUnityMesh(renderers[currentRendererIndex], umaData.skeleton);
                }
                else
                {
                    SkinnedMeshCombiner.CombineMeshes(umaMesh, combinedMeshList.ToArray(), umaData.blendShapeSettings);

                    if (updatedAtlas)
                    {
                        RecalculateUV(umaMesh);
                    }

                    umaMesh.ApplyDataToUnityMesh(renderers[currentRendererIndex], umaData.skeleton);
                }
                var cloth = renderers[currentRendererIndex].GetComponent <Cloth>();
                if (clothProperties != null)
                {
                    if (cloth != null)
                    {
                        clothProperties.ApplyValues(cloth);
                    }
                }
                else
                {
                    Destroy(cloth);
                }

                var materials = combinedMaterialList.ToArray();
                renderers[currentRendererIndex].sharedMaterials = materials;
                umaMesh.ReleaseSharedBuffers();
            }

            umaData.umaRecipe.ClearDNAConverters();
            for (int i = 0; i < umaData.umaRecipe.slotDataList.Length; i++)
            {
                SlotData slotData = umaData.umaRecipe.slotDataList[i];
                if (slotData != null)
                {
                    umaData.umaRecipe.AddDNAUpdater(slotData.asset.slotDNA);
                }
            }

            umaData.firstBake = false;
        }