-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (110 loc) · 3.79 KB
/
app.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
var express = require('express')
, http = require('http')
, path = require('path')
, THREE = require('three.js')
, helvetiker = require('./src/helvetiker_regular.typeface.js');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function() {
app.use(express.errorHandler());
});
app.get('/text/:msg', function(req, res) {
if (!req || !req.query || !res) return;
var msg = req.param("msg") || "error";
///// BEGIN MAGIC NUMBERS /////
var adjustment = 1; // times depth of text (added to distance to camera).
var fieldOfView = 90; // degrees
var altitudeRadians = ((180 - fieldOfView) / 2) * Math.PI / 180; // rads
var altitudeFactor = Math.abs(Math.tan(altitudeRadians)); // helper factor to calculate camera height
var textColor = Math.random() * 0xFFFFFF;
var backgroundColor = 0xFFFFFF ^ textColor; // Set to complement of textColor.
var backgroundAlpha = 1;
// Camera settings.
var near = 1; // px
var far = 100000; // px
var width = 1280; // px
var height = 720; // px
///// END MAGIC NUMBERS /////
var camera = new THREE.PerspectiveCamera(fieldOfView, width / height, near, far);
var scene = new THREE.Scene();
var renderer = new THREE.CanvasRenderer();
renderer.setSize(width, height);
renderer.setClearColorHex(backgroundColor, backgroundAlpha);
renderer.clear();
// Load the font so we can calculate geometry.
THREE.FontUtils.loadFace(helvetiker);
var text3dOptions = {
size: 100,
height: 50,
curveSegments: 10,
font: "helvetiker",
weight: "normal",
style: "normal",
bevelEnabled: true,
bevelThickness: 1,
bevelSize: 2
};
var text3d = new THREE.TextGeometry( msg, text3dOptions );
text3d.computeBoundingBox();
// Calculate text and camera placement from bounding box.
var textLength = ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
var textHeight = ( text3d.boundingBox.max.y - text3d.boundingBox.min.x );
var textDepth = ( text3d.boundingBox.max.z - text3d.boundingBox.min.z );
// Offsets to center text in world.
var textOffsets = {
x: -0.5 * textLength,
y: -0.5 * textHeight,
z: -0.5 * textDepth
};
// Create, texture, and position text.
var textMaterial = new THREE.MeshLambertMaterial({
color: textColor,
overdraw: true
});
var text = new THREE.Mesh( text3d, textMaterial );
text.position.x = textOffsets.x;
text.position.y = textOffsets.y;
text.position.z = textOffsets.z;
text.rotation.x = 0;
text.rotation.y = 0;
text.rotation.z = 0;
console.log("text offsets: ")
console.log(textOffsets);
scene.add( text );
// Set the camera distance to the apex of the triange between the ends.
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = altitudeFactor * textLength / 2 + textDepth * adjustment;
camera.rotation.x = 0;
camera.rotation.y = 0;
camera.rotation.z = 0;//Math.PI / 2;
console.log("eye position: ");
console.log(camera.position);
// Add lighting.
var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
directionalLight.position.set (
camera.position.x - textOffsets.x,
camera.position.y - textOffsets.y,
camera.position.z - textOffsets.z
);
scene.add( directionalLight );
// Render canvas as image and send to the client.
renderer.render(scene, camera);
renderer.domElement.toBuffer(function(err, buf) {
res.contentType('image/jpg');
res.send(buf);
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});