import { clamp01, Vector3, InterpolatorScalar } from '@zephyr3d/base';

/**
 * Converts one VRM SpringBone setting block into a JointDynamics controller
 * config fragment.
 *
 * Parameter mapping:
 * - gravityPower + gravityDir -> gravity vector
 * - stiffness / stiffnessForce -> curves.hardness
 * - dragForce -> curves.resistance = 1 - dragForce
 * - hitRadius -> curves.pointRadius
 *
 * The returned value is intended for JointDynamicsSystem.controllerConfig.
 *
 * @public
 */ function mapVrmSpringBoneToJointDynamicsConfig(params, options = {}) {
    const stiffnessScale = options.stiffnessScale ?? 0.01;
    const gravityScale = options.gravityScale ?? 1;
    const stiffness = Math.max(0, params.stiffness ?? 0);
    const dragForce = clamp01(params.dragForce ?? 0);
    const gravityPower = Math.max(0, params.gravityPower ?? 0);
    const gravityDir = normalizeGravityDir(params.gravityDir);
    const hitRadius = Math.max(0, params.hitRadius ?? options.defaultHitRadius ?? 0.05);
    return {
        gravity: Vector3.scale(gravityDir, gravityPower * gravityScale),
        curves: {
            hardness: InterpolatorScalar.constant(clamp01(stiffness * stiffnessScale)),
            resistance: InterpolatorScalar.constant(1 - dragForce),
            pointRadius: InterpolatorScalar.constant(hitRadius)
        },
        constraintOptions: {
            structuralVertical: options.structuralVertical ?? true,
            bendingVertical: options.bendingVertical ?? true
        }
    };
}
function normalizeGravityDir(dir) {
    const v = dir instanceof Vector3 ? dir.clone() : dir ? new Vector3(dir[0] ?? 0, dir[1] ?? 0, dir[2] ?? 0) : new Vector3(0, -1, 0);
    if (v.magnitudeSq <= 0) {
        return new Vector3(0, -1, 0);
    }
    return Vector3.normalize(v);
}

export { mapVrmSpringBoneToJointDynamicsConfig };
//# sourceMappingURL=vrm_spring_bone.js.map
