-
Notifications
You must be signed in to change notification settings - Fork 0
FunctionPipe
How to use the FunctionPipe object type.
create a new csharp-tesselatable object. The code should create a class inherited from FunctionPipe. The class should have a inner class which implements the IFunctionPipeFunction interface. The constructor of the outer class should allocate a new instance of the inner class to the pipeFunction member of the outer class. You can also change the parameters that control the size of the pipe, and define an outline to be extruded. It no outline if defined then we default to a 16 point 'circle'.
The interface requires three functions to be implemented
- public void GetStartPosition(Vector3 p) : Sets the vector p to the starting point of the function.
- public void InitParameters() : Should initialize any variables used by the function.
- public void GetNextPosition(Vector3 p) : Calculate the 'next value' for the function and use it to set the co-ordinates of the next point, p contains the previous point going in and should contain the resulting point going out.
The parameters that control the size and flexibility of the pipe are
- pipeSegments : The number of times the function is iterated. Each point is joined to the preceding point by a Bezier curve to make one segment.
- knotsPerPipeSegment : The number of points along the Bezier curve we calculate. Determines how smooth the pipe looks.
- pipeRadius : The radius of the default pipe created if no outline is defined.
The whole thing looks like this
object {
shader metal
accel kdtree
type csharp-tesselatable
name fred
<code>
using System;
using SunflowSharp.Maths;
using SunflowSharp.Core.Primitive;
namespace SunflowSharp.Core.Tesselatable
{
public class FunctionPipeTest : FunctionPipe
{
public FunctionPipeTest () {
// set the parameters that control the size of the pipe
pipeSegments = 1000;
knotsPerPipeSegment = 12;
pipeRadius = 0.5f;
// define an outline to extrude along the path
// created by the function. It should be defined in the x/y plane
// This one defines a 5 point star.
int circleSegments = 10;
double theta = 0;
double thetaDelta = Math.PI * 2.0 / circleSegments;
for (int i = 0; i < circleSegments ; i++) {
outlinePoints.Add(new Point3(
(float)((0.5 + i%2) * pipeRadius * Math.Cos(theta)),
(float)((0.5 + i%2) * pipeRadius * Math.Sin(theta)),
0f));
theta += thetaDelta;
}
// create an instance of the function
pipeFunction = new LineFunction();
// create an instance of the function
pipeFunction = new LineFunction();
}
class LineFunction : IFunctionPipeFunction {
float delta, x;
public void GetStartPosition(Vector3 p) {
p.set(0.0f, 0.0f, 0.0f);
}
public void InitParameters()
{
x=0;
delta = (float)(4.0 * Math.PI / 1000.0);
}
// calculate next attractor position
public void GetNextPosition(Vector3 p)
{
x += delta;
p.x += 0.5f;
p.y += (float)Math.Sin(x);
}
}
}
}
</code>
}
Which should produce a nice star shaped tube tube following a sine wave.