-
Notifications
You must be signed in to change notification settings - Fork 14
/
IKRS.ShapedPathGeometry.js
186 lines (132 loc) · 5.44 KB
/
IKRS.ShapedPathGeometry.js
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
/**
* @author Ikaros Kappler
* @date 2013-08-22
* @version 1.0.0
**/
IKRS.ShapedPathGeometry = function( shape,
path,
shapedPath,
options
) {
// Call super 'constructor'
THREE.Geometry.call( this );
var MAX_BEND = Math.PI;
// Fetch the points from the shape
var shapePoints = shape.extractAllPoints();
shape = shapePoints.shape;
if( !options.pathBend )
options.pathBend = Math.PI/4.0; // 45 degrees for testing:)
//var bendRatio = options.pathBend / MAX_BEND;
var shapedPathBounds = shapedPath.computeBoundingBox();
// Iterate through path elements in n steps
var vertexCount = 0;
// Used to determine the 'tangent' along the path
var lastPathPoint = null;
var pathTangent = null;
var pathTangentSlope = 0.0;
for( var i = 0; i <= options.curveSegments; i++ ) {
var t = i / options.curveSegments;
// This is a Vector2 (x,y)
var pathPoint = path.getPointAt( t );
if( i == 0 ) pathTangent = new THREE.Vector2( 0, 0 ); // No slope at first level
else pathTangent = new THREE.Vector2( pathPoint.x-lastPathPoint.x, pathPoint.y-lastPathPoint.y );
// Calculate slope from tangent
if( pathTangent.x == 0 ) pathTangentSlope = -Math.PI/2.0; // 90 deg
else pathTangentSlope = Math.atan( pathTangent.y/pathTangent.x );
//window.alert( "pathTangentSlope=" + ((pathTangentSlope/Math.PI)*180) + " DEG (" + pathTangentSlope + " RAD)" );
// Don't forget to make a path curve by 'options.pathBend'
var pathBendAmount = options.pathBend*t;
//pathPoint.x += Math.cos(pathBendAmount) * options.size;
//if( !pathPoint )
// window.alert( "pathPoint#" + i + " (t=" + t+ "): " + JSON.stringify(pathPoint) );
var shapedPathPoint = shapedPath.getPoint( t );
var radiusFactor = (shapedPathBounds.getXMax() - shapedPathPoint.x) / shapedPathBounds.getWidth();
var heightFactor = (shapedPathBounds.getYMax() - shapedPathPoint.y) / shapedPathBounds.getHeight();
//window.alert( "t=" + t + ", shapedPathPoint=(" + shapedPathPoint.x + ", " + shapedPathPoint.y + "), radiusFactor=" + radiusFactor );
//for( var s = 0; s < shape.length; s++ ) {
var firstShapePointIndex = vertexCount;
for( var s in shape ) {
var shapePoint2 = shapePoints.shape[s];
var shapePoint3 = new THREE.Vector3( shapePoint2.x,
shapePoint2.y,
0 // t*options.size
);
//pathTangentSlope += Math.PI/2.0;
/*
var shapePoint3 = new THREE.Vector3( Math.sin(-pathTangentSlope) * shapePoint2.x * radiusFactor, // Math.sin(-pathTangentSlope) * shapePoint2.x,
shapePoint2.y * radiusFactor,
Math.cos(-pathTangentSlope) * shapePoint2.x * radiusFactor //0 //Math.cos(-pathTangentSlope) * shapePoint2.x
);
*/
var shapeBend = options.pathBend * heightFactor;
// var zBendAmount = options.pathBend * heightFactor;
// Get the (x,y) point on the shape path (sould be a bezier curve)
// - use x as the radius factor
// - use y as the height factor
shapePoint3.multiplyScalar( radiusFactor );
shapePoint3.add( new THREE.Vector3( pathPoint.x, // Math.cos(pathBendAmount) * options.size,
pathPoint.y, // pathPoint.y,
- options.size/2.0 + heightFactor*options.size
)
); // addSelf instead of add?!
// Add path point?
// this.vertices.push( new THREE.Vertex(shapePoint3) );
// ... Vertex was replaced by Vector3 (Vertex is DEPRECATED!)
this.vertices.push( new THREE.Vector3( shapePoint3.x,
shapePoint3.y,
shapePoint3.z
)
);
// Connect previous shape/level with current?
if( i > 0 ) {
var soffset = (s==0) ? shape.length-1 : -1;
// Triangulate?
if( !options.triangulate ) {
this.faces.push( new THREE.Face4( vertexCount + soffset,
vertexCount,
vertexCount-shape.length,
vertexCount-shape.length + soffset
) );
} else {
// Triangulation=on
// -> add two Face3 facets instead of Face4!
// (Otherwise the STL export will fail as it only recognizes Face3)
this.faces.push( new THREE.Face3( vertexCount + soffset,
vertexCount,
vertexCount-shape.length
) );
this.faces.push( new THREE.Face3( vertexCount-shape.length,
vertexCount-shape.length + soffset,
vertexCount + soffset
) );
} // END else [triangulate]
}
// Close first and last shape/level (if at least 3 vertices are present: s > 1)
if( s > 1 ) {
if( i == options.curveSegments ) {
// Last segment
this.faces.push( new THREE.Face3( vertexCount,
vertexCount-1,
firstShapePointIndex
)
);
} else if( i == 0 ) {
// First segment
this.faces.push( new THREE.Face3( firstShapePointIndex,
vertexCount-1,
vertexCount
)
);
}
}
vertexCount++;
lastPathPoint = pathPoint;
} // END for [shape points]
}
this.computeCentroids();
this.computeFaceNormals();
// return new THREE.ExtrudeGeometry( shape, options );
};
IKRS.ShapedPathGeometry.prototype = new THREE.Geometry();
IKRS.ShapedPathGeometry.prototype.constructor = IKRS.ShapedPathGeometry;
// window.alert( "IKRS.ShapedPathGeometry=" + IKRS.ShapedPathGeometry );