Skip to content

Serialización

Oroya Animate incluye un sistema de serialización JSON que permite guardar y cargar escenas completas. Es la base para editores visuales, colaboración y versionado de escenas.


Tabla de contenidos


Serializar una escena

La función serialize convierte todo el scene graph a un string JSON formateado:

import { Scene, Node, createBox, Material, serialize } from '@joroya/core';

const scene = new Scene();

const cube = new Node('hero-cube');
cube.addComponent(createBox(2, 2, 2));
cube.addComponent(new Material({ color: { r: 1, g: 0.5, b: 0 } }));
cube.transform.position = { x: 3, y: 0, z: -1 };
scene.add(cube);

const json = serialize(scene);
console.log(json);

Flujo de serialización

flowchart LR
    S["Scene"] -->|"serialize()"| SR["serializeNode(root)"]
    SR -->|"recursivo"| SN["Para cada nodo:\nid, name, components, children"]
    SN -->|"spread"| SC["Cada component:\n{ type, ...data }"]
    SC --> JSON["JSON.stringify()\ncon indent 2"]
    JSON --> STR["string"]

Deserializar una escena

La función deserialize reconstruye una Scene funcional desde un string JSON:

import { deserialize } from '@joroya/core';

const restoredScene = deserialize(json);

const found = restoredScene.findNodeByName('hero-cube');
console.log(found?.transform.position); // { x: 3, y: 0, z: -1 }

La escena restaurada es completamente funcional — puede montarse en cualquier renderer:

import { ThreeRenderer } from '@joroya/renderer-three';

const renderer = new ThreeRenderer({ canvas, width, height });
renderer.mount(restoredScene);
renderer.render();

Flujo de deserialización

flowchart LR
    STR["string JSON"] -->|"JSON.parse()"| OBJ["SerializableScene"]
    OBJ -->|"deserializeNode()"| RN["Reconstruir nodos\nrecursivamente"]
    RN -->|"switch(type)"| COMP["Recrear componentes:\nTransform, Geometry, Material,\nCamera, Animation"]
    COMP --> SCENE["new Scene()"]
    RN -->|"children"| SCENE

Formato del JSON

Estructura general

graph TD
    SS["SerializableScene"] -->|"root"| SN["SerializableNode"]
    SN -->|"id"| ID["string (UUID)"]
    SN -->|"name"| NAME["string"]
    SN -->|"components"| COMPS["SerializableComponent[]"]
    SN -->|"children"| CHILDREN["SerializableNode[]"]
    CHILDREN -->|"recursivo"| SN
    COMPS --> SC["{ type: ComponentType, ...data }"]

Ejemplo completo de output

{
  "root": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "name": "root",
    "components": [
      {
        "type": "Transform",
        "position": { "x": 0, "y": 0, "z": 0 },
        "rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
        "scale": { "x": 1, "y": 1, "z": 1 },
        "localMatrix": [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1],
        "worldMatrix": [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1],
        "isDirty": true
      }
    ],
    "children": [
      {
        "id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
        "name": "hero-cube",
        "components": [
          {
            "type": "Transform",
            "position": { "x": 3, "y": 0, "z": -1 },
            "rotation": { "x": 0, "y": 0, "z": 0, "w": 1 },
            "scale": { "x": 1, "y": 1, "z": 1 },
            "localMatrix": [1,0,0,0, 0,1,0,0, 0,0,1,0, 3,0,-1,1],
            "worldMatrix": [1,0,0,0, 0,1,0,0, 0,0,1,0, 3,0,-1,1],
            "isDirty": false
          },
          {
            "type": "Geometry",
            "definition": {
              "type": "Box",
              "width": 2,
              "height": 2,
              "depth": 2
            }
          },
          {
            "type": "Material",
            "definition": {
              "color": { "r": 1, "g": 0.5, "b": 0 }
            }
          }
        ],
        "children": []
      }
    ]
  }
}

Interfaces internas

InterfaceCamposDescripción
SerializableSceneroot: SerializableNodeContenedor de nivel superior
SerializableNodeid, name, cssClass?, cssId?, components[], children[]Representación plana de un nodo
SerializableComponenttype: ComponentType, + ...dataCada componente con su tipo y datos

Componentes soportados

En serialización (serialize)

Todos los componentes se serializan usando spread ({ ...component }):

ComponenteDatos serializados
Transformposition, rotation, scale, localMatrix, worldMatrix, isDirty
Geometrydefinition completo (tipo + parámetros de geometría)
Materialdefinition completo (color, opacity, fill, stroke, strokeWidth, fillGradient, strokeGradient, filter, clipPath, mask)
Cameradefinition completo (Perspective: type, fov, aspect, near, far; Orthographic: type, left, right, top, bottom, near, far)
Animationanimations[] — array de SvgAnimationDef (animate / animateTransform)

En deserialización (deserialize)

Componente¿Se restaura?Método
TransformObject.assign(new Transform(), data)
Geometrynew Geometry(data.definition)
Materialnew Material(data.definition)
Cameranew Camera(data.definition)
Animationnew Animation(data.animations)

Nota: Todos los componentes se serializan y deserializan correctamente, incluyendo Camera y Animation.

Preservación de datos

Dato¿Se preserva?
UUID del nodo✅ Exacto
Nombre del nodo
Jerarquía padre-hijo
Posición
Rotación (quaternion)
Escala
Matrices (local + world)
Tipo de geometría
Parámetros de geometría
Color del material
Opacidad
Fill/Stroke (SVG)
Gradientes (fill/stroke)
Filter / ClipPath / Mask
Cámara (Perspective + Orthographic)
Animaciones SVG
cssClass / cssId

Casos de uso

Persistencia en localStorage

function saveScene(scene: Scene): void {
  const json = serialize(scene);
  localStorage.setItem('oroya-scene', json);
}

function loadScene(): Scene | null {
  const json = localStorage.getItem('oroya-scene');
  if (!json) return null;
  return deserialize(json);
}

Exportar como archivo descargable

function downloadScene(scene: Scene, filename: string): void {
  const json = serialize(scene);
  const blob = new Blob([json], { type: 'application/json' });
  const url = URL.createObjectURL(blob);

  const a = document.createElement('a');
  a.href = url;
  a.download = filename;
  a.click();

  URL.revokeObjectURL(url);
}

downloadScene(scene, 'my-scene.json');

Importar desde archivo

async function importScene(file: File): Promise<Scene> {
  const text = await file.text();
  return deserialize(text);
}

// Con input[type=file]
input.addEventListener('change', async (e) => {
  const file = (e.target as HTMLInputElement).files?.[0];
  if (file) {
    const scene = await importScene(file);
    renderer.mount(scene);
  }
});

Server-side rendering

// Recibir la escena serializada, renderizar a SVG en el servidor
import { deserialize } from '@joroya/core';
import { renderToSVG } from '@joroya/renderer-svg';

function handleRequest(jsonBody: string): string {
  const scene = deserialize(jsonBody);
  return renderToSVG(scene, { width: 800, height: 600 });
}

Versionado en Git

Guardar escenas como .json permite:

scenes/
├── level-01.json    → Versionado con git
├── level-02.json
└── hub-world.json

Los diffs de Git muestran exactamente qué cambió:

 "name": "hero-cube",
 "components": [
   {
     "type": "Transform",
-    "position": { "x": 3, "y": 0, "z": -1 },
+    "position": { "x": 5, "y": 2, "z": -1 },

Limitaciones

LimitaciónImpactoWorkaround
UUIDs se preservanDos escenas del mismo JSON tendrán nodos con IDs idénticosRegenerar IDs post-deserialización si se necesitan escenas independientes
Camera no se deserializaLa escena pierde su cámara al restaurarResuelto — Camera y Animation se deserializan correctamente
Componentes desconocidosSi se agrega un tipo custom y no se registra en el switch, se ignoraExtender deserializeNode() con nuevos tipos
Formato de texto JSONArchivos grandes para escenas con muchos nodosFuturo: serialización binaria (MessagePack)
Sin referencia a component.nodeLa referencia circular component → node se pierdeSe reconstruye automáticamente por addComponent()