-
Notifications
You must be signed in to change notification settings - Fork 14
/
_IKRS.PathDirectedExtrudeGeometry.js_BAK_20130829
184 lines (133 loc) · 5.34 KB
/
_IKRS.PathDirectedExtrudeGeometry.js_BAK_20130829
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
/**
* @author Ikaros Kappler
* @date 2013-08-26
* @version 1.0.0
**/
IKRS.PathDirectedExtrudeGeometry = 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();
// An object with
// - minX
// - minY
// - maxX
// - maxY
var pathBounds = path.getBoundingBox();
// 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 tSegment = i / options.curveSegments;
// This is a Vector2 (x,y)
var pathPoint = path.getPointAt( tSegment );
var shapedPathPoint = shapedPath.getPoint( tSegment );
var tHeight = Math.min( 1.0,
(shapedPathBounds.getYMax() - shapedPathPoint.y) / shapedPathBounds.getHeight()
);
//window.alert( "pathPoint=(" + pathPoint.x + ", " + pathPoint.y + ")" );
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 * tHeight; // tSegment;
//pathPoint.x += Math.cos(pathBendAmount) * options.size;
//if( !pathPoint )
// window.alert( "pathPoint#" + i + " (t=" + t+ "): " + JSON.stringify(pathPoint) );
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( Math.sin(-pathTangentSlope) * shapePoint2.x,
shapePoint2.y,
Math.cos(-pathTangentSlope) * shapePoint2.x
);
shapePoint3.multiplyScalar( radiusFactor );
// Translate along path
var pathHeightPoint = path.getPoint( tHeight );
shapePoint3.add( new THREE.Vector3( pathHeightPoint.x,
0,
pathHeightPoint.y
)
); // addSelf instead of add?!
// Add path point?
// ... 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 the mesh should be build hollow this is not yet the last segment
if( i == options.curveSegments && !options.hollow ) {
// 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.PathDirectedExtrudeGeometry.prototype = new THREE.Geometry();
IKRS.PathDirectedExtrudeGeometry.prototype.constructor = IKRS.PathDirectedExtrudeGeometry;
//IKRS.PathDirectedExtrudeGeometry.prototype.computePathBounds
// window.alert( "IKRS.ShapedPathGeometry=" + IKRS.ShapedPathGeometry );