forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeGeometry.js
More file actions
140 lines (87 loc) · 2.88 KB
/
ShapeGeometry.js
File metadata and controls
140 lines (87 loc) · 2.88 KB
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
import { Geometry } from '../core/Geometry';
import { Face3 } from '../core/Face3';
import { Vector3 } from '../math/Vector3';
import { ShapeUtils } from '../extras/ShapeUtils';
import { ExtrudeGeometry } from './ExtrudeGeometry';
/**
* @author jonobr1 / http://jonobr1.com
*
* Creates a one-sided polygonal geometry from a path shape. Similar to
* ExtrudeGeometry.
*
* parameters = {
*
* curveSegments: <int>, // number of points on the curves. NOT USED AT THE MOMENT.
*
* material: <int> // material index for front and back faces
* uvGenerator: <Object> // object that provides UV generator functions
*
* }
**/
function ShapeGeometry( shapes, options ) {
Geometry.call( this );
this.type = 'ShapeGeometry';
if ( Array.isArray( shapes ) === false ) shapes = [ shapes ];
this.addShapeList( shapes, options );
this.computeFaceNormals();
}
ShapeGeometry.prototype = Object.create( Geometry.prototype );
ShapeGeometry.prototype.constructor = ShapeGeometry;
/**
* Add an array of shapes to THREE.ShapeGeometry.
*/
ShapeGeometry.prototype.addShapeList = function ( shapes, options ) {
for ( var i = 0, l = shapes.length; i < l; i ++ ) {
this.addShape( shapes[ i ], options );
}
return this;
};
/**
* Adds a shape to THREE.ShapeGeometry, based on THREE.ExtrudeGeometry.
*/
ShapeGeometry.prototype.addShape = function ( shape, options ) {
if ( options === undefined ) options = {};
var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
var material = options.material;
var uvgen = options.UVGenerator === undefined ? ExtrudeGeometry.WorldUVGenerator : options.UVGenerator;
//
var i, l, hole;
var shapesOffset = this.vertices.length;
var shapePoints = shape.extractPoints( curveSegments );
var vertices = shapePoints.shape;
var holes = shapePoints.holes;
var reverse = ! ShapeUtils.isClockWise( vertices );
if ( reverse ) {
vertices = vertices.reverse();
// Maybe we should also check if holes are in the opposite direction, just to be safe...
for ( i = 0, l = holes.length; i < l; i ++ ) {
hole = holes[ i ];
if ( ShapeUtils.isClockWise( hole ) ) {
holes[ i ] = hole.reverse();
}
}
reverse = false;
}
var faces = ShapeUtils.triangulateShape( vertices, holes );
// Vertices
for ( i = 0, l = holes.length; i < l; i ++ ) {
hole = holes[ i ];
vertices = vertices.concat( hole );
}
//
var vert, vlen = vertices.length;
var face, flen = faces.length;
for ( i = 0; i < vlen; i ++ ) {
vert = vertices[ i ];
this.vertices.push( new Vector3( vert.x, vert.y, 0 ) );
}
for ( i = 0; i < flen; i ++ ) {
face = faces[ i ];
var a = face[ 0 ] + shapesOffset;
var b = face[ 1 ] + shapesOffset;
var c = face[ 2 ] + shapesOffset;
this.faces.push( new Face3( a, b, c, null, null, material ) );
this.faceVertexUvs[ 0 ].push( uvgen.generateTopUV( this, a, b, c ) );
}
};
export { ShapeGeometry };