I believe I have found a potential solution after not working with three.js for some time, so my approach may not be the most elegant. To begin, I referred to the Shapes Example as it demonstrates:
- The process of procedurally creating a path (utilizing commands that handle curves)
- Extracting points from the aforementioned path
Therefore, I divided the issue into two main parts:
- Generating the path
- Traversing the path with interpolation in terms of position and rotation
Generating the path:
I utilized the rounded rectangle definition, which closely resembles a portion of the screenshot you provided.
var roundedRectShape = new THREE.Shape();
( function roundedRect( ctx, x, y, width, height, radius ){
ctx.moveTo( x, y + radius );
ctx.lineTo( x, y + height - radius );
ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
ctx.lineTo( x + width - radius, y + height) ;
ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
ctx.lineTo( x + width, y + radius );
ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
ctx.lineTo( x + radius, y );
ctx.quadraticCurveTo( x, y, x, y + radius );
} )( roundedRectShape, 0, 0, 200, 200, 20 );
Your path might deviate from a rounded rectangle, but the available curve functions (quadraticCurveTo, bezierCurveTo, splineThru) can still be very handy.
Another suggestion that comes to mind is to employ a Ruby script for exporting path coordinates from Sketchup to three.js. You could create your own script or explore existing ones. Here's one such script that can be easily located through Google search.
Traversing the path:
Thankfully, three.js already provides this functionality via Path's getPoint(t)
, where 't' ranges from 0.0 to 1.0 representing the traversal across the path. Thus, obtaining the position is straightforward along with calculating the next interpolated position on the path. Furthermore, utilizing Math.atan2()
helps determine the rotation:
t = (t + s)%1.0; // increment t while keeping it within 0.0 and 1.0
var p = path.getPoint(t); // point at t
var pn = path.getPoint((t + s)%1.0); // point at the next iteration of t
if(p != null && pn != null){
// move to current position
arrow.position.x = p.x;
arrow.position.y = p.y;
// calculate orientation based on the next position
arrow.rotation.z = Math.atan2(pn.y - p.y, pn.x - p.x);
}
To demonstrate generating and traversing a path in three.js based on the shapes example, below is a basic illustration (substituting an arrow shape with a cube):
<!DOCTYPE html>
<html lang="en">
<head>
<title>path interpolation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<canvas id="debug" style="position:absolute; left:100px"></canvas>
<script src="../build/three.min.js"></script>
<script src="js/libs/stats.min.js"></script>
<script>
var container, stats;
var camera, scene, renderer;
...
(Additional code and implementation details continue)
...
</script>
</body>
</html>
To offer a practical demonstration directly on this page, here is a runnable code snippet:
(JavaScript code block providing functional code related to path traversal using three.js)
...
(CSS styling properties presentational purposes)
...
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.min.js"></script>