Class: Mittsu::Raycaster

Inherits:
Object
  • Object
show all
Defined in:
lib/mittsu/core/raycaster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin = Vector3.new, direction = Vector3.new, near = 0.0, far = Float::INFINITY) ⇒ Raycaster

Returns a new instance of Raycaster.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#farObject

Returns the value of attribute far.



3
4
5
# File 'lib/mittsu/core/raycaster.rb', line 3

def far
  @far
end

#nearObject

Returns the value of attribute near.



3
4
5
# File 'lib/mittsu/core/raycaster.rb', line 3

def near
  @near
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/mittsu/core/raycaster.rb', line 3

def params
  @params
end

#precisionObject

Returns the value of attribute precision.



3
4
5
# File 'lib/mittsu/core/raycaster.rb', line 3

def precision
  @precision
end

#rayObject

Returns the value of attribute ray.



3
4
5
# File 'lib/mittsu/core/raycaster.rb', line 3

def ray
  @ray
end

Instance Method Details

#intersect_object(object, recursive = false) ⇒ Object



42
43
44
45
46
47
48
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object



23
24
25
26
# 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) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# 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