Skip to content

Commit 8eaebbe

Browse files
committed
add KHR_lights_punctual, Light, LightSpot
1 parent c9772d2 commit 8eaebbe

File tree

1 file changed

+131
-8
lines changed

1 file changed

+131
-8
lines changed

src/compas/files/gltf/extensions.py

Lines changed: 131 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
from .data_classes import NormalTextureInfoData
44

55

6+
def create_if_data(cls, data, attr):
7+
return cls.from_data(data.get(attr)) if attr in data and len(data[attr]) else None
8+
9+
610
class KHR_materials_transmission(BaseGLTFDataClass):
711
"""glTF extension that defines the optical transmission of a material.
812
@@ -40,7 +44,7 @@ def from_data(cls, dct):
4044
return None
4145
return cls(
4246
transmission_factor=dct.get("transmissionFactor"),
43-
transmission_texture=TextureInfoData.from_data(dct.get("transmissionTexture")),
47+
transmission_texture=create_if_data(TextureInfoData, dct, "transmissionTexture"),
4448
extensions=cls.extensions_from_data(dct.get("extensions")),
4549
extras=dct.get("extras"),
4650
)
@@ -91,9 +95,9 @@ def from_data(cls, dct):
9195
return None
9296
return cls(
9397
specular_factor=dct.get("specularFactor"),
94-
specular_texture=TextureInfoData.from_data(dct.get("specularTexture")),
98+
specular_texture=create_if_data(TextureInfoData, dct, "specularTexture"),
9599
specular_color_factor=dct.get("specularColorFactor"),
96-
specular_color_texture=TextureInfoData.from_data(dct.get("specularColorTexture")),
100+
specular_color_texture=create_if_data(TextureInfoData, dct, "specularColorTexture"),
97101
extensions=cls.extensions_from_data(dct.get("extensions")),
98102
extras=dct.get("extras"),
99103
)
@@ -186,10 +190,10 @@ def from_data(cls, dct):
186190
return None
187191
return cls(
188192
clearcoat_factor=dct.get("clearcoatFactor"),
189-
clearcoat_texture=TextureInfoData.from_data(dct.get("clearcoatTexture")),
193+
clearcoat_texture=create_if_data(TextureInfoData, dct, "clearcoatTexture"),
190194
clearcoat_roughness_factor=dct.get("clearcoatRoughnessFactor"),
191-
clearcoat_roughness_texture=TextureInfoData.from_data(dct.get("clearcoatRoughnessTexture")),
192-
clearcoat_normal_texture=NormalTextureInfoData.from_data(dct.get("clearcoatNormalTexture")),
195+
clearcoat_roughness_texture=create_if_data(TextureInfoData, dct, "clearcoatRoughnessTexture"),
196+
clearcoat_normal_texture=create_if_data(NormalTextureInfoData, dct, "clearcoatNormalTexture"),
193197
extensions=cls.extensions_from_data(dct.get("extensions")),
194198
extras=dct.get("extras"),
195199
)
@@ -294,20 +298,139 @@ def from_data(cls, dct):
294298
return None
295299
return cls(
296300
diffuse_factor=dct.get("diffuseFactor"),
297-
diffuse_texture=TextureInfoData.from_data(dct.get("diffuseTexture")),
301+
diffuse_texture=create_if_data(TextureInfoData, dct, "diffuseTexture"),
298302
specular_factor=dct.get("specularFactor"),
299303
glossiness_factor=dct.get("glossinessFactor"),
300-
specular_glossiness_texture=TextureInfoData.from_data(dct.get("specularGlossinessTexture")),
304+
specular_glossiness_texture=create_if_data(TextureInfoData, dct, "specularGlossinessTexture"),
305+
extensions=cls.extensions_from_data(dct.get("extensions")),
306+
extras=dct.get("extras"),
307+
)
308+
309+
310+
class LightSpot(BaseGLTFDataClass):
311+
"""LightSpot
312+
313+
https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual/schema/light.spot.schema.json
314+
"""
315+
316+
def __init__(
317+
self,
318+
innerConeAngle=None,
319+
outerConeAngle=None,
320+
extensions=None,
321+
extras=None,
322+
):
323+
super(LightSpot, self).__init__(extras, extensions)
324+
self.innerConeAngle = innerConeAngle # Angle in radians from centre of spotlight where falloff ends.
325+
self.outerConeAngle = outerConeAngle # Angle in radians from centre of spotlight where falloff begins.
326+
327+
def to_data(self, texture_index_by_key, **kwargs):
328+
dct = super(LightSpot, self).to_data(texture_index_by_key, **kwargs)
329+
if self.innerConeAngle is not None:
330+
dct["innerConeAngle"] = self.innerConeAngle
331+
if self.outerConeAngle is not None:
332+
dct["outerConeAngle"] = self.outerConeAngle
333+
return dct
334+
335+
@classmethod
336+
def from_data(cls, dct):
337+
if dct is None:
338+
return None
339+
return cls(
340+
innerConeAngle=dct.get("innerConeAngle"), outerConeAngle=dct.get("outerConeAngle"), extensions=cls.extensions_from_data(dct.get("extensions")), extras=dct.get("extras")
341+
)
342+
343+
344+
class LightType(object):
345+
"""Specifies the light type."""
346+
347+
directional = "directional"
348+
point = "point"
349+
spot = "spot"
350+
351+
352+
class Light(BaseGLTFDataClass):
353+
"""A directional, point, or spot light.
354+
355+
https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual/schema/light.schema.json
356+
"""
357+
358+
key = "KHR_lights_punctual"
359+
360+
def __init__(self, color=None, intensity=None, spot=None, type=None, range=None, name=None, extensions=None, extras=None):
361+
super(Light, self).__init__(extras, extensions)
362+
self.color = color # [1, 1, 1]
363+
self.intensity = intensity # 1.0
364+
self.spot = spot # LightSpot
365+
self.type = type or LightType.directional
366+
self.range = range # A distance cutoff at which the light's intensity may be considered to have reached zero.
367+
self.name = name
368+
369+
def to_data(self, texture_index_by_key, **kwargs):
370+
dct = super(Light, self).to_data(texture_index_by_key, **kwargs)
371+
if self.color is not None:
372+
dct["color"] = self.color
373+
if self.intensity is not None:
374+
dct["intensity"] = self.intensity
375+
if self.spot is not None:
376+
dct["spot"] = self.spot.to_data()
377+
if self.type is not None:
378+
dct["type"] = self.type
379+
if self.range is not None:
380+
dct["range"] = self.range
381+
if self.name is not None:
382+
dct["name"] = self.name
383+
return dct
384+
385+
@classmethod
386+
def from_data(cls, dct):
387+
if dct is None:
388+
return None
389+
return cls(
390+
color=dct.get("color"),
391+
intensity=dct.get("intensity"),
392+
spot=LightSpot.from_data(dct.get("spot")),
393+
type=dct.get("type"),
394+
range=dct.get("range"),
395+
name=dct.get("name"),
301396
extensions=cls.extensions_from_data(dct.get("extensions")),
302397
extras=dct.get("extras"),
303398
)
304399

305400

401+
class KHR_lights_punctual(BaseGLTFDataClass):
402+
""""""
403+
404+
key = "KHR_lights_punctual"
405+
406+
def __init__(
407+
self,
408+
lights=None,
409+
extensions=None,
410+
extras=None,
411+
):
412+
super(KHR_lights_punctual, self).__init__(extras, extensions)
413+
self.lights = lights
414+
415+
def to_data(self, texture_index_by_key, **kwargs):
416+
dct = super(KHR_lights_punctual, self).to_data(texture_index_by_key, **kwargs)
417+
if self.lights is not None:
418+
dct["lights"] = self.lights
419+
return dct
420+
421+
@classmethod
422+
def from_data(cls, dct):
423+
if dct is None:
424+
return None
425+
return cls(lights=dct.get("lights"), extensions=cls.extensions_from_data(dct.get("extensions")), extras=dct.get("extras"))
426+
427+
306428
SUPPORTED_EXTENSIONS = {
307429
KHR_materials_transmission.key: KHR_materials_transmission,
308430
KHR_materials_clearcoat.key: KHR_materials_clearcoat,
309431
KHR_Texture_Transform.key: KHR_Texture_Transform,
310432
KHR_materials_pbrSpecularGlossiness.key: KHR_materials_pbrSpecularGlossiness,
311433
KHR_materials_specular.key: KHR_materials_specular,
312434
KHR_materials_ior.key: KHR_materials_ior,
435+
KHR_lights_punctual.key: KHR_lights_punctual,
313436
}

0 commit comments

Comments
 (0)