-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
75 lines (61 loc) · 3.02 KB
/
main.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
var Window = require('pex-sys/Window');
var PerspCamera = require('pex-cam/PerspCamera');
var Arcball = require('pex-cam/Arcball');
var createCube = require('primitive-cube');
var glslify = require('glslify-promise');
var isBrowser = require('is-browser');
var GUI = require('pex-gui');
var Color = require('pex-color');
var createGradient = require('./create-gradient');
Window.create({
settings: {
width: 1280,
height: 720,
fullScreen: isBrowser ? true : false
},
resources: {
showNormalsVert: { glsl: glslify(__dirname + '/assets/ShowNormals.vert') },
showNormalsFrag: { glsl: glslify(__dirname + '/assets/ShowNormals.frag') },
},
init: function() {
var ctx = this.getContext();
var res = this.getResources();
this.gui = new GUI(ctx, this.getWidth(), this.getHeight());
this.camera = new PerspCamera(45,this.getAspectRatio(),0.001,20.0);
this.camera.lookAt([0, 1, 3], [0, 0, 0]);
ctx.setProjectionMatrix(this.camera.getProjectionMatrix());
this.arcball = new Arcball(this.camera, this.getWidth(), this.getHeight());
this.arcball.setDistance(3.0);
this.addEventListener(this.arcball);
this.showNormalsProgram = ctx.createProgram(res.showNormalsVert, res.showNormalsFrag);
ctx.bindProgram(this.showNormalsProgram);
var cube = createCube();
var cubeAttributes = [
{ data: cube.positions, location: ctx.ATTRIB_POSITION },
{ data: cube.normals, location: ctx.ATTRIB_NORMAL }
];
var cubeIndices = { data: cube.cells };
this.cubeMesh = ctx.createMesh(cubeAttributes, cubeIndices, ctx.TRIANGLES);
var colors1 = ['#FF0000', '#00FF00', '#0000FF'].map(Color.fromHex);
this.gui.addTexture2D('Gradient', ctx.createTexture2D(createGradient(colors1, 256, 64)));
this.gui.addTexture2D('Gradient correct gamma', ctx.createTexture2D(createGradient(colors1, 256, 64, true)));
var colors2 = ['#3031FF', '#AC38FE', '#F9252B', '#FEF63D'].map(Color.fromHex);
this.gui.addTexture2D('Gradient', ctx.createTexture2D(createGradient(colors2, 256, 64)));
this.gui.addTexture2D('Gradient correct gamma', ctx.createTexture2D(createGradient(colors2, 256, 64, true)));
var colors3 = ['#32BB67', '#FDD542', '#FE2F39'].map(Color.fromHex);
this.gui.addTexture2D('Gradient', ctx.createTexture2D(createGradient(colors3, 256, 64)));
this.gui.addTexture2D('Gradient correct gamma', ctx.createTexture2D(createGradient(colors3, 256, 64, true)));
},
draw: function() {
var ctx = this.getContext();
this.arcball.apply();
ctx.setViewMatrix(this.camera.getViewMatrix());
ctx.setClearColor(0.2, 0.2, 0.2, 1);
ctx.clear(ctx.COLOR_BIT | ctx.DEPTH_BIT);
ctx.setDepthTest(true);
ctx.bindProgram(this.showNormalsProgram);
ctx.bindMesh(this.cubeMesh);
ctx.drawMesh();
this.gui.draw();
}
})