File size: 3,435 Bytes
2b7aae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { BufferGeometry } from '../core/BufferGeometry.js';
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
import { Shape } from '../extras/core/Shape.js';
import { ShapeUtils } from '../extras/ShapeUtils.js';
import { Vector2 } from '../math/Vector2.js';

class ShapeGeometry extends BufferGeometry {
	constructor(shapes = new Shape([new Vector2(0, 0.5), new Vector2(-0.5, -0.5), new Vector2(0.5, -0.5)]), curveSegments = 12) {
		super();
		this.type = 'ShapeGeometry';

		this.parameters = {
			shapes: shapes,
			curveSegments: curveSegments,
		};

		// buffers

		const indices = [];
		const vertices = [];
		const normals = [];
		const uvs = [];

		// helper variables

		let groupStart = 0;
		let groupCount = 0;

		// allow single and array values for "shapes" parameter

		if (Array.isArray(shapes) === false) {
			addShape(shapes);
		} else {
			for (let i = 0; i < shapes.length; i++) {
				addShape(shapes[i]);

				this.addGroup(groupStart, groupCount, i); // enables MultiMaterial support

				groupStart += groupCount;
				groupCount = 0;
			}
		}

		// build geometry

		this.setIndex(indices);
		this.setAttribute('position', new Float32BufferAttribute(vertices, 3));
		this.setAttribute('normal', new Float32BufferAttribute(normals, 3));
		this.setAttribute('uv', new Float32BufferAttribute(uvs, 2));

		// helper functions

		function addShape(shape) {
			const indexOffset = vertices.length / 3;
			const points = shape.extractPoints(curveSegments);

			let shapeVertices = points.shape;
			const shapeHoles = points.holes;

			// check direction of vertices

			if (ShapeUtils.isClockWise(shapeVertices) === false) {
				shapeVertices = shapeVertices.reverse();
			}

			for (let i = 0, l = shapeHoles.length; i < l; i++) {
				const shapeHole = shapeHoles[i];

				if (ShapeUtils.isClockWise(shapeHole) === true) {
					shapeHoles[i] = shapeHole.reverse();
				}
			}

			const faces = ShapeUtils.triangulateShape(shapeVertices, shapeHoles);

			// join vertices of inner and outer paths to a single array

			for (let i = 0, l = shapeHoles.length; i < l; i++) {
				const shapeHole = shapeHoles[i];
				shapeVertices = shapeVertices.concat(shapeHole);
			}

			// vertices, normals, uvs

			for (let i = 0, l = shapeVertices.length; i < l; i++) {
				const vertex = shapeVertices[i];

				vertices.push(vertex.x, vertex.y, 0);
				normals.push(0, 0, 1);
				uvs.push(vertex.x, vertex.y); // world uvs
			}

			// incides

			for (let i = 0, l = faces.length; i < l; i++) {
				const face = faces[i];

				const a = face[0] + indexOffset;
				const b = face[1] + indexOffset;
				const c = face[2] + indexOffset;

				indices.push(a, b, c);
				groupCount += 3;
			}
		}
	}

	toJSON() {
		const data = super.toJSON();

		const shapes = this.parameters.shapes;

		return toJSON(shapes, data);
	}

	static fromJSON(data, shapes) {
		const geometryShapes = [];

		for (let j = 0, jl = data.shapes.length; j < jl; j++) {
			const shape = shapes[data.shapes[j]];

			geometryShapes.push(shape);
		}

		return new ShapeGeometry(geometryShapes, data.curveSegments);
	}
}

function toJSON(shapes, data) {
	data.shapes = [];

	if (Array.isArray(shapes)) {
		for (let i = 0, l = shapes.length; i < l; i++) {
			const shape = shapes[i];

			data.shapes.push(shape.uuid);
		}
	} else {
		data.shapes.push(shapes.uuid);
	}

	return data;
}

export { ShapeGeometry, ShapeGeometry as ShapeBufferGeometry };