Path: blob/main/projects/HexGL/bkcore.coffee/Utils.js
2615 views
// Generated by CoffeeScript 1.7.112/*3Various useful methods45@class bkcore.Utils6@author Thibaut 'BKcore' Despoulain <http://bkcore.com>7*/89(function() {10var Utils, exports;1112Utils = (function() {1314/*15Creates a bkcore.threejs.Shaders["normalV"|"normal"] material16with given parameters17*/18function Utils() {}1920Utils.createNormalMaterial = function(opts) {21var material, parameters, shader, shadername, uniforms;22if (opts == null) {23opts = {};24}25if (opts.ambient == null) {26opts.ambient = 0x444444;27}28if (opts.normalScale == null) {29opts.normalScale = 1.0;30}31if (opts.reflectivity == null) {32opts.reflectivity = 0.9;33}34if (opts.shininess == null) {35opts.shininess = 42;36}37if (opts.metal == null) {38opts.metal = false;39}40shadername = opts.perPixel ? "normalV" : "normal";41shader = bkcore.threejs.Shaders[shadername];42uniforms = THREE.UniformsUtils.clone(shader.uniforms);43uniforms["enableDiffuse"].value = true;44uniforms["enableSpecular"].value = true;45uniforms["enableReflection"].value = !!opts.cube;46uniforms["tNormal"].texture = opts.normal;47uniforms["tDiffuse"].texture = opts.diffuse;48uniforms["tSpecular"].texture = opts.specular;49uniforms["uAmbientColor"].value.setHex(opts.ambient);50uniforms["uAmbientColor"].value.convertGammaToLinear();51uniforms["uNormalScale"].value = opts.normalScale;52if (opts.cube != null) {53uniforms["tCube"].texture = opts.cube;54uniforms["uReflectivity"].value = opts.reflectivity;55}56parameters = {57fragmentShader: shader.fragmentShader,58vertexShader: shader.vertexShader,59uniforms: uniforms,60lights: true,61fog: false62};63material = new THREE.ShaderMaterial(parameters);64material.perPixel = true;65material.metal = opts.metal;66return material;67};686970/*71Projects an object origin vector to screen using given camera72@param THREE.Object3D object The object which origin you want to project73@param THREE.Camera camera The camera of the projection74@return THEE.Vector3 Projected verctor75*/7677Utils.projectOnScreen = function(object, camera) {78var c, lPos, mat;79mat = new THREE.Matrix4();80mat.multiply(camera.matrixWorldInverse, object.matrixWorld);81mat.multiply(camera.projectionMatrix, mat);82c = mat.n44;83lPos = new THREE.Vector3(mat.n14 / c, mat.n24 / c, mat.n34 / c);84return lPos.multiplyScalar(0.5).addScalar(0.5);85};868788/*89Get an url parameter90@param String name Parameter slug91@return Mixed92*/9394Utils.URLParameters = null;9596Utils.getURLParameter = function(name) {97if (this.URLParameters == null) {98this.URLParameters = {};99window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, (function(_this) {100return function(m, key, val) {101return _this.URLParameters[key] = val;102};103})(this));104}105return this.URLParameters[name];106};107108109/*110Get top offset of an element111@param obj HTMLElement112*/113114Utils.getOffsetTop = function(obj) {115var curtop;116curtop = obj.offsetTop;117if (obj.offsetParent) {118while (obj = obj.offsetParent) {119curtop += obj.offsetTop;120}121}122return curtop;123};124125126/*127Scrolls page to given element id128@param string id The ID of the element129*/130131Utils.scrollTo = function(id) {132return window.scroll(0, this.getOffsetTop(document.getElementById(id)));133};134135136/*137Add or remove a class from an element138@param string id [description]139@param string cssclass [description]140@param bool active [description]141*/142143Utils.updateClass = function(id, cssclass, active) {144var e;145e = document.getElementById(id);146if (e == null) {147return;148}149if (active) {150return e.classList.add(cssclass);151} else {152return e.classList.remove(cssclass);153}154};155156157/*158Performs an XMLHttpRequest159@param string url [description]160@param bool postData true = POST, false = GET161@param {Function} callback [description]162@param {Object} data [description]163*/164165Utils.request = function(url, postData, callback, data) {166var XMLHttpFactories, createXMLHTTPObject, i, method, qdata, req, val;167XMLHttpFactories = [168function() {169return new XMLHttpRequest();170}, function() {171return new ActiveXObject("Msxml2.XMLHTTP");172}, function() {173return new ActiveXObject("Msxml3.XMLHTTP");174}, function() {175return new ActiveXObject("Microsoft.XMLHTTP");176}177];178createXMLHTTPObject = function() {179var e, i, xmlhttp, _i, _ref;180xmlhttp = false;181for (i = _i = 0, _ref = XMLHttpFactories.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {182try {183xmlhttp = XMLHttpFactories[i]();184} catch (_error) {185e = _error;186continue;187}188break;189}190return xmlhttp;191};192req = createXMLHTTPObject();193if (req == null) {194return;195}196method = postData != null ? "POST" : "GET";197qdata = "o=bk";198if (data != null) {199for (i in data) {200val = data[i];201qdata += "&" + i + "=" + val;202if (postData != null) {203url += "?" + qdata;204}205}206}207req.open(method, url, true);208if (postData != null) {209req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');210}211req.onreadystatechange = function() {212if (req.readyState !== 4) {213return;214}215if (!(req.status === 200 || req.status === 304)) {216return;217}218return typeof callback === "function" ? callback(req) : void 0;219};220req.send(qdata);221return req;222};223224225/*226Checks whether the device supports Touch input227*/228229Utils.isTouchDevice = function() {230return ('ontouchstart' in window) || (navigator.MaxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0);231};232233return Utils;234235})();236237238/*239Exports240@package bkcore241*/242243exports = exports != null ? exports : this;244245exports.bkcore || (exports.bkcore = {});246247exports.bkcore.Utils = Utils;248249}).call(this);250251252