关于[Threejs] intersectLine 方法这里看不太懂,求大佬解释下.

2023-02-09 12:49:53 +08:00
 rabbbit

源码如下,作用是求平面和线段的交点.地址在这: https://github.com/mrdoob/three.js/blob/6e897f9a42d615403dfa812b45663149f2d2db3e/src/math/Plane.js

这步 - ( line.start.dot( this.normal ) + this.constant ) / denominator 没看懂

为啥是 -(线段起点向量 ⋅ 平面法线 + 平面到原点距离) / (平面法线 ⋅ 线段方向向量) ?

	intersectLine( line, target ) {

		const direction = line.delta( _vector1 );

		const denominator = this.normal.dot( direction );

		if ( denominator === 0 ) {

			// line is coplanar, return origin
			if ( this.distanceToPoint( line.start ) === 0 ) {

				return target.copy( line.start );

			}

			// Unsure if this is the correct method to handle this case.
			return null;

		}

		const t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;

		if ( t < 0 || t > 1 ) {

			return null;

		}

		return target.copy( direction ).multiplyScalar( t ).add( line.start );

	}
350 次点击
所在节点    问与答
1 条回复
sillydaddy
2023-02-09 13:44:59 +08:00
line.start.dot( this.normal ) + this.constant 这个是计算线段起点到平面的距离 d ,这个距离有正负号。
(根据就是平面公式:Ax+By+Cz+D=0 ,A,B,C,D 对应 this.normal ,this.constant 。位于平面上的点,坐标 xyz 代入后满足=0 ,位于平面上侧的点 Ax+By+Cz+D>0 。)

然后 denominator 是线段与平面所形成夹角的正弦值 sin 。d 和斜边 l(起点到交点)的关系就是 l=d/sin 。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/914543

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX