Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if not self.raycast or self._pq.get_num_entries() == 0:
self.unhover_everything_not_hit()
return False
self._pq.sortEntries()
for entry in self._pq.getEntries():
for entity in scene.entities:
if entry.getIntoNodePath().parent == entity and entity.collision:
if entity.collision:
hit = HitInfo(
hit = entry.collided(),
entity = entity,
distance = distance(entry.getSurfacePoint(scene), camera.getPos()),
point = entry.getSurfacePoint(entity),
world_point = entry.getSurfacePoint(scene),
normal = entry.getSurfaceNormal(entity),
world_normal = entry.getSurfaceNormal(scene),
)
self.collisions.append(hit)
break
if self.collisions:
self.collision = self.collisions[0]
self.hovered_entity = self.collision.entity
if not self.hovered_entity.hovered:
self.hovered_entity.hovered = True
if hasattr(self.hovered_entity, 'on_mouse_enter'):
self.hovered_entity.on_mouse_enter()
for s in self.hovered_entity.scripts:
if hasattr(s, 'on_mouse_enter'):
s.on_mouse_enter()
self.prev_x = 0
self.prev_y = 0
self.start_x = 0
self.start_y = 0
self.velocity = Vec3(0,0,0)
self.prev_click_time = time.time()
self.double_click_distance = .5
self.hovered_entity = None
self.left = False
self.right = False
self.middle = False
self.delta_drag = Vec3(0,0,0)
self.update_step = 1
self.traverse_target = scene
self._i = 0
self._mouse_watcher = None
self._picker = CollisionTraverser() # Make a traverser
self._pq = CollisionHandlerQueue() # Make a handler
self._pickerNode = CollisionNode('mouseRay')
self._pickerNP = camera.attach_new_node(self._pickerNode)
self._pickerRay = CollisionRay() # Make our ray
self._pickerNode.addSolid(self._pickerRay)
self._picker.addCollider(self._pickerNP, self._pq)
self._pickerNode.set_into_collide_mask(0)
self.raycast = True
self.collision = None
self.collisions = list()
self.enabled = True
self.enabled = True # disabled entities wil not be visible nor run code
self.visible = True
self.ignore = False # if True, will not try to run code
self.eternal = False # eternal entities does not get destroyed on scene.clear()
self.ignore_paused = False
self.ignore_input = False
self.parent = scene
self.add_to_scene_entities = add_to_scene_entities # set to False to be ignored by the engine, but still get rendered.
if add_to_scene_entities:
scene.entities.append(self)
self.model = None # set model with model='model_name' (without file type extention)
self.color = color.white
self.texture = None # set model with texture='texture_name'. requires a model to be set beforehand.
self.reflection_map = scene.reflection_map
self.reflectivity = 0
self.render_queue = 0
self.double_sided = False
# self.always_on_top = False
self.collision = False # toggle collision without changing collider.
self.collider = None # set to 'box'/'sphere'/'mesh' for auto fitted collider.
self.scripts = list() # add with add_script(class_instance). will assign an 'entity' variable to the script.
self.animations = list()
self.hovered = False # will return True if mouse hovers entity.
self.origin = Vec3(0,0,0)
self.position = Vec3(0,0,0) # right, up, forward. can also set self.x, self.y, self.z
self.rotation = Vec3(0,0,0) # can also set self.rotation_x, self.rotation_y, self.rotation_z
self.scale = Vec3(1,1,1) # can also set self.scale_x, self.scale_y, self.scale_z
def __init__(self):
super().__init__()
self.parent = scene
self.name = 'camera'
self.eternal = True
self._cam = None
self._render = None
self.ui_size = 40
self._ui_lens_node = None
self.ui = None
self.fov = 40
self.orthographic = False
world_normal = closest.world_normal,
hits = [e.hit for e in rays],
entities = list(set([e.entity for e in rays])), # get unique entities hit
)
sys.modules[__name__] = Raycaster()
if __name__ == '__main__':
app = Ursina()
from ursina.entity import Entity
d = Entity(parent=scene, position=(0,0,2), model='cube', color=color.orange, collider='box', scale=2)
e = Entity(model='cube', color=color.lime)
camera.position = (0, 15, -15)
camera.look_at(e)
# camera.reparent_to(e)
speed = .01
rotation_speed = .1
intersection_marker = Entity(model='cube', scale=.2, color=color.red)
def update():
e.position += e.forward * held_keys['w'] * speed
e.position += e.left * held_keys['a'] * speed
e.position += e.back * held_keys['s'] * speed
e.position += e.right * held_keys['d'] * speed
e.rotation_y -= held_keys['q'] * rotation_speed
for key, value in kwargs.items():
if key == 'type':
if value=='ambient' or value=='directional':
self.type = value
else:
print("ERR Light type is not 'ambient' or 'directional'")
elif key == 'color':
self.color = value
elif key == 'direction':
self.direction = value
else:
print("Err ",key," is not a valid keyword")
scene.lights.append(self) # light added for all subsequent entities
if self.type == 'ambient':
ambientLight = AmbientLight('ambientLight')
ambientLight.setColor(self.color)
self.node = scene.attachNewNode(ambientLight)
if self.type == 'directional':
directionalLight = DirectionalLight('directionalLight')
directionalLight.setColor(self.color)
self.node = scene.attachNewNode(directionalLight)
# This light should be facing straight down, but clearly it isn't.
self.node.setHpr(self.direction) # convert vector to Hpr (in degrees!?) first
def _destroy(entity):
if not entity:
print('entity is None')
return
if entity in scene.entities:
scene.entities.remove(entity)
if hasattr(entity, 'on_destroy'):
entity.on_destroy()
if hasattr(entity, 'scripts'):
for s in entity.scripts:
del s
if hasattr(entity, 'animations'):
for anim in entity.animations:
anim.finish()
anim.kill()
if hasattr(entity, 'tooltip'):
destroy(entity.tooltip)
# entity.tooltip.removeNode()
self._i += 1
if self._i < self.update_step:
return
# collide with ui
self._pickerNP.reparent_to(scene.ui_camera)
self._pickerRay.set_from_lens(camera._ui_lens_node, self.x * 2 / window.aspect_ratio, self.y * 2)
self._picker.traverse(camera.ui)
if self._pq.get_num_entries() > 0:
# print('collided with ui', self._pq.getNumEntries())
self.find_collision()
return
# collide with world
self._pickerNP.reparent_to(camera)
self._pickerRay.set_from_lens(scene.camera.lens_node, self.x * 2 / window.aspect_ratio, self.y * 2)
try:
self._picker.traverse(self.traverse_target)
except:
print('error: mouse._picker could not traverse', self.traverse_target)
return
if self._pq.get_num_entries() > 0:
self.find_collision()
else:
# print('mouse miss', base.render)
# unhover all if it didn't hit anything
for entity in scene.entities:
if hasattr(entity, 'hovered') and entity.hovered:
entity.hovered = False
self.hovered_entity = None
if hasattr(entity, 'on_mouse_exit'):
def get_position(self, relative_to=scene):
return self.getPos(relative_to)
def set_shader_input(self, name, value):
if self.filter_quad:
self.filter_quad.setShaderInput(name, value)
else:
print('no filter quad')
sys.modules[__name__] = Camera()
if __name__ == '__main__':
from ursina import *
app = Ursina()
# app.load_editor()
scene.camera.orthographic = True
e = Entity()
e.model = 'quad'
e.color = color.random_color()
e.position = (-2, 0, 10)
e = Entity()
e.model = 'quad'
e.color = color.random_color()
e.position = (2, 0, 10)
e = Entity()
e.model = 'quad'
e.color = color.random_color()
e.position = (0, 0, 40)
# from ursina import *
# Button(text='test button')