class Mittsu::Raycaster

Attributes

far[RW]
near[RW]
params[RW]
precision[RW]
ray[RW]

Public Class Methods

new(origin = Vector3.new, direction = Vector3.new, near = 0.0, far = Float::INFINITY) click to toggle source
# File lib/mittsu/core/raycaster.rb, line 5
def initialize(origin = Vector3.new, direction = Vector3.new, near = 0.0, far = Float::INFINITY)
  @ray = Mittsu::Ray.new(origin, direction)
  # direction is assumed to be normalized (for accurate distance calculations)

  @precision = 0.0001
  @line_precision = 1

  @near, @far = near, far

  @params = {
    sprite: {},
    mesh: {},
    point_cloud: { threshold: 1.0 },
    lod:{},
    line: {}
  }
end

Public Instance Methods

intersect_object(object, recursive = false) click to toggle source
# File lib/mittsu/core/raycaster.rb, line 42
def intersect_object(object, recursive = false)
  intersects = []
  intersect(object, intersects, recursive)
  intersects.sort do |a, b|
    a[:distance] <=> b[:distance]
  end
end
intersect_objects(objects, recursive = false) click to toggle source
# File lib/mittsu/core/raycaster.rb, line 50
def intersect_objects(objects, recursive = false)
  intersects = []
  if !objects.is_a? Array
    puts 'WARNING: Mittsu::Raycaster#intersect_objects: objects is not an array'
    return intersects
  end

  objects.each do |object|
    intersect(object, intersects, recursive)
  end

  intersects.sort do |a, b|
    a[:distance] <=> b[:distance]
  end
end
set(origin, direction) click to toggle source
# File lib/mittsu/core/raycaster.rb, line 23
def set(origin, direction)
  # direction is assumed to be normalized (for accurate distance calculations)
  @ray.set(origin, direction)
end
set_from_camera(coords, camera) click to toggle source
# File lib/mittsu/core/raycaster.rb, line 28
def set_from_camera(coords, camera)
  # camera is assumed _not_ to be a child of a transformed object

  if camera.is_a? Mittsu::PerspectiveCamera
    @ray.origin.copy(camera.position)
    @ray.direction.set(coords.x, coords.y, 0.5).unproject(camera).sub(camera.position).normalize
  elsif camera.is_a? Mittsu::OrthographicCamera
    @ray.origin.set(coords.x, coords.y, -1.0).unproject(camera)
    @ray.direction.set(0.0, 0.0, -1.0).transform_direction(camera.matrix_world)
  else
    puts 'ERROR: Mittsu::Raycaster: Unsupported camera type'
  end
end

Private Instance Methods

intersect(object, intersects, recursive) click to toggle source
# File lib/mittsu/core/raycaster.rb, line 68
def intersect(object, intersects, recursive)
  object.raycast(self, intersects)

  if recursive
    object.children.each do |child|
      intersect(child, intersects, true)
    end
  end
end