The Light.toJSON()
method is used to serialize a Light
object in Three.js to a JSON format that can be saved or transmitted over a network.
.toJSON()
The toJSON()
method returns a JSON representation of the Light
object.
The toJSON()
method is part of the Light
class in Three.js. It is used to generate a JSON object that represents the Light
object. This is useful when you want to save the state of a Light
object or transmit it over a network.
The JSON object generated by toJSON()
contains all the properties of the Light
object. These include the position, color, and intensity of the light, as well as any shadow-related properties.
// create a new spotlight
const spotlight = new THREE.SpotLight(0xffffff);
// set some properties
spotlight.position.set(0, 500, 1000);
spotlight.castShadow = true;
// create a JSON representation of the spotlight
const json = spotlight.toJSON();
// log the JSON to the console
console.log(json);
The output of this example would look something like this:
{
"metadata": {
"version": 4.5,
"type": "Light",
"generator": "Object3D.toJSON"
},
"object": {
"uuid": "C64FD647-22BB-4DEE-A077-DA00039517CB",
"type": "SpotLight",
"color": 16777215,
"intensity": 1,
"distance": 0,
"angle": 1.5707963267948966,
"penumbra": 0,
"decay": 1,
"castShadow": true,
"shadow": {
"camera": {
"uuid": "49068287-C3EF-4B35-88D8-8A4F810B8894",
"type": "PerspectiveCamera",
"matrix": [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 500, 1000, 1],
"fov": 50,
"aspect": 1,
"near": 1,
"far": 2000,
"focus": 1000
},
"bias": -0.0001,
"mapSize": {"x": 2048, "y": 2048},
"radius": 1,
"shadowBias": 0.05,
"shadowMap": {
"uuid": "BB428ED5-E805-4B50-AB01-F132D3A779F7",
"type": "WebGLRenderTarget",
"width": 2048,
"height": 2048,
"scissor": {
"x": 0,
"y": 0,
"z": 2048,
"w": 2048
},
"viewport": {
"x": 0,
"y": 0,
"z": 2048,
"w": 2048
},
"texture": {
"uuid": "BA55C6F3-8002-42B2-A481-EA7BB6E39A46",
"type": "DataTexture",
"encoding": 3000,
"name": "",
"offset": {"x": 0, "y": 0},
"repeat": {"x": 1, "y": 1},
"minFilter": 1006,
"magFilter": 1006,
"anisotropy": 1,
"wrapS": 1000,
"wrapT": 1000,
"file": {"name": ""}
}
}
},
"matrix": [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 500, 1000, 1]
}
}
This JSON object contains all the properties of the spotlight, including its position, color, and shadow settings. The type
property of the object
section indicates that this is a SpotLight
object. The shadow
property contains information about the light's shadow, including the size of the shadow map and the bias value.
Object3D.toJSON()
: The base class for all objects in Three.js, which also has a toJSON()
method for serializing objects to JSON.