Three 向量运算笔记 和参考刻度绘制

2023-10-27  本文已影响0人  吉凶以情迁

在我写的ue篇中
向前向量x100
只有方向的向量才能进行乘法,
如果拿位置乘 就不对了,具体为什么根据下面的 代码就知道为什么了

https://www.jianshu.com/p/51b2b95c6206

image.png

x代表向右的方向
A设置 x yz =-50,0,0
B设置x yz = 50,0,0
a-b= 被减数-50 -50 = -100(本来欠50 还要 欠50)
b-a=50 **- **-50 本来有50 ,减去 50是0 - 减- 变成加法为100

从-50(A) 移动到 目标50 (B)需要移动距离为100

测试代码

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(500);
scene.add(axesHelper);
const mesh =new THREE.Mesh(new BoxGeometry(5,5,5),new THREE.MeshBasicMaterial({  color: 0xff0000,
  transparent: true,
  opacity: 0.5}))
scene.add(mesh)
const left50=new THREE.Vector3(-50,0,0)
const right50=new THREE.Vector3(50,20,0)
const right =new THREE.Mesh(new BoxGeometry(5,5,5),new THREE.MeshBasicMaterial({  color: 0x00ff00,
    transparent: true,
    opacity: 0.5}))
scene.add(right)
right.position.add(right50)
mesh.position.add(left50)
console.log("a",left50,"b:",right50)
 const newVec=right50.clone().sub(left50)
console.log("AB相减后向量",newVec)
console.log("长度"+newVec.length())
console.log("归一化变成方向",newVec.normalize())
console.log("归一化后长度"+newVec.length())
console.log("归一化后方向乘100",newVec.multiplyScalar(100));
console.log("需要移动的方向距离(Add)",newVec)
mesh.position.add(newVec)
console.log("A最后移动到B的位置",mesh.position)
console.log(mesh.position)

归一化

测试发现有偏差,去掉归一化normalize正常了 ,归一化仅仅是为了得到方向的, 比如camera.lookat 可以传递归一化的距离

也就是 A当前位置+(AB距离乘AB距离的长度)=B位置 加了归一化就会少一些些。

当前,a到b也可以直接设置距离为b,
归一化 distance * len/ 1

归一化后取长度会发现永远不会超过1

camera.getWorldDirection()实际上是 0-position 归一化后的结果

生成刻度线

上一篇 下一篇

猜你喜欢

热点阅读