@carbonenginejs/core-math 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,234 @@
1
+ import { vec3 } from "../vec3.js";
2
+ import { vec2 } from "../vec2.js";
3
+ import { toJSON } from "./json.js";
4
+
5
+ /**
6
+ * Creates a cylinder
7
+ * @author Three.js converted
8
+ **/
9
+ export function createCylinder(options = {})
10
+ {
11
+ let {
12
+ radiusTop = 1,
13
+ radiusBottom = 1,
14
+ height = 1,
15
+ radialSegments = 32,
16
+ heightSegments = 1,
17
+ openEnded = false,
18
+ thetaStart = 0,
19
+ thetaLength = Math.PI * 2
20
+ } = options;
21
+
22
+ radialSegments = Math.floor(radialSegments);
23
+ heightSegments = Math.floor(heightSegments);
24
+
25
+ // buffers
26
+ const
27
+ indices = [],
28
+ positions = [],
29
+ normals = [],
30
+ uvs = [];
31
+
32
+ // helper variables
33
+ let index = 0;
34
+
35
+ const
36
+ indexArray = [],
37
+ halfHeight = height / 2;
38
+
39
+ // build geometry
40
+ function generateTorso()
41
+ {
42
+ const
43
+ normal = vec3.alloc(),
44
+ vertex = vec3.alloc();
45
+
46
+ // this will be used to calculate the normal
47
+ const slope = (radiusBottom - radiusTop) / height;
48
+
49
+ // generate positions, normals and uvs
50
+
51
+ for (let y = 0; y <= heightSegments; y++)
52
+ {
53
+ const
54
+ indexRow = [],
55
+ v = y / heightSegments;
56
+
57
+ // calculate the radius of the current row
58
+ const radius = v * (radiusBottom - radiusTop) + radiusTop;
59
+ for (let x = 0; x <= radialSegments; x++)
60
+ {
61
+ const
62
+ u = x / radialSegments,
63
+ theta = u * thetaLength + thetaStart,
64
+ sinTheta = Math.sin(theta),
65
+ cosTheta = Math.cos(theta);
66
+
67
+ // vertex
68
+ vertex[0] = radius * sinTheta;
69
+ vertex[1] = -v * height + halfHeight;
70
+ vertex[2] = radius * cosTheta;
71
+ positions.push(vertex[0], vertex[1], vertex[2]);
72
+
73
+ // normal
74
+ normal[0] = sinTheta;
75
+ normal[1] = slope;
76
+ normal[2] = cosTheta;
77
+ vec3.normalize(normal, normal);
78
+ normals.push(normal[0], normal[1], normal[2]);
79
+
80
+ // uv
81
+ uvs.push(u, 1 - v);
82
+
83
+ // save index of vertex in respective row
84
+ indexRow.push(index++);
85
+ }
86
+
87
+ // now save positions of the row in our index array
88
+ indexArray.push(indexRow);
89
+ }
90
+
91
+ vec3.unalloc(vertex);
92
+ vec3.unalloc(normal);
93
+
94
+ // generate indices
95
+ for (let x = 0; x < radialSegments; x++)
96
+ {
97
+ for (let y = 0; y < heightSegments; y++)
98
+ {
99
+ // we use the index array to access the correct indices
100
+ const
101
+ a = indexArray[y][x],
102
+ b = indexArray[y + 1][x],
103
+ c = indexArray[y + 1][x + 1],
104
+ d = indexArray[y][x + 1];
105
+
106
+ // faces
107
+ indices.push(a, b, d);
108
+ indices.push(b, c, d);
109
+ }
110
+ }
111
+ }
112
+
113
+ function generateCap(top)
114
+ {
115
+
116
+ // save the index of the first center vertex
117
+ const centerIndexStart = index;
118
+
119
+ const
120
+ uv = vec2.alloc(),
121
+ vertex = vec3.alloc(),
122
+ radius = (top === true) ? radiusTop : radiusBottom,
123
+ sign = (top === true) ? 1 : -1;
124
+
125
+ // first we generate the center vertex data of the cap.
126
+ // because the geometry needs one set of uvs per face,
127
+ // we must generate a center vertex per face/segment
128
+
129
+ for (let x = 1; x <= radialSegments; x++)
130
+ {
131
+ // vertex
132
+ positions.push(0, halfHeight * sign, 0);
133
+
134
+ // normal
135
+ normals.push(0, sign, 0);
136
+
137
+ // uv
138
+ uvs.push(0.5, 0.5);
139
+
140
+ // increase index
141
+ index++;
142
+ }
143
+
144
+ // save the index of the last center vertex
145
+ const centerIndexEnd = index;
146
+
147
+ // now we generate the surrounding positions, normals and uvs
148
+
149
+ for (let x = 0; x <= radialSegments; x++)
150
+ {
151
+ const
152
+ u = x / radialSegments,
153
+ theta = u * thetaLength + thetaStart,
154
+ cosTheta = Math.cos(theta),
155
+ sinTheta = Math.sin(theta);
156
+
157
+ // vertex
158
+ vertex[0] = radius * sinTheta;
159
+ vertex[1] = halfHeight * sign;
160
+ vertex[2] = radius * cosTheta;
161
+ positions.push(vertex[0], vertex[1], vertex[2]);
162
+
163
+ // normal
164
+ normals.push(0, sign, 0);
165
+
166
+ // uv
167
+ uv[0] = (cosTheta * 0.5) + 0.5;
168
+ uv[1] = (sinTheta * 0.5 * sign) + 0.5;
169
+ uvs.push(uv[0], uv[1]);
170
+
171
+ // increase index
172
+ index++;
173
+ }
174
+
175
+ vec2.unalloc(uv);
176
+ vec3.unalloc(vertex);
177
+
178
+ // generate indices
179
+ for (let x = 0; x < radialSegments; x++)
180
+ {
181
+ const
182
+ c = centerIndexStart + x,
183
+ i = centerIndexEnd + x;
184
+
185
+ if (top === true)
186
+ {
187
+ // face top
188
+ indices.push(i, i + 1, c);
189
+ }
190
+ else
191
+ {
192
+ // face bottom
193
+ indices.push(i + 1, i, c);
194
+ }
195
+ }
196
+ }
197
+
198
+ // generate geometry
199
+ generateTorso();
200
+
201
+ if (openEnded === false)
202
+ {
203
+ if (radiusTop > 0) generateCap(true);
204
+ if (radiusBottom > 0) generateCap(false);
205
+ }
206
+
207
+ const result = toJSON(indices, positions, uvs, normals);
208
+ result.factory = createCylinder;
209
+ result.options = {
210
+ radiusTop,
211
+ radiusBottom,
212
+ height,
213
+ radialSegments,
214
+ heightSegments,
215
+ openEnded,
216
+ thetaStart,
217
+ thetaLength
218
+ };
219
+ return result;
220
+ }
221
+
222
+ export function createCone(options={})
223
+ {
224
+ const { radius = 1 } = options;
225
+ const result = createCylinder(Object.assign({}, options, {
226
+ radiusTop: 0,
227
+ radiusBottom: radius
228
+ }));
229
+ result.factory = createCone;
230
+ Reflect.deleteProperty(result.options, "radiusTop");
231
+ Reflect.deleteProperty(result.options, "radiusBottom");
232
+ result.options.radius = radius;
233
+ return result;
234
+ }
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2016, Mapbox
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose
6
+ with or without fee is hereby granted, provided that the above copyright notice
7
+ and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13
+ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14
+ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15
+ THIS SOFTWARE.