-
Notifications
You must be signed in to change notification settings - Fork 3
/
threefuncs.js
119 lines (117 loc) · 4.38 KB
/
threefuncs.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
function CPath(points, thin){
//console.log("Path from");
//console.log(points);
var v;
var rectShape = new THREE.Shape();
moves = [[0,0]];
for(var i=1; i<points.length; i++){
v = simpleperp(diff2d(points[i], points[i-1]), -thin);
moves.push(v);
}
moves[0] = moves[1];
rectShape.moveTo(points[0][0]-moves[0][0], points[0][1]-moves[0][1]);
for(var i=1; i<points.length; i++){
rectShape.lineTo(points[i][0]-moves[i][0], points[i][1]-moves[i][1]);
}
for(var i=points.length-1; i>-1; i--){
rectShape.lineTo(points[i][0]+moves[i][0], points[i][1]+moves[i][1]);
}
rectShape.lineTo(points[0][0]-moves[0][0], points[0][1]-moves[0][1]);
return rectShape;
}
function CLine(points){
var geometry = new THREE.Geometry();
for(var i=0; i<points.length; i++){
geometry.vertices.push(new THREE.Vector3( points[i][0], points[i][1], 0 ));
}
geometry.computeLineDistances();
geometry.computeBoundingSphere();
geometry.computeBoundingBox();
return geometry;
}
function CBorder(points){
var thinu, thinb, _thin = 1;
var geometry = new THREE.Geometry();
for(var i=0; i<points.length-1; i++){
thinu = (i%2) * _thin;
thinb = ((i+1)%2) * _thin;
geometry.vertices.push(new THREE.Vector3( points[i][0], points[i][1], thinu ));
geometry.vertices.push(new THREE.Vector3( points[i+1][0], points[i+1][1], thinu ));
geometry.vertices.push(new THREE.Vector3( points[i][0], points[i][1], thinb ));
geometry.faces.push( new THREE.Face3( i*3+0, i*3+1, i*3+2 ) );
}
geometry.computeBoundingSphere();
geometry.computeBoundingBox();
geometry.computeFaceNormals();
console.log(geometry);
return geometry;
}
function CShape(points){
//console.log("Shape from");
//console.log(points);
var rectShape = new THREE.Shape();
rectShape.moveTo(points[0][0], points[0][1]);
for(var i=1; i<points.length; i++){
rectShape.lineTo(points[i][0], points[i][1]);
}
return rectShape;
}
function CMesh(shapes, color){
var rectGeom = new THREE.ShapeGeometry( shapes );
var rectMesh = new THREE.Mesh( rectGeom, new THREE.MeshBasicMaterial( { color: color } ) ) ;
return rectMesh;
}
function CExtr(shapes, height, color, lvl){
console.log("Extrude to "+lvl+" lvl");
var vcs = [new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, parseFloat(lvl) )];
var curve = new THREE.SplineCurve3(vcs);
var rectGeom = new THREE.ExtrudeGeometry( shapes, {
steps: 1, size: 0.00001, amount: lvl, curveSegments: 1,
bevelThickness: 0, bevelSize: 0.0, bevelEnabled: false,
material: 1, extrudeMaterial: 0});
var rectMesh = new THREE.Mesh( rectGeom, new THREE.MeshPhongMaterial( { color: color } ) ) ;
rectMesh.matrixAutoUpdate = false;
rectMesh.updateMatrix();
return rectMesh;
}
function CText(text, color){
var shapes = THREE.FontUtils.generateShapes( text, {
font: "liberation sans",
weight: "normal",
size: 0.38
} );
var geom = new THREE.ShapeGeometry( shapes );
var mat = new THREE.MeshBasicMaterial({color: color});
return new THREE.Mesh( geom, mat );
}
function CTexturedText(text, color){
var bitmap = document.createElement('canvas');
var ctx = bitmap.getContext('2d');
bitmap.height = 32*4;
bitmap.width = bitmap.height*text.length;
/*ctx.width = ctx.width;
ctx.translate(0, bitmap.height);
ctx.scale(1, -1);*/
ctx.font = "40px 'Helvetica'";
var h = bitmap.height/4;
var w = bitmap.width/4;
ctx.rect(0, 0, bitmap.width, bitmap.height);
ctx.fillStyle = "rgba(255, 120, 240, 0.0)";;
ctx.fill();
ctx.fillStyle = '#330055';
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.scale(1,1);
ctx.fillText(text, bitmap.width/2, bitmap.height/2);
ctx.strokeStyle = '#ffffff';
ctx.strokeText(text, bitmap.width/2, bitmap.height/2);
var texture = new THREE.Texture(bitmap);
texture.wrapS = texture.wrapT = THREE.ClampToEdgeWrapping;
texture.needsUpdate = true;
texture.anisotropy = anisotropy;
console.log("anisotropy == "+anisotropy);
var rectGeom = new THREE.PlaneGeometry(w, h);
var rectMesh = new THREE.Mesh( rectGeom, new THREE.MeshPhongMaterial( { color: 0xfffeed, map: texture, transparent: true } ) ) ;
delete ctx; delete bitmap;
return rectMesh;
}