Class: Rgltf::Node

Inherits:
Properties::Base show all
Defined in:
lib/rgltf/properties/node.rb

Constant Summary collapse

IDENTITY =
Matrix4::IDENTITY

Instance Attribute Summary collapse

Attributes inherited from Properties::Base

#extensions, #extras, #index, #name, #owner_type, #source_json

Instance Method Summary collapse

Methods inherited from Properties::Base

#extension, #nested_properties, #parse_extensions!

Constructor Details

#initialize(json, document:, index:) ⇒ Node

Returns a new instance of Node.

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rgltf/properties/node.rb', line 49

def initialize(json, document:, index:)
  super(json, document:, index:, owner_type: :node)
  @child_indices = json.fetch('children', [])
  @mesh_index = json['mesh']
  @skin_index = json['skin']
  @camera_index = json['camera']
  @translation = json.fetch('translation', [0.0, 0.0, 0.0]).freeze
  @rotation = json.fetch('rotation', [0.0, 0.0, 0.0, 1.0]).freeze
  @scale = json.fetch('scale', [1.0, 1.0, 1.0]).freeze
  @weights = json['weights']&.freeze
  @matrix = json['matrix']&.freeze
  return unless @matrix && (json.key?('translation') || json.key?('rotation') || json.key?('scale'))

  raise FormatError, "node #{index} cannot define both matrix and TRS"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)



110
111
112
113
114
# File 'lib/rgltf/properties/node.rb', line 110

def method_missing(name, ...)
  return public_send(name, ...) if name == :to_larb && self.class.send(:install_larb_conversion!)

  super
end

Instance Attribute Details

#cameraObject (readonly)

Returns the value of attribute camera.



46
47
48
# File 'lib/rgltf/properties/node.rb', line 46

def camera
  @camera
end

#childrenObject (readonly)

Returns the value of attribute children.



46
47
48
# File 'lib/rgltf/properties/node.rb', line 46

def children
  @children
end

#meshObject (readonly)

Returns the value of attribute mesh.



46
47
48
# File 'lib/rgltf/properties/node.rb', line 46

def mesh
  @mesh
end

#rotationObject (readonly)

Returns the value of attribute rotation.



46
47
48
# File 'lib/rgltf/properties/node.rb', line 46

def rotation
  @rotation
end

#scaleObject (readonly)

Returns the value of attribute scale.



46
47
48
# File 'lib/rgltf/properties/node.rb', line 46

def scale
  @scale
end

#skinObject (readonly)

Returns the value of attribute skin.



46
47
48
# File 'lib/rgltf/properties/node.rb', line 46

def skin
  @skin
end

#translationObject (readonly)

Returns the value of attribute translation.



46
47
48
# File 'lib/rgltf/properties/node.rb', line 46

def translation
  @translation
end

#weightsObject (readonly)

Returns the value of attribute weights.



46
47
48
# File 'lib/rgltf/properties/node.rb', line 46

def weights
  @weights
end

Instance Method Details

#matrixObject



75
76
77
# File 'lib/rgltf/properties/node.rb', line 75

def matrix
  @matrix || (@computed_matrix ||= Matrix4.from_trs(translation, rotation, scale))
end

#resolve_references!Object



65
66
67
68
69
70
71
72
73
# File 'lib/rgltf/properties/node.rb', line 65

def resolve_references!
  @children = @child_indices.each_with_index.map do |child_index, child_position|
    @document.fetch_reference(:nodes, child_index, "nodes/#{index}/children/#{child_position}")
  end.freeze
  @mesh = optional_reference(:meshes, @mesh_index, "nodes/#{index}/mesh")
  @skin = optional_reference(:skins, @skin_index, "nodes/#{index}/skin")
  @camera = optional_reference(:cameras, @camera_index, "nodes/#{index}/camera")
  self
end

#traverse(&block) ⇒ Object



83
84
85
86
87
88
# File 'lib/rgltf/properties/node.rb', line 83

def traverse(&block)
  return enum_for(:traverse) unless block

  traverse_guarded({}, &block)
  self
end

#trs?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rgltf/properties/node.rb', line 79

def trs?
  @matrix.nil?
end

#world_matrix(parent_matrix = IDENTITY) ⇒ Object



90
91
92
# File 'lib/rgltf/properties/node.rb', line 90

def world_matrix(parent_matrix = IDENTITY)
  Matrix4.multiply(parent_matrix, matrix)
end