Class: Rgltf::Extensions::KHRLightsPunctual

Inherits:
Rgltf::Extension show all
Defined in:
lib/rgltf/extensions/khr_lights_punctual.rb

Defined Under Namespace

Classes: Collection, Light, NodeLight, Spot

Class Method Summary collapse

Methods inherited from Rgltf::Extension

decode_primitive, register, serialize_properties

Class Method Details

.copy_hash(value) ⇒ Object



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

def self.copy_hash(value)
  value.to_h { |key, child| [key, child] }
end

.parse(json, owner:, doc:) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rgltf/extensions/khr_lights_punctual.rb', line 50

def self.parse(json, owner:, doc:)
  return Collection.new(json) if owner.is_a?(Document)

  collection = doc.extension('KHR_lights_punctual')
  index = json.fetch('light')
  light = collection&.lights&.fetch(index)
  raise FormatError, "node references missing punctual light #{index}" unless light

  NodeLight.new(light)
rescue IndexError, KeyError => e
  raise FormatError, "invalid KHR_lights_punctual: #{e.message}"
end

.serialize(object, doc:) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/rgltf/extensions/khr_lights_punctual.rb', line 63

def self.serialize(object, doc:)
  if object.is_a?(Collection)
    value = { 'lights' => object.lights.map { |light| serialize_light(light) } }
    return serialize_properties(value, handled_keys: %w[lights])
  end

  serialize_properties({ 'light' => object.light.index }, handled_keys: %w[light])
end

.serialize_light(light) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rgltf/extensions/khr_lights_punctual.rb', line 72

def self.serialize_light(light)
  copy_hash(light.source_json).tap do |json|
    %w[name type color intensity range spot].each { |key| json.delete(key) }
    json['type'] = light.type.to_s
    json['name'] = light.name if light.name
    json['color'] = light.color unless light.color == [1.0, 1.0, 1.0]
    json['intensity'] = light.intensity unless light.intensity == 1.0
    json['range'] = light.range if light.range
    if light.spot
      spot = copy_hash(light.spot.source_json)
      %w[innerConeAngle outerConeAngle].each { |key| spot.delete(key) }
      spot['innerConeAngle'] = light.spot.inner_cone_angle unless light.spot.inner_cone_angle == 0.0
      spot['outerConeAngle'] = light.spot.outer_cone_angle unless light.spot.outer_cone_angle == Math::PI / 4.0
      json['spot'] = spot unless spot.empty?
    end
  end
end