Class: Rgltf::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rgltf/document.rb

Constant Summary collapse

COLLECTIONS =
{
  'buffers' => Buffer,
  'bufferViews' => BufferView,
  'accessors' => Accessor,
  'images' => Image,
  'samplers' => Sampler,
  'textures' => Texture,
  'materials' => Material,
  'meshes' => Mesh,
  'cameras' => Camera,
  'nodes' => Node,
  'skins' => Skin,
  'animations' => Animation,
  'scenes' => Scene
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, base_dir:, supplied_buffers:, glb_bin:, lazy:, extension_registry:, validate:, strict_extensions:) ⇒ Document

Returns a new instance of Document.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rgltf/document.rb', line 27

def initialize(json, base_dir:, supplied_buffers:, glb_bin:, lazy:,
               extension_registry:, validate:, strict_extensions:)
  @raw_json = json
  @extension_registry = extension_registry
  @extensions_used = json.fetch('extensionsUsed', []).freeze
  @extensions_required = json.fetch('extensionsRequired', []).freeze
  @extras = json['extras']
  reject_unsupported_extensions! if strict_extensions
  @buffer_resolver = BufferResolver.new(base_dir:, glb_bin:, supplied_buffers:)
  @asset = Asset.new(json.fetch('asset'), document: self)
  reject_unsupported_version!

  build_collections(json)
  validate_default_scene_reference!
  @extensions = parse_extensions(json.fetch('extensions', {}), :document, self).freeze
  nodes.each(&:resolve_references!)
  parse_property_extensions!
  decode_primitives!
  buffers.each(&:bytes) unless lazy

  @validation_result = Validator.validate(self) if validate || !strict_extensions
  if validate && !validation_result.errors.empty?
    raise ValidationError, validation_result.errors.map(&:message).join('; ')
  end
rescue KeyError => e
  raise FormatError, "missing required glTF property: #{e.message}"
end

Instance Attribute Details

#accessorsObject (readonly)

Returns the value of attribute accessors.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def accessors
  @accessors
end

#animationsObject (readonly)

Returns the value of attribute animations.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def animations
  @animations
end

#assetObject (readonly)

Returns the value of attribute asset.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def asset
  @asset
end

#buffer_resolverObject (readonly)

Returns the value of attribute buffer_resolver.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def buffer_resolver
  @buffer_resolver
end

#buffer_viewsObject (readonly)

Returns the value of attribute buffer_views.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def buffer_views
  @buffer_views
end

#buffersObject (readonly)

Returns the value of attribute buffers.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def buffers
  @buffers
end

#camerasObject (readonly)

Returns the value of attribute cameras.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def cameras
  @cameras
end

#extension_registryObject (readonly)

Returns the value of attribute extension_registry.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def extension_registry
  @extension_registry
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def extensions
  @extensions
end

#extensions_requiredObject (readonly)

Returns the value of attribute extensions_required.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def extensions_required
  @extensions_required
end

#extensions_usedObject (readonly)

Returns the value of attribute extensions_used.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def extensions_used
  @extensions_used
end

#extrasObject (readonly)

Returns the value of attribute extras.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def extras
  @extras
end

#imagesObject (readonly)

Returns the value of attribute images.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def images
  @images
end

#materialsObject (readonly)

Returns the value of attribute materials.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def materials
  @materials
end

#meshesObject (readonly)

Returns the value of attribute meshes.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def meshes
  @meshes
end

#nodesObject (readonly)

Returns the value of attribute nodes.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def nodes
  @nodes
end

#raw_jsonObject (readonly)

Returns the value of attribute raw_json.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def raw_json
  @raw_json
end

#samplersObject (readonly)

Returns the value of attribute samplers.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def samplers
  @samplers
end

#scenesObject (readonly)

Returns the value of attribute scenes.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def scenes
  @scenes
end

#skinsObject (readonly)

Returns the value of attribute skins.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def skins
  @skins
end

#texturesObject (readonly)

Returns the value of attribute textures.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def textures
  @textures
end

#validation_resultObject (readonly)

Returns the value of attribute validation_result.



21
22
23
# File 'lib/rgltf/document.rb', line 21

def validation_result
  @validation_result
end

Instance Method Details

#default_sceneObject



62
63
64
# File 'lib/rgltf/document.rb', line 62

def default_scene
  @default_scene_index.nil? ? nil : fetch_reference(:scenes, @default_scene_index, 'scene')
end

#each_mesh_instanceObject



70
71
72
73
74
75
76
77
# File 'lib/rgltf/document.rb', line 70

def each_mesh_instance
  return enum_for(:each_mesh_instance) unless block_given?

  scenes.each do |scene|
    scene.nodes.each { |node| visit_mesh_instances(node, Matrix4::IDENTITY, {}) { |*values| yield(*values) } }
  end
  self
end

#extension(name) ⇒ Object



66
67
68
# File 'lib/rgltf/document.rb', line 66

def extension(name)
  extensions[name]
end

#fetch_reference(collection, index, path) ⇒ Object

Raises:



55
56
57
58
59
60
# File 'lib/rgltf/document.rb', line 55

def fetch_reference(collection, index, path)
  objects = public_send(collection)
  return objects[index] if index.is_a?(Integer) && index >= 0 && index < objects.length

  raise FormatError, "#{path} references missing #{collection.to_s.tr('_', ' ')} index #{index.inspect}"
end

#release_buffers!Object



79
80
81
82
83
84
# File 'lib/rgltf/document.rb', line 79

def release_buffers!
  accessors.each(&:release!)
  images.each(&:release!)
  buffer_resolver.release!
  self
end

#to_glb(validate: true) ⇒ Object



86
87
88
# File 'lib/rgltf/document.rb', line 86

def to_glb(validate: true)
  Writer.new(self, validate:).to_glb
end

#write_glb(path, validate: true) ⇒ Object



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

def write_glb(path, validate: true)
  Writer.new(self, validate:).write_glb(path)
end

#write_gltf(path, embed: false, validate: true) ⇒ Object



94
95
96
# File 'lib/rgltf/document.rb', line 94

def write_gltf(path, embed: false, validate: true)
  Writer.new(self, validate:).write_gltf(path, embed:)
end