Skip to content

Scene exporting toJSON results in inconsistent values for Directional & Spot Lights #31141

@trapgar

Description

@trapgar

Description

If you have a scene that includes a directional light without a target, the ObjectLoader defaults to the target to a new Object3D(). This means that on export, the target will be serialized as a brand new uuid.

Then when the scene is deserialized with parseAsync etc., bindLightTargets will not be able to find the target (as the target pointed to a new Object3D, and so will default to another new Object3D.

This means that every export-to-json & import-from-json for the exact same scene w/o edits will continue to alter the resulting text.

Reproduction steps

  1. Go to https://threejs.org/editor/
  2. File > New > Pong
  3. Project > Publish
  4. File > New > Pong
  5. Project > Publish

Compare the app.json of the 2 exports - the last child in the pong app is a DirectionalLight; the "target": <uuid>" values will differ.

Code

src/loaders/ObjectLoader.js:1212

bindLightTargets(object) {
	object.traverse(function (child) {
		if (child.isDirectionalLight || child.isSpotLight) {
			const uuid = child.target;
			const target = object.getObjectByProperty('uuid', uuid);

			if (target !== undefined) {
				child.target = target;
			}
			else {
				child.target = new Object3D();
			}
		}
	});
}

if (target !== undefined) && child.target = new Object3D(); are the important parts

Live example

Screenshots

Image

Version

0.174.0

Device

No response

Browser

No response

OS

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions