Example #1
0
        private SkinnedMeshRenderer MakeRenderer(int i, Transform rootBone, UMARendererAsset rendererAsset = null)
        {
            GameObject newSMRGO = new GameObject(i == 0 ? "UMARenderer" : ("UMARenderer " + i));

            newSMRGO.transform.parent        = umaData.transform;
            newSMRGO.transform.localPosition = Vector3.zero;
            newSMRGO.transform.localRotation = Quaternion.Euler(0, 0, 0f);
            newSMRGO.transform.localScale    = Vector3.one;
            newSMRGO.gameObject.layer        = umaData.gameObject.layer;

            var newRenderer = newSMRGO.AddComponent <SkinnedMeshRenderer>();

            newRenderer.enabled    = false;
            newRenderer.sharedMesh = new Mesh();
#if UMA_32BITBUFFERS
            newRenderer.sharedMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
#endif
            newRenderer.rootBone        = rootBone;
            newRenderer.quality         = SkinQuality.Bone4;
            newRenderer.sharedMesh.name = i == 0 ? "UMAMesh" : ("UMAMesh " + i);

            if (rendererAsset != null)
            {
                rendererAsset.ApplySettingsToRenderer(newRenderer);
            }

            return(newRenderer);
        }
Example #2
0
 /// <summary>
 /// Constructor for slot using the given asset.
 /// </summary>
 /// <param name="asset">Asset.</param>
 public SlotData(SlotDataAsset asset)
 {
     this.asset = asset;
     if (asset)
     {
         overlayScale  = asset.overlayScale;
         rendererAsset = asset.RendererAsset;
     }
     else
     {
         overlayScale = 1.0f;
     }
 }
        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++;
            }
        }
Example #4
0
 /// <summary>
 /// Constructor for slot using the given asset.
 /// </summary>
 /// <param name="asset">Asset.</param>
 public SlotData(SlotDataAsset asset)
 {
     this.asset = asset;
     if (asset)
     {
         tags          = asset.tags;
         Races         = asset.Races;
         overlayScale  = asset.overlayScale;
         rendererAsset = asset.RendererAsset;
     }
     else
     {
         tags         = new string[0];
         overlayScale = 1.0f;
     }
     if (Races == null)
     {
         Races = new string[0];
     }
 }
Example #5
0
 /// <summary>
 /// Constructor for slot using the given asset.
 /// </summary>
 /// <param name="asset">Asset.</param>
 public SlotData(SlotDataAsset asset)
 {
     this.asset    = asset;
     overlayScale  = asset.overlayScale;
     rendererAsset = asset.RendererAsset;
 }
        private UMAData.GeneratedMaterial FindOrCreateGeneratedMaterial(UMAMaterial umaMaterial, UMARendererAsset renderer = null)
        {
            if (umaMaterial.materialType == UMAMaterial.MaterialType.Atlas)
            {
                foreach (var atlassedMaterial in atlassedMaterials)
                {
                    if (atlassedMaterial.umaMaterial == umaMaterial && atlassedMaterial.rendererAsset == renderer)
                    {
                        return(atlassedMaterial);
                    }
                    else
                    {
                        if (atlassedMaterial.umaMaterial.Equals(umaMaterial) && atlassedMaterial.rendererAsset == renderer)
                        {
                            return(atlassedMaterial);
                        }
                    }
                }
            }

            var res = new UMAData.GeneratedMaterial();

            res.rendererAsset = renderer;
            res.umaMaterial   = umaMaterial;
            res.material      = UnityEngine.Object.Instantiate(umaMaterial.material) as Material;
            res.material.name = umaMaterial.material.name;
#if UNITY_WEBGL
            res.material.shader = Shader.Find(res.material.shader.name);
#endif
            res.material.CopyPropertiesFromMaterial(umaMaterial.material);
            atlassedMaterials.Add(res);
            generatedMaterials.Add(res);

            return(res);
        }
Example #7
0
        protected void EnsureUMADataSetup(UMAData umaData)
        {
            if (umaData.umaRecipe != null)
            {
                umaData.umaRecipe.UpdateMeshHideMasks();
            }

            if (umaData.umaRoot != null)
            {
                umaData.CleanMesh(false);
                if (umaData.rendererCount == umaData.generatedMaterials.rendererAssets.Count && umaData.AreRenderersEqual(umaData.generatedMaterials.rendererAssets))
                {
                    renderers = umaData.GetRenderers();
                }
                else
                {
                    var oldRenderers    = umaData.GetRenderers();
                    var globalTransform = umaData.GetGlobalTransform();

                    renderers = new SkinnedMeshRenderer[umaData.generatedMaterials.rendererAssets.Count];

                    for (int i = 0; i < umaData.generatedMaterials.rendererAssets.Count; i++)
                    {
                        if (oldRenderers != null && oldRenderers.Length > i)
                        {
                            renderers[i] = oldRenderers[i];
                            if (umaData.generatedMaterials.rendererAssets[i] != null)
                            {
                                umaData.generatedMaterials.rendererAssets[i].ApplySettingsToRenderer(renderers[i]);
                            }
                            else
                            {
                                umaData.ResetRendererSettings(i);
                            }

                            continue;
                        }
                        UMARendererAsset rendererAsset = umaData.generatedMaterials.rendererAssets[i];
                        if (rendererAsset == null)
                        {
                            rendererAsset = umaData.defaultRendererAsset;
                        }

                        renderers[i] = MakeRenderer(i, globalTransform, rendererAsset);
                    }

                    if (oldRenderers != null)
                    {
                        for (int i = umaData.generatedMaterials.rendererAssets.Count; i < oldRenderers.Length; i++)
                        {
                            DestroyImmediate(oldRenderers[i].gameObject);
                            //For cloth, be aware of issue: 845868
                            //https://issuetracker.unity3d.com/issues/cloth-repeatedly-destroying-objects-with-cloth-components-causes-a-crash-in-unity-cloth-updatenormals
                        }
                    }
                    umaData.SetRenderers(renderers);
                    umaData.SetRendererAssets(umaData.generatedMaterials.rendererAssets.ToArray());
                }
                return;
            }

            if (umaData.umaRoot == null)
            {
                Transform rootTransform = umaData.gameObject.transform.Find("Root");
                if (rootTransform)
                {
                    umaData.umaRoot = rootTransform.gameObject;
                }
                else
                {
                    GameObject newRoot = new GameObject("Root");
                    //make root of the UMAAvatar respect the layer setting of the UMAAvatar so cameras can just target this layer
                    newRoot.layer                   = umaData.gameObject.layer;
                    newRoot.transform.parent        = umaData.transform;
                    newRoot.transform.localPosition = Vector3.zero;
                    newRoot.transform.localRotation = Quaternion.Euler(270f, 0, 0f);
                    newRoot.transform.localScale    = Vector3.one;
                    umaData.umaRoot                 = newRoot;
                }

                Transform globalTransform = umaData.umaRoot.transform.Find("Global");
                if (!globalTransform)
                {
                    GameObject newGlobal = new GameObject("Global");
                    newGlobal.transform.parent        = umaData.umaRoot.transform;
                    newGlobal.transform.localPosition = Vector3.zero;
                    newGlobal.transform.localRotation = Quaternion.Euler(90f, 90f, 0f);

                    globalTransform = newGlobal.transform;
                }

                umaData.skeleton = new UMASkeleton(globalTransform);

                renderers = new SkinnedMeshRenderer[umaData.generatedMaterials.rendererAssets.Count];

                for (int i = 0; i < umaData.generatedMaterials.rendererAssets.Count; i++)
                {
                    UMARendererAsset rendererAsset = umaData.generatedMaterials.rendererAssets[i];
                    if (rendererAsset == null)
                    {
                        rendererAsset = umaData.defaultRendererAsset;
                    }

                    renderers[i] = MakeRenderer(i, globalTransform, rendererAsset);
                }
                umaData.SetRenderers(renderers);
                umaData.SetRendererAssets(umaData.generatedMaterials.rendererAssets.ToArray());
            }

            //Clear out old cloth components
            for (int i = 0; i < umaData.rendererCount; i++)
            {
                Cloth cloth = renderers[i].GetComponent <Cloth>();
                if (cloth != null)
                {
                    DestroyImmediate(cloth, false);                    //Crashes if trying to use Destroy()
                }
            }
        }
Example #8
0
 public SlotData()
 {
     overlayScale  = 1.0f;
     rendererAsset = null;
 }
        protected void EnsureUMADataSetup(UMAData umaData)
        {
            if (umaData.umaRecipe != null)
            {
                umaData.umaRecipe.UpdateMeshHideMasks();
            }

            #region SetupSkeleton
            // First, ensure that the skeleton is setup, and if not,
            // then generate the root, global and set it up.
            if (umaData.umaRoot == null)
            {
                umaData.SetupSkeleton();
            }
            #endregion
            if (umaData.umaRoot != null)
            {
                umaData.CleanMesh(false);
                if (umaData.rendererCount == umaData.generatedMaterials.rendererAssets.Count && umaData.AreRenderersEqual(umaData.generatedMaterials.rendererAssets))
                {
                    renderers = umaData.GetRenderers();
                }
                else
                {
                    var oldRenderers    = umaData.GetRenderers();
                    var globalTransform = umaData.GetGlobalTransform();

                    renderers = new SkinnedMeshRenderer[umaData.generatedMaterials.rendererAssets.Count];

                    for (int i = 0; i < umaData.generatedMaterials.rendererAssets.Count; i++)
                    {
                        if (oldRenderers != null && oldRenderers.Length > i)
                        {
                            renderers[i] = oldRenderers[i];
                            if (umaData.generatedMaterials.rendererAssets[i] != null)
                            {
                                umaData.generatedMaterials.rendererAssets[i].ApplySettingsToRenderer(renderers[i]);
                            }
                            else
                            {
                                umaData.ResetRendererSettings(i);
                            }

                            continue;
                        }
                        UMARendererAsset rendererAsset = umaData.generatedMaterials.rendererAssets[i];
                        if (rendererAsset == null)
                        {
                            rendererAsset = umaData.defaultRendererAsset;
                        }

                        renderers[i] = MakeRenderer(i, globalTransform, rendererAsset);
                    }

                    if (oldRenderers != null)
                    {
                        for (int i = umaData.generatedMaterials.rendererAssets.Count; i < oldRenderers.Length; i++)
                        {
                            DestroyImmediate(oldRenderers[i].gameObject);
                            //For cloth, be aware of issue: 845868
                            //https://issuetracker.unity3d.com/issues/cloth-repeatedly-destroying-objects-with-cloth-components-causes-a-crash-in-unity-cloth-updatenormals
                        }
                    }
                    umaData.SetRenderers(renderers);
                    umaData.SetRendererAssets(umaData.generatedMaterials.rendererAssets.ToArray());
                }
                return;
            }

            //Clear out old cloth components
            for (int i = 0; i < umaData.rendererCount; i++)
            {
                Cloth cloth = renderers[i].GetComponent <Cloth>();
                if (cloth != null)
                {
                    DestroyImmediate(cloth, false);                    //Crashes if trying to use Destroy()
                }
            }
        }