when i try to serialize the scene in json doing scene.toJSON(), the console send me this error:Failed to execute ‘drawImage’ on 'CanvasRenderingContext2D.
function downloadJson(sceneJSON) {
var dataStr = "data:text/json;charset=utf-8," +
encodeURIComponent(JSON.stringify(sceneJSON));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", "world.json");
document.body.appendChild(downloadAnchorNode);
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
const sceneJSON = scene.toJSON();
downloadJson(sceneJSON)
this is how i call a function to download the scene in json
and this is the complete error:
Uncaught TypeError: Failed to execute ‘drawImage’ on ‘CanvasRenderingContext2D’: The provided value is not of type ‘(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)’
The downloadJson() function from your first post is not relevant for this issue. The actual problem is that your texture can’t be serialized for some reasons.
AFAICT, your code looks actually good. Any chances to share a live example that demonstrates the runtime error. I want to debug why your code fails at this line:
The error happens because you have THREE.Water in your scene. This object type internally uses a render target for rendering its reflections. Unfortunately, the engine is not able yet to handle render targets correctly in context of serialization/deserialization. And even when this is fixed, THREE.Water would need a custom toJSON() method.
As a workaround, you have to exclude THREE.Water from your scene when saving it to JSON.