pgl 0.0.6 → 5.0.0-alpha.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/LICENSE.md +20 -0
- package/README.md +46 -11
- package/dist/cli-run.d.ts +3 -0
- package/dist/cli-run.d.ts.map +1 -0
- package/dist/cli-run.js +7 -0
- package/dist/cli-run.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/fwd/@dataplan/json/index.d.ts +1 -0
- package/fwd/@dataplan/json/index.js +1 -0
- package/fwd/@dataplan/pg/adaptors/pg/index.d.ts +1 -0
- package/fwd/@dataplan/pg/adaptors/pg/index.js +1 -0
- package/fwd/@dataplan/pg/index.d.ts +1 -0
- package/fwd/@dataplan/pg/index.js +1 -0
- package/fwd/grafast/envelop/index.d.ts +1 -0
- package/fwd/grafast/envelop/index.js +1 -0
- package/fwd/grafast/graphql/index.d.ts +1 -0
- package/fwd/grafast/graphql/index.js +1 -0
- package/fwd/grafast/index.d.ts +1 -0
- package/fwd/grafast/index.js +1 -0
- package/fwd/grafserv/express/v4/index.d.ts +1 -0
- package/fwd/grafserv/express/v4/index.js +1 -0
- package/fwd/grafserv/fastify/v4/index.d.ts +1 -0
- package/fwd/grafserv/fastify/v4/index.js +1 -0
- package/fwd/grafserv/index.d.ts +1 -0
- package/fwd/grafserv/index.js +1 -0
- package/fwd/grafserv/koa/v2/index.d.ts +1 -0
- package/fwd/grafserv/koa/v2/index.js +1 -0
- package/fwd/grafserv/node/index.d.ts +1 -0
- package/fwd/grafserv/node/index.js +1 -0
- package/fwd/grafserv/ruru/cli/index.d.ts +1 -0
- package/fwd/grafserv/ruru/cli/index.js +1 -0
- package/fwd/grafserv/ruru/index.d.ts +1 -0
- package/fwd/grafserv/ruru/index.js +1 -0
- package/fwd/grafserv/ruru/server/index.d.ts +1 -0
- package/fwd/grafserv/ruru/server/index.js +1 -0
- package/fwd/graphile-build/index.d.ts +2 -0
- package/fwd/graphile-build/index.js +1 -0
- package/fwd/graphile-build-pg/index.d.ts +1 -0
- package/fwd/graphile-build-pg/index.js +1 -0
- package/fwd/graphile-build-pg/pg-introspection/index.d.ts +1 -0
- package/fwd/graphile-build-pg/pg-introspection/index.js +1 -0
- package/fwd/pg-sql2/index.d.ts +1 -0
- package/fwd/pg-sql2/index.js +1 -0
- package/fwd/postgraphile/presets/amber/index.d.ts +1 -0
- package/fwd/postgraphile/presets/amber/index.js +1 -0
- package/fwd/postgraphile/presets/relay/index.d.ts +1 -0
- package/fwd/postgraphile/presets/relay/index.js +1 -0
- package/fwd/postgraphile/presets/v4/index.d.ts +1 -0
- package/fwd/postgraphile/presets/v4/index.js +1 -0
- package/fwd/postgraphile/utils/index.d.ts +1 -0
- package/fwd/postgraphile/utils/index.js +1 -0
- package/fwd/tamedevil/index.d.ts +1 -0
- package/fwd/tamedevil/index.js +1 -0
- package/package.json +202 -12
- package/.npmignore +0 -2
- package/LICENSE +0 -21
- package/examples/cube.js +0 -97
- package/examples/points.js +0 -82
- package/examples/shaders/flat-surface.fshader +0 -5
- package/examples/shaders/flat-surface.vshader +0 -72
- package/examples/shaders/matrices.fshader +0 -5
- package/examples/shaders/matrices.vshader +0 -12
- package/examples/shaders/model-and-view.fshader +0 -5
- package/examples/shaders/model-and-view.vshader +0 -23
- package/examples/shaders/pgl.fshader +0 -5
- package/examples/shaders/pgl.vshader +0 -53
- package/examples/shaders/simple.fshader +0 -5
- package/examples/shaders/simple.vshader +0 -8
- package/index.js +0 -25
- package/lib/camera.js +0 -222
- package/lib/geom.js +0 -256
- package/lib/light.js +0 -27
- package/lib/line.js +0 -10
- package/lib/mesh.js +0 -266
- package/lib/pgl-utils.js +0 -76
- package/lib/plask-utils.js +0 -46
- package/lib/renderer.js +0 -418
- package/lib/scene.js +0 -167
- package/lib/triangle.js +0 -73
- package/lib/vertex.js +0 -17
- package/test/index.js +0 -53
package/examples/cube.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
var plask = require('plask');
|
|
2
|
-
var pgl = require('../index');
|
|
3
|
-
var util = require('util');
|
|
4
|
-
|
|
5
|
-
plask.simpleWindow({
|
|
6
|
-
settings: {
|
|
7
|
-
width: 640,
|
|
8
|
-
height: 480,
|
|
9
|
-
type: '3d',
|
|
10
|
-
vsync: true,
|
|
11
|
-
multisample: true
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
init : function() {
|
|
15
|
-
|
|
16
|
-
this.framerate(60);
|
|
17
|
-
|
|
18
|
-
var gl = this.gl;
|
|
19
|
-
var self = this;
|
|
20
|
-
|
|
21
|
-
pgl.renderer.create({
|
|
22
|
-
gl : gl,
|
|
23
|
-
width : this.width,
|
|
24
|
-
height : this.height,
|
|
25
|
-
shaderPath : __dirname + '/shaders',
|
|
26
|
-
shaderName : 'flat-surface'
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
pgl.scene.addLight({
|
|
30
|
-
position : { x : -10, y : 0, z : 0 }
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
pgl.scene.addCamera({
|
|
34
|
-
width : this.width,
|
|
35
|
-
height : this.height,
|
|
36
|
-
position : new plask.Vec3(0, 1, 10) // move the camera on the positve z axis
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
var cube = new pgl.mesh({
|
|
40
|
-
geometry : new pgl.geom.cube(),
|
|
41
|
-
scene : pgl.scene,
|
|
42
|
-
name : 'cube', // generate
|
|
43
|
-
position : new plask.Vec3(0, 0, 0),
|
|
44
|
-
picked : true
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
pgl.scene.addMesh( cube );
|
|
48
|
-
pgl.scene.addFloor();
|
|
49
|
-
pgl.scene.addAxes();
|
|
50
|
-
|
|
51
|
-
var points = new pgl.mesh({
|
|
52
|
-
geometry : new pgl.geom.points(),
|
|
53
|
-
scene : pgl.scene,
|
|
54
|
-
name : 'points',
|
|
55
|
-
position : new plask.Vec3(0, 0, 0),
|
|
56
|
-
pointSize : 3.0
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
points.addPoint({
|
|
60
|
-
position : new plask.Vec3(1, 2.0, 1),
|
|
61
|
-
material : {
|
|
62
|
-
diffuse : new plask.Vec3(1.0, 0.0, 0.0)
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
pgl.scene.addMesh( points );
|
|
67
|
-
|
|
68
|
-
setTimeout(function() {
|
|
69
|
-
cube.addTriangle({
|
|
70
|
-
vertices : [1, 1, 1,-1, -1, -1, 1, 2, 1],
|
|
71
|
-
material : {
|
|
72
|
-
ambient : new plask.Vec3(1.0, 0.0, 0.0),
|
|
73
|
-
diffuse : new plask.Vec3(1.0, 0.0, 0.0)
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}, 1000);
|
|
77
|
-
|
|
78
|
-
var mouseEvents = ['leftMouseDown', 'leftMouseUp', 'leftMouseDragged', 'scrollWheel'];
|
|
79
|
-
|
|
80
|
-
mouseEvents.forEach(function(mouseEvent) {
|
|
81
|
-
self.on(mouseEvent, function(e) {
|
|
82
|
-
pgl.scene.handleMouse(e);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
this.on('keyUp', function(e) {
|
|
87
|
-
pgl.scene.handleKey(e);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
this.pgl = pgl;
|
|
91
|
-
},
|
|
92
|
-
|
|
93
|
-
draw : function() {
|
|
94
|
-
var pgl = this.pgl;
|
|
95
|
-
pgl.renderer.render( pgl.scene );
|
|
96
|
-
}
|
|
97
|
-
});
|
package/examples/points.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
var plask = require('plask');
|
|
2
|
-
var pgl = require('../index');
|
|
3
|
-
var util = require('util');
|
|
4
|
-
|
|
5
|
-
plask.simpleWindow({
|
|
6
|
-
settings: {
|
|
7
|
-
width: 640,
|
|
8
|
-
height: 480,
|
|
9
|
-
type: '3d',
|
|
10
|
-
vsync: true,
|
|
11
|
-
multisample: true
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
init : function() {
|
|
15
|
-
|
|
16
|
-
this.framerate(60);
|
|
17
|
-
|
|
18
|
-
var gl = this.gl;
|
|
19
|
-
var self = this;
|
|
20
|
-
|
|
21
|
-
pgl.renderer.create({
|
|
22
|
-
gl : gl,
|
|
23
|
-
width : this.width,
|
|
24
|
-
height : this.height,
|
|
25
|
-
shaderPath : __dirname + '/shaders',
|
|
26
|
-
shaderName : 'flat-surface'
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
pgl.scene.addLight({
|
|
30
|
-
position : { x : -10, y : 0, z : 0 }
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
pgl.scene.addCamera({
|
|
34
|
-
width : this.width,
|
|
35
|
-
height : this.height,
|
|
36
|
-
position : new plask.Vec3(0, 1, 10) // move the camera on the positve z axis
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
pgl.scene.addFloor();
|
|
40
|
-
pgl.scene.addAxes();
|
|
41
|
-
|
|
42
|
-
var points = new pgl.mesh({
|
|
43
|
-
geometry : new pgl.geom.points(),
|
|
44
|
-
scene : pgl.scene,
|
|
45
|
-
name : 'points',
|
|
46
|
-
position : new plask.Vec3(0, 0, 0),
|
|
47
|
-
pointSize : 2.0
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
pgl.scene.addMesh( points );
|
|
51
|
-
|
|
52
|
-
var inc = 0;
|
|
53
|
-
setInterval(function() {
|
|
54
|
-
inc += 0.1;
|
|
55
|
-
points.addPoint({
|
|
56
|
-
position : new plask.Vec3(inc, inc, inc),
|
|
57
|
-
material : {
|
|
58
|
-
diffuse : new plask.Vec3(1.0, 0.0, 0.0)
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}, 1000);
|
|
62
|
-
|
|
63
|
-
var mouseEvents = ['leftMouseDown', 'leftMouseUp', 'leftMouseDragged', 'scrollWheel'];
|
|
64
|
-
|
|
65
|
-
mouseEvents.forEach(function(mouseEvent) {
|
|
66
|
-
self.on(mouseEvent, function(e) {
|
|
67
|
-
pgl.scene.handleMouse(e);
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
this.on('keyUp', function(e) {
|
|
72
|
-
pgl.scene.handleKey(e);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
this.pgl = pgl;
|
|
76
|
-
},
|
|
77
|
-
|
|
78
|
-
draw : function() {
|
|
79
|
-
var pgl = this.pgl;
|
|
80
|
-
pgl.renderer.render( pgl.scene );
|
|
81
|
-
}
|
|
82
|
-
});
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
// Adapted from fss shader
|
|
2
|
-
// Lambert Reflection
|
|
3
|
-
// https://github.com/wagerfield/flat-surface-shader/blob/master/README.md
|
|
4
|
-
|
|
5
|
-
// Per vertex attributes
|
|
6
|
-
attribute vec3 aVertexPosition;
|
|
7
|
-
attribute vec3 aVertexCentroid;
|
|
8
|
-
attribute vec3 aVertexNormal;
|
|
9
|
-
|
|
10
|
-
attribute vec3 aAmbient;
|
|
11
|
-
attribute vec3 aDiffuse;
|
|
12
|
-
|
|
13
|
-
// Shader uniforms
|
|
14
|
-
// uniform mat4 uMMatrix;
|
|
15
|
-
// uniform mat4 uVMatrix;
|
|
16
|
-
|
|
17
|
-
uniform mat4 uMVMatrix;
|
|
18
|
-
uniform mat3 uNMatrix;
|
|
19
|
-
|
|
20
|
-
uniform mat4 uPMatrix;
|
|
21
|
-
|
|
22
|
-
uniform vec3 uLightPosition;
|
|
23
|
-
uniform vec3 uLightAmbient;
|
|
24
|
-
uniform vec3 uLightDiffuse;
|
|
25
|
-
|
|
26
|
-
uniform bool uWireframe;
|
|
27
|
-
uniform bool uPerVertexColor;
|
|
28
|
-
|
|
29
|
-
uniform bool uPointMesh;
|
|
30
|
-
uniform float uPointSize;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
// Pass through to the fragment shader
|
|
34
|
-
varying vec4 vColor;
|
|
35
|
-
|
|
36
|
-
void main() {
|
|
37
|
-
if(uWireframe) {
|
|
38
|
-
vColor = vec4(aDiffuse, 1.0);
|
|
39
|
-
} else if(uPointMesh) {
|
|
40
|
-
vColor = vec4(aDiffuse, 1.0);
|
|
41
|
-
gl_PointSize = uPointSize;
|
|
42
|
-
} else {
|
|
43
|
-
// Create color
|
|
44
|
-
vColor = vec4(0.0);
|
|
45
|
-
|
|
46
|
-
// Set the light
|
|
47
|
-
vec3 lightPosition = uLightPosition;
|
|
48
|
-
vec4 lightAmbient = vec4(uLightAmbient, 1.0);
|
|
49
|
-
vec4 lightDiffuse = vec4(uLightDiffuse, 1.0);
|
|
50
|
-
|
|
51
|
-
// Transform the normal and centroid
|
|
52
|
-
vec3 normalEye = -normalize(uNMatrix * aVertexNormal);
|
|
53
|
-
vec3 centroidEye = normalize(uNMatrix * aVertexCentroid);
|
|
54
|
-
|
|
55
|
-
// Calculate illuminance
|
|
56
|
-
vec3 ray = normalize(lightPosition - centroidEye);
|
|
57
|
-
float illuminance = dot(normalEye, ray);
|
|
58
|
-
|
|
59
|
-
// Calculate ambient light
|
|
60
|
-
vColor += vec4(aAmbient, 1.0) * lightAmbient;
|
|
61
|
-
|
|
62
|
-
// Calculate diffuse light
|
|
63
|
-
vColor += vec4(aDiffuse, 1.0) * lightDiffuse * illuminance;
|
|
64
|
-
|
|
65
|
-
// vColor = vec4(aDiffuse, 1.0);
|
|
66
|
-
|
|
67
|
-
// Clamp color
|
|
68
|
-
vColor = clamp(vColor, 0.0, 1.0);
|
|
69
|
-
}
|
|
70
|
-
// Set position
|
|
71
|
-
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
|
|
72
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
attribute vec3 aVertexPosition;
|
|
2
|
-
attribute vec3 aVertexColor;
|
|
3
|
-
|
|
4
|
-
uniform mat4 uMVMatrix;
|
|
5
|
-
uniform mat4 uPMatrix;
|
|
6
|
-
|
|
7
|
-
varying vec4 vColor;
|
|
8
|
-
|
|
9
|
-
void main() {
|
|
10
|
-
vColor = vec4(aVertexColor, 1.0);
|
|
11
|
-
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
|
|
12
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
attribute vec3 aVertexPosition;
|
|
2
|
-
attribute vec3 aAmbient;
|
|
3
|
-
attribute vec3 aDiffuse;
|
|
4
|
-
|
|
5
|
-
attribute vec3 aVertexNormal;
|
|
6
|
-
attribute vec3 aVertexCentroid;
|
|
7
|
-
|
|
8
|
-
uniform mat4 umMatrix;
|
|
9
|
-
uniform mat4 uvMatrix;
|
|
10
|
-
|
|
11
|
-
uniform mat4 uPMatrix;
|
|
12
|
-
|
|
13
|
-
varying vec4 vColor;
|
|
14
|
-
|
|
15
|
-
void main() {
|
|
16
|
-
|
|
17
|
-
// vColor = vec4(aAmbient, 1.0);
|
|
18
|
-
// vColor = vec4(aDiffuse, 1.0);
|
|
19
|
-
vColor = vec4(aDiffuse, 1.0);
|
|
20
|
-
vec3 norm = aVertexNormal;
|
|
21
|
-
vec3 cent = aVertexCentroid;
|
|
22
|
-
gl_Position = uPMatrix * umMatrix * uvMatrix * vec4(aVertexPosition, 1.0);
|
|
23
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
// Adapted from fss shader
|
|
2
|
-
// Lambert Reflection
|
|
3
|
-
// https://github.com/wagerfield/flat-surface-shader/blob/master/README.md
|
|
4
|
-
|
|
5
|
-
// Per vertex attributes
|
|
6
|
-
attribute vec3 aVertexPosition;
|
|
7
|
-
attribute vec3 aVertexCentroid;
|
|
8
|
-
attribute vec3 aVertexNormal;
|
|
9
|
-
|
|
10
|
-
attribute vec4 aAmbient;
|
|
11
|
-
attribute vec4 aDiffuse;
|
|
12
|
-
|
|
13
|
-
// Shader uniforms
|
|
14
|
-
uniform mat4 uMVMatrix;
|
|
15
|
-
uniform mat4 uPMatrix;
|
|
16
|
-
uniform mat3 uNMatrix;
|
|
17
|
-
|
|
18
|
-
uniform vec3 uLightPosition;
|
|
19
|
-
uniform vec4 uLightAmbient;
|
|
20
|
-
uniform vec4 uLightDiffuse;
|
|
21
|
-
|
|
22
|
-
// Pass through to the fragment shader
|
|
23
|
-
varying vec4 vColor;
|
|
24
|
-
|
|
25
|
-
void main() {
|
|
26
|
-
// Create color
|
|
27
|
-
vColor = vec4(0.0);
|
|
28
|
-
|
|
29
|
-
// Set the light
|
|
30
|
-
vec3 lightPosition = uLightPosition;
|
|
31
|
-
vec4 lightAmbient = uLightAmbient;
|
|
32
|
-
vec4 lightDiffuse = uLightDiffuse;
|
|
33
|
-
|
|
34
|
-
// Transform the normal and centroid
|
|
35
|
-
vec3 normalEye = -normalize(uNMatrix * aVertexNormal);
|
|
36
|
-
vec3 centroidEye = normalize(uNMatrix * aVertexNormal);
|
|
37
|
-
|
|
38
|
-
// Calculate illuminance
|
|
39
|
-
vec3 ray = normalize(lightPosition - centroidEye);
|
|
40
|
-
float illuminance = dot(normalEye, ray);
|
|
41
|
-
|
|
42
|
-
// Calculate ambient light
|
|
43
|
-
vColor += aAmbient * lightAmbient;
|
|
44
|
-
|
|
45
|
-
// Calculate diffuse light
|
|
46
|
-
vColor += aDiffuse * lightDiffuse * illuminance;
|
|
47
|
-
|
|
48
|
-
// Clamp color
|
|
49
|
-
vColor = clamp(vColor, 0.0, 1.0);
|
|
50
|
-
|
|
51
|
-
// Set position
|
|
52
|
-
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
|
|
53
|
-
}
|
package/index.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
var plask = require('plask');
|
|
3
|
-
|
|
4
|
-
// Renderer sets up shaders
|
|
5
|
-
// retains the gl and mprogram objects
|
|
6
|
-
var renderer = require('./lib/renderer');
|
|
7
|
-
|
|
8
|
-
var mesh = require('./lib/mesh');
|
|
9
|
-
var events = require('events');
|
|
10
|
-
var geom = require('./lib/geom');
|
|
11
|
-
|
|
12
|
-
// A scene object needs to be created to set up EventEmitter inheritance
|
|
13
|
-
// The scene acts as an event emitter, the lights, cameras and meshes can trigger events
|
|
14
|
-
// on the scene to emit updates without direct binding.
|
|
15
|
-
var scene = require('./lib/scene');
|
|
16
|
-
var sceneGraph = new scene();
|
|
17
|
-
var pglUtils = require('./lib/pgl-utils');
|
|
18
|
-
|
|
19
|
-
module.exports = {
|
|
20
|
-
renderer : renderer,
|
|
21
|
-
mesh : mesh,
|
|
22
|
-
geom : geom,
|
|
23
|
-
scene : sceneGraph,
|
|
24
|
-
utils : pglUtils
|
|
25
|
-
};
|
package/lib/camera.js
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Camera object
|
|
3
|
-
* Responds directly to mouse drag increasing / decreasing azimuth or elevation accordingly
|
|
4
|
-
*
|
|
5
|
-
* @TODO
|
|
6
|
-
* + better way of dealing with params getting passed into the constructor
|
|
7
|
-
* + improve the track function, it's not really a tracking camera...
|
|
8
|
-
* + easing, lerping -> animation/simulation code
|
|
9
|
-
* + adjust zoom / perspective matrix when dollying, Hitchcock dolly zoom
|
|
10
|
-
* + Magic Mouse dollying - using dx as well?
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
var plask = require('plask');
|
|
14
|
-
|
|
15
|
-
function camera(params) {
|
|
16
|
-
|
|
17
|
-
var self = this;
|
|
18
|
-
//@TODO if params is an object with other params use it otherwise assume it's the scene object
|
|
19
|
-
|
|
20
|
-
if(!params) {
|
|
21
|
-
|
|
22
|
-
this.position = new plask.Vec3(0,0,0);
|
|
23
|
-
this.eye = new plask.Mat4();
|
|
24
|
-
this.perspMat = new plask.Mat4().perspective(45, 640 / 480, 0.1, 100.0);
|
|
25
|
-
|
|
26
|
-
} else {
|
|
27
|
-
|
|
28
|
-
this.scene = params.scene;
|
|
29
|
-
|
|
30
|
-
// Set a starting eye matrix from the scene.
|
|
31
|
-
this.eye = this.scene.eyeCurrent.dup();
|
|
32
|
-
|
|
33
|
-
// @TODO apply starting rotations to the correct axes
|
|
34
|
-
this.elevation = params.elevation || 0;
|
|
35
|
-
this.azimuth = params.azimuth || 0;
|
|
36
|
-
|
|
37
|
-
// Position vector
|
|
38
|
-
this.position = params.position || new plask.Vec3(0,0,0);
|
|
39
|
-
this.eye.translate(this.position.x, this.position.y, this.position.z);
|
|
40
|
-
|
|
41
|
-
// Projection matrix
|
|
42
|
-
this.perspMat = new plask.Mat4().perspective(45, params.width / params.height, 0.1, 100.0) || new plask.Mat4().perspective(45, 640 / 480, 0.1, 100.0);
|
|
43
|
-
|
|
44
|
-
// Set the centre of orbit for the camera
|
|
45
|
-
this.target = new plask.Vec3(0 ,0, 0);
|
|
46
|
-
|
|
47
|
-
// Emit the camera eye matrix, the scene keeps an up to date reference to this matrix and it
|
|
48
|
-
// is used in the renderer.
|
|
49
|
-
this.scene.emit('camera', { 'eye' : this.eye });
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Set up event handlers for the events coming through from the scene.
|
|
53
|
-
this.scene.on('mouse', function(e) {
|
|
54
|
-
|
|
55
|
-
// Key presses trigger interactions with objects / meshes in the scene
|
|
56
|
-
if(e.ctrl || e.shift) {
|
|
57
|
-
return;
|
|
58
|
-
} else if(e.cmd) {
|
|
59
|
-
// Move the target origin, look at target
|
|
60
|
-
self.track(e);
|
|
61
|
-
} else {
|
|
62
|
-
switch (e.type) {
|
|
63
|
-
case 'scrollWheel':
|
|
64
|
-
self.dolly(e);
|
|
65
|
-
break;
|
|
66
|
-
case 'leftMouseDown':
|
|
67
|
-
self.mouseDown(e);
|
|
68
|
-
break;
|
|
69
|
-
case 'leftMouseUp':
|
|
70
|
-
self.mouseUp(e);
|
|
71
|
-
break;
|
|
72
|
-
case 'leftMouseDragged':
|
|
73
|
-
self.orbit(e);
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
this.scene.on('key', function(e) {
|
|
80
|
-
switch(e.str) {
|
|
81
|
-
case 'r':
|
|
82
|
-
self.resetTarget();
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// Keep track of the mouse events
|
|
88
|
-
this.mouse = {
|
|
89
|
-
down : false,
|
|
90
|
-
x : null,
|
|
91
|
-
y : null
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// @TODO add smoothing / easing between values here
|
|
97
|
-
// for the magic mouse scrollWheeel has a dx
|
|
98
|
-
// @TODO adjust perspective when dollying for dolly zoom
|
|
99
|
-
camera.prototype.dolly = function(e) {
|
|
100
|
-
|
|
101
|
-
this.position.z = this.position.z + e.dy;
|
|
102
|
-
|
|
103
|
-
// Get an identity matrix
|
|
104
|
-
var newTrans = new plask.Mat4();
|
|
105
|
-
|
|
106
|
-
// Centre on the target location
|
|
107
|
-
newTrans.translate(this.target.x, this.target.y, this.target.z);
|
|
108
|
-
|
|
109
|
-
// Apply the rotations for elevation and azimuth changes
|
|
110
|
-
newTrans.rotate(this.azimuth, 0, 1, 0);
|
|
111
|
-
newTrans.rotate(this.elevation, 1, 0, 0);
|
|
112
|
-
|
|
113
|
-
// Translate to camera position
|
|
114
|
-
newTrans.translate(this.position.x, this.position.y, this.position.z);
|
|
115
|
-
|
|
116
|
-
// Set to the current eye matrix, and emit an event so that other objects can respond if necessary.
|
|
117
|
-
this.eye = newTrans;
|
|
118
|
-
this.scene.emit('camera', { 'eye' : this.eye });
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
camera.prototype.mouseDown = function(e) {
|
|
122
|
-
this.mouse.down = true;
|
|
123
|
-
this.mouse.x = e.x;
|
|
124
|
-
this.mouse.y = e.y;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
camera.prototype.orbit = function(e) {
|
|
128
|
-
|
|
129
|
-
if(!this.mouse.down) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
var diffX = e.x - this.mouse.x;
|
|
134
|
-
var diffY = e.y - this.mouse.y;
|
|
135
|
-
|
|
136
|
-
var sensitivity = -0.0001;
|
|
137
|
-
|
|
138
|
-
// e.dx and e.dy represent the acceleration in the respective direction
|
|
139
|
-
this.azimuth += (diffX * sensitivity) * Math.abs(e.dx);
|
|
140
|
-
this.elevation += (diffY * sensitivity) * Math.abs(e.dy);
|
|
141
|
-
|
|
142
|
-
var newTrans = new plask.Mat4();
|
|
143
|
-
|
|
144
|
-
// Centre on the target location
|
|
145
|
-
newTrans.translate(this.target.x, this.target.y, this.target.z);
|
|
146
|
-
|
|
147
|
-
// Apply the rotations to each axes
|
|
148
|
-
newTrans.rotate(this.azimuth, 0, 1, 0);
|
|
149
|
-
newTrans.rotate(this.elevation, 1, 0, 0);
|
|
150
|
-
|
|
151
|
-
// Translate to the camera position
|
|
152
|
-
newTrans.translate(this.position.x, this.position.y, this.position.z);
|
|
153
|
-
|
|
154
|
-
// Set the eye matrix, update the last mouse position and emit the new eye matrix as event data.
|
|
155
|
-
this.eye = newTrans;
|
|
156
|
-
this.mouse.x = e.x;
|
|
157
|
-
this.mouse.y = e.y;
|
|
158
|
-
this.scene.emit('camera', { 'eye' : this.eye });
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
// Not really a tracking camera just translation x and y
|
|
162
|
-
// Events aren't emitted if CMD key is pressed before mouse drag
|
|
163
|
-
// @TODO if rotated then the change in x might need to be applied to the z axis
|
|
164
|
-
// multiply by the inverse of the camera matrix to get world coords?
|
|
165
|
-
camera.prototype.track = function(e) {
|
|
166
|
-
|
|
167
|
-
if(!this.mouse.down) {
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
var diffX = e.x - this.mouse.x;
|
|
172
|
-
var diffY = e.y - this.mouse.y;
|
|
173
|
-
|
|
174
|
-
var sensitivity = 0.01;
|
|
175
|
-
|
|
176
|
-
var newTrans = new plask.Mat4();
|
|
177
|
-
|
|
178
|
-
this.target.x += diffX * sensitivity;
|
|
179
|
-
this.target.y += diffY * sensitivity;
|
|
180
|
-
|
|
181
|
-
// translate to centre on the target location
|
|
182
|
-
newTrans.translate(this.target.x, this.target.y, this.target.z);
|
|
183
|
-
|
|
184
|
-
// apply camera rotations
|
|
185
|
-
newTrans.rotate(this.azimuth, 0, 1, 0);
|
|
186
|
-
newTrans.rotate(this.elevation, 1, 0, 0);
|
|
187
|
-
|
|
188
|
-
// translate to camera position
|
|
189
|
-
newTrans.translate(this.position.x, this.position.y, this.position.z);
|
|
190
|
-
|
|
191
|
-
this.eye = newTrans;
|
|
192
|
-
this.mouse.x = e.x;
|
|
193
|
-
this.mouse.y = e.y;
|
|
194
|
-
|
|
195
|
-
this.scene.emit('camera', { 'eye' : this.eye });
|
|
196
|
-
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
camera.prototype.mouseUp = function() {
|
|
200
|
-
this.mouse.down = false;
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
camera.prototype.resetTarget = function() {
|
|
204
|
-
this.target = new plask.Vec3(0, 0, 0);
|
|
205
|
-
|
|
206
|
-
var newTrans = new plask.Mat4();
|
|
207
|
-
// translate to centre on the target location
|
|
208
|
-
newTrans.translate(this.target.x, this.target.y, this.target.z);
|
|
209
|
-
|
|
210
|
-
// apply camera rotations
|
|
211
|
-
newTrans.rotate(this.azimuth, 0, 1, 0);
|
|
212
|
-
newTrans.rotate(this.elevation, 1, 0, 0);
|
|
213
|
-
|
|
214
|
-
// translate to camera position
|
|
215
|
-
newTrans.translate(this.position.x, this.position.y, this.position.z);
|
|
216
|
-
|
|
217
|
-
this.eye = newTrans;
|
|
218
|
-
|
|
219
|
-
this.scene.emit('camera', { 'eye' : this.eye });
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
module.exports = camera;
|