Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore.coffee/Utils.js
4626 views
1
// Generated by CoffeeScript 1.7.1
2
3
/*
4
Various useful methods
5
6
@class bkcore.Utils
7
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
8
*/
9
10
(function() {
11
var Utils, exports;
12
13
Utils = (function() {
14
15
/*
16
Creates a bkcore.threejs.Shaders["normalV"|"normal"] material
17
with given parameters
18
*/
19
function Utils() {}
20
21
Utils.createNormalMaterial = function(opts) {
22
var material, parameters, shader, shadername, uniforms;
23
if (opts == null) {
24
opts = {};
25
}
26
if (opts.ambient == null) {
27
opts.ambient = 0x444444;
28
}
29
if (opts.normalScale == null) {
30
opts.normalScale = 1.0;
31
}
32
if (opts.reflectivity == null) {
33
opts.reflectivity = 0.9;
34
}
35
if (opts.shininess == null) {
36
opts.shininess = 42;
37
}
38
if (opts.metal == null) {
39
opts.metal = false;
40
}
41
shadername = opts.perPixel ? "normalV" : "normal";
42
shader = bkcore.threejs.Shaders[shadername];
43
uniforms = THREE.UniformsUtils.clone(shader.uniforms);
44
uniforms["enableDiffuse"].value = true;
45
uniforms["enableSpecular"].value = true;
46
uniforms["enableReflection"].value = !!opts.cube;
47
uniforms["tNormal"].texture = opts.normal;
48
uniforms["tDiffuse"].texture = opts.diffuse;
49
uniforms["tSpecular"].texture = opts.specular;
50
uniforms["uAmbientColor"].value.setHex(opts.ambient);
51
uniforms["uAmbientColor"].value.convertGammaToLinear();
52
uniforms["uNormalScale"].value = opts.normalScale;
53
if (opts.cube != null) {
54
uniforms["tCube"].texture = opts.cube;
55
uniforms["uReflectivity"].value = opts.reflectivity;
56
}
57
parameters = {
58
fragmentShader: shader.fragmentShader,
59
vertexShader: shader.vertexShader,
60
uniforms: uniforms,
61
lights: true,
62
fog: false
63
};
64
material = new THREE.ShaderMaterial(parameters);
65
material.perPixel = true;
66
material.metal = opts.metal;
67
return material;
68
};
69
70
71
/*
72
Projects an object origin vector to screen using given camera
73
@param THREE.Object3D object The object which origin you want to project
74
@param THREE.Camera camera The camera of the projection
75
@return THEE.Vector3 Projected verctor
76
*/
77
78
Utils.projectOnScreen = function(object, camera) {
79
var c, lPos, mat;
80
mat = new THREE.Matrix4();
81
mat.multiply(camera.matrixWorldInverse, object.matrixWorld);
82
mat.multiply(camera.projectionMatrix, mat);
83
c = mat.n44;
84
lPos = new THREE.Vector3(mat.n14 / c, mat.n24 / c, mat.n34 / c);
85
return lPos.multiplyScalar(0.5).addScalar(0.5);
86
};
87
88
89
/*
90
Get an url parameter
91
@param String name Parameter slug
92
@return Mixed
93
*/
94
95
Utils.URLParameters = null;
96
97
Utils.getURLParameter = function(name) {
98
if (this.URLParameters == null) {
99
this.URLParameters = {};
100
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, (function(_this) {
101
return function(m, key, val) {
102
return _this.URLParameters[key] = val;
103
};
104
})(this));
105
}
106
return this.URLParameters[name];
107
};
108
109
110
/*
111
Get top offset of an element
112
@param obj HTMLElement
113
*/
114
115
Utils.getOffsetTop = function(obj) {
116
var curtop;
117
curtop = obj.offsetTop;
118
if (obj.offsetParent) {
119
while (obj = obj.offsetParent) {
120
curtop += obj.offsetTop;
121
}
122
}
123
return curtop;
124
};
125
126
127
/*
128
Scrolls page to given element id
129
@param string id The ID of the element
130
*/
131
132
Utils.scrollTo = function(id) {
133
return window.scroll(0, this.getOffsetTop(document.getElementById(id)));
134
};
135
136
137
/*
138
Add or remove a class from an element
139
@param string id [description]
140
@param string cssclass [description]
141
@param bool active [description]
142
*/
143
144
Utils.updateClass = function(id, cssclass, active) {
145
var e;
146
e = document.getElementById(id);
147
if (e == null) {
148
return;
149
}
150
if (active) {
151
return e.classList.add(cssclass);
152
} else {
153
return e.classList.remove(cssclass);
154
}
155
};
156
157
158
/*
159
Performs an XMLHttpRequest
160
@param string url [description]
161
@param bool postData true = POST, false = GET
162
@param {Function} callback [description]
163
@param {Object} data [description]
164
*/
165
166
Utils.request = function(url, postData, callback, data) {
167
var XMLHttpFactories, createXMLHTTPObject, i, method, qdata, req, val;
168
XMLHttpFactories = [
169
function() {
170
return new XMLHttpRequest();
171
}, function() {
172
return new ActiveXObject("Msxml2.XMLHTTP");
173
}, function() {
174
return new ActiveXObject("Msxml3.XMLHTTP");
175
}, function() {
176
return new ActiveXObject("Microsoft.XMLHTTP");
177
}
178
];
179
createXMLHTTPObject = function() {
180
var e, i, xmlhttp, _i, _ref;
181
xmlhttp = false;
182
for (i = _i = 0, _ref = XMLHttpFactories.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
183
try {
184
xmlhttp = XMLHttpFactories[i]();
185
} catch (_error) {
186
e = _error;
187
continue;
188
}
189
break;
190
}
191
return xmlhttp;
192
};
193
req = createXMLHTTPObject();
194
if (req == null) {
195
return;
196
}
197
method = postData != null ? "POST" : "GET";
198
qdata = "o=bk";
199
if (data != null) {
200
for (i in data) {
201
val = data[i];
202
qdata += "&" + i + "=" + val;
203
if (postData != null) {
204
url += "?" + qdata;
205
}
206
}
207
}
208
req.open(method, url, true);
209
if (postData != null) {
210
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
211
}
212
req.onreadystatechange = function() {
213
if (req.readyState !== 4) {
214
return;
215
}
216
if (!(req.status === 200 || req.status === 304)) {
217
return;
218
}
219
return typeof callback === "function" ? callback(req) : void 0;
220
};
221
req.send(qdata);
222
return req;
223
};
224
225
226
/*
227
Checks whether the device supports Touch input
228
*/
229
230
Utils.isTouchDevice = function() {
231
return ('ontouchstart' in window) || (navigator.MaxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0);
232
};
233
234
return Utils;
235
236
})();
237
238
239
/*
240
Exports
241
@package bkcore
242
*/
243
244
exports = exports != null ? exports : this;
245
246
exports.bkcore || (exports.bkcore = {});
247
248
exports.bkcore.Utils = Utils;
249
250
}).call(this);
251
252