Skip to content

Commit da83f03

Browse files
committed
fix: clipping
1 parent 11b1aef commit da83f03

1 file changed

Lines changed: 85 additions & 24 deletions

File tree

packages/uikit/src/clipping.ts

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Signal, computed } from '@preact/signals-core'
22
import { Matrix4, Plane, Vector3 } from 'three'
3-
import type { Vector2Tuple } from 'three'
3+
import type { Box3, Line3, Matrix3, Sphere, Vector2Tuple } from 'three'
44
import { Overflow } from 'yoga-layout/load'
55
import { FlexNodeState } from './flex/node.js'
66
import { RootContext } from './context.js'
@@ -185,29 +185,90 @@ export function createGlobalClippingPlanes(root: RootContext, clippingRect: Sign
185185

186186
const helperPlane = new Plane()
187187

188-
class RelativePlane extends Plane {
189-
constructor(getLocalPlane: () => Plane | undefined, getGlobalMatrix: () => Matrix4 | undefined) {
190-
super()
191-
const computeIntoHelper = () => {
192-
const localPlane = getLocalPlane()
193-
const globalMatrix = getGlobalMatrix()
194-
if (localPlane == null || globalMatrix == null) {
195-
helperPlane.copy(NoClippingPlane)
196-
return
197-
}
198-
helperPlane.copy(localPlane).applyMatrix4(globalMatrix)
188+
class RelativePlane implements Plane {
189+
get normal(): Vector3 {
190+
this.computeInto(helperPlane)
191+
return helperPlane.normal
192+
}
193+
get constant(): number {
194+
this.computeInto(helperPlane)
195+
return helperPlane.constant
196+
}
197+
isPlane = true as const
198+
199+
constructor(
200+
private getLocalPlane: () => Plane | undefined,
201+
private getGlobalMatrix: () => Matrix4 | undefined,
202+
) {}
203+
204+
private computeInto(target: Plane): Plane {
205+
const localPlane = this.getLocalPlane()
206+
const globalMatrix = this.getGlobalMatrix()
207+
if (localPlane == null || globalMatrix == null) {
208+
return target.copy(NoClippingPlane)
199209
}
200-
Object.assign(this, {
201-
get normal(): Vector3 {
202-
computeIntoHelper()
203-
return helperPlane.normal
204-
},
205-
set normal(n: Vector3) {},
206-
get constant(): number {
207-
computeIntoHelper()
208-
return helperPlane.constant
209-
},
210-
set constant(c: number) {},
211-
})
210+
return target.copy(localPlane).applyMatrix4(globalMatrix)
211+
}
212+
213+
set(normal: Vector3, constant: number): Plane {
214+
return this
215+
}
216+
setComponents(x: number, y: number, z: number, w: number): Plane {
217+
return this
218+
}
219+
setFromNormalAndCoplanarPoint(normal: Vector3, point: Vector3): Plane {
220+
return this
221+
}
222+
setFromCoplanarPoints(a: Vector3, b: Vector3, c: Vector3): Plane {
223+
return this
224+
}
225+
clone(): this {
226+
return this.computeInto(new Plane()) as this
227+
}
228+
copy(plane: Plane): this {
229+
this.computeInto(plane)
230+
return this
231+
}
232+
normalize(): Plane {
233+
return this
234+
}
235+
negate(): Plane {
236+
return this
237+
}
238+
distanceToPoint(point: Vector3): number {
239+
return this.computeInto(helperPlane).distanceToPoint(point)
240+
}
241+
distanceToSphere(sphere: Sphere): number {
242+
return this.computeInto(helperPlane).distanceToSphere(sphere)
243+
}
244+
projectPoint(point: Vector3, target: Vector3): Vector3 {
245+
return this.computeInto(helperPlane).projectPoint(point, target)
246+
}
247+
intersectLine(line: Line3, target: Vector3): Vector3 | null {
248+
return this.computeInto(helperPlane).intersectLine(line, target)
249+
}
250+
intersectsLine(line: Line3): boolean {
251+
return this.computeInto(helperPlane).intersectsLine(line)
252+
}
253+
intersectsBox(box: Box3): boolean {
254+
return this.computeInto(helperPlane).intersectsBox(box)
255+
}
256+
intersectsSphere(sphere: Sphere): boolean {
257+
return this.computeInto(helperPlane).intersectsSphere(sphere)
258+
}
259+
coplanarPoint(target: Vector3): Vector3 {
260+
return this.computeInto(helperPlane).coplanarPoint(target)
261+
}
262+
applyMatrix4(matrix: Matrix4, optionalNormalMatrix?: Matrix3): Plane {
263+
return this
264+
}
265+
translate(offset: Vector3): Plane {
266+
return this
267+
}
268+
equals(plane: Plane): boolean {
269+
return this.computeInto(helperPlane).equals(plane)
270+
}
271+
isIntersectionLine(l: any) {
272+
return this.computeInto(helperPlane).isIntersectionLine(l)
212273
}
213274
}

0 commit comments

Comments
 (0)