Basis25D.gd 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Currently broken unless Godot makes this kind of thing possible:
  2. # https://github.com/godotengine/godot/issues/21461
  3. # https://github.com/godotengine/godot-proposals/issues/279
  4. # Basis25D structure for performing 2.5D transform math.
  5. # Note: All code assumes that Y is UP in 3D, and DOWN in 2D.
  6. # Meaning, a top-down view has a Y axis component of (0, 0), with a Z axis component of (0, 1).
  7. # For a front side view, Y is (0, -1) and Z is (0, 0).
  8. # Remember that Godot's 2D mode has the Y axis pointing DOWN on the screen.
  9. class_name Basis25D
  10. var x: Vector2 = Vector2()
  11. var y: Vector2 = Vector2()
  12. var z: Vector2 = Vector2()
  13. static func top_down():
  14. return init(1, 0, 0, 0, 0, 1)
  15. static func front_side():
  16. return init(1, 0, 0, -1, 0, 0)
  17. static func forty_five():
  18. return init(1, 0, 0, -0.70710678118, 0, 0.70710678118)
  19. static func isometric():
  20. return init(0.86602540378, 0.5, 0, -1, -0.86602540378, 0.5)
  21. static func oblique_y():
  22. return init(1, 0, -1, -1, 0, 1)
  23. static func oblique_z():
  24. return init(1, 0, 0, -1, -1, 1)
  25. # Creates a Dimetric Basis25D from the angle between the Y axis and the others.
  26. # Dimetric(2.09439510239) is the same as Isometric.
  27. # Try to keep this number away from a multiple of Tau/4 (or Pi/2) radians.
  28. static func dimetric(angle):
  29. var sine = sin(angle)
  30. var cosine = cos(angle)
  31. return init(sine, -cosine, 0, -1, -sine, -cosine)
  32. static func init(xx, xy, yx, yy, zx, zy):
  33. var xv = Vector2(xx, xy)
  34. var yv = Vector2(yx, yy)
  35. var zv = Vector2(zx, zy)
  36. return Basis25D.new(xv, yv, zv)
  37. func _init(xAxis: Vector2, yAxis: Vector2, zAxis: Vector2):
  38. x = xAxis
  39. y = yAxis
  40. z = zAxis