CurveBuilder
An evaluator of curve evaluator objects for generating vertex attributes for a curve.
For more information, see the Parametric Curves and Parametric Surfaces tutorial.
Methods
- attribute
Sets the parametric curve used to generate vertex attribute values. - clearVertices
Clears the arrays of attribute values (such as positions and normals) and vertex indices generated so far. - constantAttribute
Sets a value for an attribute semantic that will be the same for all future vertices generated by the “evalCurve” method. - constantColor
Sets a value for a color attribute that will be the same for all future vertices generated by the “evalCurve” method. - curveToBuffer
Convenience method for creating a mesh buffer from a parametric curve. - evalCurve
Generates the vertex attributes of the parametric curves. - position
Sets the parametric curve used to generate vertex positions. - toMeshBuffer
Generates a mesh buffer containing the vertex attributes generated so far.
### CurveBuilder#attribute(curve, semantic, [semanticIndex], [size])
Sets the parametric curve used to generate vertex attribute values.
Parameters
curve
(Type: Object)
A curve evaluator object that describes the parametric curve used to generate attribute values. u-coordinates for the specified curve correspond to u-coordinates for the curve used to generate vertex positions.-
semantic
(Type: numberstring)
An attribute semantic, such as “position” or “uv”. This value can be one of the following or any other nonempty string. “position”: Attribute semantic for a vertex position. In general, vertex positions are 2-dimensional or 3-dimensional. “normal”: Attribute semantic for a vertex normal.
For 3D graphics libraries to calculate a mesh buffer’s lighting and shading correctly, that mesh buffer must specify normals for all its vertices.
What are normals? A normal is a set of numbers (usually three numbers) describing a particular direction. Generally, a normal’s direction is perpendicular to a surface’s edges, and points up and away from the surface.
Normals are important in the lighting and shading model. When light hits an object’s surface, how brightly the surface will be lit depends on how directly the light points to the surface. It will be lit the most brightly if the light is directly opposite to its normal, and not at all if the light is perpendicular to the normal or in the same direction as the normal.
In general, vertex normals are 3-dimensional and are defined for a mesh buffer only if it also contains vertex positions. “uv”: Attribute semantic for a tuple of texture coordinates.
If a texture (array of memory units) will be applied to a mesh buffer’s geometry, then texture coordinates need to be specified for each vertex in that mesh buffer. In general, a texture coordinate is one of two numbers, called U and V, that map to a specific point in the texture. Each texture coordinate ranges from 0 to 1.
In most 3D graphics pipelines, u-coordinates start at the left of the texture (0) and increase to the right (1). In some graphics pipelines, such as OpenGL, v-coordinates start by default at the bottom of the texture (0) and increase to the top (1), whereas in others, such as WebGL, Vulkan, Metal, and Direct3D, v-coordinates start by default at the top of the texture and increase to the bottom. Thus, for example, in OpenGL by default, texture coordinates (0, 1) indicate the upper-left corner of the texture, and texture coordinates (0.5, 0.5) indicate the center of the texture.
In general, texture coordinates describe 2-dimensional points. However, for such texturing tasks as mapping a square to a trapezoid, trios of 3-dimensional texture coordinates (U, V, and Z) are useful to ensure the texturing remains perspective-correct. In this case, the 3-D texture coordinates are converted to 2-D by dividing the U and V components by the Z component. In a fragment shader or pixel shader, this can look like the following code: texCoord.xy/texCoord.z
. “color”: Attribute semantic for a color. In general, each color consists of three components. “tangent”, “bitangent”: Attribute semantic for a tangent vector or bitangent vector, respectively. In general, the vector consists of three components.
* semanticIndex
(Type: number) (optional)
The set index of the attribute for the specified semantic. 0 is the first index of the attribute, 1 is the second, and so on. This is ignored if “name” is a string. If null, undefined, or omitted, the default is 0.
* size
(Type: number) (optional)
The number of elements in each position value. For example, if the attribute is 3-dimensional, this parameter is 3. If null, undefined, or omitted, the default is 3. Throws an error if this value is 0 or less.
Return Value
This object. (Type: CurveBuilder)
### CurveBuilder#clearVertices()
Clears the arrays of attribute values (such as positions and normals) and vertex indices generated so far. The attributes themselves will remain.
Return Value
This object. (Type: CurveBuilder)
### CurveBuilder#constantAttribute(constantValue, semantic, [semanticIndex])
Sets a value for an attribute semantic that will be the same for all future vertices generated by the “evalCurve” method.
Parameters
-
constantValue
(Type: Objectnumber)
A constant value for the attribute semantic. -
semantic
(Type: numberstring)
An attribute semantic, such as “position” or “uv”. semanticIndex
(Type: number) (optional)
The set index of the attribute for the specified semantic. 0 is the first index of the attribute, 1 is the second, and so on. This is ignored if “name” is a string. If null, undefined, or omitted, the default is 0.
Return Value
This object. (Type: CurveBuilder)
Examples
This example sets the color to use for future vertices to be generated for the curve.
// Set color to red curve.constantAttribute([1,0,0],"COLOR");
### CurveBuilder#constantColor(color, [semanticIndex])
Sets a value for a color attribute that will be the same for all future vertices generated by the “evalCurve” method.
Parameters
-
color
(Type: Array.<number>number string)
A color vector or string identifying the constant color value. The alpha channel of the resulting color will be ignored. semanticIndex
(Type: number) (optional)
The set index of the attribute for the specified semantic. 0 is the first index of the attribute, 1 is the second, and so on. If null, undefined, or omitted, the default is 0.
Return Value
This object. (Type: CurveBuilder)
Examples
This example sets the color to use for future vertices to be generated for the curve.
// Set color to red curve.constantColor("red");
### (static) CurveBuilder.curveToBuffer(curve, [mode], [n], [u1], [u2])
Convenience method for creating a mesh buffer from a parametric curve. The mesh buffer will contain positions and vertex normals that cover the specified surface.
Parameters
curve
(Type: Object)
A curve evaluator object that describes the parametric curve used to generate positions.mode
(Type: number) (optional)
If this value is MeshBuffer.LINES, or is null, undefined, or omitted, generates a series of lines defining the curve. If this value is MeshBuffer.POINTS, generates a series of points along the curve. For any other value, this method has no effect.n
(Type: number) (optional)
Number of subdivisions of the curve to be drawn. If null or undefined, a default is determined automatically based on the position curve’s arc length, or the distance taken by its path (or the default is 24 if no position curve was defined). If 0, this method has no effect. Throws an error if this value is less than 0.u1
(Type: number) (optional)
Starting point of the curve. Default is the starting coordinate given by the curve evaluator object, or 0 if not given.u2
(Type: number) (optional)
Ending point of the curve. Default is the ending coordinate given by the curve evaluator object, or 1 if not given.
Return Value
The generated mesh buffer. (Type: MeshBuffer)
### CurveBuilder#evalCurve([mode], [n], [u1], [u2])
Generates the vertex attributes of the parametric curves.
Parameters
mode
(Type: number) (optional)
If this value is MeshBuffer.LINES, or is null, undefined, or omitted, generates a series of lines defining the curve. If this value is MeshBuffer.POINTS, generates a series of points along the curve. For any other value, this method has no effect.n
(Type: number) (optional)
Number of subdivisions of the curve to be drawn. If null or undefined, a default is determined automatically based on the position curve’s arc length, or the distance taken by its path (or the default is 24 if no position curve was defined). If 0, this method has no effect. Throws an error if this value is less than 0.u1
(Type: number) (optional)
Starting point of the curve. Default is the starting coordinate given by the curve evaluator object, or 0 if not given.u2
(Type: number) (optional)
Ending point of the curve. Default is the ending coordinate given by the curve evaluator object, or 1 if not given.
Return Value
This object. (Type: CurveBuilder)
### CurveBuilder#position(curve, [size])
Sets the parametric curve used to generate vertex positions.
Parameters
curve
(Type: Object)
A curve evaluator object that describes the parametric curve used to generate positions.size
(Type: number) (optional)
The number of elements in each position value. For example, if the attribute is 3-dimensional, this parameter is 3. If null, undefined, or omitted, the default is 3. Throws an error if this value is 0 or less.
Return Value
This object. (Type: CurveBuilder)
### CurveBuilder#toMeshBuffer()
Generates a mesh buffer containing the vertex attributes generated so far. The mesh buffer’s primitive type will equal the last type passed to the “mode” parameter in the CurveBuilder.curveEval method.
Return Value
The generated mesh buffer. (Type: MeshBuffer)