BufferGeometry
Object3D
Raycaster
Camera
CubeCamera
PerspectiveCamera
OrthographicCamera
StereoCamera
Clock
Curve
CurvePath
Path
Shape
ShapePath
ArrowHelper
AxesHelper
BoxHelper
Box3Helper
CameraHelper
DirectionalLightHelper
GridHelper
PolarGridHelper
HemisphereLightHelper
PlaneHelper
PointLightHelper
SkeletonHelper
SpotLightHelper
Light
PointLight
RectAreaLight
SpotLight
DirectionalLight
HemisphereLight
LightShadow
PointLightShadow
AnimationLoader
AudioLoader
BufferGeometryLoader
CompressedTextureLoader
CubeTextureLoader
DataTextureLoader
FileLoader
ImageBitmapLoader
ImageLoader
Loader
LoaderUtils
MaterialLoader
ObjectLoader
TextureLoader
LoadingManager
Material
Box2
Box3
Color
Cylindrical
Euler
Frustum
Interpolant
Line3
MathUtils
Matrix3
Matrix4
Plane
Quaternion
AnimationAction
AnimationClip
AnimationMixer
AnimationObjectGroup
AnimationUtils
keyframeTrack
PropertyBinding
PropertyMixer
BooleanKeyframeTrack
QuaternionKeyframeTrack
StringKeyframeTrack
Audio
AudioAnalyser
AudioContext
AudioListener
PositionalAudio

Light.toJSON()

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.

Syntax

.toJSON()

Return Value

The toJSON() method returns a JSON representation of the Light object.

Description

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.

Example

// 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.

See Also

  • Object3D.toJSON(): The base class for all objects in Three.js, which also has a toJSON() method for serializing objects to JSON.