| | 1 | | using System; |
| | 2 | | using System.Buffers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Pool; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public struct BakedCombineInstances : IDisposable |
| | 11 | | { |
| | 12 | | private Mesh[] bakedMeshes; |
| | 13 | |
|
| | 14 | | public void Dispose() |
| | 15 | | { |
| 442 | 16 | | foreach ( var mesh in bakedMeshes ) |
| 208 | 17 | | Object.Destroy(mesh); |
| | 18 | |
|
| 13 | 19 | | ArrayPool<Mesh>.Shared.Return(bakedMeshes); |
| 13 | 20 | | bakedMeshes = null; |
| 13 | 21 | | } |
| | 22 | |
|
| | 23 | | public static BakedCombineInstances Bake(IList<CombineInstance> combineInstances, IReadOnlyList<SkinnedMeshRende |
| | 24 | | { |
| 13 | 25 | | int combineInstancesCount = combineInstances.Count; |
| 13 | 26 | | var bakedCombinedInstances = ArrayPool<Mesh>.Shared.Rent(combineInstancesCount); |
| | 27 | |
|
| 182 | 28 | | for ( int i = 0; i < combineInstancesCount; i++) |
| | 29 | | { |
| 78 | 30 | | Mesh mesh = new Mesh(); |
| | 31 | |
|
| | 32 | | // Important note: It seems that mesh normals are scaled by the matrix when using BakeMesh. |
| | 33 | | // This is wrong and and shouldn't happen, so we have to arrange them manually. |
| | 34 | | // |
| | 35 | | // We DON'T do this yet because the meshes can be read-only, so the original |
| | 36 | | // normals can't be extracted. For normals, visual artifacts are minor because |
| | 37 | | // toon shader doesn't use any kind of normal mapping. |
| 78 | 38 | | renderers[i].BakeMesh(mesh, true); |
| | 39 | |
|
| 78 | 40 | | var combinedInstance = combineInstances[i]; |
| 78 | 41 | | combinedInstance.mesh = mesh; |
| 78 | 42 | | bakedCombinedInstances[i] = mesh; |
| | 43 | |
|
| 78 | 44 | | combineInstances[i] = combinedInstance; |
| | 45 | | } |
| | 46 | |
|
| 13 | 47 | | return new BakedCombineInstances { bakedMeshes = bakedCombinedInstances }; |
| | 48 | | } |
| | 49 | | } |
| | 50 | | } |