Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

132 lignes
3.2KB

  1. #[derive(Clone)]
  2. pub(crate) struct Instruction {
  3. pub instr: InstructionType,
  4. pub start: f64,
  5. pub progress: f64,
  6. pub base: CameraData,
  7. // impl in instruction_parser.rs
  8. }
  9. #[derive(Clone)]
  10. pub(crate) enum InstructionType {
  11. Track {
  12. vector: Vector3,
  13. time: f64,
  14. },
  15. Move {
  16. vector: Vector3,
  17. },
  18. Rotate {
  19. vector: Vector3,
  20. time: f64,
  21. },
  22. Look {
  23. vector: Vector3,
  24. },
  25. Multi {
  26. instructions: Vec<InstructionType>,
  27. },
  28. }
  29. impl InstructionType {
  30. pub fn time(&self) -> f64 {
  31. match self {
  32. Self::Track {time, ..} => *time,
  33. Self::Move {..} => 0.0,
  34. Self::Rotate {time, ..} => *time,
  35. Self::Look {..} => 0.0,
  36. Self::Multi {instructions, ..} => {
  37. let mut max: f64 = 0.0;
  38. for i in instructions {
  39. if i.time() > max {
  40. max = i.time();
  41. }
  42. }
  43. max
  44. },
  45. }
  46. }
  47. pub fn lerp(&self, start: f64, now: f64) -> CameraData {
  48. let delta = now - start;
  49. let mut portion = 1.0;
  50. let time = self.time();
  51. if time > 0.0 && time < delta {
  52. portion = delta/self.time();
  53. }
  54. match self {
  55. Self::Track {vector, ..} => CameraData {rotation: Vector3::default(), position: *vector * portion},
  56. Self::Move {vector, ..} => CameraData {rotation: Vector3::default(), position: *vector},
  57. Self::Rotate {vector, ..} => CameraData {rotation: *vector * portion, position: Vector3::default()},
  58. Self::Look {vector, ..} => CameraData {rotation: *vector, position: Vector3::default()},
  59. Self::Multi {instructions, ..} => {
  60. let mut data = CameraData::default();
  61. for i in instructions {
  62. data += i.lerp(start, now);
  63. }
  64. data
  65. },
  66. }
  67. }
  68. }
  69. #[derive(Clone, Copy)]
  70. pub(crate) struct CameraData {
  71. pub rotation: Vector3,
  72. pub position: Vector3,
  73. }
  74. impl std::default::Default for CameraData {
  75. fn default() -> Self {
  76. Self {rotation: Vector3::default(), position: Vector3::default()}
  77. }
  78. }
  79. impl std::ops::Add for CameraData {
  80. type Output = Self;
  81. fn add(self, rhs: Self) -> Self {
  82. Self {rotation: self.rotation + rhs.rotation, position: self.position + rhs.position}
  83. }
  84. }
  85. impl std::ops::AddAssign for CameraData {
  86. fn add_assign(&mut self, rhs: Self) {
  87. *self = *self + rhs;
  88. }
  89. }
  90. #[derive(Copy, Clone)]
  91. pub(crate) struct Vector3 {
  92. pub x: f64,
  93. pub y: f64,
  94. pub z: f64,
  95. }
  96. impl std::default::Default for Vector3 {
  97. fn default() -> Self {
  98. Self {x:0.0, y:0.0, z:0.0}
  99. }
  100. }
  101. impl std::ops::Mul<f64> for Vector3 {
  102. type Output = Self;
  103. fn mul(self, rhs: f64) -> Self {
  104. Self {
  105. x: self.x * rhs,
  106. y: self.y * rhs,
  107. z: self.z * rhs,
  108. }
  109. }
  110. }
  111. impl std::ops::Add for Vector3 {
  112. type Output = Self;
  113. fn add(self, rhs: Self) -> Self {
  114. Self {x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z}
  115. }
  116. }