mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
101.89k stars 35.32k forks source link

NURBSCurve.toJSON doesn't serialize points data #29478

Open canxerian opened 16 hours ago

canxerian commented 16 hours ago

Description

The JSON object returned from NURBSCurve.toJSON() doesn't contain data relating points, knots or control points, but instead contains:

{
    "metadata": {
        "version": 4.6,
        "type": "Curve",
        "generator": "Curve.toJSON"
    },
    "arcLengthDivisions": 200,
    "type": "Curve"
}

Reproduction steps

  1. Create an instance of NURBSCurve (sample code below),
  2. console.log nurbsCurveInstance.toJSON()
  3. Notice there is no data relating to the instance, e.g points, knots and control points)

Code

const nurbsControlPoints = [];
const nurbsKnots = [];
const nurbsDegree = 3;

for (let i = 0; i <= nurbsDegree; i++) {
  nurbsKnots.push(0);
}

for (let i = 0, j = 20; i < j; i++) {
  nurbsControlPoints.push(
    new THREE.Vector4(
      Math.random(),
      Math.random(),
      Math.random(),
      1 // weight of control point: higher means stronger attraction
    )
  );

  const knot = (i + 1) / (j - nurbsDegree);
  nurbsKnots.push(THREE.MathUtils.clamp(knot, 0, 1));
}

const nurbsCurve = new NURBSCurve(nurbsDegree, nurbsKnots, nurbsControlPoints);

console.log(nurbsCurve.toJSON());

Live example

https://codesandbox.io/p/sandbox/threejs-nurbscurve-tojson-bug-s298gc

Screenshots

No response

Version

168

Device

Desktop, Mobile

Browser

Chrome, Firefox, Safari, Edge

OS

MacOS

Mugen87 commented 16 hours ago

NURBSCurve is part of the addons and there is no JSON support for this class right now. Only curves classes in the core support that.

Even with a proper NURBSCurve.toJSON() implementation you also need the fromJSON() counterpart to make a deserialization work.

Mugen87 commented 5 hours ago

The next problem is that objects deserialized with ObjectLoader can't restore instances of NURBSCurve because the type is unknown for the core. So we can only add partial serialization/deserialization support right now.