(bounds, resolution)
| 15 | */ |
| 16 | /* eslint-disable max-statements */ |
| 17 | export default function createMesh(bounds, resolution) { |
| 18 | if (!resolution) { |
| 19 | return createQuad(bounds); |
| 20 | } |
| 21 | const maxXSpan = Math.max( |
| 22 | Math.abs(bounds[0][0] - bounds[3][0]), |
| 23 | Math.abs(bounds[1][0] - bounds[2][0]) |
| 24 | ); |
| 25 | const maxYSpan = Math.max( |
| 26 | Math.abs(bounds[1][1] - bounds[0][1]), |
| 27 | Math.abs(bounds[2][1] - bounds[3][1]) |
| 28 | ); |
| 29 | const uCount = Math.ceil(maxXSpan / resolution) + 1; |
| 30 | const vCount = Math.ceil(maxYSpan / resolution) + 1; |
| 31 | |
| 32 | const vertexCount = (uCount - 1) * (vCount - 1) * 6; |
| 33 | const indices = new Uint32Array(vertexCount); |
| 34 | const texCoords = new Float32Array(uCount * vCount * 2); |
| 35 | const positions = new Float64Array(uCount * vCount * 3); |
| 36 | |
| 37 | // Tesselate |
| 38 | let vertex = 0; |
| 39 | let index = 0; |
| 40 | for (let u = 0; u < uCount; u++) { |
| 41 | const ut = u / (uCount - 1); |
| 42 | for (let v = 0; v < vCount; v++) { |
| 43 | const vt = v / (vCount - 1); |
| 44 | const p = interpolateQuad(bounds, ut, vt); |
| 45 | |
| 46 | positions[vertex * 3 + 0] = p[0]; |
| 47 | positions[vertex * 3 + 1] = p[1]; |
| 48 | positions[vertex * 3 + 2] = p[2] || 0; |
| 49 | |
| 50 | texCoords[vertex * 2 + 0] = ut; |
| 51 | texCoords[vertex * 2 + 1] = 1 - vt; |
| 52 | |
| 53 | if (u > 0 && v > 0) { |
| 54 | indices[index++] = vertex - vCount; |
| 55 | indices[index++] = vertex - vCount - 1; |
| 56 | indices[index++] = vertex - 1; |
| 57 | indices[index++] = vertex - vCount; |
| 58 | indices[index++] = vertex - 1; |
| 59 | indices[index++] = vertex; |
| 60 | } |
| 61 | |
| 62 | vertex++; |
| 63 | } |
| 64 | } |
| 65 | return { |
| 66 | vertexCount, |
| 67 | positions, |
| 68 | indices, |
| 69 | texCoords |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | function createQuad(bounds) { |
| 74 | const positions = new Float64Array(12); |
no test coverage detected
searching dependent graphs…