Class: Three::PerspectiveCamera

Inherits:
Camera show all
Defined in:
lib/three/cameras/perspective_camera.rb

Constant Summary

Constants inherited from Object3D

Object3D::DEFAULT_MATRIX_AUTO_UPDATE, Object3D::DEFAULT_MATRIX_WORLD_AUTO_UPDATE, Object3D::DEFAULT_UP

Instance Attribute Summary collapse

Attributes inherited from Camera

#matrix_world_inverse, #projection_matrix, #projection_matrix_inverse

Attributes inherited from Object3D

#cast_shadow, #children, #id, #layers, #matrix, #matrix_auto_update, #matrix_world, #matrix_world_auto_update, #matrix_world_needs_update, #name, #parent, #position, #quaternion, #receive_shadow, #rotation, #scale, #type, #up, #user_data, #uuid, #visible

Instance Method Summary collapse

Methods inherited from Camera

#get_world_direction, #update_matrix_world, #update_world_matrix

Methods inherited from Object3D

#add, allocate_id, #clear, #dirty_dependency_changed, #get_object_by_id, #get_object_by_name, #get_object_by_property, #get_objects_by_property, #get_world_direction, #get_world_position, #get_world_quaternion, #get_world_scale, #mark_descendant_dirty!, #mark_dirty!, #remove, #remove_from_parent, #to_h, #to_json, #traverse, #traverse_ancestors, #traverse_visible, #update_matrix, #update_matrix_world, #update_world_matrix

Methods included from Dirty

#add_dirty_dependent, #dirty?, #dirty_dependents, #dirty_field?, #dirty_fields, #mark_clean!, #mark_dirty!, #remove_dirty_dependent

Methods inherited from EventDispatcher

#add_event_listener, #dispatch_event, #has_event_listener?, #remove_event_listener

Constructor Details

#initialize(fov = 50, aspect: 1, near: 0.1, far: 2000) ⇒ PerspectiveCamera

Returns a new instance of PerspectiveCamera.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/three/cameras/perspective_camera.rb', line 11

def initialize(fov = 50, aspect: 1, near: 0.1, far: 2000)
  super()
  @type = "PerspectiveCamera"
  @fov = fov
  @zoom = 1
  @near = near
  @far = far
  @focus = 10
  @aspect = aspect
  @view = nil
  @film_gauge = 35
  @film_offset = 0
  update_projection_matrix
end

Instance Attribute Details

#aspectObject

Returns the value of attribute aspect.



8
9
10
# File 'lib/three/cameras/perspective_camera.rb', line 8

def aspect
  @aspect
end

#farObject

Returns the value of attribute far.



8
9
10
# File 'lib/three/cameras/perspective_camera.rb', line 8

def far
  @far
end

#film_gaugeObject

Returns the value of attribute film_gauge.



8
9
10
# File 'lib/three/cameras/perspective_camera.rb', line 8

def film_gauge
  @film_gauge
end

#film_offsetObject

Returns the value of attribute film_offset.



8
9
10
# File 'lib/three/cameras/perspective_camera.rb', line 8

def film_offset
  @film_offset
end

#focusObject

Returns the value of attribute focus.



8
9
10
# File 'lib/three/cameras/perspective_camera.rb', line 8

def focus
  @focus
end

#fovObject

Returns the value of attribute fov.



8
9
10
# File 'lib/three/cameras/perspective_camera.rb', line 8

def fov
  @fov
end

#nearObject

Returns the value of attribute near.



8
9
10
# File 'lib/three/cameras/perspective_camera.rb', line 8

def near
  @near
end

#viewObject (readonly)

Returns the value of attribute view.



9
10
11
# File 'lib/three/cameras/perspective_camera.rb', line 9

def view
  @view
end

#zoomObject

Returns the value of attribute zoom.



8
9
10
# File 'lib/three/cameras/perspective_camera.rb', line 8

def zoom
  @zoom
end

Instance Method Details

#clear_view_offsetObject



107
108
109
110
111
112
# File 'lib/three/cameras/perspective_camera.rb', line 107

def clear_view_offset
  @view[:enabled] = false if @view
  mark_dirty!(:camera)
  update_projection_matrix
  self
end

#effective_fovObject



79
80
81
# File 'lib/three/cameras/perspective_camera.rb', line 79

def effective_fov
  MathUtils.rad_to_deg(2 * Math.atan(Math.tan(MathUtils.deg_to_rad(0.5 * @fov)) / @zoom))
end

#film_heightObject



87
88
89
# File 'lib/three/cameras/perspective_camera.rb', line 87

def film_height
  @film_gauge / [@aspect, 1].max
end

#film_widthObject



83
84
85
# File 'lib/three/cameras/perspective_camera.rb', line 83

def film_width
  @film_gauge * [@aspect, 1].min
end

#focal_lengthObject



74
75
76
77
# File 'lib/three/cameras/perspective_camera.rb', line 74

def focal_length
  vertical_extent_slope = Math.tan(MathUtils.deg_to_rad(0.5 * @fov))
  0.5 * film_height / vertical_extent_slope
end

#set_focal_length(focal_length) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/three/cameras/perspective_camera.rb', line 66

def set_focal_length(focal_length)
  vertical_extent_slope = 0.5 * film_height / focal_length
  @fov = MathUtils.rad_to_deg(2 * Math.atan(vertical_extent_slope))
  mark_dirty!(:camera)
  update_projection_matrix
  self
end

#set_view_offset(full_width, full_height, x, y, width, height) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/three/cameras/perspective_camera.rb', line 91

def set_view_offset(full_width, full_height, x, y, width, height)
  @aspect = full_width.to_f / full_height
  @view = {
    enabled: true,
    full_width: full_width,
    full_height: full_height,
    offset_x: x,
    offset_y: y,
    width: width,
    height: height
  }
  mark_dirty!(:camera)
  update_projection_matrix
  self
end

#update_projection_matrixObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/three/cameras/perspective_camera.rb', line 114

def update_projection_matrix
  top = @near * Math.tan(MathUtils.deg_to_rad(0.5 * @fov)) / @zoom
  height = 2 * top
  width = @aspect * height
  left = -0.5 * width

  if @view && @view[:enabled]
    full_width = @view[:full_width]
    full_height = @view[:full_height]
    left += @view[:offset_x] * width / full_width
    top -= @view[:offset_y] * height / full_height
    width *= @view[:width].to_f / full_width
    height *= @view[:height].to_f / full_height
  end

  left += @near * @film_offset / film_width unless @film_offset.zero?

  @projection_matrix.make_perspective(left, left + width, top, top - height, @near, @far)
  @projection_matrix_inverse.copy(@projection_matrix).invert
  mark_dirty!(:camera)
  self
end