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