-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTestShapeModels.java
363 lines (273 loc) · 13.3 KB
/
TestShapeModels.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (c) 2019, Chad Juliano, Kinetica DB Inc.
*
* SPDX-License-Identifier: MIT
*/
package io.github.chadj2.mesh.demo;
import java.awt.Color;
import java.io.File;
import java.nio.file.Paths;
import javax.vecmath.Point3f;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.javagl.jgltf.impl.v2.Material;
import io.github.chadj2.mesh.BaseBuilder;
import io.github.chadj2.mesh.MeshGltfWriter;
import io.github.chadj2.mesh.MeshGltfWriter.AlphaMode;
import io.github.chadj2.mesh.MeshBuilder;
import io.github.chadj2.mesh.MeshVertex;
public class TestShapeModels {
private final static Logger LOG = LoggerFactory.getLogger(TestShapeModels.class);
private final static String TEST_TEXTURE_PNG = "uv_grid_512.png";
private final static String OUT_PATH = "./demo";
private final MeshGltfWriter _writer = new MeshGltfWriter();
@Before
public void setup() {
// set the path where the texture files will be found
this._writer.setBasePath(new File("src/test/resources"));
}
public static File getFile(String _name) {
File _outFile = Paths.get(OUT_PATH, _name + ".gltf").toFile();
_outFile.getParentFile().mkdirs();
return _outFile;
}
/**
* Create a plane with the function y = 2*x*exp(-(x^2 + y^2)) using addPlane().
* @see MeshBuilder#addPlane
*/
@Test
public void testPlane() throws Exception {
// Set rendering for both sides of the plane
this._writer.setAlphaMode(AlphaMode.OPAQUE_DS);
final MeshBuilder _meshBuilder = new MeshBuilder("test_plane");
final Material _material = this._writer.newTextureMaterial(TEST_TEXTURE_PNG);
_meshBuilder.setMaterial(_material);
// size of grid
final int _length = 30;
// size of coordinates
final float _coordLength = 4f;
// grid to hold mesh points
final MeshVertex[][] _meshGrid = new MeshVertex[_length][_length];
for(int _xIdx = 0; _xIdx < _length; _xIdx++) {
// interpolate to within the range [-2,2]
final float _xPos = MeshBuilder.interpFloat(_length, _coordLength, _xIdx) - _coordLength/2f;
for(int _yIdx = 0; _yIdx < _length; _yIdx++) {
// interpolate to within the range [-2,2]
final float _zPos = MeshBuilder.interpFloat(_length, _coordLength, _yIdx) - _coordLength/2f;
// calculate the function 2*x*exp(-(x^2 + y^2))
final float _yPos = (float)(2*_xPos*Math.exp(-1*(_xPos*_xPos + _zPos*_zPos)));
// add the point in the mesh
Point3f _point = new Point3f(_xPos, _yPos, _zPos);
_meshGrid[_xIdx][_yIdx] = _meshBuilder.newVertex(_point);
}
}
// render the vertices in the grid
_meshBuilder.addPlane(_meshGrid, true);
// build the gltf buffers
_meshBuilder.build(this._writer);
File _outFile = TestShapeModels.getFile(_meshBuilder.getName());
this._writer.writeGltf(_outFile);
LOG.info("Finished generating: {}", _outFile);
}
/**
* Generate a rainbow colored diamond shape using addLathe().
* @see MeshBuilder#addLathe
*/
@Test
public void testDiamond() throws Exception {
final MeshBuilder _meshBuilder = new MeshBuilder("test_diamond");
Material _material = this._writer.newDefaultMaterial();
_meshBuilder.setMaterial(_material);
// number of sides around the tube
final int _sides = 12;
// radiuses along the tube
final float[] _radiusList = { 0.2f, 1f, 0.2f };
// y positions for the tube.
final float[] _yPosList = { -1f, 0f, 1f };
final MeshVertex[][] _meshGrid = new MeshVertex[_yPosList.length][];
for(int _yIdx = 0; _yIdx < _yPosList.length; _yIdx++) {
final float _yPos = _yPosList[_yIdx];
final float _radius = _radiusList[_yIdx];
// origin of the circle
final Point3f _circlePos = new Point3f(0, _yPos, 0);
// create a rainbow effect along the y axis
final Color _color = Color.getHSBColor((float)_yIdx/(float)_yPosList.length, 0.9f, 1.0f);
// add a circle that is part of the tube
_meshGrid[_yIdx] = _meshBuilder.addCircleVerticesXZ(_circlePos, _radius, _sides, _color);
}
// join the ends of the surface to create a tube
_meshBuilder.addLathe(_meshGrid, false);
// generate gltf buffers
_meshBuilder.build(this._writer);
File _outFile = TestShapeModels.getFile(_meshBuilder.getName());
this._writer.writeGltf(_outFile);
LOG.info("Finished generating: {}", _outFile);
}
/**
* Generate a textured helix with addLathe().
* @see MeshBuilder#addLathe
*/
@Test
public void testHelix() throws Exception {
// we create separate meshes for the textured helix and the ends
// because we can't mix textured and non-textured.
MeshBuilder _meshBuilder = new MeshBuilder("test_helix");
Material _materialTexture = this._writer.newTextureMaterial(TEST_TEXTURE_PNG);
_meshBuilder.setMaterial(_materialTexture);
MeshBuilder _meshBuilderEnds = new MeshBuilder("ends");
Material _materialDefault = this._writer.newDefaultMaterial();
_meshBuilderEnds.setMaterial(_materialDefault);
// height of the helix
final float _ySize = 6f;
// Number of rotations along the y-axis
final int _yDivisions = 60;
// radius of the helix rotation
final float _helixRadius = 0.5f;
// radius of the circle that constructs the helix
final float _circleRadius = 1f;
// number of levels of circles when building the grid
final int _circleSides = 12;
final MeshVertex[][] _meshGrid = new MeshVertex[_yDivisions][];
for(int _yIdx = 0; _yIdx < _yDivisions; _yIdx++) {
// Rotate 4pi radians from bottom to top
final double _angle = MeshBuilder.interpFloat(_yDivisions, 4*Math.PI, _yIdx);
final float _xPos = _helixRadius*(float)Math.cos(_angle);
final float _zPos = _helixRadius*(float)Math.sin(_angle);
// increase the height of the circles to build the helix
final float _yPos = MeshBuilder.interpFloat(_yDivisions, _ySize, _yIdx);
// position of the circle to draw
final Point3f _circlePos = new Point3f(_xPos, _yPos, _zPos);
// add the circle that forms the helix
_meshGrid[_yIdx] = _meshBuilder.addCircleVerticesXZ(_circlePos, _circleRadius, _circleSides, null);
// if this is the bottom then add a disc
if(_yIdx == 0) {
_meshBuilderEnds.addDiscXZ(_circlePos, -1*_circleRadius, _circleSides, Color.RED);
}
// if this is the top then add a disc
if(_yIdx == (_yDivisions -1)) {
_meshBuilderEnds.addDiscXZ(_circlePos, _circleRadius, _circleSides, Color.GREEN);
}
}
// join the ends of the surface to create a tube
_meshBuilder.addLathe(_meshGrid, true);
// generate gltf buffers for the cylindrical part
_meshBuilder.build(this._writer);
// generate gltf buffers for the ends
_meshBuilderEnds.build(this._writer);
File _outFile = TestShapeModels.getFile(_meshBuilder.getName());
this._writer.writeGltf(_outFile);
LOG.info("Finished generating: {}", _outFile);
}
/**
* Generate a textured torus using addManifold().
* @see MeshBuilder#addManifold
*/
@Test
public void testTorus() throws Exception {
MeshBuilder _meshBuilder = new MeshBuilder("test_torus");
Material _material = this._writer.newTextureMaterial(TEST_TEXTURE_PNG);
_meshBuilder.setMaterial(_material);
// sides of each circle in the vertical axis
final int _sizesVertical = 24;
// sides of each circle on the horizontal axis
final int _sidesHorizontal = 48;
// torus inner radius
final double _innerRadius = 2d;
// radius of the circles used to construct the torus
final double _circleRadius = 2d;
// grid to hold mesh vertices
final MeshVertex[][] _meshGrid = new MeshVertex[_sizesVertical][];
for(int _rIdx = 0; _rIdx < _sizesVertical; _rIdx++) {
// Rotate about an angle from 0 to -2pi.
// If we rotate the other way then planes would render as inverted.
final double _angle = MeshBuilder.interpFloat(_sizesVertical, 2*Math.PI, _rIdx);
// calculate points along a circle that rotates about the axis.
final double _radius = _circleRadius*Math.cos(_angle) + _circleRadius + _innerRadius;
final double _zPos = _circleRadius*Math.sin(_angle);
// position of the circle to draw.
final Point3f _circlePos = new Point3f(0, (float)_zPos, 0);
// for each point on the first circle draw another circle perpendicular
_meshGrid[_rIdx] = _meshBuilder.addCircleVerticesXZ(_circlePos,
(float)_radius, _sidesHorizontal, null);
}
// join all corners of the surface to create a closed manifold.
_meshBuilder.addManifold(_meshGrid, true);
// generate gltf buffers
_meshBuilder.build(this._writer);
File _outFile = TestShapeModels.getFile(_meshBuilder.getName());
this._writer.writeGltf(_outFile);
LOG.info("Finished generating: {}", _outFile);
}
/**
* Create the smallest possible grid to see how it is structured.
* @see MeshBuilder#addPlane
*/
@Test
public void testMinimalGrid() throws Exception {
final MeshBuilder _meshBuilder = new MeshBuilder("test_minimal_grid");
// disable normals
_meshBuilder.supressNormals(true);
// size of grid
final int _length = 3;
// size of coordinates
final float _coordLength = 4f;
final MeshVertex[][] _meshGrid = new MeshVertex[_length][_length];
// setup an flat white plane
for(int _xIdx = 0; _xIdx < _length; _xIdx++) {
final float _xPos = MeshBuilder.interpFloat(_length, _coordLength, _xIdx) - _coordLength/2f;
for(int _yIdx = 0; _yIdx < _length; _yIdx++) {
final float _zPos = MeshBuilder.interpFloat(_length, _coordLength, _yIdx) - _coordLength/2f;
Point3f _point = new Point3f(_xPos, 0, _zPos);
MeshVertex _vertex = _meshBuilder.newVertex(_point);
_meshGrid[_xIdx][_yIdx] = _vertex;
_vertex.setColor(Color.WHITE);
}
}
// add elevations at the edges and center.
_meshGrid[0][1].getVertex().y = 0.25f;
_meshGrid[1][0].getVertex().y = 0.25f;
_meshGrid[1][2].getVertex().y = 0.25f;
_meshGrid[2][1].getVertex().y = 0.25f;
_meshGrid[1][1].getVertex().y = 1f;
// render the vertices in the grid
_meshBuilder.addPlane(_meshGrid, true);
// build the gltf buffers
_meshBuilder.build(this._writer);
File _outFile = TestShapeModels.getFile(_meshBuilder.getName());
this._writer.writeGltf(_outFile);
LOG.info("Finished generating: {}", _outFile);
}
@Test
public void testBlend() throws Exception {
// Set rendering for both sides of the plane
this._writer.setAlphaMode(AlphaMode.BLEND_DS);
final int meshSize = 2;
final int numLayers = 4;
final MeshBuilder _meshBuilder = new MeshBuilder("test_blend");
final MeshVertex[][] _meshGrid = new MeshVertex[meshSize][meshSize];
for(int zIdx = 0; zIdx < numLayers; zIdx++) {
for(int xIdx = 0; xIdx < meshSize; xIdx++) {
for(int yIdx = 0; yIdx < meshSize; yIdx++) {
Point3f _point = new Point3f(xIdx, zIdx*0.4f, yIdx);
MeshVertex vertex = _meshBuilder.newVertex(_point);
_meshGrid[xIdx][yIdx] = vertex;
}
}
float partIdx = MeshBuilder.interpFloat(numLayers + 1, 1f, zIdx);
Color color = BaseBuilder.colorCreateHsba(partIdx, 0.5f, 1.0f, 0.7f);
Material material = this._writer.newBlendMaterial("test", 0.7f, 0.5f, color);
_meshBuilder.setMaterial(material);
// render the vertices in the grid
_meshBuilder.addPlane(_meshGrid, false);
// build the gltf buffers
_meshBuilder.build(this._writer);
LOG.info("Built layer: {}", zIdx);
}
_meshBuilder.debugNormals(this._writer, 0.2f);
File _outFile = TestShapeModels.getFile(_meshBuilder.getName());
this._writer.writeGltf(_outFile);
LOG.info("Finished generating: {}", _outFile);
}
}