Skip to content

Commit c59a7c8

Browse files
mvaligurskyMartin Valigurskywilleastcott
authored
Updates to Plane’s class constructor, to take a distance instead of a point (playcanvas#5196)
* Updates to Plane’s class constructor, to take a distance instead of a point * Update src/core/shape/plane.js Co-authored-by: Will Eastcott <will@playcanvas.com> * removed pc. * the --------- Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com> Co-authored-by: Will Eastcott <will@playcanvas.com>
1 parent 550aead commit c59a7c8

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed

scripts/utils/planar-renderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ PlanarRenderer.prototype.frameUpdate = function () {
128128
if (planarCamera.enabled) {
129129

130130
// update reflection camera orientation by mirroring the scene camera by the plane
131-
this.plane.set(this.planeNormal, this.planePoint);
132-
this.reflectionMatrix.setReflection(this.plane);
131+
this.plane.setFromPointNormal(this.planePoint, this.planeNormal);
132+
this.reflectionMatrix.setReflection(this.plane.normal, this.plane.distance);
133133

134134
var pos = this.sceneCameraEntity.getPosition();
135135
var reflectedPos = this.reflectionMatrix.transformPoint(pos);

src/core/math/mat4.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -782,16 +782,15 @@ class Mat4 {
782782
* Sets the matrix to a reflection matrix, which can be used as a mirror transformation by the
783783
* plane.
784784
*
785-
* @param {import('./plane.js').Plane} plane - The plane to reflect by.
785+
* @param {Vec3} normal - The normal of the plane to reflect by.
786+
* @param {number} distance - The distance of plane to reflect by.
786787
* @returns {Mat4} Self for chaining.
787788
*/
788-
setReflection(plane) {
789+
setReflection(normal, distance) {
789790

790-
const normal = plane.normal;
791791
const a = normal.x;
792792
const b = normal.y;
793793
const c = normal.z;
794-
const d = plane.distance;
795794
const data = this.data;
796795

797796
data[0] = 1.0 - 2 * a * a;
@@ -806,9 +805,9 @@ class Mat4 {
806805
data[9] = -2 * b * c;
807806
data[10] = 1.0 - 2 * c * c;
808807
data[11] = 0;
809-
data[12] = -2 * a * d;
810-
data[13] = -2 * b * d;
811-
data[14] = -2 * c * d;
808+
data[12] = -2 * a * distance;
809+
data[13] = -2 * b * distance;
810+
data[14] = -2 * c * distance;
812811
data[15] = 1;
813812

814813
return this;

src/core/shape/plane.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Plane {
1414
normal = new Vec3();
1515

1616
/**
17-
* The distance from the plan to the origin, along its normal.
17+
* The distance from the plane to the origin, along its normal.
1818
*
1919
* @readonly
2020
* @type {number}
@@ -25,22 +25,23 @@ class Plane {
2525
* Create a new Plane instance.
2626
*
2727
* @param {Vec3} [normal] - Normal of the plane. The constructor copies this parameter. Defaults
28-
* to {@link pc.Vec3#UP}.
29-
* @param {Vec3} [point] - Point position on the plane. The constructor copies this parameter.
30-
* Defaults to {@link pc.Vec3#ZERO}.
28+
* to {@link Vec3#UP}.
29+
* @param {Vec3} [distance] - The distance from the plane to the origin, along its normal.
30+
* Defaults to 0.
3131
*/
32-
constructor(normal = Vec3.UP, point = Vec3.ZERO) {
33-
this.set(normal, point);
32+
constructor(normal = Vec3.UP, distance = 0) {
33+
this.normal.copy(normal);
34+
this.distance = distance;
3435
}
3536

3637
/**
37-
* Sets the plane to use a specified normal and pass through a specified point.
38+
* Sets the plane based on a specified normal and a point on the plane.
3839
*
40+
* @param {Vec3} point - The point on the plane.
3941
* @param {Vec3} normal - The normal of the plane.
40-
* @param {Vec3} point - The point on a plane.
4142
* @returns {Plane} Self for chaining.
4243
*/
43-
set(normal, point) {
44+
setFromPointNormal(point, normal) {
4445
this.normal.copy(normal);
4546
this.distance = -this.normal.dot(point);
4647
return this;

src/framework/components/element/element-drag-helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class ElementDragHelper extends EventHandler {
161161
}
162162

163163
_normal.copy(this._element.entity.forward).mulScalar(-1);
164-
_plane.set(_normal, this._element.entity.getPosition());
164+
_plane.setFromPointNormal(this._element.entity.getPosition(), _normal);
165165

166166
if (_plane.intersectsRay(_ray, _point)) {
167167
_entityRotation.copy(this._element.entity.getRotation()).invert().transformVector(_point, _point);

test/core/shape/plane.test.mjs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,30 @@ describe('Plane', function () {
1616
});
1717

1818
it('supports arguments', function () {
19-
const p = new Plane(Vec3.UP, new Vec3(1, 2, 3));
19+
const p = new Plane(Vec3.UP, 5);
2020
expect(p.normal.x).to.equal(0);
2121
expect(p.normal.y).to.equal(1);
2222
expect(p.normal.z).to.equal(0);
23-
expect(p.distance).to.equal(-2);
23+
expect(p.distance).to.equal(5);
2424
});
2525

2626
});
2727

28+
describe('#setFromPointNormal', function () {
29+
30+
const p = new Plane();
31+
p.setFromPointNormal(new Vec3(1, 2, 3), Vec3.UP);
32+
expect(p.normal.x).to.equal(0);
33+
expect(p.normal.y).to.equal(1);
34+
expect(p.normal.z).to.equal(0);
35+
expect(p.distance).to.equal(-2);
36+
37+
});
38+
2839
describe('#intersectsLine', function () {
2940

30-
const p = new Plane(Vec3.UP, new Vec3(0, 5, 0));
41+
const p = new Plane();
42+
p.setFromPointNormal(new Vec3(0, 5, 0), Vec3.UP);
3143
const intersection = new Vec3();
3244
const intersects = p.intersectsLine(new Vec3(1, 0, 3), new Vec3(1, 6, 3), intersection);
3345
expect(intersects).to.equal(true);

0 commit comments

Comments
 (0)