-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add import support for EXT_lights_area #17146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
packages/dev/loaders/src/glTF/2.0/Extensions/EXT_lights_area.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| import type { Nullable } from "core/types"; | ||
| import { Vector3, Quaternion } from "core/Maths/math.vector"; | ||
| import { Color3 } from "core/Maths/math.color"; | ||
| import { Light } from "core/Lights/light"; | ||
| import { RectAreaLight } from "core/Lights/rectAreaLight"; | ||
| import type { TransformNode } from "core/Meshes/transformNode"; | ||
| import { TransformNode as BabylonTransformNode } from "core/Meshes/transformNode"; | ||
|
|
||
| import type { IEXTLightsArea_LightReference } from "babylonjs-gltf2interface"; | ||
| import { EXTLightsArea_LightShape } from "babylonjs-gltf2interface"; | ||
| import type { INode, IEXTLightsArea_Light } from "../glTFLoaderInterfaces"; | ||
| import type { IGLTFLoaderExtension } from "../glTFLoaderExtension"; | ||
| import { GLTFLoader, ArrayItem } from "../glTFLoader"; | ||
| import { registerGLTFExtension, unregisterGLTFExtension } from "../glTFLoaderExtensionRegistry"; | ||
|
|
||
| const NAME = "EXT_lights_area"; | ||
|
|
||
| declare module "../../glTFFileLoader" { | ||
| // eslint-disable-next-line jsdoc/require-jsdoc, @typescript-eslint/naming-convention | ||
| export interface GLTFLoaderExtensionOptions { | ||
| /** | ||
| * Defines options for the EXT_lights_area extension. | ||
| */ | ||
| // NOTE: Don't use NAME here as it will break the UMD type declarations. | ||
| ["EXT_lights_area"]: {}; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Vendor/EXT_lights_area/README.md) | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| export class EXT_lights_area implements IGLTFLoaderExtension { | ||
| /** | ||
| * The name of this extension. | ||
| */ | ||
| public readonly name = NAME; | ||
|
|
||
| /** | ||
| * Defines whether this extension is enabled. | ||
| */ | ||
| public enabled: boolean; | ||
|
|
||
| /** hidden */ | ||
| private _loader: GLTFLoader; | ||
| private _lights?: IEXTLightsArea_Light[]; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| constructor(loader: GLTFLoader) { | ||
| this._loader = loader; | ||
| this.enabled = this._loader.isExtensionUsed(NAME); | ||
| } | ||
|
|
||
| /** @internal */ | ||
| public dispose() { | ||
| (this._loader as any) = null; | ||
| delete this._lights; | ||
| } | ||
|
|
||
| /** @internal */ | ||
| public onLoading(): void { | ||
| const extensions = this._loader.gltf.extensions; | ||
| if (extensions && extensions[this.name]) { | ||
| const extension = extensions[this.name]; | ||
| this._lights = extension.lights; | ||
| ArrayItem.Assign(this._lights); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| // eslint-disable-next-line no-restricted-syntax | ||
| public loadNodeAsync(context: string, node: INode, assign: (babylonTransformNode: TransformNode) => void): Nullable<Promise<TransformNode>> { | ||
| return GLTFLoader.LoadExtensionAsync<IEXTLightsArea_LightReference, TransformNode>(context, node, this.name, async (extensionContext, extension) => { | ||
| this._loader._allMaterialsDirtyRequired = true; | ||
|
|
||
| return await this._loader.loadNodeAsync(context, node, (babylonMesh) => { | ||
| let babylonLight: Light; | ||
|
|
||
| const light = ArrayItem.Get(extensionContext, this._lights, extension.light); | ||
| const name = light.name || babylonMesh.name; | ||
|
|
||
| this._loader.babylonScene._blockEntityCollection = !!this._loader._assetContainer; | ||
|
|
||
| switch (light.shape) { | ||
| case EXTLightsArea_LightShape.RECT: { | ||
| const width = light.width !== undefined ? light.width : 1.0; | ||
| const height = light.height !== undefined ? light.height : 1.0; | ||
| const babylonRectAreaLight = new RectAreaLight(name, Vector3.Zero(), width, height, this._loader.babylonScene); | ||
| babylonLight = babylonRectAreaLight; | ||
| break; | ||
| } | ||
| case EXTLightsArea_LightShape.DISK: { | ||
| // For disk lights, we'll use RectAreaLight with equal width and height to approximate a square area | ||
| // In the future, this could be extended to support actual disk area lights | ||
| const radius = light.radius !== undefined ? light.radius : 0.5; | ||
| const size = radius * 2; // Convert radius to square size | ||
| const babylonRectAreaLight = new RectAreaLight(name, Vector3.Zero(), size, size, this._loader.babylonScene); | ||
| babylonLight = babylonRectAreaLight; | ||
| break; | ||
| } | ||
| default: { | ||
| this._loader.babylonScene._blockEntityCollection = false; | ||
| throw new Error(`${extensionContext}: Invalid area light shape (${light.shape})`); | ||
| } | ||
| } | ||
|
|
||
| babylonLight._parentContainer = this._loader._assetContainer; | ||
| this._loader.babylonScene._blockEntityCollection = false; | ||
| light._babylonLight = babylonLight; | ||
|
|
||
| babylonLight.falloffType = Light.FALLOFF_GLTF; | ||
| babylonLight.diffuse = light.color ? Color3.FromArray(light.color) : Color3.White(); | ||
| babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity; | ||
|
|
||
| // glTF EXT_lights_area specifies lights face down -Z, but Babylon.js area lights face down +Z | ||
| // Create a parent transform node with 180-degree rotation around Y axis to flip the direction | ||
| const lightParentNode = new BabylonTransformNode(`${name}_orientation`, this._loader.babylonScene); | ||
| lightParentNode.rotationQuaternion = Quaternion.RotationAxis(Vector3.Up(), Math.PI); | ||
| lightParentNode.parent = babylonMesh; | ||
| babylonLight.parent = lightParentNode; | ||
|
|
||
| this._loader._babylonLights.push(babylonLight); | ||
|
|
||
| GLTFLoader.AddPointerMetadata(babylonLight, extensionContext); | ||
|
|
||
| assign(babylonMesh); | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| unregisterGLTFExtension(NAME); | ||
| registerGLTFExtension(NAME, true, (loader) => new EXT_lights_area(loader)); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely should be marked experimental as the spec hasn't been merged yet. The link also doesn't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I'll fix this and add a test when I update this importer to match the new spec.