BabylonJS/Babylon.js · error · ReferenceError

Can not fract a color

Error message

Can not fract a color

What it means

Color3.fractToRef is an intentional stub that throws ReferenceError("Can not fract a color"). Extracting fractional parts of color components is unsupported in this API. Every call throws unconditionally.

Source

Thrown at packages/dev/core/src/Maths/math.color.pure.ts:291

     */
    public floorToRef(_result: IColor3Like): never {
        throw new ReferenceError("Can not floor a color");
    }

    /**
     * @internal
     * Do not use
     */
    public floor(): never {
        throw new ReferenceError("Can not floor a color");
    }

    /**
     * @internal
     * Do not use
     */
    public fractToRef(_result: IColor3Like): never {
        throw new ReferenceError("Can not fract a color");
    }

    /**
     * @internal
     * Do not use
     */
    public fract(): never {
        throw new ReferenceError("Can not fract a color");
    }

    /**
     * Determines equality between Color3 objects
     * @param otherColor defines the second operand
     * @returns true if the rgb values are equal to the given ones
     */
    public equals(otherColor: DeepImmutable<IColor3Like>): boolean {
        return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
    }

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Compute fractional parts manually: v - Math.floor(v) per channel (correct for negatives, unlike % 1).
  2. Use a Vector3 if fract semantics are required.
  3. Rethink the algorithm; fract on color channels is rarely the intended color operation.
  4. Guard shared math code paths that dispatch on operand type.

Example fix

// before
const out = {};
color.fractToRef(out);
// after
const out = {
  r: color.r - Math.floor(color.r),
  g: color.g - Math.floor(color.g),
  b: color.b - Math.floor(color.b)
};
Defensive patterns

Strategy: fallback

Validate before calling

if (color instanceof Color3) {
  out.r = color.r - Math.floor(color.r);
  out.g = color.g - Math.floor(color.g);
  out.b = color.b - Math.floor(color.b);
} else {
  color.fractToRef(out);
}

Type guard

function isColor3Like(c: unknown): c is IColor3Like {
  return !!c && typeof c === 'object' && 'r' in c && 'g' in c && 'b' in c && !('a' in c);
}

Try / catch

try {
  color.fractToRef(out);
} catch (e) {
  if (e instanceof ReferenceError && e.message === 'Can not fract a color') {
    out.r = color.r - Math.floor(color.r);
    out.g = color.g - Math.floor(color.g);
    out.b = color.b - Math.floor(color.b);
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling color3.fractToRef(result) directly, or generic fract-based code (e.g. tiling/wrapping logic) applied to an IColor3Like object.

Common situations: Shader-style fract() usage translated to CPU color code; wrapping/repeating color patterns; shared math utilities hitting the color branch.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/84e38ce8696d7914. Report an issue: GitHub.