Skip to content

Fix angle getters that lost the sign of negative angles - #756

Merged
vanruesc merged 2 commits into
pmndrs:devfrom
SwastikTripathi:fix/angle-getters-sign
Nov 22, 2026
Merged

vanruesc merged 2 commits into
pmndrs:devfrom
SwastikTripathi:fix/angle-getters-sign

Conversation

@SwastikTripathi

Copy link
Copy Markdown

Related issue: none

Description

Four angle getters read the angle back with Math.acos of one stored component, so they return the absolute value and lose the sign:

const effect = new TiltShiftEffect();
effect.rotation = -0.5;
effect.rotation; // 0.5

The same happens for DotScreenEffect.angle, HueSaturationEffect.hue and TiltShiftBlurMaterial.rotation. The uniforms themselves are right, since the setters store both the cosine and the sine, so only reading the value back is wrong. It matters for code that reads the property to adjust it, e.g. effect.rotation -= 0.1 past zero, or UI controls bound to it.

The getters now use Math.atan2 with both stored components:

  • TiltShiftEffect.rotation, TiltShiftBlurMaterial.rotation: atan2(y, x) of the stored (cos, sin).
  • DotScreenEffect.angle: atan2(x, y) of the stored (sin, cos).
  • HueSaturationEffect.hue: the cosine is (3x - 1) / 2 as before, and the sine is (z - y) * sqrt(3) / 2, from the setter's y = (-sqrt(3) s - c + 1) / 3 and z = (sqrt(3) s - c + 1) / 3.

Values come back in (-PI, PI], so an angle outside that range is returned as its equivalent inside it, where acos returned [0, PI].

Tests: a round-trip test for each of the four (set -2.5, -0.5, 0, 0.5, 2.5 and read it back). All four fail before the change (set -2.5, got 2.5) and pass after. pnpm ava passes (106 tests) after pnpm build:js, and ESLint is clean on the changed files.

@vanruesc

Copy link
Copy Markdown
Member

Hey, thanks for contributing!

I'd be more in favor of doing it like this:

get rotation() {
return this._rotation;
}
set rotation(value: number) {
const rotation = this.in.uniforms.get("rotation")!.value as Vector2;
rotation.set(Math.sin(value), Math.cos(value));
this._rotation = value;
}

The original values should be saved in private backing data, i.e. _rotation, _angle, _hue, etc. to avoid the lossy calculations.

@SwastikTripathi

Copy link
Copy Markdown
Author

Done in 4f1e396: switched to the private field approach as in HalftoneEffect. Each setter now stores the raw value in _angle, _rotation, or _hue, and the getters return it directly. All 106 ava tests pass.

@vanruesc
vanruesc merged commit e28bc1e into pmndrs:dev Nov 22, 2026
1 check passed
@vanruesc

Copy link
Copy Markdown
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants