CustomizerPutSliceIndexInUV0_z.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DigitalOpus.MB.Core;
  5. namespace DigitalOpus.MB.Core
  6. {
  7. /// <summary>
  8. /// This MeshAssignCustomizer alters the UV data as it is being assigned to the mesh.
  9. /// It appends the Texture Array slice index in the UV.z channel.
  10. ///
  11. /// Shaders must be modified to read the slice index from the UV.z channel to use this.
  12. /// </summary>
  13. [CreateAssetMenu(fileName = "MeshAssignCustomizerPutSliceIdxInUV0_z", menuName = "Mesh Baker/Assign To Mesh Customizer/Put Slice Index In UV0.z", order = 1)]
  14. public class CustomizerPutSliceIndexInUV0_z : MB_DefaultMeshAssignCustomizer
  15. {
  16. public override void meshAssign_UV0(int channel, MB_IMeshBakerSettings settings, MB2_TextureBakeResults textureBakeResults, Mesh mesh, Vector2[] uvs, float[] sliceIndexes)
  17. {
  18. if (textureBakeResults.resultType == MB2_TextureBakeResults.ResultType.atlas)
  19. {
  20. mesh.uv = uvs;
  21. }
  22. else
  23. {
  24. {
  25. if (uvs.Length == sliceIndexes.Length)
  26. {
  27. List<Vector3> nuvs = new List<Vector3>();
  28. for (int i = 0; i < uvs.Length; i++)
  29. {
  30. nuvs.Add(new Vector3(uvs[i].x, uvs[i].y, sliceIndexes[i]));
  31. }
  32. mesh.SetUVs(0, nuvs);
  33. }
  34. else
  35. {
  36. Debug.LogError("UV slice buffer was not the same size as the uv buffer");
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }