Skip to content

Commit 6f14cbe

Browse files
committed
feat: first working version of exportable wall corners
1 parent f5551d9 commit 6f14cbe

File tree

3 files changed

+92
-179
lines changed

3 files changed

+92
-179
lines changed

packages/clay/src/elements/Walls/SimpleWall/example.ts

Lines changed: 26 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -49,101 +49,41 @@ const simpleWallType = new CLAY.SimpleWallType(model);
4949
const wall1 = simpleWallType.addInstance();
5050
world.scene.three.add(...wall1.meshes);
5151
wall1.startPoint = new THREE.Vector2(0, 0);
52-
wall1.endPoint = new THREE.Vector2(0, 1);
52+
wall1.endPoint = new THREE.Vector2(1, 0);
5353
wall1.update(true);
5454
wall1.meshes[0].setColorAt(0, new THREE.Color(1, 0, 0));
55-
//
56-
// const wall2 = simpleWallType.addInstance();
57-
// world.scene.three.add(...wall2.meshes);
58-
// wall2.startPoint = new THREE.Vector2(0, 0);
59-
// wall2.endPoint = new THREE.Vector2(0, 1);
60-
// wall2.update(true);
55+
56+
const wall2 = simpleWallType.addInstance();
57+
world.scene.three.add(...wall2.meshes);
58+
wall2.startPoint = new THREE.Vector2(0, 0);
59+
wall2.endPoint = new THREE.Vector2(0, 1);
60+
wall2.update(true);
6161

6262
site.children.add(wall1.attributes.expressID);
63-
// site.children.add(wall2.attributes.expressID);
63+
site.children.add(wall2.attributes.expressID);
64+
65+
simpleWallType.addCorner({
66+
wall1,
67+
wall2,
68+
to: "exterior",
69+
cut: "exterior",
70+
cutDirection: "interior",
71+
priority: "start",
72+
});
6473

65-
// simpleWallType.addCorner({
66-
// wall1,
67-
// wall2,
68-
// to: "interior",
69-
// cut: "interior",
70-
// cutDirection: "interior",
71-
// priority: "start",
72-
// });
74+
simpleWallType.addCorner({
75+
wall1: wall2,
76+
wall2: wall1,
77+
to: "exterior",
78+
cut: "exterior",
79+
cutDirection: "exterior",
80+
priority: "start",
81+
});
7382

7483
simpleWallType.updateCorners();
7584

7685
world.camera.controls.fitToSphere(wall1.meshes[0], false);
7786

78-
// const simpleOpeningType = new CLAY.SimpleOpeningType(model);
79-
// const opening = simpleOpeningType.addInstance();
80-
// // world.scene.three.add(...opening.meshes);
81-
// console.log(simpleOpeningType);
82-
// opening.transformation.position.x += 1;
83-
//
84-
// await wall1.addSubtraction(opening, true);
85-
// wall1.update(true);
86-
87-
const test = new THREE.Mesh(
88-
new THREE.PlaneGeometry(),
89-
new THREE.MeshLambertMaterial({
90-
color: "blue",
91-
transparent: true,
92-
opacity: 0.3,
93-
side: 2,
94-
}),
95-
);
96-
97-
const wallAxis = new THREE.AxesHelper();
98-
wallAxis.material.depthTest = false;
99-
wallAxis.material.transparent = true;
100-
wall1.transformation.add(wallAxis);
101-
world.scene.three.add(wall1.transformation);
102-
103-
world.scene.three.add(test);
104-
test.position.set(0.5, 0.5, 0.5);
105-
test.lookAt(0, 0, 0);
106-
test.updateMatrix();
107-
108-
const halfSpace = new CLAY.HalfSpace(model);
109-
wall1.body.addSubtraction(halfSpace);
110-
111-
function updatePlane() {
112-
const delta = 0.000000001;
113-
const vector = new THREE.Vector3(0, 0, 1);
114-
115-
const planeRotation = new THREE.Matrix4();
116-
planeRotation.extractRotation(test.matrix);
117-
vector.applyMatrix4(planeRotation);
118-
119-
wall1.transformation.updateMatrix();
120-
const rotation = new THREE.Matrix4();
121-
const inverseWall = wall1.transformation.matrix.clone();
122-
inverseWall.invert();
123-
rotation.extractRotation(inverseWall);
124-
vector.applyMatrix4(rotation);
125-
126-
const position = test.position.clone();
127-
position.applyMatrix4(inverseWall);
128-
129-
halfSpace.transformation.position.copy(position);
130-
halfSpace.direction.copy(vector).normalize();
131-
132-
halfSpace.update();
133-
wall1.update(true);
134-
}
135-
136-
updatePlane();
137-
138-
function animate() {
139-
test.rotation.x += Math.PI / 180;
140-
test.updateMatrix();
141-
updatePlane();
142-
requestAnimationFrame(animate);
143-
}
144-
145-
animate();
146-
14787
// Stats
14888

14989
const stats = new Stats();
@@ -171,7 +111,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
171111
) => {
172112
wall1.startPoint.x = event.target.value;
173113
wall1.update(true);
174-
updatePlane();
175114
simpleWallType.updateCorners();
176115
177116
// simpleWallType.updateCorners();
@@ -182,7 +121,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
182121
) => {
183122
wall1.startPoint.y = event.target.value;
184123
wall1.update(true);
185-
updatePlane();
186124
simpleWallType.updateCorners();
187125
188126
console.log("hey");
@@ -199,7 +137,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
199137
) => {
200138
wall1.endPoint.x = event.target.value;
201139
wall1.update(true);
202-
updatePlane();
203140
simpleWallType.updateCorners();
204141
205142
// simpleWallType.updateCorners();
@@ -210,7 +147,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
210147
) => {
211148
wall1.endPoint.y = event.target.value;
212149
wall1.update(true);
213-
updatePlane();
214150
simpleWallType.updateCorners();
215151
216152
// simpleWallType.updateCorners();
@@ -224,7 +160,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
224160
) => {
225161
wall1.transformation.position.y = event.target.value;
226162
wall1.update(true);
227-
updatePlane();
228163
simpleWallType.updateCorners();
229164
}}"></bim-number-input>
230165
@@ -233,7 +168,6 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
233168
) => {
234169
wall1.offset = event.target.value;
235170
wall1.update(true);
236-
updatePlane();
237171
simpleWallType.updateCorners();
238172
239173
// simpleWallType.updateCorners();
@@ -244,15 +178,14 @@ const panel = BUI.Component.create<BUI.PanelSection>(() => {
244178
) => {
245179
simpleWallType.width = event.target.value;
246180
simpleWallType.update(true);
247-
// simpleWallType.updateCorners();
181+
simpleWallType.updateCorners();
248182
}}"></bim-number-input>
249183
250184
<bim-number-input slider step="0.05" label="Height" value="${wall1.height}" @change="${(
251185
event: any,
252186
) => {
253187
wall1.height = event.target.value;
254188
wall1.update(true);
255-
updatePlane();
256189
simpleWallType.updateCorners();
257190
258191
// simpleWallType.updateCorners();

packages/clay/src/elements/Walls/SimpleWall/src/simple-wall-cornerer.ts

Lines changed: 60 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ interface WallCorner extends WallCornerConfig {
2222
export class SimpleWallCornerer {
2323
list = new Map<number, Map<number, WallCorner>>();
2424

25+
private _temp = new THREE.Object3D();
26+
2527
add(config: WallCornerConfig) {
2628
const id1 = config.wall1.attributes.expressID;
2729
const id2 = config.wall2.attributes.expressID;
@@ -58,14 +60,16 @@ export class SimpleWallCornerer {
5860
// B) Each point of the wall are on one side of this wall
5961
// In that case, keep the point specified in priority
6062

63+
wall1.transformation.updateMatrix();
64+
6165
const dir1 = wall1.direction;
6266
const dir2 = wall2.direction;
6367
if (dir1.equals(dir2)) {
6468
// Same direction, so walls can't intersect
6569
return;
6670
}
6771

68-
const { plane } = wall1.getPlane(to);
72+
const { plane, p1 } = wall1.getPlane(to);
6973
if (plane === null) {
7074
// Malformed wall (e.g. zero length)
7175
return;
@@ -77,92 +81,68 @@ export class SimpleWallCornerer {
7781
const extendStart = priority === "start";
7882

7983
const pointToExtend = extendStart ? start : end;
80-
if (plane.distanceToPoint(pointToExtend) === 0) {
84+
if (plane.distanceToPoint(pointToExtend) !== 0) {
8185
// Point is already aligned with wall
82-
return;
83-
}
86+
const origin = extendStart ? end : start;
87+
const direction = extendStart ? start : end;
88+
direction.sub(origin);
8489

85-
const origin = extendStart ? end : start;
86-
const direction = extendStart ? start : end;
87-
direction.sub(origin);
90+
const ray = new THREE.Ray(origin, direction);
8891

89-
const ray = new THREE.Ray(origin, direction);
92+
const intersection = ray.intersectPlane(plane, new THREE.Vector3());
9093

91-
const intersection = ray.intersectPlane(plane, new THREE.Vector3());
94+
if (intersection === null) {
95+
return;
96+
}
9297

93-
if (intersection === null) {
94-
return;
98+
const offsetDist = offset === "auto" ? wall2.type.width : offset;
99+
const factor = extendStart ? -1 : 1;
100+
const offsetVec = dir2.multiplyScalar(offsetDist * factor);
101+
intersection.add(offsetVec);
102+
103+
const extended = extendStart ? wall2.startPoint : wall2.endPoint;
104+
extended.x = intersection.x;
105+
extended.y = intersection.z;
106+
107+
wall2.update();
95108
}
96109

97-
const offsetDist = offset === "auto" ? wall2.type.width : offset;
98-
const factor = extendStart ? -1 : 1;
99-
const offsetVec = dir2.multiplyScalar(offsetDist * factor);
100-
intersection.add(offsetVec);
101-
102-
const extended = extendStart ? wall2.startPoint : wall2.endPoint;
103-
extended.x = intersection.x;
104-
extended.y = intersection.z;
105-
106-
// if (corner.cut && corner.cutDirection) {
107-
// if (!corner.halfSpace) {
108-
// const halfSpace = new HalfSpace(wall1.model);
109-
// corner.halfSpace = halfSpace;
110-
// wall2.body.addSubtraction(halfSpace);
111-
// }
112-
//
113-
// const halfSpace = corner.halfSpace as HalfSpace;
114-
//
115-
// const temp = new THREE.Mesh(
116-
// new THREE.PlaneGeometry(10, 10, 10),
117-
// new THREE.MeshLambertMaterial({
118-
// color: "blue",
119-
// }),
120-
// );
121-
//
122-
// const temp2 = new THREE.Object3D();
123-
// temp2.position.copy(p1);
124-
// const p4 = p1.clone().add(plane.normal);
125-
// temp2.lookAt(p4);
126-
//
127-
// temp2.applyMatrix4(new THREE.Matrix4().makeRotationX(-Math.PI / 2));
128-
// temp2.applyMatrix4(new THREE.Matrix4().makeRotationY(Math.PI / 2));
129-
// temp2.applyMatrix4(new THREE.Matrix4().makeRotationX(Math.PI / 2));
130-
//
131-
// temp.applyMatrix4(temp2.matrix);
132-
//
133-
// // temp.updateMatrix();
134-
//
135-
// const temp3 = new THREE.Object3D();
136-
// const transform = wall2.transformation.matrix.clone();
137-
// transform.invert();
138-
//
139-
// temp3.applyMatrix4(transform);
140-
// temp3.applyMatrix4(new THREE.Matrix4().makeRotationZ(Math.PI / 2));
141-
// temp.applyMatrix4(temp3.matrix);
142-
//
143-
// // const temp4 = new THREE.Object3D();
144-
// // temp4.position.copy(p1);
145-
// // temp4.applyMatrix4(transform);
146-
//
147-
// // const obj = MathUtils.getTempObject3DToDisplayIfcCoords();
148-
// // obj.add(temp);
149-
// // wall1.meshes[0].parent.add(obj);
150-
//
151-
// // halfSpace.position.copy(temp.position);
152-
// // halfSpace.rotation.copy(temp.rotation);
153-
//
154-
// // const transform = wall2.getTransform();
155-
// // transform.invert();
156-
// // halfSpace.applyTransform(transform);
157-
//
158-
// // halfSpace.rotation.y = Math.PI / 2;
159-
// // halfSpace.position.x = 0.25;
160-
//
161-
// halfSpace.transformation.rotation.copy(temp.rotation);
162-
// halfSpace.transformation.position.copy(temp.position);
163-
//
164-
// halfSpace.update();
165-
// }
110+
if (corner.cut && corner.cutDirection) {
111+
if (!corner.halfSpace) {
112+
const halfSpace = new HalfSpace(wall1.model);
113+
corner.halfSpace = halfSpace;
114+
wall2.body.addSubtraction(halfSpace);
115+
}
116+
117+
const halfSpace = corner.halfSpace as HalfSpace;
118+
119+
const rotation = new THREE.Vector3(0, 0, 1);
120+
121+
this._temp.position.copy(p1);
122+
const factor = corner.cutDirection === "interior" ? -1 : 1;
123+
const minusNormal = plane.normal.clone().multiplyScalar(factor);
124+
this._temp.lookAt(p1.clone().add(minusNormal));
125+
this._temp.updateMatrix();
126+
127+
const planeRotation = new THREE.Matrix4();
128+
planeRotation.extractRotation(this._temp.matrix);
129+
rotation.applyMatrix4(planeRotation);
130+
131+
wall2.transformation.updateMatrix();
132+
const wallRotation = new THREE.Matrix4();
133+
const inverseWall = wall2.transformation.matrix.clone();
134+
inverseWall.invert();
135+
wallRotation.extractRotation(inverseWall);
136+
rotation.applyMatrix4(wallRotation);
137+
138+
const position = this._temp.position.clone();
139+
position.applyMatrix4(inverseWall);
140+
141+
halfSpace.transformation.position.copy(position);
142+
halfSpace.direction.copy(rotation).normalize();
143+
144+
halfSpace.update();
145+
}
166146

167147
wall2.update(true);
168148
}

packages/clay/src/elements/Walls/SimpleWall/src/simple-wall.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class SimpleWall extends ClayElement {
4343

4444
get normal() {
4545
const direction = this.direction;
46-
const up = new THREE.Vector3(0, 0, 1);
46+
const up = new THREE.Vector3(0, 1, 0);
4747
return direction.cross(up);
4848
}
4949

@@ -155,12 +155,12 @@ export class SimpleWall extends ClayElement {
155155
return { p1, p2, p3, plane };
156156
}
157157

158-
const offsetCorrection = normal.clone();
159-
offsetCorrection.multiplyScalar(-this.offset);
158+
// const offsetCorrection = normal.clone();
159+
// offsetCorrection.multiplyScalar(-this.offset);
160160

161-
p1.add(offsetCorrection);
162-
p2.add(offsetCorrection);
163-
p3.add(offsetCorrection);
161+
// p1.add(offsetCorrection);
162+
// p2.add(offsetCorrection);
163+
// p3.add(offsetCorrection);
164164

165165
if (type !== "center") {
166166
const offset = normal.clone();

0 commit comments

Comments
 (0)