| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using GPUSkinning; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Assertions; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public interface IAvatarMeshCombinerHelper : IDisposable |
| | 11 | | { |
| | 12 | | public bool useCullOpaqueHeuristic { get; set; } |
| | 13 | | public bool prepareMeshForGpuSkinning { get; set; } |
| | 14 | | public bool uploadMeshToGpu { get; set; } |
| | 15 | | public bool enableCombinedMesh { get; set; } |
| | 16 | |
|
| | 17 | | public GameObject container { get; } |
| | 18 | | public SkinnedMeshRenderer renderer { get; } |
| | 19 | |
|
| | 20 | | public bool Combine(SkinnedMeshRenderer bonesContainer, SkinnedMeshRenderer[] renderersToCombine); |
| | 21 | | public bool Combine(SkinnedMeshRenderer bonesContainer, SkinnedMeshRenderer[] renderersToCombine, Material mater |
| | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// AvatarMeshCombinerHelper uses the AvatarMeshCombiner utility class to combine many skinned renderers |
| | 26 | | /// into a single one. |
| | 27 | | /// |
| | 28 | | /// This class will recycle the same gameObject and renderer each time it is called, |
| | 29 | | /// and binds the AvatarMeshCombiner output to a proper well configured SkinnedMeshRenderer. |
| | 30 | | /// </summary> |
| | 31 | | public class AvatarMeshCombinerHelper : IAvatarMeshCombinerHelper |
| | 32 | | { |
| 1 | 33 | | private static bool VERBOSE = false; |
| 1 | 34 | | private static ILogger logger = new Logger(Debug.unityLogger.logHandler) { filterLogType = VERBOSE ? LogType.Log |
| | 35 | |
|
| 1319 | 36 | | public GameObject container { get; private set; } |
| 0 | 37 | | public SkinnedMeshRenderer renderer { get; private set; } |
| | 38 | |
|
| 0 | 39 | | public bool useCullOpaqueHeuristic { get; set; } = false; |
| 1304 | 40 | | public bool prepareMeshForGpuSkinning { get; set; } = false; |
| 2623 | 41 | | public bool uploadMeshToGpu { get; set; } = true; |
| 1319 | 42 | | public bool enableCombinedMesh { get; set; } = true; |
| | 43 | |
|
| | 44 | | private AvatarMeshCombiner.Output? lastOutput; |
| | 45 | |
|
| 3957 | 46 | | public AvatarMeshCombinerHelper (GameObject container = null) { this.container = container; } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Combine will use AvatarMeshCombiner to generate a combined avatar mesh. |
| | 50 | | /// After the avatar is combined, it will generate a new GameObject with a SkinnedMeshRenderer that contains |
| | 51 | | /// the generated mesh. This GameObject and Renderer will be preserved over any number of Combine() calls. |
| | 52 | | /// |
| | 53 | | /// Uses a predefined Material ready for the Avatar |
| | 54 | | /// |
| | 55 | | /// For more details on the combining check out AvatarMeshCombiner.CombineSkinnedMeshes. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="bonesContainer">A SkinnedMeshRenderer that must contain the bones and bindposes that will be us |
| | 58 | | /// <param name="renderersToCombine">A list of avatar parts to be combined</param> |
| 0 | 59 | | public bool Combine(SkinnedMeshRenderer bonesContainer, SkinnedMeshRenderer[] renderersToCombine) { return Combi |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Combine will use AvatarMeshCombiner to generate a combined avatar mesh. |
| | 63 | | /// After the avatar is combined, it will generate a new GameObject with a SkinnedMeshRenderer that contains |
| | 64 | | /// the generated mesh. This GameObject and Renderer will be preserved over any number of Combine() calls. |
| | 65 | | /// |
| | 66 | | /// For more details on the combining check out AvatarMeshCombiner.CombineSkinnedMeshes. |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="bonesContainer">A SkinnedMeshRenderer that must contain the bones and bindposes that will be us |
| | 69 | | /// <param name="renderersToCombine">A list of avatar parts to be combined</param> |
| | 70 | | /// <param name="materialAsset">A material asset that will serve as the base of the combine result. A new materi |
| | 71 | | /// <returns>true if succeeded, false if not</returns> |
| | 72 | | public bool Combine(SkinnedMeshRenderer bonesContainer, SkinnedMeshRenderer[] renderersToCombine, Material mater |
| | 73 | | { |
| 17 | 74 | | Assert.IsTrue(bonesContainer != null, "bonesContainer should never be null!"); |
| 17 | 75 | | Assert.IsTrue(renderersToCombine != null, "renderersToCombine should never be null!"); |
| 17 | 76 | | Assert.IsTrue(materialAsset != null, "materialAsset should never be null!"); |
| | 77 | |
|
| 17 | 78 | | SkinnedMeshRenderer[] renderers = renderersToCombine; |
| | 79 | |
|
| | 80 | | // Sanitize renderers list |
| 96 | 81 | | renderers = renderers.Where( (x) => x != null && x.sharedMesh != null ).ToArray(); |
| | 82 | |
|
| 17 | 83 | | if ( renderers.Length == 0 ) |
| 1 | 84 | | return false; |
| | 85 | |
|
| 16 | 86 | | bool success = CombineInternal( |
| | 87 | | bonesContainer, |
| | 88 | | renderers, |
| | 89 | | materialAsset); |
| | 90 | |
|
| | 91 | | // Disable original renderers |
| 170 | 92 | | for ( int i = 0; i < renderers.Length; i++ ) |
| | 93 | | { |
| 69 | 94 | | renderers[i].enabled = false; |
| | 95 | | } |
| | 96 | |
|
| 16 | 97 | | return success; |
| | 98 | | } |
| | 99 | |
|
| | 100 | | private bool CombineInternal(SkinnedMeshRenderer bonesContainer, SkinnedMeshRenderer[] renderers, Material mater |
| | 101 | | { |
| 16 | 102 | | Assert.IsTrue(bonesContainer != null, "bonesContainer should never be null!"); |
| 16 | 103 | | Assert.IsTrue(bonesContainer.sharedMesh != null, "bonesContainer should never be null!"); |
| 16 | 104 | | Assert.IsTrue(bonesContainer.sharedMesh.bindposes != null, "bonesContainer bindPoses should never be null!") |
| 16 | 105 | | Assert.IsTrue(bonesContainer.bones != null, "bonesContainer bones should never be null!"); |
| 16 | 106 | | Assert.IsTrue(renderers != null, "renderers should never be null!"); |
| 16 | 107 | | Assert.IsTrue(materialAsset != null, "materialAsset should never be null!"); |
| | 108 | |
|
| 16 | 109 | | CombineLayerUtils.ENABLE_CULL_OPAQUE_HEURISTIC = useCullOpaqueHeuristic; |
| | 110 | |
|
| 16 | 111 | | AvatarMeshCombiner.Output output = AvatarMeshCombiner.CombineSkinnedMeshes( |
| | 112 | | bonesContainer.sharedMesh.bindposes, |
| | 113 | | bonesContainer.bones, |
| | 114 | | renderers, |
| | 115 | | materialAsset); |
| | 116 | |
|
| 16 | 117 | | if ( !output.isValid ) |
| | 118 | | { |
| 0 | 119 | | logger.LogError("AvatarMeshCombiner", "Combine failed!"); |
| 0 | 120 | | return false; |
| | 121 | | } |
| 16 | 122 | | Transform rootBone = bonesContainer.rootBone; |
| | 123 | |
|
| 16 | 124 | | if ( container == null ) |
| 14 | 125 | | this.container = new GameObject("CombinedAvatar"); |
| | 126 | |
|
| 16 | 127 | | if ( renderer == null ) |
| 14 | 128 | | renderer = container.AddComponent<SkinnedMeshRenderer>(); |
| | 129 | |
|
| 16 | 130 | | UnloadAssets(); |
| 16 | 131 | | lastOutput = output; |
| | 132 | |
|
| 16 | 133 | | container.layer = bonesContainer.gameObject.layer; |
| 16 | 134 | | renderer.sharedMesh = output.mesh; |
| 16 | 135 | | renderer.bones = bonesContainer.bones; |
| 16 | 136 | | renderer.rootBone = rootBone; |
| 16 | 137 | | renderer.sharedMaterials = output.materials; |
| 16 | 138 | | renderer.quality = SkinQuality.Bone4; |
| 16 | 139 | | renderer.updateWhenOffscreen = false; |
| 16 | 140 | | renderer.skinnedMotionVectors = false; |
| 16 | 141 | | renderer.enabled = enableCombinedMesh; |
| | 142 | |
|
| 16 | 143 | | if (prepareMeshForGpuSkinning) |
| 1 | 144 | | GPUSkinningUtils.EncodeBindPosesIntoMesh(renderer); |
| | 145 | |
|
| 16 | 146 | | if (uploadMeshToGpu) |
| 8 | 147 | | output.mesh.UploadMeshData(true); |
| | 148 | |
|
| 16 | 149 | | logger.Log("AvatarMeshCombiner", "Finish combining avatar. Click here to focus on GameObject.", container); |
| 16 | 150 | | return true; |
| | 151 | | } |
| | 152 | |
|
| | 153 | | private void UnloadAssets() |
| | 154 | | { |
| 1510 | 155 | | if (!lastOutput.HasValue) |
| 1496 | 156 | | return; |
| | 157 | |
|
| 14 | 158 | | if (lastOutput.Value.mesh != null) |
| 14 | 159 | | Object.Destroy(lastOutput.Value.mesh); |
| | 160 | |
|
| 14 | 161 | | if (lastOutput.Value.materials != null) |
| | 162 | | { |
| 74 | 163 | | foreach ( var material in lastOutput.Value.materials ) |
| | 164 | | { |
| 23 | 165 | | Object.Destroy(material); |
| | 166 | | } |
| | 167 | | } |
| 14 | 168 | | } |
| | 169 | |
|
| | 170 | | /// <summary> |
| | 171 | | /// Disposes the created mesh, materials, GameObject and Renderer. |
| | 172 | | /// </summary> |
| | 173 | | public void Dispose() |
| | 174 | | { |
| 1494 | 175 | | UnloadAssets(); |
| 1494 | 176 | | Object.Destroy(container); |
| 1494 | 177 | | } |
| | 178 | | } |
| | 179 | | } |