V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
rabbbit
V2EX  ›  问与答

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

  •  
  •   rabbbit · 2023-02-09 12:49:53 +08:00 · 337 次点击
    这是一个创建于 455 天前的主题,其中的信息可能已经有所发展或是发生改变。

    源码如下,作用是求平面和线段的交点.地址在这: 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 );
    
    	}
    
    第 1 条附言  ·  2023-02-10 17:25:05 +08:00
    1 条回复    2023-02-09 13:44:59 +08:00
    sillydaddy
        1
    sillydaddy  
       2023-02-09 13:44:59 +08:00   ❤️ 1
    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 。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3284 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:50 · PVG 21:50 · LAX 06:50 · JFK 09:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.