Class: Rgltf::Document
- Inherits:
-
Object
- Object
- Rgltf::Document
- 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
-
#accessors ⇒ Object
readonly
Returns the value of attribute accessors.
-
#animations ⇒ Object
readonly
Returns the value of attribute animations.
-
#asset ⇒ Object
readonly
Returns the value of attribute asset.
-
#buffer_resolver ⇒ Object
readonly
Returns the value of attribute buffer_resolver.
-
#buffer_views ⇒ Object
readonly
Returns the value of attribute buffer_views.
-
#buffers ⇒ Object
readonly
Returns the value of attribute buffers.
-
#cameras ⇒ Object
readonly
Returns the value of attribute cameras.
-
#extension_registry ⇒ Object
readonly
Returns the value of attribute extension_registry.
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#extensions_required ⇒ Object
readonly
Returns the value of attribute extensions_required.
-
#extensions_used ⇒ Object
readonly
Returns the value of attribute extensions_used.
-
#extras ⇒ Object
readonly
Returns the value of attribute extras.
-
#images ⇒ Object
readonly
Returns the value of attribute images.
-
#materials ⇒ Object
readonly
Returns the value of attribute materials.
-
#meshes ⇒ Object
readonly
Returns the value of attribute meshes.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#raw_json ⇒ Object
readonly
Returns the value of attribute raw_json.
-
#samplers ⇒ Object
readonly
Returns the value of attribute samplers.
-
#scenes ⇒ Object
readonly
Returns the value of attribute scenes.
-
#skins ⇒ Object
readonly
Returns the value of attribute skins.
-
#textures ⇒ Object
readonly
Returns the value of attribute textures.
-
#validation_result ⇒ Object
readonly
Returns the value of attribute validation_result.
Instance Method Summary collapse
- #default_scene ⇒ Object
- #each_mesh_instance ⇒ Object
- #extension(name) ⇒ Object
- #fetch_reference(collection, index, path) ⇒ Object
-
#initialize(json, base_dir:, supplied_buffers:, glb_bin:, lazy:, extension_registry:, validate:, strict_extensions:) ⇒ Document
constructor
A new instance of Document.
- #release_buffers! ⇒ Object
- #to_glb(validate: true) ⇒ Object
- #write_glb(path, validate: true) ⇒ Object
- #write_gltf(path, embed: false, validate: true) ⇒ Object
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.}" end |
Instance Attribute Details
#accessors ⇒ Object (readonly)
Returns the value of attribute accessors.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def accessors @accessors end |
#animations ⇒ Object (readonly)
Returns the value of attribute animations.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def animations @animations end |
#asset ⇒ Object (readonly)
Returns the value of attribute asset.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def asset @asset end |
#buffer_resolver ⇒ Object (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_views ⇒ Object (readonly)
Returns the value of attribute buffer_views.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def buffer_views @buffer_views end |
#buffers ⇒ Object (readonly)
Returns the value of attribute buffers.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def buffers @buffers end |
#cameras ⇒ Object (readonly)
Returns the value of attribute cameras.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def cameras @cameras end |
#extension_registry ⇒ Object (readonly)
Returns the value of attribute extension_registry.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def extension_registry @extension_registry end |
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def extensions @extensions end |
#extensions_required ⇒ Object (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_used ⇒ Object (readonly)
Returns the value of attribute extensions_used.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def extensions_used @extensions_used end |
#extras ⇒ Object (readonly)
Returns the value of attribute extras.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def extras @extras end |
#images ⇒ Object (readonly)
Returns the value of attribute images.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def images @images end |
#materials ⇒ Object (readonly)
Returns the value of attribute materials.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def materials @materials end |
#meshes ⇒ Object (readonly)
Returns the value of attribute meshes.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def meshes @meshes end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def nodes @nodes end |
#raw_json ⇒ Object (readonly)
Returns the value of attribute raw_json.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def raw_json @raw_json end |
#samplers ⇒ Object (readonly)
Returns the value of attribute samplers.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def samplers @samplers end |
#scenes ⇒ Object (readonly)
Returns the value of attribute scenes.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def scenes @scenes end |
#skins ⇒ Object (readonly)
Returns the value of attribute skins.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def skins @skins end |
#textures ⇒ Object (readonly)
Returns the value of attribute textures.
21 22 23 |
# File 'lib/rgltf/document.rb', line 21 def textures @textures end |
#validation_result ⇒ Object (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_scene ⇒ Object
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_instance ⇒ Object
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
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 |