我希望创建一个跟随曲线的螺旋线,看看我在追求的是什么,这个链接有一个代表它的gif ......
https://www.mapleprimes.com/posts/202940-Spiral-Around-The-Curve
(我对动画或其他几何结构不感兴趣,只是产生的粉红色螺旋)
理想情况下,我应该能够获得任何形状曲线,知道我想要的螺旋直径/半径,并从中生成第二条曲线(螺旋线),以恒定的间距绕其移动
我在Javascript(threeJS)中这样做,但我认为它更多的是一般的数学问题.
使用以下内容,我可以在直线段周围得到一个螺旋线,但是当它改变方向/弯曲时它会失败...
let helixPoints = [];
let helixDiameter = 30;
for (let t = 0; t < 1; t += 0.01) {
let curvePoint = curve.getPointAt(t);
let helixX = curvePoint.x + (helixDiameter * Math.cos(t*100));
let helixY = curvePoint.y +(helixDiameter * Math.sin(t*100))
let helixZ = curvePoint.z;
helixPoints.push(new THREE.Vector3(helixX, helixY, helixZ));
}
let helixCurve = new THREE.CatmullRomCurve3(helixPoints);
Run Code Online (Sandbox Code Playgroud)
我知道我需要为helixZ做更多的事情,我认为它遵循任何曲线,我可能需要获得点的切线?
我无法理解数学,如果有人能指出我正确的方向,我将不胜感激
制作螺旋线跟随螺旋线...这很有趣.可能有更好的方法,但这是我的解决方案:
我以与获得初始螺旋(我的演示中的黄线)相同的方式定义了曲线.调用getPoints
给了一堆Vector3
对象,然后我开始工作.
首先,从第二点开始,我回顾了前一点并形成了一个方向.这接近于曲线的切线,但如果从曲线中收集较少的点,则会得到较不准确的曲线.
接下来,我用up
矢量划过它.我使用过(0, 1, 0)
,但原始螺旋的方向可能需要您使用不同的方向.我们的想法up
总是针对您的切线,以便穿过它们将产生垂直于曲线的矢量.
说到交叉向量,这就是我接下来做的,并得到了一个很好的统一向量集.然后applyAxisAngle
,我习惯于将切线周围的垂直向量旋转一个从当前点的索引计算出来的量.
最后,将原始点位置添加到旋转矢量为我们提供了相当于旋转点的3D空间中的点,但是遵循原始曲线.
您可以使用不同的值,例如temp
长度和用于实现不同半径,频率等的间隔.
这是一个相当大的挑战.谢谢你的运动!如果您有任何疑问,请发表评论,我会尽力回答.
// prepare the renderer
let WIDTH
let HEIGHT
let aspectRatio = function() {
return WIDTH / HEIGHT
}
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
})
document.body.appendChild(renderer.domElement)
const camera = new THREE.PerspectiveCamera(32, aspectRatio(), 1, 1000)
camera.position.set(0, 0, 50)
function resize() {
WIDTH = window.innerWidth
HEIGHT = window.innerHeight
renderer.setSize(WIDTH, HEIGHT)
camera.aspect = aspectRatio()
camera.updateProjectionMatrix()
}
resize()
window.addEventListener("resize", resize)
const scene = new THREE.Scene()
const light = new THREE.DirectionalLight(0xffffff, 1, Infinity)
light.position.set(0, 0, 1)
camera.add(light)
scene.add(camera)
// populate the scene
let group = new THREE.Group()
group.scale.set(5, 5, 5)
scene.add(group)
let curvePoints = []
let rad = Math.PI / 4
for (let i = -10; i <= 10; ++i) {
curvePoints.push(new THREE.Vector3(i / 5, Math.sin(rad * i), Math.cos(rad * i)))
}
let curve = new THREE.CatmullRomCurve3(curvePoints)
let geoPoints = curve.getPoints(500);
let geo = new THREE.BufferGeometry().setFromPoints(geoPoints)
let mat = new THREE.LineBasicMaterial({
color: "yellow"
})
let mesh = new THREE.Line(geo, mat)
group.add(mesh)
let dir = new THREE.Vector3()
let up = new THREE.Vector3(0, 1, 0)
let temp = new THREE.Vector3()
let amount = 0.25;
let innerHelixPoints = []
geoPoints.forEach(function(point, index, arr) {
if (index > 0) {
dir.subVectors(point, arr[index - 1])
dir.normalize()
temp.crossVectors(dir, up)
temp.applyAxisAngle(dir, amount * index)
temp.setLength(0.5)
temp.add(arr[index - 1])
innerHelixPoints.push(temp.clone())
}
})
geo = new THREE.BufferGeometry().setFromPoints(innerHelixPoints)
mat = new THREE.LineBasicMaterial({
color: "blue"
})
mesh = new THREE.Line(geo, mat)
group.add(mesh)
/* let geo = new THREE.BoxBufferGeometry(10, 10, 10)
let mat = new THREE.MeshLambertMaterial({
color: "red"
})
let mesh = new THREE.Mesh(geo, mat)
scene.add(mesh) */
function updateObjects() {
group.rotation.x += 0.001
group.rotation.y += 0.002
}
// rendering functions
function render() {
renderer.render(scene, camera)
}
let animationLoopId = null
function animationLoop() {
animationLoopId = requestAnimationFrame(animationLoop)
updateObjects()
render()
}
animationLoop()
Run Code Online (Sandbox Code Playgroud)
html,
body {
padding: 0;
margin: 0;
overflow: hidden;
background: slateGray;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/93/three.js"></script>
Run Code Online (Sandbox Code Playgroud)