-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
142 lines (115 loc) · 3.6 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
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
function init() {
document.onmousemove = e => {
let rect = canvas.getBoundingClientRect();
mouse.px = mouse.x;
mouse.py = mouse.y;
mouse.x = (e.x - rect.left);
mouse.y = (e.y - rect.top) ;
mouse.x = -1. + 2. * mouse.x / canvas.width;
mouse.y = 1. - 2. * mouse.y / canvas.height;
}
document.onmousedown = e => {
if (e.button == 1) {
pinned = true;
}
if (e.button == 0) {
mouse.down = true;
}
if (e.shiftKey) {
pinned = false;
unPinned = true;
}
}
document.onmouseup = e => {
currentPoint = false;
pinned = false;
unPinned = false;
mouse.down = false;
}
animate();
}
cloth = new Cloth(clothHeight, clothWidth);
cloth.createCloth();
cloth.getDefaultProfile();
try {
gl = canvas.getContext('webgl');
let EXT = gl.getExtension("OES_element_index_uint") ||
gl.getExtension("MOZ_OES_element_index_uint") ||
gl.getExtension("WEBKIT_OES_element_index_uint") ||
//gl.getExtension('WEBGL_multi_draw');
gl.clearColor(0.0, 0.0, 0.0, 0.0);
} catch (e) {
console.error("It does not appear your computer can support WebGL.");
}
let vertexShader = getShader(gl.VERTEX_SHADER, "vertex")
let fragmentShader = getShader(gl.FRAGMENT_SHADER, "fragment");
let program = initShaderProgram(vertexShader, fragmentShader);
gl.useProgram(program);
let indicesbuffer = gl.createBuffer();
let vertexbuffer = gl.createBuffer();
let colorBuffer = gl.createBuffer();
let a_PostionID = gl.getAttribLocation(program, "a_position");
let colorID = gl.getAttribLocation(program, "a_color");
let timeID = gl.getUniformLocation(program, "u_time");
initUniformParams();
let loadTime = Date.now();
let lastTime = loadTime;
let nbFrames = 0;
let render_mode = gl.LINES;
document.addEventListener("keypress", function (key) {
if (key.key == 'w') {
render_mode = render_mode == gl.LINES ? gl.POINT : gl.LINES;
} else if (key.key == 'g') {
gravity = gravity ? 0 : -9.81;
} else if (key.key == 'r') {
cloth = new Cloth(clothHeight, clothWidth);
cloth.createCloth();
cloth.getDefaultProfile();
initIndicesBuffer(render_mode, indicesbuffer, cloth.indices);
}
});
function render() {
let currentTime = Date.now();
nbFrames++;
if (currentTime - lastTime >= 1000.0) {
console.log(1000.0 / nbFrames + " ms/frame");
nbFrames = 0;
lastTime += 1000.0;
}
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
initVertexBuffer(a_PostionID, vertexbuffer, cloth.vertices);
initIndicesBuffer(render_mode, indicesbuffer, cloth.indices);
gl.uniform1f(timeID, (currentTime - loadTime) / 1000.0);
gl.flush();
}
function draggedPoint() {
if (currentPoint && pinned != undefined) {
currentPoint.x = mouse.x;
currentPoint.y = mouse.y;
if (pinned)
currentPoint.fixed = true;
else if (unPinned)
currentPoint.fixed = false;
}
}
function mousePressed() {
for (var i = 0; i < cloth.points.length; ++i) {
var point = cloth.points[i];
if (calculateDistance(point, mouse) < spacing &&
mouse.down && !currentPoint)
currentPoint = point;
}
}
function animate() {
cloth.update(0.0025);
cloth.updateStrain();
mousePressed();
draggedPoint();
render();
requestAnimFrame(animate);
}
window.onload = function() {
init();
}