VNormal.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using Autodesk.Max;
  2. using SharpDX;
  3. namespace Max2Babylon
  4. {
  5. public class VNormal
  6. {
  7. Vector3 norm;
  8. uint smooth;
  9. VNormal next;
  10. bool init;
  11. public VNormal()
  12. {
  13. smooth = 0;
  14. next = null;
  15. init = false;
  16. norm = new Vector3(0, 0, 0);
  17. }
  18. public VNormal(Vector3 n, uint s)
  19. {
  20. next = null;
  21. init = true;
  22. norm = n;
  23. smooth = s;
  24. }
  25. public void AddNormal(Vector3 n, uint s)
  26. {
  27. if (((s & smooth) == 0) && init)
  28. {
  29. if (next != null)
  30. next.AddNormal(n, s);
  31. else
  32. {
  33. next = new VNormal(n, s);
  34. }
  35. }
  36. else
  37. {
  38. norm += n;
  39. smooth |= s;
  40. init = true;
  41. }
  42. }
  43. public IPoint3 GetNormal(uint s)
  44. {
  45. if (((smooth & s) != 0) || next == null)
  46. return norm.ToPoint3();
  47. return next.GetNormal(s);
  48. }
  49. // Normalize each normal in the list
  50. public void Normalize()
  51. {
  52. VNormal ptr = next;
  53. VNormal prev = this;
  54. while (ptr != null)
  55. {
  56. if ((ptr.smooth & smooth) != 0)
  57. {
  58. norm += ptr.norm;
  59. prev.next = ptr.next;
  60. ptr = prev.next;
  61. }
  62. else
  63. {
  64. prev = ptr;
  65. ptr = ptr.next;
  66. }
  67. }
  68. norm.Normalize();
  69. if (next != null)
  70. {
  71. next.Normalize();
  72. }
  73. }
  74. }
  75. }