DirectGrid.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. from panda3d.core import *
  2. from direct.showbase.DirectObject import DirectObject
  3. from .DirectUtil import *
  4. from .DirectGeometry import *
  5. class DirectGrid(NodePath, DirectObject):
  6. def __init__(self,gridSize=100.0,gridSpacing=5.0,planeColor=(0.5,0.5,0.5,0.5),parent = None):
  7. # Initialize superclass
  8. NodePath.__init__(self, 'DirectGrid')
  9. # Don't wireframe or light
  10. useDirectRenderStyle(self)
  11. # Load up grid parts to initialize grid object
  12. # Polygon used to mark grid plane
  13. self.gridBack = loader.loadModel('models/misc/gridBack')
  14. self.gridBack.reparentTo(self)
  15. self.gridBack.setColor(*planeColor)
  16. # Grid Lines
  17. self.lines = self.attachNewNode('gridLines')
  18. self.minorLines = LineNodePath(self.lines)
  19. self.minorLines.lineNode.setName('minorLines')
  20. self.minorLines.setColor(VBase4(0.3, 0.55, 1, 1))
  21. self.minorLines.setThickness(1)
  22. self.majorLines = LineNodePath(self.lines)
  23. self.majorLines.lineNode.setName('majorLines')
  24. self.majorLines.setColor(VBase4(0.3, 0.55, 1, 1))
  25. self.majorLines.setThickness(5)
  26. self.centerLines = LineNodePath(self.lines)
  27. self.centerLines.lineNode.setName('centerLines')
  28. self.centerLines.setColor(VBase4(1, 0, 0, 0))
  29. self.centerLines.setThickness(3)
  30. # Small marker to hilight snap-to-grid point
  31. self.snapMarker = loader.loadModel('models/misc/sphere')
  32. self.snapMarker.node().setName('gridSnapMarker')
  33. self.snapMarker.reparentTo(self)
  34. self.snapMarker.setColor(1, 0, 0, 1)
  35. self.snapMarker.setScale(0.3)
  36. self.snapPos = Point3(0)
  37. # Initialize Grid characteristics
  38. self.fXyzSnap = 1
  39. self.fHprSnap = 1
  40. self.gridSize = gridSize
  41. self.gridSpacing = gridSpacing
  42. self.snapAngle = 15.0
  43. self.enable(parent = parent)
  44. def enable(self, parent = None):
  45. if parent:
  46. self.reparentTo(parent)
  47. else:
  48. self.reparentTo(base.direct.group)
  49. self.updateGrid()
  50. self.fEnabled = 1
  51. def disable(self):
  52. self.detachNode()
  53. self.fEnabled = 0
  54. def toggleGrid(self, parent = None):
  55. if self.fEnabled:
  56. self.disable()
  57. else:
  58. self.enable(parent = parent)
  59. def isEnabled(self):
  60. return self.fEnabled
  61. def updateGrid(self):
  62. # Update grid lines based upon current grid spacing and grid size
  63. # First reset existing grid lines
  64. self.minorLines.reset()
  65. self.majorLines.reset()
  66. self.centerLines.reset()
  67. # Now redraw lines
  68. numLines = int(math.ceil(self.gridSize/self.gridSpacing))
  69. scaledSize = numLines * self.gridSpacing
  70. center = self.centerLines
  71. minor = self.minorLines
  72. major = self.majorLines
  73. for i in range(-numLines, numLines + 1):
  74. if i == 0:
  75. center.moveTo(i * self.gridSpacing, -scaledSize, 0)
  76. center.drawTo(i * self.gridSpacing, scaledSize, 0)
  77. center.moveTo(-scaledSize, i * self.gridSpacing, 0)
  78. center.drawTo(scaledSize, i * self.gridSpacing, 0)
  79. else:
  80. if (i % 5) == 0:
  81. major.moveTo(i * self.gridSpacing, -scaledSize, 0)
  82. major.drawTo(i * self.gridSpacing, scaledSize, 0)
  83. major.moveTo(-scaledSize, i * self.gridSpacing, 0)
  84. major.drawTo(scaledSize, i * self.gridSpacing, 0)
  85. else:
  86. minor.moveTo(i * self.gridSpacing, -scaledSize, 0)
  87. minor.drawTo(i * self.gridSpacing, scaledSize, 0)
  88. minor.moveTo(-scaledSize, i * self.gridSpacing, 0)
  89. minor.drawTo(scaledSize, i * self.gridSpacing, 0)
  90. center.create()
  91. minor.create()
  92. major.create()
  93. if (self.gridBack):
  94. self.gridBack.setScale(scaledSize)
  95. def setXyzSnap(self, fSnap):
  96. self.fXyzSnap = fSnap
  97. def getXyzSnap(self):
  98. return self.fXyzSnap
  99. def setHprSnap(self, fSnap):
  100. self.fHprSnap = fSnap
  101. def getHprSnap(self):
  102. return self.fHprSnap
  103. def computeSnapPoint(self, point):
  104. # Start of with current point
  105. self.snapPos.assign(point)
  106. # Snap if necessary
  107. if self.fXyzSnap:
  108. self.snapPos.set(
  109. ROUND_TO(self.snapPos[0], self.gridSpacing),
  110. ROUND_TO(self.snapPos[1], self.gridSpacing),
  111. ROUND_TO(self.snapPos[2], self.gridSpacing))
  112. # Move snap marker to this point
  113. self.snapMarker.setPos(self.snapPos)
  114. # Return the hit point
  115. return self.snapPos
  116. def computeSnapAngle(self, angle):
  117. return ROUND_TO(angle, self.snapAngle)
  118. def setSnapAngle(self, angle):
  119. self.snapAngle = angle
  120. def getSnapAngle(self):
  121. return self.snapAngle
  122. def setGridSpacing(self, spacing):
  123. self.gridSpacing = spacing
  124. self.updateGrid()
  125. def getGridSpacing(self):
  126. return self.gridSpacing
  127. def setGridSize(self, size):
  128. # Set size of grid back and redraw lines
  129. self.gridSize = size
  130. self.updateGrid()
  131. def getGridSize(self):
  132. return self.gridSize