code
stringlengths
2
1.05M
'use strict'; var encryption = require('../utilities/encryption'); var mongoose = require('mongoose'); var DEFAULT_PAGE_SIZE = 10; var User = mongoose.model('User'); function createUser(req, res, next) { var newUserData = { username: req.body.username, email: req.body.email }; newUserData.salt = encryption.generateSalt(); newUserData.hashPass = encryption.generateHashedPassword(newUserData.salt, req.body.password); newUserData.roles = ['user']; User.create(newUserData, function (err, user) { if (err) { console.log('Failed to register new user: ' + err); res.status(400); res.send(false); return; } req.logIn(user, function (err) { if (err) { res.status(400); return res.send({ reason: err.toString() }); } res.send(user); }); }); } exports.createUser = createUser; function updateUser(req, res, next) { var updatedUserData = { firstName: req.body.firstName, lastName: req.body.lastName, phone: req.body.phone, city: req.body.city, imageUrl: req.body.imageUrl }; if (req.body.password && req.body.password.length > 5) { updatedUserData.salt = encryption.generateSalt(); updatedUserData.hashPass = encryption.generateHashedPassword(updatedUserData.salt, req.body.password); } User.update({ _id: req.body._id }, updatedUserData, function (err, numberAffectedRows) { if (err) { res.status(400).send('Error updating user data: ' + err); return; } res.status(200).send('User updated successfully'); }); } exports.updateUser = updateUser; function getAllUsers(req, res, next) { var page = Math.max(req.query.page, 1); var orderType = req.query.orderType === 'desc' ? '-' : ''; var username = req.query.username || ''; var firstName = req.query.firstName || ''; var lastName = req.query.lastName || ''; User.find({}).where({ username: new RegExp(username, "i") }).skip(DEFAULT_PAGE_SIZE * (page - 1)).limit(DEFAULT_PAGE_SIZE).select('_id username firstName lastName email').exec(function (error, users) { if (error) { res.status(400); res.send(error); } else { res.send(users); } }); } exports.getAllUsers = getAllUsers; function getById(req, res, next) { User.findOne({ _id: req.params.id }).select('_id username firstName lastName imageUrl city phone roles items').exec(function (err, user) { if (err) { res.status(400).send('User could not be found: ' + err); console.log('User could not be found: ' + err); return; } res.send(user); }); } exports.getById = getById; function deleteUser(req, res, next) { User.findOne({ _id: req.params.id }).remove().exec(function (err, user) { if (err) { res.status(400).send('User could not be found: ' + err); console.log('User could not be found: ' + err); return; } res.status(200).send("User deleted successfully from database" + user); }); } exports.deleteUser = deleteUser; function updateByAdmin(req, res, next) { var updatedUserData = { _id: req.body._id, firstName: req.body.firstName, lastName: req.body.lastName, phone: req.body.phone, city: req.body.city }; if (req.body.password && req.body.password.length > 5) { updatedUserData.salt = encryption.generateSalt(); updatedUserData.hashPass = encryption.generateHashedPassword(updatedUserData.salt, req.body.password); } User.update({ _id: req.body._id }, updatedUserData, function (err, numberAffectedRows) { if (err) { res.status(400).send('Error updating user data: ' + err); return; } res.status(200).send('User updated successfully'); }); } exports.updateByAdmin = updateByAdmin; //# sourceMappingURL=usersController.js.map
/*! chessboard.js v0.3.0 | (c) 2013 Chris Oakman | MIT License chessboardjs.com/license */ (function(){function l(f){return"string"!==typeof f?!1:-1!==f.search(/^[a-h][1-8]$/)}function Q(f){if("string"!==typeof f)return!1;f=f.replace(/ .+$/,"");f=f.split("/");if(8!==f.length)return!1;for(var b=0;8>b;b++)if(""===f[b]||8<f[b].length||-1!==f[b].search(/[^kqrbnpKQRNBP1-8]/))return!1;return!0}function F(f){if("object"!==typeof f)return!1;for(var b in f)if(!0===f.hasOwnProperty(b)){var n;(n=!0!==l(b))||(n=f[b],n="string"!==typeof n?!1:-1!==n.search(/^[bw][KQRNBP]$/),n=!0!==n);if(n)return!1}return!0} function K(f){if(!0!==Q(f))return!1;f=f.replace(/ .+$/,"");f=f.split("/");for(var b={},n=8,m=0;8>m;m++){for(var l=f[m].split(""),r=0,w=0;w<l.length;w++)if(-1!==l[w].search(/[1-8]/))var I=parseInt(l[w],10),r=r+I;else{var I=b,F=B[r]+n,A;A=l[w];A=A.toLowerCase()===A?"b"+A.toUpperCase():"w"+A.toUpperCase();I[F]=A;r++}n--}return b}function L(f){if(!0!==F(f))return!1;for(var b="",n=8,m=0;8>m;m++){for(var l=0;8>l;l++){var r=B[l]+n;!0===f.hasOwnProperty(r)?(r=f[r].split(""),r="w"===r[0]?r[1].toUpperCase(): r[1].toLowerCase(),b+=r):b+="1"}7!==m&&(b+="/");n--}b=b.replace(/11111111/g,"8");b=b.replace(/1111111/g,"7");b=b.replace(/111111/g,"6");b=b.replace(/11111/g,"5");b=b.replace(/1111/g,"4");b=b.replace(/111/g,"3");return b=b.replace(/11/g,"2")}var B="abcdefgh".split("");window.ChessBoard=window.ChessBoard||function(f,b){function n(){return"xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx".replace(/x/g,function(a){return(16*Math.random()|0).toString(16)})}function m(a){return JSON.parse(JSON.stringify(a))}function X(a){a= a.split(".");return{major:parseInt(a[0],10),minor:parseInt(a[1],10),patch:parseInt(a[2],10)}}function r(a,e,c){if(!0===b.hasOwnProperty("showErrors")&&!1!==b.showErrors){var d="ChessBoard Error "+a+": "+e;"console"===b.showErrors&&"object"===typeof console&&"function"===typeof console.log?(console.log(d),2<=arguments.length&&console.log(c)):"alert"===b.showErrors?(c&&(d+="\n\n"+JSON.stringify(c)),window.alert(d)):"function"===typeof b.showErrors&&b.showErrors(a,e,c)}}function w(a){return"fast"=== a||"slow"===a?!0:parseInt(a,10)+""!==a+""?!1:0<=a}function I(){for(var a=0;a<B.length;a++)for(var b=1;8>=b;b++){var c=B[a]+b;s[c]=c+"-"+n()}b="KQRBNP".split("");for(a=0;a<b.length;a++){var c="w"+b[a],d="b"+b[a];M[c]=c+"-"+n();M[d]=d+"-"+n()}}function ga(){var a='<div class="'+h.chessboard+'">';!0===b.sparePieces&&(a+='<div class="'+h.sparePieces+" "+h.sparePiecesTop+'"></div>');a+='<div class="'+h.board+'"></div>';!0===b.sparePieces&&(a+='<div class="'+h.sparePieces+" "+h.sparePiecesBottom+'"></div>'); return a+"</div>"}function A(a){"black"!==a&&(a="white");var e="",c=m(B),d=8;"black"===a&&(c.reverse(),d=1);for(var C="white",f=0;8>f;f++){for(var e=e+('<div class="'+h.row+'">'),k=0;8>k;k++){var g=c[k]+d,e=e+('<div class="'+h.square+" "+h[C]+" square-"+g+'" style="width: '+p+"px; height: "+p+'px" id="'+s[g]+'" data-square="'+g+'">');if(!0===b.showNotation){if("white"===a&&1===d||"black"===a&&8===d)e+='<div class="'+h.notation+" "+h.alpha+'">'+c[k]+"</div>";0===k&&(e+='<div class="'+h.notation+" "+ h.numeric+'">'+d+"</div>")}e+="</div>";C="white"===C?"black":"white"}e+='<div class="'+h.clearfix+'"></div></div>';C="white"===C?"black":"white";"white"===a?d--:d++}return e}function Y(a){if("function"===typeof b.pieceTheme)return b.pieceTheme(a);if("string"===typeof b.pieceTheme)return b.pieceTheme.replace(/{piece}/g,a);r(8272,"Unable to build image source for cfg.pieceTheme.");return""}function D(a,b,c){var d='<img src="'+Y(a)+'" ';c&&"string"===typeof c&&(d+='id="'+c+'" ');d+='alt="" class="'+ h.piece+'" data-piece="'+a+'" style="width: '+p+"px;height: "+p+"px;";!0===b&&(d+="display:none;");return d+'" />'}function N(a){var b="wK wQ wR wB wN wP".split(" ");"black"===a&&(b="bK bQ bR bB bN bP".split(" "));a="";for(var c=0;c<b.length;c++)a+=D(b[c],!1,M[b[c]]);return a}function ha(a,e,c,d){a=$("#"+s[a]);var C=a.offset(),f=$("#"+s[e]);e=f.offset();var k=n();$("body").append(D(c,!0,k));var g=$("#"+k);g.css({display:"",position:"absolute",top:C.top,left:C.left});a.find("."+h.piece).remove();g.animate(e, {duration:b.moveSpeed,complete:function(){f.append(D(c));g.remove();"function"===typeof d&&d()}})}function ia(a,e,c){var d=$("#"+M[a]).offset(),f=$("#"+s[e]);e=f.offset();var g=n();$("body").append(D(a,!0,g));var k=$("#"+g);k.css({display:"",position:"absolute",left:d.left,top:d.top});k.animate(e,{duration:b.moveSpeed,complete:function(){f.find("."+h.piece).remove();f.append(D(a));k.remove();"function"===typeof c&&c()}})}function ja(a,e,c){function d(){f++;if(f===a.length&&(G(),!0===b.hasOwnProperty("onMoveEnd")&& "function"===typeof b.onMoveEnd))b.onMoveEnd(m(e),m(c))}for(var f=0,g=0;g<a.length;g++)"clear"===a[g].type&&$("#"+s[a[g].square]+" ."+h.piece).fadeOut(b.trashSpeed,d),"add"===a[g].type&&!0!==b.sparePieces&&$("#"+s[a[g].square]).append(D(a[g].piece,!0)).find("."+h.piece).fadeIn(b.appearSpeed,d),"add"===a[g].type&&!0===b.sparePieces&&ia(a[g].piece,a[g].square,d),"move"===a[g].type&&ha(a[g].source,a[g].destination,a[g].piece,d)}function ka(a,b){a=a.split("");var c=B.indexOf(a[0])+1,d=parseInt(a[1],10); b=b.split("");var g=B.indexOf(b[0])+1,f=parseInt(b[1],10),c=Math.abs(c-g),d=Math.abs(d-f);return c>=d?c:d}function la(a){for(var b=[],c=0;8>c;c++)for(var d=0;8>d;d++){var g=B[c]+(d+1);a!==g&&b.push({square:g,distance:ka(a,g)})}b.sort(function(a,b){return a.distance-b.distance});a=[];for(c=0;c<b.length;c++)a.push(b[c].square);return a}function G(){x.find("."+h.piece).remove();for(var a in g)!0===g.hasOwnProperty(a)&&$("#"+s[a]).append(D(g[a]))}function R(){x.html(A(u));G();!0===b.sparePieces&&("white"=== u?(S.html(N("black")),T.html(N("white"))):(S.html(N("white")),T.html(N("black"))))}function O(a){var e=m(g),c=m(a),d=L(e),f=L(c);if(d!==f){if(!0===b.hasOwnProperty("onChange")&&"function"===typeof b.onChange)b.onChange(e,c);g=a}}function U(a,b){for(var c in J)if(!0===J.hasOwnProperty(c)){var d=J[c];if(a>=d.left&&a<d.left+p&&b>=d.top&&b<d.top+p)return c}return"offboard"}function V(){x.find("."+h.square).removeClass(h.highlight1+" "+h.highlight2)}function ma(){function a(){G();y.css("display","none"); if(!0===b.hasOwnProperty("onSnapbackEnd")&&"function"===typeof b.onSnapbackEnd)b.onSnapbackEnd(E,t,m(g),u)}if("spare"===t)Z();else{V();var e=$("#"+s[t]).offset();y.animate(e,{duration:b.snapbackSpeed,complete:a});z=!1}}function Z(){V();var a=m(g);delete a[t];O(a);G();y.fadeOut(b.trashSpeed);z=!1}function na(a){V();var e=m(g);delete e[t];e[a]=E;O(e);e=$("#"+s[a]).offset();y.animate(e,{duration:b.snapSpeed,complete:function(){G();y.css("display","none");if(!0===b.hasOwnProperty("onSnapEnd")&&"function"=== typeof b.onSnapEnd)b.onSnapEnd(t,a,E)}});z=!1}function P(a,e,c,d){if("function"!==typeof b.onDragStart||!1!==b.onDragStart(a,e,m(g),u)){z=!0;E=e;t=a;H="spare"===a?"offboard":a;J={};for(var f in s)!0===s.hasOwnProperty(f)&&(J[f]=$("#"+s[f]).offset());y.attr("src",Y(e)).css({display:"",position:"absolute",left:c-p/2,top:d-p/2});"spare"!==a&&$("#"+s[a]).addClass(h.highlight1).find("."+h.piece).css("display","none")}}function aa(a,e){y.css({left:a-p/2,top:e-p/2});var c=U(a,e);if(c!==H){!0===l(H)&&$("#"+ s[H]).removeClass(h.highlight2);!0===l(c)&&$("#"+s[c]).addClass(h.highlight2);if("function"===typeof b.onDragMove)b.onDragMove(c,H,t,E,m(g),u);H=c}}function ba(a){var e="drop";"offboard"===a&&"snapback"===b.dropOffBoard&&(e="snapback");"offboard"===a&&"trash"===b.dropOffBoard&&(e="trash");if(!0===b.hasOwnProperty("onDrop")&&"function"===typeof b.onDrop){var c=m(g);"spare"===t&&!0===l(a)&&(c[a]=E);!0===l(t)&&"offboard"===a&&delete c[t];!0===l(t)&&!0===l(a)&&(delete c[t],c[a]=E);var d=m(g),c=b.onDrop(t, a,E,c,d,u);if("snapback"===c||"trash"===c)e=c}"snapback"===e?ma():"trash"===e?Z():"drop"===e&&na(a)}function oa(a){a.preventDefault()}function pa(a){if(!0===b.draggable){var e=$(this).attr("data-square");!0===l(e)&&!0===g.hasOwnProperty(e)&&P(e,g[e],a.pageX,a.pageY)}}function qa(a){if(!0===b.draggable){var e=$(this).attr("data-square");!0===l(e)&&!0===g.hasOwnProperty(e)&&(a=a.originalEvent,P(e,g[e],a.changedTouches[0].pageX,a.changedTouches[0].pageY))}}function ra(a){if(!0===b.sparePieces){var e= $(this).attr("data-piece");P("spare",e,a.pageX,a.pageY)}}function sa(a){if(!0===b.sparePieces){var e=$(this).attr("data-piece");a=a.originalEvent;P("spare",e,a.changedTouches[0].pageX,a.changedTouches[0].pageY)}}function ca(a){!0===z&&aa(a.pageX,a.pageY)}function ta(a){!0===z&&(a.preventDefault(),aa(a.originalEvent.changedTouches[0].pageX,a.originalEvent.changedTouches[0].pageY))}function da(a){!0===z&&(a=U(a.pageX,a.pageY),ba(a))}function ua(a){!0===z&&(a=U(a.originalEvent.changedTouches[0].pageX, a.originalEvent.changedTouches[0].pageY),ba(a))}function va(a){if(!1===z&&(!0===b.hasOwnProperty("onMouseoverSquare")&&"function"===typeof b.onMouseoverSquare)&&(a=$(a.currentTarget).attr("data-square"),!0===l(a))){var e=!1;!0===g.hasOwnProperty(a)&&(e=g[a]);b.onMouseoverSquare(a,e,m(g),u)}}function wa(a){if(!1===z&&(!0===b.hasOwnProperty("onMouseoutSquare")&&"function"===typeof b.onMouseoutSquare)&&(a=$(a.currentTarget).attr("data-square"),!0===l(a))){var e=!1;!0===g.hasOwnProperty(a)&&(e=g[a]); b.onMouseoutSquare(a,e,m(g),u)}}function xa(){$("body").on("mousedown mousemove","."+h.piece,oa);x.on("mousedown","."+h.square,pa);v.on("mousedown","."+h.sparePieces+" ."+h.piece,ra);x.on("mouseenter","."+h.square,va);x.on("mouseleave","."+h.square,wa);!0===(navigator&&navigator.userAgent&&-1!==navigator.userAgent.search(/MSIE/))?(document.ondragstart=function(){return!1},$("body").on("mousemove",ca),$("body").on("mouseup",da)):($(window).on("mousemove",ca),$(window).on("mouseup",da));!0==="ontouchstart"in document.documentElement&&(x.on("touchstart","."+h.square,qa),v.on("touchstart","."+h.sparePieces+" ."+h.piece,sa),$(window).on("touchmove",ta),$(window).on("touchend",ua))}function ya(){v.html(ga());x=v.find("."+h.board);!0===b.sparePieces&&(S=v.find("."+h.sparePiecesTop),T=v.find("."+h.sparePiecesBottom));var a=n();$("body").append(D("wP",!0,a));y=$("#"+a);ea=parseInt(x.css("borderLeftWidth"),10);q.resize()}b=b||{};var fa=K("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"),h={alpha:"alpha-d2270",black:"black-3c85d", board:"board-b72b1",chessboard:"chessboard-63f37",clearfix:"clearfix-7da63",highlight1:"highlight1-32417",highlight2:"highlight2-9c5d2",notation:"notation-322f9",numeric:"numeric-fc462",piece:"piece-417db",row:"row-5277c",sparePieces:"spare-pieces-7492f",sparePiecesBottom:"spare-pieces-bottom-ae20f",sparePiecesTop:"spare-pieces-top-4028b",square:"square-55d63",white:"white-1e1d7"},v,x,y,S,T,q={},ea=2,u="white",g={},p,E,H,t,z=!1,M={},s={},J;q.clear=function(a){q.position({},a)};q.destroy=function(){v.html(""); y.remove();v.unbind()};q.fen=function(){return q.position("fen")};q.flip=function(){q.orientation("flip")};q.move=function(){if(0!==arguments.length){for(var a=!0,b={},c=0;c<arguments.length;c++)if(!1===arguments[c])a=!1;else{var d;d=arguments[c];"string"!==typeof d?d=!1:(d=d.split("-"),d=2!==d.length?!1:!0===l(d[0])&&!0===l(d[1]));!0!==d?r(2826,"Invalid move passed to the move method.",arguments[c]):(d=arguments[c].split("-"),b[d[0]]=d[1])}var c=g,c=m(c),f;for(f in b)!0===b.hasOwnProperty(f)&&!0=== c.hasOwnProperty(f)&&(d=c[f],delete c[f],c[b[f]]=d);b=c;q.position(b,a);return b}};q.orientation=function(a){if(0===arguments.length)return u;"white"===a||"black"===a?(u=a,R()):"flip"===a?(u="white"===u?"black":"white",R()):r(5482,"Invalid value passed to the orientation method.",a)};q.position=function(a,b){if(0===arguments.length)return m(g);if("string"===typeof a&&"fen"===a.toLowerCase())return L(g);!1!==b&&(b=!0);"string"===typeof a&&"start"===a.toLowerCase()&&(a=m(fa));!0===Q(a)&&(a=K(a));if(!0!== F(a))r(6482,"Invalid value passed to the position method.",a);else if(!0===b){var c=g,d=a,c=m(c),d=m(d),f=[],h={},k;for(k in d)!0===d.hasOwnProperty(k)&&(!0===c.hasOwnProperty(k)&&c[k]===d[k])&&(delete c[k],delete d[k]);for(k in d)if(!0===d.hasOwnProperty(k)){var l;a:{l=c;for(var n=d[k],s=la(k),p=0;p<s.length;p++){var q=s[p];if(!0===l.hasOwnProperty(q)&&l[q]===n){l=q;break a}}l=!1}!1!==l&&(f.push({type:"move",source:l,destination:k,piece:d[k]}),delete c[l],delete d[k],h[k]=!0)}for(k in d)!0===d.hasOwnProperty(k)&& (f.push({type:"add",square:k,piece:d[k]}),delete d[k]);for(k in c)!0===c.hasOwnProperty(k)&&!0!==h.hasOwnProperty(k)&&(f.push({type:"clear",square:k,piece:c[k]}),delete c[k]);ja(f,g,a);O(a)}else O(a),G()};q.resize=function(){var a=parseInt(v.css("width"),10);if(!a||0>=a)p=0;else{for(a-=1;0!==a%8&&0<a;)a--;p=a/8}x.css("width",8*p+"px");y.css({height:p,width:p});!0===b.sparePieces&&v.find("."+h.sparePieces).css("paddingLeft",p+ea+"px");R()};q.start=function(a){q.position("start",a)};var W;if(W=!0=== function(){if("string"===typeof f){if(""===f)return window.alert("ChessBoard Error 1001: The first argument to ChessBoard() cannot be an empty string.\n\nExiting..."),!1;var a=document.getElementById(f);if(!a)return window.alert('ChessBoard Error 1002: Element with id "'+f+'" does not exist in the DOM.\n\nExiting...'),!1;v=$(a)}else if(v=$(f),1!==v.length)return window.alert("ChessBoard Error 1003: The first argument to ChessBoard() must be an ID or a single DOM node.\n\nExiting..."),!1;if(!window.JSON|| "function"!==typeof JSON.stringify||"function"!==typeof JSON.parse)return window.alert("ChessBoard Error 1004: JSON does not exist. Please include a JSON polyfill.\n\nExiting..."),!1;if(a=typeof window.$)if(a=$.fn)if(a=$.fn.jquery)var a=$.fn.jquery,b="1.7.0",a=X(a),b=X(b),a=!0===1E8*a.major+1E4*a.minor+a.patch>=1E8*b.major+1E4*b.minor+b.patch;return a?!0:(window.alert("ChessBoard Error 1005: Unable to find a valid version of jQuery. Please include jQuery 1.7.0 or higher on the page.\n\nExiting..."), !1)}()){if("string"===typeof b||!0===F(b))b={position:b};"black"!==b.orientation&&(b.orientation="white");u=b.orientation;!1!==b.showNotation&&(b.showNotation=!0);!0!==b.draggable&&(b.draggable=!1);"trash"!==b.dropOffBoard&&(b.dropOffBoard="snapback");!0!==b.sparePieces&&(b.sparePieces=!1);!0===b.sparePieces&&(b.draggable=!0);if(!0!==b.hasOwnProperty("pieceTheme")||"string"!==typeof b.pieceTheme&&"function"!==typeof b.pieceTheme)b.pieceTheme="../img/chesspieces/wikipedia/{piece}.png";if(!0!==b.hasOwnProperty("appearSpeed")|| !0!==w(b.appearSpeed))b.appearSpeed=200;if(!0!==b.hasOwnProperty("moveSpeed")||!0!==w(b.moveSpeed))b.moveSpeed=200;if(!0!==b.hasOwnProperty("snapbackSpeed")||!0!==w(b.snapbackSpeed))b.snapbackSpeed=50;if(!0!==b.hasOwnProperty("snapSpeed")||!0!==w(b.snapSpeed))b.snapSpeed=25;if(!0!==b.hasOwnProperty("trashSpeed")||!0!==w(b.trashSpeed))b.trashSpeed=100;!0===b.hasOwnProperty("position")&&("start"===b.position?g=m(fa):!0===Q(b.position)?g=K(b.position):!0===F(b.position)?g=m(b.position):r(7263,"Invalid value passed to config.position.", b.position));W=!0}W&&(I(),ya(),xa());return q};window.ChessBoard.fenToObj=K;window.ChessBoard.objToFen=L})();
/*jshint esversion: 6 */ import { Template } from 'meteor/templating'; import './autoForm.html';
require.paths.unshift(process.cwd() + './../Source/'); var Deck = require('deck').setup(GLOBAL, 'node'); var http = require('http'); /* App */ var Application = function(request, response){ response.setStatus(200); response.setHeader('content-type', 'text/html'); if (request.params.name) response.write(['Hello ', request.params.name, '!\n'].join('')); if (request.params.age) response.write(['You are', request.params.age, 'years old.\n'].join(' ')); response.write('Have a nice Day!'); response.finish(); }; /* Deck Instance */ var Serv = new Deck(); Serv.addRoute('/{:name{/:age}}', Application); http.createServer(function (req, res) { setTimeout(function(){ Serv.dispatch(req, res); }, 0); }).listen(8000);
module.exports = deactivate deactivate.usage = "npm deactivate <pkg>" deactivate.completion = function (args, index, cb) { var installedPkgs = require("./utils/completion/installed-packages") installedPkgs(args, index, false, false, cb) } var npm = require("../npm") , fs = require("./utils/graceful-fs") , log = require("./utils/log") , path = require("path") , rm = require("./utils/rm-rf") , chain = require("./utils/chain") , lifecycle = require("./utils/lifecycle") , readJson = require("./utils/read-json") , asyncMap = require("./utils/async-map") , loadPackageDefaults = require("./utils/load-package-defaults") function deactivate (args, cb) { var rb = npm.ROLLBACK npm.ROLLBACK = true asyncMap(args.map(function (a) { return a.split("@").shift() }), preDeactivate, function (er, data) { if (er) return cb(er) asyncMap ( data , function (d, cb) { rm(path.join(npm.dir, d.name, "active"), cb) } , function (d, cb) { rm(path.join(npm.root, d.name+".js"), cb) } , function (d, cb) { rm(path.join(npm.root, d.name), cb) } , rmBins , rmMans , function (er) { if (er) return cb(er) asyncMap(data, postDeactivate, function (er) { npm.ROLLBACK = rb cb(er) }) } ) }) } function rmBins (data, cb) { var binroot = npm.config.get("binroot") if (!data.bin || !binroot) return cb() asyncMap(Object.getOwnPropertyNames(data.bin) .map(function (bin) { return path.join(binroot, bin) }) , rm , cb ) } function rmMans (pkg, cb) { var manroot = npm.config.get("manroot") if (!pkg.man || !manroot) return cb() asyncMap(pkg.man, function (man, cb) { var parseMan = man.match(/(.*)\.([0-9]+)(\.gz)?$/) , stem = parseMan[1] , sxn = parseMan[2] , gz = parseMan[3] || "" , bn = path.basename(stem) rm(path.join( manroot , "man"+sxn , (bn.indexOf(pkg.name) === 0 ? bn : pkg.name + "-" + bn) + "." + sxn + gz ), cb) }, cb) } function preDeactivate (pkg, cb) { // run the "deactivate" lifecycle event // unlink the "active" folder // unlink the libs and main.js from the npm.root // run the "postdeactivate" lifecycle event var active = path.join(npm.dir, pkg, "active") , jsonFile = path.join(active, "package", "package.json") fs.readlink(active, function (er, p) { if (er) return cb() var version = path.basename(p) readJson(jsonFile, function (er, data) { if (er) return cb(er) data.version = version data._id = data.name+"@"+data.version npm.set(data) loadPackageDefaults(data, function (er, data) { if (er) return cb(er) chain ( [lifecycle, data, "predeactivate"] , [lifecycle, data, "deactivate"] , function (er) { cb(er, data) } ) }) }) }) } function postDeactivate (data, cb) { asyncMap(data, function (d, cb) { lifecycle(d, "postdeactivate", cb) }, cb) }
"use strict"; /** * @module opcua.server */ require("requirish")._(module); var Dequeue = require("collections/deque"); var subscription_service = require("lib/services/subscription_service"); var NotificationMessage = subscription_service.NotificationMessage; var StatusChangeNotification = subscription_service.StatusChangeNotification; var StatusCodes = require("lib/datamodel/opcua_status_code").StatusCodes; var Enum = require("lib/misc/enum"); var assert = require("better-assert"); var _ = require("underscore"); var AttributeIds = require("lib/datamodel/attributeIds").AttributeIds; var SequenceNumberGenerator = require("lib/misc/sequence_number_generator").SequenceNumberGenerator; var EventEmitter = require("events").EventEmitter; var util = require("util"); var debugLog = require("lib/misc/utils").make_debugLog(__filename); // debugLog = console.log.bind(console); var doDebug = true; var SubscriptionState = new Enum([ "CLOSED", // The Subscription has not yet been created or has terminated. "CREATING", // The Subscription is being created "NORMAL", // The Subscription is cyclically checking for Notifications from its MonitoredItems. // The keep-alive counter is not used in this state. "LATE", // The publishing timer has expired and there are Notifications available or a keep-alive Message is // ready to be sent, but there are no Publish requests queued. When in this state, the next Publish // request is processed when it is received. The keep-alive counter is not used in this state. "KEEPALIVE",// The Subscription is cyclically checking for Notification // alive counter to count down to 0 from its maximum. "TERMINATED" ]); exports.SubscriptionState = SubscriptionState; var minimumPublishingInterval = 100; // fastest possible var defaultPublishingInterval = 100; var maximumPublishingInterval = 1000 * 60 * 60 * 24 * 30; // 1 month function _adjust_publishing_interval(publishingInterval) { publishingInterval = publishingInterval || defaultPublishingInterval; publishingInterval = Math.max(publishingInterval, minimumPublishingInterval); publishingInterval = Math.min(publishingInterval, maximumPublishingInterval); return publishingInterval; } var minimumMaxKeepAliveCount = 2; var maximumMaxKeepAliveCount = 12000; function _adjust_maxKeepAliveCount(maxKeepAliveCount) { maxKeepAliveCount = maxKeepAliveCount || 10; maxKeepAliveCount = Math.max(maxKeepAliveCount, minimumMaxKeepAliveCount); maxKeepAliveCount = Math.min(maxKeepAliveCount, maximumMaxKeepAliveCount); return maxKeepAliveCount; } function _adjust_lifeTimeCount(lifeTimeCount, maxKeepAliveCount) { lifeTimeCount = lifeTimeCount || 1; // let's make sure that lifeTimeCount is at least three time maxKeepAliveCount lifeTimeCount = Math.max(lifeTimeCount, maxKeepAliveCount * 3); return lifeTimeCount; } function _adjust_publishinEnable(publishingEnabled) { return (publishingEnabled === null || publishingEnabled === undefined) ? true : !!publishingEnabled; } function _adjust_maxNotificationsPerPublish(maxNotificationsPerPublish) { return (maxNotificationsPerPublish >= 0) ? maxNotificationsPerPublish : 0; } // verify that the injected publishEngine provides the expected services // regarding the Subscription requirements... function _assert_valid_publish_engine(publishEngine) { assert(_.isObject(publishEngine)); assert(_.isNumber(publishEngine.pendingPublishRequestCount)); assert(_.isFunction(publishEngine.send_notification_message)); assert(_.isFunction(publishEngine.send_keep_alive_response)); } /** * The Subscription class used in the OPCUA server side. * @class Subscription * @param {Object} options * @param options.id {Integer} - a unique identifier * @param options.publishingInterval {Integer} - [optional](default:1000) the publishing interval. * @param options.maxKeepAliveCount {Integer} - [optional](default:10) the max KeepAlive Count. * @param options.lifeTimeCount {Integer} - [optional](default:10) the max Life Time Count * @param options.publishingEnabled {Boolean} - [optional](default:true) * @param options.maxNotificationsPerPublish {Integer} - [optional](default:0) * @param options.priority {Byte} * @constructor */ function Subscription(options) { options = options || {}; EventEmitter.apply(this, arguments); var self = this; self.publishEngine = options.publishEngine; _assert_valid_publish_engine(self.publishEngine); self.id = options.id || "<invalid_id>"; /** * the Subscription publishing interval * @property publishingInterval * @type {number} * @default 1000 */ self.publishingInterval = _adjust_publishing_interval(options.publishingInterval); /** * The keep alive count defines how many times the publish interval need to * expires without having notifications available before the server send an * empty message. * OPCUA Spec says: a value of 0 is invalid. * @property maxKeepAliveCount * @type {number} * @default 10 * */ self.maxKeepAliveCount = _adjust_maxKeepAliveCount(options.maxKeepAliveCount); self.resetKeepAliveCounter(); /** * The life time count defines how many times the publish interval expires without * having a connection to the client to deliver data. * If the life time count reaches maxKeepAliveCount, the subscription will * automatically terminate. * OPCUA Spec: The life-time count shall be a minimum of three times the keep keep-alive count. * * Note: this has to be interpreted as without having a PublishRequest available * @property lifeTimeCount * @type {Number} * @default 1 */ self.lifeTimeCount = _adjust_lifeTimeCount(options.lifeTimeCount, self.maxKeepAliveCount); /** * The maximum number of notifications that the Client wishes to receive in a * single Publish response. A value of zero indicates that there is no limit. * The number of notifications per Publish is the sum of monitoredItems in the * DataChangeNotification and events in the EventNotificationList. * * @property maxNotificationsPerPublish * @type {Number} * #default 0 */ self.maxNotificationsPerPublish = _adjust_maxNotificationsPerPublish(options.maxNotificationsPerPublish); self._life_time_counter = 0; self.resetLifeTimeCounter(); // notification message that are ready to be sent to the client self._pending_notifications = new Dequeue(); self._sent_notifications = []; self._sequence_number_generator = new SequenceNumberGenerator(); // initial state of the subscription self.state = SubscriptionState.CREATING; self.publishIntervalCount = 0; self.monitoredItems = {}; // monitored item map /** * number of monitored Item * @property monitoredItemIdCounter * @type {Number} */ self.monitoredItemIdCounter = 0; self.publishingEnabled = _adjust_publishinEnable(options.publishingEnabled); self.timerId = null; self._start_timer(); //xx console.log("wxxxxx ",self.toString()); } util.inherits(Subscription, EventEmitter); var ObjectRegisty = require("lib/misc/objectRegistry").ObjectRegisty; var g_running_Subscriptions = new ObjectRegisty(); Subscription.getRunningSubscriptionsCount = g_running_Subscriptions.count.bind(g_running_Subscriptions); Subscription.prototype.toString = function () { var self = this; var str = ""; str += " publishingEnabled " + self.publishingEnabled + "\n"; str += " maxKeepAliveCount " + self.maxKeepAliveCount + "\n"; str += " publishingInterval " + self.publishingInterval + "\n"; str += " lifeTimeCount " + self.lifeTimeCount + "\n"; str += " maxKeepAliveCount " + self.maxKeepAliveCount + "\n"; return str; }; /** * @method modify * @param param {Object} * @param param.requestedPublishingInterval {Duration} requestedPublishingInterval =0 means fastest possible * @param param.requestedLifetimeCount {Counter} requestedLifetimeCount ===0 means no change * @param param.requestedMaxKeepAliveCount {Counter} requestedMaxKeepAliveCount ===0 means no change * @param param.maxNotificationsPerPublish {Counter} * @param param.priority {Byte} * */ Subscription.prototype.modify = function (param) { var self = this; var publishingInterval_old = self.publishingInterval; param.requestedPublishingInterval = param.requestedPublishingInterval || 0; param.requestedMaxKeepAliveCount = param.requestedMaxKeepAliveCount || self.maxKeepAliveCount; param.requestedLifetimeCount = param.requestedLifetimeCount || self.lifeTimeCount; self.publishingInterval = _adjust_publishing_interval(param.requestedPublishingInterval); self.maxKeepAliveCount = _adjust_maxKeepAliveCount(param.requestedMaxKeepAliveCount); self.lifeTimeCount = _adjust_lifeTimeCount(param.requestedLifetimeCount, self.maxKeepAliveCount); self.maxNotificationsPerPublish = param.maxNotificationsPerPublish; self.priority = param.priority; self.resetLifeTimeAndKeepAliveCounters(); if (publishingInterval_old !== self.publishingInterval) { // todo } self._stop_timer(); self._start_timer(); }; Subscription.prototype._stop_timer = function () { var self = this; if (self.timerId) { //xx console.log("xxxx Subscription.prototype._stop_timer".bgWhite.red); clearInterval(self.timerId); self.timerId = null; g_running_Subscriptions.unregister(self); } }; Subscription.prototype._start_timer = function () { //xx console.log("xxxx Subscription.prototype._start_timer".bgWhite.blue); var self = this; assert(self.timerId === null); // from the spec: // When a Subscription is created, the first Message is sent at the end of the first publishing cycle to // inform the Client that the Subscription is operational. A NotificationMessage is sent if there are // Notifications ready to be reported. If there are none, a keep-alive Message is sent instead that // contains a sequence number of 1, indicating that the first NotificationMessage has not yet been sent. // This is the only time a keep-alive Message is sent without waiting for the maximum keep-alive count // to be reached, as specified in (f) above. // subscription is now in Normal state self.state = SubscriptionState.NORMAL; // make sure that a keep-alive Message will be send at the end of the first publishing cycle // if there are no Notifications ready. self._keep_alive_counter = self.maxKeepAliveCount; assert(self.publishingInterval > 0); self.timerId = setInterval(self._tick.bind(self), self.publishingInterval); g_running_Subscriptions.register(self); }; // counter Subscription.prototype._get_next_sequence_number = function () { return this._sequence_number_generator.next(); }; // counter Subscription.prototype._get_future_sequence_number = function () { return this._sequence_number_generator.future(); }; Subscription.prototype.setPublishingMode = function (publishingEnabled) { this.publishingEnabled = !!publishingEnabled; return StatusCodes.Good; }; /** * _attempt_to_publish_notification send a "notification" event: * * @method _attempt_to_publish_notification * * @private * * the event receiver shall implement a method that looks like: * * @example * * subscription.on("notification",function() { * if (can_publish) { * var notification = subscription.popNotificationToSend(); * send_notification(notification); * } * }); * */ Subscription.prototype._attempt_to_publish_notification = function () { var self = this; assert(self.hasPendingNotifications); self.emit("notification"); var publishEngine = self.publishEngine; if (publishEngine.pendingPublishRequestCount > 0) { var subscriptionId = self.id; var notificationMessage = self.popNotificationToSend().notification; var availableSequenceNumbers = (self.state === SubscriptionState.CLOSED) ? [] : self.getAvailableSequenceNumbers(); assert(notificationMessage.hasOwnProperty("sequenceNumber")); assert(notificationMessage.hasOwnProperty("notificationData")); var moreNotifications = self.hasPendingNotifications; publishEngine.send_notification_message({ subscriptionId: subscriptionId, sequenceNumber: notificationMessage.sequenceNumber, notificationData: notificationMessage.notificationData, availableSequenceNumbers: availableSequenceNumbers, moreNotifications: moreNotifications }); if (doDebug) { debugLog("Subscription sending a notificationMessage ", subscriptionId, notificationMessage.toString()); } if (self.state === SubscriptionState.LATE || self.state === SubscriptionState.KEEPALIVE) { self.state = SubscriptionState.NORMAL; } } else { if (self.state === SubscriptionState.CLOSED) { debugLog("Subscription has been closed"); return; } if (self.state !== SubscriptionState.LATE) { debugLog("Subscription is now Late due to a lack of PublishRequest to process"); } // publishEngine has no PublishRequest available to process : self.state = SubscriptionState.LATE; //xx console.log(" _attempt_to_publish_notification self.state = SubscriptionState.LATE"); } }; Subscription.prototype.process_subscription = function () { var self = this; // collect notification from monitored items self.prepareNotification(); //xx console.log("xxxxx process_subscription",self.hasPendingNotifications); if (self.hasPendingNotifications && self.publishingEnabled) { self._attempt_to_publish_notification(); if (self.state === SubscriptionState.NORMAL && self.hasPendingNotifications > 0) { // TO DO : fix me , //xx console.log("xxxxx pendingPublishRequestCount > 0 && normal state => retrigger tick event immediately "); setImmediate(self._tick.bind(self)); } } else { self.increaseKeepAliveCounter(); if (self.keepAliveCounterHasExpired) { if (self._sendKeepAliveResponse()) { self.resetLifeTimeAndKeepAliveCounters(); } else { assert(false, " ?????"); self.state = SubscriptionState.LATE; } } } }; /** * @method _tick * @private */ Subscription.prototype._tick = function () { var self = this; if (self.publishEngine._on_tick) { self.publishEngine._on_tick(); } self.publishIntervalCount += 1; /** * request a notification update from the subscription owner. * @event perform_update * * this event is sent when the subscription requires a data update. it is up to the subscription owner to * perform the necessary operations to update the monitored values. * */ self.emit("perform_update"); // collect notification from monitored items self.prepareNotification(); self.increaseLifeTimeCounter(); self.discardOldSentNotifications(); if (self.lifeTimeHasExpired) { console.log(" Subscription has expired !!!!! => Terminating".red.bold); /** * notify the subscription owner that the subscription has expired by exceeding its life time. * @event expired * */ self.emit("expired"); // kill timer and delete monitored items self.terminate(); } else { var publishEngine = self.publishEngine; if (publishEngine.pendingPublishRequestCount === 0) { //xx console.log("xxxxxxxxxxxxxxxxxxxxx normal tick -> LATE"); self.state = SubscriptionState.LATE; return; } //xx console.log("xxxxxxxxxxxxxxxxxxxxx normal tick"); self.process_subscription(); } }; /** * @method _sendKeepAliveResponse * @private */ Subscription.prototype._sendKeepAliveResponse = function () { var self = this; var future_sequence_number = self._get_future_sequence_number(); if (self.publishEngine.send_keep_alive_response(self.id, future_sequence_number)) { /** * notify the subscription owner that a keepalive message has to be sent. * @event keepalive * */ self.emit("keepalive", future_sequence_number); return true; } return false; }; /** * @method resetKeepAliveCounter * @private * Reset the Lifetime Counter Variable to the value specified for the lifetime of a Subscription in * the CreateSubscription Service( 5.13.2). */ Subscription.prototype.resetKeepAliveCounter = function () { var self = this; self._keep_alive_counter = 0; }; /** * @method increaseKeepAliveCounter * @private */ Subscription.prototype.increaseKeepAliveCounter = function () { var self = this; self._keep_alive_counter += 1; }; /** * @property keepAliveCounterHasExpired * @private * @type {Boolean} true if the keep alive counter has reach its limit. */ Subscription.prototype.__defineGetter__("keepAliveCounterHasExpired", function () { var self = this; return self._keep_alive_counter >= self.maxKeepAliveCount; }); /** * Reset the Lifetime Counter Variable to the value specified for the lifetime of a Subscription in * the CreateSubscription Service( 5.13.2). * @method resetLifeTimeCounter * @private */ Subscription.prototype.resetLifeTimeCounter = function () { var self = this; self._life_time_counter = 0; }; /** * @method increaseLifeTimeCounter * @private */ Subscription.prototype.increaseLifeTimeCounter = function () { var self = this; self._life_time_counter += 1; }; /** * True if the subscription life time has expired. * * @property lifeTimeHasExpired * @type {boolean} - true if the subscription life time has expired. */ Subscription.prototype.__defineGetter__("lifeTimeHasExpired", function () { var self = this; assert(self.lifeTimeCount > 0); return self._life_time_counter >= self.lifeTimeCount; }); /** * number of milliseconds before this subscription times out (lifeTimeHasExpired === true); * @property timeToExpiration * @type {Number} */ Subscription.prototype.__defineGetter__("timeToExpiration", function () { var self = this; return (self.lifeTimeCount - self._life_time_counter ) * self.publishingInterval; }); /** * * the server invokes the resetLifeTimeAndKeepAliveCounters method of the subscription * when the server has send a Publish Response, so that the subscription * can reset its life time counter. * * @method resetLifeTimeAndKeepAliveCounters * */ Subscription.prototype.resetLifeTimeAndKeepAliveCounters = function () { var self = this; self.resetLifeTimeCounter(); self.resetKeepAliveCounter(); }; /** * Terminates the subscription. * @method terminate * * Calling this method will also remove any monitored items. * */ Subscription.prototype.terminate = function () { var self = this; if (self.state === SubscriptionState.CLOSED) { // todo verify if asserting is required here return; } assert(self.state !== SubscriptionState.CLOSED, "terminate already called ?"); // stop timer //xx console.log("xxxxxxx //xx subscription.terminate();".cyan.bgWhite); self._stop_timer(); debugLog("terminating Subscription ", self.id, " with ", self.monitoredItemCount, " monitored items"); // dispose all monitoredItem var keys = Object.keys(self.monitoredItems); keys.forEach(function (key) { var status = self.removeMonitoredItem(key); assert(status === StatusCodes.Good); }); assert(self.monitoredItemCount === 0); // notify new terminated status debugLog("adding StatusChangeNotification notification message for BadTimeout subscription = ", self.id); self.addNotificationMessage(new StatusChangeNotification({statusCode: StatusCodes.BadTimeout})); self.state = SubscriptionState.CLOSED; /** * notify the subscription owner that the subscription has been terminated. * @event "terminated" */ self.emit("terminated"); }; /** * @method addNotificationMessage * @param notificationData {DataChangeNotification|EventNotification|StatusChangeNotification} */ Subscription.prototype.addNotificationMessage = function (notificationData) { //xx console.log("xxxxx addNotificationMessage".yellow,notificationData.toString()); var self = this; assert(_.isObject(notificationData)); var notification_message = new NotificationMessage({ sequenceNumber: self._get_next_sequence_number(), publishTime: new Date(), notificationData: [notificationData] }); self._pending_notifications.push({ notification: notification_message, start_tick: self.publishIntervalCount, publishTime: new Date(), sequenceNumber: notification_message.sequenceNumber }); }; Subscription.prototype.getMessageForSequenceNumber = function (sequenceNumber) { var self = this; function filter_func(e) { return e.sequenceNumber === sequenceNumber; } var notification_message = _.find(self._sent_notifications, filter_func); if (!notification_message) { return null; } self.resetLifeTimeAndKeepAliveCounters(); return notification_message; }; /** * Extract the next Notification that is ready to be sent to the client. * @method popNotificationToSend * @return {NotificationMessage} the Notification to send._pending_notifications */ Subscription.prototype.popNotificationToSend = function () { var self = this; assert(self.hasPendingNotifications); var notification_message = self._pending_notifications.shift(); self._sent_notifications.push(notification_message); self.resetLifeTimeAndKeepAliveCounters(); return notification_message; }; /** * returns true if the notification has expired * @method notificationHasExpired * @param notification * @return {boolean} */ Subscription.prototype.notificationHasExpired = function (notification) { var self = this; assert(notification.hasOwnProperty("start_tick")); assert(_.isFinite(notification.start_tick + self.maxKeepAliveCount)); return (notification.start_tick + self.maxKeepAliveCount) < self.publishIntervalCount; }; var maxNotificationMessagesInQueue = 100; /** * discardOldSentNotification find all sent notification message that have expired keep-alive * and destroy them. * @method discardOldSentNotifications * @private * * Subscriptions maintain a retransmission queue of sent NotificationMessages. * NotificationMessages are retained in this queue until they are acknowledged or until they have * been in the queue for a minimum of one keep-alive interval. * */ Subscription.prototype.discardOldSentNotifications = function () { var self = this; // Sessions maintain a retransmission queue of sent NotificationMessages. NotificationMessages // are retained in this queue until they are acknowledged. The Session shall maintain a // retransmission queue size of at least two times the number of Publish requests per Session the // Server supports. Clients are required to acknowledge NotificationMessages as they are received. In the // case of a retransmission queue overflow, the oldest sent NotificationMessage gets deleted. If a // Subscription is transferred to another Session, the queued NotificationMessages for this // Subscription are moved from the old to the new Session. if (maxNotificationMessagesInQueue <= self._sent_notifications) { self._sent_notifications(0, self._sent_notifications.length - maxNotificationMessagesInQueue); } // //var arr = _.filter(self._sent_notifications,function(notification){ // return self.notificationHasExpired(notification); //}); //var results = arr.map(function(notification){ // return self.acknowledgeNotification(notification.sequenceNumber); //}); //xx return results; }; function getSequenceNumbers(arr) { return arr.map(function (e) { return e.notification.sequenceNumber; }); } /** * returns in an array the sequence numbers of the notifications that haven't been * acknowledged yet. * * @method getAvailableSequenceNumbers * @return {Integer[]} * */ Subscription.prototype.getAvailableSequenceNumbers = function () { var self = this; var availableSequenceNumbers = getSequenceNumbers(self._sent_notifications); var pendingSequenceNumbers = getSequenceNumbers(self._pending_notifications); return [].concat(availableSequenceNumbers, pendingSequenceNumbers); }; /** * @method acknowledgeNotification * @param sequenceNumber {Number} * @return {StatusCode} */ Subscription.prototype.acknowledgeNotification = function (sequenceNumber) { var self = this; var foundIndex = -1; _.find(self._sent_notifications, function (e, index) { if (e.sequenceNumber === sequenceNumber) { foundIndex = index; } }); if (foundIndex === -1) { return StatusCodes.BadSequenceNumberUnknown; } else { self._sent_notifications.splice(foundIndex, 1); return StatusCodes.Good; } }; /** * * @property pendingNotificationsCount - number of pending notifications * @type {Number} */ Subscription.prototype.__defineGetter__("pendingNotificationsCount", function () { return this._pending_notifications.length; }); /** * return True is there are pending notifications for this subscription. (i.e moreNotifications) * * @property hasPendingNotifications * @type {Boolean} */ Subscription.prototype.__defineGetter__("hasPendingNotifications", function () { var self = this; return self.pendingNotificationsCount > 0; }); /** * number of sent notifications * @property sentNotificationsCount * @type {Number} */ Subscription.prototype.__defineGetter__("sentNotificationsCount", function () { return this._sent_notifications.length; }); /** * number of monitored items. * @property monitoredItemCount * @type {Number} */ Subscription.prototype.__defineGetter__("monitoredItemCount", function () { return Object.keys(this.monitoredItems).length; }); var MonitoredItem = require("lib/server/monitored_item").MonitoredItem; var MonitoredItemCreateRequest = require("lib/services/subscription_service").MonitoredItemCreateRequest; /** * adjust monitored item sampling interval * - an samplingInterval ===0 means that we use a event-base model ( no sampling) * - otherwise the sampling is adjusted * * @method adjustSamplingInterval * @param samplingInterval * @param node * @return {number|*} * @private */ Subscription.prototype.adjustSamplingInterval = function (samplingInterval, node) { var self = this; if (samplingInterval < 0) { // - The value -1 indicates that the default sampling interval defined by the publishing // interval of the Subscription is requested. // - Any negative number is interpreted as -1. samplingInterval = self.publishingInterval; } else if (samplingInterval === 0 ) { // OPCUA 1.0.3 Part 4 - 5.12.1.2 // The value 0 indicates that the Server should use the fastest practical rate. // The fastest supported sampling interval may be equal to 0, which indicates // that the data item is exception-based rather than being sampled at some period. // An exception-based model means that the underlying system does not require sampling and reports data changes. var dataValueSamplingInterval = node.readAttribute(AttributeIds.MinimumSamplingInterval); // TODO if attributeId === AttributeIds.Value : sampling interval required here if (dataValueSamplingInterval.statusCode === StatusCodes.Good) { // node provides a Minimum sampling interval ... samplingInterval = dataValueSamplingInterval.value.value; assert(samplingInterval >=0 && samplingInterval <= MonitoredItem.maximumSamplingInterval); // note : at this stage, a samplingInterval===0 means that the data item is really exception-based } } else if (samplingInterval < MonitoredItem.minimumSamplingInterval) { samplingInterval = MonitoredItem.minimumSamplingInterval; } else if (samplingInterval > MonitoredItem.maximumSamplingInterval) { // If the requested samplingInterval is higher than the // maximum sampling interval supported by the Server, the maximum sampling // interval is returned. samplingInterval = MonitoredItem.maximumSamplingInterval; } var node_minimumSamplingInterval = (node && node.minimumSamplingInterval) ? node.minimumSamplingInterval : 0; samplingInterval = Math.max(samplingInterval, node_minimumSamplingInterval); return samplingInterval; }; var checkSelectClauses = require("lib/tools/tools_event_filter").checkSelectClauses; function analyseEventFilterResult(node, eventFilter) { assert(eventFilter instanceof subscription_service.EventFilter); var selectClauseResults = checkSelectClauses(node, eventFilter.selectClauses); var whereClauseResult = new subscription_service.ContentFilterResult(); return new subscription_service.EventFilterResult({ selectClauseResults: selectClauseResults, selectClauseDiagnosticInfos: [], whereClauseResult: whereClauseResult }); } function analyseDataChangeFilterResult(node, dataChangeFilter) { assert(dataChangeFilter instanceof subscription_service.DataChangeFilter); // the opcu specification doesn't provide dataChangeFilterResult return null; } function analyseAggregateFilterResult(node, aggregateFilter) { assert(aggregateFilter instanceof subscription_service.AggregateFilter); return new subscription_service.AggregateFilterResult({}); } function _process_filter(node, filter) { if (!filter) { return null; } if (filter instanceof subscription_service.EventFilter) { return analyseEventFilterResult(node, filter); } else if (filter instanceof subscription_service.DataChangeFilter) { return analyseDataChangeFilterResult(node, filter); } else if (filter instanceof subscription_service.AggregateFilter) { return analyseAggregateFilterResult(node, filter); } // istanbul ignore next assert(false || ""); } var UAVariable = require("lib/address_space/ua_variable").UAVariable; var NodeId = require("lib/datamodel/nodeid").NodeId; function __validateDataChangeFilter(filter,itemToMonitor,node) { assert(itemToMonitor.attributeId === AttributeIds.Value); assert(filter instanceof subscription_service.DataChangeFilter); assert(node instanceof UAVariable); // if node is not Numerical=> DataChangeFilter assert(node.dataType instanceof NodeId); var dataType = node.__address_space.findDataType(node.dataType); var dataTypeNumber = node.__address_space.findDataType("Number"); if (!dataType.isSupertypeOf(dataTypeNumber)) { return StatusCodes.BadFilterNotAllowed; } if (filter.deadbandType === subscription_service.DeadbandType.Percent) { if (filter.deadbandValue <= 0 || filter.deadbandValue >= 100) { return StatusCodes.BadDeadbandFilterInvalid; } } return StatusCodes.Good; } function __validateFilter(filter, itemToMonitor ,node) { // handle filter information if (filter && filter instanceof subscription_service.EventFilter && itemToMonitor.attributeId !== AttributeIds.EventNotifier) { // invalid filter on Event return StatusCodes.BadFilterNotAllowed; } if (filter && filter instanceof subscription_service.DataChangeFilter && itemToMonitor.attributeId !== AttributeIds.Value) { // invalid DataChange filter on non Value Attribute return StatusCodes.BadFilterNotAllowed; } if (filter && itemToMonitor.attributeId !== AttributeIds.EventNotifier && itemToMonitor.attributeId !== AttributeIds.Value) { return StatusCodes.BadFilterNotAllowed; } if (filter instanceof subscription_service.DataChangeFilter) { return __validateDataChangeFilter(filter,itemToMonitor,node); } return StatusCodes.Good; } var is_valid_dataEncoding = require("lib/misc/data_encoding").is_valid_dataEncoding; Subscription.prototype.createMonitoredItem = function (address_space, timestampsToReturn, monitoredItemCreateRequest) { var self = this; assert(address_space.constructor.name === "AddressSpace"); assert(monitoredItemCreateRequest instanceof MonitoredItemCreateRequest); function handle_error(statusCode) { return new subscription_service.MonitoredItemCreateResult({statusCode: statusCode}); } var itemToMonitor = monitoredItemCreateRequest.itemToMonitor; var node = address_space.findObject(itemToMonitor.nodeId); if (!node) { return handle_error(StatusCodes.BadNodeIdUnknown); } if (itemToMonitor.attributeId === AttributeIds.INVALID) { return handle_error(StatusCodes.BadAttributeIdInvalid); } if (!itemToMonitor.indexRange.isValid()) { return handle_error(StatusCodes.BadIndexRangeInvalid); } // check dataEncoding applies only on Values if (itemToMonitor.dataEncoding.name && itemToMonitor.attributeId !== AttributeIds.Value) { return handle_error(StatusCodes.BadDataEncodingInvalid); } // check dataEncoding if (!is_valid_dataEncoding(itemToMonitor.dataEncoding)) { return handle_error(StatusCodes.BadDataEncodingUnsupported); } // filter var requestedParameters = monitoredItemCreateRequest.requestedParameters; var filter = requestedParameters.filter; var statusCodeFilter = __validateFilter(filter, itemToMonitor , node); if (statusCodeFilter !== StatusCodes.Good) { return handle_error(statusCodeFilter); } //xx var monitoringMode = monitoredItemCreateRequest.monitoringMode; // Disabled, Sampling, Reporting //xx var requestedParameters = monitoredItemCreateRequest.requestedParameters; var monitoredItemCreateResult = self._createMonitoredItemStep2(timestampsToReturn, monitoredItemCreateRequest, node); assert(monitoredItemCreateResult.statusCode === StatusCodes.Good); var monitoredItem = self.getMonitoredItem(monitoredItemCreateResult.monitoredItemId); assert(monitoredItem); // TODO: fix old way to set node. !!!! monitoredItem.node = node; self.emit("monitoredItem", monitoredItem, itemToMonitor); self._createMonitoredItemStep3(monitoredItem,monitoredItemCreateRequest); return monitoredItemCreateResult; }; /** * * @method _createMonitoredItemStep2 * @param timestampsToReturn * @param {MonitoredItemCreateRequest} monitoredItemCreateRequest - the parameters describing the monitored Item to create * @param node {BaseNode} * @return {subscription_service.MonitoredItemCreateResult} * @private */ Subscription.prototype._createMonitoredItemStep2 = function (timestampsToReturn, monitoredItemCreateRequest, node) { var self = this; // note : most of the parameter inconsistencies shall have been handled by the caller // any error here will raise an assert here assert(monitoredItemCreateRequest instanceof MonitoredItemCreateRequest); var itemToMonitor = monitoredItemCreateRequest.itemToMonitor; //xx check if attribute Id invalid (we only support Value or EventNotifier ) //xx assert(itemToMonitor.attributeId !== AttributeIds.INVALID); self.monitoredItemIdCounter += 1; var monitoredItemId = self.monitoredItemIdCounter; var requestedParameters = monitoredItemCreateRequest.requestedParameters; // adjust requestedParameters.samplingInterval requestedParameters.samplingInterval = self.adjustSamplingInterval(requestedParameters.samplingInterval, node); // reincorporate monitoredItemId and itemToMonitor into the requestedParameters requestedParameters.monitoredItemId = monitoredItemId; requestedParameters.itemToMonitor = itemToMonitor; var monitoredItem = new MonitoredItem(requestedParameters); monitoredItem.timestampsToReturn = timestampsToReturn; assert(monitoredItem.monitoredItemId === monitoredItemId); self.monitoredItems[monitoredItemId] = monitoredItem; var filterResult = _process_filter(node, requestedParameters.filter); var monitoredItemCreateResult = new subscription_service.MonitoredItemCreateResult({ statusCode: StatusCodes.Good, monitoredItemId: monitoredItemId, revisedSamplingInterval: monitoredItem.samplingInterval, revisedQueueSize: monitoredItem.queueSize, filterResult: filterResult }); return monitoredItemCreateResult; }; var MonitoringMode = subscription_service.MonitoringMode; Subscription.prototype._createMonitoredItemStep3 = function (monitoredItem,monitoredItemCreateRequest) { function tmp(self) { if (self.samplingFunc) { return; } self.samplingFunc = function(oldData,callback) { callback(null,oldData); } } //tmp(monitoredItem); assert(monitoredItem.monitoringMode === MonitoringMode.Invalid); assert(_.isFunction(monitoredItem.samplingFunc)); var monitoringMode = monitoredItemCreateRequest.monitoringMode; // Disabled, Sampling, Reporting monitoredItem.setMonitoringMode(monitoringMode); }; /** * get a monitoredItem by Id. * @method getMonitoredItem * @param monitoredItemId {Number} the id of the monitored item to get. * @return {MonitoredItem} */ Subscription.prototype.getMonitoredItem = function (monitoredItemId) { assert(_.isFinite(monitoredItemId)); var self = this; return self.monitoredItems[monitoredItemId]; }; /** * getMonitoredItems is used to get information about monitored items of a subscription.Its intended * use is defined in Part 4. This method is the implementation of the Standard OPCUA GetMonitoredItems Method. * @method getMonitoredItems * @param result.serverHandles {Int32[]} Array of serverHandles for all MonitoredItems of the subscription identified by subscriptionId. * result.clientHandles {Int32[]} Array of clientHandles for all MonitoredItems of the subscription identified by subscriptionId. * result.statusCode {StatusCode} * from spec: * This method can be used to get the list of monitored items in a subscription if CreateMonitoredItems failed due to * a network interruption and the client does not know if the creation succeeded in the server. * */ Subscription.prototype.getMonitoredItems = function (/*out*/ result) { result = result || {}; var subscription = this; result.serverHandles = []; result.clientHandles = []; result.statusCode = StatusCodes.Good; Object.keys(subscription.monitoredItems).forEach(function (monitoredItemId) { var monitoredItem = subscription.getMonitoredItem(monitoredItemId); result.clientHandles.push(monitoredItem.clientHandle); // TODO: serverHandle is defined anywhere in the OPCUA Specification 1.02 // I am not sure what shall be reported for serverHandle... // using monitoredItem.monitoredItemId instead... // May be a clarification in the OPCUA Spec is required. result.serverHandles.push(monitoredItemId); }); return result; }; /** * remove a monitored Item from the subscription. * @method removeMonitoredItem * @param monitoredItemId {Number} the id of the monitored item to get. */ Subscription.prototype.removeMonitoredItem = function (monitoredItemId) { debugLog("Removing monitoredIem ", monitoredItemId); assert(_.isFinite(monitoredItemId)); var self = this; if (!self.monitoredItems.hasOwnProperty(monitoredItemId)) { return StatusCodes.BadMonitoredItemIdInvalid; } var monitoredItem = self.monitoredItems[monitoredItemId]; monitoredItem.terminate(); delete self.monitoredItems[monitoredItemId]; return StatusCodes.Good; }; var DataChangeNotification = subscription_service.DataChangeNotification; // collect DataChangeNotification Subscription.prototype.collectDataChangeNotification = function () { var self = this; var all_notifications = new Dequeue(); // visit all monitored items var keys = Object.keys(self.monitoredItems); keys.forEach(function (key) { var monitoredItem = self.monitoredItems[key]; var notifications = monitoredItem.extractMonitoredItemNotifications(); all_notifications = all_notifications.concat(notifications); }); assert(all_notifications instanceof Dequeue); function extract_monitoredItems(monitoredItems, maxNotificationsPerPublish) { var n = maxNotificationsPerPublish === 0 ? monitoredItems.length : Math.min(monitoredItems.length, maxNotificationsPerPublish); var chunk_monitoredItems = new Dequeue(); while (n) { chunk_monitoredItems.push(monitoredItems.shift()); n--; } return chunk_monitoredItems; } var dataChangeNotifications = new Dequeue(); while (all_notifications.length > 0) { // split into one or multiple dataChangeNotification with no more than // self.maxNotificationsPerPublish monitoredItems var chunk_monitoredItems = extract_monitoredItems(all_notifications, self.maxNotificationsPerPublish); var dataChangeNotification = new DataChangeNotification({ monitoredItems: chunk_monitoredItems.toArray(), diagnosticInfos: [] }); assert(self.maxNotificationsPerPublish === 0 || dataChangeNotification.monitoredItems.length <= self.maxNotificationsPerPublish); // //xx console.log("xxxxxxxxx Pushing dataChangeNotifications "); //xx console.log("xxxxxxxxxxxxxxx monitoredItems length = ".yellow,dataChangeNotification.monitoredItems.length); dataChangeNotifications.push(dataChangeNotification); } return dataChangeNotifications; }; Subscription.prototype.prepareNotification = function () { var self = this; if (self.publishEngine.pendingPublishRequestCount === 0) { return; } // Only collect data change notification for the var dataChangeNotifications = self.collectDataChangeNotification(); assert(dataChangeNotifications instanceof Dequeue); //xx console.log("xxxxxx prepareNotification dataChangeNotifications.length",dataChangeNotifications.length); dataChangeNotifications.forEach(function (notificationMessage) { //xx console.log("xxxxxx before self.addNotificationMessage",dataChangeNotifications.length); self.addNotificationMessage(notificationMessage); }); }; exports.Subscription = Subscription;
module.exports = function (Restangular) { return function (gain) { var deferred = new $.Deferred(); Restangular .all("codes") .post({type: "promo", number: gain}) .then(deferred.resolve, deferred.reject); return deferred.promise(); }; };
/* Lyle Scott, III * [email protected] * http://digitalfoo.net */ DEBUG = true var KEY_ENTER = 13; var HOST = 'http://localhost:5000'; var identifier = location.hostname + '_' + $.browser.version.replace(/[^A-Za-z0-9]+/, ''); function debug(msg) { if (DEBUG && typeof console !== undefined) { console.log(msg); } } debug('identifier: ' + identifier); // // getAutocompletes // // function getAutocompletesAjaxCall(query) { // Make an AJAX call to get a list of autocompletes. var args = {'n': 2, 'mark': true, 'query': query, 'mark': true, 'markL': '<strong>', 'markR': '</strong>'} var args_ = '?'; for (i in args) { args_ += i + '=' + args[i] + '&'; } $.ajax({ url: HOST + '/' + identifier + args_, type: 'OPTIONS', datatype: 'json', success: getAutocompletesCallback, error: ajaxError }); } function getAutocompletesCallback(data, textStatus, jqXHR) { // Show possible autocompletes to user. var json = JSON.parse(data); if (typeof json == undefined) { debug('submitAutocompletesCallback: !json:'); debug(json); return; } var html = json.join('<br>'); $('#suggestions').html(html); } // // submitQuery // // function submitQueryAjaxCall(query) { // Make an AJAX call to put a query into storage. $.ajax({ url: HOST + '/' + identifier, type: 'PUT', data: {'query': query}, datatype: 'json', success: submitQueryCallback, error: ajaxError }); } function submitQueryCallback(data, textStatus, jqXHR) { // Check for a successful return and show a success message. var json = JSON.parse(data); if (json != true) { debug('submitQueryCallback: !json:'); debug(json); return; } $('#status').html('query added...'); setTimeout(function() { $('#status').html('<br>'); getExistingQueriesAjaxCall(); }, 500); } // // getExisting // // function getExistingQueriesAjaxCall() { // Make an AJAX call to get a list of suggestions. $.ajax({ url: HOST + '/' + identifier, type: 'GET', datatype: 'json', success: getExistingQueriesCallback, error: ajaxError }); } function getExistingQueriesCallback(data, textStatus, jqXHR) { // Get all existing queries and print them in the #existing div. var json = JSON.parse(data); if (!json || json.length == 0) { debug('getExistingQueriesCallback: !json:'); debug(json); return; } var html = json.join('<br>'); $('#existing').html(html); } // // Utilities. // // function ajaxError(jqXHR, textStatus, errorThrown) { // Attempt to gracefully print an error in a jQuery AJAX call. debug('jQuery AJAX error:'); debug(textStatus); debug(errorThrown); $('#existing').html('There was a problem communicating with the server.'); } $(document).ready(function(){ getExistingQueriesAjaxCall(); $("#query").bind('keyup', function(event) { // Bail if no input in the text entry. if (!$(this).val()) { return; } var query = $(this).val(); if (event && event.keyCode == KEY_ENTER) { // Add the query to the collection of known queries. submitQueryAjaxCall(query); $(this).val(''); $('#suggestions').html(''); } else { // Get suggestions based on what the user is typing. getAutocompletesAjaxCall(query); } }); // query keyup }); // end document ready
import PageObject from 'kube-admin/tests/page-object'; let { text, count, clickable } = PageObject; export default { submit: clickable('[data-autoid=submit-file]'), error: text('.ui.negative.message'), success: text('.ui.positive.message'), messageCount: count('.ui.message'), create(manifest) { this.visit(); triggerFileSelected('input:file', JSON.stringify(manifest)); return this.submit(); }, createError(code) { let manifest = JSON.parse(JSON.stringify(this.getFileContent())); manifest.metadata.name = `error-${code}`; return this.create(manifest); }, createWithNamespace() { let manifest = JSON.parse(JSON.stringify(this.getFileContent())); manifest.metadata.namespace = 'fake'; return this.create(manifest); }, createWithoutNamespace() { return this.create(this.getFileContent()); }, createEmptyNamespace() { let manifest = JSON.parse(JSON.stringify(this.getFileContent())); manifest.metadata.namespace = ''; return this.create(manifest); } };
'use strict'; require('../../init'); let createClient = require('../../../lib/client'); const async = require('async'); describe('removeStream', function() { it('Should work', function(done) { createClient({ legacy: true }, clientCreated); function clientCreated(err, client) { if (err) { return client.close(() => done()); } let data = { client }; async.waterfall( [ async.apply(getClientIdAndActiveVessel, data), connectToStreamServer, getVesselControl, addThrottleToStream, removeStream ], function(waterfallErr) { if (waterfallErr) { return client.close(() => done(waterfallErr)); } return client.close(() => done()); } ); } }); }); function getFirstResult(response) { return getResultN(response, 0); } function getResultN(response, n) { expect(response.error).to.not.be.ok(); let result = response.results[n]; expect(result.error).to.not.be.ok(); return result.value; } function getClientIdAndActiveVessel(data, callback) { let calls = [ data.client.services.krpc.getClientId(), data.client.services.spaceCenter.getActiveVessel() ]; data.client.send(calls, function(err, response) { if (err) { return callback(err); } data.clientId = getResultN(response, 0).toString('base64'); data.vessel = { id: getResultN(response, 1) }; return callback(null, data); }); } function connectToStreamServer(data, callback) { data.client.connectToStreamServer(data.clientId, function(err) { return callback(err, data); }); } function getVesselControl(data, callback) { data.client.send(data.client.services.spaceCenter.vesselGetControl(data.vessel.id), function( err, response ) { if (err) { return callback(err); } data.vessel.controlId = getFirstResult(response); return callback(null, data); }); } function addThrottleToStream(data, callback) { let getThrottle = data.client.services.spaceCenter.controlGetThrottle(data.vessel.controlId); data.client.addStream(getThrottle, 'Throttle', throttleStreamAdded); function throttleStreamAdded(err) { return callback(err, data); } } function removeStream(data, callback) { data.client.removeStream('Throttle', throttleStreamRemoved); function throttleStreamRemoved(err) { return callback(err); } }
angular.module('category.category').controller('CategoryController', ['$scope', '$routeParams', '$location', 'Global', 'Category', function ($scope, $routeParams, $location, Global, Category) { $scope.global = Global; $scope.find = function () { Category.query(function (category) { $scope.category = category; }); }; }]);
import React, { Component } from 'react' import Footer from './Footer/Footer' import ReactDOM from 'react-dom' import { newPassword } from '../actions/user' import { connect } from 'react-redux' import { bindActionCreators } from 'redux' import Auth from '../helpers/token' class EditPassword extends Component { constructor(props) { super(props) this.state = { password: "", confirmPassword: "", } this.onChange = this.onChange.bind(this) this.onSubmit = this.onSubmit.bind(this) } onChange(e) { this.setState({ [e.target.name]: e.target.value }); } componentDidMount(){ !Auth.getToken() ? this.props.router.replace('/login') : this.props.router.replace('/edit-password') } onSubmit(e) { e.preventDefault() if(this.state.password !== this.state.confirmPassword) { alert('Password do not match') return ReactDOM.findDOMNode(this.refs.confirmPassword).focus(); } // call dispatch to state this.props.newPassword(this.state.password) } render() { const {password, confirmPassword } = this.state // isLoggedIn() return ( <div className="components-page"> <div className="wrapper"> <div id="new-idea-intro" className="header header-filter"> <div className="container"> <div className="row text-center"> <h1 className="title">Edit Password</h1> <h5>Change password of your account</h5> </div> </div> </div> <div className="main"> <div className="container"> <div className="row"> <div className="card form-card"> <div className="card-header" data-background-color="blue"> <h4 className="title">Edit Password</h4> </div> <div className="card-signup"> <div className="container"> <form onSubmit={this.onSubmit}> <div className="form-group label-floating"> <label className="control-label">New Password</label> <input type="password" className="form-control" name="password" value={password} onChange={this.onChange} pattern=".{8,}" required title="Minimum Password is 8 Characters" /> </div> <div className="form-group label-floating"> <label className="control-label">Confirm New Password</label> <input type="password" className="form-control" ref="confirmPassword" name="confirmPassword" value={confirmPassword} onChange={this.onChange} pattern=".{8,}" required title="Minimum Password is 8 Characters" /> </div> <div className="form-group"> <button type="submit" className="btn btn-info">Submit</button> </div> </form> </div> </div> </div> </div> </div> </div> </div> <Footer /> </div> ) } } function mapDispatchToProps(dispatch) { return { newPassword: bindActionCreators(newPassword, dispatch) } } export default connect(null, mapDispatchToProps)(EditPassword)
/* * Orange angular-swagger-ui - v0.2.1 * * (C) 2015 Orange, all right reserved * MIT Licensed */ 'use strict'; angular .module('swaggerUi') .service('swaggerClient', ['$q', '$http', 'swaggerModules', function($q, $http, swaggerModules) { var baseUrl; /** * format API explorer response before display */ function formatResult(deferred, response) { var query = '', data = response.data, config = response.config; if (config.params) { var parts = []; for (var key in config.params) { parts.push(key + '=' + encodeURIComponent(config.params[key])); } if (parts.length > 0) { query = '?' + parts.join('&'); } } deferred.resolve({ url: config.url + query, response: { body: data ? (angular.isString(data) ? data : angular.toJson(data, true)) : 'no content', status: response.status, headers: angular.toJson(response.headers(), true) } }); } /** * Send API explorer request */ this.send = function(swagger, operation, values) { var deferred = $q.defer(), query = {}, headers = {}, path = operation.path, body = null; // build request parameters for (var i = 0, params = operation.parameters || [], l = params.length; i < l; i++) { //TODO manage 'collectionFormat' (csv etc.) !! var param = params[i], value = values[param.name]; switch (param.in) { case 'query': if (!!value) { query[param.name] = value; } break; case 'path': path = path.replace('{' + param.name + '}', encodeURIComponent(value)); break; case 'header': if (!!value) { headers[param.name] = value; } break; case 'formData': values.body = values.body || new FormData(); if (!!value) { if (param.type === 'file') { values.contentType = undefined; // make browser defining it by himself } values.body.append(param.name, value); } break; case 'body': if(values.contentType === 'application/json') { body = JSON.stringify(values.body); } body = body || value; break; } } // add headers headers.Accept = values.responseType; headers['Content-Type'] = values.body ? values.contentType : 'text/plain'; if (!baseUrl) { // build base URL baseUrl = [ swagger.schemes[0], '://', swagger.host, swagger.basePath || '' ].join(''); } // build request //FIXME should use server hosting the documentation if scheme or host are not defined var options = { method: operation.httpMethod, url: baseUrl + path, headers: headers, data: body, params: query }, callback = function(data, status, headers, config) { // execute modules var response = { data: data, status: status, headers: headers, config: config }; swaggerModules .execute(swaggerModules.AFTER_EXPLORER_LOAD, response) .then(function() { formatResult(deferred, response); }); }; // execute modules swaggerModules .execute(swaggerModules.BEFORE_EXPLORER_LOAD, options) .then(function() { // send request $http(options) .success(callback) .error(callback); }); return deferred.promise; }; }]);
/* Careful foxy watches all incoming messages: catches mentions, or can play with other bots or users */ let botCommands = undefined; String.prototype.regexIndexOf = function(regex, startpos) { let indexOf = this.substring(startpos || 0).search(regex); return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf; }; function hasReg(msg, word) { return msg.regexIndexOf(word) !== -1; } // function hasStr(msg, word) // { // return msg.indexOf(word) !== -1; // } // let eggCommands = [ // "$color", // "$commander", // "$egg", // "$hatch", // "$ping", // "$prefix", // "$role", // "$tag", // "$hang", // "$cmd", // "$help", // "$rps", // "$quote", // Dead commands // "$talk", // "$spam" // ]; // Check out is Yoshi021's Egg bot online, if not - notify user // that Egg is "eaten" :-P // function lookUpForEgg(myBot, message, msgLowTrimmed, allowWrite) // { // if(!allowWrite) // return false; // // if(!message.channel || !message.channel.guild || message.channel.guild.id !== "692079249594515607") // return false; // Don't check egg bot out of a home server // // for(let i = 0; i < eggCommands.length; ++i) // { // if(msgLowTrimmed.startsWith(eggCommands[i])) // { // try // { // let egg = myBot.users.resolve("247080657182785546"); // if(egg && egg.presence.status === "offline") // { // message.reply("Sorry, egg is offline... :cooking:", botCommands.msgSendError); // } // } // catch (e) // { // botCommands.sendErrorMsg(myBot, message.channel, e); // } // return true; // } // } // return false; // } // let keyPrefix = [ // "wohl", // "whol", // "wolh", // "wohlstand", // "wholstand", // "wolhstand" // ]; // function lookUpForKeyPrefix(msgLowTrimmed) // { // var forMe = false; // // for(var i = 0; i < keyPrefix.length; i++) // { // if((msgLowTrimmed.indexOf(keyPrefix[i] + ":") === 0) || // (msgLowTrimmed.indexOf(keyPrefix[i] + ",") === 0)) // forMe = true; // } // // return forMe; // } // [ \-_+=\\/!@#$%^&*():;'".,?|`~] // const keyMentions = [ // /\b(?:p[ \-_+=\\/!@#$%^&*():;'".,?|`~]*g[ \-_+=\\/!@#$%^&*():;'".,?|`~]*e)\b/gi, // /\b(?:w[ \-_+=\\/!@#$%^&*():;'".,?|`~]*o[ \-_+=\\/!@#$%^&*():;'".,?|`~]*h[ \-_+=\\/!@#$%^&*():;'".,?|`~]*l)\b/gi, // /\b(?:w[ \-_+=\\/!@#$%^&*():;'".,?|`~]*h[ \-_+=\\/!@#$%^&*():;'".,?|`~]*o[ \-_+=\\/!@#$%^&*():;'".,?|`~]*l)\b/gi, // /\b(?:w[ \-_+=\\/!@#$%^&*():;'".,?|`~]*o[ \-_+=\\/!@#$%^&*():;'".,?|`~]*l[ \-_+=\\/!@#$%^&*():;'".,?|`~]*h)\b/gi, // /w(ohl|hol|olh)e?stand/gi, // /\b(?:moon[ \-_+=\\/!@#$%^&*():;'".,?|`~]*dust)\b/gi, // /\b(?:adl[ \-_+=\\/!@#$%^&*():;'".,?|`~]*midi)\b/gi, // /\b(?:opn[ \-_+=\\/!@#$%^&*():;'".,?|`~]*midi)\b/gi, // /\b(?:sdl2?)\b/gi, // /\b(?:sdl2?[ \-_+=\\/!@#$%^&*():;'".,?|`~]*mixer[ \-_+=\\/!@#$%^&*():;'".,?|`~]*?e?x?t?)\b/gi, // /\b(?:mixer[ \-_+=\\/!@#$%^&*():;'".,?|`~]*x)\b/gi, // /\b(?:midi)\b/gi // ]; // function hasMatches(text, reg) // { // let m = text.match(reg); // return m && m.length > 0; // } // function lookUpForKeyMentions(msgLowTrimmed) // { // let forMe = false; // // for(let i = 0; i < keyMentions.length; i++) // { // if(hasMatches(msgLowTrimmed, keyMentions[i])) // { // botCommands.foxyLogInfo("== Caught a mention of " + keyMentions[i] + "! =="); // forMe = true; // } // } // // return forMe; // } function messageIn(mybot, message, allowWrite) { let msgTrimmed = message.content.trim(); let msgLow = message.content.toLowerCase(); let msgLowTrimmed = msgLow.trim(); let urlMask = /(https?:\/\/wohlsoft.ru\/?[A-Za-z%0-9().\-_\/&?=]*)/g; // Remove any wohlsoft URLs from the text msgTrimmed = msgTrimmed.replace(urlMask, "[url]"); msgLow = msgLow.replace(urlMask, "[url]"); msgLowTrimmed = msgLowTrimmed.replace(urlMask, "[url]"); // if(lookUpForEgg(mybot, message, msgLowTrimmed, allowWrite)) // return; /* *********Auto-replying for some conditions********* */ let wasAsked = false; let messageForMe = false; let messageForMeReact = false; let mentions = message.mentions.users.array(); // for(let i = 0; i < mentions.length; i++) // { // botCommands.foxyLogInfo( "---> " + mentions[i].username + "#" + mentions[i].discriminator); // wasAsked = (mentions[i].id === mybot.user.id); // messageForMe = (mentions[i].id === "182039820879659008") && (message.author.id !== mybot.user.id); // messageForMeReact = messageForMe; // } // // if(lookUpForKeyMentions(msgLowTrimmed)) //Shadow email // messageForMe = true; // // if(lookUpForKeyPrefix(msgLowTrimmed)) //Transparent email // { // messageForMe = true; // messageForMeReact = true; // } // var whoWannaPing = [69055500540456960/*Spinda*/]; // if( (whoWannaPing.indexOf(message.author.id) != -1) // && hasReg(msgLowTrimmed, /don('|"|)t.*(@|ping|call).*(me|you|my)/ig) ) // { // if(hasReg(msgLowTrimmed, /fuck|shit|idiot|jerk/ig)) // message.reply("Don't swear, and disable your notifications instead! :hear_no_evil:"); // else if(hasReg(msgLowTrimmed, /evil/ig)) // message.reply("I'm not evil, I just asking you disable your notificaations! :hear_no_evil:"); // else if(hasReg(msgLowTrimmed, /cute/ig)) // message.reply("O, thanks, but please disable your notifications to don't listen so annoying pings :hear_no_evil:"); // else // message.reply("disable notifications please! :hear_no_evil:"); // } // if(message.author.id === "182039820879659008")//Don't quote me, Foxy!!! // messageForMe = false; // // if(message.author.id === "303661490114789391")//Don't disturb me, Yappy Bot // messageForMe = false; // // if(botCommands.isurl(msgTrimmed))//Also please, don't report me URLs // messageForMe = false; // // if((message.author.id === "216273975939039235") && messageForMe) // { // messageForMeReact = false; //Don't react to LunaBot // if((msgLowTrimmed.indexOf("http://wohlsoft.ru/") === 0) && (msgLowTrimmed.indexOf(" ") === -1)) // messageForMe = false; //Don't report LunaBot's URLs // } //Check is botane offline, and reply on attempt call her let Botane = mybot.users.resolve("216688100032643072"); if(allowWrite && Botane && (Botane.presence.status === "offline")) { if(msgLowTrimmed === "what is horikawa?") { message.reply("Don't try call her, she is dead bot!"); } } if(allowWrite && (wasAsked || message.channel.type === 'dm')) { if(message.author.id === "216688100032643072") // Horikawa Botane { if(msgLow.indexOf("dorkatron") !== -1) { message.reply("maybe you are a Dorkatron? I'm not!"); } } else // Any other { if(msgLow.indexOf("pets") !== -1) { setTimeout(function(){ message.reply("Do you really wanna pet the fox? :fox:"); }, 1000); setTimeout(function() { botCommands.callCommand(mybot, message, "fox", ""); }, 3500); } else if(msgLow.indexOf("hi!") !== -1) { setTimeout(function(){ message.channel.sendFile(__dirname+"/images/hi.gif"); }, 1000); } else if(msgLow.indexOf("hi") !== -1) { setTimeout(function(){ message.reply("Hi!"); }, 1000); } else if(msgLow.indexOf("i like you") !== -1) { setTimeout(function(){ message.reply(":blush:"); }, 1000); } else if(msgLow.indexOf("i love you") !== -1) { setTimeout(function(){ message.reply(":blush:"); }, 1000); } else if(msgLow.indexOf("♥") !== -1) { setTimeout(function(){ message.reply(":blush:"); }, 1000); } else if(msgLow.indexOf("❤") !== -1) { setTimeout(function(){ message.reply(":blush:"); }, 1000); }//♥ ❤ ღ ❦ ❥❣ else if(msgLow.indexOf("🍺") !== -1) { setTimeout(function(){ message.reply(":beers:"); }, 1000); } else if(msgLow.indexOf("🍻") !== -1) { setTimeout(function(){ message.reply(":beers: :beer:"); }, 1000); } else if(hasReg(msgLow, /((f[auo]([ck][ck]|[ck])|[cs][ck]rew)( ||\n)+(you|[yu]|yo|yu|yoo))/ig)) { setTimeout(function(){ message.reply("You so rude! :angry:"); }, 1000); } else if(msgLow.indexOf("🤘") !== -1) { setTimeout(function(){ message.reply("Сool, dude!"); }, 1000); } } } else { // if(messageForMe && message.guild) // { // // Check if Me is on this guild // message.guild.members.fetch("182039820879659008").then(function (gotMember) // { // // Check if Me can see this channel // if(message.channel.permissionsFor(gotMember).has('VIEW_CHANNEL')) // { // console.log("Sending email..."); // botCommands.sendEmail(message, message.content, false); // if(messageForMeReact) // { // message.react("📧").then(function (q) // { // console.log("Reaction was set"); // });//Mark message as reported // } // } // else // { // console.log("Got a keyword mention from inaccessible channel"); // } // }).catch(function(err) // { // console.log("Got a keyword mention from inaccessible guild"); // }); // } if(allowWrite) { if(botCommands.botConfig.defaultChannel.includes(message.channel.id)) // "beep-boop", "fun" 218194030662647809 { if(message.author.id === "216688100032643072")//Horikawa Botane { if(msgLowTrimmed.indexOf("is it porn?") !== -1) { setTimeout(function(){message.channel.send("No, <@216688100032643072>!").catch(botCommands.msgSendError);}, 1000); } else if(msgLowTrimmed.indexOf("i don't believe you") !== -1) { setTimeout(function(){message.channel.send("Let's play with Bastion!").catch(botCommands.msgSendError);}, 1000); setTimeout(function(){message.channel.send("Bastion Bastion Bastion Bastion!!!!").catch(botCommands.msgSendError);}, 2000); setTimeout(function(){message.channel.send("bastion bastion bastion bastion bastion").catch(botCommands.msgSendError);}, 3500); } } } } } } function registerCommands(foxyCore) { botCommands = foxyCore; } module.exports = { registerCommands: registerCommands, messageIn: messageIn };
var testdata = require('./testdata.json'); var len = Math.max(testdata.stdout.length, testdata.stderr.length); for (var i = 0; i < len; i++) { if (i < testdata.stdout.length){ console.log(testdata.stdout[i]); } if (i < testdata.stderr.length){ console.error(testdata.stderr[i]); } }
/** * @fileoverview Rule to flag use of unary increment and decrement operators. * @author Ian Christian Myers * @author Brody McKee (github.com/mrmckeb) */ "use strict"; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = { meta: { type: "suggestion", docs: { description: "disallow the unary operators `++` and `--`", category: "Stylistic Issues", recommended: false, url: "https://eslint.org/docs/rules/no-plusplus" }, schema: [ { type: "object", properties: { allowForLoopAfterthoughts: { type: "boolean", default: false } }, additionalProperties: false } ], messages: { unexpectedUnaryOp: "Unary operator '{{operator}}' used." } }, create(context) { const config = context.options[0]; let allowInForAfterthought = false; if (typeof config === "object") { allowInForAfterthought = config.allowForLoopAfterthoughts === true; } return { UpdateExpression(node) { if (allowInForAfterthought && node.parent.type === "ForStatement") { return; } context.report({ node, messageId: "unexpectedUnaryOp", data: { operator: node.operator } }); } }; } };
import _ from 'lodash' import React from 'react' import { Link } from 'react-router' import './HomeView.scss' import { resolveLocalizationGet } from 'helpers/localize' import LoadingSpinnerFlower from 'components/LoadingSpinnerFlower/LoadingSpinnerFlower' import EllipseDotAnimation from 'components/EllipseDotAnimation' import HOME_VIEW_CONTENT from 'content/views/Home/home' import { CURRENT_WORK_SOFTWARE } from 'content/views/Work/work' export class HomeView extends React.Component { static propTypes = { sendMessage: React.PropTypes.func.isRequired } constructor (props) { super(props) this.state = { name: '', email: '', message: '' } this._sendMessage = this._sendMessage.bind(this) this._onNameChange = this._onNameChange.bind(this) this._onEmailChange = this._onEmailChange.bind(this) this._onMessageChange = this._onMessageChange.bind(this) } _onNameChange (event) { this.onMessageFormChange() this.setState({ name: event.target.value, nameValid: this.validateName(event.target.value) }) } _onEmailChange (event) { this.onMessageFormChange() this.setState({ email: event.target.value, emailValid: this.validateEmail(event.target.value) }) } _onMessageChange (event) { this.onMessageFormChange() this.setState({ message: event.target.value, messageValid: this.validateMessage(event.target.value) }) } _sendMessage (e) { e.preventDefault() const nameValid = this.validateName(this.state.name) const emailValid = this.validateEmail(this.state.email) const messageValid = this.validateMessage(this.state.message) this.setState({ nameValid, emailValid, messageValid, submitIntent: true }) if (!nameValid || !emailValid || !messageValid) return this.setState({ submitIntent: undefined, messageSending: true, loadingSpinnerHeight: this.refs.letsWorkTogetherForm.clientHeight }) this.props.sendMessage({ name: this.state.name, email: this.state.email, message: this.state.message }).then(() => { this.setState({ messageSending: false, messageSentSuccessfully: true, loadingSpinnerHeight: undefined }) }).catch(() => { this.setState({ messageSending: false, messageSentSuccessfully: false, loadingSpinnerHeight: undefined }) }) } onMessageFormChange () { this.setState({ messageSentSuccessfully: undefined }) } // TODO: move to Container validateName (name) { return !_.isEmpty(name) } validateEmail (email) { return !_.isEmpty(email) } validateMessage (message) { return !_.isEmpty(message) } render () { return ( <div className='view--home container text-center'> <div className='row'> <h2 className='view--home__header__name'> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.name')} </h2> <h5 className='view--home__header__location'> <i className='view--home__header__location__icon' /> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.location')} </h5> </div> <h4 className='row'>{resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.jobTitle')}</h4> <div className='row'> <h5>{resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.currentWorkHeader')}</h5> <ul className='view--home__current_work'> {_.map(CURRENT_WORK_SOFTWARE, (workContent, index) => { const textContent = <li className='view--home__current_work__li__text'> {[ resolveLocalizationGet(workContent, 'projectNames'), resolveLocalizationGet(workContent, 'shortDescription') ].join(' - ')} </li> return _.get(workContent, 'link') ? <Link key={`current-work-${index}`} to={_.get(workContent, 'link')}>{textContent}</Link> : <a key={`current-work-${index}`} href={_.get(workContent, 'href')} target='_blank'>{textContent}</a> })} </ul> </div> {!this.state.messageSending && this.state.messageSentSuccessfully !== true ? <form onSubmit={this._sendMessage} ref='letsWorkTogetherForm'> <div className='row'> <div className='four columns font-size--5 view--home__lets-work-together__title'> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherQuestion')} </div> <div className='four columns'> <label htmlFor='lets-work-together-email' className='view--home__lets-work-together__label'> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherEmailLabel')} </label> <input type='email' className='view--home__lets-work-together__input' ref='letsWorkTogetherEmail' id='lets-work-together-email' name='lets-work-together-email' placeholder={resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherEmailPlaceholder')} value={this.state.email} onChange={this._onEmailChange} /> <div className='view--home__lets-work-together__disclaimer'> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherEmailDisclaimer')} </div> {this.state.emailValid === false && this.state.submitIntent ? <div className='view--home__lets-work-together__error'> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherEmailError')} </div> : null} </div> <div className='four columns'> <label htmlFor='lets-work-together-name' className='view--home__lets-work-together__label'> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherNameLabel')} </label> <input type='text' className='view--home__lets-work-together__input' ref='letsWorkTogetherName' id='lets-work-together-name' name='lets-work-together-name' placeholder={resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherNamePlaceholder')} value={this.state.name} onChange={this._onNameChange} /> {this.state.nameValid === false && this.state.submitIntent ? <div className='view--home__lets-work-together__error'> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherNameError')} </div> : null} </div> </div> <div className='row view--home__lets-work-together__message-section'> <div className='eight columns'> <label htmlFor='lets-work-together-message' className='view--home__lets-work-together__label'> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherMessageLabel')} </label> <textarea className='view--home__lets-work-together__input view--home__lets-work-together__textarea' ref='letsWorkTogetherMessage' id='lets-work-together-message' name='lets-work-together-message' placeholder={resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherMessagePlaceholder')} value={this.state.message} onChange={this._onMessageChange} /> {this.state.messageValid === false && this.state.submitIntent ? <div className='view--home__lets-work-together__error view--home__lets-work-together__error--mobile' > {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherMessageError')} </div> : null} {this.state.messageSentSuccessfully === false ? <div className='view--home__lets-work-together__error'> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherMessageSendError')} </div> : null} </div> <div className='four columns'> <label htmlFor='lets-work-together-send-button' className='view--home__lets-work-together__label view--home__lets-work-together__label--hidden' > {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherSendLabel')} </label> <button className='view--home__lets-work-together__input view--home__lets-work-together__send-button' type='submit' id='lets-work-together-send-button' name='lets-work-together-send-button' > {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherSendLabel')} </button> {this.state.messageValid === false && this.state.submitIntent ? <div className={'view--home__lets-work-together__error view--home__lets-work-together__error--tablet'} > {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherMessageError')} </div> : null} </div> </div> </form> : null} {this.state.messageSending ? <div className='row view--home__loading' style={{ height: this.state.loadingSpinnerHeight }}> <div className='view--home__loading__spinner-wrapper'> <LoadingSpinnerFlower /> </div> <div className='view--home__loading__text'> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherMessageSending')} <EllipseDotAnimation /> </div> </div> : null} {this.state.messageSentSuccessfully === true ? <h6 className='view--home__message-sent'> <i className='view--home__message-sent__icon' /> {resolveLocalizationGet(HOME_VIEW_CONTENT, 'header.workTogetherMessageSent')} </h6> : null} </div> ) } } export default HomeView
'use strict'; /* Controllers */ var companiesServiceURL = 'http://serene-bayou-4553.herokuapp.com'; var companiesApp = angular.module('companies-angularjs-demo.controllers', []); companiesApp.controller('CompanyListController', ['$scope', '$http', '$route', function($scope, $http, $route) { $http.get(companiesServiceURL + '/companies').success(function(data) { $scope.companies = data; }); $scope.add = function(company) { $http.post(companiesServiceURL + '/companies/', company).success(function(data) { $route.reload(); })} $scope.delete = function(id) { $http.delete(companiesServiceURL+'/companies/'+ id).success(function(data) { $route.reload(); })} }]); companiesApp.controller('CompanyGetController', ['$scope', '$http', '$routeParams', '$route', function($scope, $http, $routeParams, $route) { $http.get(companiesServiceURL+'/companies/'+$routeParams.company_id).success(function(data) { $scope.company = data; }); $scope.update = function(company) { //only update company, not individuals company.individuals = null; $http.put(companiesServiceURL+'/companies/' + company.id, company).success (function(data) { $route.reload(); })} $scope.updateperson = function(ind) { //only update individual $http.put(companiesServiceURL+'/companies/' + ind.company_id +'/individuals/' + ind.id, ind).success (function(data) { $route.reload(); })} $scope.addperson = function(ind) { //add individual $http.post(companiesServiceURL+'/companies/' + $scope.company.id +'/individuals', ind).success (function(data) { $route.reload(); })} }]);
/** * @file 设置自动调整字形位置 * @author mengke01([email protected]) */ define( function (require) { var i18n = require('../i18n/i18n'); var tpl = require('../template/dialog/setting-glyf.tpl'); var string = require('fonteditor-core/ttf/util/string'); var unicodeREG = /^(?:\$[A-F0-9]+)(?:\,\$[A-F0-9]+)*$/gi; return require('./setting').derive({ title: i18n.lang.dialog_glyph_info, getTpl: function () { return tpl; }, set: function (setting) { $('#setting-glyf-unicode').on('blur', function (e) { var val = $(this).val(); var ctlGlyfName = $('#setting-glyf-name'); if (!ctlGlyfName.val()) { if (val.match(unicodeREG)) { val = Number('0x' + val.split(',')[0].slice(1)); } else if (val) { val = val.charCodeAt(0); } ctlGlyfName.val(string.getUnicodeName(val)); } }); this.setFields(setting || {}); }, onDispose: function () { $('#setting-glyf-unicode').off('blur'); }, validate: function () { var setting = this.getFields(); if (setting.leftSideBearing === undefined && setting.rightSideBearing === undefined && setting.unicode === undefined && setting.name === undefined ) { alert(i18n.lang.dialog_no_input); return false; } return setting; } }); } );
const frisby = require('frisby') const Joi = frisby.Joi const insecurity = require('../../lib/insecurity') const API_URL = 'http://localhost:3000/api' const authHeader = { 'Authorization': 'Bearer ' + insecurity.authorize(), 'content-type': 'application/json' } describe('/api/Recycles', () => { it('POST new recycle', done => { frisby.post(API_URL + '/Recycles', { headers: authHeader, body: { quantity: 200, address: 'Bjoern Kimminich, 123 Juicy Road, Test City', isPickup: true, date: '2017-05-31' } }) .expect('status', 200) .expect('header', 'content-type', /application\/json/) .expect('jsonTypes', 'data', { id: Joi.number(), createdAt: Joi.string(), updatedAt: Joi.string() }) .done(done) }) it('GET all recycles is forbidden via public API', done => { frisby.get(API_URL + '/Recycles') .expect('status', 401) .done(done) }) it('GET all recycles', done => { frisby.get(API_URL + '/Recycles', { headers: authHeader }) .expect('status', 200) .done(done) }) }) describe('/api/Recycles/:id', () => { it('GET existing recycle by id is forbidden', done => { frisby.get(API_URL + '/Recycles/1', { headers: authHeader }) .expect('status', 401) .done(done) }) it('PUT update existing recycle is forbidden', done => { frisby.put(API_URL + '/Recycles/1', { headers: authHeader, body: { quantity: 100000 } }) .expect('status', 401) .done(done) }) it('DELETE existing recycle is forbidden', done => { frisby.del(API_URL + '/Recycles/1', { headers: authHeader }) .expect('status', 401) .done(done) }) })
var Class = require( "ee-class" ); module.exports = new Class( { render: function( data, request, response, callback ){ response.setHeader( "Content-Type", "Application/JSON" ); callback( JSON.stringify( data ) ); } } );
var searchData= [ ['onapplicationquit',['onApplicationQuit',['../class_buffer_synchronizer.html#a65548865ccdf9703fcfc6887f1a48e29',1,'BufferSynchronizer']]], ['oncreate',['onCreate',['../class_record_tab_handler.html#a17ea59082d2d9e7638fffc96a9f0717b',1,'RecordTabHandler']]], ['opendirectorydialog',['openDirectoryDialog',['../class_windows_app_dialog_helper.html#a15ebebdb4657a4846c69d175f647d2d2',1,'WindowsAppDialogHelper']]], ['openfiledialog',['openFileDialog',['../class_windows_app_dialog_helper.html#a35b2cee2fd16482aae5c2fbeca2abbcc',1,'WindowsAppDialogHelper']]], ['operator_3d',['operator=',['../class_playback_configuration.html#ac3a2de4a2492134dc7d1351e37550e46',1,'PlaybackConfiguration']]], ['operator_3d_3d',['operator==',['../class_playback_configuration.html#a928896b44f93175d1e3f6ec6388fc002',1,'PlaybackConfiguration']]], ['outputstreamsupdaterstragedy',['OutputStreamsUpdaterStragedy',['../class_output_streams_updater_stragedy.html#aa1a18127665771ddc94a896e33afd1ef',1,'OutputStreamsUpdaterStragedy']]] ];
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _identity = require('lodash/identity'); var _identity2 = _interopRequireDefault(_identity); var _createTester = require('./internal/createTester'); var _createTester2 = _interopRequireDefault(_createTester); var _eachOf = require('./eachOf'); var _eachOf2 = _interopRequireDefault(_eachOf); var _findGetResult = require('./internal/findGetResult'); var _findGetResult2 = _interopRequireDefault(_findGetResult); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Returns the first value in `coll` that passes an async truth test. The * `iteratee` is applied in parallel, meaning the first iteratee to return * `true` will fire the detect `callback` with that result. That means the * result might not be the first item in the original `coll` (in terms of order) * that passes the test. * If order within the original `coll` is important, then look at * [`detectSeries`]{@link module:Collections.detectSeries}. * * @name detect * @static * @memberOf module:Collections * @method * @alias find * @category Collections * @param {Array|Iterable|Object} coll - A collection to iterate over. * @param {Function} iteratee - A truth test to apply to each item in `coll`. * The iteratee is passed a `callback(err, truthValue)` which must be called * with a boolean argument once it has completed. Invoked with (item, callback). * @param {Function} [callback] - A callback which is called as soon as any * iteratee returns `true`, or after all the `iteratee` functions have finished. * Result will be the first item in the array that passes the truth test * (iteratee) or the value `undefined` if none passed. Invoked with * (err, result). * @example * * async.detect(['file1','file2','file3'], function(filePath, callback) { * fs.access(filePath, function(err) { * callback(null, !err) * }); * }, function(err, result) { * // result now equals the first file in the list that exists * }); */ exports.default = (0, _createTester2.default)(_eachOf2.default, _identity2.default, _findGetResult2.default); module.exports = exports['default'];
// IDEA: DEFAULT var path = require('path'), Argv = require('minimist')(process.argv); // IDEA: COMMON modules var fs = require('fs-extra'), clc = require('cli-color'), extend = require('node.extend'); // TODO: task var task = { todo:[], setting:{}, status:{ warns:[], error:[], msg:{ No:{ Init: "Nothing to initial!", OS : "No OS specified: {-- --os=?}", Config:"Package contains no configuration!", Build: "Package->configuration has no 'build' specified!", File: "file does not exist!", Dir:'Directory not exists!' }, Yes:{ Dir:'Directory created!', Empty:'Directory emtpy!' }, Invalid:{ OS : "the specified: {os} is invalid!" }, Create:'Obj created!', Empty:'Obj emptied!', Exists:'Obj exists!', DataCopied: "Data Copied...", ENOENT: "{msg} does not exist, and skipped!", Ok: "Ok: {msg}", Error: "--{msg}", Danger: "Processing task might destory your works...", Unknown: "Processing task is not available...", Processing: "Task Processing...", Completed: "Task Completed!" }, string:function(q) { if (typeof q === 'object') { return q.join("\n"); } return q; }, success:function() { if (this.error.length) { this.exit(this.error); } else { return true; } }, exit:function(msg) { if (msg) { this.msgDefault(this.string(msg)); } process.exit(0); }, msgError:function(q) { console.log(clc.red(this.string(q))); }, msgSuccess:function(q) { console.log(clc.green(this.string(q))); }, msgDefault:function(q) { console.log(clc.magenta(this.string(q))); } }, createDirectory:function(dir, callback, empty) { dir.Exists(err =>{ if (err) { // NOTE: empty exists directory if (empty) { dir.Empty(err => { if (err) { this.status.exit(err); } else { callback(this.status.msg.Empty.replace('Obj', dir)); } }); } else { callback(this.status.msg.Exists.replace('Obj', dir)); } } else { // NOTE: create new directory dir.Create(err => { if (err) { this.status.exit(err); } else { callback(this.status.msg.Create.replace('Obj', dir)); } }); } }); } }; module.exports = function(Config){ this.path = path; this.Argv = Argv; this.fs = fs; this.clc = clc; this.extend = extend; if (Config.hasOwnProperty('json')){ for (var file in Config.json) { if (Config.json.hasOwnProperty(file)) { try { var fileName = Config.json[file]; Config.json[file] = JSON.parse(fs.readFileSync(Config.json[file])); if (Argv.pro && Config.json[file].project && Config.json[file].project[Argv.pro]) { // Ok: argument, project in json, Directory? Config.json[file].project.root = path.join(Config.json[file].project[Argv.pro].root); // console.log('asdfasd',path.join(Config.json[file].project[Argv.pro].root,'package.json')); // path.join(Config.json[file].project[Argv.pro].root,'package.json').Exists(function(err){ // console.log('asdfasd',err); // }); try { extend(true,Config.json[file],JSON.parse(fs.readFileSync(path.join(Config.json[file].project.root,fileName)))); } catch(e) { task.status.warns.push(task.status.msg.No.File.replace('file', fileName)); } } } catch (e) { Config.json[file] = task.status.msg.No.File.replace('file', fileName); task.status.error.push(Config.json[file]); } } } } Object.defineProperties(Object.prototype, { Exists: { value: function(callback) { // HACK: callback(fs.existsSync(this.toString())); fs.exists(this.toString(), callback); } }, Write: { value: function(content) { // HACK: callback(fs.writeFileSync(this.toString()), content, 'utf-8'); fs.writeFile(this.toString(), content,'utf-8'); } }, Create: { value: function(callback) { fs.ensureDir(this.toString(), callback); } }, Empty: { value: function(callback) { fs.emptyDir(this.toString(), callback); } }, Copy: { value: function(callback) { fs.copy(this.src.toString(), this.des.toString(),{replace:true},function(error) { callback(error); }); } }, Read: { value: function(callback) { var o = this.toString(); fs.readdirSync(o).forEach(function(fileName) { var dir = path.join(o, fileName); var file = path.parse(dir); var state = fs.statSync(dir); // HACK: file.src=dir; // HACK: file.dest=''; callback(dir, file, state); }); } }, hasMethodProperty:{ value:function(o,i){ // return this.hasOwnProperty(o) && this.isFunction(o); if (this.hasOwnProperty(o) && this.isFunction(o)) { return i?this[o]():true; } } }, isFunction:{ value:function(o){ return typeof this[o]==='function'; } } }); extend(true,task,Config); if (task.hasOwnProperty('id')){ this[task.id] = task; } task.hasMethodProperty('initial',true); return task; };
jest.dontMock('../evaluate_key.js'); var evaluateKey = require('../evaluate_key.js') describe('evaluating the key', function(){ it('return mod properly', function(){ navigator.platform = 'yo'; expect( evaluateKey({keyCode: 17}) ).toEqual('mod'); expect( evaluateKey({keyCode: 224}) ).not.toEqual('mod'); expect( evaluateKey({keyCode: 91}) ).not.toEqual('mod'); navigator.platform = 'Mac'; expect( evaluateKey({keyCode: 17}) ).not.toEqual('mod'); expect( evaluateKey({keyCode: 224}) ).toEqual('mod'); expect( evaluateKey({keyCode: 91}) ).toEqual('mod'); }); it('returns shift', function(){ expect( evaluateKey({keyCode: 16}) ).toEqual('shift'); }); it('returns optn', function(){ expect( evaluateKey({keyCode: 18}) ).toEqual('optn'); }); it('returns space', function(){ expect( evaluateKey({keyCode: 32}) ).toEqual('space'); }); it('returns down', function(){ expect( evaluateKey({keyCode: 40}) ).toEqual('down'); }); it('returns up', function(){ expect( evaluateKey({keyCode: 38}) ).toEqual('up'); }); it('returns right', function(){ expect( evaluateKey({keyCode: 39}) ).toEqual('right'); }); it('returns left', function(){ expect( evaluateKey({keyCode: 37}) ).toEqual('left'); }); it('returns tab', function(){ expect( evaluateKey({keyCode: 9}) ).toEqual('tab'); }); it('returns rtn', function(){ expect( evaluateKey({keyCode: 13}) ).toEqual('rtn'); }); it('returns regular alpha characters', function(){ var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); // charCodes start at 65 for (var i = 65, l = alphabet.length + 65; i < l; i ++) { expect( evaluateKey({keyCode: i}) ).toEqual( alphabet[i - 65] ); } }); });
// Platform: ios // commit cd29cf0f224ccf25e9d422a33fd02ef67d3a78f4 // File generated at :: Mon Apr 29 2013 16:14:47 GMT-0700 (PDT) /* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ ;(function() { // file: lib/scripts/require.js var require, define; (function () { var modules = {}; // Stack of moduleIds currently being built. var requireStack = []; // Map of module ID -> index into requireStack of modules currently being built. var inProgressModules = {}; function build(module) { var factory = module.factory; module.exports = {}; delete module.factory; factory(require, module.exports, module); return module.exports; } require = function (id) { if (!modules[id]) { throw "module " + id + " not found"; } else if (id in inProgressModules) { var cycle = requireStack.slice(inProgressModules[id]).join('->') + '->' + id; throw "Cycle in require graph: " + cycle; } if (modules[id].factory) { try { inProgressModules[id] = requireStack.length; requireStack.push(id); return build(modules[id]); } finally { delete inProgressModules[id]; requireStack.pop(); } } return modules[id].exports; }; define = function (id, factory) { if (modules[id]) { throw "module " + id + " already defined"; } modules[id] = { id: id, factory: factory }; }; define.remove = function (id) { delete modules[id]; }; define.moduleMap = modules; })(); //Export for use in node if (typeof module === "object" && typeof require === "function") { module.exports.require = require; module.exports.define = define; } // file: lib/cordova.js define("cordova", function(require, exports, module) { var channel = require('cordova/channel'); /** * Listen for DOMContentLoaded and notify our channel subscribers. */ document.addEventListener('DOMContentLoaded', function() { channel.onDOMContentLoaded.fire(); }, false); if (document.readyState == 'complete' || document.readyState == 'interactive') { channel.onDOMContentLoaded.fire(); } /** * Intercept calls to addEventListener + removeEventListener and handle deviceready, * resume, and pause events. */ var m_document_addEventListener = document.addEventListener; var m_document_removeEventListener = document.removeEventListener; var m_window_addEventListener = window.addEventListener; var m_window_removeEventListener = window.removeEventListener; /** * Houses custom event handlers to intercept on document + window event listeners. */ var documentEventHandlers = {}, windowEventHandlers = {}; document.addEventListener = function(evt, handler, capture) { var e = evt.toLowerCase(); if (typeof documentEventHandlers[e] != 'undefined') { documentEventHandlers[e].subscribe(handler); } else { m_document_addEventListener.call(document, evt, handler, capture); } }; window.addEventListener = function(evt, handler, capture) { var e = evt.toLowerCase(); if (typeof windowEventHandlers[e] != 'undefined') { windowEventHandlers[e].subscribe(handler); } else { m_window_addEventListener.call(window, evt, handler, capture); } }; document.removeEventListener = function(evt, handler, capture) { var e = evt.toLowerCase(); // If unsubscribing from an event that is handled by a plugin if (typeof documentEventHandlers[e] != "undefined") { documentEventHandlers[e].unsubscribe(handler); } else { m_document_removeEventListener.call(document, evt, handler, capture); } }; window.removeEventListener = function(evt, handler, capture) { var e = evt.toLowerCase(); // If unsubscribing from an event that is handled by a plugin if (typeof windowEventHandlers[e] != "undefined") { windowEventHandlers[e].unsubscribe(handler); } else { m_window_removeEventListener.call(window, evt, handler, capture); } }; function createEvent(type, data) { var event = document.createEvent('Events'); event.initEvent(type, false, false); if (data) { for (var i in data) { if (data.hasOwnProperty(i)) { event[i] = data[i]; } } } return event; } if(typeof window.console === "undefined") { window.console = { log:function(){} }; } var cordova = { define:define, require:require, /** * Methods to add/remove your own addEventListener hijacking on document + window. */ addWindowEventHandler:function(event) { return (windowEventHandlers[event] = channel.create(event)); }, addStickyDocumentEventHandler:function(event) { return (documentEventHandlers[event] = channel.createSticky(event)); }, addDocumentEventHandler:function(event) { return (documentEventHandlers[event] = channel.create(event)); }, removeWindowEventHandler:function(event) { delete windowEventHandlers[event]; }, removeDocumentEventHandler:function(event) { delete documentEventHandlers[event]; }, /** * Retrieve original event handlers that were replaced by Cordova * * @return object */ getOriginalHandlers: function() { return {'document': {'addEventListener': m_document_addEventListener, 'removeEventListener': m_document_removeEventListener}, 'window': {'addEventListener': m_window_addEventListener, 'removeEventListener': m_window_removeEventListener}}; }, /** * Method to fire event from native code * bNoDetach is required for events which cause an exception which needs to be caught in native code */ fireDocumentEvent: function(type, data, bNoDetach) { var evt = createEvent(type, data); if (typeof documentEventHandlers[type] != 'undefined') { if( bNoDetach ) { documentEventHandlers[type].fire(evt); } else { setTimeout(function() { // Fire deviceready on listeners that were registered before cordova.js was loaded. if (type == 'deviceready') { document.dispatchEvent(evt); } documentEventHandlers[type].fire(evt); }, 0); } } else { document.dispatchEvent(evt); } }, fireWindowEvent: function(type, data) { var evt = createEvent(type,data); if (typeof windowEventHandlers[type] != 'undefined') { setTimeout(function() { windowEventHandlers[type].fire(evt); }, 0); } else { window.dispatchEvent(evt); } }, /** * Plugin callback mechanism. */ // Randomize the starting callbackId to avoid collisions after refreshing or navigating. // This way, it's very unlikely that any new callback would get the same callbackId as an old callback. callbackId: Math.floor(Math.random() * 2000000000), callbacks: {}, callbackStatus: { NO_RESULT: 0, OK: 1, CLASS_NOT_FOUND_EXCEPTION: 2, ILLEGAL_ACCESS_EXCEPTION: 3, INSTANTIATION_EXCEPTION: 4, MALFORMED_URL_EXCEPTION: 5, IO_EXCEPTION: 6, INVALID_ACTION: 7, JSON_EXCEPTION: 8, ERROR: 9 }, /** * Called by native code when returning successful result from an action. */ callbackSuccess: function(callbackId, args) { try { cordova.callbackFromNative(callbackId, true, args.status, [args.message], args.keepCallback); } catch (e) { console.log("Error in error callback: " + callbackId + " = "+e); } }, /** * Called by native code when returning error result from an action. */ callbackError: function(callbackId, args) { // TODO: Deprecate callbackSuccess and callbackError in favour of callbackFromNative. // Derive success from status. try { cordova.callbackFromNative(callbackId, false, args.status, [args.message], args.keepCallback); } catch (e) { console.log("Error in error callback: " + callbackId + " = "+e); } }, /** * Called by native code when returning the result from an action. */ callbackFromNative: function(callbackId, success, status, args, keepCallback) { var callback = cordova.callbacks[callbackId]; if (callback) { if (success && status == cordova.callbackStatus.OK) { callback.success && callback.success.apply(null, args); } else if (!success) { callback.fail && callback.fail.apply(null, args); } // Clear callback if not expecting any more results if (!keepCallback) { delete cordova.callbacks[callbackId]; } } }, addConstructor: function(func) { channel.onCordovaReady.subscribe(function() { try { func(); } catch(e) { console.log("Failed to run constructor: " + e); } }); } }; // Register pause, resume and deviceready channels as events on document. channel.onPause = cordova.addDocumentEventHandler('pause'); channel.onResume = cordova.addDocumentEventHandler('resume'); channel.onDeviceReady = cordova.addStickyDocumentEventHandler('deviceready'); module.exports = cordova; }); // file: lib/common/argscheck.js define("cordova/argscheck", function(require, exports, module) { var exec = require('cordova/exec'); var utils = require('cordova/utils'); var moduleExports = module.exports; var typeMap = { 'A': 'Array', 'D': 'Date', 'N': 'Number', 'S': 'String', 'F': 'Function', 'O': 'Object' }; function extractParamName(callee, argIndex) { return (/.*?\((.*?)\)/).exec(callee)[1].split(', ')[argIndex]; } function checkArgs(spec, functionName, args, opt_callee) { if (!moduleExports.enableChecks) { return; } var errMsg = null; var typeName; for (var i = 0; i < spec.length; ++i) { var c = spec.charAt(i), cUpper = c.toUpperCase(), arg = args[i]; // Asterix means allow anything. if (c == '*') { continue; } typeName = utils.typeName(arg); if ((arg === null || arg === undefined) && c == cUpper) { continue; } if (typeName != typeMap[cUpper]) { errMsg = 'Expected ' + typeMap[cUpper]; break; } } if (errMsg) { errMsg += ', but got ' + typeName + '.'; errMsg = 'Wrong type for parameter "' + extractParamName(opt_callee || args.callee, i) + '" of ' + functionName + ': ' + errMsg; // Don't log when running jake test. if (typeof jasmine == 'undefined') { console.error(errMsg); } throw TypeError(errMsg); } } function getValue(value, defaultValue) { return value === undefined ? defaultValue : value; } moduleExports.checkArgs = checkArgs; moduleExports.getValue = getValue; moduleExports.enableChecks = true; }); // file: lib/common/builder.js define("cordova/builder", function(require, exports, module) { var utils = require('cordova/utils'); function each(objects, func, context) { for (var prop in objects) { if (objects.hasOwnProperty(prop)) { func.apply(context, [objects[prop], prop]); } } } function clobber(obj, key, value) { exports.replaceHookForTesting(obj, key); obj[key] = value; // Getters can only be overridden by getters. if (obj[key] !== value) { utils.defineGetter(obj, key, function() { return value; }); } } function assignOrWrapInDeprecateGetter(obj, key, value, message) { if (message) { utils.defineGetter(obj, key, function() { console.log(message); delete obj[key]; clobber(obj, key, value); return value; }); } else { clobber(obj, key, value); } } function include(parent, objects, clobber, merge) { each(objects, function (obj, key) { try { var result = obj.path ? require(obj.path) : {}; if (clobber) { // Clobber if it doesn't exist. if (typeof parent[key] === 'undefined') { assignOrWrapInDeprecateGetter(parent, key, result, obj.deprecated); } else if (typeof obj.path !== 'undefined') { // If merging, merge properties onto parent, otherwise, clobber. if (merge) { recursiveMerge(parent[key], result); } else { assignOrWrapInDeprecateGetter(parent, key, result, obj.deprecated); } } result = parent[key]; } else { // Overwrite if not currently defined. if (typeof parent[key] == 'undefined') { assignOrWrapInDeprecateGetter(parent, key, result, obj.deprecated); } else { // Set result to what already exists, so we can build children into it if they exist. result = parent[key]; } } if (obj.children) { include(result, obj.children, clobber, merge); } } catch(e) { utils.alert('Exception building cordova JS globals: ' + e + ' for key "' + key + '"'); } }); } /** * Merge properties from one object onto another recursively. Properties from * the src object will overwrite existing target property. * * @param target Object to merge properties into. * @param src Object to merge properties from. */ function recursiveMerge(target, src) { for (var prop in src) { if (src.hasOwnProperty(prop)) { if (target.prototype && target.prototype.constructor === target) { // If the target object is a constructor override off prototype. clobber(target.prototype, prop, src[prop]); } else { if (typeof src[prop] === 'object' && typeof target[prop] === 'object') { recursiveMerge(target[prop], src[prop]); } else { clobber(target, prop, src[prop]); } } } } } exports.buildIntoButDoNotClobber = function(objects, target) { include(target, objects, false, false); }; exports.buildIntoAndClobber = function(objects, target) { include(target, objects, true, false); }; exports.buildIntoAndMerge = function(objects, target) { include(target, objects, true, true); }; exports.recursiveMerge = recursiveMerge; exports.assignOrWrapInDeprecateGetter = assignOrWrapInDeprecateGetter; exports.replaceHookForTesting = function() {}; }); // file: lib/common/channel.js define("cordova/channel", function(require, exports, module) { var utils = require('cordova/utils'), nextGuid = 1; /** * Custom pub-sub "channel" that can have functions subscribed to it * This object is used to define and control firing of events for * cordova initialization, as well as for custom events thereafter. * * The order of events during page load and Cordova startup is as follows: * * onDOMContentLoaded* Internal event that is received when the web page is loaded and parsed. * onNativeReady* Internal event that indicates the Cordova native side is ready. * onCordovaReady* Internal event fired when all Cordova JavaScript objects have been created. * onCordovaInfoReady* Internal event fired when device properties are available. * onCordovaConnectionReady* Internal event fired when the connection property has been set. * onDeviceReady* User event fired to indicate that Cordova is ready * onResume User event fired to indicate a start/resume lifecycle event * onPause User event fired to indicate a pause lifecycle event * onDestroy* Internal event fired when app is being destroyed (User should use window.onunload event, not this one). * * The events marked with an * are sticky. Once they have fired, they will stay in the fired state. * All listeners that subscribe after the event is fired will be executed right away. * * The only Cordova events that user code should register for are: * deviceready Cordova native code is initialized and Cordova APIs can be called from JavaScript * pause App has moved to background * resume App has returned to foreground * * Listeners can be registered as: * document.addEventListener("deviceready", myDeviceReadyListener, false); * document.addEventListener("resume", myResumeListener, false); * document.addEventListener("pause", myPauseListener, false); * * The DOM lifecycle events should be used for saving and restoring state * window.onload * window.onunload * */ /** * Channel * @constructor * @param type String the channel name */ var Channel = function(type, sticky) { this.type = type; // Map of guid -> function. this.handlers = {}; // 0 = Non-sticky, 1 = Sticky non-fired, 2 = Sticky fired. this.state = sticky ? 1 : 0; // Used in sticky mode to remember args passed to fire(). this.fireArgs = null; // Used by onHasSubscribersChange to know if there are any listeners. this.numHandlers = 0; // Function that is called when the first listener is subscribed, or when // the last listener is unsubscribed. this.onHasSubscribersChange = null; }, channel = { /** * Calls the provided function only after all of the channels specified * have been fired. All channels must be sticky channels. */ join: function(h, c) { var len = c.length, i = len, f = function() { if (!(--i)) h(); }; for (var j=0; j<len; j++) { if (c[j].state === 0) { throw Error('Can only use join with sticky channels.'); } c[j].subscribe(f); } if (!len) h(); }, create: function(type) { return channel[type] = new Channel(type, false); }, createSticky: function(type) { return channel[type] = new Channel(type, true); }, /** * cordova Channels that must fire before "deviceready" is fired. */ deviceReadyChannelsArray: [], deviceReadyChannelsMap: {}, /** * Indicate that a feature needs to be initialized before it is ready to be used. * This holds up Cordova's "deviceready" event until the feature has been initialized * and Cordova.initComplete(feature) is called. * * @param feature {String} The unique feature name */ waitForInitialization: function(feature) { if (feature) { var c = channel[feature] || this.createSticky(feature); this.deviceReadyChannelsMap[feature] = c; this.deviceReadyChannelsArray.push(c); } }, /** * Indicate that initialization code has completed and the feature is ready to be used. * * @param feature {String} The unique feature name */ initializationComplete: function(feature) { var c = this.deviceReadyChannelsMap[feature]; if (c) { c.fire(); } } }; function forceFunction(f) { if (typeof f != 'function') throw "Function required as first argument!"; } /** * Subscribes the given function to the channel. Any time that * Channel.fire is called so too will the function. * Optionally specify an execution context for the function * and a guid that can be used to stop subscribing to the channel. * Returns the guid. */ Channel.prototype.subscribe = function(f, c) { // need a function to call forceFunction(f); if (this.state == 2) { f.apply(c || this, this.fireArgs); return; } var func = f, guid = f.observer_guid; if (typeof c == "object") { func = utils.close(c, f); } if (!guid) { // first time any channel has seen this subscriber guid = '' + nextGuid++; } func.observer_guid = guid; f.observer_guid = guid; // Don't add the same handler more than once. if (!this.handlers[guid]) { this.handlers[guid] = func; this.numHandlers++; if (this.numHandlers == 1) { this.onHasSubscribersChange && this.onHasSubscribersChange(); } } }; /** * Unsubscribes the function with the given guid from the channel. */ Channel.prototype.unsubscribe = function(f) { // need a function to unsubscribe forceFunction(f); var guid = f.observer_guid, handler = this.handlers[guid]; if (handler) { delete this.handlers[guid]; this.numHandlers--; if (this.numHandlers === 0) { this.onHasSubscribersChange && this.onHasSubscribersChange(); } } }; /** * Calls all functions subscribed to this channel. */ Channel.prototype.fire = function(e) { var fail = false, fireArgs = Array.prototype.slice.call(arguments); // Apply stickiness. if (this.state == 1) { this.state = 2; this.fireArgs = fireArgs; } if (this.numHandlers) { // Copy the values first so that it is safe to modify it from within // callbacks. var toCall = []; for (var item in this.handlers) { toCall.push(this.handlers[item]); } for (var i = 0; i < toCall.length; ++i) { toCall[i].apply(this, fireArgs); } if (this.state == 2 && this.numHandlers) { this.numHandlers = 0; this.handlers = {}; this.onHasSubscribersChange && this.onHasSubscribersChange(); } } }; // defining them here so they are ready super fast! // DOM event that is received when the web page is loaded and parsed. channel.createSticky('onDOMContentLoaded'); // Event to indicate the Cordova native side is ready. channel.createSticky('onNativeReady'); // Event to indicate that all Cordova JavaScript objects have been created // and it's time to run plugin constructors. channel.createSticky('onCordovaReady'); // Event to indicate that device properties are available channel.createSticky('onCordovaInfoReady'); // Event to indicate that the connection property has been set. channel.createSticky('onCordovaConnectionReady'); // Event to indicate that all automatically loaded JS plugins are loaded and ready. channel.createSticky('onPluginsReady'); // Event to indicate that Cordova is ready channel.createSticky('onDeviceReady'); // Event to indicate a resume lifecycle event channel.create('onResume'); // Event to indicate a pause lifecycle event channel.create('onPause'); // Event to indicate a destroy lifecycle event channel.createSticky('onDestroy'); // Channels that must fire before "deviceready" is fired. channel.waitForInitialization('onCordovaReady'); channel.waitForInitialization('onCordovaConnectionReady'); channel.waitForInitialization('onDOMContentLoaded'); module.exports = channel; }); // file: lib/common/commandProxy.js define("cordova/commandProxy", function(require, exports, module) { // internal map of proxy function var CommandProxyMap = {}; module.exports = { // example: cordova.commandProxy.add("Accelerometer",{getCurrentAcceleration: function(successCallback, errorCallback, options) {...},...); add:function(id,proxyObj) { console.log("adding proxy for " + id); CommandProxyMap[id] = proxyObj; return proxyObj; }, // cordova.commandProxy.remove("Accelerometer"); remove:function(id) { var proxy = CommandProxyMap[id]; delete CommandProxyMap[id]; CommandProxyMap[id] = null; return proxy; }, get:function(service,action) { return ( CommandProxyMap[service] ? CommandProxyMap[service][action] : null ); } }; }); // file: lib/ios/exec.js define("cordova/exec", function(require, exports, module) { /** * Creates a gap bridge iframe used to notify the native code about queued * commands. * * @private */ var cordova = require('cordova'), channel = require('cordova/channel'), utils = require('cordova/utils'), jsToNativeModes = { IFRAME_NAV: 0, XHR_NO_PAYLOAD: 1, XHR_WITH_PAYLOAD: 2, XHR_OPTIONAL_PAYLOAD: 3 }, bridgeMode, execIframe, execXhr, requestCount = 0, vcHeaderValue = null, commandQueue = [], // Contains pending JS->Native messages. isInContextOfEvalJs = 0; function createExecIframe() { var iframe = document.createElement("iframe"); iframe.style.display = 'none'; document.body.appendChild(iframe); return iframe; } function shouldBundleCommandJson() { if (bridgeMode == jsToNativeModes.XHR_WITH_PAYLOAD) { return true; } if (bridgeMode == jsToNativeModes.XHR_OPTIONAL_PAYLOAD) { var payloadLength = 0; for (var i = 0; i < commandQueue.length; ++i) { payloadLength += commandQueue[i].length; } // The value here was determined using the benchmark within CordovaLibApp on an iPad 3. return payloadLength < 4500; } return false; } function massageArgsJsToNative(args) { if (!args || utils.typeName(args) != 'Array') { return args; } var ret = []; var encodeArrayBufferAs8bitString = function(ab) { return String.fromCharCode.apply(null, new Uint8Array(ab)); }; var encodeArrayBufferAsBase64 = function(ab) { return window.btoa(encodeArrayBufferAs8bitString(ab)); }; args.forEach(function(arg, i) { if (utils.typeName(arg) == 'ArrayBuffer') { ret.push({ 'CDVType': 'ArrayBuffer', 'data': encodeArrayBufferAsBase64(arg) }); } else { ret.push(arg); } }); return ret; } function massageMessageNativeToJs(message) { if (message.CDVType == 'ArrayBuffer') { var stringToArrayBuffer = function(str) { var ret = new Uint8Array(str.length); for (var i = 0; i < str.length; i++) { ret[i] = str.charCodeAt(i); } return ret.buffer; }; var base64ToArrayBuffer = function(b64) { return stringToArrayBuffer(atob(b64)); }; message = base64ToArrayBuffer(message.data); } return message; } function convertMessageToArgsNativeToJs(message) { var args = []; if (!message || !message.hasOwnProperty('CDVType')) { args.push(message); } else if (message.CDVType == 'MultiPart') { message.messages.forEach(function(e) { args.push(massageMessageNativeToJs(e)); }); } else { args.push(massageMessageNativeToJs(message)); } return args; } function iOSExec() { // XHR mode does not work on iOS 4.2, so default to IFRAME_NAV for such devices. // XHR mode's main advantage is working around a bug in -webkit-scroll, which // doesn't exist in 4.X devices anyways. if (bridgeMode === undefined) { bridgeMode = navigator.userAgent.indexOf(' 4_') == -1 ? jsToNativeModes.XHR_NO_PAYLOAD : jsToNativeModes.IFRAME_NAV; } var successCallback, failCallback, service, action, actionArgs, splitCommand; var callbackId = null; if (typeof arguments[0] !== "string") { // FORMAT ONE successCallback = arguments[0]; failCallback = arguments[1]; service = arguments[2]; action = arguments[3]; actionArgs = arguments[4]; // Since we need to maintain backwards compatibility, we have to pass // an invalid callbackId even if no callback was provided since plugins // will be expecting it. The Cordova.exec() implementation allocates // an invalid callbackId and passes it even if no callbacks were given. callbackId = 'INVALID'; } else { // FORMAT TWO, REMOVED try { splitCommand = arguments[0].split("."); action = splitCommand.pop(); service = splitCommand.join("."); actionArgs = Array.prototype.splice.call(arguments, 1); console.log('The old format of this exec call has been removed (deprecated since 2.1). Change to: ' + "cordova.exec(null, null, \"" + service + "\", " + action + "\"," + JSON.stringify(actionArgs) + ");" ); return; } catch (e) { } } // Register the callbacks and add the callbackId to the positional // arguments if given. if (successCallback || failCallback) { callbackId = service + cordova.callbackId++; cordova.callbacks[callbackId] = {success:successCallback, fail:failCallback}; } actionArgs = massageArgsJsToNative(actionArgs); var command = [callbackId, service, action, actionArgs]; // Stringify and queue the command. We stringify to command now to // effectively clone the command arguments in case they are mutated before // the command is executed. commandQueue.push(JSON.stringify(command)); // If we're in the context of a stringByEvaluatingJavaScriptFromString call, // then the queue will be flushed when it returns; no need for a poke. // Also, if there is already a command in the queue, then we've already // poked the native side, so there is no reason to do so again. if (!isInContextOfEvalJs && commandQueue.length == 1) { if (bridgeMode != jsToNativeModes.IFRAME_NAV) { // This prevents sending an XHR when there is already one being sent. // This should happen only in rare circumstances (refer to unit tests). if (execXhr && execXhr.readyState != 4) { execXhr = null; } // Re-using the XHR improves exec() performance by about 10%. execXhr = execXhr || new XMLHttpRequest(); // Changing this to a GET will make the XHR reach the URIProtocol on 4.2. // For some reason it still doesn't work though... // Add a timestamp to the query param to prevent caching. execXhr.open('HEAD', "/!gap_exec?" + (+new Date()), true); if (!vcHeaderValue) { vcHeaderValue = /.*\((.*)\)/.exec(navigator.userAgent)[1]; } execXhr.setRequestHeader('vc', vcHeaderValue); execXhr.setRequestHeader('rc', ++requestCount); if (shouldBundleCommandJson()) { execXhr.setRequestHeader('cmds', iOSExec.nativeFetchMessages()); } execXhr.send(null); } else { execIframe = execIframe || createExecIframe(); execIframe.src = "gap://ready"; } } } iOSExec.jsToNativeModes = jsToNativeModes; iOSExec.setJsToNativeBridgeMode = function(mode) { // Remove the iFrame since it may be no longer required, and its existence // can trigger browser bugs. // https://issues.apache.org/jira/browse/CB-593 if (execIframe) { execIframe.parentNode.removeChild(execIframe); execIframe = null; } bridgeMode = mode; }; iOSExec.nativeFetchMessages = function() { // Each entry in commandQueue is a JSON string already. if (!commandQueue.length) { return ''; } var json = '[' + commandQueue.join(',') + ']'; commandQueue.length = 0; return json; }; iOSExec.nativeCallback = function(callbackId, status, message, keepCallback) { return iOSExec.nativeEvalAndFetch(function() { var success = status === 0 || status === 1; var args = convertMessageToArgsNativeToJs(message); cordova.callbackFromNative(callbackId, success, status, args, keepCallback); }); }; iOSExec.nativeEvalAndFetch = function(func) { // This shouldn't be nested, but better to be safe. isInContextOfEvalJs++; try { func(); return iOSExec.nativeFetchMessages(); } finally { isInContextOfEvalJs--; } }; module.exports = iOSExec; }); // file: lib/common/modulemapper.js define("cordova/modulemapper", function(require, exports, module) { var builder = require('cordova/builder'), moduleMap = define.moduleMap, symbolList, deprecationMap; exports.reset = function() { symbolList = []; deprecationMap = {}; }; function addEntry(strategy, moduleName, symbolPath, opt_deprecationMessage) { if (!(moduleName in moduleMap)) { throw new Error('Module ' + moduleName + ' does not exist.'); } symbolList.push(strategy, moduleName, symbolPath); if (opt_deprecationMessage) { deprecationMap[symbolPath] = opt_deprecationMessage; } } // Note: Android 2.3 does have Function.bind(). exports.clobbers = function(moduleName, symbolPath, opt_deprecationMessage) { addEntry('c', moduleName, symbolPath, opt_deprecationMessage); }; exports.merges = function(moduleName, symbolPath, opt_deprecationMessage) { addEntry('m', moduleName, symbolPath, opt_deprecationMessage); }; exports.defaults = function(moduleName, symbolPath, opt_deprecationMessage) { addEntry('d', moduleName, symbolPath, opt_deprecationMessage); }; function prepareNamespace(symbolPath, context) { if (!symbolPath) { return context; } var parts = symbolPath.split('.'); var cur = context; for (var i = 0, part; part = parts[i]; ++i) { cur = cur[part] = cur[part] || {}; } return cur; } exports.mapModules = function(context) { var origSymbols = {}; context.CDV_origSymbols = origSymbols; for (var i = 0, len = symbolList.length; i < len; i += 3) { var strategy = symbolList[i]; var moduleName = symbolList[i + 1]; var symbolPath = symbolList[i + 2]; var lastDot = symbolPath.lastIndexOf('.'); var namespace = symbolPath.substr(0, lastDot); var lastName = symbolPath.substr(lastDot + 1); var module = require(moduleName); var deprecationMsg = symbolPath in deprecationMap ? 'Access made to deprecated symbol: ' + symbolPath + '. ' + deprecationMsg : null; var parentObj = prepareNamespace(namespace, context); var target = parentObj[lastName]; if (strategy == 'm' && target) { builder.recursiveMerge(target, module); } else if ((strategy == 'd' && !target) || (strategy != 'd')) { if (!(symbolPath in origSymbols)) { origSymbols[symbolPath] = target; } builder.assignOrWrapInDeprecateGetter(parentObj, lastName, module, deprecationMsg); } } }; exports.getOriginalSymbol = function(context, symbolPath) { var origSymbols = context.CDV_origSymbols; if (origSymbols && (symbolPath in origSymbols)) { return origSymbols[symbolPath]; } var parts = symbolPath.split('.'); var obj = context; for (var i = 0; i < parts.length; ++i) { obj = obj && obj[parts[i]]; } return obj; }; exports.loadMatchingModules = function(matchingRegExp) { for (var k in moduleMap) { if (matchingRegExp.exec(k)) { require(k); } } }; exports.reset(); }); // file: lib/ios/platform.js define("cordova/platform", function(require, exports, module) { module.exports = { id: "ios", initialize:function() { var modulemapper = require('cordova/modulemapper'); modulemapper.loadMatchingModules(/cordova.*\/plugininit$/); modulemapper.loadMatchingModules(/cordova.*\/symbols$/); modulemapper.mapModules(window); } }; }); // file: lib/common/plugin/Acceleration.js define("cordova/plugin/Acceleration", function(require, exports, module) { var Acceleration = function(x, y, z, timestamp) { this.x = x; this.y = y; this.z = z; this.timestamp = timestamp || (new Date()).getTime(); }; module.exports = Acceleration; }); // file: lib/common/plugin/Camera.js define("cordova/plugin/Camera", function(require, exports, module) { var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), Camera = require('cordova/plugin/CameraConstants'), CameraPopoverHandle = require('cordova/plugin/CameraPopoverHandle'); var cameraExport = {}; // Tack on the Camera Constants to the base camera plugin. for (var key in Camera) { cameraExport[key] = Camera[key]; } /** * Gets a picture from source defined by "options.sourceType", and returns the * image as defined by the "options.destinationType" option. * The defaults are sourceType=CAMERA and destinationType=FILE_URI. * * @param {Function} successCallback * @param {Function} errorCallback * @param {Object} options */ cameraExport.getPicture = function(successCallback, errorCallback, options) { argscheck.checkArgs('fFO', 'Camera.getPicture', arguments); options = options || {}; var getValue = argscheck.getValue; var quality = getValue(options.quality, 50); var destinationType = getValue(options.destinationType, Camera.DestinationType.FILE_URI); var sourceType = getValue(options.sourceType, Camera.PictureSourceType.CAMERA); var targetWidth = getValue(options.targetWidth, -1); var targetHeight = getValue(options.targetHeight, -1); var encodingType = getValue(options.encodingType, Camera.EncodingType.JPEG); var mediaType = getValue(options.mediaType, Camera.MediaType.PICTURE); var allowEdit = !!options.allowEdit; var correctOrientation = !!options.correctOrientation; var saveToPhotoAlbum = !!options.saveToPhotoAlbum; var popoverOptions = getValue(options.popoverOptions, null); var cameraDirection = getValue(options.cameraDirection, Camera.Direction.BACK); var args = [quality, destinationType, sourceType, targetWidth, targetHeight, encodingType, mediaType, allowEdit, correctOrientation, saveToPhotoAlbum, popoverOptions, cameraDirection]; exec(successCallback, errorCallback, "Camera", "takePicture", args); return new CameraPopoverHandle(); }; cameraExport.cleanup = function(successCallback, errorCallback) { exec(successCallback, errorCallback, "Camera", "cleanup", []); }; module.exports = cameraExport; }); // file: lib/common/plugin/CameraConstants.js define("cordova/plugin/CameraConstants", function(require, exports, module) { module.exports = { DestinationType:{ DATA_URL: 0, // Return base64 encoded string FILE_URI: 1, // Return file uri (content://media/external/images/media/2 for Android) NATIVE_URI: 2 // Return native uri (eg. asset-library://... for iOS) }, EncodingType:{ JPEG: 0, // Return JPEG encoded image PNG: 1 // Return PNG encoded image }, MediaType:{ PICTURE: 0, // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType VIDEO: 1, // allow selection of video only, ONLY RETURNS URL ALLMEDIA : 2 // allow selection from all media types }, PictureSourceType:{ PHOTOLIBRARY : 0, // Choose image from picture library (same as SAVEDPHOTOALBUM for Android) CAMERA : 1, // Take picture from camera SAVEDPHOTOALBUM : 2 // Choose image from picture library (same as PHOTOLIBRARY for Android) }, PopoverArrowDirection:{ ARROW_UP : 1, // matches iOS UIPopoverArrowDirection constants to specify arrow location on popover ARROW_DOWN : 2, ARROW_LEFT : 4, ARROW_RIGHT : 8, ARROW_ANY : 15 }, Direction:{ BACK: 0, FRONT: 1 } }; }); // file: lib/ios/plugin/CameraPopoverHandle.js define("cordova/plugin/CameraPopoverHandle", function(require, exports, module) { var exec = require('cordova/exec'); /** * A handle to an image picker popover. */ var CameraPopoverHandle = function() { this.setPosition = function(popoverOptions) { var args = [ popoverOptions ]; exec(null, null, "Camera", "repositionPopover", args); }; }; module.exports = CameraPopoverHandle; }); // file: lib/common/plugin/CameraPopoverOptions.js define("cordova/plugin/CameraPopoverOptions", function(require, exports, module) { var Camera = require('cordova/plugin/CameraConstants'); /** * Encapsulates options for iOS Popover image picker */ var CameraPopoverOptions = function(x,y,width,height,arrowDir){ // information of rectangle that popover should be anchored to this.x = x || 0; this.y = y || 32; this.width = width || 320; this.height = height || 480; // The direction of the popover arrow this.arrowDir = arrowDir || Camera.PopoverArrowDirection.ARROW_ANY; }; module.exports = CameraPopoverOptions; }); // file: lib/common/plugin/CaptureAudioOptions.js define("cordova/plugin/CaptureAudioOptions", function(require, exports, module) { /** * Encapsulates all audio capture operation configuration options. */ var CaptureAudioOptions = function(){ // Upper limit of sound clips user can record. Value must be equal or greater than 1. this.limit = 1; // Maximum duration of a single sound clip in seconds. this.duration = 0; // The selected audio mode. Must match with one of the elements in supportedAudioModes array. this.mode = null; }; module.exports = CaptureAudioOptions; }); // file: lib/common/plugin/CaptureError.js define("cordova/plugin/CaptureError", function(require, exports, module) { /** * The CaptureError interface encapsulates all errors in the Capture API. */ var CaptureError = function(c) { this.code = c || null; }; // Camera or microphone failed to capture image or sound. CaptureError.CAPTURE_INTERNAL_ERR = 0; // Camera application or audio capture application is currently serving other capture request. CaptureError.CAPTURE_APPLICATION_BUSY = 1; // Invalid use of the API (e.g. limit parameter has value less than one). CaptureError.CAPTURE_INVALID_ARGUMENT = 2; // User exited camera application or audio capture application before capturing anything. CaptureError.CAPTURE_NO_MEDIA_FILES = 3; // The requested capture operation is not supported. CaptureError.CAPTURE_NOT_SUPPORTED = 20; module.exports = CaptureError; }); // file: lib/common/plugin/CaptureImageOptions.js define("cordova/plugin/CaptureImageOptions", function(require, exports, module) { /** * Encapsulates all image capture operation configuration options. */ var CaptureImageOptions = function(){ // Upper limit of images user can take. Value must be equal or greater than 1. this.limit = 1; // The selected image mode. Must match with one of the elements in supportedImageModes array. this.mode = null; }; module.exports = CaptureImageOptions; }); // file: lib/common/plugin/CaptureVideoOptions.js define("cordova/plugin/CaptureVideoOptions", function(require, exports, module) { /** * Encapsulates all video capture operation configuration options. */ var CaptureVideoOptions = function(){ // Upper limit of videos user can record. Value must be equal or greater than 1. this.limit = 1; // Maximum duration of a single video clip in seconds. this.duration = 0; // The selected video mode. Must match with one of the elements in supportedVideoModes array. this.mode = null; }; module.exports = CaptureVideoOptions; }); // file: lib/common/plugin/CompassError.js define("cordova/plugin/CompassError", function(require, exports, module) { /** * CompassError. * An error code assigned by an implementation when an error has occurred * @constructor */ var CompassError = function(err) { this.code = (err !== undefined ? err : null); }; CompassError.COMPASS_INTERNAL_ERR = 0; CompassError.COMPASS_NOT_SUPPORTED = 20; module.exports = CompassError; }); // file: lib/common/plugin/CompassHeading.js define("cordova/plugin/CompassHeading", function(require, exports, module) { var CompassHeading = function(magneticHeading, trueHeading, headingAccuracy, timestamp) { this.magneticHeading = magneticHeading; this.trueHeading = trueHeading; this.headingAccuracy = headingAccuracy; this.timestamp = timestamp || new Date().getTime(); }; module.exports = CompassHeading; }); // file: lib/common/plugin/ConfigurationData.js define("cordova/plugin/ConfigurationData", function(require, exports, module) { /** * Encapsulates a set of parameters that the capture device supports. */ function ConfigurationData() { // The ASCII-encoded string in lower case representing the media type. this.type = null; // The height attribute represents height of the image or video in pixels. // In the case of a sound clip this attribute has value 0. this.height = 0; // The width attribute represents width of the image or video in pixels. // In the case of a sound clip this attribute has value 0 this.width = 0; } module.exports = ConfigurationData; }); // file: lib/common/plugin/Connection.js define("cordova/plugin/Connection", function(require, exports, module) { /** * Network status */ module.exports = { UNKNOWN: "unknown", ETHERNET: "ethernet", WIFI: "wifi", CELL_2G: "2g", CELL_3G: "3g", CELL_4G: "4g", CELL:"cellular", NONE: "none" }; }); // file: lib/common/plugin/Contact.js define("cordova/plugin/Contact", function(require, exports, module) { var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), ContactError = require('cordova/plugin/ContactError'), utils = require('cordova/utils'); /** * Converts primitives into Complex Object * Currently only used for Date fields */ function convertIn(contact) { var value = contact.birthday; try { contact.birthday = new Date(parseFloat(value)); } catch (exception){ console.log("Cordova Contact convertIn error: exception creating date."); } return contact; } /** * Converts Complex objects into primitives * Only conversion at present is for Dates. **/ function convertOut(contact) { var value = contact.birthday; if (value !== null) { // try to make it a Date object if it is not already if (!utils.isDate(value)){ try { value = new Date(value); } catch(exception){ value = null; } } if (utils.isDate(value)){ value = value.valueOf(); // convert to milliseconds } contact.birthday = value; } return contact; } /** * Contains information about a single contact. * @constructor * @param {DOMString} id unique identifier * @param {DOMString} displayName * @param {ContactName} name * @param {DOMString} nickname * @param {Array.<ContactField>} phoneNumbers array of phone numbers * @param {Array.<ContactField>} emails array of email addresses * @param {Array.<ContactAddress>} addresses array of addresses * @param {Array.<ContactField>} ims instant messaging user ids * @param {Array.<ContactOrganization>} organizations * @param {DOMString} birthday contact's birthday * @param {DOMString} note user notes about contact * @param {Array.<ContactField>} photos * @param {Array.<ContactField>} categories * @param {Array.<ContactField>} urls contact's web sites */ var Contact = function (id, displayName, name, nickname, phoneNumbers, emails, addresses, ims, organizations, birthday, note, photos, categories, urls) { this.id = id || null; this.rawId = null; this.displayName = displayName || null; this.name = name || null; // ContactName this.nickname = nickname || null; this.phoneNumbers = phoneNumbers || null; // ContactField[] this.emails = emails || null; // ContactField[] this.addresses = addresses || null; // ContactAddress[] this.ims = ims || null; // ContactField[] this.organizations = organizations || null; // ContactOrganization[] this.birthday = birthday || null; this.note = note || null; this.photos = photos || null; // ContactField[] this.categories = categories || null; // ContactField[] this.urls = urls || null; // ContactField[] }; /** * Removes contact from device storage. * @param successCB success callback * @param errorCB error callback */ Contact.prototype.remove = function(successCB, errorCB) { argscheck.checkArgs('FF', 'Contact.remove', arguments); var fail = errorCB && function(code) { errorCB(new ContactError(code)); }; if (this.id === null) { fail(ContactError.UNKNOWN_ERROR); } else { exec(successCB, fail, "Contacts", "remove", [this.id]); } }; /** * Creates a deep copy of this Contact. * With the contact ID set to null. * @return copy of this Contact */ Contact.prototype.clone = function() { var clonedContact = utils.clone(this); clonedContact.id = null; clonedContact.rawId = null; function nullIds(arr) { if (arr) { for (var i = 0; i < arr.length; ++i) { arr[i].id = null; } } } // Loop through and clear out any id's in phones, emails, etc. nullIds(clonedContact.phoneNumbers); nullIds(clonedContact.emails); nullIds(clonedContact.addresses); nullIds(clonedContact.ims); nullIds(clonedContact.organizations); nullIds(clonedContact.categories); nullIds(clonedContact.photos); nullIds(clonedContact.urls); return clonedContact; }; /** * Persists contact to device storage. * @param successCB success callback * @param errorCB error callback */ Contact.prototype.save = function(successCB, errorCB) { argscheck.checkArgs('FFO', 'Contact.save', arguments); var fail = errorCB && function(code) { errorCB(new ContactError(code)); }; var success = function(result) { if (result) { if (successCB) { var fullContact = require('cordova/plugin/contacts').create(result); successCB(convertIn(fullContact)); } } else { // no Entry object returned fail(ContactError.UNKNOWN_ERROR); } }; var dupContact = convertOut(utils.clone(this)); exec(success, fail, "Contacts", "save", [dupContact]); }; module.exports = Contact; }); // file: lib/common/plugin/ContactAddress.js define("cordova/plugin/ContactAddress", function(require, exports, module) { /** * Contact address. * @constructor * @param {DOMString} id unique identifier, should only be set by native code * @param formatted // NOTE: not a W3C standard * @param streetAddress * @param locality * @param region * @param postalCode * @param country */ var ContactAddress = function(pref, type, formatted, streetAddress, locality, region, postalCode, country) { this.id = null; this.pref = (typeof pref != 'undefined' ? pref : false); this.type = type || null; this.formatted = formatted || null; this.streetAddress = streetAddress || null; this.locality = locality || null; this.region = region || null; this.postalCode = postalCode || null; this.country = country || null; }; module.exports = ContactAddress; }); // file: lib/common/plugin/ContactError.js define("cordova/plugin/ContactError", function(require, exports, module) { /** * ContactError. * An error code assigned by an implementation when an error has occurred * @constructor */ var ContactError = function(err) { this.code = (typeof err != 'undefined' ? err : null); }; /** * Error codes */ ContactError.UNKNOWN_ERROR = 0; ContactError.INVALID_ARGUMENT_ERROR = 1; ContactError.TIMEOUT_ERROR = 2; ContactError.PENDING_OPERATION_ERROR = 3; ContactError.IO_ERROR = 4; ContactError.NOT_SUPPORTED_ERROR = 5; ContactError.PERMISSION_DENIED_ERROR = 20; module.exports = ContactError; }); // file: lib/common/plugin/ContactField.js define("cordova/plugin/ContactField", function(require, exports, module) { /** * Generic contact field. * @constructor * @param {DOMString} id unique identifier, should only be set by native code // NOTE: not a W3C standard * @param type * @param value * @param pref */ var ContactField = function(type, value, pref) { this.id = null; this.type = (type && type.toString()) || null; this.value = (value && value.toString()) || null; this.pref = (typeof pref != 'undefined' ? pref : false); }; module.exports = ContactField; }); // file: lib/common/plugin/ContactFindOptions.js define("cordova/plugin/ContactFindOptions", function(require, exports, module) { /** * ContactFindOptions. * @constructor * @param filter used to match contacts against * @param multiple boolean used to determine if more than one contact should be returned */ var ContactFindOptions = function(filter, multiple) { this.filter = filter || ''; this.multiple = (typeof multiple != 'undefined' ? multiple : false); }; module.exports = ContactFindOptions; }); // file: lib/common/plugin/ContactName.js define("cordova/plugin/ContactName", function(require, exports, module) { /** * Contact name. * @constructor * @param formatted // NOTE: not part of W3C standard * @param familyName * @param givenName * @param middle * @param prefix * @param suffix */ var ContactName = function(formatted, familyName, givenName, middle, prefix, suffix) { this.formatted = formatted || null; this.familyName = familyName || null; this.givenName = givenName || null; this.middleName = middle || null; this.honorificPrefix = prefix || null; this.honorificSuffix = suffix || null; }; module.exports = ContactName; }); // file: lib/common/plugin/ContactOrganization.js define("cordova/plugin/ContactOrganization", function(require, exports, module) { /** * Contact organization. * @constructor * @param {DOMString} id unique identifier, should only be set by native code // NOTE: not a W3C standard * @param name * @param dept * @param title * @param startDate * @param endDate * @param location * @param desc */ var ContactOrganization = function(pref, type, name, dept, title) { this.id = null; this.pref = (typeof pref != 'undefined' ? pref : false); this.type = type || null; this.name = name || null; this.department = dept || null; this.title = title || null; }; module.exports = ContactOrganization; }); // file: lib/common/plugin/Coordinates.js define("cordova/plugin/Coordinates", function(require, exports, module) { /** * This class contains position information. * @param {Object} lat * @param {Object} lng * @param {Object} alt * @param {Object} acc * @param {Object} head * @param {Object} vel * @param {Object} altacc * @constructor */ var Coordinates = function(lat, lng, alt, acc, head, vel, altacc) { /** * The latitude of the position. */ this.latitude = lat; /** * The longitude of the position, */ this.longitude = lng; /** * The accuracy of the position. */ this.accuracy = acc; /** * The altitude of the position. */ this.altitude = (alt !== undefined ? alt : null); /** * The direction the device is moving at the position. */ this.heading = (head !== undefined ? head : null); /** * The velocity with which the device is moving at the position. */ this.speed = (vel !== undefined ? vel : null); if (this.speed === 0 || this.speed === null) { this.heading = NaN; } /** * The altitude accuracy of the position. */ this.altitudeAccuracy = (altacc !== undefined) ? altacc : null; }; module.exports = Coordinates; }); // file: lib/common/plugin/DirectoryEntry.js define("cordova/plugin/DirectoryEntry", function(require, exports, module) { var argscheck = require('cordova/argscheck'), utils = require('cordova/utils'), exec = require('cordova/exec'), Entry = require('cordova/plugin/Entry'), FileError = require('cordova/plugin/FileError'), DirectoryReader = require('cordova/plugin/DirectoryReader'); /** * An interface representing a directory on the file system. * * {boolean} isFile always false (readonly) * {boolean} isDirectory always true (readonly) * {DOMString} name of the directory, excluding the path leading to it (readonly) * {DOMString} fullPath the absolute full path to the directory (readonly) * TODO: implement this!!! {FileSystem} filesystem on which the directory resides (readonly) */ var DirectoryEntry = function(name, fullPath) { DirectoryEntry.__super__.constructor.call(this, false, true, name, fullPath); }; utils.extend(DirectoryEntry, Entry); /** * Creates a new DirectoryReader to read entries from this directory */ DirectoryEntry.prototype.createReader = function() { return new DirectoryReader(this.fullPath); }; /** * Creates or looks up a directory * * @param {DOMString} path either a relative or absolute path from this directory in which to look up or create a directory * @param {Flags} options to create or exclusively create the directory * @param {Function} successCallback is called with the new entry * @param {Function} errorCallback is called with a FileError */ DirectoryEntry.prototype.getDirectory = function(path, options, successCallback, errorCallback) { argscheck.checkArgs('sOFF', 'DirectoryEntry.getDirectory', arguments); var win = successCallback && function(result) { var entry = new DirectoryEntry(result.name, result.fullPath); successCallback(entry); }; var fail = errorCallback && function(code) { errorCallback(new FileError(code)); }; exec(win, fail, "File", "getDirectory", [this.fullPath, path, options]); }; /** * Deletes a directory and all of it's contents * * @param {Function} successCallback is called with no parameters * @param {Function} errorCallback is called with a FileError */ DirectoryEntry.prototype.removeRecursively = function(successCallback, errorCallback) { argscheck.checkArgs('FF', 'DirectoryEntry.removeRecursively', arguments); var fail = errorCallback && function(code) { errorCallback(new FileError(code)); }; exec(successCallback, fail, "File", "removeRecursively", [this.fullPath]); }; /** * Creates or looks up a file * * @param {DOMString} path either a relative or absolute path from this directory in which to look up or create a file * @param {Flags} options to create or exclusively create the file * @param {Function} successCallback is called with the new entry * @param {Function} errorCallback is called with a FileError */ DirectoryEntry.prototype.getFile = function(path, options, successCallback, errorCallback) { argscheck.checkArgs('sOFF', 'DirectoryEntry.getFile', arguments); var win = successCallback && function(result) { var FileEntry = require('cordova/plugin/FileEntry'); var entry = new FileEntry(result.name, result.fullPath); successCallback(entry); }; var fail = errorCallback && function(code) { errorCallback(new FileError(code)); }; exec(win, fail, "File", "getFile", [this.fullPath, path, options]); }; module.exports = DirectoryEntry; }); // file: lib/common/plugin/DirectoryReader.js define("cordova/plugin/DirectoryReader", function(require, exports, module) { var exec = require('cordova/exec'), FileError = require('cordova/plugin/FileError') ; /** * An interface that lists the files and directories in a directory. */ function DirectoryReader(path) { this.path = path || null; } /** * Returns a list of entries from a directory. * * @param {Function} successCallback is called with a list of entries * @param {Function} errorCallback is called with a FileError */ DirectoryReader.prototype.readEntries = function(successCallback, errorCallback) { var win = typeof successCallback !== 'function' ? null : function(result) { var retVal = []; for (var i=0; i<result.length; i++) { var entry = null; if (result[i].isDirectory) { entry = new (require('cordova/plugin/DirectoryEntry'))(); } else if (result[i].isFile) { entry = new (require('cordova/plugin/FileEntry'))(); } entry.isDirectory = result[i].isDirectory; entry.isFile = result[i].isFile; entry.name = result[i].name; entry.fullPath = result[i].fullPath; retVal.push(entry); } successCallback(retVal); }; var fail = typeof errorCallback !== 'function' ? null : function(code) { errorCallback(new FileError(code)); }; exec(win, fail, "File", "readEntries", [this.path]); }; module.exports = DirectoryReader; }); // file: lib/common/plugin/Entry.js define("cordova/plugin/Entry", function(require, exports, module) { var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), FileError = require('cordova/plugin/FileError'), Metadata = require('cordova/plugin/Metadata'); /** * Represents a file or directory on the local file system. * * @param isFile * {boolean} true if Entry is a file (readonly) * @param isDirectory * {boolean} true if Entry is a directory (readonly) * @param name * {DOMString} name of the file or directory, excluding the path * leading to it (readonly) * @param fullPath * {DOMString} the absolute full path to the file or directory * (readonly) */ function Entry(isFile, isDirectory, name, fullPath, fileSystem) { this.isFile = !!isFile; this.isDirectory = !!isDirectory; this.name = name || ''; this.fullPath = fullPath || ''; this.filesystem = fileSystem || null; } /** * Look up the metadata of the entry. * * @param successCallback * {Function} is called with a Metadata object * @param errorCallback * {Function} is called with a FileError */ Entry.prototype.getMetadata = function(successCallback, errorCallback) { argscheck.checkArgs('FF', 'Entry.getMetadata', arguments); var success = successCallback && function(lastModified) { var metadata = new Metadata(lastModified); successCallback(metadata); }; var fail = errorCallback && function(code) { errorCallback(new FileError(code)); }; exec(success, fail, "File", "getMetadata", [this.fullPath]); }; /** * Set the metadata of the entry. * * @param successCallback * {Function} is called with a Metadata object * @param errorCallback * {Function} is called with a FileError * @param metadataObject * {Object} keys and values to set */ Entry.prototype.setMetadata = function(successCallback, errorCallback, metadataObject) { argscheck.checkArgs('FFO', 'Entry.setMetadata', arguments); exec(successCallback, errorCallback, "File", "setMetadata", [this.fullPath, metadataObject]); }; /** * Move a file or directory to a new location. * * @param parent * {DirectoryEntry} the directory to which to move this entry * @param newName * {DOMString} new name of the entry, defaults to the current name * @param successCallback * {Function} called with the new DirectoryEntry object * @param errorCallback * {Function} called with a FileError */ Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) { argscheck.checkArgs('oSFF', 'Entry.moveTo', arguments); var fail = errorCallback && function(code) { errorCallback(new FileError(code)); }; // source path var srcPath = this.fullPath, // entry name name = newName || this.name, success = function(entry) { if (entry) { if (successCallback) { // create appropriate Entry object var result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name, entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath); successCallback(result); } } else { // no Entry object returned fail && fail(FileError.NOT_FOUND_ERR); } }; // copy exec(success, fail, "File", "moveTo", [srcPath, parent.fullPath, name]); }; /** * Copy a directory to a different location. * * @param parent * {DirectoryEntry} the directory to which to copy the entry * @param newName * {DOMString} new name of the entry, defaults to the current name * @param successCallback * {Function} called with the new Entry object * @param errorCallback * {Function} called with a FileError */ Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) { argscheck.checkArgs('oSFF', 'Entry.copyTo', arguments); var fail = errorCallback && function(code) { errorCallback(new FileError(code)); }; // source path var srcPath = this.fullPath, // entry name name = newName || this.name, // success callback success = function(entry) { if (entry) { if (successCallback) { // create appropriate Entry object var result = (entry.isDirectory) ? new (require('cordova/plugin/DirectoryEntry'))(entry.name, entry.fullPath) : new (require('cordova/plugin/FileEntry'))(entry.name, entry.fullPath); successCallback(result); } } else { // no Entry object returned fail && fail(FileError.NOT_FOUND_ERR); } }; // copy exec(success, fail, "File", "copyTo", [srcPath, parent.fullPath, name]); }; /** * Return a URL that can be used to identify this entry. */ Entry.prototype.toURL = function() { // fullPath attribute contains the full URL return this.fullPath; }; /** * Returns a URI that can be used to identify this entry. * * @param {DOMString} mimeType for a FileEntry, the mime type to be used to interpret the file, when loaded through this URI. * @return uri */ Entry.prototype.toURI = function(mimeType) { console.log("DEPRECATED: Update your code to use 'toURL'"); // fullPath attribute contains the full URI return this.toURL(); }; /** * Remove a file or directory. It is an error to attempt to delete a * directory that is not empty. It is an error to attempt to delete a * root directory of a file system. * * @param successCallback {Function} called with no parameters * @param errorCallback {Function} called with a FileError */ Entry.prototype.remove = function(successCallback, errorCallback) { argscheck.checkArgs('FF', 'Entry.remove', arguments); var fail = errorCallback && function(code) { errorCallback(new FileError(code)); }; exec(successCallback, fail, "File", "remove", [this.fullPath]); }; /** * Look up the parent DirectoryEntry of this entry. * * @param successCallback {Function} called with the parent DirectoryEntry object * @param errorCallback {Function} called with a FileError */ Entry.prototype.getParent = function(successCallback, errorCallback) { argscheck.checkArgs('FF', 'Entry.getParent', arguments); var win = successCallback && function(result) { var DirectoryEntry = require('cordova/plugin/DirectoryEntry'); var entry = new DirectoryEntry(result.name, result.fullPath); successCallback(entry); }; var fail = errorCallback && function(code) { errorCallback(new FileError(code)); }; exec(win, fail, "File", "getParent", [this.fullPath]); }; module.exports = Entry; }); // file: lib/common/plugin/File.js define("cordova/plugin/File", function(require, exports, module) { /** * Constructor. * name {DOMString} name of the file, without path information * fullPath {DOMString} the full path of the file, including the name * type {DOMString} mime type * lastModifiedDate {Date} last modified date * size {Number} size of the file in bytes */ var File = function(name, fullPath, type, lastModifiedDate, size){ this.name = name || ''; this.fullPath = fullPath || null; this.type = type || null; this.lastModifiedDate = lastModifiedDate || null; this.size = size || 0; // These store the absolute start and end for slicing the file. this.start = 0; this.end = this.size; }; /** * Returns a "slice" of the file. Since Cordova Files don't contain the actual * content, this really returns a File with adjusted start and end. * Slices of slices are supported. * start {Number} The index at which to start the slice (inclusive). * end {Number} The index at which to end the slice (exclusive). */ File.prototype.slice = function(start, end) { var size = this.end - this.start; var newStart = 0; var newEnd = size; if (arguments.length) { if (start < 0) { newStart = Math.max(size + start, 0); } else { newStart = Math.min(size, start); } } if (arguments.length >= 2) { if (end < 0) { newEnd = Math.max(size + end, 0); } else { newEnd = Math.min(end, size); } } var newFile = new File(this.name, this.fullPath, this.type, this.lastModifiedData, this.size); newFile.start = this.start + newStart; newFile.end = this.start + newEnd; return newFile; }; module.exports = File; }); // file: lib/common/plugin/FileEntry.js define("cordova/plugin/FileEntry", function(require, exports, module) { var utils = require('cordova/utils'), exec = require('cordova/exec'), Entry = require('cordova/plugin/Entry'), FileWriter = require('cordova/plugin/FileWriter'), File = require('cordova/plugin/File'), FileError = require('cordova/plugin/FileError'); /** * An interface representing a file on the file system. * * {boolean} isFile always true (readonly) * {boolean} isDirectory always false (readonly) * {DOMString} name of the file, excluding the path leading to it (readonly) * {DOMString} fullPath the absolute full path to the file (readonly) * {FileSystem} filesystem on which the file resides (readonly) */ var FileEntry = function(name, fullPath) { FileEntry.__super__.constructor.apply(this, [true, false, name, fullPath]); }; utils.extend(FileEntry, Entry); /** * Creates a new FileWriter associated with the file that this FileEntry represents. * * @param {Function} successCallback is called with the new FileWriter * @param {Function} errorCallback is called with a FileError */ FileEntry.prototype.createWriter = function(successCallback, errorCallback) { this.file(function(filePointer) { var writer = new FileWriter(filePointer); if (writer.fileName === null || writer.fileName === "") { errorCallback && errorCallback(new FileError(FileError.INVALID_STATE_ERR)); } else { successCallback && successCallback(writer); } }, errorCallback); }; /** * Returns a File that represents the current state of the file that this FileEntry represents. * * @param {Function} successCallback is called with the new File object * @param {Function} errorCallback is called with a FileError */ FileEntry.prototype.file = function(successCallback, errorCallback) { var win = successCallback && function(f) { var file = new File(f.name, f.fullPath, f.type, f.lastModifiedDate, f.size); successCallback(file); }; var fail = errorCallback && function(code) { errorCallback(new FileError(code)); }; exec(win, fail, "File", "getFileMetadata", [this.fullPath]); }; module.exports = FileEntry; }); // file: lib/common/plugin/FileError.js define("cordova/plugin/FileError", function(require, exports, module) { /** * FileError */ function FileError(error) { this.code = error || null; } // File error codes // Found in DOMException FileError.NOT_FOUND_ERR = 1; FileError.SECURITY_ERR = 2; FileError.ABORT_ERR = 3; // Added by File API specification FileError.NOT_READABLE_ERR = 4; FileError.ENCODING_ERR = 5; FileError.NO_MODIFICATION_ALLOWED_ERR = 6; FileError.INVALID_STATE_ERR = 7; FileError.SYNTAX_ERR = 8; FileError.INVALID_MODIFICATION_ERR = 9; FileError.QUOTA_EXCEEDED_ERR = 10; FileError.TYPE_MISMATCH_ERR = 11; FileError.PATH_EXISTS_ERR = 12; module.exports = FileError; }); // file: lib/common/plugin/FileReader.js define("cordova/plugin/FileReader", function(require, exports, module) { var exec = require('cordova/exec'), modulemapper = require('cordova/modulemapper'), utils = require('cordova/utils'), File = require('cordova/plugin/File'), FileError = require('cordova/plugin/FileError'), ProgressEvent = require('cordova/plugin/ProgressEvent'), origFileReader = modulemapper.getOriginalSymbol(this, 'FileReader'); /** * This class reads the mobile device file system. * * For Android: * The root directory is the root of the file system. * To read from the SD card, the file name is "sdcard/my_file.txt" * @constructor */ var FileReader = function() { this._readyState = 0; this._error = null; this._result = null; this._fileName = ''; this._realReader = origFileReader ? new origFileReader() : {}; }; // States FileReader.EMPTY = 0; FileReader.LOADING = 1; FileReader.DONE = 2; utils.defineGetter(FileReader.prototype, 'readyState', function() { return this._fileName ? this._readyState : this._realReader.readyState; }); utils.defineGetter(FileReader.prototype, 'error', function() { return this._fileName ? this._error: this._realReader.error; }); utils.defineGetter(FileReader.prototype, 'result', function() { return this._fileName ? this._result: this._realReader.result; }); function defineEvent(eventName) { utils.defineGetterSetter(FileReader.prototype, eventName, function() { return this._realReader[eventName] || null; }, function(value) { this._realReader[eventName] = value; }); } defineEvent('onloadstart'); // When the read starts. defineEvent('onprogress'); // While reading (and decoding) file or fileBlob data, and reporting partial file data (progress.loaded/progress.total) defineEvent('onload'); // When the read has successfully completed. defineEvent('onerror'); // When the read has failed (see errors). defineEvent('onloadend'); // When the request has completed (either in success or failure). defineEvent('onabort'); // When the read has been aborted. For instance, by invoking the abort() method. function initRead(reader, file) { // Already loading something if (reader.readyState == FileReader.LOADING) { throw new FileError(FileError.INVALID_STATE_ERR); } reader._result = null; reader._error = null; reader._readyState = FileReader.LOADING; if (typeof file == 'string') { // Deprecated in Cordova 2.4. console.warn('Using a string argument with FileReader.readAs functions is deprecated.'); reader._fileName = file; } else if (typeof file.fullPath == 'string') { reader._fileName = file.fullPath; } else { reader._fileName = ''; return true; } reader.onloadstart && reader.onloadstart(new ProgressEvent("loadstart", {target:reader})); } /** * Abort reading file. */ FileReader.prototype.abort = function() { if (origFileReader && !this._fileName) { return this._realReader.abort(); } this._result = null; if (this._readyState == FileReader.DONE || this._readyState == FileReader.EMPTY) { return; } this._readyState = FileReader.DONE; // If abort callback if (typeof this.onabort === 'function') { this.onabort(new ProgressEvent('abort', {target:this})); } // If load end callback if (typeof this.onloadend === 'function') { this.onloadend(new ProgressEvent('loadend', {target:this})); } }; /** * Read text file. * * @param file {File} File object containing file properties * @param encoding [Optional] (see http://www.iana.org/assignments/character-sets) */ FileReader.prototype.readAsText = function(file, encoding) { if (initRead(this, file)) { return this._realReader.readAsText(file, encoding); } // Default encoding is UTF-8 var enc = encoding ? encoding : "UTF-8"; var me = this; var execArgs = [this._fileName, enc, file.start, file.end]; // Read file exec( // Success callback function(r) { // If DONE (cancelled), then don't do anything if (me._readyState === FileReader.DONE) { return; } // Save result me._result = r; // If onload callback if (typeof me.onload === "function") { me.onload(new ProgressEvent("load", {target:me})); } // DONE state me._readyState = FileReader.DONE; // If onloadend callback if (typeof me.onloadend === "function") { me.onloadend(new ProgressEvent("loadend", {target:me})); } }, // Error callback function(e) { // If DONE (cancelled), then don't do anything if (me._readyState === FileReader.DONE) { return; } // DONE state me._readyState = FileReader.DONE; // null result me._result = null; // Save error me._error = new FileError(e); // If onerror callback if (typeof me.onerror === "function") { me.onerror(new ProgressEvent("error", {target:me})); } // If onloadend callback if (typeof me.onloadend === "function") { me.onloadend(new ProgressEvent("loadend", {target:me})); } }, "File", "readAsText", execArgs); }; /** * Read file and return data as a base64 encoded data url. * A data url is of the form: * data:[<mediatype>][;base64],<data> * * @param file {File} File object containing file properties */ FileReader.prototype.readAsDataURL = function(file) { if (initRead(this, file)) { return this._realReader.readAsDataURL(file); } var me = this; var execArgs = [this._fileName, file.start, file.end]; // Read file exec( // Success callback function(r) { // If DONE (cancelled), then don't do anything if (me._readyState === FileReader.DONE) { return; } // DONE state me._readyState = FileReader.DONE; // Save result me._result = r; // If onload callback if (typeof me.onload === "function") { me.onload(new ProgressEvent("load", {target:me})); } // If onloadend callback if (typeof me.onloadend === "function") { me.onloadend(new ProgressEvent("loadend", {target:me})); } }, // Error callback function(e) { // If DONE (cancelled), then don't do anything if (me._readyState === FileReader.DONE) { return; } // DONE state me._readyState = FileReader.DONE; me._result = null; // Save error me._error = new FileError(e); // If onerror callback if (typeof me.onerror === "function") { me.onerror(new ProgressEvent("error", {target:me})); } // If onloadend callback if (typeof me.onloadend === "function") { me.onloadend(new ProgressEvent("loadend", {target:me})); } }, "File", "readAsDataURL", execArgs); }; /** * Read file and return data as a binary data. * * @param file {File} File object containing file properties */ FileReader.prototype.readAsBinaryString = function(file) { if (initRead(this, file)) { return this._realReader.readAsBinaryString(file); } var me = this; var execArgs = [this._fileName, file.start, file.end]; // Read file exec( // Success callback function(r) { // If DONE (cancelled), then don't do anything if (me._readyState === FileReader.DONE) { return; } // DONE state me._readyState = FileReader.DONE; me._result = r; // If onload callback if (typeof me.onload === "function") { me.onload(new ProgressEvent("load", {target:me})); } // If onloadend callback if (typeof me.onloadend === "function") { me.onloadend(new ProgressEvent("loadend", {target:me})); } }, // Error callback function(e) { // If DONE (cancelled), then don't do anything if (me._readyState === FileReader.DONE) { return; } // DONE state me._readyState = FileReader.DONE; me._result = null; // Save error me._error = new FileError(e); // If onerror callback if (typeof me.onerror === "function") { me.onerror(new ProgressEvent("error", {target:me})); } // If onloadend callback if (typeof me.onloadend === "function") { me.onloadend(new ProgressEvent("loadend", {target:me})); } }, "File", "readAsBinaryString", execArgs); }; /** * Read file and return data as a binary data. * * @param file {File} File object containing file properties */ FileReader.prototype.readAsArrayBuffer = function(file) { if (initRead(this, file)) { return this._realReader.readAsArrayBuffer(file); } var me = this; var execArgs = [this._fileName, file.start, file.end]; // Read file exec( // Success callback function(r) { // If DONE (cancelled), then don't do anything if (me._readyState === FileReader.DONE) { return; } // DONE state me._readyState = FileReader.DONE; me._result = r; // If onload callback if (typeof me.onload === "function") { me.onload(new ProgressEvent("load", {target:me})); } // If onloadend callback if (typeof me.onloadend === "function") { me.onloadend(new ProgressEvent("loadend", {target:me})); } }, // Error callback function(e) { // If DONE (cancelled), then don't do anything if (me._readyState === FileReader.DONE) { return; } // DONE state me._readyState = FileReader.DONE; me._result = null; // Save error me._error = new FileError(e); // If onerror callback if (typeof me.onerror === "function") { me.onerror(new ProgressEvent("error", {target:me})); } // If onloadend callback if (typeof me.onloadend === "function") { me.onloadend(new ProgressEvent("loadend", {target:me})); } }, "File", "readAsArrayBuffer", execArgs); }; module.exports = FileReader; }); // file: lib/common/plugin/FileSystem.js define("cordova/plugin/FileSystem", function(require, exports, module) { var DirectoryEntry = require('cordova/plugin/DirectoryEntry'); /** * An interface representing a file system * * @constructor * {DOMString} name the unique name of the file system (readonly) * {DirectoryEntry} root directory of the file system (readonly) */ var FileSystem = function(name, root) { this.name = name || null; if (root) { this.root = new DirectoryEntry(root.name, root.fullPath); } }; module.exports = FileSystem; }); // file: lib/common/plugin/FileTransfer.js define("cordova/plugin/FileTransfer", function(require, exports, module) { var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), FileTransferError = require('cordova/plugin/FileTransferError'), ProgressEvent = require('cordova/plugin/ProgressEvent'); function newProgressEvent(result) { var pe = new ProgressEvent(); pe.lengthComputable = result.lengthComputable; pe.loaded = result.loaded; pe.total = result.total; return pe; } function getBasicAuthHeader(urlString) { var header = null; if (window.btoa) { // parse the url using the Location object var url = document.createElement('a'); url.href = urlString; var credentials = null; var protocol = url.protocol + "//"; var origin = protocol + url.host; // check whether there are the username:password credentials in the url if (url.href.indexOf(origin) !== 0) { // credentials found var atIndex = url.href.indexOf("@"); credentials = url.href.substring(protocol.length, atIndex); } if (credentials) { var authHeader = "Authorization"; var authHeaderValue = "Basic " + window.btoa(credentials); header = { name : authHeader, value : authHeaderValue }; } } return header; } var idCounter = 0; /** * FileTransfer uploads a file to a remote server. * @constructor */ var FileTransfer = function() { this._id = ++idCounter; this.onprogress = null; // optional callback }; /** * Given an absolute file path, uploads a file on the device to a remote server * using a multipart HTTP request. * @param filePath {String} Full path of the file on the device * @param server {String} URL of the server to receive the file * @param successCallback (Function} Callback to be invoked when upload has completed * @param errorCallback {Function} Callback to be invoked upon error * @param options {FileUploadOptions} Optional parameters such as file name and mimetype * @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false */ FileTransfer.prototype.upload = function(filePath, server, successCallback, errorCallback, options, trustAllHosts) { argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments); // check for options var fileKey = null; var fileName = null; var mimeType = null; var params = null; var chunkedMode = true; var headers = null; var httpMethod = null; var basicAuthHeader = getBasicAuthHeader(server); if (basicAuthHeader) { options = options || {}; options.headers = options.headers || {}; options.headers[basicAuthHeader.name] = basicAuthHeader.value; } if (options) { fileKey = options.fileKey; fileName = options.fileName; mimeType = options.mimeType; headers = options.headers; httpMethod = options.httpMethod || "POST"; if (httpMethod.toUpperCase() == "PUT"){ httpMethod = "PUT"; } else { httpMethod = "POST"; } if (options.chunkedMode !== null || typeof options.chunkedMode != "undefined") { chunkedMode = options.chunkedMode; } if (options.params) { params = options.params; } else { params = {}; } } var fail = errorCallback && function(e) { var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body); errorCallback(error); }; var self = this; var win = function(result) { if (typeof result.lengthComputable != "undefined") { if (self.onprogress) { self.onprogress(newProgressEvent(result)); } } else { successCallback && successCallback(result); } }; exec(win, fail, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id, httpMethod]); }; /** * Downloads a file form a given URL and saves it to the specified directory. * @param source {String} URL of the server to receive the file * @param target {String} Full path of the file on the device * @param successCallback (Function} Callback to be invoked when upload has completed * @param errorCallback {Function} Callback to be invoked upon error * @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false * @param options {FileDownloadOptions} Optional parameters such as headers */ FileTransfer.prototype.download = function(source, target, successCallback, errorCallback, trustAllHosts, options) { argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments); var self = this; var basicAuthHeader = getBasicAuthHeader(source); if (basicAuthHeader) { options = options || {}; options.headers = options.headers || {}; options.headers[basicAuthHeader.name] = basicAuthHeader.value; } var headers = null; if (options) { headers = options.headers || null; } var win = function(result) { if (typeof result.lengthComputable != "undefined") { if (self.onprogress) { return self.onprogress(newProgressEvent(result)); } } else if (successCallback) { var entry = null; if (result.isDirectory) { entry = new (require('cordova/plugin/DirectoryEntry'))(); } else if (result.isFile) { entry = new (require('cordova/plugin/FileEntry'))(); } entry.isDirectory = result.isDirectory; entry.isFile = result.isFile; entry.name = result.name; entry.fullPath = result.fullPath; successCallback(entry); } }; var fail = errorCallback && function(e) { var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body); errorCallback(error); }; exec(win, fail, 'FileTransfer', 'download', [source, target, trustAllHosts, this._id, headers]); }; /** * Aborts the ongoing file transfer on this object. The original error * callback for the file transfer will be called if necessary. */ FileTransfer.prototype.abort = function() { exec(null, null, 'FileTransfer', 'abort', [this._id]); }; module.exports = FileTransfer; }); // file: lib/common/plugin/FileTransferError.js define("cordova/plugin/FileTransferError", function(require, exports, module) { /** * FileTransferError * @constructor */ var FileTransferError = function(code, source, target, status, body) { this.code = code || null; this.source = source || null; this.target = target || null; this.http_status = status || null; this.body = body || null; }; FileTransferError.FILE_NOT_FOUND_ERR = 1; FileTransferError.INVALID_URL_ERR = 2; FileTransferError.CONNECTION_ERR = 3; FileTransferError.ABORT_ERR = 4; module.exports = FileTransferError; }); // file: lib/common/plugin/FileUploadOptions.js define("cordova/plugin/FileUploadOptions", function(require, exports, module) { /** * Options to customize the HTTP request used to upload files. * @constructor * @param fileKey {String} Name of file request parameter. * @param fileName {String} Filename to be used by the server. Defaults to image.jpg. * @param mimeType {String} Mimetype of the uploaded file. Defaults to image/jpeg. * @param params {Object} Object with key: value params to send to the server. * @param headers {Object} Keys are header names, values are header values. Multiple * headers of the same name are not supported. */ var FileUploadOptions = function(fileKey, fileName, mimeType, params, headers, httpMethod) { this.fileKey = fileKey || null; this.fileName = fileName || null; this.mimeType = mimeType || null; this.params = params || null; this.headers = headers || null; this.httpMethod = httpMethod || null; }; module.exports = FileUploadOptions; }); // file: lib/common/plugin/FileUploadResult.js define("cordova/plugin/FileUploadResult", function(require, exports, module) { /** * FileUploadResult * @constructor */ var FileUploadResult = function() { this.bytesSent = 0; this.responseCode = null; this.response = null; }; module.exports = FileUploadResult; }); // file: lib/common/plugin/FileWriter.js define("cordova/plugin/FileWriter", function(require, exports, module) { var exec = require('cordova/exec'), FileError = require('cordova/plugin/FileError'), ProgressEvent = require('cordova/plugin/ProgressEvent'); /** * This class writes to the mobile device file system. * * For Android: * The root directory is the root of the file system. * To write to the SD card, the file name is "sdcard/my_file.txt" * * @constructor * @param file {File} File object containing file properties * @param append if true write to the end of the file, otherwise overwrite the file */ var FileWriter = function(file) { this.fileName = ""; this.length = 0; if (file) { this.fileName = file.fullPath || file; this.length = file.size || 0; } // default is to write at the beginning of the file this.position = 0; this.readyState = 0; // EMPTY this.result = null; // Error this.error = null; // Event handlers this.onwritestart = null; // When writing starts this.onprogress = null; // While writing the file, and reporting partial file data this.onwrite = null; // When the write has successfully completed. this.onwriteend = null; // When the request has completed (either in success or failure). this.onabort = null; // When the write has been aborted. For instance, by invoking the abort() method. this.onerror = null; // When the write has failed (see errors). }; // States FileWriter.INIT = 0; FileWriter.WRITING = 1; FileWriter.DONE = 2; /** * Abort writing file. */ FileWriter.prototype.abort = function() { // check for invalid state if (this.readyState === FileWriter.DONE || this.readyState === FileWriter.INIT) { throw new FileError(FileError.INVALID_STATE_ERR); } // set error this.error = new FileError(FileError.ABORT_ERR); this.readyState = FileWriter.DONE; // If abort callback if (typeof this.onabort === "function") { this.onabort(new ProgressEvent("abort", {"target":this})); } // If write end callback if (typeof this.onwriteend === "function") { this.onwriteend(new ProgressEvent("writeend", {"target":this})); } }; /** * Writes data to the file * * @param text to be written */ FileWriter.prototype.write = function(text) { // Throw an exception if we are already writing a file if (this.readyState === FileWriter.WRITING) { throw new FileError(FileError.INVALID_STATE_ERR); } // WRITING state this.readyState = FileWriter.WRITING; var me = this; // If onwritestart callback if (typeof me.onwritestart === "function") { me.onwritestart(new ProgressEvent("writestart", {"target":me})); } // Write file exec( // Success callback function(r) { // If DONE (cancelled), then don't do anything if (me.readyState === FileWriter.DONE) { return; } // position always increases by bytes written because file would be extended me.position += r; // The length of the file is now where we are done writing. me.length = me.position; // DONE state me.readyState = FileWriter.DONE; // If onwrite callback if (typeof me.onwrite === "function") { me.onwrite(new ProgressEvent("write", {"target":me})); } // If onwriteend callback if (typeof me.onwriteend === "function") { me.onwriteend(new ProgressEvent("writeend", {"target":me})); } }, // Error callback function(e) { // If DONE (cancelled), then don't do anything if (me.readyState === FileWriter.DONE) { return; } // DONE state me.readyState = FileWriter.DONE; // Save error me.error = new FileError(e); // If onerror callback if (typeof me.onerror === "function") { me.onerror(new ProgressEvent("error", {"target":me})); } // If onwriteend callback if (typeof me.onwriteend === "function") { me.onwriteend(new ProgressEvent("writeend", {"target":me})); } }, "File", "write", [this.fileName, text, this.position]); }; /** * Moves the file pointer to the location specified. * * If the offset is a negative number the position of the file * pointer is rewound. If the offset is greater than the file * size the position is set to the end of the file. * * @param offset is the location to move the file pointer to. */ FileWriter.prototype.seek = function(offset) { // Throw an exception if we are already writing a file if (this.readyState === FileWriter.WRITING) { throw new FileError(FileError.INVALID_STATE_ERR); } if (!offset && offset !== 0) { return; } // See back from end of file. if (offset < 0) { this.position = Math.max(offset + this.length, 0); } // Offset is bigger than file size so set position // to the end of the file. else if (offset > this.length) { this.position = this.length; } // Offset is between 0 and file size so set the position // to start writing. else { this.position = offset; } }; /** * Truncates the file to the size specified. * * @param size to chop the file at. */ FileWriter.prototype.truncate = function(size) { // Throw an exception if we are already writing a file if (this.readyState === FileWriter.WRITING) { throw new FileError(FileError.INVALID_STATE_ERR); } // WRITING state this.readyState = FileWriter.WRITING; var me = this; // If onwritestart callback if (typeof me.onwritestart === "function") { me.onwritestart(new ProgressEvent("writestart", {"target":this})); } // Write file exec( // Success callback function(r) { // If DONE (cancelled), then don't do anything if (me.readyState === FileWriter.DONE) { return; } // DONE state me.readyState = FileWriter.DONE; // Update the length of the file me.length = r; me.position = Math.min(me.position, r); // If onwrite callback if (typeof me.onwrite === "function") { me.onwrite(new ProgressEvent("write", {"target":me})); } // If onwriteend callback if (typeof me.onwriteend === "function") { me.onwriteend(new ProgressEvent("writeend", {"target":me})); } }, // Error callback function(e) { // If DONE (cancelled), then don't do anything if (me.readyState === FileWriter.DONE) { return; } // DONE state me.readyState = FileWriter.DONE; // Save error me.error = new FileError(e); // If onerror callback if (typeof me.onerror === "function") { me.onerror(new ProgressEvent("error", {"target":me})); } // If onwriteend callback if (typeof me.onwriteend === "function") { me.onwriteend(new ProgressEvent("writeend", {"target":me})); } }, "File", "truncate", [this.fileName, size]); }; module.exports = FileWriter; }); // file: lib/common/plugin/Flags.js define("cordova/plugin/Flags", function(require, exports, module) { /** * Supplies arguments to methods that lookup or create files and directories. * * @param create * {boolean} file or directory if it doesn't exist * @param exclusive * {boolean} used with create; if true the command will fail if * target path exists */ function Flags(create, exclusive) { this.create = create || false; this.exclusive = exclusive || false; } module.exports = Flags; }); // file: lib/common/plugin/GlobalizationError.js define("cordova/plugin/GlobalizationError", function(require, exports, module) { /** * Globalization error object * * @constructor * @param code * @param message */ var GlobalizationError = function(code, message) { this.code = code || null; this.message = message || ''; }; // Globalization error codes GlobalizationError.UNKNOWN_ERROR = 0; GlobalizationError.FORMATTING_ERROR = 1; GlobalizationError.PARSING_ERROR = 2; GlobalizationError.PATTERN_ERROR = 3; module.exports = GlobalizationError; }); // file: lib/common/plugin/InAppBrowser.js define("cordova/plugin/InAppBrowser", function(require, exports, module) { var exec = require('cordova/exec'); var channel = require('cordova/channel'); var modulemapper = require('cordova/modulemapper'); function InAppBrowser() { this.channels = { 'loadstart': channel.create('loadstart'), 'loadstop' : channel.create('loadstop'), 'loaderror' : channel.create('loaderror'), 'exit' : channel.create('exit') }; } InAppBrowser.prototype = { _eventHandler: function (event) { if (event.type in this.channels) { this.channels[event.type].fire(event); } }, close: function (eventname) { exec(null, null, "InAppBrowser", "close", []); }, addEventListener: function (eventname,f) { if (eventname in this.channels) { this.channels[eventname].subscribe(f); } }, removeEventListener: function(eventname, f) { if (eventname in this.channels) { this.channels[eventname].unsubscribe(f); } }, executeScript: function(injectDetails, cb) { if (injectDetails.code) { exec(cb, null, "InAppBrowser", "injectScriptCode", [injectDetails.code, !!cb]); } else if (injectDetails.file) { exec(cb, null, "InAppBrowser", "injectScriptFile", [injectDetails.file, !!cb]); } else { throw new Error('executeScript requires exactly one of code or file to be specified'); } }, insertCSS: function(injectDetails, cb) { if (injectDetails.code) { exec(cb, null, "InAppBrowser", "injectStyleCode", [injectDetails.code, !!cb]); } else if (injectDetails.file) { exec(cb, null, "InAppBrowser", "injectStyleFile", [injectDetails.file, !!cb]); } else { throw new Error('insertCSS requires exactly one of code or file to be specified'); } } }; module.exports = function(strUrl, strWindowName, strWindowFeatures) { var iab = new InAppBrowser(); var cb = function(eventname) { iab._eventHandler(eventname); }; // Don't catch calls that write to existing frames (e.g. named iframes). if (window.frames && window.frames[strWindowName]) { var origOpenFunc = modulemapper.getOriginalSymbol(window, 'open'); return origOpenFunc.apply(window, arguments); } exec(cb, cb, "InAppBrowser", "open", [strUrl, strWindowName, strWindowFeatures]); return iab; }; }); // file: lib/common/plugin/LocalFileSystem.js define("cordova/plugin/LocalFileSystem", function(require, exports, module) { var exec = require('cordova/exec'); /** * Represents a local file system. */ var LocalFileSystem = function() { }; LocalFileSystem.TEMPORARY = 0; //temporary, with no guarantee of persistence LocalFileSystem.PERSISTENT = 1; //persistent module.exports = LocalFileSystem; }); // file: lib/common/plugin/Media.js define("cordova/plugin/Media", function(require, exports, module) { var argscheck = require('cordova/argscheck'), utils = require('cordova/utils'), exec = require('cordova/exec'); var mediaObjects = {}; /** * This class provides access to the device media, interfaces to both sound and video * * @constructor * @param src The file name or url to play * @param successCallback The callback to be called when the file is done playing or recording. * successCallback() * @param errorCallback The callback to be called if there is an error. * errorCallback(int errorCode) - OPTIONAL * @param statusCallback The callback to be called when media status has changed. * statusCallback(int statusCode) - OPTIONAL */ var Media = function(src, successCallback, errorCallback, statusCallback) { argscheck.checkArgs('SFFF', 'Media', arguments); this.id = utils.createUUID(); mediaObjects[this.id] = this; this.src = src; this.successCallback = successCallback; this.errorCallback = errorCallback; this.statusCallback = statusCallback; this._duration = -1; this._position = -1; exec(null, this.errorCallback, "Media", "create", [this.id, this.src]); }; // Media messages Media.MEDIA_STATE = 1; Media.MEDIA_DURATION = 2; Media.MEDIA_POSITION = 3; Media.MEDIA_ERROR = 9; // Media states Media.MEDIA_NONE = 0; Media.MEDIA_STARTING = 1; Media.MEDIA_RUNNING = 2; Media.MEDIA_PAUSED = 3; Media.MEDIA_STOPPED = 4; Media.MEDIA_MSG = ["None", "Starting", "Running", "Paused", "Stopped"]; // "static" function to return existing objs. Media.get = function(id) { return mediaObjects[id]; }; /** * Start or resume playing audio file. */ Media.prototype.play = function(options) { exec(null, null, "Media", "startPlayingAudio", [this.id, this.src, options]); }; /** * Stop playing audio file. */ Media.prototype.stop = function() { var me = this; exec(function() { me._position = 0; }, this.errorCallback, "Media", "stopPlayingAudio", [this.id]); }; /** * Seek or jump to a new time in the track.. */ Media.prototype.seekTo = function(milliseconds) { var me = this; exec(function(p) { me._position = p; }, this.errorCallback, "Media", "seekToAudio", [this.id, milliseconds]); }; /** * Pause playing audio file. */ Media.prototype.pause = function() { exec(null, this.errorCallback, "Media", "pausePlayingAudio", [this.id]); }; /** * Get duration of an audio file. * The duration is only set for audio that is playing, paused or stopped. * * @return duration or -1 if not known. */ Media.prototype.getDuration = function() { return this._duration; }; /** * Get position of audio. */ Media.prototype.getCurrentPosition = function(success, fail) { var me = this; exec(function(p) { me._position = p; success(p); }, fail, "Media", "getCurrentPositionAudio", [this.id]); }; /** * Start recording audio file. */ Media.prototype.startRecord = function() { exec(null, this.errorCallback, "Media", "startRecordingAudio", [this.id, this.src]); }; /** * Stop recording audio file. */ Media.prototype.stopRecord = function() { exec(null, this.errorCallback, "Media", "stopRecordingAudio", [this.id]); }; /** * Release the resources. */ Media.prototype.release = function() { exec(null, this.errorCallback, "Media", "release", [this.id]); }; /** * Adjust the volume. */ Media.prototype.setVolume = function(volume) { exec(null, null, "Media", "setVolume", [this.id, volume]); }; /** * Audio has status update. * PRIVATE * * @param id The media object id (string) * @param msgType The 'type' of update this is * @param value Use of value is determined by the msgType */ Media.onStatus = function(id, msgType, value) { var media = mediaObjects[id]; if(media) { switch(msgType) { case Media.MEDIA_STATE : media.statusCallback && media.statusCallback(value); if(value == Media.MEDIA_STOPPED) { media.successCallback && media.successCallback(); } break; case Media.MEDIA_DURATION : media._duration = value; break; case Media.MEDIA_ERROR : media.errorCallback && media.errorCallback(value); break; case Media.MEDIA_POSITION : media._position = Number(value); break; default : console.error && console.error("Unhandled Media.onStatus :: " + msgType); break; } } else { console.error && console.error("Received Media.onStatus callback for unknown media :: " + id); } }; module.exports = Media; }); // file: lib/common/plugin/MediaError.js define("cordova/plugin/MediaError", function(require, exports, module) { /** * This class contains information about any Media errors. */ /* According to :: http://dev.w3.org/html5/spec-author-view/video.html#mediaerror We should never be creating these objects, we should just implement the interface which has 1 property for an instance, 'code' instead of doing : errorCallbackFunction( new MediaError(3,'msg') ); we should simply use a literal : errorCallbackFunction( {'code':3} ); */ var _MediaError = window.MediaError; if(!_MediaError) { window.MediaError = _MediaError = function(code, msg) { this.code = (typeof code != 'undefined') ? code : null; this.message = msg || ""; // message is NON-standard! do not use! }; } _MediaError.MEDIA_ERR_NONE_ACTIVE = _MediaError.MEDIA_ERR_NONE_ACTIVE || 0; _MediaError.MEDIA_ERR_ABORTED = _MediaError.MEDIA_ERR_ABORTED || 1; _MediaError.MEDIA_ERR_NETWORK = _MediaError.MEDIA_ERR_NETWORK || 2; _MediaError.MEDIA_ERR_DECODE = _MediaError.MEDIA_ERR_DECODE || 3; _MediaError.MEDIA_ERR_NONE_SUPPORTED = _MediaError.MEDIA_ERR_NONE_SUPPORTED || 4; // TODO: MediaError.MEDIA_ERR_NONE_SUPPORTED is legacy, the W3 spec now defines it as below. // as defined by http://dev.w3.org/html5/spec-author-view/video.html#error-codes _MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED = _MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED || 4; module.exports = _MediaError; }); // file: lib/common/plugin/MediaFile.js define("cordova/plugin/MediaFile", function(require, exports, module) { var utils = require('cordova/utils'), exec = require('cordova/exec'), File = require('cordova/plugin/File'), CaptureError = require('cordova/plugin/CaptureError'); /** * Represents a single file. * * name {DOMString} name of the file, without path information * fullPath {DOMString} the full path of the file, including the name * type {DOMString} mime type * lastModifiedDate {Date} last modified date * size {Number} size of the file in bytes */ var MediaFile = function(name, fullPath, type, lastModifiedDate, size){ MediaFile.__super__.constructor.apply(this, arguments); }; utils.extend(MediaFile, File); /** * Request capture format data for a specific file and type * * @param {Function} successCB * @param {Function} errorCB */ MediaFile.prototype.getFormatData = function(successCallback, errorCallback) { if (typeof this.fullPath === "undefined" || this.fullPath === null) { errorCallback(new CaptureError(CaptureError.CAPTURE_INVALID_ARGUMENT)); } else { exec(successCallback, errorCallback, "Capture", "getFormatData", [this.fullPath, this.type]); } }; module.exports = MediaFile; }); // file: lib/common/plugin/MediaFileData.js define("cordova/plugin/MediaFileData", function(require, exports, module) { /** * MediaFileData encapsulates format information of a media file. * * @param {DOMString} codecs * @param {long} bitrate * @param {long} height * @param {long} width * @param {float} duration */ var MediaFileData = function(codecs, bitrate, height, width, duration){ this.codecs = codecs || null; this.bitrate = bitrate || 0; this.height = height || 0; this.width = width || 0; this.duration = duration || 0; }; module.exports = MediaFileData; }); // file: lib/common/plugin/Metadata.js define("cordova/plugin/Metadata", function(require, exports, module) { /** * Information about the state of the file or directory * * {Date} modificationTime (readonly) */ var Metadata = function(time) { this.modificationTime = (typeof time != 'undefined'?new Date(time):null); }; module.exports = Metadata; }); // file: lib/common/plugin/Position.js define("cordova/plugin/Position", function(require, exports, module) { var Coordinates = require('cordova/plugin/Coordinates'); var Position = function(coords, timestamp) { if (coords) { this.coords = new Coordinates(coords.latitude, coords.longitude, coords.altitude, coords.accuracy, coords.heading, coords.velocity, coords.altitudeAccuracy); } else { this.coords = new Coordinates(); } this.timestamp = (timestamp !== undefined) ? timestamp : new Date(); }; module.exports = Position; }); // file: lib/common/plugin/PositionError.js define("cordova/plugin/PositionError", function(require, exports, module) { /** * Position error object * * @constructor * @param code * @param message */ var PositionError = function(code, message) { this.code = code || null; this.message = message || ''; }; PositionError.PERMISSION_DENIED = 1; PositionError.POSITION_UNAVAILABLE = 2; PositionError.TIMEOUT = 3; module.exports = PositionError; }); // file: lib/common/plugin/ProgressEvent.js define("cordova/plugin/ProgressEvent", function(require, exports, module) { // If ProgressEvent exists in global context, use it already, otherwise use our own polyfill // Feature test: See if we can instantiate a native ProgressEvent; // if so, use that approach, // otherwise fill-in with our own implementation. // // NOTE: right now we always fill in with our own. Down the road would be nice if we can use whatever is native in the webview. var ProgressEvent = (function() { /* var createEvent = function(data) { var event = document.createEvent('Events'); event.initEvent('ProgressEvent', false, false); if (data) { for (var i in data) { if (data.hasOwnProperty(i)) { event[i] = data[i]; } } if (data.target) { // TODO: cannot call <some_custom_object>.dispatchEvent // need to first figure out how to implement EventTarget } } return event; }; try { var ev = createEvent({type:"abort",target:document}); return function ProgressEvent(type, data) { data.type = type; return createEvent(data); }; } catch(e){ */ return function ProgressEvent(type, dict) { this.type = type; this.bubbles = false; this.cancelBubble = false; this.cancelable = false; this.lengthComputable = false; this.loaded = dict && dict.loaded ? dict.loaded : 0; this.total = dict && dict.total ? dict.total : 0; this.target = dict && dict.target ? dict.target : null; }; //} })(); module.exports = ProgressEvent; }); // file: lib/common/plugin/accelerometer.js define("cordova/plugin/accelerometer", function(require, exports, module) { /** * This class provides access to device accelerometer data. * @constructor */ var argscheck = require('cordova/argscheck'), utils = require("cordova/utils"), exec = require("cordova/exec"), Acceleration = require('cordova/plugin/Acceleration'); // Is the accel sensor running? var running = false; // Keeps reference to watchAcceleration calls. var timers = {}; // Array of listeners; used to keep track of when we should call start and stop. var listeners = []; // Last returned acceleration object from native var accel = null; // Tells native to start. function start() { exec(function(a) { var tempListeners = listeners.slice(0); accel = new Acceleration(a.x, a.y, a.z, a.timestamp); for (var i = 0, l = tempListeners.length; i < l; i++) { tempListeners[i].win(accel); } }, function(e) { var tempListeners = listeners.slice(0); for (var i = 0, l = tempListeners.length; i < l; i++) { tempListeners[i].fail(e); } }, "Accelerometer", "start", []); running = true; } // Tells native to stop. function stop() { exec(null, null, "Accelerometer", "stop", []); running = false; } // Adds a callback pair to the listeners array function createCallbackPair(win, fail) { return {win:win, fail:fail}; } // Removes a win/fail listener pair from the listeners array function removeListeners(l) { var idx = listeners.indexOf(l); if (idx > -1) { listeners.splice(idx, 1); if (listeners.length === 0) { stop(); } } } var accelerometer = { /** * Asynchronously acquires the current acceleration. * * @param {Function} successCallback The function to call when the acceleration data is available * @param {Function} errorCallback The function to call when there is an error getting the acceleration data. (OPTIONAL) * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL) */ getCurrentAcceleration: function(successCallback, errorCallback, options) { argscheck.checkArgs('fFO', 'accelerometer.getCurrentAcceleration', arguments); var p; var win = function(a) { removeListeners(p); successCallback(a); }; var fail = function(e) { removeListeners(p); errorCallback && errorCallback(e); }; p = createCallbackPair(win, fail); listeners.push(p); if (!running) { start(); } }, /** * Asynchronously acquires the acceleration repeatedly at a given interval. * * @param {Function} successCallback The function to call each time the acceleration data is available * @param {Function} errorCallback The function to call when there is an error getting the acceleration data. (OPTIONAL) * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL) * @return String The watch id that must be passed to #clearWatch to stop watching. */ watchAcceleration: function(successCallback, errorCallback, options) { argscheck.checkArgs('fFO', 'accelerometer.watchAcceleration', arguments); // Default interval (10 sec) var frequency = (options && options.frequency && typeof options.frequency == 'number') ? options.frequency : 10000; // Keep reference to watch id, and report accel readings as often as defined in frequency var id = utils.createUUID(); var p = createCallbackPair(function(){}, function(e) { removeListeners(p); errorCallback && errorCallback(e); }); listeners.push(p); timers[id] = { timer:window.setInterval(function() { if (accel) { successCallback(accel); } }, frequency), listeners:p }; if (running) { // If we're already running then immediately invoke the success callback // but only if we have retrieved a value, sample code does not check for null ... if (accel) { successCallback(accel); } } else { start(); } return id; }, /** * Clears the specified accelerometer watch. * * @param {String} id The id of the watch returned from #watchAcceleration. */ clearWatch: function(id) { // Stop javascript timer & remove from timer list if (id && timers[id]) { window.clearInterval(timers[id].timer); removeListeners(timers[id].listeners); delete timers[id]; } } }; module.exports = accelerometer; }); // file: lib/common/plugin/accelerometer/symbols.js define("cordova/plugin/accelerometer/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.defaults('cordova/plugin/Acceleration', 'Acceleration'); modulemapper.defaults('cordova/plugin/accelerometer', 'navigator.accelerometer'); }); // file: lib/common/plugin/battery.js define("cordova/plugin/battery", function(require, exports, module) { /** * This class contains information about the current battery status. * @constructor */ var cordova = require('cordova'), exec = require('cordova/exec'); function handlers() { return battery.channels.batterystatus.numHandlers + battery.channels.batterylow.numHandlers + battery.channels.batterycritical.numHandlers; } var Battery = function() { this._level = null; this._isPlugged = null; // Create new event handlers on the window (returns a channel instance) this.channels = { batterystatus:cordova.addWindowEventHandler("batterystatus"), batterylow:cordova.addWindowEventHandler("batterylow"), batterycritical:cordova.addWindowEventHandler("batterycritical") }; for (var key in this.channels) { this.channels[key].onHasSubscribersChange = Battery.onHasSubscribersChange; } }; /** * Event handlers for when callbacks get registered for the battery. * Keep track of how many handlers we have so we can start and stop the native battery listener * appropriately (and hopefully save on battery life!). */ Battery.onHasSubscribersChange = function() { // If we just registered the first handler, make sure native listener is started. if (this.numHandlers === 1 && handlers() === 1) { exec(battery._status, battery._error, "Battery", "start", []); } else if (handlers() === 0) { exec(null, null, "Battery", "stop", []); } }; /** * Callback for battery status * * @param {Object} info keys: level, isPlugged */ Battery.prototype._status = function(info) { if (info) { var me = battery; var level = info.level; if (me._level !== level || me._isPlugged !== info.isPlugged) { // Fire batterystatus event cordova.fireWindowEvent("batterystatus", info); // Fire low battery event if (level === 20 || level === 5) { if (level === 20) { cordova.fireWindowEvent("batterylow", info); } else { cordova.fireWindowEvent("batterycritical", info); } } } me._level = level; me._isPlugged = info.isPlugged; } }; /** * Error callback for battery start */ Battery.prototype._error = function(e) { console.log("Error initializing Battery: " + e); }; var battery = new Battery(); module.exports = battery; }); // file: lib/common/plugin/battery/symbols.js define("cordova/plugin/battery/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.defaults('cordova/plugin/battery', 'navigator.battery'); }); // file: lib/common/plugin/camera/symbols.js define("cordova/plugin/camera/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.defaults('cordova/plugin/Camera', 'navigator.camera'); modulemapper.defaults('cordova/plugin/CameraConstants', 'Camera'); modulemapper.defaults('cordova/plugin/CameraPopoverOptions', 'CameraPopoverOptions'); }); // file: lib/common/plugin/capture.js define("cordova/plugin/capture", function(require, exports, module) { var exec = require('cordova/exec'), MediaFile = require('cordova/plugin/MediaFile'); /** * Launches a capture of different types. * * @param (DOMString} type * @param {Function} successCB * @param {Function} errorCB * @param {CaptureVideoOptions} options */ function _capture(type, successCallback, errorCallback, options) { var win = function(pluginResult) { var mediaFiles = []; var i; for (i = 0; i < pluginResult.length; i++) { var mediaFile = new MediaFile(); mediaFile.name = pluginResult[i].name; mediaFile.fullPath = pluginResult[i].fullPath; mediaFile.type = pluginResult[i].type; mediaFile.lastModifiedDate = pluginResult[i].lastModifiedDate; mediaFile.size = pluginResult[i].size; mediaFiles.push(mediaFile); } successCallback(mediaFiles); }; exec(win, errorCallback, "Capture", type, [options]); } /** * The Capture interface exposes an interface to the camera and microphone of the hosting device. */ function Capture() { this.supportedAudioModes = []; this.supportedImageModes = []; this.supportedVideoModes = []; } /** * Launch audio recorder application for recording audio clip(s). * * @param {Function} successCB * @param {Function} errorCB * @param {CaptureAudioOptions} options */ Capture.prototype.captureAudio = function(successCallback, errorCallback, options){ _capture("captureAudio", successCallback, errorCallback, options); }; /** * Launch camera application for taking image(s). * * @param {Function} successCB * @param {Function} errorCB * @param {CaptureImageOptions} options */ Capture.prototype.captureImage = function(successCallback, errorCallback, options){ _capture("captureImage", successCallback, errorCallback, options); }; /** * Launch device camera application for recording video(s). * * @param {Function} successCB * @param {Function} errorCB * @param {CaptureVideoOptions} options */ Capture.prototype.captureVideo = function(successCallback, errorCallback, options){ _capture("captureVideo", successCallback, errorCallback, options); }; module.exports = new Capture(); }); // file: lib/common/plugin/capture/symbols.js define("cordova/plugin/capture/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/CaptureError', 'CaptureError'); modulemapper.clobbers('cordova/plugin/CaptureAudioOptions', 'CaptureAudioOptions'); modulemapper.clobbers('cordova/plugin/CaptureImageOptions', 'CaptureImageOptions'); modulemapper.clobbers('cordova/plugin/CaptureVideoOptions', 'CaptureVideoOptions'); modulemapper.clobbers('cordova/plugin/ConfigurationData', 'ConfigurationData'); modulemapper.clobbers('cordova/plugin/MediaFile', 'MediaFile'); modulemapper.clobbers('cordova/plugin/MediaFileData', 'MediaFileData'); modulemapper.clobbers('cordova/plugin/capture', 'navigator.device.capture'); }); // file: lib/common/plugin/compass.js define("cordova/plugin/compass", function(require, exports, module) { var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), utils = require('cordova/utils'), CompassHeading = require('cordova/plugin/CompassHeading'), CompassError = require('cordova/plugin/CompassError'), timers = {}, compass = { /** * Asynchronously acquires the current heading. * @param {Function} successCallback The function to call when the heading * data is available * @param {Function} errorCallback The function to call when there is an error * getting the heading data. * @param {CompassOptions} options The options for getting the heading data (not used). */ getCurrentHeading:function(successCallback, errorCallback, options) { argscheck.checkArgs('fFO', 'compass.getCurrentHeading', arguments); var win = function(result) { var ch = new CompassHeading(result.magneticHeading, result.trueHeading, result.headingAccuracy, result.timestamp); successCallback(ch); }; var fail = errorCallback && function(code) { var ce = new CompassError(code); errorCallback(ce); }; // Get heading exec(win, fail, "Compass", "getHeading", [options]); }, /** * Asynchronously acquires the heading repeatedly at a given interval. * @param {Function} successCallback The function to call each time the heading * data is available * @param {Function} errorCallback The function to call when there is an error * getting the heading data. * @param {HeadingOptions} options The options for getting the heading data * such as timeout and the frequency of the watch. For iOS, filter parameter * specifies to watch via a distance filter rather than time. */ watchHeading:function(successCallback, errorCallback, options) { argscheck.checkArgs('fFO', 'compass.watchHeading', arguments); // Default interval (100 msec) var frequency = (options !== undefined && options.frequency !== undefined) ? options.frequency : 100; var filter = (options !== undefined && options.filter !== undefined) ? options.filter : 0; var id = utils.createUUID(); if (filter > 0) { // is an iOS request for watch by filter, no timer needed timers[id] = "iOS"; compass.getCurrentHeading(successCallback, errorCallback, options); } else { // Start watch timer to get headings timers[id] = window.setInterval(function() { compass.getCurrentHeading(successCallback, errorCallback); }, frequency); } return id; }, /** * Clears the specified heading watch. * @param {String} watchId The ID of the watch returned from #watchHeading. */ clearWatch:function(id) { // Stop javascript timer & remove from timer list if (id && timers[id]) { if (timers[id] != "iOS") { clearInterval(timers[id]); } else { // is iOS watch by filter so call into device to stop exec(null, null, "Compass", "stopHeading", []); } delete timers[id]; } } }; module.exports = compass; }); // file: lib/common/plugin/compass/symbols.js define("cordova/plugin/compass/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/CompassHeading', 'CompassHeading'); modulemapper.clobbers('cordova/plugin/CompassError', 'CompassError'); modulemapper.clobbers('cordova/plugin/compass', 'navigator.compass'); }); // file: lib/common/plugin/console-via-logger.js define("cordova/plugin/console-via-logger", function(require, exports, module) { //------------------------------------------------------------------------------ var logger = require("cordova/plugin/logger"); var utils = require("cordova/utils"); //------------------------------------------------------------------------------ // object that we're exporting //------------------------------------------------------------------------------ var console = module.exports; //------------------------------------------------------------------------------ // copy of the original console object //------------------------------------------------------------------------------ var WinConsole = window.console; //------------------------------------------------------------------------------ // whether to use the logger //------------------------------------------------------------------------------ var UseLogger = false; //------------------------------------------------------------------------------ // Timers //------------------------------------------------------------------------------ var Timers = {}; //------------------------------------------------------------------------------ // used for unimplemented methods //------------------------------------------------------------------------------ function noop() {} //------------------------------------------------------------------------------ // used for unimplemented methods //------------------------------------------------------------------------------ console.useLogger = function (value) { if (arguments.length) UseLogger = !!value; if (UseLogger) { if (logger.useConsole()) { throw new Error("console and logger are too intertwingly"); } } return UseLogger; }; //------------------------------------------------------------------------------ console.log = function() { if (logger.useConsole()) return; logger.log.apply(logger, [].slice.call(arguments)); }; //------------------------------------------------------------------------------ console.error = function() { if (logger.useConsole()) return; logger.error.apply(logger, [].slice.call(arguments)); }; //------------------------------------------------------------------------------ console.warn = function() { if (logger.useConsole()) return; logger.warn.apply(logger, [].slice.call(arguments)); }; //------------------------------------------------------------------------------ console.info = function() { if (logger.useConsole()) return; logger.info.apply(logger, [].slice.call(arguments)); }; //------------------------------------------------------------------------------ console.debug = function() { if (logger.useConsole()) return; logger.debug.apply(logger, [].slice.call(arguments)); }; //------------------------------------------------------------------------------ console.assert = function(expression) { if (expression) return; var message = logger.format.apply(logger.format, [].slice.call(arguments, 1)); console.log("ASSERT: " + message); }; //------------------------------------------------------------------------------ console.clear = function() {}; //------------------------------------------------------------------------------ console.dir = function(object) { console.log("%o", object); }; //------------------------------------------------------------------------------ console.dirxml = function(node) { console.log(node.innerHTML); }; //------------------------------------------------------------------------------ console.trace = noop; //------------------------------------------------------------------------------ console.group = console.log; //------------------------------------------------------------------------------ console.groupCollapsed = console.log; //------------------------------------------------------------------------------ console.groupEnd = noop; //------------------------------------------------------------------------------ console.time = function(name) { Timers[name] = new Date().valueOf(); }; //------------------------------------------------------------------------------ console.timeEnd = function(name) { var timeStart = Timers[name]; if (!timeStart) { console.warn("unknown timer: " + name); return; } var timeElapsed = new Date().valueOf() - timeStart; console.log(name + ": " + timeElapsed + "ms"); }; //------------------------------------------------------------------------------ console.timeStamp = noop; //------------------------------------------------------------------------------ console.profile = noop; //------------------------------------------------------------------------------ console.profileEnd = noop; //------------------------------------------------------------------------------ console.count = noop; //------------------------------------------------------------------------------ console.exception = console.log; //------------------------------------------------------------------------------ console.table = function(data, columns) { console.log("%o", data); }; //------------------------------------------------------------------------------ // return a new function that calls both functions passed as args //------------------------------------------------------------------------------ function wrappedOrigCall(orgFunc, newFunc) { return function() { var args = [].slice.call(arguments); try { orgFunc.apply(WinConsole, args); } catch (e) {} try { newFunc.apply(console, args); } catch (e) {} }; } //------------------------------------------------------------------------------ // For every function that exists in the original console object, that // also exists in the new console object, wrap the new console method // with one that calls both //------------------------------------------------------------------------------ for (var key in console) { if (typeof WinConsole[key] == "function") { console[key] = wrappedOrigCall(WinConsole[key], console[key]); } } }); // file: lib/common/plugin/contacts.js define("cordova/plugin/contacts", function(require, exports, module) { var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), ContactError = require('cordova/plugin/ContactError'), utils = require('cordova/utils'), Contact = require('cordova/plugin/Contact'); /** * Represents a group of Contacts. * @constructor */ var contacts = { /** * Returns an array of Contacts matching the search criteria. * @param fields that should be searched * @param successCB success callback * @param errorCB error callback * @param {ContactFindOptions} options that can be applied to contact searching * @return array of Contacts matching search criteria */ find:function(fields, successCB, errorCB, options) { argscheck.checkArgs('afFO', 'contacts.find', arguments); if (!fields.length) { errorCB && errorCB(new ContactError(ContactError.INVALID_ARGUMENT_ERROR)); } else { var win = function(result) { var cs = []; for (var i = 0, l = result.length; i < l; i++) { cs.push(contacts.create(result[i])); } successCB(cs); }; exec(win, errorCB, "Contacts", "search", [fields, options]); } }, /** * This function creates a new contact, but it does not persist the contact * to device storage. To persist the contact to device storage, invoke * contact.save(). * @param properties an object whose properties will be examined to create a new Contact * @returns new Contact object */ create:function(properties) { argscheck.checkArgs('O', 'contacts.create', arguments); var contact = new Contact(); for (var i in properties) { if (typeof contact[i] !== 'undefined' && properties.hasOwnProperty(i)) { contact[i] = properties[i]; } } return contact; } }; module.exports = contacts; }); // file: lib/common/plugin/contacts/symbols.js define("cordova/plugin/contacts/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/contacts', 'navigator.contacts'); modulemapper.clobbers('cordova/plugin/Contact', 'Contact'); modulemapper.clobbers('cordova/plugin/ContactAddress', 'ContactAddress'); modulemapper.clobbers('cordova/plugin/ContactError', 'ContactError'); modulemapper.clobbers('cordova/plugin/ContactField', 'ContactField'); modulemapper.clobbers('cordova/plugin/ContactFindOptions', 'ContactFindOptions'); modulemapper.clobbers('cordova/plugin/ContactName', 'ContactName'); modulemapper.clobbers('cordova/plugin/ContactOrganization', 'ContactOrganization'); }); // file: lib/common/plugin/device.js define("cordova/plugin/device", function(require, exports, module) { var argscheck = require('cordova/argscheck'), channel = require('cordova/channel'), utils = require('cordova/utils'), exec = require('cordova/exec'); // Tell cordova channel to wait on the CordovaInfoReady event channel.waitForInitialization('onCordovaInfoReady'); /** * This represents the mobile device, and provides properties for inspecting the model, version, UUID of the * phone, etc. * @constructor */ function Device() { this.available = false; this.platform = null; this.version = null; this.name = null; this.uuid = null; this.cordova = null; this.model = null; var me = this; channel.onCordovaReady.subscribe(function() { me.getInfo(function(info) { me.available = true; me.platform = info.platform; me.version = info.version; me.name = info.name; me.uuid = info.uuid; me.cordova = info.cordova; me.model = info.model; channel.onCordovaInfoReady.fire(); },function(e) { me.available = false; utils.alert("[ERROR] Error initializing Cordova: " + e); }); }); } /** * Get device info * * @param {Function} successCallback The function to call when the heading data is available * @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL) */ Device.prototype.getInfo = function(successCallback, errorCallback) { argscheck.checkArgs('fF', 'Device.getInfo', arguments); exec(successCallback, errorCallback, "Device", "getDeviceInfo", []); }; module.exports = new Device(); }); // file: lib/common/plugin/device/symbols.js define("cordova/plugin/device/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/device', 'device'); }); // file: lib/common/plugin/echo.js define("cordova/plugin/echo", function(require, exports, module) { var exec = require('cordova/exec'), utils = require('cordova/utils'); /** * Sends the given message through exec() to the Echo plugin, which sends it back to the successCallback. * @param successCallback invoked with a FileSystem object * @param errorCallback invoked if error occurs retrieving file system * @param message The string to be echoed. * @param forceAsync Whether to force an async return value (for testing native->js bridge). */ module.exports = function(successCallback, errorCallback, message, forceAsync) { var action = 'echo'; var messageIsMultipart = (utils.typeName(message) == "Array"); var args = messageIsMultipart ? message : [message]; if (utils.typeName(message) == 'ArrayBuffer') { if (forceAsync) { console.warn('Cannot echo ArrayBuffer with forced async, falling back to sync.'); } action += 'ArrayBuffer'; } else if (messageIsMultipart) { if (forceAsync) { console.warn('Cannot echo MultiPart Array with forced async, falling back to sync.'); } action += 'MultiPart'; } else if (forceAsync) { action += 'Async'; } exec(successCallback, errorCallback, "Echo", action, args); }; }); // file: lib/ios/plugin/file/symbols.js define("cordova/plugin/file/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'), symbolshelper = require('cordova/plugin/file/symbolshelper'); symbolshelper(modulemapper.clobbers); modulemapper.merges('cordova/plugin/ios/Entry', 'Entry'); }); // file: lib/common/plugin/file/symbolshelper.js define("cordova/plugin/file/symbolshelper", function(require, exports, module) { module.exports = function(exportFunc) { exportFunc('cordova/plugin/DirectoryEntry', 'DirectoryEntry'); exportFunc('cordova/plugin/DirectoryReader', 'DirectoryReader'); exportFunc('cordova/plugin/Entry', 'Entry'); exportFunc('cordova/plugin/File', 'File'); exportFunc('cordova/plugin/FileEntry', 'FileEntry'); exportFunc('cordova/plugin/FileError', 'FileError'); exportFunc('cordova/plugin/FileReader', 'FileReader'); exportFunc('cordova/plugin/FileSystem', 'FileSystem'); exportFunc('cordova/plugin/FileUploadOptions', 'FileUploadOptions'); exportFunc('cordova/plugin/FileUploadResult', 'FileUploadResult'); exportFunc('cordova/plugin/FileWriter', 'FileWriter'); exportFunc('cordova/plugin/Flags', 'Flags'); exportFunc('cordova/plugin/LocalFileSystem', 'LocalFileSystem'); exportFunc('cordova/plugin/Metadata', 'Metadata'); exportFunc('cordova/plugin/ProgressEvent', 'ProgressEvent'); exportFunc('cordova/plugin/requestFileSystem', 'requestFileSystem'); exportFunc('cordova/plugin/resolveLocalFileSystemURI', 'resolveLocalFileSystemURI'); }; }); // file: lib/common/plugin/filetransfer/symbols.js define("cordova/plugin/filetransfer/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/FileTransfer', 'FileTransfer'); modulemapper.clobbers('cordova/plugin/FileTransferError', 'FileTransferError'); }); // file: lib/common/plugin/geolocation.js define("cordova/plugin/geolocation", function(require, exports, module) { var argscheck = require('cordova/argscheck'), utils = require('cordova/utils'), exec = require('cordova/exec'), PositionError = require('cordova/plugin/PositionError'), Position = require('cordova/plugin/Position'); var timers = {}; // list of timers in use // Returns default params, overrides if provided with values function parseParameters(options) { var opt = { maximumAge: 0, enableHighAccuracy: false, timeout: Infinity }; if (options) { if (options.maximumAge !== undefined && !isNaN(options.maximumAge) && options.maximumAge > 0) { opt.maximumAge = options.maximumAge; } if (options.enableHighAccuracy !== undefined) { opt.enableHighAccuracy = options.enableHighAccuracy; } if (options.timeout !== undefined && !isNaN(options.timeout)) { if (options.timeout < 0) { opt.timeout = 0; } else { opt.timeout = options.timeout; } } } return opt; } // Returns a timeout failure, closed over a specified timeout value and error callback. function createTimeout(errorCallback, timeout) { var t = setTimeout(function() { clearTimeout(t); t = null; errorCallback({ code:PositionError.TIMEOUT, message:"Position retrieval timed out." }); }, timeout); return t; } var geolocation = { lastPosition:null, // reference to last known (cached) position returned /** * Asynchronously acquires the current position. * * @param {Function} successCallback The function to call when the position data is available * @param {Function} errorCallback The function to call when there is an error getting the heading position. (OPTIONAL) * @param {PositionOptions} options The options for getting the position data. (OPTIONAL) */ getCurrentPosition:function(successCallback, errorCallback, options) { argscheck.checkArgs('fFO', 'geolocation.getCurrentPosition', arguments); options = parseParameters(options); // Timer var that will fire an error callback if no position is retrieved from native // before the "timeout" param provided expires var timeoutTimer = {timer:null}; var win = function(p) { clearTimeout(timeoutTimer.timer); if (!(timeoutTimer.timer)) { // Timeout already happened, or native fired error callback for // this geo request. // Don't continue with success callback. return; } var pos = new Position( { latitude:p.latitude, longitude:p.longitude, altitude:p.altitude, accuracy:p.accuracy, heading:p.heading, velocity:p.velocity, altitudeAccuracy:p.altitudeAccuracy }, (p.timestamp === undefined ? new Date() : ((p.timestamp instanceof Date) ? p.timestamp : new Date(p.timestamp))) ); geolocation.lastPosition = pos; successCallback(pos); }; var fail = function(e) { clearTimeout(timeoutTimer.timer); timeoutTimer.timer = null; var err = new PositionError(e.code, e.message); if (errorCallback) { errorCallback(err); } }; // Check our cached position, if its timestamp difference with current time is less than the maximumAge, then just // fire the success callback with the cached position. if (geolocation.lastPosition && options.maximumAge && (((new Date()).getTime() - geolocation.lastPosition.timestamp.getTime()) <= options.maximumAge)) { successCallback(geolocation.lastPosition); // If the cached position check failed and the timeout was set to 0, error out with a TIMEOUT error object. } else if (options.timeout === 0) { fail({ code:PositionError.TIMEOUT, message:"timeout value in PositionOptions set to 0 and no cached Position object available, or cached Position object's age exceeds provided PositionOptions' maximumAge parameter." }); // Otherwise we have to call into native to retrieve a position. } else { if (options.timeout !== Infinity) { // If the timeout value was not set to Infinity (default), then // set up a timeout function that will fire the error callback // if no successful position was retrieved before timeout expired. timeoutTimer.timer = createTimeout(fail, options.timeout); } else { // This is here so the check in the win function doesn't mess stuff up // may seem weird but this guarantees timeoutTimer is // always truthy before we call into native timeoutTimer.timer = true; } exec(win, fail, "Geolocation", "getLocation", [options.enableHighAccuracy, options.maximumAge]); } return timeoutTimer; }, /** * Asynchronously watches the geolocation for changes to geolocation. When a change occurs, * the successCallback is called with the new location. * * @param {Function} successCallback The function to call each time the location data is available * @param {Function} errorCallback The function to call when there is an error getting the location data. (OPTIONAL) * @param {PositionOptions} options The options for getting the location data such as frequency. (OPTIONAL) * @return String The watch id that must be passed to #clearWatch to stop watching. */ watchPosition:function(successCallback, errorCallback, options) { argscheck.checkArgs('fFO', 'geolocation.getCurrentPosition', arguments); options = parseParameters(options); var id = utils.createUUID(); // Tell device to get a position ASAP, and also retrieve a reference to the timeout timer generated in getCurrentPosition timers[id] = geolocation.getCurrentPosition(successCallback, errorCallback, options); var fail = function(e) { clearTimeout(timers[id].timer); var err = new PositionError(e.code, e.message); if (errorCallback) { errorCallback(err); } }; var win = function(p) { clearTimeout(timers[id].timer); if (options.timeout !== Infinity) { timers[id].timer = createTimeout(fail, options.timeout); } var pos = new Position( { latitude:p.latitude, longitude:p.longitude, altitude:p.altitude, accuracy:p.accuracy, heading:p.heading, velocity:p.velocity, altitudeAccuracy:p.altitudeAccuracy }, (p.timestamp === undefined ? new Date() : ((p.timestamp instanceof Date) ? p.timestamp : new Date(p.timestamp))) ); geolocation.lastPosition = pos; successCallback(pos); }; exec(win, fail, "Geolocation", "addWatch", [id, options.enableHighAccuracy]); return id; }, /** * Clears the specified heading watch. * * @param {String} id The ID of the watch returned from #watchPosition */ clearWatch:function(id) { if (id && timers[id] !== undefined) { clearTimeout(timers[id].timer); timers[id].timer = false; exec(null, null, "Geolocation", "clearWatch", [id]); } } }; module.exports = geolocation; }); // file: lib/common/plugin/geolocation/symbols.js define("cordova/plugin/geolocation/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.defaults('cordova/plugin/geolocation', 'navigator.geolocation'); modulemapper.clobbers('cordova/plugin/PositionError', 'PositionError'); modulemapper.clobbers('cordova/plugin/Position', 'Position'); modulemapper.clobbers('cordova/plugin/Coordinates', 'Coordinates'); }); // file: lib/common/plugin/globalization.js define("cordova/plugin/globalization", function(require, exports, module) { var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), GlobalizationError = require('cordova/plugin/GlobalizationError'); var globalization = { /** * Returns the string identifier for the client's current language. * It returns the language identifier string to the successCB callback with a * properties object as a parameter. If there is an error getting the language, * then the errorCB callback is invoked. * * @param {Function} successCB * @param {Function} errorCB * * @return Object.value {String}: The language identifier * * @error GlobalizationError.UNKNOWN_ERROR * * Example * globalization.getPreferredLanguage(function (language) {alert('language:' + language.value + '\n');}, * function () {}); */ getPreferredLanguage:function(successCB, failureCB) { argscheck.checkArgs('fF', 'Globalization.getPreferredLanguage', arguments); exec(successCB, failureCB, "Globalization","getPreferredLanguage", []); }, /** * Returns the string identifier for the client's current locale setting. * It returns the locale identifier string to the successCB callback with a * properties object as a parameter. If there is an error getting the locale, * then the errorCB callback is invoked. * * @param {Function} successCB * @param {Function} errorCB * * @return Object.value {String}: The locale identifier * * @error GlobalizationError.UNKNOWN_ERROR * * Example * globalization.getLocaleName(function (locale) {alert('locale:' + locale.value + '\n');}, * function () {}); */ getLocaleName:function(successCB, failureCB) { argscheck.checkArgs('fF', 'Globalization.getLocaleName', arguments); exec(successCB, failureCB, "Globalization","getLocaleName", []); }, /** * Returns a date formatted as a string according to the client's user preferences and * calendar using the time zone of the client. It returns the formatted date string to the * successCB callback with a properties object as a parameter. If there is an error * formatting the date, then the errorCB callback is invoked. * * The defaults are: formatLenght="short" and selector="date and time" * * @param {Date} date * @param {Function} successCB * @param {Function} errorCB * @param {Object} options {optional} * formatLength {String}: 'short', 'medium', 'long', or 'full' * selector {String}: 'date', 'time', or 'date and time' * * @return Object.value {String}: The localized date string * * @error GlobalizationError.FORMATTING_ERROR * * Example * globalization.dateToString(new Date(), * function (date) {alert('date:' + date.value + '\n');}, * function (errorCode) {alert(errorCode);}, * {formatLength:'short'}); */ dateToString:function(date, successCB, failureCB, options) { argscheck.checkArgs('dfFO', 'Globalization.dateToString', arguments); var dateValue = date.valueOf(); exec(successCB, failureCB, "Globalization", "dateToString", [{"date": dateValue, "options": options}]); }, /** * Parses a date formatted as a string according to the client's user * preferences and calendar using the time zone of the client and returns * the corresponding date object. It returns the date to the successCB * callback with a properties object as a parameter. If there is an error * parsing the date string, then the errorCB callback is invoked. * * The defaults are: formatLength="short" and selector="date and time" * * @param {String} dateString * @param {Function} successCB * @param {Function} errorCB * @param {Object} options {optional} * formatLength {String}: 'short', 'medium', 'long', or 'full' * selector {String}: 'date', 'time', or 'date and time' * * @return Object.year {Number}: The four digit year * Object.month {Number}: The month from (0 - 11) * Object.day {Number}: The day from (1 - 31) * Object.hour {Number}: The hour from (0 - 23) * Object.minute {Number}: The minute from (0 - 59) * Object.second {Number}: The second from (0 - 59) * Object.millisecond {Number}: The milliseconds (from 0 - 999), * not available on all platforms * * @error GlobalizationError.PARSING_ERROR * * Example * globalization.stringToDate('4/11/2011', * function (date) { alert('Month:' + date.month + '\n' + * 'Day:' + date.day + '\n' + * 'Year:' + date.year + '\n');}, * function (errorCode) {alert(errorCode);}, * {selector:'date'}); */ stringToDate:function(dateString, successCB, failureCB, options) { argscheck.checkArgs('sfFO', 'Globalization.stringToDate', arguments); exec(successCB, failureCB, "Globalization", "stringToDate", [{"dateString": dateString, "options": options}]); }, /** * Returns a pattern string for formatting and parsing dates according to the client's * user preferences. It returns the pattern to the successCB callback with a * properties object as a parameter. If there is an error obtaining the pattern, * then the errorCB callback is invoked. * * The defaults are: formatLength="short" and selector="date and time" * * @param {Function} successCB * @param {Function} errorCB * @param {Object} options {optional} * formatLength {String}: 'short', 'medium', 'long', or 'full' * selector {String}: 'date', 'time', or 'date and time' * * @return Object.pattern {String}: The date and time pattern for formatting and parsing dates. * The patterns follow Unicode Technical Standard #35 * http://unicode.org/reports/tr35/tr35-4.html * Object.timezone {String}: The abbreviated name of the time zone on the client * Object.utc_offset {Number}: The current difference in seconds between the client's * time zone and coordinated universal time. * Object.dst_offset {Number}: The current daylight saving time offset in seconds * between the client's non-daylight saving's time zone * and the client's daylight saving's time zone. * * @error GlobalizationError.PATTERN_ERROR * * Example * globalization.getDatePattern( * function (date) {alert('pattern:' + date.pattern + '\n');}, * function () {}, * {formatLength:'short'}); */ getDatePattern:function(successCB, failureCB, options) { argscheck.checkArgs('fFO', 'Globalization.getDatePattern', arguments); exec(successCB, failureCB, "Globalization", "getDatePattern", [{"options": options}]); }, /** * Returns an array of either the names of the months or days of the week * according to the client's user preferences and calendar. It returns the array of names to the * successCB callback with a properties object as a parameter. If there is an error obtaining the * names, then the errorCB callback is invoked. * * The defaults are: type="wide" and item="months" * * @param {Function} successCB * @param {Function} errorCB * @param {Object} options {optional} * type {String}: 'narrow' or 'wide' * item {String}: 'months', or 'days' * * @return Object.value {Array{String}}: The array of names starting from either * the first month in the year or the * first day of the week. * @error GlobalizationError.UNKNOWN_ERROR * * Example * globalization.getDateNames(function (names) { * for(var i = 0; i < names.value.length; i++) { * alert('Month:' + names.value[i] + '\n');}}, * function () {}); */ getDateNames:function(successCB, failureCB, options) { argscheck.checkArgs('fFO', 'Globalization.getDateNames', arguments); exec(successCB, failureCB, "Globalization", "getDateNames", [{"options": options}]); }, /** * Returns whether daylight savings time is in effect for a given date using the client's * time zone and calendar. It returns whether or not daylight savings time is in effect * to the successCB callback with a properties object as a parameter. If there is an error * reading the date, then the errorCB callback is invoked. * * @param {Date} date * @param {Function} successCB * @param {Function} errorCB * * @return Object.dst {Boolean}: The value "true" indicates that daylight savings time is * in effect for the given date and "false" indicate that it is not. * * @error GlobalizationError.UNKNOWN_ERROR * * Example * globalization.isDayLightSavingsTime(new Date(), * function (date) {alert('dst:' + date.dst + '\n');} * function () {}); */ isDayLightSavingsTime:function(date, successCB, failureCB) { argscheck.checkArgs('dfF', 'Globalization.isDayLightSavingsTime', arguments); var dateValue = date.valueOf(); exec(successCB, failureCB, "Globalization", "isDayLightSavingsTime", [{"date": dateValue}]); }, /** * Returns the first day of the week according to the client's user preferences and calendar. * The days of the week are numbered starting from 1 where 1 is considered to be Sunday. * It returns the day to the successCB callback with a properties object as a parameter. * If there is an error obtaining the pattern, then the errorCB callback is invoked. * * @param {Function} successCB * @param {Function} errorCB * * @return Object.value {Number}: The number of the first day of the week. * * @error GlobalizationError.UNKNOWN_ERROR * * Example * globalization.getFirstDayOfWeek(function (day) * { alert('Day:' + day.value + '\n');}, * function () {}); */ getFirstDayOfWeek:function(successCB, failureCB) { argscheck.checkArgs('fF', 'Globalization.getFirstDayOfWeek', arguments); exec(successCB, failureCB, "Globalization", "getFirstDayOfWeek", []); }, /** * Returns a number formatted as a string according to the client's user preferences. * It returns the formatted number string to the successCB callback with a properties object as a * parameter. If there is an error formatting the number, then the errorCB callback is invoked. * * The defaults are: type="decimal" * * @param {Number} number * @param {Function} successCB * @param {Function} errorCB * @param {Object} options {optional} * type {String}: 'decimal', "percent", or 'currency' * * @return Object.value {String}: The formatted number string. * * @error GlobalizationError.FORMATTING_ERROR * * Example * globalization.numberToString(3.25, * function (number) {alert('number:' + number.value + '\n');}, * function () {}, * {type:'decimal'}); */ numberToString:function(number, successCB, failureCB, options) { argscheck.checkArgs('nfFO', 'Globalization.numberToString', arguments); exec(successCB, failureCB, "Globalization", "numberToString", [{"number": number, "options": options}]); }, /** * Parses a number formatted as a string according to the client's user preferences and * returns the corresponding number. It returns the number to the successCB callback with a * properties object as a parameter. If there is an error parsing the number string, then * the errorCB callback is invoked. * * The defaults are: type="decimal" * * @param {String} numberString * @param {Function} successCB * @param {Function} errorCB * @param {Object} options {optional} * type {String}: 'decimal', "percent", or 'currency' * * @return Object.value {Number}: The parsed number. * * @error GlobalizationError.PARSING_ERROR * * Example * globalization.stringToNumber('1234.56', * function (number) {alert('Number:' + number.value + '\n');}, * function () { alert('Error parsing number');}); */ stringToNumber:function(numberString, successCB, failureCB, options) { argscheck.checkArgs('sfFO', 'Globalization.stringToNumber', arguments); exec(successCB, failureCB, "Globalization", "stringToNumber", [{"numberString": numberString, "options": options}]); }, /** * Returns a pattern string for formatting and parsing numbers according to the client's user * preferences. It returns the pattern to the successCB callback with a properties object as a * parameter. If there is an error obtaining the pattern, then the errorCB callback is invoked. * * The defaults are: type="decimal" * * @param {Function} successCB * @param {Function} errorCB * @param {Object} options {optional} * type {String}: 'decimal', "percent", or 'currency' * * @return Object.pattern {String}: The number pattern for formatting and parsing numbers. * The patterns follow Unicode Technical Standard #35. * http://unicode.org/reports/tr35/tr35-4.html * Object.symbol {String}: The symbol to be used when formatting and parsing * e.g., percent or currency symbol. * Object.fraction {Number}: The number of fractional digits to use when parsing and * formatting numbers. * Object.rounding {Number}: The rounding increment to use when parsing and formatting. * Object.positive {String}: The symbol to use for positive numbers when parsing and formatting. * Object.negative: {String}: The symbol to use for negative numbers when parsing and formatting. * Object.decimal: {String}: The decimal symbol to use for parsing and formatting. * Object.grouping: {String}: The grouping symbol to use for parsing and formatting. * * @error GlobalizationError.PATTERN_ERROR * * Example * globalization.getNumberPattern( * function (pattern) {alert('Pattern:' + pattern.pattern + '\n');}, * function () {}); */ getNumberPattern:function(successCB, failureCB, options) { argscheck.checkArgs('fFO', 'Globalization.getNumberPattern', arguments); exec(successCB, failureCB, "Globalization", "getNumberPattern", [{"options": options}]); }, /** * Returns a pattern string for formatting and parsing currency values according to the client's * user preferences and ISO 4217 currency code. It returns the pattern to the successCB callback with a * properties object as a parameter. If there is an error obtaining the pattern, then the errorCB * callback is invoked. * * @param {String} currencyCode * @param {Function} successCB * @param {Function} errorCB * * @return Object.pattern {String}: The currency pattern for formatting and parsing currency values. * The patterns follow Unicode Technical Standard #35 * http://unicode.org/reports/tr35/tr35-4.html * Object.code {String}: The ISO 4217 currency code for the pattern. * Object.fraction {Number}: The number of fractional digits to use when parsing and * formatting currency. * Object.rounding {Number}: The rounding increment to use when parsing and formatting. * Object.decimal: {String}: The decimal symbol to use for parsing and formatting. * Object.grouping: {String}: The grouping symbol to use for parsing and formatting. * * @error GlobalizationError.FORMATTING_ERROR * * Example * globalization.getCurrencyPattern('EUR', * function (currency) {alert('Pattern:' + currency.pattern + '\n');} * function () {}); */ getCurrencyPattern:function(currencyCode, successCB, failureCB) { argscheck.checkArgs('sfF', 'Globalization.getCurrencyPattern', arguments); exec(successCB, failureCB, "Globalization", "getCurrencyPattern", [{"currencyCode": currencyCode}]); } }; module.exports = globalization; }); // file: lib/common/plugin/globalization/symbols.js define("cordova/plugin/globalization/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/globalization', 'navigator.globalization'); modulemapper.clobbers('cordova/plugin/GlobalizationError', 'GlobalizationError'); }); // file: lib/ios/plugin/inappbrowser/symbols.js define("cordova/plugin/inappbrowser/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/InAppBrowser', 'open'); }); // file: lib/ios/plugin/ios/Contact.js define("cordova/plugin/ios/Contact", function(require, exports, module) { var exec = require('cordova/exec'), ContactError = require('cordova/plugin/ContactError'); /** * Provides iOS Contact.display API. */ module.exports = { display : function(errorCB, options) { /* * Display a contact using the iOS Contact Picker UI * NOT part of W3C spec so no official documentation * * @param errorCB error callback * @param options object * allowsEditing: boolean AS STRING * "true" to allow editing the contact * "false" (default) display contact */ if (this.id === null) { if (typeof errorCB === "function") { var errorObj = new ContactError(ContactError.UNKNOWN_ERROR); errorCB(errorObj); } } else { exec(null, errorCB, "Contacts","displayContact", [this.id, options]); } } }; }); // file: lib/ios/plugin/ios/Entry.js define("cordova/plugin/ios/Entry", function(require, exports, module) { module.exports = { toURL:function() { // TODO: refactor path in a cross-platform way so we can eliminate // these kinds of platform-specific hacks. return "file://localhost" + this.fullPath; }, toURI: function() { console.log("DEPRECATED: Update your code to use 'toURL'"); return "file://localhost" + this.fullPath; } }; }); // file: lib/ios/plugin/ios/contacts.js define("cordova/plugin/ios/contacts", function(require, exports, module) { var exec = require('cordova/exec'); /** * Provides iOS enhanced contacts API. */ module.exports = { newContactUI : function(successCallback) { /* * Create a contact using the iOS Contact Picker UI * NOT part of W3C spec so no official documentation * * returns: the id of the created contact as param to successCallback */ exec(successCallback, null, "Contacts","newContact", []); }, chooseContact : function(successCallback, options) { /* * Select a contact using the iOS Contact Picker UI * NOT part of W3C spec so no official documentation * * @param errorCB error callback * @param options object * allowsEditing: boolean AS STRING * "true" to allow editing the contact * "false" (default) display contact * fields: array of fields to return in contact object (see ContactOptions.fields) * * @returns * id of contact selected * ContactObject * if no fields provided contact contains just id information * if fields provided contact object contains information for the specified fields * */ var win = function(result) { var fullContact = require('cordova/plugin/contacts').create(result); successCallback(fullContact.id, fullContact); }; exec(win, null, "Contacts","chooseContact", [options]); } }; }); // file: lib/ios/plugin/ios/contacts/symbols.js define("cordova/plugin/ios/contacts/symbols", function(require, exports, module) { require('cordova/plugin/contacts/symbols'); var modulemapper = require('cordova/modulemapper'); modulemapper.merges('cordova/plugin/ios/contacts', 'navigator.contacts'); modulemapper.merges('cordova/plugin/ios/Contact', 'Contact'); }); // file: lib/ios/plugin/ios/geolocation/symbols.js define("cordova/plugin/ios/geolocation/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.merges('cordova/plugin/geolocation', 'navigator.geolocation'); }); // file: lib/ios/plugin/ios/logger/plugininit.js define("cordova/plugin/ios/logger/plugininit", function(require, exports, module) { // use the native logger var logger = require("cordova/plugin/logger"); logger.useConsole(false); }); // file: lib/ios/plugin/ios/logger/symbols.js define("cordova/plugin/ios/logger/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/logger', 'console'); }); // file: lib/ios/plugin/ios/notification.js define("cordova/plugin/ios/notification", function(require, exports, module) { var Media = require('cordova/plugin/Media'); module.exports = { beep:function(count) { (new Media('beep.wav')).play(); } }; }); // file: lib/common/plugin/logger.js define("cordova/plugin/logger", function(require, exports, module) { //------------------------------------------------------------------------------ // The logger module exports the following properties/functions: // // LOG - constant for the level LOG // ERROR - constant for the level ERROR // WARN - constant for the level WARN // INFO - constant for the level INFO // DEBUG - constant for the level DEBUG // logLevel() - returns current log level // logLevel(value) - sets and returns a new log level // useConsole() - returns whether logger is using console // useConsole(value) - sets and returns whether logger is using console // log(message,...) - logs a message at level LOG // error(message,...) - logs a message at level ERROR // warn(message,...) - logs a message at level WARN // info(message,...) - logs a message at level INFO // debug(message,...) - logs a message at level DEBUG // logLevel(level,message,...) - logs a message specified level // //------------------------------------------------------------------------------ var logger = exports; var exec = require('cordova/exec'); var utils = require('cordova/utils'); var UseConsole = true; var Queued = []; var DeviceReady = false; var CurrentLevel; /** * Logging levels */ var Levels = [ "LOG", "ERROR", "WARN", "INFO", "DEBUG" ]; /* * add the logging levels to the logger object and * to a separate levelsMap object for testing */ var LevelsMap = {}; for (var i=0; i<Levels.length; i++) { var level = Levels[i]; LevelsMap[level] = i; logger[level] = level; } CurrentLevel = LevelsMap.WARN; /** * Getter/Setter for the logging level * * Returns the current logging level. * * When a value is passed, sets the logging level to that value. * The values should be one of the following constants: * logger.LOG * logger.ERROR * logger.WARN * logger.INFO * logger.DEBUG * * The value used determines which messages get printed. The logging * values above are in order, and only messages logged at the logging * level or above will actually be displayed to the user. E.g., the * default level is WARN, so only messages logged with LOG, ERROR, or * WARN will be displayed; INFO and DEBUG messages will be ignored. */ logger.level = function (value) { if (arguments.length) { if (LevelsMap[value] === null) { throw new Error("invalid logging level: " + value); } CurrentLevel = LevelsMap[value]; } return Levels[CurrentLevel]; }; /** * Getter/Setter for the useConsole functionality * * When useConsole is true, the logger will log via the * browser 'console' object. Otherwise, it will use the * native Logger plugin. */ logger.useConsole = function (value) { if (arguments.length) UseConsole = !!value; if (UseConsole) { if (typeof console == "undefined") { throw new Error("global console object is not defined"); } if (typeof console.log != "function") { throw new Error("global console object does not have a log function"); } if (typeof console.useLogger == "function") { if (console.useLogger()) { throw new Error("console and logger are too intertwingly"); } } } return UseConsole; }; /** * Logs a message at the LOG level. * * Parameters passed after message are used applied to * the message with utils.format() */ logger.log = function(message) { logWithArgs("LOG", arguments); }; /** * Logs a message at the ERROR level. * * Parameters passed after message are used applied to * the message with utils.format() */ logger.error = function(message) { logWithArgs("ERROR", arguments); }; /** * Logs a message at the WARN level. * * Parameters passed after message are used applied to * the message with utils.format() */ logger.warn = function(message) { logWithArgs("WARN", arguments); }; /** * Logs a message at the INFO level. * * Parameters passed after message are used applied to * the message with utils.format() */ logger.info = function(message) { logWithArgs("INFO", arguments); }; /** * Logs a message at the DEBUG level. * * Parameters passed after message are used applied to * the message with utils.format() */ logger.debug = function(message) { logWithArgs("DEBUG", arguments); }; // log at the specified level with args function logWithArgs(level, args) { args = [level].concat([].slice.call(args)); logger.logLevel.apply(logger, args); } /** * Logs a message at the specified level. * * Parameters passed after message are used applied to * the message with utils.format() */ logger.logLevel = function(level /* , ... */) { // format the message with the parameters var formatArgs = [].slice.call(arguments, 1); var message = logger.format.apply(logger.format, formatArgs); if (LevelsMap[level] === null) { throw new Error("invalid logging level: " + level); } if (LevelsMap[level] > CurrentLevel) return; // queue the message if not yet at deviceready if (!DeviceReady && !UseConsole) { Queued.push([level, message]); return; } // if not using the console, use the native logger if (!UseConsole) { exec(null, null, "Logger", "logLevel", [level, message]); return; } // make sure console is not using logger if (console.__usingCordovaLogger) { throw new Error("console and logger are too intertwingly"); } // log to the console switch (level) { case logger.LOG: console.log(message); break; case logger.ERROR: console.log("ERROR: " + message); break; case logger.WARN: console.log("WARN: " + message); break; case logger.INFO: console.log("INFO: " + message); break; case logger.DEBUG: console.log("DEBUG: " + message); break; } }; /** * Formats a string and arguments following it ala console.log() * * Any remaining arguments will be appended to the formatted string. * * for rationale, see FireBug's Console API: * http://getfirebug.com/wiki/index.php/Console_API */ logger.format = function(formatString, args) { return __format(arguments[0], [].slice.call(arguments,1)).join(' '); }; //------------------------------------------------------------------------------ /** * Formats a string and arguments following it ala vsprintf() * * format chars: * %j - format arg as JSON * %o - format arg as JSON * %c - format arg as '' * %% - replace with '%' * any other char following % will format it's * arg via toString(). * * Returns an array containing the formatted string and any remaining * arguments. */ function __format(formatString, args) { if (formatString === null || formatString === undefined) return [""]; if (arguments.length == 1) return [formatString.toString()]; if (typeof formatString != "string") formatString = formatString.toString(); var pattern = /(.*?)%(.)(.*)/; var rest = formatString; var result = []; while (args.length) { var match = pattern.exec(rest); if (!match) break; var arg = args.shift(); rest = match[3]; result.push(match[1]); if (match[2] == '%') { result.push('%'); args.unshift(arg); continue; } result.push(__formatted(arg, match[2])); } result.push(rest); var remainingArgs = [].slice.call(args); remainingArgs.unshift(result.join('')); return remainingArgs; } function __formatted(object, formatChar) { try { switch(formatChar) { case 'j': case 'o': return JSON.stringify(object); case 'c': return ''; } } catch (e) { return "error JSON.stringify()ing argument: " + e; } if ((object === null) || (object === undefined)) { return Object.prototype.toString.call(object); } return object.toString(); } //------------------------------------------------------------------------------ // when deviceready fires, log queued messages logger.__onDeviceReady = function() { if (DeviceReady) return; DeviceReady = true; for (var i=0; i<Queued.length; i++) { var messageArgs = Queued[i]; logger.logLevel(messageArgs[0], messageArgs[1]); } Queued = null; }; // add a deviceready event to log queued messages document.addEventListener("deviceready", logger.__onDeviceReady, false); }); // file: lib/common/plugin/logger/symbols.js define("cordova/plugin/logger/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/logger', 'cordova.logger'); }); // file: lib/ios/plugin/media/symbols.js define("cordova/plugin/media/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.defaults('cordova/plugin/Media', 'Media'); modulemapper.clobbers('cordova/plugin/MediaError', 'MediaError'); }); // file: lib/common/plugin/network.js define("cordova/plugin/network", function(require, exports, module) { var exec = require('cordova/exec'), cordova = require('cordova'), channel = require('cordova/channel'), utils = require('cordova/utils'); // Link the onLine property with the Cordova-supplied network info. // This works because we clobber the naviagtor object with our own // object in bootstrap.js. if (typeof navigator != 'undefined') { utils.defineGetter(navigator, 'onLine', function() { return this.connection.type != 'none'; }); } function NetworkConnection() { this.type = 'unknown'; } /** * Get connection info * * @param {Function} successCallback The function to call when the Connection data is available * @param {Function} errorCallback The function to call when there is an error getting the Connection data. (OPTIONAL) */ NetworkConnection.prototype.getInfo = function(successCallback, errorCallback) { exec(successCallback, errorCallback, "NetworkStatus", "getConnectionInfo", []); }; var me = new NetworkConnection(); var timerId = null; var timeout = 500; channel.onCordovaReady.subscribe(function() { me.getInfo(function(info) { me.type = info; if (info === "none") { // set a timer if still offline at the end of timer send the offline event timerId = setTimeout(function(){ cordova.fireDocumentEvent("offline"); timerId = null; }, timeout); } else { // If there is a current offline event pending clear it if (timerId !== null) { clearTimeout(timerId); timerId = null; } cordova.fireDocumentEvent("online"); } // should only fire this once if (channel.onCordovaConnectionReady.state !== 2) { channel.onCordovaConnectionReady.fire(); } }, function (e) { // If we can't get the network info we should still tell Cordova // to fire the deviceready event. if (channel.onCordovaConnectionReady.state !== 2) { channel.onCordovaConnectionReady.fire(); } console.log("Error initializing Network Connection: " + e); }); }); module.exports = me; }); // file: lib/common/plugin/networkstatus/symbols.js define("cordova/plugin/networkstatus/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/network', 'navigator.network.connection', 'navigator.network.connection is deprecated. Use navigator.connection instead.'); modulemapper.clobbers('cordova/plugin/network', 'navigator.connection'); modulemapper.defaults('cordova/plugin/Connection', 'Connection'); }); // file: lib/common/plugin/notification.js define("cordova/plugin/notification", function(require, exports, module) { var exec = require('cordova/exec'); var platform = require('cordova/platform'); /** * Provides access to notifications on the device. */ module.exports = { /** * Open a native alert dialog, with a customizable title and button text. * * @param {String} message Message to print in the body of the alert * @param {Function} completeCallback The callback that is called when user clicks on a button. * @param {String} title Title of the alert dialog (default: Alert) * @param {String} buttonLabel Label of the close button (default: OK) */ alert: function(message, completeCallback, title, buttonLabel) { var _title = (title || "Alert"); var _buttonLabel = (buttonLabel || "OK"); exec(completeCallback, null, "Notification", "alert", [message, _title, _buttonLabel]); }, /** * Open a native confirm dialog, with a customizable title and button text. * The result that the user selects is returned to the result callback. * * @param {String} message Message to print in the body of the alert * @param {Function} resultCallback The callback that is called when user clicks on a button. * @param {String} title Title of the alert dialog (default: Confirm) * @param {Array} buttonLabels Array of the labels of the buttons (default: ['OK', 'Cancel']) */ confirm: function(message, resultCallback, title, buttonLabels) { var _title = (title || "Confirm"); var _buttonLabels = (buttonLabels || ["OK", "Cancel"]); // Strings are deprecated! if (typeof _buttonLabels === 'string') { console.log("Notification.confirm(string, function, string, string) is deprecated. Use Notification.confirm(string, function, string, array)."); } // Some platforms take an array of button label names. // Other platforms take a comma separated list. // For compatibility, we convert to the desired type based on the platform. if (platform.id == "android" || platform.id == "ios" || platform.id == "windowsphone") { if (typeof _buttonLabels === 'string') { var buttonLabelString = _buttonLabels; _buttonLabels = _buttonLabels.split(","); // not crazy about changing the var type here } } else { if (Array.isArray(_buttonLabels)) { var buttonLabelArray = _buttonLabels; _buttonLabels = buttonLabelArray.toString(); } } exec(resultCallback, null, "Notification", "confirm", [message, _title, _buttonLabels]); }, /** * Open a native prompt dialog, with a customizable title and button text. * The following results are returned to the result callback: * buttonIndex Index number of the button selected. * input1 The text entered in the prompt dialog box. * * @param {String} message Dialog message to display (default: "Prompt message") * @param {Function} resultCallback The callback that is called when user clicks on a button. * @param {String} title Title of the dialog (default: "Prompt") * @param {Array} buttonLabels Array of strings for the button labels (default: ["OK","Cancel"]) */ prompt: function(message, resultCallback, title, buttonLabels) { var _message = (message || "Prompt message"); var _title = (title || "Prompt"); var _buttonLabels = (buttonLabels || ["OK","Cancel"]); exec(resultCallback, null, "Notification", "prompt", [_message, _title, _buttonLabels]); }, /** * Causes the device to vibrate. * * @param {Integer} mills The number of milliseconds to vibrate for. */ vibrate: function(mills) { exec(null, null, "Notification", "vibrate", [mills]); }, /** * Causes the device to beep. * On Android, the default notification ringtone is played "count" times. * * @param {Integer} count The number of beeps. */ beep: function(count) { exec(null, null, "Notification", "beep", [count]); } }; }); // file: lib/ios/plugin/notification/symbols.js define("cordova/plugin/notification/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/notification', 'navigator.notification'); modulemapper.merges('cordova/plugin/ios/notification', 'navigator.notification'); }); // file: lib/common/plugin/requestFileSystem.js define("cordova/plugin/requestFileSystem", function(require, exports, module) { var argscheck = require('cordova/argscheck'), FileError = require('cordova/plugin/FileError'), FileSystem = require('cordova/plugin/FileSystem'), exec = require('cordova/exec'); /** * Request a file system in which to store application data. * @param type local file system type * @param size indicates how much storage space, in bytes, the application expects to need * @param successCallback invoked with a FileSystem object * @param errorCallback invoked if error occurs retrieving file system */ var requestFileSystem = function(type, size, successCallback, errorCallback) { argscheck.checkArgs('nnFF', 'requestFileSystem', arguments); var fail = function(code) { errorCallback && errorCallback(new FileError(code)); }; if (type < 0 || type > 3) { fail(FileError.SYNTAX_ERR); } else { // if successful, return a FileSystem object var success = function(file_system) { if (file_system) { if (successCallback) { // grab the name and root from the file system object var result = new FileSystem(file_system.name, file_system.root); successCallback(result); } } else { // no FileSystem object returned fail(FileError.NOT_FOUND_ERR); } }; exec(success, fail, "File", "requestFileSystem", [type, size]); } }; module.exports = requestFileSystem; }); // file: lib/common/plugin/resolveLocalFileSystemURI.js define("cordova/plugin/resolveLocalFileSystemURI", function(require, exports, module) { var argscheck = require('cordova/argscheck'), DirectoryEntry = require('cordova/plugin/DirectoryEntry'), FileEntry = require('cordova/plugin/FileEntry'), FileError = require('cordova/plugin/FileError'), exec = require('cordova/exec'); /** * Look up file system Entry referred to by local URI. * @param {DOMString} uri URI referring to a local file or directory * @param successCallback invoked with Entry object corresponding to URI * @param errorCallback invoked if error occurs retrieving file system entry */ module.exports = function(uri, successCallback, errorCallback) { argscheck.checkArgs('sFF', 'resolveLocalFileSystemURI', arguments); // error callback var fail = function(error) { errorCallback && errorCallback(new FileError(error)); }; // sanity check for 'not:valid:filename' if(!uri || uri.split(":").length > 2) { setTimeout( function() { fail(FileError.ENCODING_ERR); },0); return; } // if successful, return either a file or directory entry var success = function(entry) { var result; if (entry) { if (successCallback) { // create appropriate Entry object result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath) : new FileEntry(entry.name, entry.fullPath); successCallback(result); } } else { // no Entry object returned fail(FileError.NOT_FOUND_ERR); } }; exec(success, fail, "File", "resolveLocalFileSystemURI", [uri]); }; }); // file: lib/common/plugin/splashscreen.js define("cordova/plugin/splashscreen", function(require, exports, module) { var exec = require('cordova/exec'); var splashscreen = { show:function() { exec(null, null, "SplashScreen", "show", []); }, hide:function() { exec(null, null, "SplashScreen", "hide", []); } }; module.exports = splashscreen; }); // file: lib/common/plugin/splashscreen/symbols.js define("cordova/plugin/splashscreen/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); modulemapper.clobbers('cordova/plugin/splashscreen', 'navigator.splashscreen'); }); // file: lib/common/symbols.js define("cordova/symbols", function(require, exports, module) { var modulemapper = require('cordova/modulemapper'); // Use merges here in case others symbols files depend on this running first, // but fail to declare the dependency with a require(). modulemapper.merges('cordova', 'cordova'); modulemapper.clobbers('cordova/exec', 'cordova.exec'); modulemapper.clobbers('cordova/exec', 'Cordova.exec'); }); // file: lib/common/utils.js define("cordova/utils", function(require, exports, module) { var utils = exports; /** * Defines a property getter / setter for obj[key]. */ utils.defineGetterSetter = function(obj, key, getFunc, opt_setFunc) { if (Object.defineProperty) { var desc = { get: getFunc, configurable: true }; if (opt_setFunc) { desc.set = opt_setFunc; } Object.defineProperty(obj, key, desc); } else { obj.__defineGetter__(key, getFunc); if (opt_setFunc) { obj.__defineSetter__(key, opt_setFunc); } } }; /** * Defines a property getter for obj[key]. */ utils.defineGetter = utils.defineGetterSetter; utils.arrayIndexOf = function(a, item) { if (a.indexOf) { return a.indexOf(item); } var len = a.length; for (var i = 0; i < len; ++i) { if (a[i] == item) { return i; } } return -1; }; /** * Returns whether the item was found in the array. */ utils.arrayRemove = function(a, item) { var index = utils.arrayIndexOf(a, item); if (index != -1) { a.splice(index, 1); } return index != -1; }; utils.typeName = function(val) { return Object.prototype.toString.call(val).slice(8, -1); }; /** * Returns an indication of whether the argument is an array or not */ utils.isArray = function(a) { return utils.typeName(a) == 'Array'; }; /** * Returns an indication of whether the argument is a Date or not */ utils.isDate = function(d) { return utils.typeName(d) == 'Date'; }; /** * Does a deep clone of the object. */ utils.clone = function(obj) { if(!obj || typeof obj == 'function' || utils.isDate(obj) || typeof obj != 'object') { return obj; } var retVal, i; if(utils.isArray(obj)){ retVal = []; for(i = 0; i < obj.length; ++i){ retVal.push(utils.clone(obj[i])); } return retVal; } retVal = {}; for(i in obj){ if(!(i in retVal) || retVal[i] != obj[i]) { retVal[i] = utils.clone(obj[i]); } } return retVal; }; /** * Returns a wrapped version of the function */ utils.close = function(context, func, params) { if (typeof params == 'undefined') { return function() { return func.apply(context, arguments); }; } else { return function() { return func.apply(context, params); }; } }; /** * Create a UUID */ utils.createUUID = function() { return UUIDcreatePart(4) + '-' + UUIDcreatePart(2) + '-' + UUIDcreatePart(2) + '-' + UUIDcreatePart(2) + '-' + UUIDcreatePart(6); }; /** * Extends a child object from a parent object using classical inheritance * pattern. */ utils.extend = (function() { // proxy used to establish prototype chain var F = function() {}; // extend Child from Parent return function(Child, Parent) { F.prototype = Parent.prototype; Child.prototype = new F(); Child.__super__ = Parent.prototype; Child.prototype.constructor = Child; }; }()); /** * Alerts a message in any available way: alert or console.log. */ utils.alert = function(msg) { if (window.alert) { window.alert(msg); } else if (console && console.log) { console.log(msg); } }; //------------------------------------------------------------------------------ function UUIDcreatePart(length) { var uuidpart = ""; for (var i=0; i<length; i++) { var uuidchar = parseInt((Math.random() * 256), 10).toString(16); if (uuidchar.length == 1) { uuidchar = "0" + uuidchar; } uuidpart += uuidchar; } return uuidpart; } }); window.cordova = require('cordova'); // file: lib/scripts/bootstrap.js (function (context) { var channel = require('cordova/channel'); var platformInitChannelsArray = [channel.onNativeReady, channel.onPluginsReady]; function logUnfiredChannels(arr) { for (var i = 0; i < arr.length; ++i) { if (arr[i].state != 2) { console.log('Channel not fired: ' + arr[i].type); } } } window.setTimeout(function() { if (channel.onDeviceReady.state != 2) { console.log('deviceready has not fired after 5 seconds.'); logUnfiredChannels(platformInitChannelsArray); logUnfiredChannels(channel.deviceReadyChannelsArray); } }, 5000); // Replace navigator before any modules are required(), to ensure it happens as soon as possible. // We replace it so that properties that can't be clobbered can instead be overridden. function replaceNavigator(origNavigator) { var CordovaNavigator = function() {}; CordovaNavigator.prototype = origNavigator; var newNavigator = new CordovaNavigator(); // This work-around really only applies to new APIs that are newer than Function.bind. // Without it, APIs such as getGamepads() break. if (CordovaNavigator.bind) { for (var key in origNavigator) { if (typeof origNavigator[key] == 'function') { newNavigator[key] = origNavigator[key].bind(origNavigator); } } } return newNavigator; } if (context.navigator) { context.navigator = replaceNavigator(context.navigator); } // _nativeReady is global variable that the native side can set // to signify that the native code is ready. It is a global since // it may be called before any cordova JS is ready. if (window._nativeReady) { channel.onNativeReady.fire(); } /** * Create all cordova objects once native side is ready. */ channel.join(function() { // Call the platform-specific initialization require('cordova/platform').initialize(); // Fire event to notify that all objects are created channel.onCordovaReady.fire(); // Fire onDeviceReady event once page has fully loaded, all // constructors have run and cordova info has been received from native // side. // This join call is deliberately made after platform.initialize() in // order that plugins may manipulate channel.deviceReadyChannelsArray // if necessary. channel.join(function() { require('cordova').fireDocumentEvent('deviceready'); }, channel.deviceReadyChannelsArray); }, platformInitChannelsArray); }(window)); // file: lib/scripts/bootstrap-ios.js require('cordova/channel').onNativeReady.fire(); // file: lib/scripts/plugin_loader.js // Tries to load all plugins' js-modules. // This is an async process, but onDeviceReady is blocked on onPluginsReady. // onPluginsReady is fired when there are no plugins to load, or they are all done. (function (context) { // To be populated with the handler by handlePluginsObject. var onScriptLoadingComplete; var scriptCounter = 0; function scriptLoadedCallback() { scriptCounter--; if (scriptCounter === 0) { onScriptLoadingComplete && onScriptLoadingComplete(); } } // Helper function to inject a <script> tag. function injectScript(path) { scriptCounter++; var script = document.createElement("script"); script.onload = scriptLoadedCallback; script.src = path; document.head.appendChild(script); } // Called when: // * There are plugins defined and all plugins are finished loading. // * There are no plugins to load. function finishPluginLoading() { context.cordova.require('cordova/channel').onPluginsReady.fire(); } // Handler for the cordova_plugins.json content. // See plugman's plugin_loader.js for the details of this object. // This function is only called if the really is a plugins array that isn't empty. // Otherwise the XHR response handler will just call finishPluginLoading(). function handlePluginsObject(modules) { // First create the callback for when all plugins are loaded. var mapper = context.cordova.require('cordova/modulemapper'); onScriptLoadingComplete = function() { // Loop through all the plugins and then through their clobbers and merges. for (var i = 0; i < modules.length; i++) { var module = modules[i]; if (!module) continue; if (module.clobbers && module.clobbers.length) { for (var j = 0; j < module.clobbers.length; j++) { mapper.clobbers(module.id, module.clobbers[j]); } } if (module.merges && module.merges.length) { for (var k = 0; k < module.merges.length; k++) { mapper.merges(module.id, module.merges[k]); } } // Finally, if runs is truthy we want to simply require() the module. // This can be skipped if it had any merges or clobbers, though, // since the mapper will already have required the module. if (module.runs && !(module.clobbers && module.clobbers.length) && !(module.merges && module.merges.length)) { context.cordova.require(module.id); } } finishPluginLoading(); }; // Now inject the scripts. for (var i = 0; i < modules.length; i++) { injectScript(modules[i].file); } } // // Try to XHR the cordova_plugins.json file asynchronously. // try { // we commented we were going to try, so let us actually try and catch // var xhr = new context.XMLHttpRequest(); // xhr.onload = function() { // // If the response is a JSON string which composes an array, call handlePluginsObject. // // If the request fails, or the response is not a JSON array, just call finishPluginLoading. // var obj = this.responseText && JSON.parse(this.responseText); // if (obj && obj instanceof Array && obj.length > 0) { // handlePluginsObject(obj); // } else { // finishPluginLoading(); // } // }; // xhr.onerror = function() { // finishPluginLoading(); // }; // xhr.open('GET', 'cordova_plugins.json', true); // Async // xhr.send(); // } // catch(err){ // finishPluginLoading(); // } finishPluginLoading(); }(window)); })();
"use strict" // module Test.Phantomjs.Object exports.addCookie = function (cookie) { return function () { return phantom.addCookie(cookie) } } exports.clearCookies = function () { return phantom.clearCookies() } exports.getCookies = function () { return phantom.cookies } exports.setCookies = function (v) { return function () { return phantom.cookies = v } } exports.getCookiesEnabled = function () { return phantom.cookiesEnabled } exports.setCookiesEnabled = function (v) { return function () { return phantom.cookiesEnabled = v } } exports.deleteCookie = function (key) { return function () { return phantom.deleteCookie(key) } } exports.exit = function (exitCode) { return function () { return phantom.exit(exitCode) } } exports.getLibraryPath = function () { return phantom.libraryPath } exports.setLibraryPath = function (path) { return function () { return phantom.libraryPath = path } } exports.version = function () { return phantom.version }
'use strict'; /** * This method initializes a web server with certificates if needed and returns a promise * @param app * @param settings * @param certificates * @param leSettings * @param log * @param adapter * @returns {Promise<unknown>} */ async function createServerAsync(app, settings, certificates, leSettings, log, adapter) { if (!settings.secure) { log.debug('Using http createServer'); return require('http').createServer(app); } else if (!settings.leEnabled) { log.debug('Using https createServer'); return require('https').createServer(certificates, app); } // prepare domains if (leSettings && typeof leSettings.domains === 'string') { leSettings.domains = leSettings.domains .split(',') .map(d => d.trim()) .filter(d => d); } if (!leSettings || !leSettings.email || !Array.isArray(leSettings.domains) || !leSettings.domains.length) { log.error( "Please specify the email address and domains to use Let's Encrypt certificates! We reuse old certificates" ); return require('https').createServer(certificates, app); } const { tools } = require('@iobroker/js-controller-common'); const fs = require('fs-extra'); let leDir; let configPath = tools.getConfigFileName().replace(/\\/g, '/'); const parts = configPath.split('/'); parts.pop(); configPath = parts.join('/'); if (leSettings.path && (leSettings.path[0] === '/' || leSettings.path.match(/^[A-Za-z]:/))) { leDir = leSettings.path; } else { leDir = configPath + '/' + (leSettings.path || 'letsencrypt'); } try { if (!fs.existsSync(leDir)) { fs.mkdirSync(leDir); } } catch (err) { log.error( `Lets encrypt Directory does not exist and can not be created as ${leDir}: ${err.message}. We reuse old certificates` ); return require('https').createServer(certificates, app); } // TODO? Tried adding sites in the gl create but that didn't work so is below with gl.add. const pkg = require('../package.json'); const glOpts = { packageRoot: __dirname + '/..', configDir: leDir, maintainerEmail: leSettings.email, cluster: false, packageAgent: pkg.name + '/' + pkg.version, renew: settings.leUpdate, notify: (ev, params) => { switch (ev) { case 'warning': log.warn('[LE] ' + (typeof params === 'string' ? params : JSON.stringify(params))); break; case 'error': log.error('[LE] ' + (typeof params === 'string' ? params : JSON.stringify(params))); break; default: // For all other event types, always debug log.debug(`[LE] ${ev}: ` + (typeof params === 'string' ? params : JSON.stringify(params))); // Special cases for certificate issue/renewal notices // TODO: possibly check for ev === 'cert_renewal' / 'cert_issue' if (params.status === 'valid' && params.altname) { log.info(`Received new certificate for ${params.altname} via ${params.type}`); } else if (params.renewAt) { log.info( `Certificate for ${params.subject} is valid till ${new Date(params.renewAt).toISOString()}` ); adapter && adapter.sendToHost(adapter.host, 'certsUpdated', { validTill: params.renewAt, domain: params.subject, instance: adapter.namespace }); } break; } } }; if (settings.leStaging) { log.debug('Greenlock staging enabled'); glOpts.staging = true; } const gl = require('greenlock').create(glOpts); let addedDomains = 0; // Add sites. // Allowed in leSettings.domains: // * "String" with comma separated list of single domain names (parsing above) // * each will be registered as own cert // * "Array" with eighter // * "String" that is the domain name and each will be registered as own cert // * "Array of String where the first entry is the main domain name and others // alternative names (alternative names also contain main domain for greenlock) for (const domainEntry of leSettings.domains) { let altnames; let domain; if (Array.isArray(domainEntry)) { altnames = domainEntry; domain = altnames[0]; } else if (typeof domainEntry === 'string') { altnames = [domainEntry]; domain = domainEntry; } if (!domain || !altnames) { continue; } log.debug(`Adding to Greenlock: ${domain} with alternative names ${altnames.join(',')}`); try { await gl.add({ subject: domain, altnames }); addedDomains++; } catch (err) { log.warn(`Failed to add "${domain}" into configuration: ${err.message}`); } } if (!addedDomains) { log.error(`No domains available for Let's Encrypt, reuse old certificates`); return require('https').createServer(certificates, app); } await gl.manager.defaults({ agreeToTerms: true, subscriberEmail: leSettings.email }); return new Promise((resolve, reject) => { require('greenlock-express') .init({ greenlock: gl }) .ready(glx => { // The below is from greenlock-express examples/https/server.js if (settings.leUpdate) { // Start the challenge server. Wait for it before resolving (pass back https). settings.lePort = parseInt(settings.lePort, 10) || 80; const bind = !settings.bind || settings.bind === '0.0.0.0' ? undefined : settings.bind || undefined; // Check port not in use, because catching EADDRINUSE from httpServer.listen not possible. adapter.getPort(settings.lePort, bind, port => { if (port !== settings.lePort) { reject(new Error(`Challenge server port ${settings.lePort} already in use`)); } else { const httpServer = glx.httpServer(); httpServer.listen(settings.lePort, bind, () => { log.info(`Challenge server listening on port ${settings.lePort}`); log.info( `If something is not working and your adapter is not reachable anymore you can turn off HTTPS with executing "iobroker ${ adapter ? adapter.namespace : 'admin' } set --secure false" in your shell.` ); resolve(glx.httpsServer(null, app)); }); } }); } else { // Just resolve with https. // Don't call listen here (or above) because caller will do that resolve(glx.httpsServer(null, app)); } }); }); } /** * This method initializes a web server with certificates if needed. No Let's encrypt support anymore * @param app * @param settings * @param certificates * @param leSettings * @param log * @deprecated * @returns {object} */ function createServer(app, settings, certificates, leSettings, log) { let server; if (settings.secure) { if (leSettings && settings.leEnabled) { log.info( 'Please update this adapter to the latest version to allow again to update your certificates. Using the current certificates' ); } try { server = require('https').createServer(certificates, app); } catch (err) { log.error('HTTPS server could not be started: ' + err.message); } } else { server = require('http').createServer(app); } return server; } module.exports = { createServerAsync, createServer };
'use strict'; angular.module('openDeskApp.appDrawer', ['ngMaterial']);
/** * React Starter (https://github.com/erichardson30/react-starter) * * Copyright © 2016 Eric Richardson. All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE.txt file in the root directory of this source tree. */ import React from 'react'; import { IndexRoute, Route } from 'react-router'; import fetch from './core/fetch'; import App from './components/App'; import ContentPage from './components/ContentPage'; import TemplatePage from './components/TemplatePage'; import LoginPage from './components/LoginPage'; import QuestionPage from './components/QuestionPage'; import RegisterPage from './components/RegisterPage'; import NotFoundPage from './components/NotFoundPage'; import TemplateActions from './actions/TemplateActions'; import QuestionActions from './actions/QuestionActions'; async function getContextComponent(location, callback) { const response = await fetch(`/api/content?path=${location.pathname}`); const content = await response.json(); // using an arrow to pass page instance instead of page class; cb accepts class by default callback(null, () => <ContentPage {...content} />); } const getTemplates = async (location, callback) => { await TemplateActions.getTemplates(); callback(null, () => <TemplatePage />) } const getQuestions = async (location, callback) => { await QuestionActions.getQuestions(); callback(null, () => <QuestionPage />) } export default ( <Route> <Route path="/" component={App}> <IndexRoute getComponent={getContextComponent} /> <Route path="login" component={LoginPage} /> <Route path="templates" getComponent={getTemplates} /> <Route path="questions" getComponent={getQuestions} /> </Route> <Route path="*" component={NotFoundPage} /> </Route> );
import CreateActions from '../../utils/ActionsConstructor'; export default CreateActions( { fetch: {}, selectPricingPlan: {}, setInstances: {}, sliderChange: {}, submitPlan: {}, getBillingPlans: {}, updateBillingProfile: { asyncResult: true, children: ['completed', 'failure'], method: 'Syncano.Actions.Billing.updateProfile' }, subscribePlan: { asyncResult: true, asyncForm: true, loading: true, children: ['completed', 'failure'], method: 'Syncano.Actions.Billing.subscribePlan' }, fetchBillingCard: { asyncResult: true, loading: true, children: ['completed', 'failure'], method: 'Syncano.Actions.Billing.getCard' }, fetchBillingSubscriptions: { asyncResult: true, asyncForm: true, loading: true, children: ['completed', 'failure'], method: 'Syncano.Actions.Billing.listSubscriptions' }, addCard: { asyncResult: true, asyncForm: true, loading: true, children: ['completed', 'failure'], method: 'Syncano.Actions.Billing.addCard' }, updateCard: { asyncResult: true, asyncForm: true, loading: true, children: ['completed', 'failure'], method: 'Syncano.Actions.Billing.updateCard' } }, { withDialog: true } );
define(function (require) { const [$, $$] = require('/vools.js') const Profile = require('controller/editor/profile/profile.js') const Editor = require('controller/editor/editor.js') const input = $$('[name="link_symbol"]', Profile.container) Editor.addProperty('link_symbol', () => { const value = input.value return value.length ? value : null }) Editor.addApplyQuery('link_symbol', link_symbol => { if (typeof(link_symbol) !== 'string') { input.value = '' } else { input.value = link_symbol } }) return {} })
//Proplem 7. Write an expression that checks if given positive integer number n (n ≤ 100) is prime// jsConsole.writeLine('Problem 7. Is prime') var input = [1, 2, 3, 4, 9, 37, 97, 51, -3, 0], len = input.length, number, i; for (i = 0; i < len; i++) { number = input[i] result = ((number % 2) != 0 && (number % 3) != 0 && (number % 5) != 0 && (number % 7) != 0); jsConsole.writeLine('This number ' + number + ' is prime - ' + result); console.log('This number ' + number + ' is prime - ' + result); } jsConsole.writeLine('-----------------------------------------------------------------------------');
var statements = require('../statements'); var SyntaxError = require('../SyntaxError'); /** * Shifts data from the stack * * @param {String} args The arguments to the command * @constructor */ function ReadCommand(args) { var parsed = new statements.ArgumentStatement(args); for (var i = 0; i < parsed.args.length; i++) { var placeVar = parsed.args[i]; if (!(placeVar.child instanceof statements.VariableStatement || placeVar.child instanceof statements.FunctionStatement)) throw new SyntaxError('Expected variable'); } this.items = parsed.args; } /** * Converts the command arguments to a string * * @returns {string} */ ReadCommand.prototype.toString = function() { return this.items.join(", "); }; /** * Converts the command to JSON * * @returns {Object} */ ReadCommand.prototype.toJSON = function() { return { items: this.items }; }; /** * Executes the command * * @param {ExecutionContext} data * @param {Function} next */ ReadCommand.prototype.execute = function(data, next) { for (var i = 0; i < this.items.length; i++) { if (!data.private.data.length) throw new Error('No more data'); var placeVar = this.items[i].child; var poppedVal = data.private.data.shift(); data.setVariable(placeVar, poppedVal); } next(); }; module.exports = ReadCommand;
import { invariant } from '../helpers'; import { getOwnPropertyDescriptor } from '../Reflect'; export function deleteProperty(trapResult, target, key) { if (!trapResult) { return false; } var targetDesc = getOwnPropertyDescriptor(target, key); if (targetDesc !== undefined && !targetDesc.configurable) { invariant('A property cannot be reported as deleted, if it exists as a non-configurable own property of the target object.'); } return true; }
#!/usr/bin/env node var program = require('commander'); var nStore = require('nstore'); function list(val) { return val.split(',').map(Number); } program .version('0.0.1') .option('-c, --clear', 'delete all todo items') program.on('--help', function(){ console.log(' Examples:'); console.log(''); console.log(' $ custom-help --help'); console.log(' $ custom-help -h'); console.log(''); }); program.parse(process.argv); var todoItems = nStore.new('data/todo.db', function () { console.log("loaded"); }); todoItems.save(null, {title: "Demo", "deadline":"demo"}, function (err, key) { if (err) { throw err; } console.log("created new todo item with key "+key); }); console.log('todo.js');
import api from './api'; export default { api }
/** * THIS FILE IS AUTO-GENERATED * DON'T MAKE CHANGES HERE */ import { absDependencies } from './dependenciesAbs.generated' import { addDependencies } from './dependenciesAdd.generated' import { conjDependencies } from './dependenciesConj.generated' import { equalScalarDependencies } from './dependenciesEqualScalar.generated' import { largerDependencies } from './dependenciesLarger.generated' import { matrixDependencies } from './dependenciesMatrix.generated' import { multiplyDependencies } from './dependenciesMultiply.generated' import { powDependencies } from './dependenciesPow.generated' import { smallerDependencies } from './dependenciesSmaller.generated' import { sqrtDependencies } from './dependenciesSqrt.generated' import { typedDependencies } from './dependenciesTyped.generated' import { createNorm } from '../../factoriesAny.js' export const normDependencies = { absDependencies, addDependencies, conjDependencies, equalScalarDependencies, largerDependencies, matrixDependencies, multiplyDependencies, powDependencies, smallerDependencies, sqrtDependencies, typedDependencies, createNorm }
$Behavior.photoCategoryDropDown = function() { $('.js_photo_category').click(function() { var iId = this.id.replace('js_photo_category_', ''); var iItemId = $(this).parents('div:first').parent().parent().parent().find('.js_photo_item_id').html(); var sValue = $(this).html(); $('#js_photo_category_id_' + iItemId + iId).remove(); $(this).prepend('<div id="js_photo_category_id_' + iItemId + iId + '"><input type="hidden" name="val[category_id]" value="' + iId + '" /></div>'); $('.js_photo_category').removeClass('selected'); $(this).addClass('selected'); $('.select_clone').hide(); $('.select_clone_select').html(sValue); return false; }); $('.js_photo_category_done').click(function() { $('.select_clone').hide(); return false; }); $('.select_clone_select').click(function() { $(this).next('.select_clone').toggle(); return false; }); $(document).click(function() { $('.select_clone').hide(); }); if (trim($('.select_clone_inner').html()) == '<ul></ul>') { $('#js_photo_parent').remove(); $('#js_category_holder').remove(); } }
Game.GemsCrusher = function(board) { this.board = board; this.crushCounter = 0; } Game.GemsCrusher.prototype = { run: function(gems) { var self = this, crushCounterTemHash = {}; gems.forEach(function(gem) { if(!crushCounterTemHash[gem.id]) { self.crushCounter += 1; crushCounterTemHash[gem.id] = true; } }); gems.forEach(function(gem) { var animation = gem.animations.add('explosion', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); gem.loadTexture('explosion', 0); gem.destroying = true; animation.onComplete.add(function() { self.crushCounter--; self.board.deadGemsGroup.add(arguments[0]); if(self.crushCounter === 0) { self.board.setStateTo('refill'); } }, self); animation.play(20, false); }); }, }
var common = require('../../common'); var assert = require('assert'); var pool = common.createPool(); pool.query('SELECT 1', function (err, _rows, _fields) { if (err) throw err; rows = _rows; fields = _fields; pool.end(); }); process.on('exit', function () { assert.deepEqual(rows, [{1: 1}]); assert.equal(fields[0].name, '1'); });
import _ from 'lodash' import { assert, splitAt, debug, regexpmatch, does_match, types, } from './utils' const EMBEDDED = ['javascript'] const makeNode = (type, payload) => ({type, payload}) const regexp = { matchElement: /^([a-z#.][^ :]*) *([|= ]|==|$)((?: *[^ ]+)*)$/i, matchInline: /^([^: ]+): *(.*)/, tagOrClassOrId: /(?:[a-z]+)|(?:#[a-z0-9-]+)|(?:\.[a-z0-9-]+)/ig, attribute: /([a-z]+)=[^ ]/i, matchLogic: /^- (.*)/, matchSpecialText: /^(\||==?) *(.*)/, } const makeTextNode = node => { assert(node.getChildren().length !== [], 'A text node can\'t have any children', node) const match = regexpmatch(regexp.matchSpecialText, node.line) const [operator, text] = match || ['|', node.line] switch (operator) { case '|': return makeNode(types.Text, text) case '=': return makeNode(types.Variable, { code: text, escaped: true, }) case '==': return makeNode(types.Variable, { code: text, escaped: false, }) } } const matchers = [{ regexp: regexp.matchSpecialText, handler: (node, tail, next) => { assert( node.getChildren.length == 0, `No please, don't give text any children!`, node ) return [makeTextNode(node)].concat(next(tail)) } }, { regexp: regexp.matchInline, handler: (node, tail, next) => { const [tag, rest] = regexpmatch(regexp.matchInline, node.line) // It's an embedder if (EMBEDDED.indexOf(tag) !== -1) { return [makeNode(types.Block, { type: tag, block: (rest == '' ? [] : [rest]).concat(node.getBlock()) })].concat(next(tail)) } else { // It's an embedded tag assert(rest !== '', `After a inline colon you need content!`, node) return next([{ line: tag, getChildren: () => [{ line: rest, getChildren: () => [], getBlock: () => [], }].concat(node.getChildren()), getBlock: () => [node.line].concat(node.getBlock()), }].concat(tail)) } } }, { // Html tags like tag, .class, #id and mixes of those regexp: regexp.matchElement, handler: (node, tail, next) => { const [tag, ...args] = node.line.split(' ') const info = Object.assign( {id: [], className: [], tag: []}, _.groupBy(tag.match(regexp.tagOrClassOrId), match => { switch (match[0]) { case '#': return 'id' case '.': return 'className' default: return 'tag' } }) ) const [attributes, text] = splitAt(args, (subject) => does_match(regexp.attribute, subject)) const attributes2 = attributes.map(x => x.split('=')) const hasText = text.length !== 0 const newChildren = hasText ? [makeTextNode({line: text.join(' '), getChildren: () => []})] .concat(node.getChildren().map(makeTextNode)) : next(node.getChildren()) const id = info.id.map(x => x.slice(1)).join(' ') const className = info.className.map(x => x.slice(1)).join(' ') const props = attributes2 .concat(id == '' ? [] : [['id', `"${id}"`]]) .concat(className == '' ? [] : [['className', `"${className}"`]]) return [makeNode(types.HTMLElement, { tag: info.tag[0] || 'div', props: _.zipObject(props), children: newChildren, })].concat(next(tail)) }, }, { // Conditions like - if ... - else ... regexp: regexp.matchLogic, handler: (node, tail, next) => { const withoutPrefix = regexpmatch(regexp.matchLogic, node.line)[0] const [command, ...args] = withoutPrefix.split(' ') .filter(x => x !== '') if (command == 'if') { const [_else_, ...withoutElse] = tail if (_else_ && /^- *?else */.test(_else_.line)) { return [makeNode(types.Logic, { type: 'if', payload: { predicate: withoutPrefix.match(/^if ?(.*)/)[1], else: next(_else_.getChildren()) }, children: next(node.getChildren()), })].concat(next(withoutElse)) } else { return [makeNode(types.Logic, { type: 'if', payload: { predicate: withoutPrefix.match(/^if ?(.*)/)[1], }, children: next(node.getChildren()), })].concat(next(tail)) } } if (command == 'for') { const [item, _in_, collection, ...tooMuch] = args assert( _in_ == 'in' && tooMuch.length == 0, `Malformed for loop (${node.line})`, node ) return [makeNode(types.Logic, { type: 'for', payload: { item, collection }, children: next(node.getChildren()), })].concat(next(tail)) } assert(command !== 'else', `Else condition without a if statement in front! D:`, node) assert(false, `Unsupported condition '${node.line}'`, node) } }] // }, { // // Conditions like - if ... - else ... // regexp: /^\$/, // handler: (line, children) => { // return [{ // type: 'ATTR', // payload: { // line: 'ng-' + line.slice(1), // children: [], // } // }] // } // }, { // // Conditions like - if ... - else ... // regexp: /^\@/, // handler: (line, children) => { // return [{ // type: 'ATTR', // payload: { // line: line.slice(1), // children: [], // } // }] // } // }] const graphToReact = nodes => { if (nodes.length == 0) { return [] } const [node, ...tail] = nodes const matcher = matchers.find( ({regexp}) => regexp.test(node.line)) //assert(matcher !== undefined, 'Weird things') if (matcher == undefined) { return [{ type: 'UNKNOWN', payload: node.line, }].concat(graphToReact(tail)) } //const match = line.line.match(matcher.regexp).slice(1); return matcher.handler(node, tail, graphToReact) } export default graphToReact
import React from 'react'; import EntypoIcon from '../EntypoIcon'; const iconClass = 'entypo-svgicon entypo--GraduationCap'; let EntypoGraduationCap = (props) => ( <EntypoIcon propClass={iconClass} {...props}> <path d="M3.302,12.238c0.464,1.879,1.054,2.701,3.022,3.562C8.293,16.66,9.228,17.6,10,17.6c0.771,0,1.648-0.822,3.616-1.684c1.969-0.861,1.443-1.123,1.907-3.002L10,15.6L3.302,12.238z M19.511,7.336l-8.325-4.662c-0.652-0.365-1.72-0.365-2.372,0L0.488,7.336c-0.652,0.365-0.652,0.963,0,1.328l8.325,4.662c0.652,0.365,1.72,0.365,2.372,0l5.382-3.014l-5.836-1.367C10.506,9,10.259,9.031,10,9.031c-1.052,0-1.904-0.506-1.904-1.131c0-0.627,0.853-1.133,1.904-1.133c0.816,0,1.51,0.307,1.78,0.734l6.182,2.029l1.549-0.867C20.162,8.299,20.162,7.701,19.511,7.336z M16.967,16.17c-0.065,0.385,1.283,1.018,1.411-0.107c0.579-5.072-0.416-6.531-0.416-6.531l-1.395,0.781C16.567,10.312,17.75,11.438,16.967,16.17z"/> </EntypoIcon> ); export default EntypoGraduationCap;
import { isAdded, getFilename } from './utils/files_to_commit'; export default function registerCheckAddedFilenames(grunt) { grunt.registerTask('checkAddedFilenames', function () { grunt.task.requires('collectFilesToCommit'); const invalid = grunt.config .get('filesToCommit') .map(getFilename) .filter(isAdded) .filter(name => !name.match(/([\/\\]|^)(docs|tasks[\/\\]config|webpackShims)([\/\\]|$)/)) .filter(name => name.match(/[A-Z \-]/)) .reduce((all, name) => `${all} ${name}\n`, ''); if (invalid) { grunt.fail.fatal(`Filenames must use snake_case.\n${invalid}`); } }); }
'use strict'; angular.module('core.utils', []);
const { EPS } = require('../../constants') //const FuzzyShape2Factory = require('../FuzzyFactory2d') const fromSides = require('./fromSides') const vec2 = require('../../../math/vec2') const shape2FromFuzzyFactory = (factory, sourceShape) => { let _factory = factory const newsides = sourceShape.sides .map(side => _factory.getSide(side)) // remove any side that is smaller than the EPS constant .filter(side => vec2.length(vec2.subtract(side[1], side[0])) > EPS) return fromSides(newsides) } const canonicalize = (shape, options) => { if (shape.isCanonicalized) { return shape } return shape //const factory = new FuzzyShape2Factory() //const result = shape2FromFuzzyFactory(factory, shape) //console.log('result', result) //result.isCanonicalized = true //return result } module.exports = canonicalize
'use strict' import angular from 'angular' import homeController from '../views/home/home-controller' import captureController from '../views/capture/capture-controller' export default () => { angular.module('app') .controller('HomeController', ['$scope', 'FirebaseFactory', homeController]) .controller('CaptureController', ['$scope', 'FirebaseFactory', captureController]) }
import * as React from 'react'; import createSvgIcon from './utils/createSvgIcon'; export default createSvgIcon( <path d="M12 8c1.93 0 3.5 1.57 3.5 3.5S13.93 15 12 15s-3.5-1.57-3.5-3.5S10.07 8 12 8zm4.53.38l3.97-3.96V7h2V1h-6v2h2.58l-3.97 3.97C14.23 6.36 13.16 6 12 6s-2.23.36-3.11.97l-.65-.65 1.41-1.41-1.41-1.42L6.82 4.9 4.92 3H7.5V1h-6v6h2V4.42l1.91 1.9-1.42 1.42L5.4 9.15l1.41-1.41.65.65c-.6.88-.96 1.95-.96 3.11 0 2.7 1.94 4.94 4.5 5.41V19H9v2h2v2h2v-2h2v-2h-2v-2.09c2.56-.47 4.5-2.71 4.5-5.41 0-1.16-.36-2.23-.97-3.12z" /> , 'TransgenderSharp');
/* "rpd-util" Personal Utilities Libary Language: JavaScript Author: Richard Price <[email protected]> For author's reference: http://www.w3schools.com/js/js_function_parameters.asp */ // JResig like 'log' function function log(target, debug){ // NEED TO ADD VARIABLE TRAVERSING HERE!!! // WHAT if it's null too?? var target = target + '\n'; var res = document.write(target); if (debug === true) { console.log(target); } return res; } // JResig like 'error' function function error(target, debug){ var target = target + '\n'; var res = document.write(target); if (debug === true) { console.log(target); } return res; }
var base = require('./karma.base.js') module.exports = function (config) { config.set(Object.assign(base, { browsers: ['Chrome'], reporters: ['progress'], singleRun: true })) }
module.exports = { require: jest.fn(), match: jest.fn(), app: jest.fn(), remote: jest.fn(), dialog: jest.fn() }; module.exports.app = { getPath: jest.fn() }
var test = require('tape'), diffo = require('../'); test('Simple object', function(t){ var a = { foo: 'bar' }, b = { foo: 'bar', bar: 'foo' }, expectedOutput = { bar: 'foo' }; t.plan(1); var result = diffo(a, b); t.deepEqual(result, expectedOutput); }); test('Deep object', function(t){ var a = { foo: 'bar', bar: 'foo', b: { a: 1, b: 2 } }, b = { foo: 'bar' }, expectedOutput = {}; t.plan(1); var result = diffo(a, b); t.deepEqual(result, expectedOutput); }); test('Deep object with same keys only one value different', function(t){ var a = { foo: 'bar', bar: 'foo', b: { a: 2, b: 2 } }, b = { foo: 'bar', b: { a: 1, b: 2 } }, expectedOutput = { b: { a: 1 } }; t.plan(1); var result = diffo(a, b); t.deepEqual(result, expectedOutput); }); test('Deep object with same keys only one value different and additional keys', function(t){ var a = { foo: 'bar', bar: 'foo', b: { a: 2, b: 2 }, c: { a: 1 } }, b = { foo: 'bar', b: { a: 1, b: 2 }, d: { a: 1 } }, expectedOutput = { b: { a: 1 }, d:{ a: 1 } }; t.plan(1); var result = diffo(a, b); t.deepEqual(result, expectedOutput); }); test('Deeper object', function(t){ var a = { foo: 'bar', bar: 'foo', b: { a: 2, b: 2 }, c: { a: 1 }, d: { e: { a: 1 } } }, b = { foo: 'bar', b: { a: 1, b: 2 }, d: { a: 1 }, e: { a: 1 } }, expectedOutput = { b: { a: 1 }, d:{ a: 1 }, e: { a: 1 } }; t.plan(1); var result = diffo(a, b); t.deepEqual(result, expectedOutput); }); test('Array values', function(t){ t.plan(1); var a = { x: 1 }, b = { x: 1, y: [1,2,3,4] }, expectedOutput = { y: [1,2,3,4] }; t.deepEqual(diffo(a, b), expectedOutput); }); test('Array same key, values in different order', function(t){ t.plan(1); var a = { x: 1, y: [4,3,2,1] }, b = { a: 1, y: [1,2,3,4] }, expectedOutput = { a: 1, y: [1,2,3,4] }; t.deepEqual(diffo(a, b), expectedOutput); }); test('Pass null for object a results in object b', function(t){ t.plan(1); var b = {a:1}, expectedOutput = b; t.equal(diffo(null, b), expectedOutput); }); test('Array same key of same length differnt values', function(t){ t.plan(1); var a = { x: 1, y: [1,2,4,3] }, b = { a: 1, y: [1,2,3,4] }, expectedOutput = { a: 1, y: [1,2,3,4] }; t.deepEqual(diffo(a, b), expectedOutput); }); test('Array same key of different length', function(t){ t.plan(1); var a = { x: 1, y: [1,2,3,4,5] }, b = { a: 1, y: [1,2,3,4] }, expectedOutput = { a: 1, y: [1,2,3,4] }; t.deepEqual(diffo(a, b), expectedOutput); }); test('Object A same key not an array', function(t){ t.plan(1); var a = { x: 1, y: 2 }, b = { a:1, y: [1,2,3,4] }, expectedOutput = { a: 1, y: [1,2,3,4] }; t.deepEqual(diffo(a, b), expectedOutput); }); test('Array same key of different length 2', function(t){ t.plan(1); var a = { x:1, y: [ 1, { a:1, b:2 }, 3, 4 ] }, b = { a:1, y: [ 1, { a:1, b:1 }, 3, 4 ] }, expectedOutput = { a: 1, y: [ 1, { b:1 }, 3, 4 ] }; t.deepEqual(diffo(a, b), expectedOutput); }); test('Empty arrays are treated as no change', function(t){ t.plan(1); var a = { a:2, x: [] }, b = { a: 1, x: [] }, expectedOutput = { a: 1 }; t.deepEqual(diffo(a, b), expectedOutput); }); test('Arrays with identical objects are not returned', function(t){ t.plan(1); var a = {foo:[{}]}, b = {foo:[{}]}, expectedOutput = {}; t.deepEqual(diffo(a, b), expectedOutput); });
console.log('options.js'); /** * Create an optionsview and bind it to the dom with the config and accounts */ optionsview = new OptionsView({ el: $('#content'), model: config, collection: accounts, }); /** * When both are readdy render the finished UI */ config.on('ready', optionsview.ready); accounts.on('ready', optionsview.ready);
/** * Created by toni on 19.7.2017. */ define([ "components/elementStyles", "utils/eventListeners", "configuration/classNames", "utils/graphUtils", "utils/gwClient", "components/menuItems", "components/nodeForm/nodeForm" ], function (elementStyles, eventListeners, classNames, graphUtils, gwClient, menuItems, nodeForm) { 'use strict'; var name = "ui"; var dispatch; var configs; /** * User interface components. Collection of functions to create UI components. * @exports ui */ /** @function * @name contentContainer * @description Returns the root div container element where to append the rest of the application. * @return {HTMLDivElement} HTML div element. */ function contentContainer(props) { var container = document.createElement('div'); //props.cy = graphUtils.cy(); container.setAttribute('id', configs.containerId); container.classList.add("content-container"); container.appendChild(panel(props)); container.appendChild(graphColumn(props)); return container; } /** @function createPopUp * popup for saving the graphs */ function createPopUp(props) { // naming should be more declarative // the props act as a panelProps // and props.props as props // for the next function var container = d.getElementById('message-container'); var content = menuExtension.render(props); container.appendChild(content); console.debug("createPopup()"); console.debug(props); if (!funcProps.cy) { // todo: refactor this to eventlistener funcProps.cy = props.cy; } console.log(funcProps.context); var divPopup = d.createElement('div'); divPopup.classList.add('popup'); divPopup.setAttribute('id', 'popup'); var content = popup.render(props); divPopup.appendChild(content); document.body.appendChild(divPopup); //var infoBox = d.getElementById('message-container'); //infoBox.appendChild(divPopup); console.debug('popup'); } function destroyPopUp() { var p = d.getElementById('popup'); document.body.removeChild(p); console.debug("closed popup!"); } /** * @function * @name elementsContent * @description Describing * @param {Object} variable - Desc. * @return {Type} desc. */ function elementsContent(props) { try { console.debug("elementsContent props!"); console.debug(props); var content = props.content; var div = document.createElement('div'); div.setAttribute('id', "elements-content"); div.appendChild(elementsFilter(props)); div.appendChild(elementsList(props)); console.debug("returning"); console.debug(div); return div; } catch (e) { console.group("Exception raised by ui.elementsContent()"); console.info("props"); console.info(props); console.warn(e); console.groupEnd(); } } /** * @function * @name createNodeContent * @description Create content for panel tab "Create node" * @param {Object} variable - Desc. * @return {HTMLElement} desc. */ function createNodeContent(props) { try { console.debug("createnodeContent props!"); console.debug(props); // var content = props.content; return nodeForm.render(); } catch (e) { console.group("Exception raised by ui.createnodeContent()"); console.info("props"); console.info(props); console.warn(e); console.groupEnd(); } } /** @function graphColumn * Description * @param {Object} variable - Desc. * @return {Type} desc. */ function graphColumn() { var graphColumnContainer = document.createElement('div'); graphColumnContainer.setAttribute('id', 'graph-column-container'); graphColumnContainer.classList.add("graph-column"); var messageContainer = document.createElement('div'); messageContainer.setAttribute('id', 'message-container'); messageContainer.classList.add('message-container'); var messageText = document.createElement('span'); messageText.setAttribute('id', 'message-text'); messageContainer.appendChild(messageText); var graphContainer = document.createElement('div'); graphContainer.setAttribute('id', configs.graphContainerId); graphContainer.classList.add("graph-container"); var textPrevievContainer = textPreview(); graphColumnContainer.appendChild(messageContainer); graphColumnContainer.appendChild(graphContainer); graphColumnContainer.appendChild(textPrevievContainer); return graphColumnContainer; } /** @function renderGraphsContent * Description * @param {Object} variable - Desc. * @return {Type} desc. */ function graphsContent(props) { /* * Implement graphs tab rendering here * * */ try { var gwClient = props.gwClient; var classes = classNames.tab.graph; var div = document.createElement('div'); var ul = document.createElement('ul'); div.setAttribute('id', classes.container); // todo: implement via eventbus for loose coupling try { var graphListPromise = gwClient.getGraphList(); graphListPromise.then(function (response) { return response.json(); }).then(function (json) { var graphs = json.data; graphs.sort(); /* loop array of graphName strings and generate * the list items for the panel */ graphs.forEach(function (graph) { graphListItem({ cy: props.cy, graphName: graph, gwClient: gwClient, listElement: ul }); }); }); } catch (error) { console.warn(error); } div.appendChild(ul); console.groupEnd(); return div; } catch (e) { console.group("Exception raised by ui.graphsContent()"); console.warn(e); console.groupEnd(); return document.createElement('div'); } } /** @function renderGraphListItem * Description * @param {Object} variable - Desc. * @return {Type} desc. */ function graphListItem(props) { var graphName = props.graphName; var gwClient = props.gwClient; var list = props.listElement; var listItemClass = props.listItemClass; var li = document.createElement('li'); li.classList.add(listItemClass); li.innerHTML = graphName; li.addEventListener('click', function (event) { eventListeners.graphsList.listItem.onClick({ graphName: graphName, gwClient: gwClient, cy: cy }); }); list.appendChild(li); return li; } /** @function renderMenu * Description * @param {Object} variable - Desc. * @return {Element} desc. */ function menu(props) { // setDispatch fn for menuItem interaction with eventProxy // menuItems.setDispatch(dispatch); // Create the div which contains graphingwikiBrowser navigation tabs. var gw = props.gwClient; // css classes var classes = props.classNames.menu; var divMenu = document.createElement('div'); divMenu.classList.add(classes.container); divMenu.id = classes.container; divMenu.classList.add(classes.container); var divToggleMenu = document.createElement('div'); divToggleMenu.innerHTML = '#'; //divMenu.appendChild(divToggleMenu); // console.log(menuItems); var menus = Object.keys(menuItems); console.debug(props); var itemKeys = Object.keys(props.menuItems.items); itemKeys.forEach(function (key) { console.log(key); var item = props.menuItems.items[key]; var div = document.createElement('div'); div.setAttribute('id', "panel-menu__item__" + key); var divContent = document.createElement('div'); divContent.setAttribute('id', 'panel-menu__item__' + item.label.toLowerCase() + '-content'); // Bind an action to the click of the label. div.addEventListener('click', function () { item.onClick({ cy: graphUtils.cy(), gw: gw, msg: item.label + " clicked" }); }); // Show the content. div.addEventListener('mouseover', function () { divContent.classList.add('show'); }); // Hide the content. div.addEventListener('mouseout', function () { divContent.classList.remove('show'); }); div.classList.add('panel-menu-item'); divContent.classList.add('panel-menu-content'); div.innerHTML = item.label; // Put the item content in to the html element. // console.log(item); divContent.appendChild(item.generateContent()); div.appendChild(divContent); // divMenu.appendChild(createPopup(item.label)); divMenu.appendChild(div); }); return divMenu; } /** @function renderElementsfilter * Description * @param {Object} variable - Desc. * @return {Type} desc. */ function elementsFilter(props) { var div = document.createElement('div'); var spanFilter = document.createElement('span'); var btnClearFilter = document.createElement("button"); div.classList.add("element-filter"); btnClearFilter.innerHTML = "ClearFilter"; var inFilter = document.createElement("input"); inFilter.type = "text"; inFilter.placeholder = "Filter..."; inFilter.setAttribute('id', 'filter'); inFilter.classList.add(classNames.tab.elements.filterInput); var filtProps = { divList: document.getElementById('elements-list'), inFilter: inFilter, renderNewContent: elementsList, spanFilter: spanFilter }; btnClearFilter.addEventListener('click', function () { eventListeners.elementsFilter.btnClearFilter.onClick(updateTabs); }); inFilter.addEventListener('keypress', function (event) { console.debug(filtProps); eventListeners.elementsFilter.inFilter.keypress(filtProps); }); div.appendChild(inFilter); //div.appendChild(btnClearFilter); div.appendChild(spanFilter); return div; } /** @function renderElementsList * Description * Todo: Refactor * @param {Object} variable - Desc. * @return {Type} desc. */ function elementsList(props) { try { console.debug("elementList(props)"); console.debug(props); // var content = props.content; var filter = props.filter || ""; var cy = graphUtils.cy(); var gw = gwClient; // which are meant to be used with function getElementIDsToArray(selector) { /* * param eles: cy.elements * return: array of element id strings */ var idArray = []; try { cy.elements(selector).forEach(function (el) { var id = el.id(); // var filter = content.filter.toLowerCase(); var filterIncludes = id.toLowerCase().includes(filter); if (filter == '' || filter == 'undefined') { idArray.push(id); } else { if (filterIncludes) { idArray.push(id); } } }); } catch (e) { console.error(e); } return idArray; } // extract the ids from aforementioned elements var response = dispatch({ action: "ELEMENT_IDS_TO_ARRAY", ctx: this, fn: null, info: "dev test", data: {selector: "node", filter: ""}, target: "graphUtils", source: "ui" }); var nodes = getElementIDsToArray("node"); var edges = getElementIDsToArray("edge"); var div = document.createElement('div'); div.classList.add(classNames.tab.elements.listContainer); var hdNodes = document.createElement('span'); hdNodes.innerHTML = "Pages"; var pNodeNotes = document.createElement('p'); pNodeNotes.innerHTML = "order by degree?"; var hdEdges = document.createElement('span'); hdEdges.innerHTML = "Links"; var ulNodes = unorderedListFromArray({ array: nodes, cy: cy, gw: gw, onClick: eventListeners.elementsList.onClick, onMouseOver: eventListeners.elementsList.onMouseOver, onMouseOut: eventListeners.elementsList.onMouseOut, toggleVisibility: graphUtils.toggleVisibility }); ulNodes.classList.add(classNames.tab.elements.list); var ulEdges = unorderedListFromArray({ cy: cy, array: edges, onClick: eventListeners.elementsList.onClick, onMouseOver: eventListeners.elementsList.onMouseOver, onMouseOut: eventListeners.elementsList.onMouseOut, toggleVisibility: graphUtils.toggleVisibility }); ulEdges.classList.add(classNames.tab.elements.list); div.setAttribute('id', "elements-list"); div.appendChild(hdNodes); div.appendChild(ulNodes); div.appendChild(hdEdges); div.appendChild(ulEdges); return div; } catch (e) { console.group("Exception raised by ui.elementsList()"); console.info("props"); console.info(props); console.warn(e); console.groupEnd(); } } /** @function renderHeaderContainer * Description * @param {Object} variable - Desc. * @return {Type} desc. */ function renderHeaderContainer() { var headerContainer = d.createElement('div'); headerContainer.setAttribute('id', "header-container"); headerContainer.classList.add("header-container"); headerContainer.appendChild(renderHeader()); return headerContainer; } /** * */ function setMenuItems(props) { menuItems = props.menuItems; } // Todo: refactor - too big function! /** * @function * @name stylesContent * @description Create styles tab of the panel. Lists style categories and the parameters with options. * @param {Object} props * @param {Object} props.Variable * @return {HTMLDivElement} styles tab content. */ function stylesContent(props) { /* * Implement style tab rendering here * * styles: { label: "Styles", active: false, styles: [ { categoryName: "category 1", data: "data for 1" }, { categoryName: "category 2", data: "data for 2" } ] } * */ var styles = elementStyles.styles(); var div = document.createElement('div'); div.setAttribute('id', "styles-content"); div.classList.add("styles-content"); var ul = document.createElement('ul'); var cy = graphUtils.cy(); // Todo: make this generic version to work for all of the following use cases function styleSelection(props) { try { var div = document.createElement('div'); var selection = document.createElement('select'); selection.classList.add('style-selection'); selection.setAttribute('id', props.selectionId); // generate options for selection props.options.forEach(function (option) { var opt = document.createElement('option'); opt.setAttribute( 'id', props.attributeId + "-" + option.label ); var current = elementStyles.getStyleObject(props.category)[props.parameter]; opt.innerHTML = option.label; opt.value = option.styleClass; // set selected option to same which is selected for the category if (current === option.styleClass) { opt.selected = true; } selection.appendChild(opt); }); // event listener for selection selection.addEventListener('change', function () { eventListeners.styleList.styleSelection({ baseClass: 'edge', category: props.category, parameter: props.parameter, value: selection.value }); }); div.appendChild(selection); return div; } catch (e) { console.group('Exception raised by styleSelection()'); console.debug("props:"); console.debug(props); console.warn(e); console.groupEnd(); } } /** * @function * @name styleList * @description TELL ABOUT IT * @param props * @returns {HTMLDivElement} */ // Fixme: Too long function! function styleList(props) { var divStyleTable = document.createElement('div'); var tableHeader = document.createElement('span'); tableHeader.innerHTML = props.category; divStyleTable.appendChild(tableHeader); var table = document.createElement('table'); table.classList.add(classNames.tab.styles.selectionTable); var parameters = ["arrowShape", "lineColor", "lineStyle", "lineWidth"]; parameters.forEach(function (parameter) { var row = document.createElement('tr'); var tdLabel = document.createElement('td'); var tdSelection = document.createElement('td'); var paramLabel = document.createElement('label'); paramLabel.innerHTML = parameter; tdLabel.appendChild(paramLabel); var options = configs.styleOptions[parameter]; var values = []; if (typeof options != 'undefined') { values = options.map(function (option) { return option.styleClass; }); } try { var selection = styleSelection({ attributeId: 'select-line-style', category: props.category, selectionId: 'option-line-style', options: options, parameter: parameter }); } catch (e) { console.warn("parameter not found"); } tdSelection.appendChild(selection); row.appendChild(tdLabel); row.appendChild(tdSelection); table.appendChild(row); //divCategory.appendChild(selection); }); divStyleTable.appendChild(table); return divStyleTable; } try { var keys = Object.keys(styles); keys.forEach(function (category) { if (category != 'generic') { var divCategory = styleList({ category: category, style: styles[category] }); div.appendChild(divCategory); } }); } catch (e) { console.group("Exception Styles"); console.warn(e); } console.groupEnd(); return div; } /** @function renderTextPreview * renderTextPreview() * @return {Object} Div element with two child divs */ function textPreview() { var divTextPreviewContainer = document.createElement('div'); divTextPreviewContainer.classList.add(classNames.text.container); divTextPreviewContainer.setAttribute('id', classNames.text.container); var textPreviewHeader = document.createElement('div'); textPreviewHeader.classList.add(classNames.text.header); textPreviewHeader.setAttribute('id', classNames.text.header); var spHeader = document.createElement('span'); spHeader.setAttribute('id', 'header-text'); textPreviewHeader.appendChild(spHeader); var textPreviewContent = document.createElement('div'); textPreviewContent.classList.add(classNames.text.content); textPreviewContent.setAttribute('id', classNames.text.content); divTextPreviewContainer.appendChild(textPreviewHeader); divTextPreviewContainer.appendChild(textPreviewContent); return divTextPreviewContainer; } /** @function renderNavigation * Description * @param {Object} variable - Desc. * @return {Type} desc. */ function navigation(props) { var tabs = props.configs.tabs; // css classes var classes = classNames.tab.nav; var divNav = document.createElement('div'); divNav.classList.add(classes.container); divNav.id = classes.container; var links = Object.keys(tabs); links.forEach(function (key) { var link = tabs[key]; var divLink = document.createElement('div'); divLink.addEventListener('click', function (event) { eventListeners.ui.navClick({ cy: props.cy, configs: props.configs, keyToActivate: key, classNames: props.classNames, updateTabs: updateTabs }); }); if (link.active) { divLink.classList.add(classes.item.item); divLink.classList.add(classes.item.active); } else { divLink.classList.add(classes.item.item); } divLink.innerHTML = link.label; // Fixme: clean the implementation! divLink.setAttribute('id', "nav-item-" + link.key.toLowerCase()); divNav.appendChild(divLink); }); return divNav; } /** @function renderPanel * renderPanel : returns ui panel, wich contain <DESCRIBE> * @param {Object} variable - Desc. * @return {Type} desc. */ function panel(props) { var divPanel = document.createElement('div'); divPanel.setAttribute('id', classNames.panel.container); divPanel.classList.add(classNames.panel.container); var menuDiv = menu({ gwClient: gwClient, classNames: classNames, menuItems: menuItems }); // Todo: clean up temporary refactoring aux. var navProps = props; navProps.configs = configs; navProps.classNames = classNames; navProps.cy = graphUtils.cy(); // console.debug("NavProps!"); // console.debug(navProps); var tabNavDiv = navigation(navProps); // console.debug("TabProps:"); // console.debug(props); props.tabs = props.configs.tabs; var tabsDiv = tabs(props); divPanel.appendChild(menuDiv); divPanel.appendChild(tabNavDiv); divPanel.appendChild(tabsDiv); return divPanel; } /** @function setTextPreviewContent * Description * Todo: TEST! * @param {Object} variable - Desc. * @return {Type} desc. */ function setTextPreviewContent(param) { // set header to reflect the content setTextPreviewHeader(param); var textPromise = gwClient.getPageText(param); var textPreviewContent = document.getElementById(classNames.text.content); console.debug(textPreviewContent); var linkToSite = document.createElement('a'); linkToSite.innerHTML = param; // Todo: use URL from config and use a function to generate the complete path linkToSite.setAttribute('href', "http://localhost/" + param); textPromise.then(function (response) { return response.json(); }).then(function (json) { textPreviewContent.innerHTML = json.data; document.body.appendChild(textPreviewContent); dispatch() return json.data; }); } /** @function setTextPreviewHeader * Description * @param {Object} variable - Desc. * @return {Type} desc. */ function setTextPreviewHeader(headerText) { var spHeader = $('#header-text'); spHeader.innerHTML = headerText; } /** * @function * @name tabs * @description Tab content rendering logic. * @param {Object} prosp * @return {Type} desc. */ function tabs(props) { try { props.cy = graphUtils.cy(); var divContent = document.createElement('div'); divContent.classList.add(classNames.tab.container); divContent.id = classNames.tab.container; props.gwClient = gwClient; if (props.tabs.graphs.active) { divContent.appendChild( graphsContent({ cy: props.cy, gwClient: props.gwClient }) ); } else if (props.tabs.elements.active) { divContent.appendChild(elementsContent(props)); } else if (props.tabs.styles.active) { divContent.appendChild(stylesContent()); } else if (props.tabs.createnode.active) { divContent.appendChild(createNodeContent()); } return divContent; } catch (e) { console.group("Exception raised by tabs()"); console.debug("props"); console.debug(props); console.warn(e); console.groupEnd(); } } /** * @function * @name unorderedListFromArray * @description Create unordered list of Cytoscape elements and bind event listeners to list elements. * @param {Object} props * @param {Type} props.PROP * @return {HTMLUListElement} list of elements with event listeners */ function unorderedListFromArray(props) { /* * Todo: get event listeners from eventListeners * array: array of string items * return: unordered html element with list items * from the array * */ var cy = props.cy; var array = props.array; array.sort(); var ul = document.createElement('ul'); array.forEach(function (listElementId) { var li = document.createElement('li'); var checkBox = document.createElement('input'); checkBox.setAttribute('id', 'visibility_' + listElementId); checkBox.setAttribute('type', 'checkbox'); var elementHidden = cy.getElementById(listElementId).hidden() checkBox.setAttribute('checked', elementHidden); checkBox.checked = true; //console.log(checkBox); checkBox.addEventListener('click', function (event) { console.log(event.target); graphUtils.toggleVisibility(event.target); console.log(event.target.id); }); li.appendChild(checkBox); li.innerHTML += listElementId; li.setAttribute('id', listElementId); li.addEventListener('mouseover', function (evt) { props.onMouseOver({ cy: cy, listItemId: evt.target.id }); }); li.addEventListener('mouseout', function (evt) { props.onMouseOut({ cy: cy, listItemId: evt.target.id }); }); li.addEventListener('click', function (evt) { props.onClick({ evt: evt, elementId: listElementId, currentDetail: props.currentDetail, classToToggle: classNames.tab.elements.listItem.selected, setTextPreview: function () { setTextPreviewContent(evt.target.id); } }); }); ul.appendChild(li); }); return ul; } /** * @function * @name updateTabs * @description Called when tab links are clicked. Removes content to make room for the tab clicked. * Updates tab content by calling tabs(). * @param {Object} props * @return {Type} to be refactored. */ function updateTabs(props) { try { console.log('updateTabs()'); var divTabContainer = document.getElementById(classNames.tab.container); var panelContainer = document.getElementById(classNames.panel.container); var childsToRemove = divTabContainer.childNodes; childsToRemove.forEach(function (child) { divTabContainer.remove(child); }); var tabsContent = tabs({ cy: props.cy, tabs: configs.tabs }); panelContainer.appendChild(tabsContent); } catch (e) { console.group("Exception raised by ui.updateTabs()"); console.info(props); console.warn(e); console.groupEnd(); } } var dispatchActions = { ELEMENTS_LIST: elementsList, UPDATE_TABS: updateTabs, trigger: function (props) { this[props.action](props.data); } }; return { contentContainer: contentContainer, elementsContent: elementsContent, elementsFilter: elementsFilter, graphsContent: graphsContent, graphListItem: graphListItem, menu: menu, name: function () { return name; }, navigation: navigation, setMenuItems: setMenuItems, stylesContent: stylesContent, tabs: tabs, textPreview: textPreview, updateTabs: updateTabs, info: function () { return { name: "UI", description: "user interface components." }; }, setConfigs: function (props) { configs = props.configs; }, setDispatch: function (fn) { dispatch = fn; dispatch({ action: "CONFIRM_SUBSCRIPTION", ctx: this, target: "eventProxy", source: "ui", fn: null, info: "dev test" }); }, triggerEvent: function (props) { console.log(props); return dispatchActions.trigger(props); } } } );
/* http://keith-wood.name/datepick.html Datepicker Validation extension for jQuery 4.0.6. Requires Jörn Zaefferer's Validation plugin (http://plugins.jquery.com/project/validate). Written by Keith Wood (kbwood{at}iinet.com.au). Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. Please attribute the author if you use it. */ (function($) { // Hide the namespace /* Add validation methods if validation plugin available. */ if ($.fn.validate) { $.datepick.selectDateOrig = $.datepick.selectDate; $.extend($.datepick.regional[''], { validateDate: 'Please enter a valid date', validateDateMin: 'Please enter a date on or after {0}', validateDateMax: 'Please enter a date on or before {0}', validateDateMinMax: 'Please enter a date between {0} and {1}', validateDateCompare: 'Please enter a date {0} {1}', validateDateToday: 'today', validateDateOther: 'the other date', validateDateEQ: 'equal to', validateDateNE: 'not equal to', validateDateLT: 'before', validateDateGT: 'after', validateDateLE: 'not after', validateDateGE: 'not before' }); $.extend($.datepick._defaults, $.datepick.regional['']); $.extend($.datepick, { /* Trigger a validation after updating the input field with the selected date. @param target (element) the control to examine @param elem (element) the selected datepicker element */ selectDate: function(target, elem) { this.selectDateOrig(target, elem); var inst = $.data(target, $.datepick.dataName); if (!inst.inline && $.fn.validate) { var validation = $(target).parents('form').validate(); if (validation) { validation.element('#' + target.id); } } }, /* Correct error placement for validation errors - after any trigger. @param error (jQuery) the error message @param element (jQuery) the field in error */ errorPlacement: function(error, element) { var inst = $.data(element[0], $.datepick.dataName); if (inst) { error[inst.get('isRTL') ? 'insertBefore' : 'insertAfter']( inst.trigger.length > 0 ? inst.trigger : element); } else { error.insertAfter(element); } }, /* Format a validation error message involving dates. @param source (string) the error message @param params (Date[]) the dates @return (string) the formatted message */ errorFormat: function(source, params) { var format = ($.datepick.curInst ? $.datepick.curInst.get('dateFormat') : $.datepick._defaults.dateFormat); $.each(params, function(index, value) { source = source.replace(new RegExp('\\{' + index + '\\}', 'g'), $.datepick.formatDate(format, value) || 'nothing'); }); return source; } }); var lastElement = null; /* Validate date field. */ $.validator.addMethod('dpDate', function(value, element) { lastElement = element; return this.optional(element) || validateEach(value, element); }, function(params) { var inst = $.data(lastElement, $.datepick.dataName); var minDate = inst.get('minDate'); var maxDate = inst.get('maxDate'); var messages = $.datepick._defaults; return (minDate && maxDate ? $.datepick.errorFormat(messages.validateDateMinMax, [minDate, maxDate]) : (minDate ? $.datepick.errorFormat(messages.validateDateMin, [minDate]) : (maxDate ? $.datepick.errorFormat(messages.validateDateMax, [maxDate]) : messages.validateDate))); }); /* Apply a validation test to each date provided. @param value (string) the current field value @param element (element) the field control @return (boolean) true if OK, false if failed validation */ function validateEach(value, element) { var inst = $.data(element, $.datepick.dataName); var rangeSelect = inst.get('rangeSelect'); var multiSelect = inst.get('multiSelect'); var dates = (multiSelect ? value.split(inst.get('multiSeparator')) : (rangeSelect ? value.split(inst.get('rangeSeparator')) : [value])); var ok = (multiSelect && dates.length <= multiSelect) || (!multiSelect && rangeSelect && dates.length == 2) || (!multiSelect && !rangeSelect && dates.length == 1); if (ok) { try { var dateFormat = inst.get('dateFormat'); var minDate = inst.get('minDate'); var maxDate = inst.get('maxDate'); var dp = $(element); $.each(dates, function(i, v) { dates[i] = $.datepick.parseDate(dateFormat, v); ok = ok && (!dates[i] || (dp.datepick('isSelectable', dates[i]) && (!minDate || dates[i].getTime() >= minDate.getTime()) && (!maxDate || dates[i].getTime() <= maxDate.getTime()))); }); } catch (e) { ok = false; } } if (ok && rangeSelect) { ok = (dates[0].getTime() <= dates[1].getTime()); } return ok; } /* And allow as a class rule. */ $.validator.addClassRules('dpDate', {dpDate: true}); var comparisons = {equal: 'eq', same: 'eq', notEqual: 'ne', notSame: 'ne', lessThan: 'lt', before: 'lt', greaterThan: 'gt', after: 'gt', notLessThan: 'ge', notBefore: 'ge', notGreaterThan: 'le', notAfter: 'le'}; /* Cross-validate date fields. params should be an array with [0] comparison type eq/ne/lt/gt/le/ge or synonyms, [1] 'today' or date string or Date or other field selector/element/jQuery OR an object with one attribute with name eq/ne/lt/gt/le/ge or synonyms and value 'today' or date string or Date or other field selector/element/jQuery OR a string with eq/ne/lt/gt/le/ge or synonyms followed by 'today' or date string or jQuery selector */ $.validator.addMethod('dpCompareDate', function(value, element, params) { if (this.optional(element)) { return true; } params = normaliseParams(params); var thisDate = $(element).datepick('getDate'); var thatDate = extractOtherDate(element, params[1]); if (thisDate.length == 0 || thatDate.length == 0) { return true; } lastElement = element; var finalResult = true; for (var i = 0; i < thisDate.length; i++) { switch (comparisons[params[0]] || params[0]) { case 'eq': finalResult = (thisDate[i].getTime() == thatDate[0].getTime()); break; case 'ne': finalResult = (thisDate[i].getTime() != thatDate[0].getTime()); break; case 'lt': finalResult = (thisDate[i].getTime() < thatDate[0].getTime()); break; case 'gt': finalResult = (thisDate[i].getTime() > thatDate[0].getTime()); break; case 'le': finalResult = (thisDate[i].getTime() <= thatDate[0].getTime()); break; case 'ge': finalResult = (thisDate[i].getTime() >= thatDate[0].getTime()); break; default: finalResult = true; } if (!finalResult) { break; } } return finalResult; }, function(params) { var messages = $.datepick._defaults; var inst = $.data(lastElement, $.datepick.dataName); params = normaliseParams(params); var thatDate = extractOtherDate(lastElement, params[1], true); thatDate = (params[1] == 'today' ? messages.validateDateToday : (thatDate.length ? $.datepick.formatDate(inst.get('dateFormat'), thatDate[0], inst.getConfig()) : messages.validateDateOther)); return messages.validateDateCompare.replace(/\{0\}/, messages['validateDate' + (comparisons[params[0]] || params[0]).toUpperCase()]). replace(/\{1\}/, thatDate); }); /* Normalise the comparison parameters to an array. @param params (array or object or string) the original parameters @return (array) the normalised parameters */ function normaliseParams(params) { if (typeof params == 'string') { params = params.split(' '); } else if (!$.isArray(params)) { var opts = []; for (var name in params) { opts[0] = name; opts[1] = params[name]; } params = opts; } return params; } /* Determine the comparison date. @param element (element) the current datepicker element @param source (string or Date or jQuery or element) the source of the other date @param noOther (boolean) true to not get the date from another field @return (Date[1]) the date for comparison */ function extractOtherDate(element, source, noOther) { if (source.constructor == Date) { return [source]; } var inst = $.data(element, $.datepick.dataName); var thatDate = null; try { if (typeof source == 'string' && source != 'today') { thatDate = $.datepick.parseDate(inst.get('dateFormat'), source, inst.getConfig()); } } catch (e) { // Ignore } thatDate = (thatDate ? [thatDate] : (source == 'today' ? [$.datepick.today()] : (noOther ? [] : $(source).datepick('getDate')))); return thatDate; } } })(jQuery);
/* global KkSimple */ KkSimple.Item = function Item(itemData, map, x, y) { KkSimple.Unit.call(this, itemData.ch, map, x, y); this._code = itemData.code; }; KkSimple.Item.prototype = Object.create(KkSimple.Unit.prototype); KkSimple.Item.prototype.constructor = KkSimple.Item; KkSimple.Item.prototype.getCode = function getCode() { return this._code; }; //Item data KkSimple.items = { krystal: { ch: '*' } }; //Automatically add code string to each item data for (var itemCode in KkSimple.items) KkSimple.items[itemCode].code = itemCode;
require('./proof')(2, prove) function prove (async, okay) { var strata = createStrata({ directory: tmp, leafSize: 3, branchSize: 3 }) async(function () { serialize(__dirname + '/fixtures/leaf-three.before.json', tmp, async()) }, function () { strata.open(async()) }, function () { strata.mutator('b', async()) }, function (cursor) { cursor.insert('b', 'b', ~cursor.index) cursor.unlock(async()) }, function () { gather(strata, async()) }, function (records) { okay(records, [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ], 'records') strata.balance(async()) }, function () { vivify(tmp, async()) load(__dirname + '/fixtures/leaf-three.after.json', async()) }, function (actual, expected) { okay(actual, expected, 'split') }, function() { strata.close(async()) }) }
Flame.LabelView = Flame.View.extend(Flame.ActionSupport, { layout: { left: 0, top: 0 }, classNames: ['flame-label-view'], classNameBindings: ['textAlign', 'isSelectable'], defaultHeight: 22, defaultWidth: 200, isSelectable: false, handlebars: '{{value}}', render: function(buffer) { var height = this.getPath('layout.height'); if (this.get('useAbsolutePosition') && !Ember.none(height)) buffer.style('line-height', height+'px'); this._super(buffer); }, mouseDown: function(evt) { return this.fireAction(); }, // We should never let mouseUp propagate. If we handled mouseDown, we will receive mouseUp and obviously // it shouldn't be propagated. If we didn't handle mouseDown (there was no action), it was propagated up // and the mouse responder logic will relay mouseUp directly to the view that handler mouseDown. mouseUp: function(evt) { return true; } }); Flame.LabelView.reopenClass({ // Shortcut for creating label views with a static label label: function(value, left, top, width, height) { return Flame.LabelView.extend({ layout: { left: left, top: top, width: width, height: height }, value: value }); }, // Shortcut for creating label views using a binding binding: function(valueBinding, left, top, width, height) { return Flame.LabelView.extend({ layout: { left: left, top: top, width: width, height: height }, valueBinding: valueBinding }); } });
var MashThickness, VolSparge, PreviousSparge, PreviousThickness; $(document).ready(function() { $('#calcForm').delegate('input[type=text]', 'change', function() { if (validateField(this)) { updateCalc(); } }).submit(function(e) { e.defaultPrevented; updateCalc(); }); $('#Volume').focus(); }); function allFieldsValid() { var fields = [ 'VolSparge', 'MashThickness', ]; for (i = 0; i < fields.length; i++) { if (!$('#' + fields[i]).val().match(/^d*(.\d+)?/)) { return false; } } return true; } function byId(id) { return (document.getElementById(id)); } function updateCalc() { if (!allFieldsValid()) { return; }//Parse input variables MashThickness = parseFloat($('#MashThickness').val()); VolSparge = parseFloat($('#VolSparge').val()); //do math if (MashThickness != PreviousThickness) { VolSparge = MashThickness * 2; byId("VolSparge").value = VolSparge.toFixed(2); PreviousSparge = VolSparge.toFixed(2); } if (VolSparge != PreviousSparge) { MashThickness = VolSparge / 2; byId("MashThickness").value = MashThickness.toFixed(3); PreviousThickness = MashThickness.toFixed(3); PreviousSparge = VolSparge.toFixed(2); } if (VolSparge <= 0) { VolSparge = 0; byId("VolSparge").value = VolSparge.toFixed(2); MashThickness = VolSparge / 2; byId("MashThickness").value = MashThickness.toFixed(3); PreviousThickness = MashThickness.toFixed(3); PreviousSparge = 0; } PreviousSparge = VolSparge; PreviousThickness = MashThickness; } function validateField(field) { $field = $(field); if ($field.val().match(/^d*(.\d+)?/)) { $field.parents('div.control-group:first').removeClass('error'); return true; } $field.parents('div.control-group:first').addClass('error'); return false; } window.onload = updateCalc();
/* jslint node: true */ 'use strict'; var TraversalTracker = require('./traversalTracker'); /** * Creates an instance of DocumentContext - a store for current x, y positions and available width/height. * It facilitates column divisions and vertical sync */ function DocumentContext(pageSize, pageMargins) { this.pages = []; this.pageMargins = pageMargins; this.x = pageMargins.left; this.availableWidth = pageSize.width - pageMargins.left - pageMargins.right; this.availableHeight = 0; this.imageFloating = false; this.page = -1; this.snapshots = []; this.imageFloatSnapshots = []; this.endingCell = null; this.tracker = new TraversalTracker(); this.addPage(pageSize); } DocumentContext.prototype.pushSnapshot = function(snapshots) { var snapshot = { x: this.x, y: this.y, availableHeight: this.availableHeight, availableWidth: this.availableWidth, imageFloating: this.imageFloating, page: this.page, bottomMost: {y: this.y, page: this.page}, endingCell: this.endingCell, lastColumnWidth: this.lastColumnWidth }; if (this.imageFloating) { snapshot.availableFloatHeight = this.availableFloatHeight; snapshot.floatHeight = this.floatHeight; } snapshots.push(snapshot); }; DocumentContext.prototype.restoreSnapshot = function(snapshots) { var snapshot = snapshots.pop(); this.x = snapshot.x; this.y = snapshot.y; this.availableWidth = snapshot.availableWidth; this.imageFloating = snapshot.imageFloating; if (snapshot.imageFloating) { this.availableFloatHeight = snapshot.availableFloatHeight; this.floatHeight = snapshot.floatHeight; } else { this.page = snapshot.page; this.availableFloatHeight = undefined; } this.bottomMost = snapshot.bottomMost; this.endingCell = snapshot.endingCell; this.lastColumnWidth = snapshot.lastColumnWidth; }; DocumentContext.prototype.beginColumnGroup = function() { this.pushSnapshot(this.snapshots); this.lastColumnWidth = 0; }; DocumentContext.prototype.beginColumn = function (width, offset, endingCell) { var saved = this.snapshots[this.snapshots.length - 1]; this.calculateBottomMost(saved); this.endingCell = endingCell; this.page = saved.page; this.x = this.x + this.lastColumnWidth + (offset || 0); this.y = saved.y; this.availableWidth = width; //saved.availableWidth - offset; this.availableHeight = saved.availableHeight; this.lastColumnWidth = width; }; DocumentContext.prototype.endColumn = function() { this.stopImageFloating(); }; DocumentContext.prototype.calculateBottomMost = function(destContext) { if (this.endingCell) { this.saveContextInEndingCell(this.endingCell); this.endingCell = null; } else { destContext.bottomMost = bottomMostContext(this, destContext.bottomMost); } }; DocumentContext.prototype.markEnding = function (endingCell) { this.page = endingCell._columnEndingContext.page; this.x = endingCell._columnEndingContext.x; this.y = endingCell._columnEndingContext.y; this.availableWidth = endingCell._columnEndingContext.availableWidth; this.availableHeight = endingCell._columnEndingContext.availableHeight; this.lastColumnWidth = endingCell._columnEndingContext.lastColumnWidth; }; DocumentContext.prototype.saveContextInEndingCell = function (endingCell) { endingCell._columnEndingContext = { page: this.page, x: this.x, y: this.y, availableHeight: this.availableHeight, availableWidth: this.availableWidth, lastColumnWidth: this.lastColumnWidth }; }; DocumentContext.prototype.completeColumnGroup = function () { if (this.imageFloating) { this.stopImageFloating(); } var saved = this.snapshots.pop(); this.calculateBottomMost(saved); this.endingCell = null; this.x = saved.x; this.y = saved.bottomMost.y; this.page = saved.bottomMost.page; this.availableWidth = saved.availableWidth; this.availableHeight = saved.bottomMost.availableHeight; this.lastColumnWidth = saved.lastColumnWidth; }; DocumentContext.prototype.addMargin = function (left, right) { this.x += left; this.availableWidth -= left + (right || 0); }; DocumentContext.prototype.moveDownImageFloat = function (offset) { if (this.availableFloatHeight > offset) { this.availableFloatHeight -= offset; } else { var beforeRestore = { y: this.y, availableFloatHeight: this.availableFloatHeight }; this.restoreSnapshot(this.imageFloatSnapshots); this.y = Math.max(beforeRestore.y, this.y); var remainingOffsetAfterImplicitMoveDownByRestoreSnapshot = offset - beforeRestore.availableFloatHeight; if (this.imageFloating) { this.moveDownImageFloat(remainingOffsetAfterImplicitMoveDownByRestoreSnapshot); } else { this.availableHeight -= remainingOffsetAfterImplicitMoveDownByRestoreSnapshot; } } }; DocumentContext.prototype.moveDown = function(offset) { this.y += offset; if (this.imageFloating) { this.moveDownImageFloat(offset); } else { this.availableHeight -= offset; } return this.availableHeight > 0; }; DocumentContext.prototype.initializePage = function() { var self = this; function clearImageFloating() { self.x = self.imageFloatSnapshots[0].x; self.imageFloatSnapshots = []; self.imageFloating = false; } this.y = this.pageMargins.top; this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom; this.pageSnapshot().availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right; if (this.imageFloating) { clearImageFloating(); } }; DocumentContext.prototype.pageSnapshot = function () { if (this.snapshots[0]) { return this.snapshots[0]; } else { return this; } }; DocumentContext.prototype.stopImageFloating = function() { var bottomMargin = this.currentMargin ? this.currentMargin[3] : 0; var offset; var yMax = this.y + this.availableFloatHeight || 0; var floatingAlreadyStopped = this.imageFloatSnapshots.length <= 0; if(! floatingAlreadyStopped){ while(this.imageFloatSnapshots.length > 0) { this.restoreSnapshot(this.imageFloatSnapshots); yMax = Math.max(yMax, this.y + this.floatHeight || 0); } offset = yMax - this.y; this.availableHeight += offset; // availableHeight will be adjusted in one step by moveDown this.moveDown(offset + bottomMargin); } this.imageFloating = false; this.availableFloatHeight = 0; this.floatHeight = 0; this.currentMargin = undefined; }; DocumentContext.prototype.beginImageFloating = function(imageWidth, imageHeight, floatGap, floatMax) { if(imageWidth <= (this.availableWidth * floatMax)) { if (this.imageFloating) { if (imageHeight > this.availableFloatHeight) { this.availableHeight -= (imageHeight - this.availableFloatHeight); } this.availableFloatHeight -= imageHeight; } else { this.availableHeight -= imageHeight; } this.pushSnapshot(this.imageFloatSnapshots); this.imageFloating = true; this.floatHeight = imageHeight; this.availableFloatHeight = imageHeight; var moveRight = imageWidth + floatGap; this.availableWidth -= moveRight; this.x += moveRight; } else { this.moveDown(imageHeight); } }; DocumentContext.prototype.moveTo = function (x, y) { if (x !== undefined && x !== null) { this.x = x; this.availableWidth = this.getCurrentPage().pageSize.width - this.x - this.pageMargins.right; } if (y !== undefined && y !== null) { this.y = y; this.availableHeight = this.getCurrentPage().pageSize.height - this.y - this.pageMargins.bottom; } }; DocumentContext.prototype.beginDetachedBlock = function () { this.snapshots.push({ x: this.x, y: this.y, availableHeight: this.availableHeight, availableWidth: this.availableWidth, page: this.page, endingCell: this.endingCell, lastColumnWidth: this.lastColumnWidth }); }; DocumentContext.prototype.endDetachedBlock = function () { var saved = this.snapshots.pop(); this.x = saved.x; this.y = saved.y; this.availableWidth = saved.availableWidth; this.availableHeight = saved.availableHeight; this.page = saved.page; this.endingCell = saved.endingCell; this.lastColumnWidth = saved.lastColumnWidth; }; function pageOrientation(pageOrientationString, currentPageOrientation) { if (pageOrientationString === undefined) { return currentPageOrientation; } else if (pageOrientationString === 'landscape') { return 'landscape'; } else { return 'portrait'; } } var getPageSize = function (currentPage, newPageOrientation) { newPageOrientation = pageOrientation(newPageOrientation, currentPage.pageSize.orientation); if (newPageOrientation !== currentPage.pageSize.orientation) { return { orientation: newPageOrientation, width: currentPage.pageSize.height, height: currentPage.pageSize.width }; } else { return { orientation: currentPage.pageSize.orientation, width: currentPage.pageSize.width, height: currentPage.pageSize.height }; } }; DocumentContext.prototype.moveToNextPage = function (pageOrientation) { var nextPageIndex = this.page + 1; var prevPage = this.page; var prevY = this.y + (this.imageFloating ? this.availableFloatHeight : 0); var createNewPage = nextPageIndex >= this.pages.length; if (createNewPage) { this.addPage(getPageSize(this.getCurrentPage(), pageOrientation)); } else { this.page = nextPageIndex; this.initializePage(); } return { newPageCreated: createNewPage, prevPage: prevPage, prevY: prevY, y: this.y }; }; DocumentContext.prototype.addPage = function (pageSize) { var page = {items: [], pageSize: pageSize}; this.pages.push(page); this.page = this.pages.length - 1; this.initializePage(); this.tracker.emit('pageAdded'); return page; }; DocumentContext.prototype.getCurrentPage = function () { if (this.page < 0 || this.page >= this.pages.length) { return null; } return this.pages[this.page]; }; DocumentContext.prototype.getCurrentPosition = function () { var pageSize = this.getCurrentPage().pageSize; var innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom; var innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right; return { pageNumber: this.page + 1, pageOrientation: pageSize.orientation, pageInnerHeight: innerHeight, pageInnerWidth: innerWidth, left: this.x, top: this.y, verticalRatio: ((this.y - this.pageMargins.top) / innerHeight), horizontalRatio: ((this.x - this.pageMargins.left) / innerWidth) }; }; function bottomMostContext(c1, c2) { var r; if (c1.page > c2.page) { r = c1; } else if (c2.page > c1.page) { r = c2; } else { r = (c1.y > c2.y) ? c1 : c2; } return { page: r.page, x: r.x, y: r.y, availableHeight: r.availableHeight, availableWidth: r.availableWidth }; } /****TESTS**** (add a leading '/' to uncomment) DocumentContext.bottomMostContext = bottomMostContext; // */ module.exports = DocumentContext;
define(function(require, exports, module) { var $ = require("$"), $window = $(window), $document = $(document), ie6 = $.browser.msie && $.browser.version == "6.0"; var loadingHtml = '<div style="width:200px;height:100px;background:url(http://cn-style.gcimg.net/static/work/global/1.0.1/loading.gif) 50% 50% no-repeat;"></div>'; require('./x-style.css'); function XDialog() { this._getDOM(); this._attachE(); } XDialog.prototype = { init: function(config) { this.config = config; this.DOM.main.attr('id', config.id ? config.id : ''); this.DOM.main.css('padding', config.borderWidth); this.DOM.content.css({'width': config.width, 'height': config.height}); this.DOM.close[config.close ? 'show' : 'hide'](); this.title(config.title); this.button(config.button); this.content(config.content); config.mask ? this.showMask() : this.hideMask(); }, title: function(t) { t === false ? this.DOM.title.empty().hide() : this.DOM.title.html(t).show(); }, content: function(s) { this.DOM.content.html(s.length ? s : loadingHtml); this.DOM.main.show(); this.center(); return this; }, button: function(args) { args = args || []; var that = this; var html = ''; this.config._callbacks = []; this.DOM.footer[args.length ? 'show' : 'hide'](); if (typeof args === 'string') { html = args; } else { $.each(args, function (i, val) { that.config._callbacks[i] = val.callback || ''; html += '<a href="javascript:;" class="x-dialog-button'+ (val.focus ? ' x-button-hilight' : '') +'">'+ val.text +'</a>'; }); } this.DOM.footer.html(html); return this; }, hide: function() { this.DOM.main.hide(); this.hideMask(); }, center: function() { var dom = this.DOM, main = dom.main[0], fixed = this.config.fixed, dl = !fixed ? 0 : $document.scrollLeft(), dt = !fixed ? 0 : $document.scrollTop(), ww = $window.width(), wh = $window.height(), ow = main.offsetWidth, oh = main.offsetHeight, left = (ww - ow) / 2 + dl, top = (wh - oh) * 382 / 1000 + dt,// 黄金比例 style = main.style; style.left = Math.max(parseInt(left), dl) + 'px'; style.top = Math.max(parseInt(top), dt) + 'px'; return this; }, showMask: function() { this.DOM.mask.show(); if (ie6) { this.DOM.mask.height($("body").height()); } }, hideMask: function() { this.DOM.mask.hide(); }, _getDOM: function() { var that = this, _DOM = {}, str = '<div class="x-dialog-mask"></div>' + '<div class="x-dialog-main">' + '<div class="x-dialog-inner">' + '<a class="x-dialog-close" href="javascript:;">×</a>' + '<table class="x-dialog-table">' + '<tr>' + '<td class="x-dialog-cell">' + '<div class="x-dialog-title"></div>' + '<div class="x-dialog-content"></div>' + '<div class="x-dialog-footer"></div>' + '</td>' + '</tr>' + '</table>' + '</div>' + '</div>'; $(str).appendTo("body").hide(); // 获取窗口各个节点 _DOM["mask"] = $(".x-dialog-mask"); _DOM["main"] = $(".x-dialog-main"); _DOM["title"] = $(".x-dialog-title"); _DOM["close"] = $(".x-dialog-close"); _DOM["content"] = $(".x-dialog-content"); _DOM["footer"] = $(".x-dialog-footer"); this.DOM = _DOM; }, _attachE: function() { var that = this; this.DOM.close.bind("click", function() { that.hide(); return false; }); $window.bind('resize scroll', function() { that.center(); }); this.DOM.main.on('click', '.x-dialog-button', function (e) { var $this = $(this), index = $this.index('.x-dialog-button'); that._trigger(index); e.preventDefault(); }); }, _trigger: function(i) { var fn = this.config._callbacks[i]; return typeof fn !== 'function' || fn.call(this) !== false ? this.hide(): this; } }; module.exports = { // 默认配置 _default: { id: "", title: "标题", content: loadingHtml, mask: true, close: true, width: "", height: "", fixed: false, borderWidth: '5px', button: [] }, show: function(opts) { var dialog = this._getInstance(), conf = $.extend({}, this._default, opts); // 传入配置和默认配置合并(覆盖上一次配置) // 应用配置初始化窗口 dialog.init(conf); return dialog; }, // 只被实例化一次 _getInstance: function() { return this.instance || (this.instance = new XDialog()); } }; });
// Test geo.polygonFeature and geo.gl.polygonFeature var $ = require('jquery'); var geo = require('../test-utils').geo; var mockVGLRenderer = geo.util.mockVGLRenderer; var restoreVGLRenderer = geo.util.restoreVGLRenderer; var vgl = require('vgl'); var waitForIt = require('../test-utils').waitForIt; // var closeToArray = require('../test-utils').closeToArray; describe('geo.polygonFeature', function () { 'use strict'; var testPolygons = [ [{x: 0, y: 0}, {x: 10, y: 5}, {x: 5, y: 10}], { outer: [{x: 20, y: 10}, {x: 30, y: 0}, {x: 40, y: 10}, {x: 30, y: 20}], inner: [ [{x: 25, y: 10}, {x: 30, y: 5}, {x: 35, y: 10}, {x: 30, y: 15}] ], fillOpacity: 0.5 }, { outer: [{x: 50, y: 10}, {x: 60, y: 0}, {x: 70, y: 10}, {x: 60, y: 20}], inner: [ [{x: 55, y: 10}, {x: 60, y: 15}, {x: 65, y: 10}, {x: 60, y: 5}] ], fillColor: '#FF8000', strokeColor: '#008' }, { outer: [{x: 50, y: 8}, {x: 70, y: 8}, {x: 70, y: 12}, {x: 50, y: 12}], inner: [ [{x: 58, y: 10}, {x: 60, y: 15}, {x: 62, y: 10}, {x: 60, y: 5}] ], uniformPolygon: true } ]; var stylesVisited = []; var testStyle = { fillOpacity: function (d, idx, poly, polyidx) { stylesVisited.push({style: 'fillOpacity', params: [d, idx, poly, polyidx]}); return poly.fillOpacity !== undefined ? poly.fillOpacity : 1; }, fillColor: function (d, idx, poly, polyidx) { stylesVisited.push({style: 'fillColor', params: [d, idx, poly, polyidx]}); return poly.fillColor !== undefined ? poly.fillColor : 'blue'; }, stroke: true, strokeColor: function (d, idx, poly, polyidx) { stylesVisited.push({style: 'strokeColor', params: [d, idx, poly, polyidx]}); return poly.strokeColor !== undefined ? poly.strokeColor : 'red'; }, uniformPolygon: function (d) { stylesVisited.push({style: 'uniformPolygon', params: [d]}); return d.uniformPolygon !== undefined ? d.uniformPolygon : false; } }; function create_map(opts) { var node = $('<div id="map"/>').css({width: '640px', height: '360px'}); $('#map').remove(); $('body').append(node); opts = $.extend({}, opts); opts.node = node; return geo.map(opts); } describe('create', function () { it('create function', function () { mockVGLRenderer(); var map, layer, polygon; map = create_map(); layer = map.createLayer('feature', {renderer: 'vgl'}); polygon = geo.polygonFeature.create(layer); expect(polygon instanceof geo.polygonFeature).toBe(true); restoreVGLRenderer(); }); }); describe('Check class accessors', function () { var map, layer, polygon; var pos = [[[0, 0], [10, 5], [5, 10]]]; it('position', function () { map = create_map(); layer = map.createLayer('feature', {renderer: null}); polygon = geo.polygonFeature({layer: layer}); polygon._init(); expect(polygon.position()('a')).toBe('a'); polygon.position(pos); expect(polygon.position()).toEqual(pos); polygon.position(function () { return 'b'; }); expect(polygon.position()('a')).toEqual('b'); polygon = geo.polygonFeature({layer: layer}); polygon._init({position: pos}); expect(polygon.position()).toEqual(pos); }); it('polygon', function () { map = create_map(); layer = map.createLayer('feature', {renderer: null}); polygon = geo.polygonFeature({layer: layer}); polygon._init(); expect(polygon.polygon()('a')).toBe('a'); polygon.polygon(pos); expect(polygon.polygon()).toEqual(pos); polygon.polygon(function () { return 'b'; }); expect(polygon.polygon()('a')).toEqual('b'); polygon = geo.polygonFeature({layer: layer}); polygon._init({polygon: pos}); expect(polygon.polygon()).toEqual(pos); }); it('data', function () { map = create_map(); layer = map.createLayer('feature', {renderer: null}); polygon = geo.polygonFeature({layer: layer}); polygon._init(); expect(polygon.data()).toEqual([]); polygon.data(pos); expect(polygon.data()).toEqual(pos); polygon = geo.polygonFeature({layer: layer}); polygon._init({style: {data: pos}}); expect(polygon.data()).toEqual(pos); }); it('style', function () { mockVGLRenderer(); map = create_map(); // we have to use a valid renderer so that the stroke can be enabled. layer = map.createLayer('feature', {renderer: 'vgl'}); polygon = geo.polygonFeature({layer: layer}); polygon._init(); expect(polygon.style().stroke).toBe(false); expect(polygon.dependentFeatures()).toEqual([]); polygon.style('stroke', true); expect(polygon.style().stroke).toBe(true); expect(polygon.dependentFeatures().length).toEqual(1); polygon.style({stroke: false}); expect(polygon.style().stroke).toBe(false); expect(polygon.dependentFeatures()).toEqual([]); map.deleteLayer(layer); restoreVGLRenderer(); }); }); describe('Public utility methods', function () { describe('pointSearch', function () { it('basic usage', function () { var map, layer, polygon, data, pt; map = create_map(); layer = map.createLayer('feature', {renderer: null}); polygon = geo.polygonFeature({layer: layer}); polygon._init(); data = testPolygons; polygon.data(data); pt = polygon.pointSearch({x: 5, y: 5}); expect(pt.index).toEqual([0]); expect(pt.found.length).toBe(1); expect(pt.found[0][0]).toEqual(data[0][0]); pt = polygon.pointSearch({x: 21, y: 10}); expect(pt.index).toEqual([1]); expect(pt.found.length).toBe(1); pt = polygon.pointSearch({x: 30, y: 10}); expect(pt.index).toEqual([]); expect(pt.found.length).toBe(0); pt = polygon.pointSearch({x: 51, y: 10}); expect(pt.index).toEqual([2, 3]); expect(pt.found.length).toBe(2); pt = polygon.pointSearch({x: 57, y: 10}); expect(pt.index).toEqual([3]); expect(pt.found.length).toBe(1); /* If the inner hole extends past the outside, it doesn't make that * point in the polygon */ pt = polygon.pointSearch({x: 60, y: 13}); expect(pt.index).toEqual([]); expect(pt.found.length).toBe(0); }); }); }); describe('Private utility methods', function () { describe('_init', function () { var map, layer; it('arg gets added to style', function () { var polygon; map = create_map(); layer = map.createLayer('feature', {renderer: null}); polygon = geo.polygonFeature({layer: layer}); /* init is not automatically called on the geo.polygonFeature (it is on * geo.gl.polygonFeature). */ polygon._init({ style: {fillColor: '#FFFFFF'} }); expect(polygon.style('fillColor')).toBe('#FFFFFF'); }); }); }); /* This is a basic integration test of geo.gl.polygonFeature. */ describe('geo.gl.polygonFeature', function () { var map, layer, polygons, glCounts, buildTime; it('basic usage', function () { stylesVisited.splice(0, stylesVisited.length); mockVGLRenderer(); map = create_map(); layer = map.createLayer('feature'); polygons = layer.createFeature('polygon', {style: testStyle, data: testPolygons}); buildTime = polygons.buildTime().getMTime(); /* Trigger rerendering */ polygons.data(testPolygons); map.draw(); expect(buildTime).not.toEqual(polygons.buildTime().getMTime()); glCounts = $.extend({}, vgl.mockCounts()); }); waitForIt('next render gl A', function () { return vgl.mockCounts().createProgram >= (glCounts.createProgram || 0) + 2; }); it('check that styles were used', function () { $.each(stylesVisited, function (idx, val) { if (val.style === 'strokeColor') { expect(typeof val.params[3]).toBe('number'); expect(val.params[3]).toBeLessThan(4); } }); }); it('update the style', function () { polygons.style('fillColor', function (d) { return 'red'; }); glCounts = $.extend({}, vgl.mockCounts()); buildTime = polygons.buildTime().getMTime(); polygons.draw(); }); waitForIt('next render gl B', function () { return vgl.mockCounts().bufferData >= (glCounts.bufferData || 0) + 1 && buildTime !== polygons.buildTime().getMTime(); }); it('update the style B', function () { polygons.style('fillColor', function (d) { return '#ff0000'; }); glCounts = $.extend({}, vgl.mockCounts()); buildTime = polygons.buildTime().getMTime(); polygons.draw(); }); waitForIt('next render gl C', function () { return vgl.mockCounts().bufferData >= (glCounts.bufferData || 0) + 1 && buildTime !== polygons.buildTime().getMTime(); }); it('update the style C', function () { polygons.style('fill', function (d, i) { return i % 2 > 0; }); glCounts = $.extend({}, vgl.mockCounts()); buildTime = polygons.buildTime().getMTime(); polygons.draw(); }); waitForIt('next render gl D', function () { return vgl.mockCounts().bufferData >= (glCounts.bufferData || 0) + 1 && buildTime !== polygons.buildTime().getMTime(); }); it('poor data', function () { polygons.data([undefined, testPolygons[1]]); polygons.style('fill', true); glCounts = $.extend({}, vgl.mockCounts()); buildTime = polygons.buildTime().getMTime(); polygons.draw(); }); waitForIt('next render gl E', function () { return vgl.mockCounts().bufferData >= (glCounts.bufferData || 0) + 1 && buildTime !== polygons.buildTime().getMTime(); }); it('_exit', function () { var buildTime = polygons.buildTime().getMTime(); layer.deleteFeature(polygons); polygons.data(testPolygons); map.draw(); expect(buildTime).toEqual(polygons.buildTime().getMTime()); restoreVGLRenderer(); }); }); });
version https://git-lfs.github.com/spec/v1 oid sha256:886f050bd2bc75d42e00242b1114602ac8bc4f0fdbf371824344c425679077f8 size 5631
var webpack = require('webpack'); module.exports = { entry: './src/index.js', module: { loaders: [ { test: /\.jsx?$/, loader: 'babel', exclude: /node_modules/ }, ] }, externals: { 'react': 'React', 'react-dom': 'ReactDOM', }, output: { filename: 'dist/react-sanfona.js', libraryTarget: 'umd', library: 'ReactSanfona' }, plugins: [ new webpack.optimize.DedupePlugin() ], resolve: { extensions: ['', '.jsx', '.js'] } };
/**[@test({ "title": "Test.mock.mocked.MockElement" })]*/ function testMockElement(act, assert, callback, module) { var mock, el, createElement, mockEl; act(function () { el = {}; createElement = callback(el); mock = callback(el); mockEl = module(['TruJS.test.mock.mocked._MockElement', [createElement, mock]]); }); assert(function (test) { test('The createElement callback should have been called once').value(createElement).hasBeenCalled(1); test('The createElement callback should have one arg').value(createElement).hasBeenCalledWithArg(0, 0, 'div'); test('The mock callback should have been called once').value(mock).hasBeenCalled(1); test('The mock callback should have 2 args').value(mock).hasArgCountOf(2); test('The mock callback first arg should be').value(mock).hasBeenCalledWithArg(0, 0, el); test('The mock callback second arg should have 2 props').value(mock.getArgs(0), '1').hasPropertyCountOf(3); }); }
function menu(href) { document.location.href=href; load(); } function load() { var hrefPart = document.location.href.split('#')[1]; var chart = document.getElementById('chart'); chart.style.display = 'block'; chart.src = 'fund.html?' + hrefPart; } function defaultMenu(item) { var href = document.location.href.split('#')[1]; if(href == undefined) { href = item; } menu('#' + href); } function generateMenu(fundId, fundName) { var scriptNode = document.getElementById(fundId); var mainTag = scriptNode.parentNode; mainTag.removeChild(scriptNode); var li = document.createElement('li'); mainTag.appendChild(li); var a = document.createElement('a'); var t = document.createTextNode(fundName); a.appendChild(t); a.href = '#' + fundId; a.setAttribute('onclick', 'javascript:menu(this.href);'); li.appendChild(a); var ul2 = document.createElement('ul'); li.appendChild(ul2); var li2 = document.createElement('li'); ul2.appendChild(li2); var ad = document.createElement('a'); t = document.createTextNode('Дневни данни'); ad.appendChild(t); ad.href = 'data/' + fundId + '-dnevni-danni.csv'; ad.download = fundId + '-dnevni-danni.csv'; li2.appendChild(ad); var li3 = document.createElement('li'); ul2.appendChild(li3); var aw = document.createElement('a'); t = document.createTextNode('Седмични данни'); aw.appendChild(t); aw.href = 'data/' + fundId + '-sedmichni-danni.csv'; aw.download = fundId + '-sedmichni-danni.csv'; li3.appendChild(aw); } function parse_csv(content, row_handler) { //console.log('parse_csv', content.indexOf("\n")) var i = 0; do { var nl_pos = content.indexOf("\n", i); var line = nl_pos == -1 ? content.substr(i) : content.substr(i, nl_pos-i); if (i > 0 && line.length) {//skip header var row = line.split(',') //console.log('line ' + row); row_handler(row) } i = nl_pos + 1; } while(nl_pos != -1) } function load_csv(url, done) { jQuery.ajax({url:url, mimeType: 'text/csv', success: done}) } function override_json(xhr){ //prevents xml parse error in firefox when loaded locally (file://..) if (xhr.overrideMimeType) xhr.overrideMimeType("application/json"); } function add_trace(csv_url, name) { var chart = $('#chart2')[0] load_csv(csv_url, function(csv){ var dates = [], prices = []; parse_csv(csv, function(row){dates.push(row[0]); prices.push(+row[2])}); //console.log(dates) console.log(prices); //lines.push({x:dates,y:prices}) //Plotly.newPlot(document.getElementById('chart'), lines) // Plotly.newPlot(document.getElementById('chart'), [{x:dates,y:prices}]) Plotly.addTraces(chart, {x:dates,y:prices,name:name}); align_chart(); }) }
'use strict'; (function(mod) { const homeController = {}; homeController.index = function() { $('html').css({'height':'100%', 'margin':'auto', 'padding':'inherit'}); $('section').fadeOut(); $('#home').fadeIn(); }; mod.homeController = homeController; })(window);
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update }); var leftEmitter; var rightEmitter; function preload() { game.load.image('sky', 'assets/skies/cavern2.png'); game.load.spritesheet('balls', 'assets/sprites/balls.png', 17, 17); } function create() { game.add.image(0, 0, 'sky'); game.physics.startSystem(Phaser.Physics.ARCADE); leftEmitter = game.add.emitter(50, game.world.centerY - 200); leftEmitter.bounce.setTo(0.5, 0.5); leftEmitter.setXSpeed(100, 200); leftEmitter.setYSpeed(-50, 50); leftEmitter.makeParticles('balls', 0, 250, 1, true); rightEmitter = game.add.emitter(game.world.width - 50, game.world.centerY - 200); rightEmitter.bounce.setTo(0.5, 0.5); rightEmitter.setXSpeed(-100, -200); rightEmitter.setYSpeed(-50, 50); rightEmitter.makeParticles('balls', 1, 250, 1, true); // explode, lifespan, frequency, quantity leftEmitter.start(false, 5000, 20); rightEmitter.start(false, 5000, 20); } function update() { game.physics.arcade.collide(leftEmitter, rightEmitter, change, null, this); } function change(a, b) { a.frame = 3; b.frame = 3; }
var searchData= [ ['search_2ejs',['search.js',['../d2/d19/search_8js.html',1,'']]], ['searchdata_2ejs',['searchdata.js',['../df/d80/searchdata_8js.html',1,'']]], ['sequence_2ec',['sequence.c',['../d4/d43/sequence_8c.html',1,'']]], ['sequence_2eh',['sequence.h',['../db/df3/sequence_8h.html',1,'']]], ['string_2ec',['string.c',['../d1/db0/string_8c.html',1,'']]], ['string_2eh',['string.h',['../da/d66/string_8h.html',1,'']]], ['string_2etest_2ec',['string.test.c',['../d2/d16/string_8test_8c.html',1,'']]], ['summary_2emd',['SUMMARY.md',['../d0/dd1/_s_u_m_m_a_r_y_8md.html',1,'']]] ];
// Adapted from https://github.com/rubenv/angular-gettext/blob/master/src/directive.js function generateDirective( attrName ) { function normalizeAttributeName(attributeName) { // copied from angular.js v1.2.2 // (c) 2010-2012 Google, Inc. http://angularjs.org // License: MIT // Copied from http://thetoeb.de/2014/01/14/angular-normalized-attribute-names/ // Modified to escape hyphens in the regexs var SPECIAL_CHARS_REGEXP = /([:\-_]+(.))/g; var MOZ_HACK_REGEXP = /^moz([A-Z])/; function camelCase(name) { return name.replace(SPECIAL_CHARS_REGEXP, function (_, separator, letter, offset) { return offset ? letter.toUpperCase() : letter; }).replace(MOZ_HACK_REGEXP, 'Moz$1'); } var PREFIX_REGEXP = /^(x[:\-_]|data[:\-_])/i; function directiveNormalize(name) { return camelCase(name.replace(PREFIX_REGEXP, '')); } return directiveNormalize(attributeName); } function assert(condition, missing, found) { if (!condition) { throw new Error('You should add a ' + missing + ' attribute whenever you add a ' + found + ' attribute.'); } } var normAttrName = normalizeAttributeName( attrName ); return function (gettextCatalog, $parse, $animate, $compile, $window) { var msie = parseInt((/msie (\d+)/.exec(angular.lowercase($window.navigator.userAgent)) || [])[1], 10); return { restrict: 'A', terminal: true, priority: 1000, compile: function compile(element, attrs) { // Validate attributes if(!attrs[normAttrName+'Translate']) { throw new Error('Missing '+normAttrName+'-translate attribute!'); } assert(!attrs[normAttrName+'TranslatePlural'] || attrs[normAttrName+'TranslateN'], normAttrName+'translate-n', normAttrName+'translate-plural'); assert(!attrs[normAttrName+'TranslateN'] || attrs[normAttrName+'TranslatePlural'], normAttrName+'translate-plural', normAttrName+'translate-n'); var msgid = attrs[normAttrName+'Translate']; var translatePlural = attrs[normAttrName+'TranslatePlural']; var translateN = attrs[normAttrName+'TranslateN']; var translateContext = attrs[normAttrName+'TranslateContext']; if (msie <= 8) { // Workaround fix relating to angular adding a comment node to // anchors. angular/angular.js/#1949 / angular/angular.js/#2013 if (msgid.slice(-13) === '<!--IE fix-->') { msgid = msgid.slice(0, -13); } } return { pre: function (scope, element, attrs) { var countFn = $parse(attrs[normAttrName+'TranslateN']); var pluralScope = null; function update() { // Fetch correct translated string. var translated; if (translatePlural) { scope = pluralScope || (pluralScope = scope.$new()); scope.$count = countFn(scope); translated = gettextCatalog.getPlural(scope.$count, msgid, translatePlural, null, translateContext); } else { translated = gettextCatalog.getString(msgid, null, translateContext); } var oldContents = attrs[normAttrName]; // Avoid redundant swaps if (translated === oldContents){ return; } // Swap in the translation element.attr(attrName, translated); } if (attrs[normAttrName+'TranslateN']) { scope.$watch(attrs[normAttrName+'TranslateN'], update); } /** * @ngdoc event * @name translate#gettextLanguageChanged * @eventType listen on scope * @description Listens for language updates and changes translation accordingly */ scope.$on('gettextLanguageChanged', update); update(); element.removeAttr(attrName+'-translate'); $compile(element)(scope); } }; } }; }; } angular.module('proton.translate', []) .directive('placeholderTranslate', generateDirective( 'placeholder' )) .directive('titleTranslate', generateDirective( 'title' )) .directive('ptTooltipTranslate', generateDirective( 'pt-tooltip' ));
module.exports = { "extends": "@17media/eslint-config-17media/node" };
'use strict'; /* jshint ignore:start */ /** * This code was generated by * \ / _ _ _| _ _ * | (_)\/(_)(_|\/| |(/_ v1.0.0 * / / */ /* jshint ignore:end */ var _ = require('lodash'); /* jshint ignore:line */ var CompositionHookList = require('./v1/compositionHook').CompositionHookList; var CompositionList = require('./v1/composition').CompositionList; var CompositionSettingsList = require( './v1/compositionSettings').CompositionSettingsList; var RecordingList = require('./v1/recording').RecordingList; var RecordingSettingsList = require( './v1/recordingSettings').RecordingSettingsList; var RoomList = require('./v1/room').RoomList; var Version = require('../../base/Version'); /* jshint ignore:line */ /* jshint ignore:start */ /** * Initialize the V1 version of Video * * @constructor Twilio.Video.V1 * * @property {Twilio.Video.V1.CompositionList} compositions - compositions resource * @property {Twilio.Video.V1.CompositionHookList} compositionHooks - * compositionHooks resource * @property {Twilio.Video.V1.CompositionSettingsList} compositionSettings - * compositionSettings resource * @property {Twilio.Video.V1.RecordingList} recordings - recordings resource * @property {Twilio.Video.V1.RecordingSettingsList} recordingSettings - * recordingSettings resource * @property {Twilio.Video.V1.RoomList} rooms - rooms resource * * @param {Twilio.Video} domain - The twilio domain */ /* jshint ignore:end */ function V1(domain) { Version.prototype.constructor.call(this, domain, 'v1'); // Resources this._compositions = undefined; this._compositionHooks = undefined; this._compositionSettings = undefined; this._recordings = undefined; this._recordingSettings = undefined; this._rooms = undefined; } _.extend(V1.prototype, Version.prototype); V1.prototype.constructor = V1; Object.defineProperty(V1.prototype, 'compositions', { get: function() { this._compositions = this._compositions || new CompositionList(this); return this._compositions; } }); Object.defineProperty(V1.prototype, 'compositionHooks', { get: function() { this._compositionHooks = this._compositionHooks || new CompositionHookList(this); return this._compositionHooks; } }); Object.defineProperty(V1.prototype, 'compositionSettings', { get: function() { this._compositionSettings = this._compositionSettings || new CompositionSettingsList(this); return this._compositionSettings; } }); Object.defineProperty(V1.prototype, 'recordings', { get: function() { this._recordings = this._recordings || new RecordingList(this); return this._recordings; } }); Object.defineProperty(V1.prototype, 'recordingSettings', { get: function() { this._recordingSettings = this._recordingSettings || new RecordingSettingsList(this); return this._recordingSettings; } }); Object.defineProperty(V1.prototype, 'rooms', { get: function() { this._rooms = this._rooms || new RoomList(this); return this._rooms; } }); module.exports = V1;
/* ## @author github.com/tarampampam ## @project Wget GUI Light ## @package Google Chrome Extension ## @copyright 2014 <github.com/tarampampam> ## @license MIT <http://opensource.org/licenses/MIT> ## @github https://github.com/tarampampam/wget-gui-light ## @version Look in 'manifest.json' */ 'use strict'; var saved = false; function bg(){return chrome.extension.getBackgroundPage()} function $(a){return document.getElementById(a)} function noty(a){$('noty').innerHTML = (typeof a == 'string') ? a : ''} $('pathToRpc').value = bg().getPathToRpc(); $('saveSettings').onclick = function(e) { var pathToRpc = $('pathToRpc').value || ''; if(pathToRpc.length !== 0) { if((pathToRpc.substr(pathToRpc.length - 1) == '/') && !saved) { pathToRpc += 'rpc.php'; $('pathToRpc').value = pathToRpc; } if(bg().validateUrl(pathToRpc)) { bg().setPathToRpc(pathToRpc); bg().saveSettings(function(){ if(bg().getPathToRpc() === pathToRpc) { noty('Settings saved'); saved = true; } else { noty('Error while saving settings, <a target="_blank" href="http://goo.gl/sUW63T">write about it here</a>'); console.log('Saved URL: '+bg().getPathToRpc()); console.log('URL for save: '+pathToRpc); } }); } else { noty('Url not valid'); } } else { noty('Url cannot be empty'); } };
/* * /MathJax-v2/localization/gl/MathML.js * * Copyright (c) 2009-2018 The MathJax Consortium * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ MathJax.Localization.addTranslation("gl","MathML",{version:"2.7.8",isLoaded:true,strings:{}});MathJax.Ajax.loadComplete("[MathJax]/localization/gl/MathML.js");
/** * @ag-grid-community/core - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components * @version v25.1.0 * @link http://www.ag-grid.com/ * @license MIT */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var SelectionHandleType; (function (SelectionHandleType) { SelectionHandleType[SelectionHandleType["FILL"] = 0] = "FILL"; SelectionHandleType[SelectionHandleType["RANGE"] = 1] = "RANGE"; })(SelectionHandleType = exports.SelectionHandleType || (exports.SelectionHandleType = {})); var CellRangeType; (function (CellRangeType) { CellRangeType[CellRangeType["VALUE"] = 0] = "VALUE"; CellRangeType[CellRangeType["DIMENSION"] = 1] = "DIMENSION"; })(CellRangeType = exports.CellRangeType || (exports.CellRangeType = {})); //# sourceMappingURL=iRangeController.js.map
var searchData= [ ['accept_5fexternal_5frng',['accept_external_rng',['../classsnemo_1_1simulation_1_1gg__step__hit__processor.html#ae528939e0d8212f34bba17111c99f705',1,'snemo::simulation::gg_step_hit_processor']]], ['adc_5fmax_5fvalue',['ADC_MAX_VALUE',['../classsnemo_1_1datamodel_1_1raw__calorimeter__hit.html#a6ea2ea33cfce195af4287a62b5146d59',1,'snemo::datamodel::raw_calorimeter_hit']]], ['adc_5fmin_5fvalue',['ADC_MIN_VALUE',['../classsnemo_1_1datamodel_1_1raw__calorimeter__hit.html#a9dba73cbb9617c6884c5cb0b9e6c5050',1,'snemo::datamodel::raw_calorimeter_hit']]], ['adc_5foverflow',['ADC_OVERFLOW',['../classsnemo_1_1datamodel_1_1raw__calorimeter__hit.html#aaa06c37fd2771cf13c8a86a0f1ce8494',1,'snemo::datamodel::raw_calorimeter_hit']]], ['adc_5fundefined',['ADC_UNDEFINED',['../classsnemo_1_1datamodel_1_1raw__calorimeter__hit.html#ab0e58eb491c373ba677c6771a901da7f',1,'snemo::datamodel::raw_calorimeter_hit']]], ['adc_5funderflow',['ADC_UNDERFLOW',['../classsnemo_1_1datamodel_1_1raw__calorimeter__hit.html#abf16c77d04dc50bbfd47aaa78bf077d2',1,'snemo::datamodel::raw_calorimeter_hit']]], ['add_5fparticle',['add_particle',['../classsnemo_1_1datamodel_1_1particle__track__data.html#a9e7b2b7465d17a2b8b86b1f356216d44',1,'snemo::datamodel::particle_track_data']]], ['add_5fsolution',['add_solution',['../classsnemo_1_1datamodel_1_1tracker__clustering__data.html#a589e2f6e23d55f82e9837b205a4b702a',1,'snemo::datamodel::tracker_clustering_data::add_solution()'],['../classsnemo_1_1datamodel_1_1tracker__trajectory__data.html#ac9b096c9d546413a6cccd360be1d48ec',1,'snemo::datamodel::tracker_trajectory_data::add_solution()']]], ['address_5fboard_5fid',['ADDRESS_BOARD_ID',['../classsnemo_1_1datamodel_1_1raw__calorimeter__hit.html#ae7f8beeee7c1826621f439f340ee8c53a560c85037f86f33552b5f00e108fa0d2',1,'snemo::datamodel::raw_calorimeter_hit::ADDRESS_BOARD_ID()'],['../classsnemo_1_1datamodel_1_1raw__tracker__hit.html#a157fdede1f58af71ab2b1b9524f4e935aa051843299dc2599a027a6fae96076d7',1,'snemo::datamodel::raw_tracker_hit::ADDRESS_BOARD_ID()']]], ['address_5fchannel_5fid',['ADDRESS_CHANNEL_ID',['../classsnemo_1_1datamodel_1_1raw__calorimeter__hit.html#ae7f8beeee7c1826621f439f340ee8c53ad8d4c0a029dce380a1094e50236aa3d2',1,'snemo::datamodel::raw_calorimeter_hit::ADDRESS_CHANNEL_ID()'],['../classsnemo_1_1datamodel_1_1raw__tracker__hit.html#a157fdede1f58af71ab2b1b9524f4e935acbb132817066708a770495ccfce8128e',1,'snemo::datamodel::raw_tracker_hit::ADDRESS_CHANNEL_ID()']]], ['address_5fcrate_5fid',['ADDRESS_CRATE_ID',['../classsnemo_1_1datamodel_1_1raw__calorimeter__hit.html#ae7f8beeee7c1826621f439f340ee8c53a188adeb0e32d9bcc1ea5e2f96e548519',1,'snemo::datamodel::raw_calorimeter_hit::ADDRESS_CRATE_ID()'],['../classsnemo_1_1datamodel_1_1raw__tracker__hit.html#a157fdede1f58af71ab2b1b9524f4e935a044bb47e2fbe1f79250a2a86d5465561',1,'snemo::datamodel::raw_tracker_hit::ADDRESS_CRATE_ID()']]], ['address_5fmask',['address_mask',['../classsnemo_1_1datamodel_1_1raw__calorimeter__hit.html#ae7f8beeee7c1826621f439f340ee8c53',1,'snemo::datamodel::raw_calorimeter_hit::address_mask()'],['../classsnemo_1_1datamodel_1_1raw__tracker__hit.html#a157fdede1f58af71ab2b1b9524f4e935',1,'snemo::datamodel::raw_tracker_hit::address_mask()']]], ['address_5fmodule_5fid',['ADDRESS_MODULE_ID',['../classsnemo_1_1datamodel_1_1raw__calorimeter__hit.html#ae7f8beeee7c1826621f439f340ee8c53a439ad7ef4f3153e7fc0ee06f3fb5b092',1,'snemo::datamodel::raw_calorimeter_hit::ADDRESS_MODULE_ID()'],['../classsnemo_1_1datamodel_1_1raw__tracker__hit.html#a157fdede1f58af71ab2b1b9524f4e935a95d05b15e307de4ee77656c0cce86815',1,'snemo::datamodel::raw_tracker_hit::ADDRESS_MODULE_ID()']]], ['angular_5fvnm',['angular_VNM',['../structsnemo_1_1simulation_1_1cosmic__muon__generator_1_1sea__level__toy__setup.html#a93332552258695dc009921f6ef40130d',1,'snemo::simulation::cosmic_muon_generator::sea_level_toy_setup']]], ['anode_5ftime_5fkey',['anode_time_key',['../classsnemo_1_1datamodel_1_1calibrated__tracker__hit.html#a4f7a16dbc741c3fa421f4220577e149d',1,'snemo::datamodel::calibrated_tracker_hit']]], ['are_5fboth_5fcathodes_5fmissing',['are_both_cathodes_missing',['../classsnemo_1_1datamodel_1_1calibrated__tracker__hit.html#ae51d0c2c26673a18665170df0a123d5b',1,'snemo::datamodel::calibrated_tracker_hit']]] ];
'use strict'; module.exports = { db: 'mongodb://alberts:[email protected]:35617/albertsdormont', port: 3001, app: { title: 'Albert\'s | Dormont' }, facebook: { clientID: process.env.FACEBOOK_ID || '613888522032151', clientSecret: process.env.FACEBOOK_SECRET || '3640cb46e64ae4fc9963ce17a8c657f1', callbackURL: 'http://www.albertslounge.com/auth/facebook/callback' }, twitter: { clientID: process.env.TWITTER_KEY || 'CONSUMER_KEY', clientSecret: process.env.TWITTER_SECRET || 'CONSUMER_SECRET', callbackURL: 'http://localhost:3000/auth/twitter/callback' }, google: { clientID: process.env.GOOGLE_ID || 'APP_ID', clientSecret: process.env.GOOGLE_SECRET || 'APP_SECRET', callbackURL: 'http://localhost:3000/auth/google/callback' }, linkedin: { clientID: process.env.LINKEDIN_ID || 'APP_ID', clientSecret: process.env.LINKEDIN_SECRET || 'APP_SECRET', callbackURL: 'http://localhost:3000/auth/linkedin/callback' } };
/** * @author mrdoob / http://mrdoob.com/ * @author Larry Battle / http://bateru.com/news * @author bhouston / http://exocortex.com */ var THREE = { REVISION: '66dev' }; self.console = self.console || { info: function () {}, log: function () {}, debug: function () {}, warn: function () {}, error: function () {} }; // based on https://github.com/documentcloud/underscore/blob/bf657be243a075b5e72acc8a83e6f12a564d8f55/underscore.js#L767 THREE.extend = function ( obj, source ) { // ECMAScript5 compatibility based on: http://www.nczonline.net/blog/2012/12/11/are-your-mixins-ecmascript-5-compatible/ if ( Object.keys ) { var keys = Object.keys( source ); for (var i = 0, il = keys.length; i < il; i++) { var prop = keys[i]; Object.defineProperty( obj, prop, Object.getOwnPropertyDescriptor( source, prop ) ); } } else { var safeHasOwnProperty = {}.hasOwnProperty; for ( var prop in source ) { if ( safeHasOwnProperty.call( source, prop ) ) { obj[prop] = source[prop]; } } } return obj; }; // http://paulirish.com/2011/requestanimationframe-for-smart-animating/ // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating // requestAnimationFrame polyfill by Erik Möller // fixes from Paul Irish and Tino Zijdel // using 'self' instead of 'window' for compatibility with both NodeJS and IE10. ( function () { var lastTime = 0; var vendors = [ 'ms', 'moz', 'webkit', 'o' ]; for ( var x = 0; x < vendors.length && !self.requestAnimationFrame; ++ x ) { self.requestAnimationFrame = self[ vendors[ x ] + 'RequestAnimationFrame' ]; self.cancelAnimationFrame = self[ vendors[ x ] + 'CancelAnimationFrame' ] || self[ vendors[ x ] + 'CancelRequestAnimationFrame' ]; } if ( self.requestAnimationFrame === undefined && self['setTimeout'] !== undefined ) { self.requestAnimationFrame = function ( callback ) { var currTime = Date.now(), timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ); var id = self.setTimeout( function() { callback( currTime + timeToCall ); }, timeToCall ); lastTime = currTime + timeToCall; return id; }; } if( self.cancelAnimationFrame === undefined && self['clearTimeout'] !== undefined ) { self.cancelAnimationFrame = function ( id ) { self.clearTimeout( id ) }; } }() ); // GL STATE CONSTANTS THREE.CullFaceNone = 0; THREE.CullFaceBack = 1; THREE.CullFaceFront = 2; THREE.CullFaceFrontBack = 3; THREE.FrontFaceDirectionCW = 0; THREE.FrontFaceDirectionCCW = 1; // SHADOWING TYPES THREE.BasicShadowMap = 0; THREE.PCFShadowMap = 1; THREE.PCFSoftShadowMap = 2; // MATERIAL CONSTANTS // side THREE.FrontSide = 0; THREE.BackSide = 1; THREE.DoubleSide = 2; // shading THREE.NoShading = 0; THREE.FlatShading = 1; THREE.SmoothShading = 2; // colors THREE.NoColors = 0; THREE.FaceColors = 1; THREE.VertexColors = 2; // blending modes THREE.NoBlending = 0; THREE.NormalBlending = 1; THREE.AdditiveBlending = 2; THREE.SubtractiveBlending = 3; THREE.MultiplyBlending = 4; THREE.CustomBlending = 5; // custom blending equations // (numbers start from 100 not to clash with other // mappings to OpenGL constants defined in Texture.js) THREE.AddEquation = 100; THREE.SubtractEquation = 101; THREE.ReverseSubtractEquation = 102; // custom blending destination factors THREE.ZeroFactor = 200; THREE.OneFactor = 201; THREE.SrcColorFactor = 202; THREE.OneMinusSrcColorFactor = 203; THREE.SrcAlphaFactor = 204; THREE.OneMinusSrcAlphaFactor = 205; THREE.DstAlphaFactor = 206; THREE.OneMinusDstAlphaFactor = 207; // custom blending source factors //THREE.ZeroFactor = 200; //THREE.OneFactor = 201; //THREE.SrcAlphaFactor = 204; //THREE.OneMinusSrcAlphaFactor = 205; //THREE.DstAlphaFactor = 206; //THREE.OneMinusDstAlphaFactor = 207; THREE.DstColorFactor = 208; THREE.OneMinusDstColorFactor = 209; THREE.SrcAlphaSaturateFactor = 210; // TEXTURE CONSTANTS THREE.MultiplyOperation = 0; THREE.MixOperation = 1; THREE.AddOperation = 2; // Mapping modes THREE.UVMapping = function () {}; THREE.CubeReflectionMapping = function () {}; THREE.CubeRefractionMapping = function () {}; THREE.SphericalReflectionMapping = function () {}; THREE.SphericalRefractionMapping = function () {}; // Wrapping modes THREE.RepeatWrapping = 1000; THREE.ClampToEdgeWrapping = 1001; THREE.MirroredRepeatWrapping = 1002; // Filters THREE.NearestFilter = 1003; THREE.NearestMipMapNearestFilter = 1004; THREE.NearestMipMapLinearFilter = 1005; THREE.LinearFilter = 1006; THREE.LinearMipMapNearestFilter = 1007; THREE.LinearMipMapLinearFilter = 1008; // Data types THREE.UnsignedByteType = 1009; THREE.ByteType = 1010; THREE.ShortType = 1011; THREE.UnsignedShortType = 1012; THREE.IntType = 1013; THREE.UnsignedIntType = 1014; THREE.FloatType = 1015; // Pixel types //THREE.UnsignedByteType = 1009; THREE.UnsignedShort4444Type = 1016; THREE.UnsignedShort5551Type = 1017; THREE.UnsignedShort565Type = 1018; // Pixel formats THREE.AlphaFormat = 1019; THREE.RGBFormat = 1020; THREE.RGBAFormat = 1021; THREE.LuminanceFormat = 1022; THREE.LuminanceAlphaFormat = 1023; // Compressed texture formats THREE.RGB_S3TC_DXT1_Format = 2001; THREE.RGBA_S3TC_DXT1_Format = 2002; THREE.RGBA_S3TC_DXT3_Format = 2003; THREE.RGBA_S3TC_DXT5_Format = 2004; /* // Potential future PVRTC compressed texture formats THREE.RGB_PVRTC_4BPPV1_Format = 2100; THREE.RGB_PVRTC_2BPPV1_Format = 2101; THREE.RGBA_PVRTC_4BPPV1_Format = 2102; THREE.RGBA_PVRTC_2BPPV1_Format = 2103; */ /** * @author mrdoob / http://mrdoob.com/ */ THREE.Color = function ( color ) { if ( arguments.length === 3 ) { return this.setRGB( arguments[ 0 ], arguments[ 1 ], arguments[ 2 ] ); } return this.set( color ) }; THREE.Color.prototype = { constructor: THREE.Color, r: 1, g: 1, b: 1, set: function ( value ) { if ( value instanceof THREE.Color ) { this.copy( value ); } else if ( typeof value === 'number' ) { this.setHex( value ); } else if ( typeof value === 'string' ) { this.setStyle( value ); } return this; }, setHex: function ( hex ) { hex = Math.floor( hex ); this.r = ( hex >> 16 & 255 ) / 255; this.g = ( hex >> 8 & 255 ) / 255; this.b = ( hex & 255 ) / 255; return this; }, setRGB: function ( r, g, b ) { this.r = r; this.g = g; this.b = b; return this; }, setHSL: function ( h, s, l ) { // h,s,l ranges are in 0.0 - 1.0 if ( s === 0 ) { this.r = this.g = this.b = l; } else { var hue2rgb = function ( p, q, t ) { if ( t < 0 ) t += 1; if ( t > 1 ) t -= 1; if ( t < 1 / 6 ) return p + ( q - p ) * 6 * t; if ( t < 1 / 2 ) return q; if ( t < 2 / 3 ) return p + ( q - p ) * 6 * ( 2 / 3 - t ); return p; }; var p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s ); var q = ( 2 * l ) - p; this.r = hue2rgb( q, p, h + 1 / 3 ); this.g = hue2rgb( q, p, h ); this.b = hue2rgb( q, p, h - 1 / 3 ); } return this; }, setStyle: function ( style ) { // rgb(255,0,0) if ( /^rgb\((\d+), ?(\d+), ?(\d+)\)$/i.test( style ) ) { var color = /^rgb\((\d+), ?(\d+), ?(\d+)\)$/i.exec( style ); this.r = Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255; this.g = Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255; this.b = Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255; return this; } // rgb(100%,0%,0%) if ( /^rgb\((\d+)\%, ?(\d+)\%, ?(\d+)\%\)$/i.test( style ) ) { var color = /^rgb\((\d+)\%, ?(\d+)\%, ?(\d+)\%\)$/i.exec( style ); this.r = Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100; this.g = Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100; this.b = Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100; return this; } // #ff0000 if ( /^\#([0-9a-f]{6})$/i.test( style ) ) { var color = /^\#([0-9a-f]{6})$/i.exec( style ); this.setHex( parseInt( color[ 1 ], 16 ) ); return this; } // #f00 if ( /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i.test( style ) ) { var color = /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i.exec( style ); this.setHex( parseInt( color[ 1 ] + color[ 1 ] + color[ 2 ] + color[ 2 ] + color[ 3 ] + color[ 3 ], 16 ) ); return this; } // red if ( /^(\w+)$/i.test( style ) ) { this.setHex( THREE.ColorKeywords[ style ] ); return this; } }, copy: function ( color ) { this.r = color.r; this.g = color.g; this.b = color.b; return this; }, copyGammaToLinear: function ( color ) { this.r = color.r * color.r; this.g = color.g * color.g; this.b = color.b * color.b; return this; }, copyLinearToGamma: function ( color ) { this.r = Math.sqrt( color.r ); this.g = Math.sqrt( color.g ); this.b = Math.sqrt( color.b ); return this; }, convertGammaToLinear: function () { var r = this.r, g = this.g, b = this.b; this.r = r * r; this.g = g * g; this.b = b * b; return this; }, convertLinearToGamma: function () { this.r = Math.sqrt( this.r ); this.g = Math.sqrt( this.g ); this.b = Math.sqrt( this.b ); return this; }, getHex: function () { return ( this.r * 255 ) << 16 ^ ( this.g * 255 ) << 8 ^ ( this.b * 255 ) << 0; }, getHexString: function () { return ( '000000' + this.getHex().toString( 16 ) ).slice( - 6 ); }, getHSL: function ( optionalTarget ) { // h,s,l ranges are in 0.0 - 1.0 var hsl = optionalTarget || { h: 0, s: 0, l: 0 }; var r = this.r, g = this.g, b = this.b; var max = Math.max( r, g, b ); var min = Math.min( r, g, b ); var hue, saturation; var lightness = ( min + max ) / 2.0; if ( min === max ) { hue = 0; saturation = 0; } else { var delta = max - min; saturation = lightness <= 0.5 ? delta / ( max + min ) : delta / ( 2 - max - min ); switch ( max ) { case r: hue = ( g - b ) / delta + ( g < b ? 6 : 0 ); break; case g: hue = ( b - r ) / delta + 2; break; case b: hue = ( r - g ) / delta + 4; break; } hue /= 6; } hsl.h = hue; hsl.s = saturation; hsl.l = lightness; return hsl; }, getStyle: function () { return 'rgb(' + ( ( this.r * 255 ) | 0 ) + ',' + ( ( this.g * 255 ) | 0 ) + ',' + ( ( this.b * 255 ) | 0 ) + ')'; }, offsetHSL: function ( h, s, l ) { var hsl = this.getHSL(); hsl.h += h; hsl.s += s; hsl.l += l; this.setHSL( hsl.h, hsl.s, hsl.l ); return this; }, add: function ( color ) { this.r += color.r; this.g += color.g; this.b += color.b; return this; }, addColors: function ( color1, color2 ) { this.r = color1.r + color2.r; this.g = color1.g + color2.g; this.b = color1.b + color2.b; return this; }, addScalar: function ( s ) { this.r += s; this.g += s; this.b += s; return this; }, multiply: function ( color ) { this.r *= color.r; this.g *= color.g; this.b *= color.b; return this; }, multiplyScalar: function ( s ) { this.r *= s; this.g *= s; this.b *= s; return this; }, lerp: function ( color, alpha ) { this.r += ( color.r - this.r ) * alpha; this.g += ( color.g - this.g ) * alpha; this.b += ( color.b - this.b ) * alpha; return this; }, equals: function ( c ) { return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b ); }, fromArray: function ( array ) { this.r = array[ 0 ]; this.g = array[ 1 ]; this.b = array[ 2 ]; return this; }, toArray: function () { return [ this.r, this.g, this.b ]; }, clone: function () { return new THREE.Color().setRGB( this.r, this.g, this.b ); } }; THREE.ColorKeywords = { "aliceblue": 0xF0F8FF, "antiquewhite": 0xFAEBD7, "aqua": 0x00FFFF, "aquamarine": 0x7FFFD4, "azure": 0xF0FFFF, "beige": 0xF5F5DC, "bisque": 0xFFE4C4, "black": 0x000000, "blanchedalmond": 0xFFEBCD, "blue": 0x0000FF, "blueviolet": 0x8A2BE2, "brown": 0xA52A2A, "burlywood": 0xDEB887, "cadetblue": 0x5F9EA0, "chartreuse": 0x7FFF00, "chocolate": 0xD2691E, "coral": 0xFF7F50, "cornflowerblue": 0x6495ED, "cornsilk": 0xFFF8DC, "crimson": 0xDC143C, "cyan": 0x00FFFF, "darkblue": 0x00008B, "darkcyan": 0x008B8B, "darkgoldenrod": 0xB8860B, "darkgray": 0xA9A9A9, "darkgreen": 0x006400, "darkgrey": 0xA9A9A9, "darkkhaki": 0xBDB76B, "darkmagenta": 0x8B008B, "darkolivegreen": 0x556B2F, "darkorange": 0xFF8C00, "darkorchid": 0x9932CC, "darkred": 0x8B0000, "darksalmon": 0xE9967A, "darkseagreen": 0x8FBC8F, "darkslateblue": 0x483D8B, "darkslategray": 0x2F4F4F, "darkslategrey": 0x2F4F4F, "darkturquoise": 0x00CED1, "darkviolet": 0x9400D3, "deeppink": 0xFF1493, "deepskyblue": 0x00BFFF, "dimgray": 0x696969, "dimgrey": 0x696969, "dodgerblue": 0x1E90FF, "firebrick": 0xB22222, "floralwhite": 0xFFFAF0, "forestgreen": 0x228B22, "fuchsia": 0xFF00FF, "gainsboro": 0xDCDCDC, "ghostwhite": 0xF8F8FF, "gold": 0xFFD700, "goldenrod": 0xDAA520, "gray": 0x808080, "green": 0x008000, "greenyellow": 0xADFF2F, "grey": 0x808080, "honeydew": 0xF0FFF0, "hotpink": 0xFF69B4, "indianred": 0xCD5C5C, "indigo": 0x4B0082, "ivory": 0xFFFFF0, "khaki": 0xF0E68C, "lavender": 0xE6E6FA, "lavenderblush": 0xFFF0F5, "lawngreen": 0x7CFC00, "lemonchiffon": 0xFFFACD, "lightblue": 0xADD8E6, "lightcoral": 0xF08080, "lightcyan": 0xE0FFFF, "lightgoldenrodyellow": 0xFAFAD2, "lightgray": 0xD3D3D3, "lightgreen": 0x90EE90, "lightgrey": 0xD3D3D3, "lightpink": 0xFFB6C1, "lightsalmon": 0xFFA07A, "lightseagreen": 0x20B2AA, "lightskyblue": 0x87CEFA, "lightslategray": 0x778899, "lightslategrey": 0x778899, "lightsteelblue": 0xB0C4DE, "lightyellow": 0xFFFFE0, "lime": 0x00FF00, "limegreen": 0x32CD32, "linen": 0xFAF0E6, "magenta": 0xFF00FF, "maroon": 0x800000, "mediumaquamarine": 0x66CDAA, "mediumblue": 0x0000CD, "mediumorchid": 0xBA55D3, "mediumpurple": 0x9370DB, "mediumseagreen": 0x3CB371, "mediumslateblue": 0x7B68EE, "mediumspringgreen": 0x00FA9A, "mediumturquoise": 0x48D1CC, "mediumvioletred": 0xC71585, "midnightblue": 0x191970, "mintcream": 0xF5FFFA, "mistyrose": 0xFFE4E1, "moccasin": 0xFFE4B5, "navajowhite": 0xFFDEAD, "navy": 0x000080, "oldlace": 0xFDF5E6, "olive": 0x808000, "olivedrab": 0x6B8E23, "orange": 0xFFA500, "orangered": 0xFF4500, "orchid": 0xDA70D6, "palegoldenrod": 0xEEE8AA, "palegreen": 0x98FB98, "paleturquoise": 0xAFEEEE, "palevioletred": 0xDB7093, "papayawhip": 0xFFEFD5, "peachpuff": 0xFFDAB9, "peru": 0xCD853F, "pink": 0xFFC0CB, "plum": 0xDDA0DD, "powderblue": 0xB0E0E6, "purple": 0x800080, "red": 0xFF0000, "rosybrown": 0xBC8F8F, "royalblue": 0x4169E1, "saddlebrown": 0x8B4513, "salmon": 0xFA8072, "sandybrown": 0xF4A460, "seagreen": 0x2E8B57, "seashell": 0xFFF5EE, "sienna": 0xA0522D, "silver": 0xC0C0C0, "skyblue": 0x87CEEB, "slateblue": 0x6A5ACD, "slategray": 0x708090, "slategrey": 0x708090, "snow": 0xFFFAFA, "springgreen": 0x00FF7F, "steelblue": 0x4682B4, "tan": 0xD2B48C, "teal": 0x008080, "thistle": 0xD8BFD8, "tomato": 0xFF6347, "turquoise": 0x40E0D0, "violet": 0xEE82EE, "wheat": 0xF5DEB3, "white": 0xFFFFFF, "whitesmoke": 0xF5F5F5, "yellow": 0xFFFF00, "yellowgreen": 0x9ACD32 }; /** * @author mikael emtinger / http://gomo.se/ * @author alteredq / http://alteredqualia.com/ * @author WestLangley / http://github.com/WestLangley * @author bhouston / http://exocortex.com */ THREE.Quaternion = function ( x, y, z, w ) { this._x = x || 0; this._y = y || 0; this._z = z || 0; this._w = ( w !== undefined ) ? w : 1; }; THREE.Quaternion.prototype = { constructor: THREE.Quaternion, _x: 0,_y: 0, _z: 0, _w: 0, _euler: undefined, _updateEuler: function ( callback ) { if ( this._euler !== undefined ) { this._euler.setFromQuaternion( this, undefined, false ); } }, get x () { return this._x; }, set x ( value ) { this._x = value; this._updateEuler(); }, get y () { return this._y; }, set y ( value ) { this._y = value; this._updateEuler(); }, get z () { return this._z; }, set z ( value ) { this._z = value; this._updateEuler(); }, get w () { return this._w; }, set w ( value ) { this._w = value; this._updateEuler(); }, set: function ( x, y, z, w ) { this._x = x; this._y = y; this._z = z; this._w = w; this._updateEuler(); return this; }, copy: function ( quaternion ) { this._x = quaternion._x; this._y = quaternion._y; this._z = quaternion._z; this._w = quaternion._w; this._updateEuler(); return this; }, setFromEuler: function ( euler, update ) { if ( euler instanceof THREE.Euler === false ) { throw new Error( 'ERROR: Quaternion\'s .setFromEuler() now expects a Euler rotation rather than a Vector3 and order. Please update your code.' ); } // http://www.mathworks.com/matlabcentral/fileexchange/ // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/ // content/SpinCalc.m var c1 = Math.cos( euler._x / 2 ); var c2 = Math.cos( euler._y / 2 ); var c3 = Math.cos( euler._z / 2 ); var s1 = Math.sin( euler._x / 2 ); var s2 = Math.sin( euler._y / 2 ); var s3 = Math.sin( euler._z / 2 ); if ( euler.order === 'XYZ' ) { this._x = s1 * c2 * c3 + c1 * s2 * s3; this._y = c1 * s2 * c3 - s1 * c2 * s3; this._z = c1 * c2 * s3 + s1 * s2 * c3; this._w = c1 * c2 * c3 - s1 * s2 * s3; } else if ( euler.order === 'YXZ' ) { this._x = s1 * c2 * c3 + c1 * s2 * s3; this._y = c1 * s2 * c3 - s1 * c2 * s3; this._z = c1 * c2 * s3 - s1 * s2 * c3; this._w = c1 * c2 * c3 + s1 * s2 * s3; } else if ( euler.order === 'ZXY' ) { this._x = s1 * c2 * c3 - c1 * s2 * s3; this._y = c1 * s2 * c3 + s1 * c2 * s3; this._z = c1 * c2 * s3 + s1 * s2 * c3; this._w = c1 * c2 * c3 - s1 * s2 * s3; } else if ( euler.order === 'ZYX' ) { this._x = s1 * c2 * c3 - c1 * s2 * s3; this._y = c1 * s2 * c3 + s1 * c2 * s3; this._z = c1 * c2 * s3 - s1 * s2 * c3; this._w = c1 * c2 * c3 + s1 * s2 * s3; } else if ( euler.order === 'YZX' ) { this._x = s1 * c2 * c3 + c1 * s2 * s3; this._y = c1 * s2 * c3 + s1 * c2 * s3; this._z = c1 * c2 * s3 - s1 * s2 * c3; this._w = c1 * c2 * c3 - s1 * s2 * s3; } else if ( euler.order === 'XZY' ) { this._x = s1 * c2 * c3 - c1 * s2 * s3; this._y = c1 * s2 * c3 - s1 * c2 * s3; this._z = c1 * c2 * s3 + s1 * s2 * c3; this._w = c1 * c2 * c3 + s1 * s2 * s3; } if ( update !== false ) this._updateEuler(); return this; }, setFromAxisAngle: function ( axis, angle ) { // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm // axis have to be normalized var halfAngle = angle / 2, s = Math.sin( halfAngle ); this._x = axis.x * s; this._y = axis.y * s; this._z = axis.z * s; this._w = Math.cos( halfAngle ); this._updateEuler(); return this; }, setFromRotationMatrix: function ( m ) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) var te = m.elements, m11 = te[0], m12 = te[4], m13 = te[8], m21 = te[1], m22 = te[5], m23 = te[9], m31 = te[2], m32 = te[6], m33 = te[10], trace = m11 + m22 + m33, s; if ( trace > 0 ) { s = 0.5 / Math.sqrt( trace + 1.0 ); this._w = 0.25 / s; this._x = ( m32 - m23 ) * s; this._y = ( m13 - m31 ) * s; this._z = ( m21 - m12 ) * s; } else if ( m11 > m22 && m11 > m33 ) { s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 ); this._w = (m32 - m23 ) / s; this._x = 0.25 * s; this._y = (m12 + m21 ) / s; this._z = (m13 + m31 ) / s; } else if ( m22 > m33 ) { s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 ); this._w = (m13 - m31 ) / s; this._x = (m12 + m21 ) / s; this._y = 0.25 * s; this._z = (m23 + m32 ) / s; } else { s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 ); this._w = ( m21 - m12 ) / s; this._x = ( m13 + m31 ) / s; this._y = ( m23 + m32 ) / s; this._z = 0.25 * s; } this._updateEuler(); return this; }, inverse: function () { this.conjugate().normalize(); return this; }, conjugate: function () { this._x *= -1; this._y *= -1; this._z *= -1; this._updateEuler(); return this; }, lengthSq: function () { return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w; }, length: function () { return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w ); }, normalize: function () { var l = this.length(); if ( l === 0 ) { this._x = 0; this._y = 0; this._z = 0; this._w = 1; } else { l = 1 / l; this._x = this._x * l; this._y = this._y * l; this._z = this._z * l; this._w = this._w * l; } return this; }, multiply: function ( q, p ) { if ( p !== undefined ) { console.warn( 'DEPRECATED: Quaternion\'s .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' ); return this.multiplyQuaternions( q, p ); } return this.multiplyQuaternions( this, q ); }, multiplyQuaternions: function ( a, b ) { // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w; var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w; this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby; this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz; this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx; this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz; this._updateEuler(); return this; }, multiplyVector3: function ( vector ) { console.warn( 'DEPRECATED: Quaternion\'s .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' ); return vector.applyQuaternion( this ); }, slerp: function ( qb, t ) { var x = this._x, y = this._y, z = this._z, w = this._w; // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/ var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z; if ( cosHalfTheta < 0 ) { this._w = -qb._w; this._x = -qb._x; this._y = -qb._y; this._z = -qb._z; cosHalfTheta = -cosHalfTheta; } else { this.copy( qb ); } if ( cosHalfTheta >= 1.0 ) { this._w = w; this._x = x; this._y = y; this._z = z; return this; } var halfTheta = Math.acos( cosHalfTheta ); var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta ); if ( Math.abs( sinHalfTheta ) < 0.001 ) { this._w = 0.5 * ( w + this._w ); this._x = 0.5 * ( x + this._x ); this._y = 0.5 * ( y + this._y ); this._z = 0.5 * ( z + this._z ); return this; } var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta, ratioB = Math.sin( t * halfTheta ) / sinHalfTheta; this._w = ( w * ratioA + this._w * ratioB ); this._x = ( x * ratioA + this._x * ratioB ); this._y = ( y * ratioA + this._y * ratioB ); this._z = ( z * ratioA + this._z * ratioB ); this._updateEuler(); return this; }, equals: function ( quaternion ) { return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w ); }, fromArray: function ( array ) { this._x = array[ 0 ]; this._y = array[ 1 ]; this._z = array[ 2 ]; this._w = array[ 3 ]; this._updateEuler(); return this; }, toArray: function () { return [ this._x, this._y, this._z, this._w ]; }, clone: function () { return new THREE.Quaternion( this._x, this._y, this._z, this._w ); } }; THREE.Quaternion.slerp = function ( qa, qb, qm, t ) { return qm.copy( qa ).slerp( qb, t ); } /** * @author mrdoob / http://mrdoob.com/ * @author philogb / http://blog.thejit.org/ * @author egraether / http://egraether.com/ * @author zz85 / http://www.lab4games.net/zz85/blog */ THREE.Vector2 = function ( x, y ) { this.x = x || 0; this.y = y || 0; }; THREE.Vector2.prototype = { constructor: THREE.Vector2, set: function ( x, y ) { this.x = x; this.y = y; return this; }, setX: function ( x ) { this.x = x; return this; }, setY: function ( y ) { this.y = y; return this; }, setComponent: function ( index, value ) { switch ( index ) { case 0: this.x = value; break; case 1: this.y = value; break; default: throw new Error( "index is out of range: " + index ); } }, getComponent: function ( index ) { switch ( index ) { case 0: return this.x; case 1: return this.y; default: throw new Error( "index is out of range: " + index ); } }, copy: function ( v ) { this.x = v.x; this.y = v.y; return this; }, add: function ( v, w ) { if ( w !== undefined ) { console.warn( 'DEPRECATED: Vector2\'s .add() now only accepts one argument. Use .addVectors( a, b ) instead.' ); return this.addVectors( v, w ); } this.x += v.x; this.y += v.y; return this; }, addVectors: function ( a, b ) { this.x = a.x + b.x; this.y = a.y + b.y; return this; }, addScalar: function ( s ) { this.x += s; this.y += s; return this; }, sub: function ( v, w ) { if ( w !== undefined ) { console.warn( 'DEPRECATED: Vector2\'s .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' ); return this.subVectors( v, w ); } this.x -= v.x; this.y -= v.y; return this; }, subVectors: function ( a, b ) { this.x = a.x - b.x; this.y = a.y - b.y; return this; }, multiplyScalar: function ( s ) { this.x *= s; this.y *= s; return this; }, divideScalar: function ( scalar ) { if ( scalar !== 0 ) { var invScalar = 1 / scalar; this.x *= invScalar; this.y *= invScalar; } else { this.x = 0; this.y = 0; } return this; }, min: function ( v ) { if ( this.x > v.x ) { this.x = v.x; } if ( this.y > v.y ) { this.y = v.y; } return this; }, max: function ( v ) { if ( this.x < v.x ) { this.x = v.x; } if ( this.y < v.y ) { this.y = v.y; } return this; }, clamp: function ( min, max ) { // This function assumes min < max, if this assumption isn't true it will not operate correctly if ( this.x < min.x ) { this.x = min.x; } else if ( this.x > max.x ) { this.x = max.x; } if ( this.y < min.y ) { this.y = min.y; } else if ( this.y > max.y ) { this.y = max.y; } return this; }, negate: function() { return this.multiplyScalar( - 1 ); }, dot: function ( v ) { return this.x * v.x + this.y * v.y; }, lengthSq: function () { return this.x * this.x + this.y * this.y; }, length: function () { return Math.sqrt( this.x * this.x + this.y * this.y ); }, normalize: function () { return this.divideScalar( this.length() ); }, distanceTo: function ( v ) { return Math.sqrt( this.distanceToSquared( v ) ); }, distanceToSquared: function ( v ) { var dx = this.x - v.x, dy = this.y - v.y; return dx * dx + dy * dy; }, setLength: function ( l ) { var oldLength = this.length(); if ( oldLength !== 0 && l !== oldLength ) { this.multiplyScalar( l / oldLength ); } return this; }, lerp: function ( v, alpha ) { this.x += ( v.x - this.x ) * alpha; this.y += ( v.y - this.y ) * alpha; return this; }, equals: function( v ) { return ( ( v.x === this.x ) && ( v.y === this.y ) ); }, fromArray: function ( array ) { this.x = array[ 0 ]; this.y = array[ 1 ]; return this; }, toArray: function () { return [ this.x, this.y ]; }, clone: function () { return new THREE.Vector2( this.x, this.y ); } }; /** * @author mrdoob / http://mrdoob.com/ * @author *kile / http://kile.stravaganza.org/ * @author philogb / http://blog.thejit.org/ * @author mikael emtinger / http://gomo.se/ * @author egraether / http://egraether.com/ * @author WestLangley / http://github.com/WestLangley */ THREE.Vector3 = function ( x, y, z ) { this.x = x || 0; this.y = y || 0; this.z = z || 0; }; THREE.Vector3.prototype = { constructor: THREE.Vector3, set: function ( x, y, z ) { this.x = x; this.y = y; this.z = z; return this; }, setX: function ( x ) { this.x = x; return this; }, setY: function ( y ) { this.y = y; return this; }, setZ: function ( z ) { this.z = z; return this; }, setComponent: function ( index, value ) { switch ( index ) { case 0: this.x = value; break; case 1: this.y = value; break; case 2: this.z = value; break; default: throw new Error( "index is out of range: " + index ); } }, getComponent: function ( index ) { switch ( index ) { case 0: return this.x; case 1: return this.y; case 2: return this.z; default: throw new Error( "index is out of range: " + index ); } }, copy: function ( v ) { this.x = v.x; this.y = v.y; this.z = v.z; return this; }, add: function ( v, w ) { if ( w !== undefined ) { console.warn( 'DEPRECATED: Vector3\'s .add() now only accepts one argument. Use .addVectors( a, b ) instead.' ); return this.addVectors( v, w ); } this.x += v.x; this.y += v.y; this.z += v.z; return this; }, addScalar: function ( s ) { this.x += s; this.y += s; this.z += s; return this; }, addVectors: function ( a, b ) { this.x = a.x + b.x; this.y = a.y + b.y; this.z = a.z + b.z; return this; }, sub: function ( v, w ) { if ( w !== undefined ) { console.warn( 'DEPRECATED: Vector3\'s .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' ); return this.subVectors( v, w ); } this.x -= v.x; this.y -= v.y; this.z -= v.z; return this; }, subVectors: function ( a, b ) { this.x = a.x - b.x; this.y = a.y - b.y; this.z = a.z - b.z; return this; }, multiply: function ( v, w ) { if ( w !== undefined ) { console.warn( 'DEPRECATED: Vector3\'s .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' ); return this.multiplyVectors( v, w ); } this.x *= v.x; this.y *= v.y; this.z *= v.z; return this; }, multiplyScalar: function ( scalar ) { this.x *= scalar; this.y *= scalar; this.z *= scalar; return this; }, multiplyVectors: function ( a, b ) { this.x = a.x * b.x; this.y = a.y * b.y; this.z = a.z * b.z; return this; }, applyMatrix3: function ( m ) { var x = this.x; var y = this.y; var z = this.z; var e = m.elements; this.x = e[0] * x + e[3] * y + e[6] * z; this.y = e[1] * x + e[4] * y + e[7] * z; this.z = e[2] * x + e[5] * y + e[8] * z; return this; }, applyMatrix4: function ( m ) { // input: THREE.Matrix4 affine matrix var x = this.x, y = this.y, z = this.z; var e = m.elements; this.x = e[0] * x + e[4] * y + e[8] * z + e[12]; this.y = e[1] * x + e[5] * y + e[9] * z + e[13]; this.z = e[2] * x + e[6] * y + e[10] * z + e[14]; return this; }, applyProjection: function ( m ) { // input: THREE.Matrix4 projection matrix var x = this.x, y = this.y, z = this.z; var e = m.elements; var d = 1 / ( e[3] * x + e[7] * y + e[11] * z + e[15] ); // perspective divide this.x = ( e[0] * x + e[4] * y + e[8] * z + e[12] ) * d; this.y = ( e[1] * x + e[5] * y + e[9] * z + e[13] ) * d; this.z = ( e[2] * x + e[6] * y + e[10] * z + e[14] ) * d; return this; }, applyQuaternion: function ( q ) { var x = this.x; var y = this.y; var z = this.z; var qx = q.x; var qy = q.y; var qz = q.z; var qw = q.w; // calculate quat * vector var ix = qw * x + qy * z - qz * y; var iy = qw * y + qz * x - qx * z; var iz = qw * z + qx * y - qy * x; var iw = -qx * x - qy * y - qz * z; // calculate result * inverse quat this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy; this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz; this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx; return this; }, transformDirection: function ( m ) { // input: THREE.Matrix4 affine matrix // vector interpreted as a direction var x = this.x, y = this.y, z = this.z; var e = m.elements; this.x = e[0] * x + e[4] * y + e[8] * z; this.y = e[1] * x + e[5] * y + e[9] * z; this.z = e[2] * x + e[6] * y + e[10] * z; this.normalize(); return this; }, divide: function ( v ) { this.x /= v.x; this.y /= v.y; this.z /= v.z; return this; }, divideScalar: function ( scalar ) { if ( scalar !== 0 ) { var invScalar = 1 / scalar; this.x *= invScalar; this.y *= invScalar; this.z *= invScalar; } else { this.x = 0; this.y = 0; this.z = 0; } return this; }, min: function ( v ) { if ( this.x > v.x ) { this.x = v.x; } if ( this.y > v.y ) { this.y = v.y; } if ( this.z > v.z ) { this.z = v.z; } return this; }, max: function ( v ) { if ( this.x < v.x ) { this.x = v.x; } if ( this.y < v.y ) { this.y = v.y; } if ( this.z < v.z ) { this.z = v.z; } return this; }, clamp: function ( min, max ) { // This function assumes min < max, if this assumption isn't true it will not operate correctly if ( this.x < min.x ) { this.x = min.x; } else if ( this.x > max.x ) { this.x = max.x; } if ( this.y < min.y ) { this.y = min.y; } else if ( this.y > max.y ) { this.y = max.y; } if ( this.z < min.z ) { this.z = min.z; } else if ( this.z > max.z ) { this.z = max.z; } return this; }, negate: function () { return this.multiplyScalar( - 1 ); }, dot: function ( v ) { return this.x * v.x + this.y * v.y + this.z * v.z; }, lengthSq: function () { return this.x * this.x + this.y * this.y + this.z * this.z; }, length: function () { return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z ); }, lengthManhattan: function () { return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ); }, normalize: function () { return this.divideScalar( this.length() ); }, setLength: function ( l ) { var oldLength = this.length(); if ( oldLength !== 0 && l !== oldLength ) { this.multiplyScalar( l / oldLength ); } return this; }, lerp: function ( v, alpha ) { this.x += ( v.x - this.x ) * alpha; this.y += ( v.y - this.y ) * alpha; this.z += ( v.z - this.z ) * alpha; return this; }, cross: function ( v, w ) { if ( w !== undefined ) { console.warn( 'DEPRECATED: Vector3\'s .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' ); return this.crossVectors( v, w ); } var x = this.x, y = this.y, z = this.z; this.x = y * v.z - z * v.y; this.y = z * v.x - x * v.z; this.z = x * v.y - y * v.x; return this; }, crossVectors: function ( a, b ) { var ax = a.x, ay = a.y, az = a.z; var bx = b.x, by = b.y, bz = b.z; this.x = ay * bz - az * by; this.y = az * bx - ax * bz; this.z = ax * by - ay * bx; return this; }, angleTo: function ( v ) { var theta = this.dot( v ) / ( this.length() * v.length() ); // clamp, to handle numerical problems return Math.acos( THREE.Math.clamp( theta, -1, 1 ) ); }, distanceTo: function ( v ) { return Math.sqrt( this.distanceToSquared( v ) ); }, distanceToSquared: function ( v ) { var dx = this.x - v.x; var dy = this.y - v.y; var dz = this.z - v.z; return dx * dx + dy * dy + dz * dz; }, setEulerFromRotationMatrix: function ( m, order ) { console.error( "REMOVED: Vector3\'s setEulerFromRotationMatrix has been removed in favor of Euler.setFromRotationMatrix(), please update your code."); }, setEulerFromQuaternion: function ( q, order ) { console.error( "REMOVED: Vector3\'s setEulerFromQuaternion: has been removed in favor of Euler.setFromQuaternion(), please update your code."); }, getPositionFromMatrix: function ( m ) { console.warn( "DEPRECATED: Vector3\'s .getPositionFromMatrix() has been renamed to .setFromMatrixPosition(). Please update your code." ); return this.setFromMatrixPosition( m ); }, getScaleFromMatrix: function ( m ) { console.warn( "DEPRECATED: Vector3\'s .getScaleFromMatrix() has been renamed to .setFromMatrixScale(). Please update your code." ); return this.setFromMatrixScale( m ); }, getColumnFromMatrix: function ( index, matrix ) { console.warn( "DEPRECATED: Vector3\'s .getColumnFromMatrix() has been renamed to .setFromMatrixColumn(). Please update your code." ); return this.setFromMatrixColumn( index, matrix ); }, setFromMatrixPosition: function ( m ) { this.x = m.elements[ 12 ]; this.y = m.elements[ 13 ]; this.z = m.elements[ 14 ]; return this; }, setFromMatrixScale: function ( m ) { var sx = this.set( m.elements[ 0 ], m.elements[ 1 ], m.elements[ 2 ] ).length(); var sy = this.set( m.elements[ 4 ], m.elements[ 5 ], m.elements[ 6 ] ).length(); var sz = this.set( m.elements[ 8 ], m.elements[ 9 ], m.elements[ 10 ] ).length(); this.x = sx; this.y = sy; this.z = sz; return this; }, setFromMatrixColumn: function ( index, matrix ) { var offset = index * 4; var me = matrix.elements; this.x = me[ offset ]; this.y = me[ offset + 1 ]; this.z = me[ offset + 2 ]; return this; }, equals: function ( v ) { return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) ); }, fromArray: function ( array ) { this.x = array[ 0 ]; this.y = array[ 1 ]; this.z = array[ 2 ]; return this; }, toArray: function () { return [ this.x, this.y, this.z ]; }, clone: function () { return new THREE.Vector3( this.x, this.y, this.z ); } }; THREE.extend( THREE.Vector3.prototype, { applyEuler: function () { var quaternion = new THREE.Quaternion(); return function ( euler ) { if ( euler instanceof THREE.Euler === false ) { console.error( 'ERROR: Vector3\'s .applyEuler() now expects a Euler rotation rather than a Vector3 and order. Please update your code.' ); } this.applyQuaternion( quaternion.setFromEuler( euler ) ); return this; }; }(), applyAxisAngle: function () { var quaternion = new THREE.Quaternion(); return function ( axis, angle ) { this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) ); return this; }; }(), projectOnVector: function () { var v1 = new THREE.Vector3(); return function ( vector ) { v1.copy( vector ).normalize(); var d = this.dot( v1 ); return this.copy( v1 ).multiplyScalar( d ); }; }(), projectOnPlane: function () { var v1 = new THREE.Vector3(); return function ( planeNormal ) { v1.copy( this ).projectOnVector( planeNormal ); return this.sub( v1 ); } }(), reflect: function () { // reflect incident vector off plane orthogonal to normal // normal is assumed to have unit length var v1 = new THREE.Vector3(); return function ( normal ) { return this.sub( v1.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) ); } }() } ); /** * @author supereggbert / http://www.paulbrunt.co.uk/ * @author philogb / http://blog.thejit.org/ * @author mikael emtinger / http://gomo.se/ * @author egraether / http://egraether.com/ * @author WestLangley / http://github.com/WestLangley */ THREE.Vector4 = function ( x, y, z, w ) { this.x = x || 0; this.y = y || 0; this.z = z || 0; this.w = ( w !== undefined ) ? w : 1; }; THREE.Vector4.prototype = { constructor: THREE.Vector4, set: function ( x, y, z, w ) { this.x = x; this.y = y; this.z = z; this.w = w; return this; }, setX: function ( x ) { this.x = x; return this; }, setY: function ( y ) { this.y = y; return this; }, setZ: function ( z ) { this.z = z; return this; }, setW: function ( w ) { this.w = w; return this; }, setComponent: function ( index, value ) { switch ( index ) { case 0: this.x = value; break; case 1: this.y = value; break; case 2: this.z = value; break; case 3: this.w = value; break; default: throw new Error( "index is out of range: " + index ); } }, getComponent: function ( index ) { switch ( index ) { case 0: return this.x; case 1: return this.y; case 2: return this.z; case 3: return this.w; default: throw new Error( "index is out of range: " + index ); } }, copy: function ( v ) { this.x = v.x; this.y = v.y; this.z = v.z; this.w = ( v.w !== undefined ) ? v.w : 1; return this; }, add: function ( v, w ) { if ( w !== undefined ) { console.warn( 'DEPRECATED: Vector4\'s .add() now only accepts one argument. Use .addVectors( a, b ) instead.' ); return this.addVectors( v, w ); } this.x += v.x; this.y += v.y; this.z += v.z; this.w += v.w; return this; }, addScalar: function ( s ) { this.x += s; this.y += s; this.z += s; this.w += s; return this; }, addVectors: function ( a, b ) { this.x = a.x + b.x; this.y = a.y + b.y; this.z = a.z + b.z; this.w = a.w + b.w; return this; }, sub: function ( v, w ) { if ( w !== undefined ) { console.warn( 'DEPRECATED: Vector4\'s .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' ); return this.subVectors( v, w ); } this.x -= v.x; this.y -= v.y; this.z -= v.z; this.w -= v.w; return this; }, subVectors: function ( a, b ) { this.x = a.x - b.x; this.y = a.y - b.y; this.z = a.z - b.z; this.w = a.w - b.w; return this; }, multiplyScalar: function ( scalar ) { this.x *= scalar; this.y *= scalar; this.z *= scalar; this.w *= scalar; return this; }, applyMatrix4: function ( m ) { var x = this.x; var y = this.y; var z = this.z; var w = this.w; var e = m.elements; this.x = e[0] * x + e[4] * y + e[8] * z + e[12] * w; this.y = e[1] * x + e[5] * y + e[9] * z + e[13] * w; this.z = e[2] * x + e[6] * y + e[10] * z + e[14] * w; this.w = e[3] * x + e[7] * y + e[11] * z + e[15] * w; return this; }, divideScalar: function ( scalar ) { if ( scalar !== 0 ) { var invScalar = 1 / scalar; this.x *= invScalar; this.y *= invScalar; this.z *= invScalar; this.w *= invScalar; } else { this.x = 0; this.y = 0; this.z = 0; this.w = 1; } return this; }, setAxisAngleFromQuaternion: function ( q ) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm // q is assumed to be normalized this.w = 2 * Math.acos( q.w ); var s = Math.sqrt( 1 - q.w * q.w ); if ( s < 0.0001 ) { this.x = 1; this.y = 0; this.z = 0; } else { this.x = q.x / s; this.y = q.y / s; this.z = q.z / s; } return this; }, setAxisAngleFromRotationMatrix: function ( m ) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) var angle, x, y, z, // variables for result epsilon = 0.01, // margin to allow for rounding errors epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees te = m.elements, m11 = te[0], m12 = te[4], m13 = te[8], m21 = te[1], m22 = te[5], m23 = te[9], m31 = te[2], m32 = te[6], m33 = te[10]; if ( ( Math.abs( m12 - m21 ) < epsilon ) && ( Math.abs( m13 - m31 ) < epsilon ) && ( Math.abs( m23 - m32 ) < epsilon ) ) { // singularity found // first check for identity matrix which must have +1 for all terms // in leading diagonal and zero in other terms if ( ( Math.abs( m12 + m21 ) < epsilon2 ) && ( Math.abs( m13 + m31 ) < epsilon2 ) && ( Math.abs( m23 + m32 ) < epsilon2 ) && ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) { // this singularity is identity matrix so angle = 0 this.set( 1, 0, 0, 0 ); return this; // zero angle, arbitrary axis } // otherwise this singularity is angle = 180 angle = Math.PI; var xx = ( m11 + 1 ) / 2; var yy = ( m22 + 1 ) / 2; var zz = ( m33 + 1 ) / 2; var xy = ( m12 + m21 ) / 4; var xz = ( m13 + m31 ) / 4; var yz = ( m23 + m32 ) / 4; if ( ( xx > yy ) && ( xx > zz ) ) { // m11 is the largest diagonal term if ( xx < epsilon ) { x = 0; y = 0.707106781; z = 0.707106781; } else { x = Math.sqrt( xx ); y = xy / x; z = xz / x; } } else if ( yy > zz ) { // m22 is the largest diagonal term if ( yy < epsilon ) { x = 0.707106781; y = 0; z = 0.707106781; } else { y = Math.sqrt( yy ); x = xy / y; z = yz / y; } } else { // m33 is the largest diagonal term so base result on this if ( zz < epsilon ) { x = 0.707106781; y = 0.707106781; z = 0; } else { z = Math.sqrt( zz ); x = xz / z; y = yz / z; } } this.set( x, y, z, angle ); return this; // return 180 deg rotation } // as we have reached here there are no singularities so we can handle normally var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) + ( m13 - m31 ) * ( m13 - m31 ) + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize if ( Math.abs( s ) < 0.001 ) s = 1; // prevent divide by zero, should not happen if matrix is orthogonal and should be // caught by singularity test above, but I've left it in just in case this.x = ( m32 - m23 ) / s; this.y = ( m13 - m31 ) / s; this.z = ( m21 - m12 ) / s; this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 ); return this; }, min: function ( v ) { if ( this.x > v.x ) { this.x = v.x; } if ( this.y > v.y ) { this.y = v.y; } if ( this.z > v.z ) { this.z = v.z; } if ( this.w > v.w ) { this.w = v.w; } return this; }, max: function ( v ) { if ( this.x < v.x ) { this.x = v.x; } if ( this.y < v.y ) { this.y = v.y; } if ( this.z < v.z ) { this.z = v.z; } if ( this.w < v.w ) { this.w = v.w; } return this; }, clamp: function ( min, max ) { // This function assumes min < max, if this assumption isn't true it will not operate correctly if ( this.x < min.x ) { this.x = min.x; } else if ( this.x > max.x ) { this.x = max.x; } if ( this.y < min.y ) { this.y = min.y; } else if ( this.y > max.y ) { this.y = max.y; } if ( this.z < min.z ) { this.z = min.z; } else if ( this.z > max.z ) { this.z = max.z; } if ( this.w < min.w ) { this.w = min.w; } else if ( this.w > max.w ) { this.w = max.w; } return this; }, negate: function() { return this.multiplyScalar( -1 ); }, dot: function ( v ) { return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w; }, lengthSq: function () { return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; }, length: function () { return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w ); }, lengthManhattan: function () { return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w ); }, normalize: function () { return this.divideScalar( this.length() ); }, setLength: function ( l ) { var oldLength = this.length(); if ( oldLength !== 0 && l !== oldLength ) { this.multiplyScalar( l / oldLength ); } return this; }, lerp: function ( v, alpha ) { this.x += ( v.x - this.x ) * alpha; this.y += ( v.y - this.y ) * alpha; this.z += ( v.z - this.z ) * alpha; this.w += ( v.w - this.w ) * alpha; return this; }, equals: function ( v ) { return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) ); }, fromArray: function ( array ) { this.x = array[ 0 ]; this.y = array[ 1 ]; this.z = array[ 2 ]; this.w = array[ 3 ]; return this; }, toArray: function () { return [ this.x, this.y, this.z, this.w ]; }, clone: function () { return new THREE.Vector4( this.x, this.y, this.z, this.w ); } }; /** * @author mrdoob / http://mrdoob.com/ * @author WestLangley / http://github.com/WestLangley * @author bhouston / http://exocortex.com */ THREE.Euler = function ( x, y, z, order ) { this._x = x || 0; this._y = y || 0; this._z = z || 0; this._order = order || THREE.Euler.DefaultOrder; }; THREE.Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]; THREE.Euler.DefaultOrder = 'XYZ'; THREE.Euler.prototype = { constructor: THREE.Euler, _x: 0, _y: 0, _z: 0, _order: THREE.Euler.DefaultOrder, _quaternion: undefined, _updateQuaternion: function () { if ( this._quaternion !== undefined ) { this._quaternion.setFromEuler( this, false ); } }, get x () { return this._x; }, set x ( value ) { this._x = value; this._updateQuaternion(); }, get y () { return this._y; }, set y ( value ) { this._y = value; this._updateQuaternion(); }, get z () { return this._z; }, set z ( value ) { this._z = value; this._updateQuaternion(); }, get order () { return this._order; }, set order ( value ) { this._order = value; this._updateQuaternion(); }, set: function ( x, y, z, order ) { this._x = x; this._y = y; this._z = z; this._order = order || this._order; this._updateQuaternion(); return this; }, copy: function ( euler ) { this._x = euler._x; this._y = euler._y; this._z = euler._z; this._order = euler._order; this._updateQuaternion(); return this; }, setFromRotationMatrix: function ( m, order ) { // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) // clamp, to handle numerical problems function clamp( x ) { return Math.min( Math.max( x, -1 ), 1 ); } var te = m.elements; var m11 = te[0], m12 = te[4], m13 = te[8]; var m21 = te[1], m22 = te[5], m23 = te[9]; var m31 = te[2], m32 = te[6], m33 = te[10]; order = order || this._order; if ( order === 'XYZ' ) { this._y = Math.asin( clamp( m13 ) ); if ( Math.abs( m13 ) < 0.99999 ) { this._x = Math.atan2( - m23, m33 ); this._z = Math.atan2( - m12, m11 ); } else { this._x = Math.atan2( m32, m22 ); this._z = 0; } } else if ( order === 'YXZ' ) { this._x = Math.asin( - clamp( m23 ) ); if ( Math.abs( m23 ) < 0.99999 ) { this._y = Math.atan2( m13, m33 ); this._z = Math.atan2( m21, m22 ); } else { this._y = Math.atan2( - m31, m11 ); this._z = 0; } } else if ( order === 'ZXY' ) { this._x = Math.asin( clamp( m32 ) ); if ( Math.abs( m32 ) < 0.99999 ) { this._y = Math.atan2( - m31, m33 ); this._z = Math.atan2( - m12, m22 ); } else { this._y = 0; this._z = Math.atan2( m21, m11 ); } } else if ( order === 'ZYX' ) { this._y = Math.asin( - clamp( m31 ) ); if ( Math.abs( m31 ) < 0.99999 ) { this._x = Math.atan2( m32, m33 ); this._z = Math.atan2( m21, m11 ); } else { this._x = 0; this._z = Math.atan2( - m12, m22 ); } } else if ( order === 'YZX' ) { this._z = Math.asin( clamp( m21 ) ); if ( Math.abs( m21 ) < 0.99999 ) { this._x = Math.atan2( - m23, m22 ); this._y = Math.atan2( - m31, m11 ); } else { this._x = 0; this._y = Math.atan2( m13, m33 ); } } else if ( order === 'XZY' ) { this._z = Math.asin( - clamp( m12 ) ); if ( Math.abs( m12 ) < 0.99999 ) { this._x = Math.atan2( m32, m22 ); this._y = Math.atan2( m13, m11 ); } else { this._x = Math.atan2( - m23, m33 ); this._y = 0; } } else { console.warn( 'WARNING: Euler.setFromRotationMatrix() given unsupported order: ' + order ) } this._order = order; this._updateQuaternion(); return this; }, setFromQuaternion: function ( q, order, update ) { // q is assumed to be normalized // clamp, to handle numerical problems function clamp( x ) { return Math.min( Math.max( x, -1 ), 1 ); } // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m var sqx = q.x * q.x; var sqy = q.y * q.y; var sqz = q.z * q.z; var sqw = q.w * q.w; order = order || this._order; if ( order === 'XYZ' ) { this._x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) ); this._y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ) ) ); this._z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) ); } else if ( order === 'YXZ' ) { this._x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ) ) ); this._y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) ); this._z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) ); } else if ( order === 'ZXY' ) { this._x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ) ) ); this._y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) ); this._z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) ); } else if ( order === 'ZYX' ) { this._x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) ); this._y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ) ) ); this._z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) ); } else if ( order === 'YZX' ) { this._x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) ); this._y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) ); this._z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ) ) ); } else if ( order === 'XZY' ) { this._x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) ); this._y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) ); this._z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ) ) ); } else { console.warn( 'WARNING: Euler.setFromQuaternion() given unsupported order: ' + order ) } this._order = order; if ( update !== false ) this._updateQuaternion(); return this; }, reorder: function () { // WARNING: this discards revolution information -bhouston var q = new THREE.Quaternion(); return function ( newOrder ) { q.setFromEuler( this ); this.setFromQuaternion( q, newOrder ); }; }(), fromArray: function ( array ) { this._x = array[ 0 ]; this._y = array[ 1 ]; this._z = array[ 2 ]; if ( array[ 3 ] !== undefined ) this._order = array[ 3 ]; this._updateQuaternion(); return this; }, toArray: function () { return [ this._x, this._y, this._z, this._order ]; }, equals: function ( euler ) { return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order ); }, clone: function () { return new THREE.Euler( this._x, this._y, this._z, this._order ); } }; /** * @author bhouston / http://exocortex.com */ THREE.Line3 = function ( start, end ) { this.start = ( start !== undefined ) ? start : new THREE.Vector3(); this.end = ( end !== undefined ) ? end : new THREE.Vector3(); }; THREE.Line3.prototype = { constructor: THREE.Line3, set: function ( start, end ) { this.start.copy( start ); this.end.copy( end ); return this; }, copy: function ( line ) { this.start.copy( line.start ); this.end.copy( line.end ); return this; }, center: function ( optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); return result.addVectors( this.start, this.end ).multiplyScalar( 0.5 ); }, delta: function ( optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); return result.subVectors( this.end, this.start ); }, distanceSq: function () { return this.start.distanceToSquared( this.end ); }, distance: function () { return this.start.distanceTo( this.end ); }, at: function ( t, optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); return this.delta( result ).multiplyScalar( t ).add( this.start ); }, closestPointToPointParameter: function() { var startP = new THREE.Vector3(); var startEnd = new THREE.Vector3(); return function ( point, clampToLine ) { startP.subVectors( point, this.start ); startEnd.subVectors( this.end, this.start ); var startEnd2 = startEnd.dot( startEnd ); var startEnd_startP = startEnd.dot( startP ); var t = startEnd_startP / startEnd2; if ( clampToLine ) { t = THREE.Math.clamp( t, 0, 1 ); } return t; }; }(), closestPointToPoint: function ( point, clampToLine, optionalTarget ) { var t = this.closestPointToPointParameter( point, clampToLine ); var result = optionalTarget || new THREE.Vector3(); return this.delta( result ).multiplyScalar( t ).add( this.start ); }, applyMatrix4: function ( matrix ) { this.start.applyMatrix4( matrix ); this.end.applyMatrix4( matrix ); return this; }, equals: function ( line ) { return line.start.equals( this.start ) && line.end.equals( this.end ); }, clone: function () { return new THREE.Line3().copy( this ); } }; /** * @author bhouston / http://exocortex.com */ THREE.Box2 = function ( min, max ) { this.min = ( min !== undefined ) ? min : new THREE.Vector2( Infinity, Infinity ); this.max = ( max !== undefined ) ? max : new THREE.Vector2( -Infinity, -Infinity ); }; THREE.Box2.prototype = { constructor: THREE.Box2, set: function ( min, max ) { this.min.copy( min ); this.max.copy( max ); return this; }, setFromPoints: function ( points ) { if ( points.length > 0 ) { var point = points[ 0 ]; this.min.copy( point ); this.max.copy( point ); for ( var i = 1, il = points.length; i < il; i ++ ) { point = points[ i ]; if ( point.x < this.min.x ) { this.min.x = point.x; } else if ( point.x > this.max.x ) { this.max.x = point.x; } if ( point.y < this.min.y ) { this.min.y = point.y; } else if ( point.y > this.max.y ) { this.max.y = point.y; } } } else { this.makeEmpty(); } return this; }, setFromCenterAndSize: function () { var v1 = new THREE.Vector2(); return function ( center, size ) { var halfSize = v1.copy( size ).multiplyScalar( 0.5 ); this.min.copy( center ).sub( halfSize ); this.max.copy( center ).add( halfSize ); return this; }; }(), copy: function ( box ) { this.min.copy( box.min ); this.max.copy( box.max ); return this; }, makeEmpty: function () { this.min.x = this.min.y = Infinity; this.max.x = this.max.y = -Infinity; return this; }, empty: function () { // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ); }, center: function ( optionalTarget ) { var result = optionalTarget || new THREE.Vector2(); return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 ); }, size: function ( optionalTarget ) { var result = optionalTarget || new THREE.Vector2(); return result.subVectors( this.max, this.min ); }, expandByPoint: function ( point ) { this.min.min( point ); this.max.max( point ); return this; }, expandByVector: function ( vector ) { this.min.sub( vector ); this.max.add( vector ); return this; }, expandByScalar: function ( scalar ) { this.min.addScalar( -scalar ); this.max.addScalar( scalar ); return this; }, containsPoint: function ( point ) { if ( point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ) { return false; } return true; }, containsBox: function ( box ) { if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) && ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) ) { return true; } return false; }, getParameter: function ( point, optionalTarget ) { // This can potentially have a divide by zero if the box // has a size dimension of 0. var result = optionalTarget || new THREE.Vector2(); return result.set( ( point.x - this.min.x ) / ( this.max.x - this.min.x ), ( point.y - this.min.y ) / ( this.max.y - this.min.y ) ); }, isIntersectionBox: function ( box ) { // using 6 splitting planes to rule out intersections. if ( box.max.x < this.min.x || box.min.x > this.max.x || box.max.y < this.min.y || box.min.y > this.max.y ) { return false; } return true; }, clampPoint: function ( point, optionalTarget ) { var result = optionalTarget || new THREE.Vector2(); return result.copy( point ).clamp( this.min, this.max ); }, distanceToPoint: function () { var v1 = new THREE.Vector2(); return function ( point ) { var clampedPoint = v1.copy( point ).clamp( this.min, this.max ); return clampedPoint.sub( point ).length(); }; }(), intersect: function ( box ) { this.min.max( box.min ); this.max.min( box.max ); return this; }, union: function ( box ) { this.min.min( box.min ); this.max.max( box.max ); return this; }, translate: function ( offset ) { this.min.add( offset ); this.max.add( offset ); return this; }, equals: function ( box ) { return box.min.equals( this.min ) && box.max.equals( this.max ); }, clone: function () { return new THREE.Box2().copy( this ); } }; /** * @author bhouston / http://exocortex.com * @author WestLangley / http://github.com/WestLangley */ THREE.Box3 = function ( min, max ) { this.min = ( min !== undefined ) ? min : new THREE.Vector3( Infinity, Infinity, Infinity ); this.max = ( max !== undefined ) ? max : new THREE.Vector3( -Infinity, -Infinity, -Infinity ); }; THREE.Box3.prototype = { constructor: THREE.Box3, set: function ( min, max ) { this.min.copy( min ); this.max.copy( max ); return this; }, addPoint: function ( point ) { if ( point.x < this.min.x ) { this.min.x = point.x; } else if ( point.x > this.max.x ) { this.max.x = point.x; } if ( point.y < this.min.y ) { this.min.y = point.y; } else if ( point.y > this.max.y ) { this.max.y = point.y; } if ( point.z < this.min.z ) { this.min.z = point.z; } else if ( point.z > this.max.z ) { this.max.z = point.z; } }, setFromPoints: function ( points ) { if ( points.length > 0 ) { var point = points[ 0 ]; this.min.copy( point ); this.max.copy( point ); for ( var i = 1, il = points.length; i < il; i ++ ) { this.addPoint( points[ i ] ) } } else { this.makeEmpty(); } return this; }, setFromCenterAndSize: function() { var v1 = new THREE.Vector3(); return function ( center, size ) { var halfSize = v1.copy( size ).multiplyScalar( 0.5 ); this.min.copy( center ).sub( halfSize ); this.max.copy( center ).add( halfSize ); return this; }; }(), setFromObject: function() { // Computes the world-axis-aligned bounding box of an object (including its children), // accounting for both the object's, and childrens', world transforms var v1 = new THREE.Vector3(); return function( object ) { var scope = this; object.updateMatrixWorld( true ); this.makeEmpty(); object.traverse( function ( node ) { if ( node.geometry !== undefined && node.geometry.vertices !== undefined ) { var vertices = node.geometry.vertices; for ( var i = 0, il = vertices.length; i < il; i++ ) { v1.copy( vertices[ i ] ); v1.applyMatrix4( node.matrixWorld ); scope.expandByPoint( v1 ); } } } ); return this; }; }(), copy: function ( box ) { this.min.copy( box.min ); this.max.copy( box.max ); return this; }, makeEmpty: function () { this.min.x = this.min.y = this.min.z = Infinity; this.max.x = this.max.y = this.max.z = -Infinity; return this; }, empty: function () { // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z ); }, center: function ( optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 ); }, size: function ( optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); return result.subVectors( this.max, this.min ); }, expandByPoint: function ( point ) { this.min.min( point ); this.max.max( point ); return this; }, expandByVector: function ( vector ) { this.min.sub( vector ); this.max.add( vector ); return this; }, expandByScalar: function ( scalar ) { this.min.addScalar( -scalar ); this.max.addScalar( scalar ); return this; }, containsPoint: function ( point ) { if ( point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y || point.z < this.min.z || point.z > this.max.z ) { return false; } return true; }, containsBox: function ( box ) { if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) && ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) && ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) { return true; } return false; }, getParameter: function ( point, optionalTarget ) { // This can potentially have a divide by zero if the box // has a size dimension of 0. var result = optionalTarget || new THREE.Vector3(); return result.set( ( point.x - this.min.x ) / ( this.max.x - this.min.x ), ( point.y - this.min.y ) / ( this.max.y - this.min.y ), ( point.z - this.min.z ) / ( this.max.z - this.min.z ) ); }, isIntersectionBox: function ( box ) { // using 6 splitting planes to rule out intersections. if ( box.max.x < this.min.x || box.min.x > this.max.x || box.max.y < this.min.y || box.min.y > this.max.y || box.max.z < this.min.z || box.min.z > this.max.z ) { return false; } return true; }, clampPoint: function ( point, optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); return result.copy( point ).clamp( this.min, this.max ); }, distanceToPoint: function() { var v1 = new THREE.Vector3(); return function ( point ) { var clampedPoint = v1.copy( point ).clamp( this.min, this.max ); return clampedPoint.sub( point ).length(); }; }(), getBoundingSphere: function() { var v1 = new THREE.Vector3(); return function ( optionalTarget ) { var result = optionalTarget || new THREE.Sphere(); result.center = this.center(); result.radius = this.size( v1 ).length() * 0.5; return result; }; }(), intersect: function ( box ) { this.min.max( box.min ); this.max.min( box.max ); return this; }, union: function ( box ) { this.min.min( box.min ); this.max.max( box.max ); return this; }, applyMatrix4: function() { var points = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ]; return function ( matrix ) { // NOTE: I am using a binary pattern to specify all 2^3 combinations below points[0].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000 points[1].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001 points[2].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010 points[3].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011 points[4].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100 points[5].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101 points[6].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110 points[7].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111 this.makeEmpty(); this.setFromPoints( points ); return this; }; }(), translate: function ( offset ) { this.min.add( offset ); this.max.add( offset ); return this; }, equals: function ( box ) { return box.min.equals( this.min ) && box.max.equals( this.max ); }, clone: function () { return new THREE.Box3().copy( this ); } }; /** * @author alteredq / http://alteredqualia.com/ * @author WestLangley / http://github.com/WestLangley * @author bhouston / http://exocortex.com */ THREE.Matrix3 = function ( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) { this.elements = new Float32Array(9); this.set( ( n11 !== undefined ) ? n11 : 1, n12 || 0, n13 || 0, n21 || 0, ( n22 !== undefined ) ? n22 : 1, n23 || 0, n31 || 0, n32 || 0, ( n33 !== undefined ) ? n33 : 1 ); }; THREE.Matrix3.prototype = { constructor: THREE.Matrix3, set: function ( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) { var te = this.elements; te[0] = n11; te[3] = n12; te[6] = n13; te[1] = n21; te[4] = n22; te[7] = n23; te[2] = n31; te[5] = n32; te[8] = n33; return this; }, identity: function () { this.set( 1, 0, 0, 0, 1, 0, 0, 0, 1 ); return this; }, copy: function ( m ) { var me = m.elements; this.set( me[0], me[3], me[6], me[1], me[4], me[7], me[2], me[5], me[8] ); return this; }, multiplyVector3: function ( vector ) { console.warn( 'DEPRECATED: Matrix3\'s .multiplyVector3() has been removed. Use vector.applyMatrix3( matrix ) instead.' ); return vector.applyMatrix3( this ); }, multiplyVector3Array: function() { var v1 = new THREE.Vector3(); return function ( a ) { for ( var i = 0, il = a.length; i < il; i += 3 ) { v1.x = a[ i ]; v1.y = a[ i + 1 ]; v1.z = a[ i + 2 ]; v1.applyMatrix3(this); a[ i ] = v1.x; a[ i + 1 ] = v1.y; a[ i + 2 ] = v1.z; } return a; }; }(), multiplyScalar: function ( s ) { var te = this.elements; te[0] *= s; te[3] *= s; te[6] *= s; te[1] *= s; te[4] *= s; te[7] *= s; te[2] *= s; te[5] *= s; te[8] *= s; return this; }, determinant: function () { var te = this.elements; var a = te[0], b = te[1], c = te[2], d = te[3], e = te[4], f = te[5], g = te[6], h = te[7], i = te[8]; return a*e*i - a*f*h - b*d*i + b*f*g + c*d*h - c*e*g; }, getInverse: function ( matrix, throwOnInvertible ) { // input: THREE.Matrix4 // ( based on http://code.google.com/p/webgl-mjs/ ) var me = matrix.elements; var te = this.elements; te[ 0 ] = me[10] * me[5] - me[6] * me[9]; te[ 1 ] = - me[10] * me[1] + me[2] * me[9]; te[ 2 ] = me[6] * me[1] - me[2] * me[5]; te[ 3 ] = - me[10] * me[4] + me[6] * me[8]; te[ 4 ] = me[10] * me[0] - me[2] * me[8]; te[ 5 ] = - me[6] * me[0] + me[2] * me[4]; te[ 6 ] = me[9] * me[4] - me[5] * me[8]; te[ 7 ] = - me[9] * me[0] + me[1] * me[8]; te[ 8 ] = me[5] * me[0] - me[1] * me[4]; var det = me[ 0 ] * te[ 0 ] + me[ 1 ] * te[ 3 ] + me[ 2 ] * te[ 6 ]; // no inverse if ( det === 0 ) { var msg = "Matrix3.getInverse(): can't invert matrix, determinant is 0"; if ( throwOnInvertible || false ) { throw new Error( msg ); } else { console.warn( msg ); } this.identity(); return this; } this.multiplyScalar( 1.0 / det ); return this; }, transpose: function () { var tmp, m = this.elements; tmp = m[1]; m[1] = m[3]; m[3] = tmp; tmp = m[2]; m[2] = m[6]; m[6] = tmp; tmp = m[5]; m[5] = m[7]; m[7] = tmp; return this; }, getNormalMatrix: function ( m ) { // input: THREE.Matrix4 this.getInverse( m ).transpose(); return this; }, transposeIntoArray: function ( r ) { var m = this.elements; r[ 0 ] = m[ 0 ]; r[ 1 ] = m[ 3 ]; r[ 2 ] = m[ 6 ]; r[ 3 ] = m[ 1 ]; r[ 4 ] = m[ 4 ]; r[ 5 ] = m[ 7 ]; r[ 6 ] = m[ 2 ]; r[ 7 ] = m[ 5 ]; r[ 8 ] = m[ 8 ]; return this; }, fromArray: function ( array ) { this.elements.set( array ); return this; }, toArray: function () { var te = this.elements; return [ te[ 0 ], te[ 1 ], te[ 2 ], te[ 3 ], te[ 4 ], te[ 5 ], te[ 6 ], te[ 7 ], te[ 8 ] ]; }, clone: function () { var te = this.elements; return new THREE.Matrix3( te[0], te[3], te[6], te[1], te[4], te[7], te[2], te[5], te[8] ); } }; /** * @author mrdoob / http://mrdoob.com/ * @author supereggbert / http://www.paulbrunt.co.uk/ * @author philogb / http://blog.thejit.org/ * @author jordi_ros / http://plattsoft.com * @author D1plo1d / http://github.com/D1plo1d * @author alteredq / http://alteredqualia.com/ * @author mikael emtinger / http://gomo.se/ * @author timknip / http://www.floorplanner.com/ * @author bhouston / http://exocortex.com * @author WestLangley / http://github.com/WestLangley */ THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) { this.elements = new Float32Array( 16 ); // TODO: if n11 is undefined, then just set to identity, otherwise copy all other values into matrix // we should not support semi specification of Matrix4, it is just weird. var te = this.elements; te[0] = ( n11 !== undefined ) ? n11 : 1; te[4] = n12 || 0; te[8] = n13 || 0; te[12] = n14 || 0; te[1] = n21 || 0; te[5] = ( n22 !== undefined ) ? n22 : 1; te[9] = n23 || 0; te[13] = n24 || 0; te[2] = n31 || 0; te[6] = n32 || 0; te[10] = ( n33 !== undefined ) ? n33 : 1; te[14] = n34 || 0; te[3] = n41 || 0; te[7] = n42 || 0; te[11] = n43 || 0; te[15] = ( n44 !== undefined ) ? n44 : 1; }; THREE.Matrix4.prototype = { constructor: THREE.Matrix4, set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) { var te = this.elements; te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14; te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24; te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34; te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44; return this; }, identity: function () { this.set( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); return this; }, copy: function ( m ) { this.elements.set( m.elements ); return this; }, extractPosition: function ( m ) { console.warn( 'DEPRECATED: Matrix4\'s .extractPosition() has been renamed to .copyPosition().' ); return this.copyPosition( m ); }, copyPosition: function ( m ) { var te = this.elements; var me = m.elements; te[12] = me[12]; te[13] = me[13]; te[14] = me[14]; return this; }, extractRotation: function () { var v1 = new THREE.Vector3(); return function ( m ) { var te = this.elements; var me = m.elements; var scaleX = 1 / v1.set( me[0], me[1], me[2] ).length(); var scaleY = 1 / v1.set( me[4], me[5], me[6] ).length(); var scaleZ = 1 / v1.set( me[8], me[9], me[10] ).length(); te[0] = me[0] * scaleX; te[1] = me[1] * scaleX; te[2] = me[2] * scaleX; te[4] = me[4] * scaleY; te[5] = me[5] * scaleY; te[6] = me[6] * scaleY; te[8] = me[8] * scaleZ; te[9] = me[9] * scaleZ; te[10] = me[10] * scaleZ; return this; }; }(), makeRotationFromEuler: function ( euler ) { if ( euler instanceof THREE.Euler === false ) { console.error( 'ERROR: Matrix\'s .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order. Please update your code.' ); } var te = this.elements; var x = euler.x, y = euler.y, z = euler.z; var a = Math.cos( x ), b = Math.sin( x ); var c = Math.cos( y ), d = Math.sin( y ); var e = Math.cos( z ), f = Math.sin( z ); if ( euler.order === 'XYZ' ) { var ae = a * e, af = a * f, be = b * e, bf = b * f; te[0] = c * e; te[4] = - c * f; te[8] = d; te[1] = af + be * d; te[5] = ae - bf * d; te[9] = - b * c; te[2] = bf - ae * d; te[6] = be + af * d; te[10] = a * c; } else if ( euler.order === 'YXZ' ) { var ce = c * e, cf = c * f, de = d * e, df = d * f; te[0] = ce + df * b; te[4] = de * b - cf; te[8] = a * d; te[1] = a * f; te[5] = a * e; te[9] = - b; te[2] = cf * b - de; te[6] = df + ce * b; te[10] = a * c; } else if ( euler.order === 'ZXY' ) { var ce = c * e, cf = c * f, de = d * e, df = d * f; te[0] = ce - df * b; te[4] = - a * f; te[8] = de + cf * b; te[1] = cf + de * b; te[5] = a * e; te[9] = df - ce * b; te[2] = - a * d; te[6] = b; te[10] = a * c; } else if ( euler.order === 'ZYX' ) { var ae = a * e, af = a * f, be = b * e, bf = b * f; te[0] = c * e; te[4] = be * d - af; te[8] = ae * d + bf; te[1] = c * f; te[5] = bf * d + ae; te[9] = af * d - be; te[2] = - d; te[6] = b * c; te[10] = a * c; } else if ( euler.order === 'YZX' ) { var ac = a * c, ad = a * d, bc = b * c, bd = b * d; te[0] = c * e; te[4] = bd - ac * f; te[8] = bc * f + ad; te[1] = f; te[5] = a * e; te[9] = - b * e; te[2] = - d * e; te[6] = ad * f + bc; te[10] = ac - bd * f; } else if ( euler.order === 'XZY' ) { var ac = a * c, ad = a * d, bc = b * c, bd = b * d; te[0] = c * e; te[4] = - f; te[8] = d * e; te[1] = ac * f + bd; te[5] = a * e; te[9] = ad * f - bc; te[2] = bc * f - ad; te[6] = b * e; te[10] = bd * f + ac; } // last column te[3] = 0; te[7] = 0; te[11] = 0; // bottom row te[12] = 0; te[13] = 0; te[14] = 0; te[15] = 1; return this; }, setRotationFromQuaternion: function ( q ) { console.warn( 'DEPRECATED: Matrix4\'s .setRotationFromQuaternion() has been deprecated in favor of makeRotationFromQuaternion. Please update your code.' ); return this.makeRotationFromQuaternion( q ); }, makeRotationFromQuaternion: function ( q ) { var te = this.elements; var x = q.x, y = q.y, z = q.z, w = q.w; var x2 = x + x, y2 = y + y, z2 = z + z; var xx = x * x2, xy = x * y2, xz = x * z2; var yy = y * y2, yz = y * z2, zz = z * z2; var wx = w * x2, wy = w * y2, wz = w * z2; te[0] = 1 - ( yy + zz ); te[4] = xy - wz; te[8] = xz + wy; te[1] = xy + wz; te[5] = 1 - ( xx + zz ); te[9] = yz - wx; te[2] = xz - wy; te[6] = yz + wx; te[10] = 1 - ( xx + yy ); // last column te[3] = 0; te[7] = 0; te[11] = 0; // bottom row te[12] = 0; te[13] = 0; te[14] = 0; te[15] = 1; return this; }, lookAt: function() { var x = new THREE.Vector3(); var y = new THREE.Vector3(); var z = new THREE.Vector3(); return function ( eye, target, up ) { var te = this.elements; z.subVectors( eye, target ).normalize(); if ( z.length() === 0 ) { z.z = 1; } x.crossVectors( up, z ).normalize(); if ( x.length() === 0 ) { z.x += 0.0001; x.crossVectors( up, z ).normalize(); } y.crossVectors( z, x ); te[0] = x.x; te[4] = y.x; te[8] = z.x; te[1] = x.y; te[5] = y.y; te[9] = z.y; te[2] = x.z; te[6] = y.z; te[10] = z.z; return this; }; }(), multiply: function ( m, n ) { if ( n !== undefined ) { console.warn( 'DEPRECATED: Matrix4\'s .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.' ); return this.multiplyMatrices( m, n ); } return this.multiplyMatrices( this, m ); }, multiplyMatrices: function ( a, b ) { var ae = a.elements; var be = b.elements; var te = this.elements; var a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12]; var a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13]; var a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14]; var a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15]; var b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12]; var b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13]; var b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14]; var b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15]; te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41; te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42; te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43; te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44; te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41; te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42; te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43; te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44; te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41; te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42; te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43; te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44; te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41; te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42; te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43; te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44; return this; }, multiplyToArray: function ( a, b, r ) { var te = this.elements; this.multiplyMatrices( a, b ); r[ 0 ] = te[0]; r[ 1 ] = te[1]; r[ 2 ] = te[2]; r[ 3 ] = te[3]; r[ 4 ] = te[4]; r[ 5 ] = te[5]; r[ 6 ] = te[6]; r[ 7 ] = te[7]; r[ 8 ] = te[8]; r[ 9 ] = te[9]; r[ 10 ] = te[10]; r[ 11 ] = te[11]; r[ 12 ] = te[12]; r[ 13 ] = te[13]; r[ 14 ] = te[14]; r[ 15 ] = te[15]; return this; }, multiplyScalar: function ( s ) { var te = this.elements; te[0] *= s; te[4] *= s; te[8] *= s; te[12] *= s; te[1] *= s; te[5] *= s; te[9] *= s; te[13] *= s; te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s; te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s; return this; }, multiplyVector3: function ( vector ) { console.warn( 'DEPRECATED: Matrix4\'s .multiplyVector3() has been removed. Use vector.applyMatrix4( matrix ) or vector.applyProjection( matrix ) instead.' ); return vector.applyProjection( this ); }, multiplyVector4: function ( vector ) { console.warn( 'DEPRECATED: Matrix4\'s .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' ); return vector.applyMatrix4( this ); }, multiplyVector3Array: function() { var v1 = new THREE.Vector3(); return function ( a ) { for ( var i = 0, il = a.length; i < il; i += 3 ) { v1.x = a[ i ]; v1.y = a[ i + 1 ]; v1.z = a[ i + 2 ]; v1.applyProjection( this ); a[ i ] = v1.x; a[ i + 1 ] = v1.y; a[ i + 2 ] = v1.z; } return a; }; }(), rotateAxis: function ( v ) { console.warn( 'DEPRECATED: Matrix4\'s .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' ); v.transformDirection( this ); }, crossVector: function ( vector ) { console.warn( 'DEPRECATED: Matrix4\'s .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' ); return vector.applyMatrix4( this ); }, determinant: function () { var te = this.elements; var n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12]; var n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13]; var n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14]; var n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15]; //TODO: make this more efficient //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm ) return ( n41 * ( +n14 * n23 * n32 -n13 * n24 * n32 -n14 * n22 * n33 +n12 * n24 * n33 +n13 * n22 * n34 -n12 * n23 * n34 ) + n42 * ( +n11 * n23 * n34 -n11 * n24 * n33 +n14 * n21 * n33 -n13 * n21 * n34 +n13 * n24 * n31 -n14 * n23 * n31 ) + n43 * ( +n11 * n24 * n32 -n11 * n22 * n34 -n14 * n21 * n32 +n12 * n21 * n34 +n14 * n22 * n31 -n12 * n24 * n31 ) + n44 * ( -n13 * n22 * n31 -n11 * n23 * n32 +n11 * n22 * n33 +n13 * n21 * n32 -n12 * n21 * n33 +n12 * n23 * n31 ) ); }, transpose: function () { var te = this.elements; var tmp; tmp = te[1]; te[1] = te[4]; te[4] = tmp; tmp = te[2]; te[2] = te[8]; te[8] = tmp; tmp = te[6]; te[6] = te[9]; te[9] = tmp; tmp = te[3]; te[3] = te[12]; te[12] = tmp; tmp = te[7]; te[7] = te[13]; te[13] = tmp; tmp = te[11]; te[11] = te[14]; te[14] = tmp; return this; }, flattenToArray: function ( flat ) { var te = this.elements; flat[ 0 ] = te[0]; flat[ 1 ] = te[1]; flat[ 2 ] = te[2]; flat[ 3 ] = te[3]; flat[ 4 ] = te[4]; flat[ 5 ] = te[5]; flat[ 6 ] = te[6]; flat[ 7 ] = te[7]; flat[ 8 ] = te[8]; flat[ 9 ] = te[9]; flat[ 10 ] = te[10]; flat[ 11 ] = te[11]; flat[ 12 ] = te[12]; flat[ 13 ] = te[13]; flat[ 14 ] = te[14]; flat[ 15 ] = te[15]; return flat; }, flattenToArrayOffset: function( flat, offset ) { var te = this.elements; flat[ offset ] = te[0]; flat[ offset + 1 ] = te[1]; flat[ offset + 2 ] = te[2]; flat[ offset + 3 ] = te[3]; flat[ offset + 4 ] = te[4]; flat[ offset + 5 ] = te[5]; flat[ offset + 6 ] = te[6]; flat[ offset + 7 ] = te[7]; flat[ offset + 8 ] = te[8]; flat[ offset + 9 ] = te[9]; flat[ offset + 10 ] = te[10]; flat[ offset + 11 ] = te[11]; flat[ offset + 12 ] = te[12]; flat[ offset + 13 ] = te[13]; flat[ offset + 14 ] = te[14]; flat[ offset + 15 ] = te[15]; return flat; }, getPosition: function() { var v1 = new THREE.Vector3(); return function () { console.warn( 'DEPRECATED: Matrix4\'s .getPosition() has been removed. Use Vector3.setFromMatrixPosition( matrix ) instead.' ); var te = this.elements; return v1.set( te[12], te[13], te[14] ); }; }(), setPosition: function ( v ) { var te = this.elements; te[12] = v.x; te[13] = v.y; te[14] = v.z; return this; }, getInverse: function ( m, throwOnInvertible ) { // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm var te = this.elements; var me = m.elements; var n11 = me[0], n12 = me[4], n13 = me[8], n14 = me[12]; var n21 = me[1], n22 = me[5], n23 = me[9], n24 = me[13]; var n31 = me[2], n32 = me[6], n33 = me[10], n34 = me[14]; var n41 = me[3], n42 = me[7], n43 = me[11], n44 = me[15]; te[0] = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44; te[4] = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44; te[8] = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44; te[12] = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34; te[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44; te[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44; te[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44; te[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34; te[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44; te[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44; te[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44; te[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34; te[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43; te[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43; te[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43; te[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33; var det = n11 * te[ 0 ] + n21 * te[ 4 ] + n31 * te[ 8 ] + n41 * te[ 12 ]; if ( det == 0 ) { var msg = "Matrix4.getInverse(): can't invert matrix, determinant is 0"; if ( throwOnInvertible || false ) { throw new Error( msg ); } else { console.warn( msg ); } this.identity(); return this; } this.multiplyScalar( 1 / det ); return this; }, translate: function ( v ) { console.warn( 'DEPRECATED: Matrix4\'s .translate() has been removed.'); }, rotateX: function ( angle ) { console.warn( 'DEPRECATED: Matrix4\'s .rotateX() has been removed.'); }, rotateY: function ( angle ) { console.warn( 'DEPRECATED: Matrix4\'s .rotateY() has been removed.'); }, rotateZ: function ( angle ) { console.warn( 'DEPRECATED: Matrix4\'s .rotateZ() has been removed.'); }, rotateByAxis: function ( axis, angle ) { console.warn( 'DEPRECATED: Matrix4\'s .rotateByAxis() has been removed.'); }, scale: function ( v ) { var te = this.elements; var x = v.x, y = v.y, z = v.z; te[0] *= x; te[4] *= y; te[8] *= z; te[1] *= x; te[5] *= y; te[9] *= z; te[2] *= x; te[6] *= y; te[10] *= z; te[3] *= x; te[7] *= y; te[11] *= z; return this; }, getMaxScaleOnAxis: function () { var te = this.elements; var scaleXSq = te[0] * te[0] + te[1] * te[1] + te[2] * te[2]; var scaleYSq = te[4] * te[4] + te[5] * te[5] + te[6] * te[6]; var scaleZSq = te[8] * te[8] + te[9] * te[9] + te[10] * te[10]; return Math.sqrt( Math.max( scaleXSq, Math.max( scaleYSq, scaleZSq ) ) ); }, makeTranslation: function ( x, y, z ) { this.set( 1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1 ); return this; }, makeRotationX: function ( theta ) { var c = Math.cos( theta ), s = Math.sin( theta ); this.set( 1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1 ); return this; }, makeRotationY: function ( theta ) { var c = Math.cos( theta ), s = Math.sin( theta ); this.set( c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1 ); return this; }, makeRotationZ: function ( theta ) { var c = Math.cos( theta ), s = Math.sin( theta ); this.set( c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); return this; }, makeRotationAxis: function ( axis, angle ) { // Based on http://www.gamedev.net/reference/articles/article1199.asp var c = Math.cos( angle ); var s = Math.sin( angle ); var t = 1 - c; var x = axis.x, y = axis.y, z = axis.z; var tx = t * x, ty = t * y; this.set( tx * x + c, tx * y - s * z, tx * z + s * y, 0, tx * y + s * z, ty * y + c, ty * z - s * x, 0, tx * z - s * y, ty * z + s * x, t * z * z + c, 0, 0, 0, 0, 1 ); return this; }, makeScale: function ( x, y, z ) { this.set( x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 ); return this; }, compose: function ( position, quaternion, scale ) { this.makeRotationFromQuaternion( quaternion ); this.scale( scale ); this.setPosition( position ); return this; }, decompose: function () { var vector = new THREE.Vector3(); var matrix = new THREE.Matrix4(); return function ( position, quaternion, scale ) { var te = this.elements; var sx = vector.set( te[0], te[1], te[2] ).length(); var sy = vector.set( te[4], te[5], te[6] ).length(); var sz = vector.set( te[8], te[9], te[10] ).length(); // if determine is negative, we need to invert one scale var det = this.determinant(); if( det < 0 ) { sx = -sx; } position.x = te[12]; position.y = te[13]; position.z = te[14]; // scale the rotation part matrix.elements.set( this.elements ); // at this point matrix is incomplete so we can't use .copy() var invSX = 1 / sx; var invSY = 1 / sy; var invSZ = 1 / sz; matrix.elements[0] *= invSX; matrix.elements[1] *= invSX; matrix.elements[2] *= invSX; matrix.elements[4] *= invSY; matrix.elements[5] *= invSY; matrix.elements[6] *= invSY; matrix.elements[8] *= invSZ; matrix.elements[9] *= invSZ; matrix.elements[10] *= invSZ; quaternion.setFromRotationMatrix( matrix ); scale.x = sx; scale.y = sy; scale.z = sz; return this; }; }(), makeFrustum: function ( left, right, bottom, top, near, far ) { var te = this.elements; var x = 2 * near / ( right - left ); var y = 2 * near / ( top - bottom ); var a = ( right + left ) / ( right - left ); var b = ( top + bottom ) / ( top - bottom ); var c = - ( far + near ) / ( far - near ); var d = - 2 * far * near / ( far - near ); te[0] = x; te[4] = 0; te[8] = a; te[12] = 0; te[1] = 0; te[5] = y; te[9] = b; te[13] = 0; te[2] = 0; te[6] = 0; te[10] = c; te[14] = d; te[3] = 0; te[7] = 0; te[11] = - 1; te[15] = 0; return this; }, makePerspective: function ( fov, aspect, near, far ) { var ymax = near * Math.tan( THREE.Math.degToRad( fov * 0.5 ) ); var ymin = - ymax; var xmin = ymin * aspect; var xmax = ymax * aspect; return this.makeFrustum( xmin, xmax, ymin, ymax, near, far ); }, makeOrthographic: function ( left, right, top, bottom, near, far ) { var te = this.elements; var w = right - left; var h = top - bottom; var p = far - near; var x = ( right + left ) / w; var y = ( top + bottom ) / h; var z = ( far + near ) / p; te[0] = 2 / w; te[4] = 0; te[8] = 0; te[12] = -x; te[1] = 0; te[5] = 2 / h; te[9] = 0; te[13] = -y; te[2] = 0; te[6] = 0; te[10] = -2/p; te[14] = -z; te[3] = 0; te[7] = 0; te[11] = 0; te[15] = 1; return this; }, fromArray: function ( array ) { this.elements.set( array ); return this; }, toArray: function () { var te = this.elements; return [ te[ 0 ], te[ 1 ], te[ 2 ], te[ 3 ], te[ 4 ], te[ 5 ], te[ 6 ], te[ 7 ], te[ 8 ], te[ 9 ], te[ 10 ], te[ 11 ], te[ 12 ], te[ 13 ], te[ 14 ], te[ 15 ] ]; }, clone: function () { var te = this.elements; return new THREE.Matrix4( te[0], te[4], te[8], te[12], te[1], te[5], te[9], te[13], te[2], te[6], te[10], te[14], te[3], te[7], te[11], te[15] ); } }; /** * @author bhouston / http://exocortex.com */ THREE.Ray = function ( origin, direction ) { this.origin = ( origin !== undefined ) ? origin : new THREE.Vector3(); this.direction = ( direction !== undefined ) ? direction : new THREE.Vector3(); }; THREE.Ray.prototype = { constructor: THREE.Ray, set: function ( origin, direction ) { this.origin.copy( origin ); this.direction.copy( direction ); return this; }, copy: function ( ray ) { this.origin.copy( ray.origin ); this.direction.copy( ray.direction ); return this; }, at: function ( t, optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); return result.copy( this.direction ).multiplyScalar( t ).add( this.origin ); }, recast: function () { var v1 = new THREE.Vector3(); return function ( t ) { this.origin.copy( this.at( t, v1 ) ); return this; }; }(), closestPointToPoint: function ( point, optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); result.subVectors( point, this.origin ); var directionDistance = result.dot( this.direction ); if ( directionDistance < 0 ) { return result.copy( this.origin ); } return result.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin ); }, distanceToPoint: function () { var v1 = new THREE.Vector3(); return function ( point ) { var directionDistance = v1.subVectors( point, this.origin ).dot( this.direction ); // point behind the ray if ( directionDistance < 0 ) { return this.origin.distanceTo( point ); } v1.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin ); return v1.distanceTo( point ); }; }(), distanceSqToSegment: function( v0, v1, optionalPointOnRay, optionalPointOnSegment ) { // from http://www.geometrictools.com/LibMathematics/Distance/Wm5DistRay3Segment3.cpp // It returns the min distance between the ray and the segment // defined by v0 and v1 // It can also set two optional targets : // - The closest point on the ray // - The closest point on the segment var segCenter = v0.clone().add( v1 ).multiplyScalar( 0.5 ); var segDir = v1.clone().sub( v0 ).normalize(); var segExtent = v0.distanceTo( v1 ) * 0.5; var diff = this.origin.clone().sub( segCenter ); var a01 = - this.direction.dot( segDir ); var b0 = diff.dot( this.direction ); var b1 = - diff.dot( segDir ); var c = diff.lengthSq(); var det = Math.abs( 1 - a01 * a01 ); var s0, s1, sqrDist, extDet; if ( det >= 0 ) { // The ray and segment are not parallel. s0 = a01 * b1 - b0; s1 = a01 * b0 - b1; extDet = segExtent * det; if ( s0 >= 0 ) { if ( s1 >= - extDet ) { if ( s1 <= extDet ) { // region 0 // Minimum at interior points of ray and segment. var invDet = 1 / det; s0 *= invDet; s1 *= invDet; sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c; } else { // region 1 s1 = segExtent; s0 = Math.max( 0, - ( a01 * s1 + b0) ); sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; } } else { // region 5 s1 = - segExtent; s0 = Math.max( 0, - ( a01 * s1 + b0) ); sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; } } else { if ( s1 <= - extDet) { // region 4 s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) ); s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent ); sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; } else if ( s1 <= extDet ) { // region 3 s0 = 0; s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent ); sqrDist = s1 * ( s1 + 2 * b1 ) + c; } else { // region 2 s0 = Math.max( 0, - ( a01 * segExtent + b0 ) ); s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent ); sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; } } } else { // Ray and segment are parallel. s1 = ( a01 > 0 ) ? - segExtent : segExtent; s0 = Math.max( 0, - ( a01 * s1 + b0 ) ); sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; } if ( optionalPointOnRay ) { optionalPointOnRay.copy( this.direction.clone().multiplyScalar( s0 ).add( this.origin ) ); } if ( optionalPointOnSegment ) { optionalPointOnSegment.copy( segDir.clone().multiplyScalar( s1 ).add( segCenter ) ); } return sqrDist; }, isIntersectionSphere: function ( sphere ) { return this.distanceToPoint( sphere.center ) <= sphere.radius; }, isIntersectionPlane: function ( plane ) { // check if the ray lies on the plane first var distToPoint = plane.distanceToPoint( this.origin ); if ( distToPoint === 0 ) { return true; } var denominator = plane.normal.dot( this.direction ); if ( denominator * distToPoint < 0 ) { return true } // ray origin is behind the plane (and is pointing behind it) return false; }, distanceToPlane: function ( plane ) { var denominator = plane.normal.dot( this.direction ); if ( denominator == 0 ) { // line is coplanar, return origin if( plane.distanceToPoint( this.origin ) == 0 ) { return 0; } // Null is preferable to undefined since undefined means.... it is undefined return null; } var t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator; // Return if the ray never intersects the plane return t >= 0 ? t : null; }, intersectPlane: function ( plane, optionalTarget ) { var t = this.distanceToPlane( plane ); if ( t === null ) { return null; } return this.at( t, optionalTarget ); }, isIntersectionBox: function () { var v = new THREE.Vector3(); return function ( box ) { return this.intersectBox( box, v ) !== null; } }(), intersectBox: function ( box , optionalTarget ) { // http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-box-intersection/ var tmin,tmax,tymin,tymax,tzmin,tzmax; var invdirx = 1/this.direction.x, invdiry = 1/this.direction.y, invdirz = 1/this.direction.z; var origin = this.origin; if (invdirx >= 0) { tmin = (box.min.x - origin.x) * invdirx; tmax = (box.max.x - origin.x) * invdirx; } else { tmin = (box.max.x - origin.x) * invdirx; tmax = (box.min.x - origin.x) * invdirx; } if (invdiry >= 0) { tymin = (box.min.y - origin.y) * invdiry; tymax = (box.max.y - origin.y) * invdiry; } else { tymin = (box.max.y - origin.y) * invdiry; tymax = (box.min.y - origin.y) * invdiry; } if ((tmin > tymax) || (tymin > tmax)) return null; // These lines also handle the case where tmin or tmax is NaN // (result of 0 * Infinity). x !== x returns true if x is NaN if (tymin > tmin || tmin !== tmin ) tmin = tymin; if (tymax < tmax || tmax !== tmax ) tmax = tymax; if (invdirz >= 0) { tzmin = (box.min.z - origin.z) * invdirz; tzmax = (box.max.z - origin.z) * invdirz; } else { tzmin = (box.max.z - origin.z) * invdirz; tzmax = (box.min.z - origin.z) * invdirz; } if ((tmin > tzmax) || (tzmin > tmax)) return null; if (tzmin > tmin || tmin !== tmin ) tmin = tzmin; if (tzmax < tmax || tmax !== tmax ) tmax = tzmax; //return point closest to the ray (positive side) if ( tmax < 0 ) return null; return this.at( tmin >= 0 ? tmin : tmax, optionalTarget ); }, intersectTriangle: function() { // Compute the offset origin, edges, and normal. var diff = new THREE.Vector3(); var edge1 = new THREE.Vector3(); var edge2 = new THREE.Vector3(); var normal = new THREE.Vector3(); return function ( a, b, c, backfaceCulling, optionalTarget ) { // from http://www.geometrictools.com/LibMathematics/Intersection/Wm5IntrRay3Triangle3.cpp edge1.subVectors( b, a ); edge2.subVectors( c, a ); normal.crossVectors( edge1, edge2 ); // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction, // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2)) // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q)) // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N) var DdN = this.direction.dot( normal ); var sign; if ( DdN > 0 ) { if ( backfaceCulling ) return null; sign = 1; } else if ( DdN < 0 ) { sign = - 1; DdN = - DdN; } else { return null; } diff.subVectors( this.origin, a ); var DdQxE2 = sign * this.direction.dot( edge2.crossVectors( diff, edge2 ) ); // b1 < 0, no intersection if ( DdQxE2 < 0 ) { return null; } var DdE1xQ = sign * this.direction.dot( edge1.cross( diff ) ); // b2 < 0, no intersection if ( DdE1xQ < 0 ) { return null; } // b1+b2 > 1, no intersection if ( DdQxE2 + DdE1xQ > DdN ) { return null; } // Line intersects triangle, check if ray does. var QdN = - sign * diff.dot( normal ); // t < 0, no intersection if ( QdN < 0 ) { return null; } // Ray intersects triangle. return this.at( QdN / DdN, optionalTarget ); } }(), applyMatrix4: function ( matrix4 ) { this.direction.add( this.origin ).applyMatrix4( matrix4 ); this.origin.applyMatrix4( matrix4 ); this.direction.sub( this.origin ); this.direction.normalize(); return this; }, equals: function ( ray ) { return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction ); }, clone: function () { return new THREE.Ray().copy( this ); } }; /** * @author bhouston / http://exocortex.com * @author mrdoob / http://mrdoob.com/ */ THREE.Sphere = function ( center, radius ) { this.center = ( center !== undefined ) ? center : new THREE.Vector3(); this.radius = ( radius !== undefined ) ? radius : 0; }; THREE.Sphere.prototype = { constructor: THREE.Sphere, set: function ( center, radius ) { this.center.copy( center ); this.radius = radius; return this; }, setFromPoints: function () { var box = new THREE.Box3(); return function ( points, optionalCenter ) { var center = this.center; if ( optionalCenter !== undefined ) { center.copy( optionalCenter ); } else { box.setFromPoints( points ).center( center ); } var maxRadiusSq = 0; for ( var i = 0, il = points.length; i < il; i ++ ) { maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) ); } this.radius = Math.sqrt( maxRadiusSq ); return this; }; }(), copy: function ( sphere ) { this.center.copy( sphere.center ); this.radius = sphere.radius; return this; }, empty: function () { return ( this.radius <= 0 ); }, containsPoint: function ( point ) { return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) ); }, distanceToPoint: function ( point ) { return ( point.distanceTo( this.center ) - this.radius ); }, intersectsSphere: function ( sphere ) { var radiusSum = this.radius + sphere.radius; return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum ); }, clampPoint: function ( point, optionalTarget ) { var deltaLengthSq = this.center.distanceToSquared( point ); var result = optionalTarget || new THREE.Vector3(); result.copy( point ); if ( deltaLengthSq > ( this.radius * this.radius ) ) { result.sub( this.center ).normalize(); result.multiplyScalar( this.radius ).add( this.center ); } return result; }, getBoundingBox: function ( optionalTarget ) { var box = optionalTarget || new THREE.Box3(); box.set( this.center, this.center ); box.expandByScalar( this.radius ); return box; }, applyMatrix4: function ( matrix ) { this.center.applyMatrix4( matrix ); this.radius = this.radius * matrix.getMaxScaleOnAxis(); return this; }, translate: function ( offset ) { this.center.add( offset ); return this; }, equals: function ( sphere ) { return sphere.center.equals( this.center ) && ( sphere.radius === this.radius ); }, clone: function () { return new THREE.Sphere().copy( this ); } }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * @author bhouston / http://exocortex.com */ THREE.Frustum = function ( p0, p1, p2, p3, p4, p5 ) { this.planes = [ ( p0 !== undefined ) ? p0 : new THREE.Plane(), ( p1 !== undefined ) ? p1 : new THREE.Plane(), ( p2 !== undefined ) ? p2 : new THREE.Plane(), ( p3 !== undefined ) ? p3 : new THREE.Plane(), ( p4 !== undefined ) ? p4 : new THREE.Plane(), ( p5 !== undefined ) ? p5 : new THREE.Plane() ]; }; THREE.Frustum.prototype = { constructor: THREE.Frustum, set: function ( p0, p1, p2, p3, p4, p5 ) { var planes = this.planes; planes[0].copy( p0 ); planes[1].copy( p1 ); planes[2].copy( p2 ); planes[3].copy( p3 ); planes[4].copy( p4 ); planes[5].copy( p5 ); return this; }, copy: function ( frustum ) { var planes = this.planes; for( var i = 0; i < 6; i ++ ) { planes[i].copy( frustum.planes[i] ); } return this; }, setFromMatrix: function ( m ) { var planes = this.planes; var me = m.elements; var me0 = me[0], me1 = me[1], me2 = me[2], me3 = me[3]; var me4 = me[4], me5 = me[5], me6 = me[6], me7 = me[7]; var me8 = me[8], me9 = me[9], me10 = me[10], me11 = me[11]; var me12 = me[12], me13 = me[13], me14 = me[14], me15 = me[15]; planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize(); planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize(); planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize(); planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize(); planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize(); planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize(); return this; }, intersectsObject: function () { var sphere = new THREE.Sphere(); return function ( object ) { var geometry = object.geometry; if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); sphere.copy( geometry.boundingSphere ); sphere.applyMatrix4( object.matrixWorld ); return this.intersectsSphere( sphere ); }; }(), intersectsSphere: function ( sphere ) { var planes = this.planes; var center = sphere.center; var negRadius = -sphere.radius; for ( var i = 0; i < 6; i ++ ) { var distance = planes[ i ].distanceToPoint( center ); if ( distance < negRadius ) { return false; } } return true; }, intersectsBox : function() { var p1 = new THREE.Vector3(), p2 = new THREE.Vector3(); return function( box ) { var planes = this.planes; for ( var i = 0; i < 6 ; i ++ ) { var plane = planes[i]; p1.x = plane.normal.x > 0 ? box.min.x : box.max.x; p2.x = plane.normal.x > 0 ? box.max.x : box.min.x; p1.y = plane.normal.y > 0 ? box.min.y : box.max.y; p2.y = plane.normal.y > 0 ? box.max.y : box.min.y; p1.z = plane.normal.z > 0 ? box.min.z : box.max.z; p2.z = plane.normal.z > 0 ? box.max.z : box.min.z; var d1 = plane.distanceToPoint( p1 ); var d2 = plane.distanceToPoint( p2 ); // if both outside plane, no intersection if ( d1 < 0 && d2 < 0 ) { return false; } } return true; }; }(), containsPoint: function ( point ) { var planes = this.planes; for ( var i = 0; i < 6; i ++ ) { if ( planes[ i ].distanceToPoint( point ) < 0 ) { return false; } } return true; }, clone: function () { return new THREE.Frustum().copy( this ); } }; /** * @author bhouston / http://exocortex.com */ THREE.Plane = function ( normal, constant ) { this.normal = ( normal !== undefined ) ? normal : new THREE.Vector3( 1, 0, 0 ); this.constant = ( constant !== undefined ) ? constant : 0; }; THREE.Plane.prototype = { constructor: THREE.Plane, set: function ( normal, constant ) { this.normal.copy( normal ); this.constant = constant; return this; }, setComponents: function ( x, y, z, w ) { this.normal.set( x, y, z ); this.constant = w; return this; }, setFromNormalAndCoplanarPoint: function ( normal, point ) { this.normal.copy( normal ); this.constant = - point.dot( this.normal ); // must be this.normal, not normal, as this.normal is normalized return this; }, setFromCoplanarPoints: function() { var v1 = new THREE.Vector3(); var v2 = new THREE.Vector3(); return function ( a, b, c ) { var normal = v1.subVectors( c, b ).cross( v2.subVectors( a, b ) ).normalize(); // Q: should an error be thrown if normal is zero (e.g. degenerate plane)? this.setFromNormalAndCoplanarPoint( normal, a ); return this; }; }(), copy: function ( plane ) { this.normal.copy( plane.normal ); this.constant = plane.constant; return this; }, normalize: function () { // Note: will lead to a divide by zero if the plane is invalid. var inverseNormalLength = 1.0 / this.normal.length(); this.normal.multiplyScalar( inverseNormalLength ); this.constant *= inverseNormalLength; return this; }, negate: function () { this.constant *= -1; this.normal.negate(); return this; }, distanceToPoint: function ( point ) { return this.normal.dot( point ) + this.constant; }, distanceToSphere: function ( sphere ) { return this.distanceToPoint( sphere.center ) - sphere.radius; }, projectPoint: function ( point, optionalTarget ) { return this.orthoPoint( point, optionalTarget ).sub( point ).negate(); }, orthoPoint: function ( point, optionalTarget ) { var perpendicularMagnitude = this.distanceToPoint( point ); var result = optionalTarget || new THREE.Vector3(); return result.copy( this.normal ).multiplyScalar( perpendicularMagnitude ); }, isIntersectionLine: function ( line ) { // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it. var startSign = this.distanceToPoint( line.start ); var endSign = this.distanceToPoint( line.end ); return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 ); }, intersectLine: function() { var v1 = new THREE.Vector3(); return function ( line, optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); var direction = line.delta( v1 ); var denominator = this.normal.dot( direction ); if ( denominator == 0 ) { // line is coplanar, return origin if( this.distanceToPoint( line.start ) == 0 ) { return result.copy( line.start ); } // Unsure if this is the correct method to handle this case. return undefined; } var t = - ( line.start.dot( this.normal ) + this.constant ) / denominator; if( t < 0 || t > 1 ) { return undefined; } return result.copy( direction ).multiplyScalar( t ).add( line.start ); }; }(), coplanarPoint: function ( optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); return result.copy( this.normal ).multiplyScalar( - this.constant ); }, applyMatrix4: function() { var v1 = new THREE.Vector3(); var v2 = new THREE.Vector3(); var m1 = new THREE.Matrix3(); return function ( matrix, optionalNormalMatrix ) { // compute new normal based on theory here: // http://www.songho.ca/opengl/gl_normaltransform.html var normalMatrix = optionalNormalMatrix || m1.getNormalMatrix( matrix ); var newNormal = v1.copy( this.normal ).applyMatrix3( normalMatrix ); var newCoplanarPoint = this.coplanarPoint( v2 ); newCoplanarPoint.applyMatrix4( matrix ); this.setFromNormalAndCoplanarPoint( newNormal, newCoplanarPoint ); return this; }; }(), translate: function ( offset ) { this.constant = this.constant - offset.dot( this.normal ); return this; }, equals: function ( plane ) { return plane.normal.equals( this.normal ) && ( plane.constant == this.constant ); }, clone: function () { return new THREE.Plane().copy( this ); } }; /** * @author alteredq / http://alteredqualia.com/ * @author mrdoob / http://mrdoob.com/ */ THREE.Math = { PI2: Math.PI * 2, generateUUID: function () { // http://www.broofa.com/Tools/Math.uuid.htm var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); var uuid = new Array(36); var rnd = 0, r; return function () { for ( var i = 0; i < 36; i ++ ) { if ( i == 8 || i == 13 || i == 18 || i == 23 ) { uuid[ i ] = '-'; } else if ( i == 14 ) { uuid[ i ] = '4'; } else { if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0; r = rnd & 0xf; rnd = rnd >> 4; uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]; } } return uuid.join(''); }; }(), // Clamp value to range <a, b> clamp: function ( x, a, b ) { return ( x < a ) ? a : ( ( x > b ) ? b : x ); }, // Clamp value to range <a, inf) clampBottom: function ( x, a ) { return x < a ? a : x; }, // Linear mapping from range <a1, a2> to range <b1, b2> mapLinear: function ( x, a1, a2, b1, b2 ) { return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 ); }, // http://en.wikipedia.org/wiki/Smoothstep smoothstep: function ( x, min, max ) { if ( x <= min ) return 0; if ( x >= max ) return 1; x = ( x - min )/( max - min ); return x*x*(3 - 2*x); }, smootherstep: function ( x, min, max ) { if ( x <= min ) return 0; if ( x >= max ) return 1; x = ( x - min )/( max - min ); return x*x*x*(x*(x*6 - 15) + 10); }, // Random float from <0, 1> with 16 bits of randomness // (standard Math.random() creates repetitive patterns when applied over larger space) random16: function () { return ( 65280 * Math.random() + 255 * Math.random() ) / 65535; }, // Random integer from <low, high> interval randInt: function ( low, high ) { return low + Math.floor( Math.random() * ( high - low + 1 ) ); }, // Random float from <low, high> interval randFloat: function ( low, high ) { return low + Math.random() * ( high - low ); }, // Random float from <-range/2, range/2> interval randFloatSpread: function ( range ) { return range * ( 0.5 - Math.random() ); }, sign: function ( x ) { return ( x < 0 ) ? - 1 : ( x > 0 ) ? 1 : 0; }, degToRad: function() { var degreeToRadiansFactor = Math.PI / 180; return function ( degrees ) { return degrees * degreeToRadiansFactor; }; }(), radToDeg: function() { var radianToDegreesFactor = 180 / Math.PI; return function ( radians ) { return radians * radianToDegreesFactor; }; }(), isPowerOfTwo: function ( value ) { return ( value & ( value - 1 ) ) === 0 && value !== 0; } }; /** * Spline from Tween.js, slightly optimized (and trashed) * http://sole.github.com/tween.js/examples/05_spline.html * * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ THREE.Spline = function ( points ) { this.points = points; var c = [], v3 = { x: 0, y: 0, z: 0 }, point, intPoint, weight, w2, w3, pa, pb, pc, pd; this.initFromArray = function( a ) { this.points = []; for ( var i = 0; i < a.length; i++ ) { this.points[ i ] = { x: a[ i ][ 0 ], y: a[ i ][ 1 ], z: a[ i ][ 2 ] }; } }; this.getPoint = function ( k ) { point = ( this.points.length - 1 ) * k; intPoint = Math.floor( point ); weight = point - intPoint; c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1; c[ 1 ] = intPoint; c[ 2 ] = intPoint > this.points.length - 2 ? this.points.length - 1 : intPoint + 1; c[ 3 ] = intPoint > this.points.length - 3 ? this.points.length - 1 : intPoint + 2; pa = this.points[ c[ 0 ] ]; pb = this.points[ c[ 1 ] ]; pc = this.points[ c[ 2 ] ]; pd = this.points[ c[ 3 ] ]; w2 = weight * weight; w3 = weight * w2; v3.x = interpolate( pa.x, pb.x, pc.x, pd.x, weight, w2, w3 ); v3.y = interpolate( pa.y, pb.y, pc.y, pd.y, weight, w2, w3 ); v3.z = interpolate( pa.z, pb.z, pc.z, pd.z, weight, w2, w3 ); return v3; }; this.getControlPointsArray = function () { var i, p, l = this.points.length, coords = []; for ( i = 0; i < l; i ++ ) { p = this.points[ i ]; coords[ i ] = [ p.x, p.y, p.z ]; } return coords; }; // approximate length by summing linear segments this.getLength = function ( nSubDivisions ) { var i, index, nSamples, position, point = 0, intPoint = 0, oldIntPoint = 0, oldPosition = new THREE.Vector3(), tmpVec = new THREE.Vector3(), chunkLengths = [], totalLength = 0; // first point has 0 length chunkLengths[ 0 ] = 0; if ( !nSubDivisions ) nSubDivisions = 100; nSamples = this.points.length * nSubDivisions; oldPosition.copy( this.points[ 0 ] ); for ( i = 1; i < nSamples; i ++ ) { index = i / nSamples; position = this.getPoint( index ); tmpVec.copy( position ); totalLength += tmpVec.distanceTo( oldPosition ); oldPosition.copy( position ); point = ( this.points.length - 1 ) * index; intPoint = Math.floor( point ); if ( intPoint != oldIntPoint ) { chunkLengths[ intPoint ] = totalLength; oldIntPoint = intPoint; } } // last point ends with total length chunkLengths[ chunkLengths.length ] = totalLength; return { chunks: chunkLengths, total: totalLength }; }; this.reparametrizeByArcLength = function ( samplingCoef ) { var i, j, index, indexCurrent, indexNext, linearDistance, realDistance, sampling, position, newpoints = [], tmpVec = new THREE.Vector3(), sl = this.getLength(); newpoints.push( tmpVec.copy( this.points[ 0 ] ).clone() ); for ( i = 1; i < this.points.length; i++ ) { //tmpVec.copy( this.points[ i - 1 ] ); //linearDistance = tmpVec.distanceTo( this.points[ i ] ); realDistance = sl.chunks[ i ] - sl.chunks[ i - 1 ]; sampling = Math.ceil( samplingCoef * realDistance / sl.total ); indexCurrent = ( i - 1 ) / ( this.points.length - 1 ); indexNext = i / ( this.points.length - 1 ); for ( j = 1; j < sampling - 1; j++ ) { index = indexCurrent + j * ( 1 / sampling ) * ( indexNext - indexCurrent ); position = this.getPoint( index ); newpoints.push( tmpVec.copy( position ).clone() ); } newpoints.push( tmpVec.copy( this.points[ i ] ).clone() ); } this.points = newpoints; }; // Catmull-Rom function interpolate( p0, p1, p2, p3, t, t2, t3 ) { var v0 = ( p2 - p0 ) * 0.5, v1 = ( p3 - p1 ) * 0.5; return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1; }; }; /** * @author bhouston / http://exocortex.com * @author mrdoob / http://mrdoob.com/ */ THREE.Triangle = function ( a, b, c ) { this.a = ( a !== undefined ) ? a : new THREE.Vector3(); this.b = ( b !== undefined ) ? b : new THREE.Vector3(); this.c = ( c !== undefined ) ? c : new THREE.Vector3(); }; THREE.Triangle.normal = function() { var v0 = new THREE.Vector3(); return function ( a, b, c, optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); result.subVectors( c, b ); v0.subVectors( a, b ); result.cross( v0 ); var resultLengthSq = result.lengthSq(); if( resultLengthSq > 0 ) { return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) ); } return result.set( 0, 0, 0 ); }; }(); // static/instance method to calculate barycoordinates // based on: http://www.blackpawn.com/texts/pointinpoly/default.html THREE.Triangle.barycoordFromPoint = function() { var v0 = new THREE.Vector3(); var v1 = new THREE.Vector3(); var v2 = new THREE.Vector3(); return function ( point, a, b, c, optionalTarget ) { v0.subVectors( c, a ); v1.subVectors( b, a ); v2.subVectors( point, a ); var dot00 = v0.dot( v0 ); var dot01 = v0.dot( v1 ); var dot02 = v0.dot( v2 ); var dot11 = v1.dot( v1 ); var dot12 = v1.dot( v2 ); var denom = ( dot00 * dot11 - dot01 * dot01 ); var result = optionalTarget || new THREE.Vector3(); // colinear or singular triangle if( denom == 0 ) { // arbitrary location outside of triangle? // not sure if this is the best idea, maybe should be returning undefined return result.set( -2, -1, -1 ); } var invDenom = 1 / denom; var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom; var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom; // barycoordinates must always sum to 1 return result.set( 1 - u - v, v, u ); }; }(); THREE.Triangle.containsPoint = function() { var v1 = new THREE.Vector3(); return function ( point, a, b, c ) { var result = THREE.Triangle.barycoordFromPoint( point, a, b, c, v1 ); return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 ); }; }(); THREE.Triangle.prototype = { constructor: THREE.Triangle, set: function ( a, b, c ) { this.a.copy( a ); this.b.copy( b ); this.c.copy( c ); return this; }, setFromPointsAndIndices: function ( points, i0, i1, i2 ) { this.a.copy( points[i0] ); this.b.copy( points[i1] ); this.c.copy( points[i2] ); return this; }, copy: function ( triangle ) { this.a.copy( triangle.a ); this.b.copy( triangle.b ); this.c.copy( triangle.c ); return this; }, area: function() { var v0 = new THREE.Vector3(); var v1 = new THREE.Vector3(); return function () { v0.subVectors( this.c, this.b ); v1.subVectors( this.a, this.b ); return v0.cross( v1 ).length() * 0.5; }; }(), midpoint: function ( optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); return result.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 ); }, normal: function ( optionalTarget ) { return THREE.Triangle.normal( this.a, this.b, this.c, optionalTarget ); }, plane: function ( optionalTarget ) { var result = optionalTarget || new THREE.Plane(); return result.setFromCoplanarPoints( this.a, this.b, this.c ); }, barycoordFromPoint: function ( point, optionalTarget ) { return THREE.Triangle.barycoordFromPoint( point, this.a, this.b, this.c, optionalTarget ); }, containsPoint: function ( point ) { return THREE.Triangle.containsPoint( point, this.a, this.b, this.c ); }, equals: function ( triangle ) { return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c ); }, clone: function () { return new THREE.Triangle().copy( this ); } }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.Vertex = function ( v ) { console.warn( 'THREE.Vertex has been DEPRECATED. Use THREE.Vector3 instead.') return v; }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.UV = function ( u, v ) { console.warn( 'THREE.UV has been DEPRECATED. Use THREE.Vector2 instead.') return new THREE.Vector2( u, v ); }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.Clock = function ( autoStart ) { this.autoStart = ( autoStart !== undefined ) ? autoStart : true; this.startTime = 0; this.oldTime = 0; this.elapsedTime = 0; this.running = false; }; THREE.Clock.prototype = { constructor: THREE.Clock, start: function () { this.startTime = self.performance !== undefined && self.performance.now !== undefined ? self.performance.now() : Date.now(); this.oldTime = this.startTime; this.running = true; }, stop: function () { this.getElapsedTime(); this.running = false; }, getElapsedTime: function () { this.getDelta(); return this.elapsedTime; }, getDelta: function () { var diff = 0; if ( this.autoStart && ! this.running ) { this.start(); } if ( this.running ) { var newTime = self.performance !== undefined && self.performance.now !== undefined ? self.performance.now() : Date.now(); diff = 0.001 * ( newTime - this.oldTime ); this.oldTime = newTime; this.elapsedTime += diff; } return diff; } }; /** * https://github.com/mrdoob/eventdispatcher.js/ */ THREE.EventDispatcher = function () {} THREE.EventDispatcher.prototype = { constructor: THREE.EventDispatcher, apply: function ( object ) { object.addEventListener = THREE.EventDispatcher.prototype.addEventListener; object.hasEventListener = THREE.EventDispatcher.prototype.hasEventListener; object.removeEventListener = THREE.EventDispatcher.prototype.removeEventListener; object.dispatchEvent = THREE.EventDispatcher.prototype.dispatchEvent; }, addEventListener: function ( type, listener ) { if ( this._listeners === undefined ) this._listeners = {}; var listeners = this._listeners; if ( listeners[ type ] === undefined ) { listeners[ type ] = []; } if ( listeners[ type ].indexOf( listener ) === - 1 ) { listeners[ type ].push( listener ); } }, hasEventListener: function ( type, listener ) { if ( this._listeners === undefined ) return false; var listeners = this._listeners; if ( listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1 ) { return true; } return false; }, removeEventListener: function ( type, listener ) { if ( this._listeners === undefined ) return; var listeners = this._listeners; var listenerArray = listeners[ type ]; if ( listenerArray !== undefined ) { var index = listenerArray.indexOf( listener ); if ( index !== - 1 ) { listenerArray.splice( index, 1 ); } } }, dispatchEvent: function () { var array = []; return function ( event ) { if ( this._listeners === undefined ) return; var listeners = this._listeners; var listenerArray = listeners[ event.type ]; if ( listenerArray !== undefined ) { event.target = this; var length = listenerArray.length; for ( var i = 0; i < length; i ++ ) { array[ i ] = listenerArray[ i ]; } for ( var i = 0; i < length; i ++ ) { array[ i ].call( this, event ); } } }; }() }; /** * @author mrdoob / http://mrdoob.com/ * @author bhouston / http://exocortex.com/ * @author stephomi / http://stephaneginier.com/ */ ( function ( THREE ) { THREE.Raycaster = function ( origin, direction, near, far ) { this.ray = new THREE.Ray( origin, direction ); // direction is assumed to be normalized (for accurate distance calculations) this.near = near || 0; this.far = far || Infinity; }; var sphere = new THREE.Sphere(); var localRay = new THREE.Ray(); var facePlane = new THREE.Plane(); var intersectPoint = new THREE.Vector3(); var matrixPosition = new THREE.Vector3(); var inverseMatrix = new THREE.Matrix4(); var descSort = function ( a, b ) { return a.distance - b.distance; }; var vA = new THREE.Vector3(); var vB = new THREE.Vector3(); var vC = new THREE.Vector3(); var intersectObject = function ( object, raycaster, intersects ) { if ( object instanceof THREE.Sprite ) { matrixPosition.setFromMatrixPosition( object.matrixWorld ); var distance = raycaster.ray.distanceToPoint( matrixPosition ); if ( distance > object.scale.x ) { return intersects; } intersects.push( { distance: distance, point: object.position, face: null, object: object } ); } else if ( object instanceof THREE.LOD ) { matrixPosition.setFromMatrixPosition( object.matrixWorld ); var distance = raycaster.ray.origin.distanceTo( matrixPosition ); intersectObject( object.getObjectForDistance( distance ), raycaster, intersects ); } else if ( object instanceof THREE.Mesh ) { var geometry = object.geometry; // Checking boundingSphere distance to ray if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); sphere.copy( geometry.boundingSphere ); sphere.applyMatrix4( object.matrixWorld ); if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) { return intersects; } // Check boundingBox before continuing inverseMatrix.getInverse( object.matrixWorld ); localRay.copy( raycaster.ray ).applyMatrix4( inverseMatrix ); if ( geometry.boundingBox !== null ) { if ( localRay.isIntersectionBox( geometry.boundingBox ) === false ) { return intersects; } } if ( geometry instanceof THREE.BufferGeometry ) { var material = object.material; if ( material === undefined ) return intersects; if ( geometry.dynamic === false ) return intersects; var a, b, c; var precision = raycaster.precision; if ( geometry.attributes.index !== undefined ) { var offsets = geometry.offsets; var indices = geometry.attributes.index.array; var positions = geometry.attributes.position.array; var offLength = geometry.offsets.length; var fl = geometry.attributes.index.array.length / 3; for ( var oi = 0; oi < offLength; ++oi ) { var start = offsets[ oi ].start; var count = offsets[ oi ].count; var index = offsets[ oi ].index; for ( var i = start, il = start + count; i < il; i += 3 ) { a = index + indices[ i ]; b = index + indices[ i + 1 ]; c = index + indices[ i + 2 ]; vA.set( positions[ a * 3 ], positions[ a * 3 + 1 ], positions[ a * 3 + 2 ] ); vB.set( positions[ b * 3 ], positions[ b * 3 + 1 ], positions[ b * 3 + 2 ] ); vC.set( positions[ c * 3 ], positions[ c * 3 + 1 ], positions[ c * 3 + 2 ] ); if ( material.side === THREE.BackSide ) { var intersectionPoint = localRay.intersectTriangle( vC, vB, vA, true ); } else { var intersectionPoint = localRay.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide ); } if ( intersectionPoint === null ) continue; intersectionPoint.applyMatrix4( object.matrixWorld ); var distance = raycaster.ray.origin.distanceTo( intersectionPoint ); if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue; intersects.push( { distance: distance, point: intersectionPoint, face: null, faceIndex: null, object: object } ); } } } else { var offsets = geometry.offsets; var positions = geometry.attributes.position.array; var offLength = geometry.offsets.length; var fl = geometry.attributes.position.array.length; for ( var i = 0; i < fl; i += 3 ) { a = i; b = i + 1; c = i + 2; vA.set( positions[ a * 3 ], positions[ a * 3 + 1 ], positions[ a * 3 + 2 ] ); vB.set( positions[ b * 3 ], positions[ b * 3 + 1 ], positions[ b * 3 + 2 ] ); vC.set( positions[ c * 3 ], positions[ c * 3 + 1 ], positions[ c * 3 + 2 ] ); if ( material.side === THREE.BackSide ) { var intersectionPoint = localRay.intersectTriangle( vC, vB, vA, true ); } else { var intersectionPoint = localRay.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide ); } if ( intersectionPoint === null ) continue; intersectionPoint.applyMatrix4( object.matrixWorld ); var distance = raycaster.ray.origin.distanceTo( intersectionPoint ); if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue; intersects.push( { distance: distance, point: intersectionPoint, face: null, faceIndex: null, object: object } ); } } } else if ( geometry instanceof THREE.Geometry ) { var isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial; var objectMaterials = isFaceMaterial === true ? object.material.materials : null; var a, b, c, d; var precision = raycaster.precision; var vertices = geometry.vertices; for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) { var face = geometry.faces[ f ]; var material = isFaceMaterial === true ? objectMaterials[ face.materialIndex ] : object.material; if ( material === undefined ) continue; a = vertices[ face.a ]; b = vertices[ face.b ]; c = vertices[ face.c ]; if ( material.morphTargets === true ) { var morphTargets = geometry.morphTargets; var morphInfluences = object.morphTargetInfluences; vA.set( 0, 0, 0 ); vB.set( 0, 0, 0 ); vC.set( 0, 0, 0 ); for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) { var influence = morphInfluences[ t ]; if ( influence === 0 ) continue; var targets = morphTargets[ t ].vertices; vA.x += ( targets[ face.a ].x - a.x ) * influence; vA.y += ( targets[ face.a ].y - a.y ) * influence; vA.z += ( targets[ face.a ].z - a.z ) * influence; vB.x += ( targets[ face.b ].x - b.x ) * influence; vB.y += ( targets[ face.b ].y - b.y ) * influence; vB.z += ( targets[ face.b ].z - b.z ) * influence; vC.x += ( targets[ face.c ].x - c.x ) * influence; vC.y += ( targets[ face.c ].y - c.y ) * influence; vC.z += ( targets[ face.c ].z - c.z ) * influence; } vA.add( a ); vB.add( b ); vC.add( c ); a = vA; b = vB; c = vC; } if ( material.side === THREE.BackSide ) { var intersectionPoint = localRay.intersectTriangle( c, b, a, true ); } else { var intersectionPoint = localRay.intersectTriangle( a, b, c, material.side !== THREE.DoubleSide ); } if ( intersectionPoint === null ) continue; intersectionPoint.applyMatrix4( object.matrixWorld ); var distance = raycaster.ray.origin.distanceTo( intersectionPoint ); if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue; intersects.push( { distance: distance, point: intersectionPoint, face: face, faceIndex: f, object: object } ); } } } else if ( object instanceof THREE.Line ) { var precision = raycaster.linePrecision; var precisionSq = precision * precision; var geometry = object.geometry; if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); // Checking boundingSphere distance to ray sphere.copy( geometry.boundingSphere ); sphere.applyMatrix4( object.matrixWorld ); if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) { return intersects; } inverseMatrix.getInverse( object.matrixWorld ); localRay.copy( raycaster.ray ).applyMatrix4( inverseMatrix ); /* if ( geometry instanceof THREE.BufferGeometry ) { } else */ if ( geometry instanceof THREE.Geometry ) { var vertices = geometry.vertices; var nbVertices = vertices.length; var interSegment = new THREE.Vector3(); var interRay = new THREE.Vector3(); var step = object.type === THREE.LineStrip ? 1 : 2; for ( var i = 0; i < nbVertices - 1; i = i + step ) { var distSq = localRay.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment ); if ( distSq > precisionSq ) continue; var distance = localRay.origin.distanceTo( interRay ); if ( distance < raycaster.near || distance > raycaster.far ) continue; intersects.push( { distance: distance, // What do we want? intersection point on the ray or on the segment?? // point: raycaster.ray.at( distance ), point: interSegment.clone().applyMatrix4( object.matrixWorld ), face: null, faceIndex: null, object: object } ); } } } }; var intersectDescendants = function ( object, raycaster, intersects ) { var descendants = object.getDescendants(); for ( var i = 0, l = descendants.length; i < l; i ++ ) { intersectObject( descendants[ i ], raycaster, intersects ); } }; // THREE.Raycaster.prototype.precision = 0.0001; THREE.Raycaster.prototype.linePrecision = 1; THREE.Raycaster.prototype.set = function ( origin, direction ) { this.ray.set( origin, direction ); // direction is assumed to be normalized (for accurate distance calculations) }; THREE.Raycaster.prototype.intersectObject = function ( object, recursive ) { var intersects = []; if ( recursive === true ) { intersectDescendants( object, this, intersects ); } intersectObject( object, this, intersects ); intersects.sort( descSort ); return intersects; }; THREE.Raycaster.prototype.intersectObjects = function ( objects, recursive ) { var intersects = []; for ( var i = 0, l = objects.length; i < l; i ++ ) { intersectObject( objects[ i ], this, intersects ); if ( recursive === true ) { intersectDescendants( objects[ i ], this, intersects ); } } intersects.sort( descSort ); return intersects; }; }( THREE ) ); /** * @author mrdoob / http://mrdoob.com/ * @author mikael emtinger / http://gomo.se/ * @author alteredq / http://alteredqualia.com/ * @author WestLangley / http://github.com/WestLangley */ THREE.Object3D = function () { this.id = THREE.Object3DIdCount ++; this.uuid = THREE.Math.generateUUID(); this.name = ''; this.parent = undefined; this.children = []; this.up = new THREE.Vector3( 0, 1, 0 ); this.position = new THREE.Vector3(); this._rotation = new THREE.Euler(); this._quaternion = new THREE.Quaternion(); this.scale = new THREE.Vector3( 1, 1, 1 ); // keep rotation and quaternion in sync this._rotation._quaternion = this.quaternion; this._quaternion._euler = this.rotation; this.renderDepth = null; this.rotationAutoUpdate = true; this.matrix = new THREE.Matrix4(); this.matrixWorld = new THREE.Matrix4(); this.matrixAutoUpdate = true; this.matrixWorldNeedsUpdate = true; this.visible = true; this.castShadow = false; this.receiveShadow = false; this.frustumCulled = true; this.userData = {}; }; THREE.Object3D.prototype = { constructor: THREE.Object3D, get rotation () { return this._rotation; }, set rotation ( value ) { this._rotation = value; this._rotation._quaternion = this._quaternion; this._quaternion._euler = this._rotation; this._rotation._updateQuaternion(); }, get quaternion () { return this._quaternion; }, set quaternion ( value ) { this._quaternion = value; this._quaternion._euler = this._rotation; this._rotation._quaternion = this._quaternion; this._quaternion._updateEuler(); }, get eulerOrder () { console.warn( 'DEPRECATED: Object3D\'s .eulerOrder has been moved to Object3D\'s .rotation.order.' ); return this.rotation.order; }, set eulerOrder ( value ) { console.warn( 'DEPRECATED: Object3D\'s .eulerOrder has been moved to Object3D\'s .rotation.order.' ); this.rotation.order = value; }, get useQuaternion () { console.warn( 'DEPRECATED: Object3D\'s .useQuaternion has been removed. The library now uses quaternions by default.' ); }, set useQuaternion ( value ) { console.warn( 'DEPRECATED: Object3D\'s .useQuaternion has been removed. The library now uses quaternions by default.' ); }, applyMatrix: function ( matrix ) { this.matrix.multiplyMatrices( matrix, this.matrix ); this.matrix.decompose( this.position, this.quaternion, this.scale ); }, setRotationFromAxisAngle: function ( axis, angle ) { // assumes axis is normalized this.quaternion.setFromAxisAngle( axis, angle ); }, setRotationFromEuler: function ( euler ) { this.quaternion.setFromEuler( euler, true ); }, setRotationFromMatrix: function ( m ) { // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) this.quaternion.setFromRotationMatrix( m ); }, setRotationFromQuaternion: function ( q ) { // assumes q is normalized this.quaternion.copy( q ); }, rotateOnAxis: function() { // rotate object on axis in object space // axis is assumed to be normalized var q1 = new THREE.Quaternion(); return function ( axis, angle ) { q1.setFromAxisAngle( axis, angle ); this.quaternion.multiply( q1 ); return this; } }(), rotateX: function () { var v1 = new THREE.Vector3( 1, 0, 0 ); return function ( angle ) { return this.rotateOnAxis( v1, angle ); }; }(), rotateY: function () { var v1 = new THREE.Vector3( 0, 1, 0 ); return function ( angle ) { return this.rotateOnAxis( v1, angle ); }; }(), rotateZ: function () { var v1 = new THREE.Vector3( 0, 0, 1 ); return function ( angle ) { return this.rotateOnAxis( v1, angle ); }; }(), translateOnAxis: function () { // translate object by distance along axis in object space // axis is assumed to be normalized var v1 = new THREE.Vector3(); return function ( axis, distance ) { v1.copy( axis ); v1.applyQuaternion( this.quaternion ); this.position.add( v1.multiplyScalar( distance ) ); return this; } }(), translate: function ( distance, axis ) { console.warn( 'DEPRECATED: Object3D\'s .translate() has been removed. Use .translateOnAxis( axis, distance ) instead. Note args have been changed.' ); return this.translateOnAxis( axis, distance ); }, translateX: function () { var v1 = new THREE.Vector3( 1, 0, 0 ); return function ( distance ) { return this.translateOnAxis( v1, distance ); }; }(), translateY: function () { var v1 = new THREE.Vector3( 0, 1, 0 ); return function ( distance ) { return this.translateOnAxis( v1, distance ); }; }(), translateZ: function () { var v1 = new THREE.Vector3( 0, 0, 1 ); return function ( distance ) { return this.translateOnAxis( v1, distance ); }; }(), localToWorld: function ( vector ) { return vector.applyMatrix4( this.matrixWorld ); }, worldToLocal: function () { var m1 = new THREE.Matrix4(); return function ( vector ) { return vector.applyMatrix4( m1.getInverse( this.matrixWorld ) ); }; }(), lookAt: function () { // This routine does not support objects with rotated and/or translated parent(s) var m1 = new THREE.Matrix4(); return function ( vector ) { m1.lookAt( vector, this.position, this.up ); this.quaternion.setFromRotationMatrix( m1 ); }; }(), add: function ( object ) { if ( object === this ) { console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' ); return; } if ( object instanceof THREE.Object3D ) { if ( object.parent !== undefined ) { object.parent.remove( object ); } object.parent = this; object.dispatchEvent( { type: 'added' } ); this.children.push( object ); // add to scene var scene = this; while ( scene.parent !== undefined ) { scene = scene.parent; } if ( scene !== undefined && scene instanceof THREE.Scene ) { scene.__addObject( object ); } } }, remove: function ( object ) { var index = this.children.indexOf( object ); if ( index !== - 1 ) { object.parent = undefined; object.dispatchEvent( { type: 'removed' } ); this.children.splice( index, 1 ); // remove from scene var scene = this; while ( scene.parent !== undefined ) { scene = scene.parent; } if ( scene !== undefined && scene instanceof THREE.Scene ) { scene.__removeObject( object ); } } }, traverse: function ( callback ) { callback( this ); for ( var i = 0, l = this.children.length; i < l; i ++ ) { this.children[ i ].traverse( callback ); } }, getObjectById: function ( id, recursive ) { for ( var i = 0, l = this.children.length; i < l; i ++ ) { var child = this.children[ i ]; if ( child.id === id ) { return child; } if ( recursive === true ) { child = child.getObjectById( id, recursive ); if ( child !== undefined ) { return child; } } } return undefined; }, getObjectByName: function ( name, recursive ) { for ( var i = 0, l = this.children.length; i < l; i ++ ) { var child = this.children[ i ]; if ( child.name === name ) { return child; } if ( recursive === true ) { child = child.getObjectByName( name, recursive ); if ( child !== undefined ) { return child; } } } return undefined; }, getChildByName: function ( name, recursive ) { console.warn( 'DEPRECATED: Object3D\'s .getChildByName() has been renamed to .getObjectByName().' ); return this.getObjectByName( name, recursive ); }, getDescendants: function ( array ) { if ( array === undefined ) array = []; Array.prototype.push.apply( array, this.children ); for ( var i = 0, l = this.children.length; i < l; i ++ ) { this.children[ i ].getDescendants( array ); } return array; }, updateMatrix: function () { this.matrix.compose( this.position, this.quaternion, this.scale ); this.matrixWorldNeedsUpdate = true; }, updateMatrixWorld: function ( force ) { if ( this.matrixAutoUpdate === true ) this.updateMatrix(); if ( this.matrixWorldNeedsUpdate === true || force === true ) { if ( this.parent === undefined ) { this.matrixWorld.copy( this.matrix ); } else { this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix ); } this.matrixWorldNeedsUpdate = false; force = true; } // update children for ( var i = 0, l = this.children.length; i < l; i ++ ) { this.children[ i ].updateMatrixWorld( force ); } }, clone: function ( object, recursive ) { if ( object === undefined ) object = new THREE.Object3D(); if ( recursive === undefined ) recursive = true; object.name = this.name; object.up.copy( this.up ); object.position.copy( this.position ); object.quaternion.copy( this.quaternion ); object.scale.copy( this.scale ); object.renderDepth = this.renderDepth; object.rotationAutoUpdate = this.rotationAutoUpdate; object.matrix.copy( this.matrix ); object.matrixWorld.copy( this.matrixWorld ); object.matrixAutoUpdate = this.matrixAutoUpdate; object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate; object.visible = this.visible; object.castShadow = this.castShadow; object.receiveShadow = this.receiveShadow; object.frustumCulled = this.frustumCulled; object.userData = JSON.parse( JSON.stringify( this.userData ) ); if ( recursive === true ) { for ( var i = 0; i < this.children.length; i ++ ) { var child = this.children[ i ]; object.add( child.clone() ); } } return object; } }; THREE.EventDispatcher.prototype.apply( THREE.Object3D.prototype ); THREE.Object3DIdCount = 0; /** * @author mrdoob / http://mrdoob.com/ * @author supereggbert / http://www.paulbrunt.co.uk/ * @author julianwa / https://github.com/julianwa */ THREE.Projector = function () { var _object, _objectCount, _objectPool = [], _objectPoolLength = 0, _vertex, _vertexCount, _vertexPool = [], _vertexPoolLength = 0, _face, _face3Count, _face3Pool = [], _face3PoolLength = 0, _line, _lineCount, _linePool = [], _linePoolLength = 0, _sprite, _spriteCount, _spritePool = [], _spritePoolLength = 0, _renderData = { objects: [], sprites: [], lights: [], elements: [] }, _vA = new THREE.Vector3(), _vB = new THREE.Vector3(), _vC = new THREE.Vector3(), _vector3 = new THREE.Vector3(), _vector4 = new THREE.Vector4(), _clipBox = new THREE.Box3( new THREE.Vector3( -1, -1, -1 ), new THREE.Vector3( 1, 1, 1 ) ), _boundingBox = new THREE.Box3(), _points3 = new Array( 3 ), _points4 = new Array( 4 ), _viewMatrix = new THREE.Matrix4(), _viewProjectionMatrix = new THREE.Matrix4(), _modelMatrix, _modelViewProjectionMatrix = new THREE.Matrix4(), _normalMatrix = new THREE.Matrix3(), _centroid = new THREE.Vector3(), _frustum = new THREE.Frustum(), _clippedVertex1PositionScreen = new THREE.Vector4(), _clippedVertex2PositionScreen = new THREE.Vector4(); this.projectVector = function ( vector, camera ) { camera.matrixWorldInverse.getInverse( camera.matrixWorld ); _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); return vector.applyProjection( _viewProjectionMatrix ); }; this.unprojectVector = function () { var projectionMatrixInverse = new THREE.Matrix4(); return function ( vector, camera ) { projectionMatrixInverse.getInverse( camera.projectionMatrix ); _viewProjectionMatrix.multiplyMatrices( camera.matrixWorld, projectionMatrixInverse ); return vector.applyProjection( _viewProjectionMatrix ); }; }(); this.pickingRay = function ( vector, camera ) { // set two vectors with opposing z values vector.z = -1.0; var end = new THREE.Vector3( vector.x, vector.y, 1.0 ); this.unprojectVector( vector, camera ); this.unprojectVector( end, camera ); // find direction from vector to end end.sub( vector ).normalize(); return new THREE.Raycaster( vector, end ); }; var getObject = function ( object ) { _object = getNextObjectInPool(); _object.id = object.id; _object.object = object; if ( object.renderDepth !== null ) { _object.z = object.renderDepth; } else { _vector3.setFromMatrixPosition( object.matrixWorld ); _vector3.applyProjection( _viewProjectionMatrix ); _object.z = _vector3.z; } return _object; }; var projectObject = function ( object ) { if ( object.visible === false ) return; if ( object instanceof THREE.Light ) { _renderData.lights.push( object ); } else if ( object instanceof THREE.Mesh || object instanceof THREE.Line ) { if ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) { _renderData.objects.push( getObject( object ) ); } } else if ( object instanceof THREE.Sprite ) { _renderData.sprites.push( getObject( object ) ); } for ( var i = 0, l = object.children.length; i < l; i ++ ) { projectObject( object.children[ i ] ); } }; var projectGraph = function ( root, sortObjects ) { _objectCount = 0; _renderData.objects.length = 0; _renderData.sprites.length = 0; _renderData.lights.length = 0; projectObject( root ); if ( sortObjects === true ) { _renderData.objects.sort( painterSort ); } }; var RenderList = function () { var normals = []; var object = null; var normalMatrix = new THREE.Matrix3(); var setObject = function ( value ) { object = value; normalMatrix.getNormalMatrix( object.matrixWorld ); normals.length = 0; }; var projectVertex = function ( vertex ) { var position = vertex.position; var positionWorld = vertex.positionWorld; var positionScreen = vertex.positionScreen; positionWorld.copy( position ).applyMatrix4( _modelMatrix ); positionScreen.copy( positionWorld ).applyMatrix4( _viewProjectionMatrix ); var invW = 1 / positionScreen.w; positionScreen.x *= invW; positionScreen.y *= invW; positionScreen.z *= invW; vertex.visible = positionScreen.x >= -1 && positionScreen.x <= 1 && positionScreen.y >= -1 && positionScreen.y <= 1 && positionScreen.z >= -1 && positionScreen.z <= 1; }; var pushVertex = function ( x, y, z ) { _vertex = getNextVertexInPool(); _vertex.position.set( x, y, z ); projectVertex( _vertex ); }; var pushNormal = function ( x, y, z ) { normals.push( x, y, z ); }; var checkTriangleVisibility = function ( v1, v2, v3 ) { _points3[ 0 ] = v1.positionScreen; _points3[ 1 ] = v2.positionScreen; _points3[ 2 ] = v3.positionScreen; if ( v1.visible === true || v2.visible === true || v3.visible === true || _clipBox.isIntersectionBox( _boundingBox.setFromPoints( _points3 ) ) ) { return ( ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) - ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0; } return false; }; var pushLine = function ( a, b ) { var v1 = _vertexPool[ a ]; var v2 = _vertexPool[ b ]; _line = getNextLineInPool(); _line.id = object.id; _line.v1.copy( v1 ); _line.v2.copy( v2 ); _face.z = ( v1.positionScreen.z + v2.positionScreen.z ) / 2; _line.material = object.material; _renderData.elements.push( _line ); }; var pushTriangle = function ( a, b, c ) { var v1 = _vertexPool[ a ]; var v2 = _vertexPool[ b ]; var v3 = _vertexPool[ c ]; if ( checkTriangleVisibility( v1, v2, v3 ) === true ) { _face = getNextFace3InPool(); _face.id = object.id; _face.v1.copy( v1 ); _face.v2.copy( v2 ); _face.v3.copy( v3 ); _face.z = ( v1.positionScreen.z + v2.positionScreen.z + v3.positionScreen.z ) / 3; for ( var i = 0; i < 3; i ++ ) { var offset = arguments[ i ] * 3; var normal = _face.vertexNormalsModel[ i ]; normal.set( normals[ offset + 0 ], normals[ offset + 1 ], normals[ offset + 2 ] ); normal.applyMatrix3( normalMatrix ).normalize(); } _face.vertexNormalsLength = 3; _face.material = object.material; _renderData.elements.push( _face ); } }; return { setObject: setObject, projectVertex: projectVertex, checkTriangleVisibility: checkTriangleVisibility, pushVertex: pushVertex, pushNormal: pushNormal, pushLine: pushLine, pushTriangle: pushTriangle } }; var renderList = new RenderList(); this.projectScene = function ( scene, camera, sortObjects, sortElements ) { var object, geometry, vertices, faces, face, faceVertexNormals, faceVertexUvs, uvs, isFaceMaterial, objectMaterials; _face3Count = 0; _lineCount = 0; _spriteCount = 0; _renderData.elements.length = 0; if ( scene.autoUpdate === true ) scene.updateMatrixWorld(); if ( camera.parent === undefined ) camera.updateMatrixWorld(); _viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) ); _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix ); _frustum.setFromMatrix( _viewProjectionMatrix ); projectGraph( scene, sortObjects ); for ( var o = 0, ol = _renderData.objects.length; o < ol; o ++ ) { object = _renderData.objects[ o ].object; geometry = object.geometry; renderList.setObject( object ); _modelMatrix = object.matrixWorld; _vertexCount = 0; if ( object instanceof THREE.Mesh ) { if ( geometry instanceof THREE.BufferGeometry ) { var attributes = geometry.attributes; if ( attributes.position !== undefined ) { var positions = attributes.position.array; for ( var i = 0, l = positions.length; i < l; i += 3 ) { renderList.pushVertex( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] ); } var normals = attributes.normal.array; for ( var i = 0, l = normals.length; i < l; i += 3 ) { renderList.pushNormal( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] ); } if ( attributes.index !== undefined ) { var indices = attributes.index.array; for ( var i = 0, l = indices.length; i < l; i += 3 ) { renderList.pushTriangle( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] ); } } else { for ( var i = 0, l = positions.length / 3; i < l; i += 3 ) { renderList.pushTriangle( i, i + 1, i + 2 ); } } } } else if ( geometry instanceof THREE.Geometry2 ) { vertices = geometry.vertices; for ( var i = 0, j = 0, l = vertices.length; i < l; i += 9, j += 3 ) { renderList.pushVertex( vertices[ i + 0 ], vertices[ i + 1 ], vertices[ i + 2 ] ); renderList.pushVertex( vertices[ i + 3 ], vertices[ i + 4 ], vertices[ i + 5 ] ); renderList.pushVertex( vertices[ i + 6 ], vertices[ i + 7 ], vertices[ i + 8 ] ); renderList.pushTriangle( j + 0, j + 1, j + 2 ); } } else if ( geometry instanceof THREE.Geometry ) { vertices = geometry.vertices; faces = geometry.faces; faceVertexUvs = geometry.faceVertexUvs; _normalMatrix.getNormalMatrix( _modelMatrix ); isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial; objectMaterials = isFaceMaterial === true ? object.material : null; for ( var v = 0, vl = vertices.length; v < vl; v ++ ) { var vertex = vertices[ v ]; renderList.pushVertex( vertex.x, vertex.y, vertex.z ); } for ( var f = 0, fl = faces.length; f < fl; f ++ ) { face = faces[ f ]; var material = isFaceMaterial === true ? objectMaterials.materials[ face.materialIndex ] : object.material; if ( material === undefined ) continue; var side = material.side; var v1 = _vertexPool[ face.a ]; var v2 = _vertexPool[ face.b ]; var v3 = _vertexPool[ face.c ]; if ( material.morphTargets === true ) { var morphTargets = geometry.morphTargets; var morphInfluences = object.morphTargetInfluences; var v1p = v1.position; var v2p = v2.position; var v3p = v3.position; _vA.set( 0, 0, 0 ); _vB.set( 0, 0, 0 ); _vC.set( 0, 0, 0 ); for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) { var influence = morphInfluences[ t ]; if ( influence === 0 ) continue; var targets = morphTargets[ t ].vertices; _vA.x += ( targets[ face.a ].x - v1p.x ) * influence; _vA.y += ( targets[ face.a ].y - v1p.y ) * influence; _vA.z += ( targets[ face.a ].z - v1p.z ) * influence; _vB.x += ( targets[ face.b ].x - v2p.x ) * influence; _vB.y += ( targets[ face.b ].y - v2p.y ) * influence; _vB.z += ( targets[ face.b ].z - v2p.z ) * influence; _vC.x += ( targets[ face.c ].x - v3p.x ) * influence; _vC.y += ( targets[ face.c ].y - v3p.y ) * influence; _vC.z += ( targets[ face.c ].z - v3p.z ) * influence; } v1.position.add( _vA ); v2.position.add( _vB ); v3.position.add( _vC ); renderList.projectVertex( v1 ); renderList.projectVertex( v2 ); renderList.projectVertex( v3 ); } var visible = renderList.checkTriangleVisibility( v1, v2, v3 ); if ( visible === ( side === THREE.BackSide ) ) continue; _face = getNextFace3InPool(); _face.id = object.id; _face.v1.copy( v1 ); _face.v2.copy( v2 ); _face.v3.copy( v3 ); _face.normalModel.copy( face.normal ); if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) { _face.normalModel.negate(); } _face.normalModel.applyMatrix3( _normalMatrix ).normalize(); _face.centroidModel.copy( face.centroid ).applyMatrix4( _modelMatrix ); faceVertexNormals = face.vertexNormals; for ( var n = 0, nl = Math.min( faceVertexNormals.length, 3 ); n < nl; n ++ ) { var normalModel = _face.vertexNormalsModel[ n ]; normalModel.copy( faceVertexNormals[ n ] ); if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) { normalModel.negate(); } normalModel.applyMatrix3( _normalMatrix ).normalize(); } _face.vertexNormalsLength = faceVertexNormals.length; for ( var c = 0, cl = Math.min( faceVertexUvs.length, 3 ); c < cl; c ++ ) { uvs = faceVertexUvs[ c ][ f ]; if ( uvs === undefined ) continue; for ( var u = 0, ul = uvs.length; u < ul; u ++ ) { _face.uvs[ c ][ u ] = uvs[ u ]; } } _face.color = face.color; _face.material = material; _centroid.copy( _face.centroidModel ).applyProjection( _viewProjectionMatrix ); _face.z = _centroid.z; _renderData.elements.push( _face ); } } } else if ( object instanceof THREE.Line ) { if ( geometry instanceof THREE.BufferGeometry ) { var attributes = geometry.attributes; if ( attributes.position !== undefined ) { var positions = attributes.position.array; for ( var i = 0, l = positions.length; i < l; i += 3 ) { renderList.pushVertex( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] ); } if ( attributes.index !== undefined ) { var indices = attributes.index.array; for ( var i = 0, l = indices.length; i < l; i += 2 ) { renderList.pushLine( indices[ i ], indices[ i + 1 ] ); } } else { for ( var i = 0, l = ( positions.length / 3 ) - 1; i < l; i ++ ) { renderList.pushLine( i, i + 1 ); } } } } else if ( geometry instanceof THREE.Geometry ) { _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix ); vertices = object.geometry.vertices; v1 = getNextVertexInPool(); v1.positionScreen.copy( vertices[ 0 ] ).applyMatrix4( _modelViewProjectionMatrix ); // Handle LineStrip and LinePieces var step = object.type === THREE.LinePieces ? 2 : 1; for ( v = 1, vl = vertices.length; v < vl; v ++ ) { v1 = getNextVertexInPool(); v1.positionScreen.copy( vertices[ v ] ).applyMatrix4( _modelViewProjectionMatrix ); if ( ( v + 1 ) % step > 0 ) continue; v2 = _vertexPool[ _vertexCount - 2 ]; _clippedVertex1PositionScreen.copy( v1.positionScreen ); _clippedVertex2PositionScreen.copy( v2.positionScreen ); if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) === true ) { // Perform the perspective divide _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w ); _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w ); _line = getNextLineInPool(); _line.id = object.id; _line.v1.positionScreen.copy( _clippedVertex1PositionScreen ); _line.v2.positionScreen.copy( _clippedVertex2PositionScreen ); _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z ); _line.material = object.material; if ( object.material.vertexColors === THREE.VertexColors ) { _line.vertexColors[ 0 ].copy( object.geometry.colors[ v ] ); _line.vertexColors[ 1 ].copy( object.geometry.colors[ v - 1 ] ); } _renderData.elements.push( _line ); } } } } } for ( o = 0, ol = _renderData.sprites.length; o < ol; o++ ) { object = _renderData.sprites[ o ].object; _modelMatrix = object.matrixWorld; _vector4.set( _modelMatrix.elements[12], _modelMatrix.elements[13], _modelMatrix.elements[14], 1 ); _vector4.applyMatrix4( _viewProjectionMatrix ); var invW = 1 / _vector4.w; _vector4.z *= invW; if ( _vector4.z >= -1 && _vector4.z <= 1 ) { _sprite = getNextSpriteInPool(); _sprite.id = object.id; _sprite.x = _vector4.x * invW; _sprite.y = _vector4.y * invW; _sprite.z = _vector4.z; _sprite.object = object; _sprite.rotation = object.rotation; _sprite.scale.x = object.scale.x * Math.abs( _sprite.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) ); _sprite.scale.y = object.scale.y * Math.abs( _sprite.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) ); _sprite.material = object.material; _renderData.elements.push( _sprite ); } } if ( sortElements === true ) _renderData.elements.sort( painterSort ); return _renderData; }; // Pools function getNextObjectInPool() { if ( _objectCount === _objectPoolLength ) { var object = new THREE.RenderableObject(); _objectPool.push( object ); _objectPoolLength ++; _objectCount ++; return object; } return _objectPool[ _objectCount ++ ]; } function getNextVertexInPool() { if ( _vertexCount === _vertexPoolLength ) { var vertex = new THREE.RenderableVertex(); _vertexPool.push( vertex ); _vertexPoolLength ++; _vertexCount ++; return vertex; } return _vertexPool[ _vertexCount ++ ]; } function getNextFace3InPool() { if ( _face3Count === _face3PoolLength ) { var face = new THREE.RenderableFace3(); _face3Pool.push( face ); _face3PoolLength ++; _face3Count ++; return face; } return _face3Pool[ _face3Count ++ ]; } function getNextLineInPool() { if ( _lineCount === _linePoolLength ) { var line = new THREE.RenderableLine(); _linePool.push( line ); _linePoolLength ++; _lineCount ++ return line; } return _linePool[ _lineCount ++ ]; } function getNextSpriteInPool() { if ( _spriteCount === _spritePoolLength ) { var sprite = new THREE.RenderableSprite(); _spritePool.push( sprite ); _spritePoolLength ++; _spriteCount ++ return sprite; } return _spritePool[ _spriteCount ++ ]; } // function painterSort( a, b ) { if ( a.z !== b.z ) { return b.z - a.z; } else if ( a.id !== b.id ) { return a.id - b.id; } else { return 0; } } function clipLine( s1, s2 ) { var alpha1 = 0, alpha2 = 1, // Calculate the boundary coordinate of each vertex for the near and far clip planes, // Z = -1 and Z = +1, respectively. bc1near = s1.z + s1.w, bc2near = s2.z + s2.w, bc1far = - s1.z + s1.w, bc2far = - s2.z + s2.w; if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) { // Both vertices lie entirely within all clip planes. return true; } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) { // Both vertices lie entirely outside one of the clip planes. return false; } else { // The line segment spans at least one clip plane. if ( bc1near < 0 ) { // v1 lies outside the near plane, v2 inside alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) ); } else if ( bc2near < 0 ) { // v2 lies outside the near plane, v1 inside alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) ); } if ( bc1far < 0 ) { // v1 lies outside the far plane, v2 inside alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) ); } else if ( bc2far < 0 ) { // v2 lies outside the far plane, v2 inside alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) ); } if ( alpha2 < alpha1 ) { // The line segment spans two boundaries, but is outside both of them. // (This can't happen when we're only clipping against just near/far but good // to leave the check here for future usage if other clip planes are added.) return false; } else { // Update the s1 and s2 vertices to match the clipped line segment. s1.lerp( s2, alpha1 ); s2.lerp( s1, 1 - alpha2 ); return true; } } } }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ THREE.Face3 = function ( a, b, c, normal, color, materialIndex ) { this.a = a; this.b = b; this.c = c; this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3(); this.vertexNormals = normal instanceof Array ? normal : [ ]; this.color = color instanceof THREE.Color ? color : new THREE.Color(); this.vertexColors = color instanceof Array ? color : []; this.vertexTangents = []; this.materialIndex = materialIndex !== undefined ? materialIndex : 0; this.centroid = new THREE.Vector3(); }; THREE.Face3.prototype = { constructor: THREE.Face3, clone: function () { var face = new THREE.Face3( this.a, this.b, this.c ); face.normal.copy( this.normal ); face.color.copy( this.color ); face.centroid.copy( this.centroid ); face.materialIndex = this.materialIndex; var i, il; for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone(); for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone(); for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone(); return face; } }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.Face4 = function ( a, b, c, d, normal, color, materialIndex ) { console.warn( 'THREE.Face4 has been removed. A THREE.Face3 will be created instead.') return new THREE.Face3( a, b, c, normal, color, materialIndex ); }; /** * @author mrdoob / http://mrdoob.com/ * @author kile / http://kile.stravaganza.org/ * @author alteredq / http://alteredqualia.com/ * @author mikael emtinger / http://gomo.se/ * @author zz85 / http://www.lab4games.net/zz85/blog * @author bhouston / http://exocortex.com */ THREE.Geometry = function () { this.id = THREE.GeometryIdCount ++; this.uuid = THREE.Math.generateUUID(); this.name = ''; this.vertices = []; this.colors = []; // one-to-one vertex colors, used in ParticleSystem and Line this.faces = []; this.faceVertexUvs = [[]]; this.morphTargets = []; this.morphColors = []; this.morphNormals = []; this.skinWeights = []; this.skinIndices = []; this.lineDistances = []; this.boundingBox = null; this.boundingSphere = null; this.hasTangents = false; this.dynamic = true; // the intermediate typed arrays will be deleted when set to false // update flags this.verticesNeedUpdate = false; this.elementsNeedUpdate = false; this.uvsNeedUpdate = false; this.normalsNeedUpdate = false; this.tangentsNeedUpdate = false; this.colorsNeedUpdate = false; this.lineDistancesNeedUpdate = false; this.buffersNeedUpdate = false; }; THREE.Geometry.prototype = { constructor: THREE.Geometry, applyMatrix: function ( matrix ) { var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix ); for ( var i = 0, il = this.vertices.length; i < il; i ++ ) { var vertex = this.vertices[ i ]; vertex.applyMatrix4( matrix ); } for ( var i = 0, il = this.faces.length; i < il; i ++ ) { var face = this.faces[ i ]; face.normal.applyMatrix3( normalMatrix ).normalize(); for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) { face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize(); } face.centroid.applyMatrix4( matrix ); } if ( this.boundingBox instanceof THREE.Box3 ) { this.computeBoundingBox(); } if ( this.boundingSphere instanceof THREE.Sphere ) { this.computeBoundingSphere(); } }, computeCentroids: function () { var f, fl, face; for ( f = 0, fl = this.faces.length; f < fl; f ++ ) { face = this.faces[ f ]; face.centroid.set( 0, 0, 0 ); face.centroid.add( this.vertices[ face.a ] ); face.centroid.add( this.vertices[ face.b ] ); face.centroid.add( this.vertices[ face.c ] ); face.centroid.divideScalar( 3 ); } }, computeFaceNormals: function () { var cb = new THREE.Vector3(), ab = new THREE.Vector3(); for ( var f = 0, fl = this.faces.length; f < fl; f ++ ) { var face = this.faces[ f ]; var vA = this.vertices[ face.a ]; var vB = this.vertices[ face.b ]; var vC = this.vertices[ face.c ]; cb.subVectors( vC, vB ); ab.subVectors( vA, vB ); cb.cross( ab ); cb.normalize(); face.normal.copy( cb ); } }, computeVertexNormals: function ( areaWeighted ) { var v, vl, f, fl, face, vertices; vertices = new Array( this.vertices.length ); for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) { vertices[ v ] = new THREE.Vector3(); } if ( areaWeighted ) { // vertex normals weighted by triangle areas // http://www.iquilezles.org/www/articles/normals/normals.htm var vA, vB, vC, vD; var cb = new THREE.Vector3(), ab = new THREE.Vector3(), db = new THREE.Vector3(), dc = new THREE.Vector3(), bc = new THREE.Vector3(); for ( f = 0, fl = this.faces.length; f < fl; f ++ ) { face = this.faces[ f ]; vA = this.vertices[ face.a ]; vB = this.vertices[ face.b ]; vC = this.vertices[ face.c ]; cb.subVectors( vC, vB ); ab.subVectors( vA, vB ); cb.cross( ab ); vertices[ face.a ].add( cb ); vertices[ face.b ].add( cb ); vertices[ face.c ].add( cb ); } } else { for ( f = 0, fl = this.faces.length; f < fl; f ++ ) { face = this.faces[ f ]; vertices[ face.a ].add( face.normal ); vertices[ face.b ].add( face.normal ); vertices[ face.c ].add( face.normal ); } } for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) { vertices[ v ].normalize(); } for ( f = 0, fl = this.faces.length; f < fl; f ++ ) { face = this.faces[ f ]; face.vertexNormals[ 0 ] = vertices[ face.a ].clone(); face.vertexNormals[ 1 ] = vertices[ face.b ].clone(); face.vertexNormals[ 2 ] = vertices[ face.c ].clone(); } }, computeMorphNormals: function () { var i, il, f, fl, face; // save original normals // - create temp variables on first access // otherwise just copy (for faster repeated calls) for ( f = 0, fl = this.faces.length; f < fl; f ++ ) { face = this.faces[ f ]; if ( ! face.__originalFaceNormal ) { face.__originalFaceNormal = face.normal.clone(); } else { face.__originalFaceNormal.copy( face.normal ); } if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = []; for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) { if ( ! face.__originalVertexNormals[ i ] ) { face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone(); } else { face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] ); } } } // use temp geometry to compute face and vertex normals for each morph var tmpGeo = new THREE.Geometry(); tmpGeo.faces = this.faces; for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) { // create on first access if ( ! this.morphNormals[ i ] ) { this.morphNormals[ i ] = {}; this.morphNormals[ i ].faceNormals = []; this.morphNormals[ i ].vertexNormals = []; var dstNormalsFace = this.morphNormals[ i ].faceNormals; var dstNormalsVertex = this.morphNormals[ i ].vertexNormals; var faceNormal, vertexNormals; for ( f = 0, fl = this.faces.length; f < fl; f ++ ) { face = this.faces[ f ]; faceNormal = new THREE.Vector3(); vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() }; dstNormalsFace.push( faceNormal ); dstNormalsVertex.push( vertexNormals ); } } var morphNormals = this.morphNormals[ i ]; // set vertices to morph target tmpGeo.vertices = this.morphTargets[ i ].vertices; // compute morph normals tmpGeo.computeFaceNormals(); tmpGeo.computeVertexNormals(); // store morph normals var faceNormal, vertexNormals; for ( f = 0, fl = this.faces.length; f < fl; f ++ ) { face = this.faces[ f ]; faceNormal = morphNormals.faceNormals[ f ]; vertexNormals = morphNormals.vertexNormals[ f ]; faceNormal.copy( face.normal ); vertexNormals.a.copy( face.vertexNormals[ 0 ] ); vertexNormals.b.copy( face.vertexNormals[ 1 ] ); vertexNormals.c.copy( face.vertexNormals[ 2 ] ); } } // restore original normals for ( f = 0, fl = this.faces.length; f < fl; f ++ ) { face = this.faces[ f ]; face.normal = face.__originalFaceNormal; face.vertexNormals = face.__originalVertexNormals; } }, computeTangents: function () { // based on http://www.terathon.com/code/tangent.html // tangents go to vertices var f, fl, v, vl, i, il, vertexIndex, face, uv, vA, vB, vC, uvA, uvB, uvC, x1, x2, y1, y2, z1, z2, s1, s2, t1, t2, r, t, test, tan1 = [], tan2 = [], sdir = new THREE.Vector3(), tdir = new THREE.Vector3(), tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(), n = new THREE.Vector3(), w; for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) { tan1[ v ] = new THREE.Vector3(); tan2[ v ] = new THREE.Vector3(); } function handleTriangle( context, a, b, c, ua, ub, uc ) { vA = context.vertices[ a ]; vB = context.vertices[ b ]; vC = context.vertices[ c ]; uvA = uv[ ua ]; uvB = uv[ ub ]; uvC = uv[ uc ]; x1 = vB.x - vA.x; x2 = vC.x - vA.x; y1 = vB.y - vA.y; y2 = vC.y - vA.y; z1 = vB.z - vA.z; z2 = vC.z - vA.z; s1 = uvB.x - uvA.x; s2 = uvC.x - uvA.x; t1 = uvB.y - uvA.y; t2 = uvC.y - uvA.y; r = 1.0 / ( s1 * t2 - s2 * t1 ); sdir.set( ( t2 * x1 - t1 * x2 ) * r, ( t2 * y1 - t1 * y2 ) * r, ( t2 * z1 - t1 * z2 ) * r ); tdir.set( ( s1 * x2 - s2 * x1 ) * r, ( s1 * y2 - s2 * y1 ) * r, ( s1 * z2 - s2 * z1 ) * r ); tan1[ a ].add( sdir ); tan1[ b ].add( sdir ); tan1[ c ].add( sdir ); tan2[ a ].add( tdir ); tan2[ b ].add( tdir ); tan2[ c ].add( tdir ); } for ( f = 0, fl = this.faces.length; f < fl; f ++ ) { face = this.faces[ f ]; uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 ); } var faceIndex = [ 'a', 'b', 'c', 'd' ]; for ( f = 0, fl = this.faces.length; f < fl; f ++ ) { face = this.faces[ f ]; for ( i = 0; i < Math.min( face.vertexNormals.length, 3 ); i++ ) { n.copy( face.vertexNormals[ i ] ); vertexIndex = face[ faceIndex[ i ] ]; t = tan1[ vertexIndex ]; // Gram-Schmidt orthogonalize tmp.copy( t ); tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize(); // Calculate handedness tmp2.crossVectors( face.vertexNormals[ i ], t ); test = tmp2.dot( tan2[ vertexIndex ] ); w = (test < 0.0) ? -1.0 : 1.0; face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w ); } } this.hasTangents = true; }, computeLineDistances: function ( ) { var d = 0; var vertices = this.vertices; for ( var i = 0, il = vertices.length; i < il; i ++ ) { if ( i > 0 ) { d += vertices[ i ].distanceTo( vertices[ i - 1 ] ); } this.lineDistances[ i ] = d; } }, computeBoundingBox: function () { if ( this.boundingBox === null ) { this.boundingBox = new THREE.Box3(); } this.boundingBox.setFromPoints( this.vertices ); }, computeBoundingSphere: function () { if ( this.boundingSphere === null ) { this.boundingSphere = new THREE.Sphere(); } this.boundingSphere.setFromPoints( this.vertices ); }, /* * Checks for duplicate vertices with hashmap. * Duplicated vertices are removed * and faces' vertices are updated. */ mergeVertices: function () { var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique) var unique = [], changes = []; var v, key; var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001 var precision = Math.pow( 10, precisionPoints ); var i,il, face; var indices, k, j, jl, u; for ( i = 0, il = this.vertices.length; i < il; i ++ ) { v = this.vertices[ i ]; key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision ); if ( verticesMap[ key ] === undefined ) { verticesMap[ key ] = i; unique.push( this.vertices[ i ] ); changes[ i ] = unique.length - 1; } else { //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]); changes[ i ] = changes[ verticesMap[ key ] ]; } }; // if faces are completely degenerate after merging vertices, we // have to remove them from the geometry. var faceIndicesToRemove = []; for( i = 0, il = this.faces.length; i < il; i ++ ) { face = this.faces[ i ]; face.a = changes[ face.a ]; face.b = changes[ face.b ]; face.c = changes[ face.c ]; indices = [ face.a, face.b, face.c ]; var dupIndex = -1; // if any duplicate vertices are found in a Face3 // we have to remove the face as nothing can be saved for ( var n = 0; n < 3; n ++ ) { if ( indices[ n ] == indices[ ( n + 1 ) % 3 ] ) { dupIndex = n; faceIndicesToRemove.push( i ); break; } } } for ( i = faceIndicesToRemove.length - 1; i >= 0; i -- ) { var idx = faceIndicesToRemove[ i ]; this.faces.splice( idx, 1 ); for ( j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) { this.faceVertexUvs[ j ].splice( idx, 1 ); } } // Use unique set of vertices var diff = this.vertices.length - unique.length; this.vertices = unique; return diff; }, clone: function () { var geometry = new THREE.Geometry(); var vertices = this.vertices; for ( var i = 0, il = vertices.length; i < il; i ++ ) { geometry.vertices.push( vertices[ i ].clone() ); } var faces = this.faces; for ( var i = 0, il = faces.length; i < il; i ++ ) { geometry.faces.push( faces[ i ].clone() ); } var uvs = this.faceVertexUvs[ 0 ]; for ( var i = 0, il = uvs.length; i < il; i ++ ) { var uv = uvs[ i ], uvCopy = []; for ( var j = 0, jl = uv.length; j < jl; j ++ ) { uvCopy.push( new THREE.Vector2( uv[ j ].x, uv[ j ].y ) ); } geometry.faceVertexUvs[ 0 ].push( uvCopy ); } return geometry; }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } }; THREE.EventDispatcher.prototype.apply( THREE.Geometry.prototype ); THREE.GeometryIdCount = 0; /** * @author mrdoob / http://mrdoob.com/ */ THREE.Geometry2 = function ( size ) { this.id = THREE.GeometryIdCount ++; this.uuid = THREE.Math.generateUUID(); this.name = ''; this.vertices = size !== undefined ? new Float32Array( size * 3 ) : []; this.normals = size !== undefined ? new Float32Array( size * 3 ) : []; this.uvs = size !== undefined ? new Float32Array( size * 2 ) : []; this.boundingBox = null; this.boundingSphere = null; }; THREE.Geometry2.prototype = { constructor: THREE.Geometry2, applyMatrix: function ( matrix ) { matrix.multiplyVector3Array( this.vertices ); }, computeBoundingSphere: function () { var box = new THREE.Box3(); var vector = new THREE.Vector3(); return function () { if ( this.boundingSphere === null ) { this.boundingSphere = new THREE.Sphere(); } box.makeEmpty(); var vertices = this.vertices; var center = this.boundingSphere.center; for ( var i = 0, il = vertices.length; i < il; i += 3 ) { vector.set( vertices[ i ], vertices[ i + 1 ], vertices[ i + 2 ] ); box.addPoint( vector ); } box.center( center ); var maxRadiusSq = 0; for ( var i = 0, il = vertices.length; i < il; i += 3 ) { vector.set( vertices[ i ], vertices[ i + 1 ], vertices[ i + 2 ] ); maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) ); } this.boundingSphere.radius = Math.sqrt( maxRadiusSq ); } }(), dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } }; THREE.EventDispatcher.prototype.apply( THREE.Geometry2.prototype ); /** * @author alteredq / http://alteredqualia.com/ */ THREE.BufferGeometry = function () { this.id = THREE.GeometryIdCount ++; this.uuid = THREE.Math.generateUUID(); this.name = ''; // attributes this.attributes = {}; // attributes typed arrays are kept only if dynamic flag is set this.dynamic = true; // offsets for chunks when using indexed elements this.offsets = []; // boundings this.boundingBox = null; this.boundingSphere = null; this.hasTangents = false; }; THREE.BufferGeometry.prototype = { constructor: THREE.BufferGeometry, addAttribute: function ( name, type, numItems, itemSize ) { this.attributes[ name ] = { itemSize: itemSize, array: new type( numItems * itemSize ) }; }, applyMatrix: function ( matrix ) { var positionArray; var normalArray; if ( this.attributes[ "position" ] ) positionArray = this.attributes[ "position" ].array; if ( this.attributes[ "normal" ] ) normalArray = this.attributes[ "normal" ].array; if ( positionArray !== undefined ) { matrix.multiplyVector3Array( positionArray ); this.verticesNeedUpdate = true; } if ( normalArray !== undefined ) { var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix ); normalMatrix.multiplyVector3Array( normalArray ); this.normalizeNormals(); this.normalsNeedUpdate = true; } }, computeBoundingBox: function () { if ( this.boundingBox === null ) { this.boundingBox = new THREE.Box3(); } var positions = this.attributes[ "position" ].array; if ( positions ) { var bb = this.boundingBox; var x, y, z; if( positions.length >= 3 ) { bb.min.x = bb.max.x = positions[ 0 ]; bb.min.y = bb.max.y = positions[ 1 ]; bb.min.z = bb.max.z = positions[ 2 ]; } for ( var i = 3, il = positions.length; i < il; i += 3 ) { x = positions[ i ]; y = positions[ i + 1 ]; z = positions[ i + 2 ]; // bounding box if ( x < bb.min.x ) { bb.min.x = x; } else if ( x > bb.max.x ) { bb.max.x = x; } if ( y < bb.min.y ) { bb.min.y = y; } else if ( y > bb.max.y ) { bb.max.y = y; } if ( z < bb.min.z ) { bb.min.z = z; } else if ( z > bb.max.z ) { bb.max.z = z; } } } if ( positions === undefined || positions.length === 0 ) { this.boundingBox.min.set( 0, 0, 0 ); this.boundingBox.max.set( 0, 0, 0 ); } }, computeBoundingSphere: function () { var box = new THREE.Box3(); var vector = new THREE.Vector3(); return function () { if ( this.boundingSphere === null ) { this.boundingSphere = new THREE.Sphere(); } var positions = this.attributes[ "position" ].array; if ( positions ) { box.makeEmpty(); var center = this.boundingSphere.center; for ( var i = 0, il = positions.length; i < il; i += 3 ) { vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] ); box.addPoint( vector ); } box.center( center ); var maxRadiusSq = 0; for ( var i = 0, il = positions.length; i < il; i += 3 ) { vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] ); maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) ); } this.boundingSphere.radius = Math.sqrt( maxRadiusSq ); } } }(), computeVertexNormals: function () { if ( this.attributes[ "position" ] ) { var i, il; var j, jl; var nVertexElements = this.attributes[ "position" ].array.length; if ( this.attributes[ "normal" ] === undefined ) { this.attributes[ "normal" ] = { itemSize: 3, array: new Float32Array( nVertexElements ) }; } else { // reset existing normals to zero for ( i = 0, il = this.attributes[ "normal" ].array.length; i < il; i ++ ) { this.attributes[ "normal" ].array[ i ] = 0; } } var positions = this.attributes[ "position" ].array; var normals = this.attributes[ "normal" ].array; var vA, vB, vC, x, y, z, pA = new THREE.Vector3(), pB = new THREE.Vector3(), pC = new THREE.Vector3(), cb = new THREE.Vector3(), ab = new THREE.Vector3(); // indexed elements if ( this.attributes[ "index" ] ) { var indices = this.attributes[ "index" ].array; var offsets = this.offsets; for ( j = 0, jl = offsets.length; j < jl; ++ j ) { var start = offsets[ j ].start; var count = offsets[ j ].count; var index = offsets[ j ].index; for ( i = start, il = start + count; i < il; i += 3 ) { vA = index + indices[ i ]; vB = index + indices[ i + 1 ]; vC = index + indices[ i + 2 ]; x = positions[ vA * 3 ]; y = positions[ vA * 3 + 1 ]; z = positions[ vA * 3 + 2 ]; pA.set( x, y, z ); x = positions[ vB * 3 ]; y = positions[ vB * 3 + 1 ]; z = positions[ vB * 3 + 2 ]; pB.set( x, y, z ); x = positions[ vC * 3 ]; y = positions[ vC * 3 + 1 ]; z = positions[ vC * 3 + 2 ]; pC.set( x, y, z ); cb.subVectors( pC, pB ); ab.subVectors( pA, pB ); cb.cross( ab ); normals[ vA * 3 ] += cb.x; normals[ vA * 3 + 1 ] += cb.y; normals[ vA * 3 + 2 ] += cb.z; normals[ vB * 3 ] += cb.x; normals[ vB * 3 + 1 ] += cb.y; normals[ vB * 3 + 2 ] += cb.z; normals[ vC * 3 ] += cb.x; normals[ vC * 3 + 1 ] += cb.y; normals[ vC * 3 + 2 ] += cb.z; } } // non-indexed elements (unconnected triangle soup) } else { for ( i = 0, il = positions.length; i < il; i += 9 ) { x = positions[ i ]; y = positions[ i + 1 ]; z = positions[ i + 2 ]; pA.set( x, y, z ); x = positions[ i + 3 ]; y = positions[ i + 4 ]; z = positions[ i + 5 ]; pB.set( x, y, z ); x = positions[ i + 6 ]; y = positions[ i + 7 ]; z = positions[ i + 8 ]; pC.set( x, y, z ); cb.subVectors( pC, pB ); ab.subVectors( pA, pB ); cb.cross( ab ); normals[ i ] = cb.x; normals[ i + 1 ] = cb.y; normals[ i + 2 ] = cb.z; normals[ i + 3 ] = cb.x; normals[ i + 4 ] = cb.y; normals[ i + 5 ] = cb.z; normals[ i + 6 ] = cb.x; normals[ i + 7 ] = cb.y; normals[ i + 8 ] = cb.z; } } this.normalizeNormals(); this.normalsNeedUpdate = true; } }, normalizeNormals: function () { var normals = this.attributes[ "normal" ].array; var x, y, z, n; for ( var i = 0, il = normals.length; i < il; i += 3 ) { x = normals[ i ]; y = normals[ i + 1 ]; z = normals[ i + 2 ]; n = 1.0 / Math.sqrt( x * x + y * y + z * z ); normals[ i ] *= n; normals[ i + 1 ] *= n; normals[ i + 2 ] *= n; } }, computeTangents: function () { // based on http://www.terathon.com/code/tangent.html // (per vertex tangents) if ( this.attributes[ "index" ] === undefined || this.attributes[ "position" ] === undefined || this.attributes[ "normal" ] === undefined || this.attributes[ "uv" ] === undefined ) { console.warn( "Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()" ); return; } var indices = this.attributes[ "index" ].array; var positions = this.attributes[ "position" ].array; var normals = this.attributes[ "normal" ].array; var uvs = this.attributes[ "uv" ].array; var nVertices = positions.length / 3; if ( this.attributes[ "tangent" ] === undefined ) { var nTangentElements = 4 * nVertices; this.attributes[ "tangent" ] = { itemSize: 4, array: new Float32Array( nTangentElements ) }; } var tangents = this.attributes[ "tangent" ].array; var tan1 = [], tan2 = []; for ( var k = 0; k < nVertices; k ++ ) { tan1[ k ] = new THREE.Vector3(); tan2[ k ] = new THREE.Vector3(); } var xA, yA, zA, xB, yB, zB, xC, yC, zC, uA, vA, uB, vB, uC, vC, x1, x2, y1, y2, z1, z2, s1, s2, t1, t2, r; var sdir = new THREE.Vector3(), tdir = new THREE.Vector3(); function handleTriangle( a, b, c ) { xA = positions[ a * 3 ]; yA = positions[ a * 3 + 1 ]; zA = positions[ a * 3 + 2 ]; xB = positions[ b * 3 ]; yB = positions[ b * 3 + 1 ]; zB = positions[ b * 3 + 2 ]; xC = positions[ c * 3 ]; yC = positions[ c * 3 + 1 ]; zC = positions[ c * 3 + 2 ]; uA = uvs[ a * 2 ]; vA = uvs[ a * 2 + 1 ]; uB = uvs[ b * 2 ]; vB = uvs[ b * 2 + 1 ]; uC = uvs[ c * 2 ]; vC = uvs[ c * 2 + 1 ]; x1 = xB - xA; x2 = xC - xA; y1 = yB - yA; y2 = yC - yA; z1 = zB - zA; z2 = zC - zA; s1 = uB - uA; s2 = uC - uA; t1 = vB - vA; t2 = vC - vA; r = 1.0 / ( s1 * t2 - s2 * t1 ); sdir.set( ( t2 * x1 - t1 * x2 ) * r, ( t2 * y1 - t1 * y2 ) * r, ( t2 * z1 - t1 * z2 ) * r ); tdir.set( ( s1 * x2 - s2 * x1 ) * r, ( s1 * y2 - s2 * y1 ) * r, ( s1 * z2 - s2 * z1 ) * r ); tan1[ a ].add( sdir ); tan1[ b ].add( sdir ); tan1[ c ].add( sdir ); tan2[ a ].add( tdir ); tan2[ b ].add( tdir ); tan2[ c ].add( tdir ); } var i, il; var j, jl; var iA, iB, iC; var offsets = this.offsets; for ( j = 0, jl = offsets.length; j < jl; ++ j ) { var start = offsets[ j ].start; var count = offsets[ j ].count; var index = offsets[ j ].index; for ( i = start, il = start + count; i < il; i += 3 ) { iA = index + indices[ i ]; iB = index + indices[ i + 1 ]; iC = index + indices[ i + 2 ]; handleTriangle( iA, iB, iC ); } } var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(); var n = new THREE.Vector3(), n2 = new THREE.Vector3(); var w, t, test; function handleVertex( v ) { n.x = normals[ v * 3 ]; n.y = normals[ v * 3 + 1 ]; n.z = normals[ v * 3 + 2 ]; n2.copy( n ); t = tan1[ v ]; // Gram-Schmidt orthogonalize tmp.copy( t ); tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize(); // Calculate handedness tmp2.crossVectors( n2, t ); test = tmp2.dot( tan2[ v ] ); w = ( test < 0.0 ) ? -1.0 : 1.0; tangents[ v * 4 ] = tmp.x; tangents[ v * 4 + 1 ] = tmp.y; tangents[ v * 4 + 2 ] = tmp.z; tangents[ v * 4 + 3 ] = w; } for ( j = 0, jl = offsets.length; j < jl; ++ j ) { var start = offsets[ j ].start; var count = offsets[ j ].count; var index = offsets[ j ].index; for ( i = start, il = start + count; i < il; i += 3 ) { iA = index + indices[ i ]; iB = index + indices[ i + 1 ]; iC = index + indices[ i + 2 ]; handleVertex( iA ); handleVertex( iB ); handleVertex( iC ); } } this.hasTangents = true; this.tangentsNeedUpdate = true; }, clone: function () { var geometry = new THREE.BufferGeometry(); var types = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array ]; for ( var attr in this.attributes ) { var sourceAttr = this.attributes[ attr ]; var sourceArray = sourceAttr.array; var attribute = { itemSize: sourceAttr.itemSize, array: null }; for ( var i = 0, il = types.length; i < il; i ++ ) { var type = types[ i ]; if ( sourceArray instanceof type ) { attribute.array = new type( sourceArray ); break; } } geometry.attributes[ attr ] = attribute; } for ( var i = 0, il = this.offsets.length; i < il; i ++ ) { var offset = this.offsets[ i ]; geometry.offsets.push( { start: offset.start, index: offset.index, count: offset.count } ); } return geometry; }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } }; THREE.EventDispatcher.prototype.apply( THREE.BufferGeometry.prototype ); /** * @author mrdoob / http://mrdoob.com/ * @author mikael emtinger / http://gomo.se/ * @author WestLangley / http://github.com/WestLangley */ THREE.Camera = function () { THREE.Object3D.call( this ); this.matrixWorldInverse = new THREE.Matrix4(); this.projectionMatrix = new THREE.Matrix4(); }; THREE.Camera.prototype = Object.create( THREE.Object3D.prototype ); THREE.Camera.prototype.lookAt = function () { // This routine does not support cameras with rotated and/or translated parent(s) var m1 = new THREE.Matrix4(); return function ( vector ) { m1.lookAt( this.position, vector, this.up ); this.quaternion.setFromRotationMatrix( m1 ); }; }(); THREE.Camera.prototype.clone = function (camera) { if ( camera === undefined ) camera = new THREE.Camera(); THREE.Object3D.prototype.clone.call( this, camera ); camera.matrixWorldInverse.copy( this.matrixWorldInverse ); camera.projectionMatrix.copy( this.projectionMatrix ); return camera; }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.OrthographicCamera = function ( left, right, top, bottom, near, far ) { THREE.Camera.call( this ); this.left = left; this.right = right; this.top = top; this.bottom = bottom; this.near = ( near !== undefined ) ? near : 0.1; this.far = ( far !== undefined ) ? far : 2000; this.updateProjectionMatrix(); }; THREE.OrthographicCamera.prototype = Object.create( THREE.Camera.prototype ); THREE.OrthographicCamera.prototype.updateProjectionMatrix = function () { this.projectionMatrix.makeOrthographic( this.left, this.right, this.top, this.bottom, this.near, this.far ); }; THREE.OrthographicCamera.prototype.clone = function () { var camera = new THREE.OrthographicCamera(); THREE.Camera.prototype.clone.call( this, camera ); camera.left = this.left; camera.right = this.right; camera.top = this.top; camera.bottom = this.bottom; camera.near = this.near; camera.far = this.far; return camera; }; /** * @author mrdoob / http://mrdoob.com/ * @author greggman / http://games.greggman.com/ * @author zz85 / http://www.lab4games.net/zz85/blog */ THREE.PerspectiveCamera = function ( fov, aspect, near, far ) { THREE.Camera.call( this ); this.fov = fov !== undefined ? fov : 50; this.aspect = aspect !== undefined ? aspect : 1; this.near = near !== undefined ? near : 0.1; this.far = far !== undefined ? far : 2000; this.updateProjectionMatrix(); }; THREE.PerspectiveCamera.prototype = Object.create( THREE.Camera.prototype ); /** * Uses Focal Length (in mm) to estimate and set FOV * 35mm (fullframe) camera is used if frame size is not specified; * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html */ THREE.PerspectiveCamera.prototype.setLens = function ( focalLength, frameHeight ) { if ( frameHeight === undefined ) frameHeight = 24; this.fov = 2 * THREE.Math.radToDeg( Math.atan( frameHeight / ( focalLength * 2 ) ) ); this.updateProjectionMatrix(); } /** * Sets an offset in a larger frustum. This is useful for multi-window or * multi-monitor/multi-machine setups. * * For example, if you have 3x2 monitors and each monitor is 1920x1080 and * the monitors are in grid like this * * +---+---+---+ * | A | B | C | * +---+---+---+ * | D | E | F | * +---+---+---+ * * then for each monitor you would call it like this * * var w = 1920; * var h = 1080; * var fullWidth = w * 3; * var fullHeight = h * 2; * * --A-- * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h ); * --B-- * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h ); * --C-- * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h ); * --D-- * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h ); * --E-- * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h ); * --F-- * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h ); * * Note there is no reason monitors have to be the same size or in a grid. */ THREE.PerspectiveCamera.prototype.setViewOffset = function ( fullWidth, fullHeight, x, y, width, height ) { this.fullWidth = fullWidth; this.fullHeight = fullHeight; this.x = x; this.y = y; this.width = width; this.height = height; this.updateProjectionMatrix(); }; THREE.PerspectiveCamera.prototype.updateProjectionMatrix = function () { if ( this.fullWidth ) { var aspect = this.fullWidth / this.fullHeight; var top = Math.tan( THREE.Math.degToRad( this.fov * 0.5 ) ) * this.near; var bottom = -top; var left = aspect * bottom; var right = aspect * top; var width = Math.abs( right - left ); var height = Math.abs( top - bottom ); this.projectionMatrix.makeFrustum( left + this.x * width / this.fullWidth, left + ( this.x + this.width ) * width / this.fullWidth, top - ( this.y + this.height ) * height / this.fullHeight, top - this.y * height / this.fullHeight, this.near, this.far ); } else { this.projectionMatrix.makePerspective( this.fov, this.aspect, this.near, this.far ); } }; THREE.PerspectiveCamera.prototype.clone = function () { var camera = new THREE.PerspectiveCamera(); THREE.Camera.prototype.clone.call( this, camera ); camera.fov = this.fov; camera.aspect = this.aspect; camera.near = this.near; camera.far = this.far; return camera; }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ THREE.Light = function ( color ) { THREE.Object3D.call( this ); this.color = new THREE.Color( color ); }; THREE.Light.prototype = Object.create( THREE.Object3D.prototype ); THREE.Light.prototype.clone = function ( light ) { if ( light === undefined ) light = new THREE.Light(); THREE.Object3D.prototype.clone.call( this, light ); light.color.copy( this.color ); return light; }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.AmbientLight = function ( color ) { THREE.Light.call( this, color ); }; THREE.AmbientLight.prototype = Object.create( THREE.Light.prototype ); THREE.AmbientLight.prototype.clone = function () { var light = new THREE.AmbientLight(); THREE.Light.prototype.clone.call( this, light ); return light; }; /** * @author MPanknin / http://www.redplant.de/ * @author alteredq / http://alteredqualia.com/ */ THREE.AreaLight = function ( color, intensity ) { THREE.Light.call( this, color ); this.normal = new THREE.Vector3( 0, -1, 0 ); this.right = new THREE.Vector3( 1, 0, 0 ); this.intensity = ( intensity !== undefined ) ? intensity : 1; this.width = 1.0; this.height = 1.0; this.constantAttenuation = 1.5; this.linearAttenuation = 0.5; this.quadraticAttenuation = 0.1; }; THREE.AreaLight.prototype = Object.create( THREE.Light.prototype ); /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ THREE.DirectionalLight = function ( color, intensity ) { THREE.Light.call( this, color ); this.position.set( 0, 1, 0 ); this.target = new THREE.Object3D(); this.intensity = ( intensity !== undefined ) ? intensity : 1; this.castShadow = false; this.onlyShadow = false; // this.shadowCameraNear = 50; this.shadowCameraFar = 5000; this.shadowCameraLeft = -500; this.shadowCameraRight = 500; this.shadowCameraTop = 500; this.shadowCameraBottom = -500; this.shadowCameraVisible = false; this.shadowBias = 0; this.shadowDarkness = 0.5; this.shadowMapWidth = 512; this.shadowMapHeight = 512; // this.shadowCascade = false; this.shadowCascadeOffset = new THREE.Vector3( 0, 0, -1000 ); this.shadowCascadeCount = 2; this.shadowCascadeBias = [ 0, 0, 0 ]; this.shadowCascadeWidth = [ 512, 512, 512 ]; this.shadowCascadeHeight = [ 512, 512, 512 ]; this.shadowCascadeNearZ = [ -1.000, 0.990, 0.998 ]; this.shadowCascadeFarZ = [ 0.990, 0.998, 1.000 ]; this.shadowCascadeArray = []; // this.shadowMap = null; this.shadowMapSize = null; this.shadowCamera = null; this.shadowMatrix = null; }; THREE.DirectionalLight.prototype = Object.create( THREE.Light.prototype ); THREE.DirectionalLight.prototype.clone = function () { var light = new THREE.DirectionalLight(); THREE.Light.prototype.clone.call( this, light ); light.target = this.target.clone(); light.intensity = this.intensity; light.castShadow = this.castShadow; light.onlyShadow = this.onlyShadow; return light; }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.HemisphereLight = function ( skyColor, groundColor, intensity ) { THREE.Light.call( this, skyColor ); this.position.set( 0, 100, 0 ); this.groundColor = new THREE.Color( groundColor ); this.intensity = ( intensity !== undefined ) ? intensity : 1; }; THREE.HemisphereLight.prototype = Object.create( THREE.Light.prototype ); THREE.HemisphereLight.prototype.clone = function () { var light = new THREE.HemisphereLight(); THREE.Light.prototype.clone.call( this, light ); light.groundColor.copy( this.groundColor ); light.intensity = this.intensity; return light; }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.PointLight = function ( color, intensity, distance ) { THREE.Light.call( this, color ); this.intensity = ( intensity !== undefined ) ? intensity : 1; this.distance = ( distance !== undefined ) ? distance : 0; }; THREE.PointLight.prototype = Object.create( THREE.Light.prototype ); THREE.PointLight.prototype.clone = function () { var light = new THREE.PointLight(); THREE.Light.prototype.clone.call( this, light ); light.intensity = this.intensity; light.distance = this.distance; return light; }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.SpotLight = function ( color, intensity, distance, angle, exponent ) { THREE.Light.call( this, color ); this.position.set( 0, 1, 0 ); this.target = new THREE.Object3D(); this.intensity = ( intensity !== undefined ) ? intensity : 1; this.distance = ( distance !== undefined ) ? distance : 0; this.angle = ( angle !== undefined ) ? angle : Math.PI / 3; this.exponent = ( exponent !== undefined ) ? exponent : 10; this.castShadow = false; this.onlyShadow = false; // this.shadowCameraNear = 50; this.shadowCameraFar = 5000; this.shadowCameraFov = 50; this.shadowCameraVisible = false; this.shadowBias = 0; this.shadowDarkness = 0.5; this.shadowMapWidth = 512; this.shadowMapHeight = 512; // this.shadowMap = null; this.shadowMapSize = null; this.shadowCamera = null; this.shadowMatrix = null; }; THREE.SpotLight.prototype = Object.create( THREE.Light.prototype ); THREE.SpotLight.prototype.clone = function () { var light = new THREE.SpotLight(); THREE.Light.prototype.clone.call( this, light ); light.target = this.target.clone(); light.intensity = this.intensity; light.distance = this.distance; light.angle = this.angle; light.exponent = this.exponent; light.castShadow = this.castShadow; light.onlyShadow = this.onlyShadow; return light; }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.Loader = function ( showStatus ) { this.showStatus = showStatus; this.statusDomElement = showStatus ? THREE.Loader.prototype.addStatusElement() : null; this.onLoadStart = function () {}; this.onLoadProgress = function () {}; this.onLoadComplete = function () {}; }; THREE.Loader.prototype = { constructor: THREE.Loader, crossOrigin: undefined, addStatusElement: function () { var e = document.createElement( "div" ); e.style.position = "absolute"; e.style.right = "0px"; e.style.top = "0px"; e.style.fontSize = "0.8em"; e.style.textAlign = "left"; e.style.background = "rgba(0,0,0,0.25)"; e.style.color = "#fff"; e.style.width = "120px"; e.style.padding = "0.5em 0.5em 0.5em 0.5em"; e.style.zIndex = 1000; e.innerHTML = "Loading ..."; return e; }, updateProgress: function ( progress ) { var message = "Loaded "; if ( progress.total ) { message += ( 100 * progress.loaded / progress.total ).toFixed(0) + "%"; } else { message += ( progress.loaded / 1000 ).toFixed(2) + " KB"; } this.statusDomElement.innerHTML = message; }, extractUrlBase: function ( url ) { var parts = url.split( '/' ); if ( parts.length === 1 ) return './'; parts.pop(); return parts.join( '/' ) + '/'; }, initMaterials: function ( materials, texturePath ) { var array = []; for ( var i = 0; i < materials.length; ++ i ) { array[ i ] = THREE.Loader.prototype.createMaterial( materials[ i ], texturePath ); } return array; }, needsTangents: function ( materials ) { for( var i = 0, il = materials.length; i < il; i ++ ) { var m = materials[ i ]; if ( m instanceof THREE.ShaderMaterial ) return true; } return false; }, createMaterial: function ( m, texturePath ) { var _this = this; function is_pow2( n ) { var l = Math.log( n ) / Math.LN2; return Math.floor( l ) == l; } function nearest_pow2( n ) { var l = Math.log( n ) / Math.LN2; return Math.pow( 2, Math.round( l ) ); } function load_image( where, url ) { var image = new Image(); image.onload = function () { if ( !is_pow2( this.width ) || !is_pow2( this.height ) ) { var width = nearest_pow2( this.width ); var height = nearest_pow2( this.height ); where.image.width = width; where.image.height = height; where.image.getContext( '2d' ).drawImage( this, 0, 0, width, height ); } else { where.image = this; } where.needsUpdate = true; }; if ( _this.crossOrigin !== undefined ) image.crossOrigin = _this.crossOrigin; image.src = url; } function create_texture( where, name, sourceFile, repeat, offset, wrap, anisotropy ) { var isCompressed = /\.dds$/i.test( sourceFile ); var fullPath = texturePath + sourceFile; if ( isCompressed ) { var texture = THREE.ImageUtils.loadCompressedTexture( fullPath ); where[ name ] = texture; } else { var texture = document.createElement( 'canvas' ); where[ name ] = new THREE.Texture( texture ); } where[ name ].sourceFile = sourceFile; if( repeat ) { where[ name ].repeat.set( repeat[ 0 ], repeat[ 1 ] ); if ( repeat[ 0 ] !== 1 ) where[ name ].wrapS = THREE.RepeatWrapping; if ( repeat[ 1 ] !== 1 ) where[ name ].wrapT = THREE.RepeatWrapping; } if ( offset ) { where[ name ].offset.set( offset[ 0 ], offset[ 1 ] ); } if ( wrap ) { var wrapMap = { "repeat": THREE.RepeatWrapping, "mirror": THREE.MirroredRepeatWrapping } if ( wrapMap[ wrap[ 0 ] ] !== undefined ) where[ name ].wrapS = wrapMap[ wrap[ 0 ] ]; if ( wrapMap[ wrap[ 1 ] ] !== undefined ) where[ name ].wrapT = wrapMap[ wrap[ 1 ] ]; } if ( anisotropy ) { where[ name ].anisotropy = anisotropy; } if ( ! isCompressed ) { load_image( where[ name ], fullPath ); } } function rgb2hex( rgb ) { return ( rgb[ 0 ] * 255 << 16 ) + ( rgb[ 1 ] * 255 << 8 ) + rgb[ 2 ] * 255; } // defaults var mtype = "MeshLambertMaterial"; var mpars = { color: 0xeeeeee, opacity: 1.0, map: null, lightMap: null, normalMap: null, bumpMap: null, wireframe: false }; // parameters from model file if ( m.shading ) { var shading = m.shading.toLowerCase(); if ( shading === "phong" ) mtype = "MeshPhongMaterial"; else if ( shading === "basic" ) mtype = "MeshBasicMaterial"; } if ( m.blending !== undefined && THREE[ m.blending ] !== undefined ) { mpars.blending = THREE[ m.blending ]; } if ( m.transparent !== undefined || m.opacity < 1.0 ) { mpars.transparent = m.transparent; } if ( m.depthTest !== undefined ) { mpars.depthTest = m.depthTest; } if ( m.depthWrite !== undefined ) { mpars.depthWrite = m.depthWrite; } if ( m.visible !== undefined ) { mpars.visible = m.visible; } if ( m.flipSided !== undefined ) { mpars.side = THREE.BackSide; } if ( m.doubleSided !== undefined ) { mpars.side = THREE.DoubleSide; } if ( m.wireframe !== undefined ) { mpars.wireframe = m.wireframe; } if ( m.vertexColors !== undefined ) { if ( m.vertexColors === "face" ) { mpars.vertexColors = THREE.FaceColors; } else if ( m.vertexColors ) { mpars.vertexColors = THREE.VertexColors; } } // colors if ( m.colorDiffuse ) { mpars.color = rgb2hex( m.colorDiffuse ); } else if ( m.DbgColor ) { mpars.color = m.DbgColor; } if ( m.colorSpecular ) { mpars.specular = rgb2hex( m.colorSpecular ); } if ( m.colorAmbient ) { mpars.ambient = rgb2hex( m.colorAmbient ); } // modifiers if ( m.transparency ) { mpars.opacity = m.transparency; } if ( m.specularCoef ) { mpars.shininess = m.specularCoef; } // textures if ( m.mapDiffuse && texturePath ) { create_texture( mpars, "map", m.mapDiffuse, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap, m.mapDiffuseAnisotropy ); } if ( m.mapLight && texturePath ) { create_texture( mpars, "lightMap", m.mapLight, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap, m.mapLightAnisotropy ); } if ( m.mapBump && texturePath ) { create_texture( mpars, "bumpMap", m.mapBump, m.mapBumpRepeat, m.mapBumpOffset, m.mapBumpWrap, m.mapBumpAnisotropy ); } if ( m.mapNormal && texturePath ) { create_texture( mpars, "normalMap", m.mapNormal, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap, m.mapNormalAnisotropy ); } if ( m.mapSpecular && texturePath ) { create_texture( mpars, "specularMap", m.mapSpecular, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap, m.mapSpecularAnisotropy ); } // if ( m.mapBumpScale ) { mpars.bumpScale = m.mapBumpScale; } // special case for normal mapped material if ( m.mapNormal ) { var shader = THREE.ShaderLib[ "normalmap" ]; var uniforms = THREE.UniformsUtils.clone( shader.uniforms ); uniforms[ "tNormal" ].value = mpars.normalMap; if ( m.mapNormalFactor ) { uniforms[ "uNormalScale" ].value.set( m.mapNormalFactor, m.mapNormalFactor ); } if ( mpars.map ) { uniforms[ "tDiffuse" ].value = mpars.map; uniforms[ "enableDiffuse" ].value = true; } if ( mpars.specularMap ) { uniforms[ "tSpecular" ].value = mpars.specularMap; uniforms[ "enableSpecular" ].value = true; } if ( mpars.lightMap ) { uniforms[ "tAO" ].value = mpars.lightMap; uniforms[ "enableAO" ].value = true; } // for the moment don't handle displacement texture uniforms[ "diffuse" ].value.setHex( mpars.color ); uniforms[ "specular" ].value.setHex( mpars.specular ); uniforms[ "ambient" ].value.setHex( mpars.ambient ); uniforms[ "shininess" ].value = mpars.shininess; if ( mpars.opacity !== undefined ) { uniforms[ "opacity" ].value = mpars.opacity; } var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: true }; var material = new THREE.ShaderMaterial( parameters ); if ( mpars.transparent ) { material.transparent = true; } } else { var material = new THREE[ mtype ]( mpars ); } if ( m.DbgName !== undefined ) material.name = m.DbgName; return material; } }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.XHRLoader = function ( manager ) { this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager; }; THREE.XHRLoader.prototype = { constructor: THREE.XHRLoader, load: function ( url, onLoad, onProgress, onError ) { var scope = this; var request = new XMLHttpRequest(); if ( onLoad !== undefined ) { request.addEventListener( 'load', function ( event ) { onLoad( event.target.responseText ); scope.manager.itemEnd( url ); }, false ); } if ( onProgress !== undefined ) { request.addEventListener( 'progress', function ( event ) { onProgress( event ); }, false ); } if ( onError !== undefined ) { request.addEventListener( 'error', function ( event ) { onError( event ); }, false ); } if ( this.crossOrigin !== undefined ) request.crossOrigin = this.crossOrigin; request.open( 'GET', url, true ); request.send( null ); scope.manager.itemStart( url ); }, setCrossOrigin: function ( value ) { this.crossOrigin = value; } }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.ImageLoader = function ( manager ) { this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager; }; THREE.ImageLoader.prototype = { constructor: THREE.ImageLoader, load: function ( url, onLoad, onProgress, onError ) { var scope = this; var image = document.createElement( 'img' ); if ( onLoad !== undefined ) { image.addEventListener( 'load', function ( event ) { scope.manager.itemEnd( url ); onLoad( this ); }, false ); } if ( onProgress !== undefined ) { image.addEventListener( 'progress', function ( event ) { onProgress( event ); }, false ); } if ( onError !== undefined ) { image.addEventListener( 'error', function ( event ) { onError( event ); }, false ); } if ( this.crossOrigin !== undefined ) image.crossOrigin = this.crossOrigin; image.src = url; scope.manager.itemStart( url ); return image; }, setCrossOrigin: function ( value ) { this.crossOrigin = value; } } /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ THREE.JSONLoader = function ( showStatus ) { THREE.Loader.call( this, showStatus ); this.withCredentials = false; }; THREE.JSONLoader.prototype = Object.create( THREE.Loader.prototype ); THREE.JSONLoader.prototype.load = function ( url, callback, texturePath ) { var scope = this; // todo: unify load API to for easier SceneLoader use texturePath = texturePath && ( typeof texturePath === "string" ) ? texturePath : this.extractUrlBase( url ); this.onLoadStart(); this.loadAjaxJSON( this, url, callback, texturePath ); }; THREE.JSONLoader.prototype.loadAjaxJSON = function ( context, url, callback, texturePath, callbackProgress ) { var xhr = new XMLHttpRequest(); var length = 0; xhr.onreadystatechange = function () { if ( xhr.readyState === xhr.DONE ) { if ( xhr.status === 200 || xhr.status === 0 ) { if ( xhr.responseText ) { var json = JSON.parse( xhr.responseText ); if ( json.metadata.type === 'scene' ) { console.error( 'THREE.JSONLoader: "' + url + '" seems to be a Scene. Use THREE.SceneLoader instead.' ); return; } var result = context.parse( json, texturePath ); callback( result.geometry, result.materials ); } else { console.error( 'THREE.JSONLoader: "' + url + '" seems to be unreachable or the file is empty.' ); } // in context of more complex asset initialization // do not block on single failed file // maybe should go even one more level up context.onLoadComplete(); } else { console.error( 'THREE.JSONLoader: Couldn\'t load "' + url + '" (' + xhr.status + ')' ); } } else if ( xhr.readyState === xhr.LOADING ) { if ( callbackProgress ) { if ( length === 0 ) { length = xhr.getResponseHeader( 'Content-Length' ); } callbackProgress( { total: length, loaded: xhr.responseText.length } ); } } else if ( xhr.readyState === xhr.HEADERS_RECEIVED ) { if ( callbackProgress !== undefined ) { length = xhr.getResponseHeader( "Content-Length" ); } } }; xhr.open( "GET", url, true ); xhr.withCredentials = this.withCredentials; xhr.send( null ); }; THREE.JSONLoader.prototype.parse = function ( json, texturePath ) { var scope = this, geometry = new THREE.Geometry(), scale = ( json.scale !== undefined ) ? 1.0 / json.scale : 1.0; parseModel( scale ); parseSkin(); parseMorphing( scale ); geometry.computeCentroids(); geometry.computeFaceNormals(); geometry.computeBoundingSphere(); function parseModel( scale ) { function isBitSet( value, position ) { return value & ( 1 << position ); } var i, j, fi, offset, zLength, colorIndex, normalIndex, uvIndex, materialIndex, type, isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor, vertex, face, faceA, faceB, color, hex, normal, uvLayer, uv, u, v, faces = json.faces, vertices = json.vertices, normals = json.normals, colors = json.colors, nUvLayers = 0; if ( json.uvs !== undefined ) { // disregard empty arrays for ( i = 0; i < json.uvs.length; i++ ) { if ( json.uvs[ i ].length ) nUvLayers ++; } for ( i = 0; i < nUvLayers; i++ ) { geometry.faceVertexUvs[ i ] = []; } } offset = 0; zLength = vertices.length; while ( offset < zLength ) { vertex = new THREE.Vector3(); vertex.x = vertices[ offset ++ ] * scale; vertex.y = vertices[ offset ++ ] * scale; vertex.z = vertices[ offset ++ ] * scale; geometry.vertices.push( vertex ); } offset = 0; zLength = faces.length; while ( offset < zLength ) { type = faces[ offset ++ ]; isQuad = isBitSet( type, 0 ); hasMaterial = isBitSet( type, 1 ); hasFaceVertexUv = isBitSet( type, 3 ); hasFaceNormal = isBitSet( type, 4 ); hasFaceVertexNormal = isBitSet( type, 5 ); hasFaceColor = isBitSet( type, 6 ); hasFaceVertexColor = isBitSet( type, 7 ); // console.log("type", type, "bits", isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor); if ( isQuad ) { faceA = new THREE.Face3(); faceA.a = faces[ offset ]; faceA.b = faces[ offset + 1 ]; faceA.c = faces[ offset + 3 ]; faceB = new THREE.Face3(); faceB.a = faces[ offset + 1 ]; faceB.b = faces[ offset + 2 ]; faceB.c = faces[ offset + 3 ]; offset += 4; if ( hasMaterial ) { materialIndex = faces[ offset ++ ]; faceA.materialIndex = materialIndex; faceB.materialIndex = materialIndex; } // to get face <=> uv index correspondence fi = geometry.faces.length; if ( hasFaceVertexUv ) { for ( i = 0; i < nUvLayers; i++ ) { uvLayer = json.uvs[ i ]; geometry.faceVertexUvs[ i ][ fi ] = []; geometry.faceVertexUvs[ i ][ fi + 1 ] = [] for ( j = 0; j < 4; j ++ ) { uvIndex = faces[ offset ++ ]; u = uvLayer[ uvIndex * 2 ]; v = uvLayer[ uvIndex * 2 + 1 ]; uv = new THREE.Vector2( u, v ); if ( j !== 2 ) geometry.faceVertexUvs[ i ][ fi ].push( uv ); if ( j !== 0 ) geometry.faceVertexUvs[ i ][ fi + 1 ].push( uv ); } } } if ( hasFaceNormal ) { normalIndex = faces[ offset ++ ] * 3; faceA.normal.set( normals[ normalIndex ++ ], normals[ normalIndex ++ ], normals[ normalIndex ] ); faceB.normal.copy( faceA.normal ); } if ( hasFaceVertexNormal ) { for ( i = 0; i < 4; i++ ) { normalIndex = faces[ offset ++ ] * 3; normal = new THREE.Vector3( normals[ normalIndex ++ ], normals[ normalIndex ++ ], normals[ normalIndex ] ); if ( i !== 2 ) faceA.vertexNormals.push( normal ); if ( i !== 0 ) faceB.vertexNormals.push( normal ); } } if ( hasFaceColor ) { colorIndex = faces[ offset ++ ]; hex = colors[ colorIndex ]; faceA.color.setHex( hex ); faceB.color.setHex( hex ); } if ( hasFaceVertexColor ) { for ( i = 0; i < 4; i++ ) { colorIndex = faces[ offset ++ ]; hex = colors[ colorIndex ]; if ( i !== 2 ) faceA.vertexColors.push( new THREE.Color( hex ) ); if ( i !== 0 ) faceB.vertexColors.push( new THREE.Color( hex ) ); } } geometry.faces.push( faceA ); geometry.faces.push( faceB ); } else { face = new THREE.Face3(); face.a = faces[ offset ++ ]; face.b = faces[ offset ++ ]; face.c = faces[ offset ++ ]; if ( hasMaterial ) { materialIndex = faces[ offset ++ ]; face.materialIndex = materialIndex; } // to get face <=> uv index correspondence fi = geometry.faces.length; if ( hasFaceVertexUv ) { for ( i = 0; i < nUvLayers; i++ ) { uvLayer = json.uvs[ i ]; geometry.faceVertexUvs[ i ][ fi ] = []; for ( j = 0; j < 3; j ++ ) { uvIndex = faces[ offset ++ ]; u = uvLayer[ uvIndex * 2 ]; v = uvLayer[ uvIndex * 2 + 1 ]; uv = new THREE.Vector2( u, v ); geometry.faceVertexUvs[ i ][ fi ].push( uv ); } } } if ( hasFaceNormal ) { normalIndex = faces[ offset ++ ] * 3; face.normal.set( normals[ normalIndex ++ ], normals[ normalIndex ++ ], normals[ normalIndex ] ); } if ( hasFaceVertexNormal ) { for ( i = 0; i < 3; i++ ) { normalIndex = faces[ offset ++ ] * 3; normal = new THREE.Vector3( normals[ normalIndex ++ ], normals[ normalIndex ++ ], normals[ normalIndex ] ); face.vertexNormals.push( normal ); } } if ( hasFaceColor ) { colorIndex = faces[ offset ++ ]; face.color.setHex( colors[ colorIndex ] ); } if ( hasFaceVertexColor ) { for ( i = 0; i < 3; i++ ) { colorIndex = faces[ offset ++ ]; face.vertexColors.push( new THREE.Color( colors[ colorIndex ] ) ); } } geometry.faces.push( face ); } } }; function parseSkin() { if ( json.skinWeights ) { for ( var i = 0, l = json.skinWeights.length; i < l; i += 2 ) { var x = json.skinWeights[ i ]; var y = json.skinWeights[ i + 1 ]; var z = 0; var w = 0; geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) ); } } if ( json.skinIndices ) { for ( var i = 0, l = json.skinIndices.length; i < l; i += 2 ) { var a = json.skinIndices[ i ]; var b = json.skinIndices[ i + 1 ]; var c = 0; var d = 0; geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) ); } } geometry.bones = json.bones; if ( geometry.bones && geometry.bones.length > 0 && ( geometry.skinWeights.length !== geometry.skinIndices.length || geometry.skinIndices.length !== geometry.vertices.length ) ) { console.warn( 'When skinning, number of vertices (' + geometry.vertices.length + '), skinIndices (' + geometry.skinIndices.length + '), and skinWeights (' + geometry.skinWeights.length + ') should match.' ); } // could change this to json.animations[0] or remove completely geometry.animation = json.animation; geometry.animations = json.animations; }; function parseMorphing( scale ) { if ( json.morphTargets !== undefined ) { var i, l, v, vl, dstVertices, srcVertices; for ( i = 0, l = json.morphTargets.length; i < l; i ++ ) { geometry.morphTargets[ i ] = {}; geometry.morphTargets[ i ].name = json.morphTargets[ i ].name; geometry.morphTargets[ i ].vertices = []; dstVertices = geometry.morphTargets[ i ].vertices; srcVertices = json.morphTargets [ i ].vertices; for( v = 0, vl = srcVertices.length; v < vl; v += 3 ) { var vertex = new THREE.Vector3(); vertex.x = srcVertices[ v ] * scale; vertex.y = srcVertices[ v + 1 ] * scale; vertex.z = srcVertices[ v + 2 ] * scale; dstVertices.push( vertex ); } } } if ( json.morphColors !== undefined ) { var i, l, c, cl, dstColors, srcColors, color; for ( i = 0, l = json.morphColors.length; i < l; i++ ) { geometry.morphColors[ i ] = {}; geometry.morphColors[ i ].name = json.morphColors[ i ].name; geometry.morphColors[ i ].colors = []; dstColors = geometry.morphColors[ i ].colors; srcColors = json.morphColors [ i ].colors; for ( c = 0, cl = srcColors.length; c < cl; c += 3 ) { color = new THREE.Color( 0xffaa00 ); color.setRGB( srcColors[ c ], srcColors[ c + 1 ], srcColors[ c + 2 ] ); dstColors.push( color ); } } } }; if ( json.materials === undefined ) { return { geometry: geometry }; } else { var materials = this.initMaterials( json.materials, texturePath ); if ( this.needsTangents( materials ) ) { geometry.computeTangents(); } return { geometry: geometry, materials: materials }; } }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.LoadingManager = function ( onLoad, onProgress, onError ) { var scope = this; var loaded = 0, total = 0; this.onLoad = onLoad; this.onProgress = onProgress; this.onError = onError; this.itemStart = function ( url ) { total ++; }; this.itemEnd = function ( url ) { loaded ++; if ( scope.onProgress !== undefined ) { scope.onProgress( url, loaded, total ); } if ( loaded === total && scope.onLoad !== undefined ) { scope.onLoad(); } }; }; THREE.DefaultLoadingManager = new THREE.LoadingManager(); /** * @author mrdoob / http://mrdoob.com/ */ THREE.BufferGeometryLoader = function ( manager ) { this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager; }; THREE.BufferGeometryLoader.prototype = { constructor: THREE.BufferGeometryLoader, load: function ( url, onLoad, onProgress, onError ) { var scope = this; var loader = new THREE.XHRLoader(); loader.setCrossOrigin( this.crossOrigin ); loader.load( url, function ( text ) { onLoad( scope.parse( JSON.parse( text ) ) ); } ); }, setCrossOrigin: function ( value ) { this.crossOrigin = value; }, parse: function ( json ) { var geometry = new THREE.BufferGeometry(); var attributes = json.attributes; var offsets = json.offsets; var boundingSphere = json.boundingSphere; for ( var key in attributes ) { var attribute = attributes[ key ]; geometry.attributes[ key ] = { itemSize: attribute.itemSize, array: new self[ attribute.type ]( attribute.array ) } } if ( offsets !== undefined ) { geometry.offsets = JSON.parse( JSON.stringify( offsets ) ); } if ( boundingSphere !== undefined ) { geometry.boundingSphere = new THREE.Sphere( new THREE.Vector3().fromArray( boundingSphere.center !== undefined ? boundingSphere.center : [ 0, 0, 0 ] ), boundingSphere.radius ); } return geometry; } }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.GeometryLoader = function ( manager ) { this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager; }; THREE.GeometryLoader.prototype = { constructor: THREE.GeometryLoader, load: function ( url, onLoad, onProgress, onError ) { var scope = this; var loader = new THREE.XHRLoader(); loader.setCrossOrigin( this.crossOrigin ); loader.load( url, function ( text ) { onLoad( scope.parse( JSON.parse( text ) ) ); } ); }, setCrossOrigin: function ( value ) { this.crossOrigin = value; }, parse: function ( json ) { } }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.MaterialLoader = function ( manager ) { this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager; }; THREE.MaterialLoader.prototype = { constructor: THREE.MaterialLoader, load: function ( url, onLoad, onProgress, onError ) { var scope = this; var loader = new THREE.XHRLoader(); loader.setCrossOrigin( this.crossOrigin ); loader.load( url, function ( text ) { onLoad( scope.parse( JSON.parse( text ) ) ); } ); }, setCrossOrigin: function ( value ) { this.crossOrigin = value; }, parse: function ( json ) { var material = new THREE[ json.type ]; if ( json.color !== undefined ) material.color.setHex( json.color ); if ( json.ambient !== undefined ) material.ambient.setHex( json.ambient ); if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive ); if ( json.specular !== undefined ) material.specular.setHex( json.specular ); if ( json.shininess !== undefined ) material.shininess = json.shininess; if ( json.vertexColors !== undefined ) material.vertexColors = json.vertexColors; if ( json.blending !== undefined ) material.blending = json.blending; if ( json.side !== undefined ) material.side = json.side; if ( json.opacity !== undefined ) material.opacity = json.opacity; if ( json.transparent !== undefined ) material.transparent = json.transparent; if ( json.wireframe !== undefined ) material.wireframe = json.wireframe; if ( json.materials !== undefined ) { for ( var i = 0, l = json.materials.length; i < l; i ++ ) { material.materials.push( this.parse( json.materials[ i ] ) ); } } return material; } }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.ObjectLoader = function ( manager ) { this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager; }; THREE.ObjectLoader.prototype = { constructor: THREE.ObjectLoader, load: function ( url, onLoad, onProgress, onError ) { var scope = this; var loader = new THREE.XHRLoader( scope.manager ); loader.setCrossOrigin( this.crossOrigin ); loader.load( url, function ( text ) { onLoad( scope.parse( JSON.parse( text ) ) ); } ); }, setCrossOrigin: function ( value ) { this.crossOrigin = value; }, parse: function ( json ) { var geometries = this.parseGeometries( json.geometries ); var materials = this.parseMaterials( json.materials ); var object = this.parseObject( json.object, geometries, materials ); return object; }, parseGeometries: function ( json ) { var geometries = {}; if ( json !== undefined ) { var geometryLoader = new THREE.JSONLoader(); var bufferGeometryLoader = new THREE.BufferGeometryLoader(); for ( var i = 0, l = json.length; i < l; i ++ ) { var geometry; var data = json[ i ]; switch ( data.type ) { case 'PlaneGeometry': geometry = new THREE.PlaneGeometry( data.width, data.height, data.widthSegments, data.heightSegments ); break; case 'BoxGeometry': case 'CubeGeometry': // DEPRECATED geometry = new THREE.BoxGeometry( data.width, data.height, data.depth, data.widthSegments, data.heightSegments, data.depthSegments ); break; case 'CircleGeometry': geometry = new THREE.CircleGeometry( data.radius, data.segments ); break; case 'CylinderGeometry': geometry = new THREE.CylinderGeometry( data.radiusTop, data.radiusBottom, data.height, data.radialSegments, data.heightSegments, data.openEnded ); break; case 'SphereGeometry': geometry = new THREE.SphereGeometry( data.radius, data.widthSegments, data.heightSegments, data.phiStart, data.phiLength, data.thetaStart, data.thetaLength ); break; case 'IcosahedronGeometry': geometry = new THREE.IcosahedronGeometry( data.radius, data.detail ); break; case 'TorusGeometry': geometry = new THREE.TorusGeometry( data.radius, data.tube, data.radialSegments, data.tubularSegments, data.arc ); break; case 'TorusKnotGeometry': geometry = new THREE.TorusKnotGeometry( data.radius, data.tube, data.radialSegments, data.tubularSegments, data.p, data.q, data.heightScale ); break; case 'BufferGeometry': geometry = bufferGeometryLoader.parse( data.data ); break; case 'Geometry': geometry = geometryLoader.parse( data.data ).geometry; break; } geometry.uuid = data.uuid; if ( data.name !== undefined ) geometry.name = data.name; geometries[ data.uuid ] = geometry; } } return geometries; }, parseMaterials: function ( json ) { var materials = {}; if ( json !== undefined ) { var loader = new THREE.MaterialLoader(); for ( var i = 0, l = json.length; i < l; i ++ ) { var data = json[ i ]; var material = loader.parse( data ); material.uuid = data.uuid; if ( data.name !== undefined ) material.name = data.name; materials[ data.uuid ] = material; } } return materials; }, parseObject: function () { var matrix = new THREE.Matrix4(); return function ( data, geometries, materials ) { var object; switch ( data.type ) { case 'Scene': object = new THREE.Scene(); break; case 'PerspectiveCamera': object = new THREE.PerspectiveCamera( data.fov, data.aspect, data.near, data.far ); break; case 'OrthographicCamera': object = new THREE.OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far ); break; case 'AmbientLight': object = new THREE.AmbientLight( data.color ); break; case 'DirectionalLight': object = new THREE.DirectionalLight( data.color, data.intensity ); break; case 'PointLight': object = new THREE.PointLight( data.color, data.intensity, data.distance ); break; case 'SpotLight': object = new THREE.SpotLight( data.color, data.intensity, data.distance, data.angle, data.exponent ); break; case 'HemisphereLight': object = new THREE.HemisphereLight( data.color, data.groundColor, data.intensity ); break; case 'Mesh': var geometry = geometries[ data.geometry ]; var material = materials[ data.material ]; if ( geometry === undefined ) { console.error( 'THREE.ObjectLoader: Undefined geometry ' + data.geometry ); } if ( material === undefined ) { console.error( 'THREE.ObjectLoader: Undefined material ' + data.material ); } object = new THREE.Mesh( geometry, material ); break; case 'Sprite': var material = materials[ data.material ]; if ( material === undefined ) { console.error( 'THREE.ObjectLoader: Undefined material ' + data.material ); } object = new THREE.Sprite( material ); break; default: object = new THREE.Object3D(); } object.uuid = data.uuid; if ( data.name !== undefined ) object.name = data.name; if ( data.matrix !== undefined ) { matrix.fromArray( data.matrix ); matrix.decompose( object.position, object.quaternion, object.scale ); } else { if ( data.position !== undefined ) object.position.fromArray( data.position ); if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation ); if ( data.scale !== undefined ) object.scale.fromArray( data.scale ); } if ( data.visible !== undefined ) object.visible = data.visible; if ( data.userData !== undefined ) object.userData = data.userData; if ( data.children !== undefined ) { for ( var child in data.children ) { object.add( this.parseObject( data.children[ child ], geometries, materials ) ); } } return object; } }() }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.SceneLoader = function () { this.onLoadStart = function () {}; this.onLoadProgress = function() {}; this.onLoadComplete = function () {}; this.callbackSync = function () {}; this.callbackProgress = function () {}; this.geometryHandlers = {}; this.hierarchyHandlers = {}; this.addGeometryHandler( "ascii", THREE.JSONLoader ); }; THREE.SceneLoader.prototype = { constructor: THREE.SceneLoader, load: function ( url, onLoad, onProgress, onError ) { var scope = this; var loader = new THREE.XHRLoader( scope.manager ); loader.setCrossOrigin( this.crossOrigin ); loader.load( url, function ( text ) { scope.parse( JSON.parse( text ), onLoad, url ); } ); }, setCrossOrigin: function ( value ) { this.crossOrigin = value; }, addGeometryHandler: function ( typeID, loaderClass ) { this.geometryHandlers[ typeID ] = { "loaderClass": loaderClass }; }, addHierarchyHandler: function ( typeID, loaderClass ) { this.hierarchyHandlers[ typeID ] = { "loaderClass": loaderClass }; }, parse: function ( json, callbackFinished, url ) { var scope = this; var urlBase = THREE.Loader.prototype.extractUrlBase( url ); var geometry, material, camera, fog, texture, images, color, light, hex, intensity, counter_models, counter_textures, total_models, total_textures, result; var target_array = []; var data = json; // async geometry loaders for ( var typeID in this.geometryHandlers ) { var loaderClass = this.geometryHandlers[ typeID ][ "loaderClass" ]; this.geometryHandlers[ typeID ][ "loaderObject" ] = new loaderClass(); } // async hierachy loaders for ( var typeID in this.hierarchyHandlers ) { var loaderClass = this.hierarchyHandlers[ typeID ][ "loaderClass" ]; this.hierarchyHandlers[ typeID ][ "loaderObject" ] = new loaderClass(); } counter_models = 0; counter_textures = 0; result = { scene: new THREE.Scene(), geometries: {}, face_materials: {}, materials: {}, textures: {}, objects: {}, cameras: {}, lights: {}, fogs: {}, empties: {}, groups: {} }; if ( data.transform ) { var position = data.transform.position, rotation = data.transform.rotation, scale = data.transform.scale; if ( position ) { result.scene.position.fromArray( position ); } if ( rotation ) { result.scene.rotation.fromArray( rotation ); } if ( scale ) { result.scene.scale.fromArray( scale ); } if ( position || rotation || scale ) { result.scene.updateMatrix(); result.scene.updateMatrixWorld(); } } function get_url( source_url, url_type ) { if ( url_type == "relativeToHTML" ) { return source_url; } else { return urlBase + "/" + source_url; } }; // toplevel loader function, delegates to handle_children function handle_objects() { handle_children( result.scene, data.objects ); } // handle all the children from the loaded json and attach them to given parent function handle_children( parent, children ) { var mat, dst, pos, rot, scl, quat; for ( var objID in children ) { // check by id if child has already been handled, // if not, create new object var object = result.objects[ objID ]; var objJSON = children[ objID ]; if ( object === undefined ) { // meshes if ( objJSON.type && ( objJSON.type in scope.hierarchyHandlers ) ) { if ( objJSON.loading === undefined ) { var reservedTypes = { "type": 1, "url": 1, "material": 1, "position": 1, "rotation": 1, "scale" : 1, "visible": 1, "children": 1, "userData": 1, "skin": 1, "morph": 1, "mirroredLoop": 1, "duration": 1 }; var loaderParameters = {}; for ( var parType in objJSON ) { if ( ! ( parType in reservedTypes ) ) { loaderParameters[ parType ] = objJSON[ parType ]; } } material = result.materials[ objJSON.material ]; objJSON.loading = true; var loader = scope.hierarchyHandlers[ objJSON.type ][ "loaderObject" ]; // ColladaLoader if ( loader.options ) { loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ) ); // UTF8Loader // OBJLoader } else { loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ), loaderParameters ); } } } else if ( objJSON.geometry !== undefined ) { geometry = result.geometries[ objJSON.geometry ]; // geometry already loaded if ( geometry ) { var needsTangents = false; material = result.materials[ objJSON.material ]; needsTangents = material instanceof THREE.ShaderMaterial; pos = objJSON.position; rot = objJSON.rotation; scl = objJSON.scale; mat = objJSON.matrix; quat = objJSON.quaternion; // use materials from the model file // if there is no material specified in the object if ( ! objJSON.material ) { material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] ); } // use materials from the model file // if there is just empty face material // (must create new material as each model has its own face material) if ( ( material instanceof THREE.MeshFaceMaterial ) && material.materials.length === 0 ) { material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] ); } if ( material instanceof THREE.MeshFaceMaterial ) { for ( var i = 0; i < material.materials.length; i ++ ) { needsTangents = needsTangents || ( material.materials[ i ] instanceof THREE.ShaderMaterial ); } } if ( needsTangents ) { geometry.computeTangents(); } if ( objJSON.skin ) { object = new THREE.SkinnedMesh( geometry, material ); } else if ( objJSON.morph ) { object = new THREE.MorphAnimMesh( geometry, material ); if ( objJSON.duration !== undefined ) { object.duration = objJSON.duration; } if ( objJSON.time !== undefined ) { object.time = objJSON.time; } if ( objJSON.mirroredLoop !== undefined ) { object.mirroredLoop = objJSON.mirroredLoop; } if ( material.morphNormals ) { geometry.computeMorphNormals(); } } else { object = new THREE.Mesh( geometry, material ); } object.name = objID; if ( mat ) { object.matrixAutoUpdate = false; object.matrix.set( mat[0], mat[1], mat[2], mat[3], mat[4], mat[5], mat[6], mat[7], mat[8], mat[9], mat[10], mat[11], mat[12], mat[13], mat[14], mat[15] ); } else { object.position.fromArray( pos ); if ( quat ) { object.quaternion.fromArray( quat ); } else { object.rotation.fromArray( rot ); } object.scale.fromArray( scl ); } object.visible = objJSON.visible; object.castShadow = objJSON.castShadow; object.receiveShadow = objJSON.receiveShadow; parent.add( object ); result.objects[ objID ] = object; } // lights } else if ( objJSON.type === "AmbientLight" || objJSON.type === "PointLight" || objJSON.type === "DirectionalLight" || objJSON.type === "SpotLight" || objJSON.type === "HemisphereLight" || objJSON.type === "AreaLight" ) { var color = objJSON.color; var intensity = objJSON.intensity; var distance = objJSON.distance; var position = objJSON.position; var rotation = objJSON.rotation; switch ( objJSON.type ) { case 'AmbientLight': light = new THREE.AmbientLight( color ); break; case 'PointLight': light = new THREE.PointLight( color, intensity, distance ); light.position.fromArray( position ); break; case 'DirectionalLight': light = new THREE.DirectionalLight( color, intensity ); light.position.fromArray( objJSON.direction ); break; case 'SpotLight': light = new THREE.SpotLight( color, intensity, distance, 1 ); light.angle = objJSON.angle; light.position.fromArray( position ); light.target.set( position[ 0 ], position[ 1 ] - distance, position[ 2 ] ); light.target.applyEuler( new THREE.Euler( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ], 'XYZ' ) ); break; case 'HemisphereLight': light = new THREE.DirectionalLight( color, intensity, distance ); light.target.set( position[ 0 ], position[ 1 ] - distance, position[ 2 ] ); light.target.applyEuler( new THREE.Euler( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ], 'XYZ' ) ); break; case 'AreaLight': light = new THREE.AreaLight(color, intensity); light.position.fromArray( position ); light.width = objJSON.size; light.height = objJSON.size_y; break; } parent.add( light ); light.name = objID; result.lights[ objID ] = light; result.objects[ objID ] = light; // cameras } else if ( objJSON.type === "PerspectiveCamera" || objJSON.type === "OrthographicCamera" ) { pos = objJSON.position; rot = objJSON.rotation; quat = objJSON.quaternion; if ( objJSON.type === "PerspectiveCamera" ) { camera = new THREE.PerspectiveCamera( objJSON.fov, objJSON.aspect, objJSON.near, objJSON.far ); } else if ( objJSON.type === "OrthographicCamera" ) { camera = new THREE.OrthographicCamera( objJSON.left, objJSON.right, objJSON.top, objJSON.bottom, objJSON.near, objJSON.far ); } camera.name = objID; camera.position.fromArray( pos ); if ( quat !== undefined ) { camera.quaternion.fromArray( quat ); } else if ( rot !== undefined ) { camera.rotation.fromArray( rot ); } parent.add( camera ); result.cameras[ objID ] = camera; result.objects[ objID ] = camera; // pure Object3D } else { pos = objJSON.position; rot = objJSON.rotation; scl = objJSON.scale; quat = objJSON.quaternion; object = new THREE.Object3D(); object.name = objID; object.position.fromArray( pos ); if ( quat ) { object.quaternion.fromArray( quat ); } else { object.rotation.fromArray( rot ); } object.scale.fromArray( scl ); object.visible = ( objJSON.visible !== undefined ) ? objJSON.visible : false; parent.add( object ); result.objects[ objID ] = object; result.empties[ objID ] = object; } if ( object ) { if ( objJSON.userData !== undefined ) { for ( var key in objJSON.userData ) { var value = objJSON.userData[ key ]; object.userData[ key ] = value; } } if ( objJSON.groups !== undefined ) { for ( var i = 0; i < objJSON.groups.length; i ++ ) { var groupID = objJSON.groups[ i ]; if ( result.groups[ groupID ] === undefined ) { result.groups[ groupID ] = []; } result.groups[ groupID ].push( objID ); } } } } if ( object !== undefined && objJSON.children !== undefined ) { handle_children( object, objJSON.children ); } } }; function handle_mesh( geo, mat, id ) { result.geometries[ id ] = geo; result.face_materials[ id ] = mat; handle_objects(); }; function handle_hierarchy( node, id, parent, material, obj ) { var p = obj.position; var r = obj.rotation; var q = obj.quaternion; var s = obj.scale; node.position.fromArray( p ); if ( q ) { node.quaternion.fromArray( q ); } else { node.rotation.fromArray( r ); } node.scale.fromArray( s ); // override children materials // if object material was specified in JSON explicitly if ( material ) { node.traverse( function ( child ) { child.material = material; } ); } // override children visibility // with root node visibility as specified in JSON var visible = ( obj.visible !== undefined ) ? obj.visible : true; node.traverse( function ( child ) { child.visible = visible; } ); parent.add( node ); node.name = id; result.objects[ id ] = node; handle_objects(); }; function create_callback_geometry( id ) { return function ( geo, mat ) { geo.name = id; handle_mesh( geo, mat, id ); counter_models -= 1; scope.onLoadComplete(); async_callback_gate(); } }; function create_callback_hierachy( id, parent, material, obj ) { return function ( event ) { var result; // loaders which use EventDispatcher if ( event.content ) { result = event.content; // ColladaLoader } else if ( event.dae ) { result = event.scene; // UTF8Loader } else { result = event; } handle_hierarchy( result, id, parent, material, obj ); counter_models -= 1; scope.onLoadComplete(); async_callback_gate(); } }; function create_callback_embed( id ) { return function ( geo, mat ) { geo.name = id; result.geometries[ id ] = geo; result.face_materials[ id ] = mat; } }; function async_callback_gate() { var progress = { totalModels : total_models, totalTextures : total_textures, loadedModels : total_models - counter_models, loadedTextures : total_textures - counter_textures }; scope.callbackProgress( progress, result ); scope.onLoadProgress(); if ( counter_models === 0 && counter_textures === 0 ) { finalize(); callbackFinished( result ); } }; function finalize() { // take care of targets which could be asynchronously loaded objects for ( var i = 0; i < target_array.length; i ++ ) { var ta = target_array[ i ]; var target = result.objects[ ta.targetName ]; if ( target ) { ta.object.target = target; } else { // if there was error and target of specified name doesn't exist in the scene file // create instead dummy target // (target must be added to scene explicitly as parent is already added) ta.object.target = new THREE.Object3D(); result.scene.add( ta.object.target ); } ta.object.target.userData.targetInverse = ta.object; } }; var callbackTexture = function ( count ) { counter_textures -= count; async_callback_gate(); scope.onLoadComplete(); }; // must use this instead of just directly calling callbackTexture // because of closure in the calling context loop var generateTextureCallback = function ( count ) { return function () { callbackTexture( count ); }; }; function traverse_json_hierarchy( objJSON, callback ) { callback( objJSON ); if ( objJSON.children !== undefined ) { for ( var objChildID in objJSON.children ) { traverse_json_hierarchy( objJSON.children[ objChildID ], callback ); } } }; // first go synchronous elements // fogs var fogID, fogJSON; for ( fogID in data.fogs ) { fogJSON = data.fogs[ fogID ]; if ( fogJSON.type === "linear" ) { fog = new THREE.Fog( 0x000000, fogJSON.near, fogJSON.far ); } else if ( fogJSON.type === "exp2" ) { fog = new THREE.FogExp2( 0x000000, fogJSON.density ); } color = fogJSON.color; fog.color.setRGB( color[0], color[1], color[2] ); result.fogs[ fogID ] = fog; } // now come potentially asynchronous elements // geometries // count how many geometries will be loaded asynchronously var geoID, geoJSON; for ( geoID in data.geometries ) { geoJSON = data.geometries[ geoID ]; if ( geoJSON.type in this.geometryHandlers ) { counter_models += 1; scope.onLoadStart(); } } // count how many hierarchies will be loaded asynchronously for ( var objID in data.objects ) { traverse_json_hierarchy( data.objects[ objID ], function ( objJSON ) { if ( objJSON.type && ( objJSON.type in scope.hierarchyHandlers ) ) { counter_models += 1; scope.onLoadStart(); } }); } total_models = counter_models; for ( geoID in data.geometries ) { geoJSON = data.geometries[ geoID ]; if ( geoJSON.type === "cube" ) { geometry = new THREE.BoxGeometry( geoJSON.width, geoJSON.height, geoJSON.depth, geoJSON.widthSegments, geoJSON.heightSegments, geoJSON.depthSegments ); geometry.name = geoID; result.geometries[ geoID ] = geometry; } else if ( geoJSON.type === "plane" ) { geometry = new THREE.PlaneGeometry( geoJSON.width, geoJSON.height, geoJSON.widthSegments, geoJSON.heightSegments ); geometry.name = geoID; result.geometries[ geoID ] = geometry; } else if ( geoJSON.type === "sphere" ) { geometry = new THREE.SphereGeometry( geoJSON.radius, geoJSON.widthSegments, geoJSON.heightSegments ); geometry.name = geoID; result.geometries[ geoID ] = geometry; } else if ( geoJSON.type === "cylinder" ) { geometry = new THREE.CylinderGeometry( geoJSON.topRad, geoJSON.botRad, geoJSON.height, geoJSON.radSegs, geoJSON.heightSegs ); geometry.name = geoID; result.geometries[ geoID ] = geometry; } else if ( geoJSON.type === "torus" ) { geometry = new THREE.TorusGeometry( geoJSON.radius, geoJSON.tube, geoJSON.segmentsR, geoJSON.segmentsT ); geometry.name = geoID; result.geometries[ geoID ] = geometry; } else if ( geoJSON.type === "icosahedron" ) { geometry = new THREE.IcosahedronGeometry( geoJSON.radius, geoJSON.subdivisions ); geometry.name = geoID; result.geometries[ geoID ] = geometry; } else if ( geoJSON.type in this.geometryHandlers ) { var loaderParameters = {}; for ( var parType in geoJSON ) { if ( parType !== "type" && parType !== "url" ) { loaderParameters[ parType ] = geoJSON[ parType ]; } } var loader = this.geometryHandlers[ geoJSON.type ][ "loaderObject" ]; loader.load( get_url( geoJSON.url, data.urlBaseType ), create_callback_geometry( geoID ), loaderParameters ); } else if ( geoJSON.type === "embedded" ) { var modelJson = data.embeds[ geoJSON.id ], texture_path = ""; // pass metadata along to jsonLoader so it knows the format version modelJson.metadata = data.metadata; if ( modelJson ) { var jsonLoader = this.geometryHandlers[ "ascii" ][ "loaderObject" ]; var model = jsonLoader.parse( modelJson, texture_path ); create_callback_embed( geoID )( model.geometry, model.materials ); } } } // textures // count how many textures will be loaded asynchronously var textureID, textureJSON; for ( textureID in data.textures ) { textureJSON = data.textures[ textureID ]; if ( textureJSON.url instanceof Array ) { counter_textures += textureJSON.url.length; for( var n = 0; n < textureJSON.url.length; n ++ ) { scope.onLoadStart(); } } else { counter_textures += 1; scope.onLoadStart(); } } total_textures = counter_textures; for ( textureID in data.textures ) { textureJSON = data.textures[ textureID ]; if ( textureJSON.mapping !== undefined && THREE[ textureJSON.mapping ] !== undefined ) { textureJSON.mapping = new THREE[ textureJSON.mapping ](); } if ( textureJSON.url instanceof Array ) { var count = textureJSON.url.length; var url_array = []; for( var i = 0; i < count; i ++ ) { url_array[ i ] = get_url( textureJSON.url[ i ], data.urlBaseType ); } var isCompressed = /\.dds$/i.test( url_array[ 0 ] ); if ( isCompressed ) { texture = THREE.ImageUtils.loadCompressedTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) ); } else { texture = THREE.ImageUtils.loadTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) ); } } else { var isCompressed = /\.dds$/i.test( textureJSON.url ); var fullUrl = get_url( textureJSON.url, data.urlBaseType ); var textureCallback = generateTextureCallback( 1 ); if ( isCompressed ) { texture = THREE.ImageUtils.loadCompressedTexture( fullUrl, textureJSON.mapping, textureCallback ); } else { texture = THREE.ImageUtils.loadTexture( fullUrl, textureJSON.mapping, textureCallback ); } if ( THREE[ textureJSON.minFilter ] !== undefined ) texture.minFilter = THREE[ textureJSON.minFilter ]; if ( THREE[ textureJSON.magFilter ] !== undefined ) texture.magFilter = THREE[ textureJSON.magFilter ]; if ( textureJSON.anisotropy ) texture.anisotropy = textureJSON.anisotropy; if ( textureJSON.repeat ) { texture.repeat.set( textureJSON.repeat[ 0 ], textureJSON.repeat[ 1 ] ); if ( textureJSON.repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping; if ( textureJSON.repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping; } if ( textureJSON.offset ) { texture.offset.set( textureJSON.offset[ 0 ], textureJSON.offset[ 1 ] ); } // handle wrap after repeat so that default repeat can be overriden if ( textureJSON.wrap ) { var wrapMap = { "repeat": THREE.RepeatWrapping, "mirror": THREE.MirroredRepeatWrapping } if ( wrapMap[ textureJSON.wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ textureJSON.wrap[ 0 ] ]; if ( wrapMap[ textureJSON.wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ textureJSON.wrap[ 1 ] ]; } } result.textures[ textureID ] = texture; } // materials var matID, matJSON; var parID; for ( matID in data.materials ) { matJSON = data.materials[ matID ]; for ( parID in matJSON.parameters ) { if ( parID === "envMap" || parID === "map" || parID === "lightMap" || parID === "bumpMap" ) { matJSON.parameters[ parID ] = result.textures[ matJSON.parameters[ parID ] ]; } else if ( parID === "shading" ) { matJSON.parameters[ parID ] = ( matJSON.parameters[ parID ] === "flat" ) ? THREE.FlatShading : THREE.SmoothShading; } else if ( parID === "side" ) { if ( matJSON.parameters[ parID ] == "double" ) { matJSON.parameters[ parID ] = THREE.DoubleSide; } else if ( matJSON.parameters[ parID ] == "back" ) { matJSON.parameters[ parID ] = THREE.BackSide; } else { matJSON.parameters[ parID ] = THREE.FrontSide; } } else if ( parID === "blending" ) { matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.NormalBlending; } else if ( parID === "combine" ) { matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.MultiplyOperation; } else if ( parID === "vertexColors" ) { if ( matJSON.parameters[ parID ] == "face" ) { matJSON.parameters[ parID ] = THREE.FaceColors; // default to vertex colors if "vertexColors" is anything else face colors or 0 / null / false } else if ( matJSON.parameters[ parID ] ) { matJSON.parameters[ parID ] = THREE.VertexColors; } } else if ( parID === "wrapRGB" ) { var v3 = matJSON.parameters[ parID ]; matJSON.parameters[ parID ] = new THREE.Vector3( v3[ 0 ], v3[ 1 ], v3[ 2 ] ); } } if ( matJSON.parameters.opacity !== undefined && matJSON.parameters.opacity < 1.0 ) { matJSON.parameters.transparent = true; } if ( matJSON.parameters.normalMap ) { var shader = THREE.ShaderLib[ "normalmap" ]; var uniforms = THREE.UniformsUtils.clone( shader.uniforms ); var diffuse = matJSON.parameters.color; var specular = matJSON.parameters.specular; var ambient = matJSON.parameters.ambient; var shininess = matJSON.parameters.shininess; uniforms[ "tNormal" ].value = result.textures[ matJSON.parameters.normalMap ]; if ( matJSON.parameters.normalScale ) { uniforms[ "uNormalScale" ].value.set( matJSON.parameters.normalScale[ 0 ], matJSON.parameters.normalScale[ 1 ] ); } if ( matJSON.parameters.map ) { uniforms[ "tDiffuse" ].value = matJSON.parameters.map; uniforms[ "enableDiffuse" ].value = true; } if ( matJSON.parameters.envMap ) { uniforms[ "tCube" ].value = matJSON.parameters.envMap; uniforms[ "enableReflection" ].value = true; uniforms[ "reflectivity" ].value = matJSON.parameters.reflectivity; } if ( matJSON.parameters.lightMap ) { uniforms[ "tAO" ].value = matJSON.parameters.lightMap; uniforms[ "enableAO" ].value = true; } if ( matJSON.parameters.specularMap ) { uniforms[ "tSpecular" ].value = result.textures[ matJSON.parameters.specularMap ]; uniforms[ "enableSpecular" ].value = true; } if ( matJSON.parameters.displacementMap ) { uniforms[ "tDisplacement" ].value = result.textures[ matJSON.parameters.displacementMap ]; uniforms[ "enableDisplacement" ].value = true; uniforms[ "uDisplacementBias" ].value = matJSON.parameters.displacementBias; uniforms[ "uDisplacementScale" ].value = matJSON.parameters.displacementScale; } uniforms[ "diffuse" ].value.setHex( diffuse ); uniforms[ "specular" ].value.setHex( specular ); uniforms[ "ambient" ].value.setHex( ambient ); uniforms[ "shininess" ].value = shininess; if ( matJSON.parameters.opacity ) { uniforms[ "opacity" ].value = matJSON.parameters.opacity; } var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: true }; material = new THREE.ShaderMaterial( parameters ); } else { material = new THREE[ matJSON.type ]( matJSON.parameters ); } material.name = matID; result.materials[ matID ] = material; } // second pass through all materials to initialize MeshFaceMaterials // that could be referring to other materials out of order for ( matID in data.materials ) { matJSON = data.materials[ matID ]; if ( matJSON.parameters.materials ) { var materialArray = []; for ( var i = 0; i < matJSON.parameters.materials.length; i ++ ) { var label = matJSON.parameters.materials[ i ]; materialArray.push( result.materials[ label ] ); } result.materials[ matID ].materials = materialArray; } } // objects ( synchronous init of procedural primitives ) handle_objects(); // defaults if ( result.cameras && data.defaults.camera ) { result.currentCamera = result.cameras[ data.defaults.camera ]; } if ( result.fogs && data.defaults.fog ) { result.scene.fog = result.fogs[ data.defaults.fog ]; } // synchronous callback scope.callbackSync( result ); // just in case there are no async elements async_callback_gate(); } } /** * @author mrdoob / http://mrdoob.com/ */ THREE.TextureLoader = function ( manager ) { this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager; }; THREE.TextureLoader.prototype = { constructor: THREE.TextureLoader, load: function ( url, onLoad, onProgress, onError ) { var scope = this; var loader = new THREE.ImageLoader( scope.manager ); loader.setCrossOrigin( this.crossOrigin ); loader.load( url, function ( image ) { var texture = new THREE.Texture( image ); texture.needsUpdate = true; if ( onLoad !== undefined ) { onLoad( texture ); } } ); }, setCrossOrigin: function ( value ) { this.crossOrigin = value; } }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ THREE.Material = function () { this.id = THREE.MaterialIdCount ++; this.uuid = THREE.Math.generateUUID(); this.name = ''; this.side = THREE.FrontSide; this.opacity = 1; this.transparent = false; this.blending = THREE.NormalBlending; this.blendSrc = THREE.SrcAlphaFactor; this.blendDst = THREE.OneMinusSrcAlphaFactor; this.blendEquation = THREE.AddEquation; this.depthTest = true; this.depthWrite = true; this.polygonOffset = false; this.polygonOffsetFactor = 0; this.polygonOffsetUnits = 0; this.alphaTest = 0; this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer this.visible = true; this.needsUpdate = true; }; THREE.Material.prototype = { constructor: THREE.Material, setValues: function ( values ) { if ( values === undefined ) return; for ( var key in values ) { var newValue = values[ key ]; if ( newValue === undefined ) { console.warn( 'THREE.Material: \'' + key + '\' parameter is undefined.' ); continue; } if ( key in this ) { var currentValue = this[ key ]; if ( currentValue instanceof THREE.Color ) { currentValue.set( newValue ); } else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) { currentValue.copy( newValue ); } else if ( key == 'overdraw') { // ensure overdraw is backwards-compatable with legacy boolean type this[ key ] = Number(newValue); } else { this[ key ] = newValue; } } } }, clone: function ( material ) { if ( material === undefined ) material = new THREE.Material(); material.name = this.name; material.side = this.side; material.opacity = this.opacity; material.transparent = this.transparent; material.blending = this.blending; material.blendSrc = this.blendSrc; material.blendDst = this.blendDst; material.blendEquation = this.blendEquation; material.depthTest = this.depthTest; material.depthWrite = this.depthWrite; material.polygonOffset = this.polygonOffset; material.polygonOffsetFactor = this.polygonOffsetFactor; material.polygonOffsetUnits = this.polygonOffsetUnits; material.alphaTest = this.alphaTest; material.overdraw = this.overdraw; material.visible = this.visible; return material; }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } }; THREE.EventDispatcher.prototype.apply( THREE.Material.prototype ); THREE.MaterialIdCount = 0; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * * parameters = { * color: <hex>, * opacity: <float>, * * blending: THREE.NormalBlending, * depthTest: <bool>, * depthWrite: <bool>, * * linewidth: <float>, * linecap: "round", * linejoin: "round", * * vertexColors: <bool> * * fog: <bool> * } */ THREE.LineBasicMaterial = function ( parameters ) { THREE.Material.call( this ); this.color = new THREE.Color( 0xffffff ); this.linewidth = 1; this.linecap = 'round'; this.linejoin = 'round'; this.vertexColors = false; this.fog = true; this.setValues( parameters ); }; THREE.LineBasicMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.LineBasicMaterial.prototype.clone = function () { var material = new THREE.LineBasicMaterial(); THREE.Material.prototype.clone.call( this, material ); material.color.copy( this.color ); material.linewidth = this.linewidth; material.linecap = this.linecap; material.linejoin = this.linejoin; material.vertexColors = this.vertexColors; material.fog = this.fog; return material; }; /** * @author alteredq / http://alteredqualia.com/ * * parameters = { * color: <hex>, * opacity: <float>, * * blending: THREE.NormalBlending, * depthTest: <bool>, * depthWrite: <bool>, * * linewidth: <float>, * * scale: <float>, * dashSize: <float>, * gapSize: <float>, * * vertexColors: <bool> * * fog: <bool> * } */ THREE.LineDashedMaterial = function ( parameters ) { THREE.Material.call( this ); this.color = new THREE.Color( 0xffffff ); this.linewidth = 1; this.scale = 1; this.dashSize = 3; this.gapSize = 1; this.vertexColors = false; this.fog = true; this.setValues( parameters ); }; THREE.LineDashedMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.LineDashedMaterial.prototype.clone = function () { var material = new THREE.LineDashedMaterial(); THREE.Material.prototype.clone.call( this, material ); material.color.copy( this.color ); material.linewidth = this.linewidth; material.scale = this.scale; material.dashSize = this.dashSize; material.gapSize = this.gapSize; material.vertexColors = this.vertexColors; material.fog = this.fog; return material; }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * * parameters = { * color: <hex>, * opacity: <float>, * map: new THREE.Texture( <Image> ), * * lightMap: new THREE.Texture( <Image> ), * * specularMap: new THREE.Texture( <Image> ), * * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ), * combine: THREE.Multiply, * reflectivity: <float>, * refractionRatio: <float>, * * shading: THREE.SmoothShading, * blending: THREE.NormalBlending, * depthTest: <bool>, * depthWrite: <bool>, * * wireframe: <boolean>, * wireframeLinewidth: <float>, * * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors, * * skinning: <bool>, * morphTargets: <bool>, * * fog: <bool> * } */ THREE.MeshBasicMaterial = function ( parameters ) { THREE.Material.call( this ); this.color = new THREE.Color( 0xffffff ); // emissive this.map = null; this.lightMap = null; this.specularMap = null; this.envMap = null; this.combine = THREE.MultiplyOperation; this.reflectivity = 1; this.refractionRatio = 0.98; this.fog = true; this.shading = THREE.SmoothShading; this.wireframe = false; this.wireframeLinewidth = 1; this.wireframeLinecap = 'round'; this.wireframeLinejoin = 'round'; this.vertexColors = THREE.NoColors; this.skinning = false; this.morphTargets = false; this.setValues( parameters ); }; THREE.MeshBasicMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.MeshBasicMaterial.prototype.clone = function () { var material = new THREE.MeshBasicMaterial(); THREE.Material.prototype.clone.call( this, material ); material.color.copy( this.color ); material.map = this.map; material.lightMap = this.lightMap; material.specularMap = this.specularMap; material.envMap = this.envMap; material.combine = this.combine; material.reflectivity = this.reflectivity; material.refractionRatio = this.refractionRatio; material.fog = this.fog; material.shading = this.shading; material.wireframe = this.wireframe; material.wireframeLinewidth = this.wireframeLinewidth; material.wireframeLinecap = this.wireframeLinecap; material.wireframeLinejoin = this.wireframeLinejoin; material.vertexColors = this.vertexColors; material.skinning = this.skinning; material.morphTargets = this.morphTargets; return material; }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * * parameters = { * color: <hex>, * ambient: <hex>, * emissive: <hex>, * opacity: <float>, * * map: new THREE.Texture( <Image> ), * * lightMap: new THREE.Texture( <Image> ), * * specularMap: new THREE.Texture( <Image> ), * * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ), * combine: THREE.Multiply, * reflectivity: <float>, * refractionRatio: <float>, * * shading: THREE.SmoothShading, * blending: THREE.NormalBlending, * depthTest: <bool>, * depthWrite: <bool>, * * wireframe: <boolean>, * wireframeLinewidth: <float>, * * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors, * * skinning: <bool>, * morphTargets: <bool>, * morphNormals: <bool>, * * fog: <bool> * } */ THREE.MeshLambertMaterial = function ( parameters ) { THREE.Material.call( this ); this.color = new THREE.Color( 0xffffff ); // diffuse this.ambient = new THREE.Color( 0xffffff ); this.emissive = new THREE.Color( 0x000000 ); this.wrapAround = false; this.wrapRGB = new THREE.Vector3( 1, 1, 1 ); this.map = null; this.lightMap = null; this.specularMap = null; this.envMap = null; this.combine = THREE.MultiplyOperation; this.reflectivity = 1; this.refractionRatio = 0.98; this.fog = true; this.shading = THREE.SmoothShading; this.wireframe = false; this.wireframeLinewidth = 1; this.wireframeLinecap = 'round'; this.wireframeLinejoin = 'round'; this.vertexColors = THREE.NoColors; this.skinning = false; this.morphTargets = false; this.morphNormals = false; this.setValues( parameters ); }; THREE.MeshLambertMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.MeshLambertMaterial.prototype.clone = function () { var material = new THREE.MeshLambertMaterial(); THREE.Material.prototype.clone.call( this, material ); material.color.copy( this.color ); material.ambient.copy( this.ambient ); material.emissive.copy( this.emissive ); material.wrapAround = this.wrapAround; material.wrapRGB.copy( this.wrapRGB ); material.map = this.map; material.lightMap = this.lightMap; material.specularMap = this.specularMap; material.envMap = this.envMap; material.combine = this.combine; material.reflectivity = this.reflectivity; material.refractionRatio = this.refractionRatio; material.fog = this.fog; material.shading = this.shading; material.wireframe = this.wireframe; material.wireframeLinewidth = this.wireframeLinewidth; material.wireframeLinecap = this.wireframeLinecap; material.wireframeLinejoin = this.wireframeLinejoin; material.vertexColors = this.vertexColors; material.skinning = this.skinning; material.morphTargets = this.morphTargets; material.morphNormals = this.morphNormals; return material; }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * * parameters = { * color: <hex>, * ambient: <hex>, * emissive: <hex>, * specular: <hex>, * shininess: <float>, * opacity: <float>, * * map: new THREE.Texture( <Image> ), * * lightMap: new THREE.Texture( <Image> ), * * bumpMap: new THREE.Texture( <Image> ), * bumpScale: <float>, * * normalMap: new THREE.Texture( <Image> ), * normalScale: <Vector2>, * * specularMap: new THREE.Texture( <Image> ), * * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ), * combine: THREE.Multiply, * reflectivity: <float>, * refractionRatio: <float>, * * shading: THREE.SmoothShading, * blending: THREE.NormalBlending, * depthTest: <bool>, * depthWrite: <bool>, * * wireframe: <boolean>, * wireframeLinewidth: <float>, * * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors, * * skinning: <bool>, * morphTargets: <bool>, * morphNormals: <bool>, * * fog: <bool> * } */ THREE.MeshPhongMaterial = function ( parameters ) { THREE.Material.call( this ); this.color = new THREE.Color( 0xffffff ); // diffuse this.ambient = new THREE.Color( 0xffffff ); this.emissive = new THREE.Color( 0x000000 ); this.specular = new THREE.Color( 0x111111 ); this.shininess = 30; this.metal = false; this.wrapAround = false; this.wrapRGB = new THREE.Vector3( 1, 1, 1 ); this.map = null; this.lightMap = null; this.bumpMap = null; this.bumpScale = 1; this.normalMap = null; this.normalScale = new THREE.Vector2( 1, 1 ); this.specularMap = null; this.envMap = null; this.combine = THREE.MultiplyOperation; this.reflectivity = 1; this.refractionRatio = 0.98; this.fog = true; this.shading = THREE.SmoothShading; this.wireframe = false; this.wireframeLinewidth = 1; this.wireframeLinecap = 'round'; this.wireframeLinejoin = 'round'; this.vertexColors = THREE.NoColors; this.skinning = false; this.morphTargets = false; this.morphNormals = false; this.setValues( parameters ); }; THREE.MeshPhongMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.MeshPhongMaterial.prototype.clone = function () { var material = new THREE.MeshPhongMaterial(); THREE.Material.prototype.clone.call( this, material ); material.color.copy( this.color ); material.ambient.copy( this.ambient ); material.emissive.copy( this.emissive ); material.specular.copy( this.specular ); material.shininess = this.shininess; material.metal = this.metal; material.wrapAround = this.wrapAround; material.wrapRGB.copy( this.wrapRGB ); material.map = this.map; material.lightMap = this.lightMap; material.bumpMap = this.bumpMap; material.bumpScale = this.bumpScale; material.normalMap = this.normalMap; material.normalScale.copy( this.normalScale ); material.specularMap = this.specularMap; material.envMap = this.envMap; material.combine = this.combine; material.reflectivity = this.reflectivity; material.refractionRatio = this.refractionRatio; material.fog = this.fog; material.shading = this.shading; material.wireframe = this.wireframe; material.wireframeLinewidth = this.wireframeLinewidth; material.wireframeLinecap = this.wireframeLinecap; material.wireframeLinejoin = this.wireframeLinejoin; material.vertexColors = this.vertexColors; material.skinning = this.skinning; material.morphTargets = this.morphTargets; material.morphNormals = this.morphNormals; return material; }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * * parameters = { * opacity: <float>, * * blending: THREE.NormalBlending, * depthTest: <bool>, * depthWrite: <bool>, * * wireframe: <boolean>, * wireframeLinewidth: <float> * } */ THREE.MeshDepthMaterial = function ( parameters ) { THREE.Material.call( this ); this.wireframe = false; this.wireframeLinewidth = 1; this.setValues( parameters ); }; THREE.MeshDepthMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.MeshDepthMaterial.prototype.clone = function () { var material = new THREE.MeshDepthMaterial(); THREE.Material.prototype.clone.call( this, material ); material.wireframe = this.wireframe; material.wireframeLinewidth = this.wireframeLinewidth; return material; }; /** * @author mrdoob / http://mrdoob.com/ * * parameters = { * opacity: <float>, * * shading: THREE.FlatShading, * blending: THREE.NormalBlending, * depthTest: <bool>, * depthWrite: <bool>, * * wireframe: <boolean>, * wireframeLinewidth: <float> * } */ THREE.MeshNormalMaterial = function ( parameters ) { THREE.Material.call( this, parameters ); this.shading = THREE.FlatShading; this.wireframe = false; this.wireframeLinewidth = 1; this.morphTargets = false; this.setValues( parameters ); }; THREE.MeshNormalMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.MeshNormalMaterial.prototype.clone = function () { var material = new THREE.MeshNormalMaterial(); THREE.Material.prototype.clone.call( this, material ); material.shading = this.shading; material.wireframe = this.wireframe; material.wireframeLinewidth = this.wireframeLinewidth; return material; }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.MeshFaceMaterial = function ( materials ) { this.materials = materials instanceof Array ? materials : []; }; THREE.MeshFaceMaterial.prototype.clone = function () { var material = new THREE.MeshFaceMaterial(); for ( var i = 0; i < this.materials.length; i ++ ) { material.materials.push( this.materials[ i ].clone() ); } return material; }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * * parameters = { * color: <hex>, * opacity: <float>, * map: new THREE.Texture( <Image> ), * * size: <float>, * * blending: THREE.NormalBlending, * depthTest: <bool>, * depthWrite: <bool>, * * vertexColors: <bool>, * * fog: <bool> * } */ THREE.ParticleSystemMaterial = function ( parameters ) { THREE.Material.call( this ); this.color = new THREE.Color( 0xffffff ); this.map = null; this.size = 1; this.sizeAttenuation = true; this.vertexColors = false; this.fog = true; this.setValues( parameters ); }; THREE.ParticleSystemMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.ParticleSystemMaterial.prototype.clone = function () { var material = new THREE.ParticleSystemMaterial(); THREE.Material.prototype.clone.call( this, material ); material.color.copy( this.color ); material.map = this.map; material.size = this.size; material.sizeAttenuation = this.sizeAttenuation; material.vertexColors = this.vertexColors; material.fog = this.fog; return material; }; // backwards compatibility THREE.ParticleBasicMaterial = THREE.ParticleSystemMaterial; /** * @author alteredq / http://alteredqualia.com/ * * parameters = { * fragmentShader: <string>, * vertexShader: <string>, * * uniforms: { "parameter1": { type: "f", value: 1.0 }, "parameter2": { type: "i" value2: 2 } }, * * defines: { "label" : "value" }, * * shading: THREE.SmoothShading, * blending: THREE.NormalBlending, * depthTest: <bool>, * depthWrite: <bool>, * * wireframe: <boolean>, * wireframeLinewidth: <float>, * * lights: <bool>, * * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors, * * skinning: <bool>, * morphTargets: <bool>, * morphNormals: <bool>, * * fog: <bool> * } */ THREE.ShaderMaterial = function ( parameters ) { THREE.Material.call( this ); this.fragmentShader = "void main() {}"; this.vertexShader = "void main() {}"; this.uniforms = {}; this.defines = {}; this.attributes = null; this.shading = THREE.SmoothShading; this.linewidth = 1; this.wireframe = false; this.wireframeLinewidth = 1; this.fog = false; // set to use scene fog this.lights = false; // set to use scene lights this.vertexColors = THREE.NoColors; // set to use "color" attribute stream this.skinning = false; // set to use skinning attribute streams this.morphTargets = false; // set to use morph targets this.morphNormals = false; // set to use morph normals // When rendered geometry doesn't include these attributes but the material does, // use these default values in WebGL. This avoids errors when buffer data is missing. this.defaultAttributeValues = { "color" : [ 1, 1, 1], "uv" : [ 0, 0 ], "uv2" : [ 0, 0 ] }; // By default, bind position to attribute index 0. In WebGL, attribute 0 // should always be used to avoid potentially expensive emulation. this.index0AttributeName = "position"; this.setValues( parameters ); }; THREE.ShaderMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.ShaderMaterial.prototype.clone = function () { var material = new THREE.ShaderMaterial(); THREE.Material.prototype.clone.call( this, material ); material.fragmentShader = this.fragmentShader; material.vertexShader = this.vertexShader; material.uniforms = THREE.UniformsUtils.clone( this.uniforms ); material.attributes = this.attributes; material.defines = this.defines; material.shading = this.shading; material.wireframe = this.wireframe; material.wireframeLinewidth = this.wireframeLinewidth; material.fog = this.fog; material.lights = this.lights; material.vertexColors = this.vertexColors; material.skinning = this.skinning; material.morphTargets = this.morphTargets; material.morphNormals = this.morphNormals; return material; }; /** * @author alteredq / http://alteredqualia.com/ * * parameters = { * color: <hex>, * opacity: <float>, * map: new THREE.Texture( <Image> ), * * blending: THREE.NormalBlending, * depthTest: <bool>, * depthWrite: <bool>, * * uvOffset: new THREE.Vector2(), * uvScale: new THREE.Vector2(), * * fog: <bool> * } */ THREE.SpriteMaterial = function ( parameters ) { THREE.Material.call( this ); // defaults this.color = new THREE.Color( 0xffffff ); this.map = null; this.rotation = 0; this.fog = false; // set parameters this.setValues( parameters ); }; THREE.SpriteMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.SpriteMaterial.prototype.clone = function () { var material = new THREE.SpriteMaterial(); THREE.Material.prototype.clone.call( this, material ); material.color.copy( this.color ); material.map = this.map; material.rotation = this.rotation; material.fog = this.fog; return material; }; /** * @author mrdoob / http://mrdoob.com/ * * parameters = { * color: <hex>, * program: <function>, * opacity: <float>, * blending: THREE.NormalBlending * } */ THREE.SpriteCanvasMaterial = function ( parameters ) { THREE.Material.call( this ); this.color = new THREE.Color( 0xffffff ); this.program = function ( context, color ) {}; this.setValues( parameters ); }; THREE.SpriteCanvasMaterial.prototype = Object.create( THREE.Material.prototype ); THREE.SpriteCanvasMaterial.prototype.clone = function () { var material = new THREE.SpriteCanvasMaterial(); THREE.Material.prototype.clone.call( this, material ); material.color.copy( this.color ); material.program = this.program; return material; }; // backwards compatibility THREE.ParticleCanvasMaterial = THREE.SpriteCanvasMaterial; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * @author szimek / https://github.com/szimek/ */ THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) { this.id = THREE.TextureIdCount ++; this.uuid = THREE.Math.generateUUID(); this.name = ''; this.image = image; this.mipmaps = []; this.mapping = mapping !== undefined ? mapping : new THREE.UVMapping(); this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping; this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping; this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter; this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter; this.anisotropy = anisotropy !== undefined ? anisotropy : 1; this.format = format !== undefined ? format : THREE.RGBAFormat; this.type = type !== undefined ? type : THREE.UnsignedByteType; this.offset = new THREE.Vector2( 0, 0 ); this.repeat = new THREE.Vector2( 1, 1 ); this.generateMipmaps = true; this.premultiplyAlpha = false; this.flipY = true; this.unpackAlignment = 4; // valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml) this._needsUpdate = false; this.onUpdate = null; }; THREE.Texture.prototype = { constructor: THREE.Texture, get needsUpdate () { return this._needsUpdate; }, set needsUpdate ( value ) { if ( value === true ) this.update(); this._needsUpdate = value; }, clone: function ( texture ) { if ( texture === undefined ) texture = new THREE.Texture(); texture.image = this.image; texture.mipmaps = this.mipmaps.slice(0); texture.mapping = this.mapping; texture.wrapS = this.wrapS; texture.wrapT = this.wrapT; texture.magFilter = this.magFilter; texture.minFilter = this.minFilter; texture.anisotropy = this.anisotropy; texture.format = this.format; texture.type = this.type; texture.offset.copy( this.offset ); texture.repeat.copy( this.repeat ); texture.generateMipmaps = this.generateMipmaps; texture.premultiplyAlpha = this.premultiplyAlpha; texture.flipY = this.flipY; texture.unpackAlignment = this.unpackAlignment; return texture; }, update: function () { this.dispatchEvent( { type: 'update' } ); }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } }; THREE.EventDispatcher.prototype.apply( THREE.Texture.prototype ); THREE.TextureIdCount = 0; /** * @author alteredq / http://alteredqualia.com/ */ THREE.CompressedTexture = function ( mipmaps, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy ) { THREE.Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ); this.image = { width: width, height: height }; this.mipmaps = mipmaps; this.generateMipmaps = false; // WebGL currently can't generate mipmaps for compressed textures, they must be embedded in DDS file }; THREE.CompressedTexture.prototype = Object.create( THREE.Texture.prototype ); THREE.CompressedTexture.prototype.clone = function () { var texture = new THREE.CompressedTexture(); THREE.Texture.prototype.clone.call( this, texture ); return texture; }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.DataTexture = function ( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy ) { THREE.Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ); this.image = { data: data, width: width, height: height }; }; THREE.DataTexture.prototype = Object.create( THREE.Texture.prototype ); THREE.DataTexture.prototype.clone = function () { var texture = new THREE.DataTexture(); THREE.Texture.prototype.clone.call( this, texture ); return texture; }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.ParticleSystem = function ( geometry, material ) { THREE.Object3D.call( this ); this.geometry = geometry !== undefined ? geometry : new THREE.Geometry(); this.material = material !== undefined ? material : new THREE.ParticleSystemMaterial( { color: Math.random() * 0xffffff } ); this.sortParticles = false; this.frustumCulled = false; }; THREE.ParticleSystem.prototype = Object.create( THREE.Object3D.prototype ); THREE.ParticleSystem.prototype.clone = function ( object ) { if ( object === undefined ) object = new THREE.ParticleSystem( this.geometry, this.material ); object.sortParticles = this.sortParticles; THREE.Object3D.prototype.clone.call( this, object ); return object; }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.Line = function ( geometry, material, type ) { THREE.Object3D.call( this ); this.geometry = geometry !== undefined ? geometry : new THREE.Geometry(); this.material = material !== undefined ? material : new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff } ); this.type = ( type !== undefined ) ? type : THREE.LineStrip; }; THREE.LineStrip = 0; THREE.LinePieces = 1; THREE.Line.prototype = Object.create( THREE.Object3D.prototype ); THREE.Line.prototype.clone = function ( object ) { if ( object === undefined ) object = new THREE.Line( this.geometry, this.material, this.type ); THREE.Object3D.prototype.clone.call( this, object ); return object; }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * @author mikael emtinger / http://gomo.se/ * @author jonobr1 / http://jonobr1.com/ */ THREE.Mesh = function ( geometry, material ) { THREE.Object3D.call( this ); this.geometry = geometry !== undefined ? geometry : new THREE.Geometry(); this.material = material !== undefined ? material : new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ); this.updateMorphTargets(); }; THREE.Mesh.prototype = Object.create( THREE.Object3D.prototype ); THREE.Mesh.prototype.updateMorphTargets = function () { if ( this.geometry.morphTargets !== undefined && this.geometry.morphTargets.length > 0 ) { this.morphTargetBase = -1; this.morphTargetForcedOrder = []; this.morphTargetInfluences = []; this.morphTargetDictionary = {}; for ( var m = 0, ml = this.geometry.morphTargets.length; m < ml; m ++ ) { this.morphTargetInfluences.push( 0 ); this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m; } } }; THREE.Mesh.prototype.getMorphTargetIndexByName = function ( name ) { if ( this.morphTargetDictionary[ name ] !== undefined ) { return this.morphTargetDictionary[ name ]; } console.log( "THREE.Mesh.getMorphTargetIndexByName: morph target " + name + " does not exist. Returning 0." ); return 0; }; THREE.Mesh.prototype.clone = function ( object ) { if ( object === undefined ) object = new THREE.Mesh( this.geometry, this.material ); THREE.Object3D.prototype.clone.call( this, object ); return object; }; /** * @author mikael emtinger / http://gomo.se/ * @author alteredq / http://alteredqualia.com/ */ THREE.Bone = function( belongsToSkin ) { THREE.Object3D.call( this ); this.skin = belongsToSkin; this.skinMatrix = new THREE.Matrix4(); }; THREE.Bone.prototype = Object.create( THREE.Object3D.prototype ); THREE.Bone.prototype.update = function ( parentSkinMatrix, forceUpdate ) { // update local if ( this.matrixAutoUpdate ) { forceUpdate |= this.updateMatrix(); } // update skin matrix if ( forceUpdate || this.matrixWorldNeedsUpdate ) { if( parentSkinMatrix ) { this.skinMatrix.multiplyMatrices( parentSkinMatrix, this.matrix ); } else { this.skinMatrix.copy( this.matrix ); } this.matrixWorldNeedsUpdate = false; forceUpdate = true; } // update children var child, i, l = this.children.length; for ( i = 0; i < l; i ++ ) { this.children[ i ].update( this.skinMatrix, forceUpdate ); } }; /** * @author mikael emtinger / http://gomo.se/ * @author alteredq / http://alteredqualia.com/ */ THREE.SkinnedMesh = function ( geometry, material, useVertexTexture ) { THREE.Mesh.call( this, geometry, material ); // this.useVertexTexture = useVertexTexture !== undefined ? useVertexTexture : true; // init bones this.identityMatrix = new THREE.Matrix4(); this.bones = []; this.boneMatrices = []; var b, bone, gbone, p, q, s; if ( this.geometry && this.geometry.bones !== undefined ) { for ( b = 0; b < this.geometry.bones.length; b ++ ) { gbone = this.geometry.bones[ b ]; p = gbone.pos; q = gbone.rotq; s = gbone.scl; bone = this.addBone(); bone.name = gbone.name; bone.position.set( p[0], p[1], p[2] ); bone.quaternion.set( q[0], q[1], q[2], q[3] ); if ( s !== undefined ) { bone.scale.set( s[0], s[1], s[2] ); } else { bone.scale.set( 1, 1, 1 ); } } for ( b = 0; b < this.bones.length; b ++ ) { gbone = this.geometry.bones[ b ]; bone = this.bones[ b ]; if ( gbone.parent === -1 ) { this.add( bone ); } else { this.bones[ gbone.parent ].add( bone ); } } // var nBones = this.bones.length; if ( this.useVertexTexture ) { // layout (1 matrix = 4 pixels) // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4) // with 8x8 pixel texture max 16 bones (8 * 8 / 4) // 16x16 pixel texture max 64 bones (16 * 16 / 4) // 32x32 pixel texture max 256 bones (32 * 32 / 4) // 64x64 pixel texture max 1024 bones (64 * 64 / 4) var size; if ( nBones > 256 ) size = 64; else if ( nBones > 64 ) size = 32; else if ( nBones > 16 ) size = 16; else size = 8; this.boneTextureWidth = size; this.boneTextureHeight = size; this.boneMatrices = new Float32Array( this.boneTextureWidth * this.boneTextureHeight * 4 ); // 4 floats per RGBA pixel this.boneTexture = new THREE.DataTexture( this.boneMatrices, this.boneTextureWidth, this.boneTextureHeight, THREE.RGBAFormat, THREE.FloatType ); this.boneTexture.minFilter = THREE.NearestFilter; this.boneTexture.magFilter = THREE.NearestFilter; this.boneTexture.generateMipmaps = false; this.boneTexture.flipY = false; } else { this.boneMatrices = new Float32Array( 16 * nBones ); } this.pose(); } }; THREE.SkinnedMesh.prototype = Object.create( THREE.Mesh.prototype ); THREE.SkinnedMesh.prototype.addBone = function( bone ) { if ( bone === undefined ) { bone = new THREE.Bone( this ); } this.bones.push( bone ); return bone; }; THREE.SkinnedMesh.prototype.updateMatrixWorld = function () { var offsetMatrix = new THREE.Matrix4(); return function ( force ) { this.matrixAutoUpdate && this.updateMatrix(); // update matrixWorld if ( this.matrixWorldNeedsUpdate || force ) { if ( this.parent ) { this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix ); } else { this.matrixWorld.copy( this.matrix ); } this.matrixWorldNeedsUpdate = false; force = true; } // update children for ( var i = 0, l = this.children.length; i < l; i ++ ) { var child = this.children[ i ]; if ( child instanceof THREE.Bone ) { child.update( this.identityMatrix, false ); } else { child.updateMatrixWorld( true ); } } // make a snapshot of the bones' rest position if ( this.boneInverses == undefined ) { this.boneInverses = []; for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) { var inverse = new THREE.Matrix4(); inverse.getInverse( this.bones[ b ].skinMatrix ); this.boneInverses.push( inverse ); } } // flatten bone matrices to array for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) { // compute the offset between the current and the original transform; // TODO: we could get rid of this multiplication step if the skinMatrix // was already representing the offset; however, this requires some // major changes to the animation system offsetMatrix.multiplyMatrices( this.bones[ b ].skinMatrix, this.boneInverses[ b ] ); offsetMatrix.flattenToArrayOffset( this.boneMatrices, b * 16 ); } if ( this.useVertexTexture ) { this.boneTexture.needsUpdate = true; } }; }(); THREE.SkinnedMesh.prototype.pose = function () { this.updateMatrixWorld( true ); this.normalizeSkinWeights(); }; THREE.SkinnedMesh.prototype.normalizeSkinWeights = function () { if ( this.geometry instanceof THREE.Geometry ) { for ( var i = 0; i < this.geometry.skinIndices.length; i ++ ) { var sw = this.geometry.skinWeights[ i ]; var scale = 1.0 / sw.lengthManhattan(); if ( scale !== Infinity ) { sw.multiplyScalar( scale ); } else { sw.set( 1 ); // this will be normalized by the shader anyway } } } else { // skinning weights assumed to be normalized for THREE.BufferGeometry } }; THREE.SkinnedMesh.prototype.clone = function ( object ) { if ( object === undefined ) { object = new THREE.SkinnedMesh( this.geometry, this.material, this.useVertexTexture ); } THREE.Mesh.prototype.clone.call( this, object ); return object; }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.MorphAnimMesh = function ( geometry, material ) { THREE.Mesh.call( this, geometry, material ); // API this.duration = 1000; // milliseconds this.mirroredLoop = false; this.time = 0; // internals this.lastKeyframe = 0; this.currentKeyframe = 0; this.direction = 1; this.directionBackwards = false; this.setFrameRange( 0, this.geometry.morphTargets.length - 1 ); }; THREE.MorphAnimMesh.prototype = Object.create( THREE.Mesh.prototype ); THREE.MorphAnimMesh.prototype.setFrameRange = function ( start, end ) { this.startKeyframe = start; this.endKeyframe = end; this.length = this.endKeyframe - this.startKeyframe + 1; }; THREE.MorphAnimMesh.prototype.setDirectionForward = function () { this.direction = 1; this.directionBackwards = false; }; THREE.MorphAnimMesh.prototype.setDirectionBackward = function () { this.direction = -1; this.directionBackwards = true; }; THREE.MorphAnimMesh.prototype.parseAnimations = function () { var geometry = this.geometry; if ( ! geometry.animations ) geometry.animations = {}; var firstAnimation, animations = geometry.animations; var pattern = /([a-z]+)(\d+)/; for ( var i = 0, il = geometry.morphTargets.length; i < il; i ++ ) { var morph = geometry.morphTargets[ i ]; var parts = morph.name.match( pattern ); if ( parts && parts.length > 1 ) { var label = parts[ 1 ]; var num = parts[ 2 ]; if ( ! animations[ label ] ) animations[ label ] = { start: Infinity, end: -Infinity }; var animation = animations[ label ]; if ( i < animation.start ) animation.start = i; if ( i > animation.end ) animation.end = i; if ( ! firstAnimation ) firstAnimation = label; } } geometry.firstAnimation = firstAnimation; }; THREE.MorphAnimMesh.prototype.setAnimationLabel = function ( label, start, end ) { if ( ! this.geometry.animations ) this.geometry.animations = {}; this.geometry.animations[ label ] = { start: start, end: end }; }; THREE.MorphAnimMesh.prototype.playAnimation = function ( label, fps ) { var animation = this.geometry.animations[ label ]; if ( animation ) { this.setFrameRange( animation.start, animation.end ); this.duration = 1000 * ( ( animation.end - animation.start ) / fps ); this.time = 0; } else { console.warn( "animation[" + label + "] undefined" ); } }; THREE.MorphAnimMesh.prototype.updateAnimation = function ( delta ) { var frameTime = this.duration / this.length; this.time += this.direction * delta; if ( this.mirroredLoop ) { if ( this.time > this.duration || this.time < 0 ) { this.direction *= -1; if ( this.time > this.duration ) { this.time = this.duration; this.directionBackwards = true; } if ( this.time < 0 ) { this.time = 0; this.directionBackwards = false; } } } else { this.time = this.time % this.duration; if ( this.time < 0 ) this.time += this.duration; } var keyframe = this.startKeyframe + THREE.Math.clamp( Math.floor( this.time / frameTime ), 0, this.length - 1 ); if ( keyframe !== this.currentKeyframe ) { this.morphTargetInfluences[ this.lastKeyframe ] = 0; this.morphTargetInfluences[ this.currentKeyframe ] = 1; this.morphTargetInfluences[ keyframe ] = 0; this.lastKeyframe = this.currentKeyframe; this.currentKeyframe = keyframe; } var mix = ( this.time % frameTime ) / frameTime; if ( this.directionBackwards ) { mix = 1 - mix; } this.morphTargetInfluences[ this.currentKeyframe ] = mix; this.morphTargetInfluences[ this.lastKeyframe ] = 1 - mix; }; THREE.MorphAnimMesh.prototype.clone = function ( object ) { if ( object === undefined ) object = new THREE.MorphAnimMesh( this.geometry, this.material ); object.duration = this.duration; object.mirroredLoop = this.mirroredLoop; object.time = this.time; object.lastKeyframe = this.lastKeyframe; object.currentKeyframe = this.currentKeyframe; object.direction = this.direction; object.directionBackwards = this.directionBackwards; THREE.Mesh.prototype.clone.call( this, object ); return object; }; /** * @author mikael emtinger / http://gomo.se/ * @author alteredq / http://alteredqualia.com/ * @author mrdoob / http://mrdoob.com/ */ THREE.LOD = function () { THREE.Object3D.call( this ); this.objects = []; }; THREE.LOD.prototype = Object.create( THREE.Object3D.prototype ); THREE.LOD.prototype.addLevel = function ( object, distance ) { if ( distance === undefined ) distance = 0; distance = Math.abs( distance ); for ( var l = 0; l < this.objects.length; l ++ ) { if ( distance < this.objects[ l ].distance ) { break; } } this.objects.splice( l, 0, { distance: distance, object: object } ); this.add( object ); }; THREE.LOD.prototype.getObjectForDistance = function ( distance ) { for ( var i = 1, l = this.objects.length; i < l; i ++ ) { if ( distance < this.objects[ i ].distance ) { break; } } return this.objects[ i - 1 ].object; }; THREE.LOD.prototype.update = function () { var v1 = new THREE.Vector3(); var v2 = new THREE.Vector3(); return function ( camera ) { if ( this.objects.length > 1 ) { v1.setFromMatrixPosition( camera.matrixWorld ); v2.setFromMatrixPosition( this.matrixWorld ); var distance = v1.distanceTo( v2 ); this.objects[ 0 ].object.visible = true; for ( var i = 1, l = this.objects.length; i < l; i ++ ) { if ( distance >= this.objects[ i ].distance ) { this.objects[ i - 1 ].object.visible = false; this.objects[ i ].object.visible = true; } else { break; } } for( ; i < l; i ++ ) { this.objects[ i ].object.visible = false; } } }; }(); THREE.LOD.prototype.clone = function ( object ) { if ( object === undefined ) object = new THREE.LOD(); THREE.Object3D.prototype.clone.call( this, object ); for ( var i = 0, l = this.objects.length; i < l; i ++ ) { var x = this.objects[i].object.clone(); x.visible = i === 0; object.addLevel( x, this.objects[i].distance ); } return object; }; /** * @author mikael emtinger / http://gomo.se/ * @author alteredq / http://alteredqualia.com/ */ THREE.Sprite = function ( material ) { THREE.Object3D.call( this ); this.material = ( material !== undefined ) ? material : new THREE.SpriteMaterial(); }; THREE.Sprite.prototype = Object.create( THREE.Object3D.prototype ); /* * Custom update matrix */ THREE.Sprite.prototype.updateMatrix = function () { this.matrix.compose( this.position, this.quaternion, this.scale ); this.matrixWorldNeedsUpdate = true; }; THREE.Sprite.prototype.clone = function ( object ) { if ( object === undefined ) object = new THREE.Sprite( this.material ); THREE.Object3D.prototype.clone.call( this, object ); return object; }; // Backwards compatibility THREE.Particle = THREE.Sprite; /** * @author mrdoob / http://mrdoob.com/ */ THREE.Scene = function () { THREE.Object3D.call( this ); this.fog = null; this.overrideMaterial = null; this.autoUpdate = true; // checked by the renderer this.matrixAutoUpdate = false; this.__lights = []; this.__objectsAdded = []; this.__objectsRemoved = []; }; THREE.Scene.prototype = Object.create( THREE.Object3D.prototype ); THREE.Scene.prototype.__addObject = function ( object ) { if ( object instanceof THREE.Light ) { if ( this.__lights.indexOf( object ) === - 1 ) { this.__lights.push( object ); } if ( object.target && object.target.parent === undefined ) { this.add( object.target ); } } else if ( !( object instanceof THREE.Camera || object instanceof THREE.Bone ) ) { this.__objectsAdded.push( object ); // check if previously removed var i = this.__objectsRemoved.indexOf( object ); if ( i !== -1 ) { this.__objectsRemoved.splice( i, 1 ); } } this.dispatchEvent( { type: 'objectAdded', object: object } ); object.dispatchEvent( { type: 'addedToScene', scene: this } ); for ( var c = 0; c < object.children.length; c ++ ) { this.__addObject( object.children[ c ] ); } }; THREE.Scene.prototype.__removeObject = function ( object ) { if ( object instanceof THREE.Light ) { var i = this.__lights.indexOf( object ); if ( i !== -1 ) { this.__lights.splice( i, 1 ); } if ( object.shadowCascadeArray ) { for ( var x = 0; x < object.shadowCascadeArray.length; x ++ ) { this.__removeObject( object.shadowCascadeArray[ x ] ); } } } else if ( !( object instanceof THREE.Camera ) ) { this.__objectsRemoved.push( object ); // check if previously added var i = this.__objectsAdded.indexOf( object ); if ( i !== -1 ) { this.__objectsAdded.splice( i, 1 ); } } this.dispatchEvent( { type: 'objectRemoved', object: object } ); object.dispatchEvent( { type: 'removedFromScene', scene: this } ); for ( var c = 0; c < object.children.length; c ++ ) { this.__removeObject( object.children[ c ] ); } }; THREE.Scene.prototype.clone = function ( object ) { if ( object === undefined ) object = new THREE.Scene(); THREE.Object3D.prototype.clone.call(this, object); if ( this.fog !== null ) object.fog = this.fog.clone(); if ( this.overrideMaterial !== null ) object.overrideMaterial = this.overrideMaterial.clone(); object.autoUpdate = this.autoUpdate; object.matrixAutoUpdate = this.matrixAutoUpdate; return object; }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ THREE.Fog = function ( color, near, far ) { this.name = ''; this.color = new THREE.Color( color ); this.near = ( near !== undefined ) ? near : 1; this.far = ( far !== undefined ) ? far : 1000; }; THREE.Fog.prototype.clone = function () { return new THREE.Fog( this.color.getHex(), this.near, this.far ); }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ THREE.FogExp2 = function ( color, density ) { this.name = ''; this.color = new THREE.Color( color ); this.density = ( density !== undefined ) ? density : 0.00025; }; THREE.FogExp2.prototype.clone = function () { return new THREE.FogExp2( this.color.getHex(), this.density ); }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.CanvasRenderer = function ( parameters ) { console.log( 'THREE.CanvasRenderer', THREE.REVISION ); var smoothstep = THREE.Math.smoothstep; parameters = parameters || {}; var _this = this, _renderData, _elements, _lights, _projector = new THREE.Projector(), _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ), _canvasWidth = _canvas.width, _canvasHeight = _canvas.height, _canvasWidthHalf = Math.floor( _canvasWidth / 2 ), _canvasHeightHalf = Math.floor( _canvasHeight / 2 ), _context = _canvas.getContext( '2d' ), _clearColor = new THREE.Color( 0x000000 ), _clearAlpha = 0, _contextGlobalAlpha = 1, _contextGlobalCompositeOperation = 0, _contextStrokeStyle = null, _contextFillStyle = null, _contextLineWidth = null, _contextLineCap = null, _contextLineJoin = null, _contextDashSize = null, _contextGapSize = 0, _camera, _v1, _v2, _v3, _v4, _v5 = new THREE.RenderableVertex(), _v6 = new THREE.RenderableVertex(), _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y, _v5x, _v5y, _v6x, _v6y, _color = new THREE.Color(), _color1 = new THREE.Color(), _color2 = new THREE.Color(), _color3 = new THREE.Color(), _color4 = new THREE.Color(), _diffuseColor = new THREE.Color(), _emissiveColor = new THREE.Color(), _lightColor = new THREE.Color(), _patterns = {}, _near, _far, _image, _uvs, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, _clipBox = new THREE.Box2(), _clearBox = new THREE.Box2(), _elemBox = new THREE.Box2(), _ambientLight = new THREE.Color(), _directionalLights = new THREE.Color(), _pointLights = new THREE.Color(), _vector3 = new THREE.Vector3(), // Needed for PointLight _normal = new THREE.Vector3(), _normalViewMatrix = new THREE.Matrix3(), _pixelMap, _pixelMapContext, _pixelMapImage, _pixelMapData, _gradientMap, _gradientMapContext, _gradientMapQuality = 16; _pixelMap = document.createElement( 'canvas' ); _pixelMap.width = _pixelMap.height = 2; _pixelMapContext = _pixelMap.getContext( '2d' ); _pixelMapContext.fillStyle = 'rgba(0,0,0,1)'; _pixelMapContext.fillRect( 0, 0, 2, 2 ); _pixelMapImage = _pixelMapContext.getImageData( 0, 0, 2, 2 ); _pixelMapData = _pixelMapImage.data; _gradientMap = document.createElement( 'canvas' ); _gradientMap.width = _gradientMap.height = _gradientMapQuality; _gradientMapContext = _gradientMap.getContext( '2d' ); _gradientMapContext.translate( - _gradientMapQuality / 2, - _gradientMapQuality / 2 ); _gradientMapContext.scale( _gradientMapQuality, _gradientMapQuality ); _gradientMapQuality --; // Fix UVs // dash+gap fallbacks for Firefox and everything else if ( _context.setLineDash === undefined ) { if ( _context.mozDash !== undefined ) { _context.setLineDash = function ( values ) { _context.mozDash = values[ 0 ] !== null ? values : null; } } else { _context.setLineDash = function () {} } } this.domElement = _canvas; this.devicePixelRatio = parameters.devicePixelRatio !== undefined ? parameters.devicePixelRatio : self.devicePixelRatio !== undefined ? self.devicePixelRatio : 1; this.autoClear = true; this.sortObjects = true; this.sortElements = true; this.info = { render: { vertices: 0, faces: 0 } } // WebGLRenderer compatibility this.supportsVertexTextures = function () {}; this.setFaceCulling = function () {}; this.setSize = function ( width, height, updateStyle ) { _canvasWidth = width * this.devicePixelRatio; _canvasHeight = height * this.devicePixelRatio; _canvasWidthHalf = Math.floor( _canvasWidth / 2 ); _canvasHeightHalf = Math.floor( _canvasHeight / 2 ); _canvas.width = _canvasWidth; _canvas.height = _canvasHeight; if ( this.devicePixelRatio !== 1 && updateStyle !== false ) { _canvas.style.width = width + 'px'; _canvas.style.height = height + 'px'; } _clipBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf ), _clipBox.max.set( _canvasWidthHalf, _canvasHeightHalf ); _clearBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf ); _clearBox.max.set( _canvasWidthHalf, _canvasHeightHalf ); _contextGlobalAlpha = 1; _contextGlobalCompositeOperation = 0; _contextStrokeStyle = null; _contextFillStyle = null; _contextLineWidth = null; _contextLineCap = null; _contextLineJoin = null; }; this.setClearColor = function ( color, alpha ) { _clearColor.set( color ); _clearAlpha = alpha !== undefined ? alpha : 1; _clearBox.min.set( - _canvasWidthHalf, - _canvasHeightHalf ); _clearBox.max.set( _canvasWidthHalf, _canvasHeightHalf ); }; this.setClearColorHex = function ( hex, alpha ) { console.warn( 'DEPRECATED: .setClearColorHex() is being removed. Use .setClearColor() instead.' ); this.setClearColor( hex, alpha ); }; this.getMaxAnisotropy = function () { return 0; }; this.clear = function () { _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf ); if ( _clearBox.empty() === false ) { _clearBox.intersect( _clipBox ); _clearBox.expandByScalar( 2 ); if ( _clearAlpha < 1 ) { _context.clearRect( _clearBox.min.x | 0, _clearBox.min.y | 0, ( _clearBox.max.x - _clearBox.min.x ) | 0, ( _clearBox.max.y - _clearBox.min.y ) | 0 ); } if ( _clearAlpha > 0 ) { setBlending( THREE.NormalBlending ); setOpacity( 1 ); setFillStyle( 'rgba(' + Math.floor( _clearColor.r * 255 ) + ',' + Math.floor( _clearColor.g * 255 ) + ',' + Math.floor( _clearColor.b * 255 ) + ',' + _clearAlpha + ')' ); _context.fillRect( _clearBox.min.x | 0, _clearBox.min.y | 0, ( _clearBox.max.x - _clearBox.min.x ) | 0, ( _clearBox.max.y - _clearBox.min.y ) | 0 ); } _clearBox.makeEmpty(); } }; // compatibility this.clearColor = function () {}; this.clearDepth = function () {}; this.clearStencil = function () {}; this.render = function ( scene, camera ) { if ( camera instanceof THREE.Camera === false ) { console.error( 'THREE.CanvasRenderer.render: camera is not an instance of THREE.Camera.' ); return; } if ( this.autoClear === true ) this.clear(); _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf ); _this.info.render.vertices = 0; _this.info.render.faces = 0; _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements ); _elements = _renderData.elements; _lights = _renderData.lights; _camera = camera; _normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse ); /* DEBUG setFillStyle( 'rgba( 0, 255, 255, 0.5 )' ); _context.fillRect( _clipBox.min.x, _clipBox.min.y, _clipBox.max.x - _clipBox.min.x, _clipBox.max.y - _clipBox.min.y ); */ calculateLights(); for ( var e = 0, el = _elements.length; e < el; e ++ ) { var element = _elements[ e ]; var material = element.material; if ( material === undefined || material.visible === false ) continue; _elemBox.makeEmpty(); if ( element instanceof THREE.RenderableSprite ) { _v1 = element; _v1.x *= _canvasWidthHalf; _v1.y *= _canvasHeightHalf; renderSprite( _v1, element, material ); } else if ( element instanceof THREE.RenderableLine ) { _v1 = element.v1; _v2 = element.v2; _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf; _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf; _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen ] ); if ( _clipBox.isIntersectionBox( _elemBox ) === true ) { renderLine( _v1, _v2, element, material ); } } else if ( element instanceof THREE.RenderableFace3 ) { _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue; if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue; if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue; _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf; _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf; _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf; if ( material.overdraw > 0 ) { expand( _v1.positionScreen, _v2.positionScreen, material.overdraw ); expand( _v2.positionScreen, _v3.positionScreen, material.overdraw ); expand( _v3.positionScreen, _v1.positionScreen, material.overdraw ); } _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen, _v3.positionScreen ] ); if ( _clipBox.isIntersectionBox( _elemBox ) === true ) { renderFace3( _v1, _v2, _v3, 0, 1, 2, element, material ); } } /* DEBUG setLineWidth( 1 ); setStrokeStyle( 'rgba( 0, 255, 0, 0.5 )' ); _context.strokeRect( _elemBox.min.x, _elemBox.min.y, _elemBox.max.x - _elemBox.min.x, _elemBox.max.y - _elemBox.min.y ); */ _clearBox.union( _elemBox ); } /* DEBUG setLineWidth( 1 ); setStrokeStyle( 'rgba( 255, 0, 0, 0.5 )' ); _context.strokeRect( _clearBox.min.x, _clearBox.min.y, _clearBox.max.x - _clearBox.min.x, _clearBox.max.y - _clearBox.min.y ); */ _context.setTransform( 1, 0, 0, 1, 0, 0 ); }; // function calculateLights() { _ambientLight.setRGB( 0, 0, 0 ); _directionalLights.setRGB( 0, 0, 0 ); _pointLights.setRGB( 0, 0, 0 ); for ( var l = 0, ll = _lights.length; l < ll; l ++ ) { var light = _lights[ l ]; var lightColor = light.color; if ( light instanceof THREE.AmbientLight ) { _ambientLight.add( lightColor ); } else if ( light instanceof THREE.DirectionalLight ) { // for sprites _directionalLights.add( lightColor ); } else if ( light instanceof THREE.PointLight ) { // for sprites _pointLights.add( lightColor ); } } } function calculateLight( position, normal, color ) { for ( var l = 0, ll = _lights.length; l < ll; l ++ ) { var light = _lights[ l ]; _lightColor.copy( light.color ); if ( light instanceof THREE.DirectionalLight ) { var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize(); var amount = normal.dot( lightPosition ); if ( amount <= 0 ) continue; amount *= light.intensity; color.add( _lightColor.multiplyScalar( amount ) ); } else if ( light instanceof THREE.PointLight ) { var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ); var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() ); if ( amount <= 0 ) continue; amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 ); if ( amount == 0 ) continue; amount *= light.intensity; color.add( _lightColor.multiplyScalar( amount ) ); } } } function renderSprite( v1, element, material ) { setOpacity( material.opacity ); setBlending( material.blending ); var scaleX = element.scale.x * _canvasWidthHalf; var scaleY = element.scale.y * _canvasHeightHalf; var dist = 0.5 * Math.sqrt( scaleX * scaleX + scaleY * scaleY ); // allow for rotated sprite _elemBox.min.set( v1.x - dist, v1.y - dist ); _elemBox.max.set( v1.x + dist, v1.y + dist ); if ( _clipBox.isIntersectionBox( _elemBox ) === false ) { _elemBox.makeEmpty(); return; } if ( material instanceof THREE.SpriteMaterial || material instanceof THREE.ParticleSystemMaterial ) { // Backwards compatibility var texture = material.map; if ( texture !== null ) { if ( texture.hasEventListener( 'update', onTextureUpdate ) === false ) { if ( texture.image !== undefined && texture.image.width > 0 ) { textureToPattern( texture ); } texture.addEventListener( 'update', onTextureUpdate ); } var pattern = _patterns[ texture.id ]; if ( pattern !== undefined ) { setFillStyle( pattern ); } else { setFillStyle( 'rgba( 0, 0, 0, 1 )' ); } // var bitmap = texture.image; var ox = bitmap.width * texture.offset.x; var oy = bitmap.height * texture.offset.y; var sx = bitmap.width * texture.repeat.x; var sy = bitmap.height * texture.repeat.y; var cx = scaleX / sx; var cy = scaleY / sy; _context.save(); _context.translate( v1.x, v1.y ); if ( material.rotation !== 0 ) _context.rotate( material.rotation ); _context.translate( - scaleX / 2, - scaleY / 2 ); _context.scale( cx, cy ); _context.translate( - ox, - oy ); _context.fillRect( ox, oy, sx, sy ); _context.restore(); } else { // no texture setFillStyle( material.color.getStyle() ); _context.save(); _context.translate( v1.x, v1.y ); if ( material.rotation !== 0 ) _context.rotate( material.rotation ); _context.scale( scaleX, - scaleY ); _context.fillRect( - 0.5, - 0.5, 1, 1 ); _context.restore(); } } else if ( material instanceof THREE.SpriteCanvasMaterial ) { setStrokeStyle( material.color.getStyle() ); setFillStyle( material.color.getStyle() ); _context.save(); _context.translate( v1.x, v1.y ); if ( material.rotation !== 0 ) _context.rotate( material.rotation ); _context.scale( scaleX, scaleY ); material.program( _context ); _context.restore(); } /* DEBUG setStrokeStyle( 'rgb(255,255,0)' ); _context.beginPath(); _context.moveTo( v1.x - 10, v1.y ); _context.lineTo( v1.x + 10, v1.y ); _context.moveTo( v1.x, v1.y - 10 ); _context.lineTo( v1.x, v1.y + 10 ); _context.stroke(); */ } function renderLine( v1, v2, element, material ) { setOpacity( material.opacity ); setBlending( material.blending ); _context.beginPath(); _context.moveTo( v1.positionScreen.x, v1.positionScreen.y ); _context.lineTo( v2.positionScreen.x, v2.positionScreen.y ); if ( material instanceof THREE.LineBasicMaterial ) { setLineWidth( material.linewidth ); setLineCap( material.linecap ); setLineJoin( material.linejoin ); if ( material.vertexColors !== THREE.VertexColors ) { setStrokeStyle( material.color.getStyle() ); } else { var colorStyle1 = element.vertexColors[0].getStyle(); var colorStyle2 = element.vertexColors[1].getStyle(); if ( colorStyle1 === colorStyle2 ) { setStrokeStyle( colorStyle1 ); } else { try { var grad = _context.createLinearGradient( v1.positionScreen.x, v1.positionScreen.y, v2.positionScreen.x, v2.positionScreen.y ); grad.addColorStop( 0, colorStyle1 ); grad.addColorStop( 1, colorStyle2 ); } catch ( exception ) { grad = colorStyle1; } setStrokeStyle( grad ); } } _context.stroke(); _elemBox.expandByScalar( material.linewidth * 2 ); } else if ( material instanceof THREE.LineDashedMaterial ) { setLineWidth( material.linewidth ); setLineCap( material.linecap ); setLineJoin( material.linejoin ); setStrokeStyle( material.color.getStyle() ); setDashAndGap( material.dashSize, material.gapSize ); _context.stroke(); _elemBox.expandByScalar( material.linewidth * 2 ); setDashAndGap( null, null ); } } function renderFace3( v1, v2, v3, uv1, uv2, uv3, element, material ) { _this.info.render.vertices += 3; _this.info.render.faces ++; setOpacity( material.opacity ); setBlending( material.blending ); _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y; _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y; _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y; drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y ); if ( ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) && material.map === null ) { _diffuseColor.copy( material.color ); _emissiveColor.copy( material.emissive ); if ( material.vertexColors === THREE.FaceColors ) { _diffuseColor.multiply( element.color ); } if ( material.wireframe === false && material.shading === THREE.SmoothShading && element.vertexNormalsLength === 3 ) { _color1.copy( _ambientLight ); _color2.copy( _ambientLight ); _color3.copy( _ambientLight ); calculateLight( element.v1.positionWorld, element.vertexNormalsModel[ 0 ], _color1 ); calculateLight( element.v2.positionWorld, element.vertexNormalsModel[ 1 ], _color2 ); calculateLight( element.v3.positionWorld, element.vertexNormalsModel[ 2 ], _color3 ); _color1.multiply( _diffuseColor ).add( _emissiveColor ); _color2.multiply( _diffuseColor ).add( _emissiveColor ); _color3.multiply( _diffuseColor ).add( _emissiveColor ); _color4.addColors( _color2, _color3 ).multiplyScalar( 0.5 ); _image = getGradientTexture( _color1, _color2, _color3, _color4 ); clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image ); } else { _color.copy( _ambientLight ); calculateLight( element.centroidModel, element.normalModel, _color ); _color.multiply( _diffuseColor ).add( _emissiveColor ); material.wireframe === true ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color ); } } else if ( material instanceof THREE.MeshBasicMaterial || material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) { if ( material.map !== null ) { if ( material.map.mapping instanceof THREE.UVMapping ) { _uvs = element.uvs[ 0 ]; patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uvs[ uv1 ].x, _uvs[ uv1 ].y, _uvs[ uv2 ].x, _uvs[ uv2 ].y, _uvs[ uv3 ].x, _uvs[ uv3 ].y, material.map ); } } else if ( material.envMap !== null ) { if ( material.envMap.mapping instanceof THREE.SphericalReflectionMapping ) { _normal.copy( element.vertexNormalsModel[ uv1 ] ).applyMatrix3( _normalViewMatrix ); _uv1x = 0.5 * _normal.x + 0.5; _uv1y = 0.5 * _normal.y + 0.5; _normal.copy( element.vertexNormalsModel[ uv2 ] ).applyMatrix3( _normalViewMatrix ); _uv2x = 0.5 * _normal.x + 0.5; _uv2y = 0.5 * _normal.y + 0.5; _normal.copy( element.vertexNormalsModel[ uv3 ] ).applyMatrix3( _normalViewMatrix ); _uv3x = 0.5 * _normal.x + 0.5; _uv3y = 0.5 * _normal.y + 0.5; patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, material.envMap ); }/* else if ( material.envMap.mapping === THREE.SphericalRefractionMapping ) { }*/ } else { _color.copy( material.color ); if ( material.vertexColors === THREE.FaceColors ) { _color.multiply( element.color ); } material.wireframe === true ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color ); } } else if ( material instanceof THREE.MeshDepthMaterial ) { _near = _camera.near; _far = _camera.far; _color1.r = _color1.g = _color1.b = 1 - smoothstep( v1.positionScreen.z * v1.positionScreen.w, _near, _far ); _color2.r = _color2.g = _color2.b = 1 - smoothstep( v2.positionScreen.z * v2.positionScreen.w, _near, _far ); _color3.r = _color3.g = _color3.b = 1 - smoothstep( v3.positionScreen.z * v3.positionScreen.w, _near, _far ); _color4.addColors( _color2, _color3 ).multiplyScalar( 0.5 ); _image = getGradientTexture( _color1, _color2, _color3, _color4 ); clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image ); } else if ( material instanceof THREE.MeshNormalMaterial ) { if ( material.shading === THREE.FlatShading ) { _normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix ); _color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 ); material.wireframe === true ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color ); } else if ( material.shading === THREE.SmoothShading ) { _normal.copy( element.vertexNormalsModel[ uv1 ] ).applyMatrix3( _normalViewMatrix ); _color1.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 ); _normal.copy( element.vertexNormalsModel[ uv2 ] ).applyMatrix3( _normalViewMatrix ); _color2.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 ); _normal.copy( element.vertexNormalsModel[ uv3 ] ).applyMatrix3( _normalViewMatrix ); _color3.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 ); _color4.addColors( _color2, _color3 ).multiplyScalar( 0.5 ); _image = getGradientTexture( _color1, _color2, _color3, _color4 ); clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image ); } } } // function drawTriangle( x0, y0, x1, y1, x2, y2 ) { _context.beginPath(); _context.moveTo( x0, y0 ); _context.lineTo( x1, y1 ); _context.lineTo( x2, y2 ); _context.closePath(); } function strokePath( color, linewidth, linecap, linejoin ) { setLineWidth( linewidth ); setLineCap( linecap ); setLineJoin( linejoin ); setStrokeStyle( color.getStyle() ); _context.stroke(); _elemBox.expandByScalar( linewidth * 2 ); } function fillPath( color ) { setFillStyle( color.getStyle() ); _context.fill(); } function onTextureUpdate ( event ) { textureToPattern( event.target ); } function textureToPattern( texture ) { var repeatX = texture.wrapS === THREE.RepeatWrapping; var repeatY = texture.wrapT === THREE.RepeatWrapping; var image = texture.image; var canvas = document.createElement( 'canvas' ); canvas.width = image.width; canvas.height = image.height; var context = canvas.getContext( '2d' ); context.setTransform( 1, 0, 0, - 1, 0, image.height ); context.drawImage( image, 0, 0 ); _patterns[ texture.id ] = _context.createPattern( canvas, repeatX === true && repeatY === true ? 'repeat' : repeatX === true && repeatY === false ? 'repeat-x' : repeatX === false && repeatY === true ? 'repeat-y' : 'no-repeat' ); } function patternPath( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, texture ) { if ( texture instanceof THREE.DataTexture ) return; if ( texture.hasEventListener( 'update', onTextureUpdate ) === false ) { if ( texture.image !== undefined && texture.image.width > 0 ) { textureToPattern( texture ); } texture.addEventListener( 'update', onTextureUpdate ); } var pattern = _patterns[ texture.id ]; if ( pattern !== undefined ) { setFillStyle( pattern ); } else { setFillStyle( 'rgba(0,0,0,1)' ); _context.fill(); return; } // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120 var a, b, c, d, e, f, det, idet, offsetX = texture.offset.x / texture.repeat.x, offsetY = texture.offset.y / texture.repeat.y, width = texture.image.width * texture.repeat.x, height = texture.image.height * texture.repeat.y; u0 = ( u0 + offsetX ) * width; v0 = ( v0 + offsetY ) * height; u1 = ( u1 + offsetX ) * width; v1 = ( v1 + offsetY ) * height; u2 = ( u2 + offsetX ) * width; v2 = ( v2 + offsetY ) * height; x1 -= x0; y1 -= y0; x2 -= x0; y2 -= y0; u1 -= u0; v1 -= v0; u2 -= u0; v2 -= v0; det = u1 * v2 - u2 * v1; if ( det === 0 ) return; idet = 1 / det; a = ( v2 * x1 - v1 * x2 ) * idet; b = ( v2 * y1 - v1 * y2 ) * idet; c = ( u1 * x2 - u2 * x1 ) * idet; d = ( u1 * y2 - u2 * y1 ) * idet; e = x0 - a * u0 - c * v0; f = y0 - b * u0 - d * v0; _context.save(); _context.transform( a, b, c, d, e, f ); _context.fill(); _context.restore(); } function clipImage( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, image ) { // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120 var a, b, c, d, e, f, det, idet, width = image.width - 1, height = image.height - 1; u0 *= width; v0 *= height; u1 *= width; v1 *= height; u2 *= width; v2 *= height; x1 -= x0; y1 -= y0; x2 -= x0; y2 -= y0; u1 -= u0; v1 -= v0; u2 -= u0; v2 -= v0; det = u1 * v2 - u2 * v1; idet = 1 / det; a = ( v2 * x1 - v1 * x2 ) * idet; b = ( v2 * y1 - v1 * y2 ) * idet; c = ( u1 * x2 - u2 * x1 ) * idet; d = ( u1 * y2 - u2 * y1 ) * idet; e = x0 - a * u0 - c * v0; f = y0 - b * u0 - d * v0; _context.save(); _context.transform( a, b, c, d, e, f ); _context.clip(); _context.drawImage( image, 0, 0 ); _context.restore(); } function getGradientTexture( color1, color2, color3, color4 ) { // http://mrdoob.com/blog/post/710 _pixelMapData[ 0 ] = ( color1.r * 255 ) | 0; _pixelMapData[ 1 ] = ( color1.g * 255 ) | 0; _pixelMapData[ 2 ] = ( color1.b * 255 ) | 0; _pixelMapData[ 4 ] = ( color2.r * 255 ) | 0; _pixelMapData[ 5 ] = ( color2.g * 255 ) | 0; _pixelMapData[ 6 ] = ( color2.b * 255 ) | 0; _pixelMapData[ 8 ] = ( color3.r * 255 ) | 0; _pixelMapData[ 9 ] = ( color3.g * 255 ) | 0; _pixelMapData[ 10 ] = ( color3.b * 255 ) | 0; _pixelMapData[ 12 ] = ( color4.r * 255 ) | 0; _pixelMapData[ 13 ] = ( color4.g * 255 ) | 0; _pixelMapData[ 14 ] = ( color4.b * 255 ) | 0; _pixelMapContext.putImageData( _pixelMapImage, 0, 0 ); _gradientMapContext.drawImage( _pixelMap, 0, 0 ); return _gradientMap; } // Hide anti-alias gaps function expand( v1, v2, pixels ) { var x = v2.x - v1.x, y = v2.y - v1.y, det = x * x + y * y, idet; if ( det === 0 ) return; idet = pixels / Math.sqrt( det ); x *= idet; y *= idet; v2.x += x; v2.y += y; v1.x -= x; v1.y -= y; } // Context cached methods. function setOpacity( value ) { if ( _contextGlobalAlpha !== value ) { _context.globalAlpha = value; _contextGlobalAlpha = value; } } function setBlending( value ) { if ( _contextGlobalCompositeOperation !== value ) { if ( value === THREE.NormalBlending ) { _context.globalCompositeOperation = 'source-over'; } else if ( value === THREE.AdditiveBlending ) { _context.globalCompositeOperation = 'lighter'; } else if ( value === THREE.SubtractiveBlending ) { _context.globalCompositeOperation = 'darker'; } _contextGlobalCompositeOperation = value; } } function setLineWidth( value ) { if ( _contextLineWidth !== value ) { _context.lineWidth = value; _contextLineWidth = value; } } function setLineCap( value ) { // "butt", "round", "square" if ( _contextLineCap !== value ) { _context.lineCap = value; _contextLineCap = value; } } function setLineJoin( value ) { // "round", "bevel", "miter" if ( _contextLineJoin !== value ) { _context.lineJoin = value; _contextLineJoin = value; } } function setStrokeStyle( value ) { if ( _contextStrokeStyle !== value ) { _context.strokeStyle = value; _contextStrokeStyle = value; } } function setFillStyle( value ) { if ( _contextFillStyle !== value ) { _context.fillStyle = value; _contextFillStyle = value; } } function setDashAndGap( dashSizeValue, gapSizeValue ) { if ( _contextDashSize !== dashSizeValue || _contextGapSize !== gapSizeValue ) { _context.setLineDash( [ dashSizeValue, gapSizeValue ] ); _contextDashSize = dashSizeValue; _contextGapSize = gapSizeValue; } } }; /** * Shader chunks for WebLG Shader library * * @author alteredq / http://alteredqualia.com/ * @author mrdoob / http://mrdoob.com/ * @author mikael emtinger / http://gomo.se/ */ THREE.ShaderChunk = { // FOG fog_pars_fragment: [ "#ifdef USE_FOG", "uniform vec3 fogColor;", "#ifdef FOG_EXP2", "uniform float fogDensity;", "#else", "uniform float fogNear;", "uniform float fogFar;", "#endif", "#endif" ].join("\n"), fog_fragment: [ "#ifdef USE_FOG", "float depth = gl_FragCoord.z / gl_FragCoord.w;", "#ifdef FOG_EXP2", "const float LOG2 = 1.442695;", "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );", "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );", "#else", "float fogFactor = smoothstep( fogNear, fogFar, depth );", "#endif", "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );", "#endif" ].join("\n"), // ENVIRONMENT MAP envmap_pars_fragment: [ "#ifdef USE_ENVMAP", "uniform float reflectivity;", "uniform samplerCube envMap;", "uniform float flipEnvMap;", "uniform int combine;", "#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP )", "uniform bool useRefract;", "uniform float refractionRatio;", "#else", "varying vec3 vReflect;", "#endif", "#endif" ].join("\n"), envmap_fragment: [ "#ifdef USE_ENVMAP", "vec3 reflectVec;", "#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP )", "vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );", "if ( useRefract ) {", "reflectVec = refract( cameraToVertex, normal, refractionRatio );", "} else { ", "reflectVec = reflect( cameraToVertex, normal );", "}", "#else", "reflectVec = vReflect;", "#endif", "#ifdef DOUBLE_SIDED", "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );", "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );", "#else", "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );", "#endif", "#ifdef GAMMA_INPUT", "cubeColor.xyz *= cubeColor.xyz;", "#endif", "if ( combine == 1 ) {", "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularStrength * reflectivity );", "} else if ( combine == 2 ) {", "gl_FragColor.xyz += cubeColor.xyz * specularStrength * reflectivity;", "} else {", "gl_FragColor.xyz = mix( gl_FragColor.xyz, gl_FragColor.xyz * cubeColor.xyz, specularStrength * reflectivity );", "}", "#endif" ].join("\n"), envmap_pars_vertex: [ "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP )", "varying vec3 vReflect;", "uniform float refractionRatio;", "uniform bool useRefract;", "#endif" ].join("\n"), worldpos_vertex : [ "#if defined( USE_ENVMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP )", "#ifdef USE_SKINNING", "vec4 worldPosition = modelMatrix * skinned;", "#endif", "#if defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )", "vec4 worldPosition = modelMatrix * vec4( morphed, 1.0 );", "#endif", "#if ! defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )", "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );", "#endif", "#endif" ].join("\n"), envmap_vertex : [ "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP )", "vec3 worldNormal = mat3( modelMatrix[ 0 ].xyz, modelMatrix[ 1 ].xyz, modelMatrix[ 2 ].xyz ) * objectNormal;", "worldNormal = normalize( worldNormal );", "vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition );", "if ( useRefract ) {", "vReflect = refract( cameraToVertex, worldNormal, refractionRatio );", "} else {", "vReflect = reflect( cameraToVertex, worldNormal );", "}", "#endif" ].join("\n"), // COLOR MAP (particles) map_particle_pars_fragment: [ "#ifdef USE_MAP", "uniform sampler2D map;", "#endif" ].join("\n"), map_particle_fragment: [ "#ifdef USE_MAP", "gl_FragColor = gl_FragColor * texture2D( map, vec2( gl_PointCoord.x, 1.0 - gl_PointCoord.y ) );", "#endif" ].join("\n"), // COLOR MAP (triangles) map_pars_vertex: [ "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )", "varying vec2 vUv;", "uniform vec4 offsetRepeat;", "#endif" ].join("\n"), map_pars_fragment: [ "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )", "varying vec2 vUv;", "#endif", "#ifdef USE_MAP", "uniform sampler2D map;", "#endif" ].join("\n"), map_vertex: [ "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )", "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;", "#endif" ].join("\n"), map_fragment: [ "#ifdef USE_MAP", "vec4 texelColor = texture2D( map, vUv );", "#ifdef GAMMA_INPUT", "texelColor.xyz *= texelColor.xyz;", "#endif", "gl_FragColor = gl_FragColor * texelColor;", "#endif" ].join("\n"), // LIGHT MAP lightmap_pars_fragment: [ "#ifdef USE_LIGHTMAP", "varying vec2 vUv2;", "uniform sampler2D lightMap;", "#endif" ].join("\n"), lightmap_pars_vertex: [ "#ifdef USE_LIGHTMAP", "varying vec2 vUv2;", "#endif" ].join("\n"), lightmap_fragment: [ "#ifdef USE_LIGHTMAP", "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );", "#endif" ].join("\n"), lightmap_vertex: [ "#ifdef USE_LIGHTMAP", "vUv2 = uv2;", "#endif" ].join("\n"), // BUMP MAP bumpmap_pars_fragment: [ "#ifdef USE_BUMPMAP", "uniform sampler2D bumpMap;", "uniform float bumpScale;", // Derivative maps - bump mapping unparametrized surfaces by Morten Mikkelsen // http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2) "vec2 dHdxy_fwd() {", "vec2 dSTdx = dFdx( vUv );", "vec2 dSTdy = dFdy( vUv );", "float Hll = bumpScale * texture2D( bumpMap, vUv ).x;", "float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;", "float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;", "return vec2( dBx, dBy );", "}", "vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {", "vec3 vSigmaX = dFdx( surf_pos );", "vec3 vSigmaY = dFdy( surf_pos );", "vec3 vN = surf_norm;", // normalized "vec3 R1 = cross( vSigmaY, vN );", "vec3 R2 = cross( vN, vSigmaX );", "float fDet = dot( vSigmaX, R1 );", "vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );", "return normalize( abs( fDet ) * surf_norm - vGrad );", "}", "#endif" ].join("\n"), // NORMAL MAP normalmap_pars_fragment: [ "#ifdef USE_NORMALMAP", "uniform sampler2D normalMap;", "uniform vec2 normalScale;", // Per-Pixel Tangent Space Normal Mapping // http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html "vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm ) {", "vec3 q0 = dFdx( eye_pos.xyz );", "vec3 q1 = dFdy( eye_pos.xyz );", "vec2 st0 = dFdx( vUv.st );", "vec2 st1 = dFdy( vUv.st );", "vec3 S = normalize( q0 * st1.t - q1 * st0.t );", "vec3 T = normalize( -q0 * st1.s + q1 * st0.s );", "vec3 N = normalize( surf_norm );", "vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;", "mapN.xy = normalScale * mapN.xy;", "mat3 tsn = mat3( S, T, N );", "return normalize( tsn * mapN );", "}", "#endif" ].join("\n"), // SPECULAR MAP specularmap_pars_fragment: [ "#ifdef USE_SPECULARMAP", "uniform sampler2D specularMap;", "#endif" ].join("\n"), specularmap_fragment: [ "float specularStrength;", "#ifdef USE_SPECULARMAP", "vec4 texelSpecular = texture2D( specularMap, vUv );", "specularStrength = texelSpecular.r;", "#else", "specularStrength = 1.0;", "#endif" ].join("\n"), // LIGHTS LAMBERT lights_lambert_pars_vertex: [ "uniform vec3 ambient;", "uniform vec3 diffuse;", "uniform vec3 emissive;", "uniform vec3 ambientLightColor;", "#if MAX_DIR_LIGHTS > 0", "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];", "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];", "#endif", "#if MAX_HEMI_LIGHTS > 0", "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];", "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];", "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];", "#endif", "#if MAX_POINT_LIGHTS > 0", "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];", "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];", "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];", "#endif", "#if MAX_SPOT_LIGHTS > 0", "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];", "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];", "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];", "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];", "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];", "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];", "#endif", "#ifdef WRAP_AROUND", "uniform vec3 wrapRGB;", "#endif" ].join("\n"), lights_lambert_vertex: [ "vLightFront = vec3( 0.0 );", "#ifdef DOUBLE_SIDED", "vLightBack = vec3( 0.0 );", "#endif", "transformedNormal = normalize( transformedNormal );", "#if MAX_DIR_LIGHTS > 0", "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {", "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );", "vec3 dirVector = normalize( lDirection.xyz );", "float dotProduct = dot( transformedNormal, dirVector );", "vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );", "#ifdef DOUBLE_SIDED", "vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );", "#ifdef WRAP_AROUND", "vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );", "#endif", "#endif", "#ifdef WRAP_AROUND", "vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );", "directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );", "#ifdef DOUBLE_SIDED", "directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );", "#endif", "#endif", "vLightFront += directionalLightColor[ i ] * directionalLightWeighting;", "#ifdef DOUBLE_SIDED", "vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;", "#endif", "}", "#endif", "#if MAX_POINT_LIGHTS > 0", "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {", "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );", "vec3 lVector = lPosition.xyz - mvPosition.xyz;", "float lDistance = 1.0;", "if ( pointLightDistance[ i ] > 0.0 )", "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );", "lVector = normalize( lVector );", "float dotProduct = dot( transformedNormal, lVector );", "vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );", "#ifdef DOUBLE_SIDED", "vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );", "#ifdef WRAP_AROUND", "vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );", "#endif", "#endif", "#ifdef WRAP_AROUND", "vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );", "pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );", "#ifdef DOUBLE_SIDED", "pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );", "#endif", "#endif", "vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;", "#ifdef DOUBLE_SIDED", "vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;", "#endif", "}", "#endif", "#if MAX_SPOT_LIGHTS > 0", "for( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {", "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );", "vec3 lVector = lPosition.xyz - mvPosition.xyz;", "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - worldPosition.xyz ) );", "if ( spotEffect > spotLightAngleCos[ i ] ) {", "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );", "float lDistance = 1.0;", "if ( spotLightDistance[ i ] > 0.0 )", "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );", "lVector = normalize( lVector );", "float dotProduct = dot( transformedNormal, lVector );", "vec3 spotLightWeighting = vec3( max( dotProduct, 0.0 ) );", "#ifdef DOUBLE_SIDED", "vec3 spotLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );", "#ifdef WRAP_AROUND", "vec3 spotLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );", "#endif", "#endif", "#ifdef WRAP_AROUND", "vec3 spotLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );", "spotLightWeighting = mix( spotLightWeighting, spotLightWeightingHalf, wrapRGB );", "#ifdef DOUBLE_SIDED", "spotLightWeightingBack = mix( spotLightWeightingBack, spotLightWeightingHalfBack, wrapRGB );", "#endif", "#endif", "vLightFront += spotLightColor[ i ] * spotLightWeighting * lDistance * spotEffect;", "#ifdef DOUBLE_SIDED", "vLightBack += spotLightColor[ i ] * spotLightWeightingBack * lDistance * spotEffect;", "#endif", "}", "}", "#endif", "#if MAX_HEMI_LIGHTS > 0", "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {", "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );", "vec3 lVector = normalize( lDirection.xyz );", "float dotProduct = dot( transformedNormal, lVector );", "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;", "float hemiDiffuseWeightBack = -0.5 * dotProduct + 0.5;", "vLightFront += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );", "#ifdef DOUBLE_SIDED", "vLightBack += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeightBack );", "#endif", "}", "#endif", "vLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;", "#ifdef DOUBLE_SIDED", "vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;", "#endif" ].join("\n"), // LIGHTS PHONG lights_phong_pars_vertex: [ "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP ) || defined( USE_ENVMAP )", "varying vec3 vWorldPosition;", "#endif" ].join("\n"), lights_phong_vertex: [ "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP ) || defined( USE_ENVMAP )", "vWorldPosition = worldPosition.xyz;", "#endif" ].join("\n"), lights_phong_pars_fragment: [ "uniform vec3 ambientLightColor;", "#if MAX_DIR_LIGHTS > 0", "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];", "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];", "#endif", "#if MAX_HEMI_LIGHTS > 0", "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];", "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];", "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];", "#endif", "#if MAX_POINT_LIGHTS > 0", "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];", "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];", "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];", "#endif", "#if MAX_SPOT_LIGHTS > 0", "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];", "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];", "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];", "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];", "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];", "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];", "#endif", "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP ) || defined( USE_ENVMAP )", "varying vec3 vWorldPosition;", "#endif", "#ifdef WRAP_AROUND", "uniform vec3 wrapRGB;", "#endif", "varying vec3 vViewPosition;", "varying vec3 vNormal;" ].join("\n"), lights_phong_fragment: [ "vec3 normal = normalize( vNormal );", "vec3 viewPosition = normalize( vViewPosition );", "#ifdef DOUBLE_SIDED", "normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );", "#endif", "#ifdef USE_NORMALMAP", "normal = perturbNormal2Arb( -vViewPosition, normal );", "#elif defined( USE_BUMPMAP )", "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );", "#endif", "#if MAX_POINT_LIGHTS > 0", "vec3 pointDiffuse = vec3( 0.0 );", "vec3 pointSpecular = vec3( 0.0 );", "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {", "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );", "vec3 lVector = lPosition.xyz + vViewPosition.xyz;", "float lDistance = 1.0;", "if ( pointLightDistance[ i ] > 0.0 )", "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );", "lVector = normalize( lVector );", // diffuse "float dotProduct = dot( normal, lVector );", "#ifdef WRAP_AROUND", "float pointDiffuseWeightFull = max( dotProduct, 0.0 );", "float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );", "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );", "#else", "float pointDiffuseWeight = max( dotProduct, 0.0 );", "#endif", "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;", // specular "vec3 pointHalfVector = normalize( lVector + viewPosition );", "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );", "float pointSpecularWeight = specularStrength * max( pow( pointDotNormalHalf, shininess ), 0.0 );", // 2.0 => 2.0001 is hack to work around ANGLE bug "float specularNormalization = ( shininess + 2.0001 ) / 8.0;", "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, pointHalfVector ), 5.0 );", "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance * specularNormalization;", "}", "#endif", "#if MAX_SPOT_LIGHTS > 0", "vec3 spotDiffuse = vec3( 0.0 );", "vec3 spotSpecular = vec3( 0.0 );", "for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {", "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );", "vec3 lVector = lPosition.xyz + vViewPosition.xyz;", "float lDistance = 1.0;", "if ( spotLightDistance[ i ] > 0.0 )", "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );", "lVector = normalize( lVector );", "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );", "if ( spotEffect > spotLightAngleCos[ i ] ) {", "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );", // diffuse "float dotProduct = dot( normal, lVector );", "#ifdef WRAP_AROUND", "float spotDiffuseWeightFull = max( dotProduct, 0.0 );", "float spotDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );", "vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );", "#else", "float spotDiffuseWeight = max( dotProduct, 0.0 );", "#endif", "spotDiffuse += diffuse * spotLightColor[ i ] * spotDiffuseWeight * lDistance * spotEffect;", // specular "vec3 spotHalfVector = normalize( lVector + viewPosition );", "float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );", "float spotSpecularWeight = specularStrength * max( pow( spotDotNormalHalf, shininess ), 0.0 );", // 2.0 => 2.0001 is hack to work around ANGLE bug "float specularNormalization = ( shininess + 2.0001 ) / 8.0;", "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, spotHalfVector ), 5.0 );", "spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * lDistance * specularNormalization * spotEffect;", "}", "}", "#endif", "#if MAX_DIR_LIGHTS > 0", "vec3 dirDiffuse = vec3( 0.0 );", "vec3 dirSpecular = vec3( 0.0 );" , "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {", "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );", "vec3 dirVector = normalize( lDirection.xyz );", // diffuse "float dotProduct = dot( normal, dirVector );", "#ifdef WRAP_AROUND", "float dirDiffuseWeightFull = max( dotProduct, 0.0 );", "float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );", "vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );", "#else", "float dirDiffuseWeight = max( dotProduct, 0.0 );", "#endif", "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;", // specular "vec3 dirHalfVector = normalize( dirVector + viewPosition );", "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );", "float dirSpecularWeight = specularStrength * max( pow( dirDotNormalHalf, shininess ), 0.0 );", /* // fresnel term from skin shader "const float F0 = 0.128;", "float base = 1.0 - dot( viewPosition, dirHalfVector );", "float exponential = pow( base, 5.0 );", "float fresnel = exponential + F0 * ( 1.0 - exponential );", */ /* // fresnel term from fresnel shader "const float mFresnelBias = 0.08;", "const float mFresnelScale = 0.3;", "const float mFresnelPower = 5.0;", "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );", */ // 2.0 => 2.0001 is hack to work around ANGLE bug "float specularNormalization = ( shininess + 2.0001 ) / 8.0;", //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;", "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );", "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;", "}", "#endif", "#if MAX_HEMI_LIGHTS > 0", "vec3 hemiDiffuse = vec3( 0.0 );", "vec3 hemiSpecular = vec3( 0.0 );" , "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {", "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );", "vec3 lVector = normalize( lDirection.xyz );", // diffuse "float dotProduct = dot( normal, lVector );", "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;", "vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );", "hemiDiffuse += diffuse * hemiColor;", // specular (sky light) "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );", "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;", "float hemiSpecularWeightSky = specularStrength * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );", // specular (ground light) "vec3 lVectorGround = -lVector;", "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );", "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;", "float hemiSpecularWeightGround = specularStrength * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );", "float dotProductGround = dot( normal, lVectorGround );", // 2.0 => 2.0001 is hack to work around ANGLE bug "float specularNormalization = ( shininess + 2.0001 ) / 8.0;", "vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );", "vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );", "hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );", "}", "#endif", "vec3 totalDiffuse = vec3( 0.0 );", "vec3 totalSpecular = vec3( 0.0 );", "#if MAX_DIR_LIGHTS > 0", "totalDiffuse += dirDiffuse;", "totalSpecular += dirSpecular;", "#endif", "#if MAX_HEMI_LIGHTS > 0", "totalDiffuse += hemiDiffuse;", "totalSpecular += hemiSpecular;", "#endif", "#if MAX_POINT_LIGHTS > 0", "totalDiffuse += pointDiffuse;", "totalSpecular += pointSpecular;", "#endif", "#if MAX_SPOT_LIGHTS > 0", "totalDiffuse += spotDiffuse;", "totalSpecular += spotSpecular;", "#endif", "#ifdef METAL", "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );", "#else", "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;", "#endif" ].join("\n"), // VERTEX COLORS color_pars_fragment: [ "#ifdef USE_COLOR", "varying vec3 vColor;", "#endif" ].join("\n"), color_fragment: [ "#ifdef USE_COLOR", "gl_FragColor = gl_FragColor * vec4( vColor, 1.0 );", "#endif" ].join("\n"), color_pars_vertex: [ "#ifdef USE_COLOR", "varying vec3 vColor;", "#endif" ].join("\n"), color_vertex: [ "#ifdef USE_COLOR", "#ifdef GAMMA_INPUT", "vColor = color * color;", "#else", "vColor = color;", "#endif", "#endif" ].join("\n"), // SKINNING skinning_pars_vertex: [ "#ifdef USE_SKINNING", "#ifdef BONE_TEXTURE", "uniform sampler2D boneTexture;", "uniform int boneTextureWidth;", "uniform int boneTextureHeight;", "mat4 getBoneMatrix( const in float i ) {", "float j = i * 4.0;", "float x = mod( j, float( boneTextureWidth ) );", "float y = floor( j / float( boneTextureWidth ) );", "float dx = 1.0 / float( boneTextureWidth );", "float dy = 1.0 / float( boneTextureHeight );", "y = dy * ( y + 0.5 );", "vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );", "vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );", "vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );", "vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );", "mat4 bone = mat4( v1, v2, v3, v4 );", "return bone;", "}", "#else", "uniform mat4 boneGlobalMatrices[ MAX_BONES ];", "mat4 getBoneMatrix( const in float i ) {", "mat4 bone = boneGlobalMatrices[ int(i) ];", "return bone;", "}", "#endif", "#endif" ].join("\n"), skinbase_vertex: [ "#ifdef USE_SKINNING", "mat4 boneMatX = getBoneMatrix( skinIndex.x );", "mat4 boneMatY = getBoneMatrix( skinIndex.y );", "mat4 boneMatZ = getBoneMatrix( skinIndex.z );", "mat4 boneMatW = getBoneMatrix( skinIndex.w );", "#endif" ].join("\n"), skinning_vertex: [ "#ifdef USE_SKINNING", "#ifdef USE_MORPHTARGETS", "vec4 skinVertex = vec4( morphed, 1.0 );", "#else", "vec4 skinVertex = vec4( position, 1.0 );", "#endif", "vec4 skinned = boneMatX * skinVertex * skinWeight.x;", "skinned += boneMatY * skinVertex * skinWeight.y;", "skinned += boneMatZ * skinVertex * skinWeight.z;", "skinned += boneMatW * skinVertex * skinWeight.w;", "#endif" ].join("\n"), // MORPHING morphtarget_pars_vertex: [ "#ifdef USE_MORPHTARGETS", "#ifndef USE_MORPHNORMALS", "uniform float morphTargetInfluences[ 8 ];", "#else", "uniform float morphTargetInfluences[ 4 ];", "#endif", "#endif" ].join("\n"), morphtarget_vertex: [ "#ifdef USE_MORPHTARGETS", "vec3 morphed = vec3( 0.0 );", "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];", "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];", "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];", "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];", "#ifndef USE_MORPHNORMALS", "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];", "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];", "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];", "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];", "#endif", "morphed += position;", "#endif" ].join("\n"), default_vertex : [ "vec4 mvPosition;", "#ifdef USE_SKINNING", "mvPosition = modelViewMatrix * skinned;", "#endif", "#if !defined( USE_SKINNING ) && defined( USE_MORPHTARGETS )", "mvPosition = modelViewMatrix * vec4( morphed, 1.0 );", "#endif", "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHTARGETS )", "mvPosition = modelViewMatrix * vec4( position, 1.0 );", "#endif", "gl_Position = projectionMatrix * mvPosition;" ].join("\n"), morphnormal_vertex: [ "#ifdef USE_MORPHNORMALS", "vec3 morphedNormal = vec3( 0.0 );", "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];", "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];", "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];", "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];", "morphedNormal += normal;", "#endif" ].join("\n"), skinnormal_vertex: [ "#ifdef USE_SKINNING", "mat4 skinMatrix = skinWeight.x * boneMatX;", "skinMatrix += skinWeight.y * boneMatY;", "#ifdef USE_MORPHNORMALS", "vec4 skinnedNormal = skinMatrix * vec4( morphedNormal, 0.0 );", "#else", "vec4 skinnedNormal = skinMatrix * vec4( normal, 0.0 );", "#endif", "#endif" ].join("\n"), defaultnormal_vertex: [ "vec3 objectNormal;", "#ifdef USE_SKINNING", "objectNormal = skinnedNormal.xyz;", "#endif", "#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )", "objectNormal = morphedNormal;", "#endif", "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )", "objectNormal = normal;", "#endif", "#ifdef FLIP_SIDED", "objectNormal = -objectNormal;", "#endif", "vec3 transformedNormal = normalMatrix * objectNormal;" ].join("\n"), // SHADOW MAP // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples // http://spidergl.org/example.php?id=6 // http://fabiensanglard.net/shadowmapping shadowmap_pars_fragment: [ "#ifdef USE_SHADOWMAP", "uniform sampler2D shadowMap[ MAX_SHADOWS ];", "uniform vec2 shadowMapSize[ MAX_SHADOWS ];", "uniform float shadowDarkness[ MAX_SHADOWS ];", "uniform float shadowBias[ MAX_SHADOWS ];", "varying vec4 vShadowCoord[ MAX_SHADOWS ];", "float unpackDepth( const in vec4 rgba_depth ) {", "const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );", "float depth = dot( rgba_depth, bit_shift );", "return depth;", "}", "#endif" ].join("\n"), shadowmap_fragment: [ "#ifdef USE_SHADOWMAP", "#ifdef SHADOWMAP_DEBUG", "vec3 frustumColors[3];", "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );", "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );", "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );", "#endif", "#ifdef SHADOWMAP_CASCADE", "int inFrustumCount = 0;", "#endif", "float fDepth;", "vec3 shadowColor = vec3( 1.0 );", "for( int i = 0; i < MAX_SHADOWS; i ++ ) {", "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;", // "if ( something && something )" breaks ATI OpenGL shader compiler // "if ( all( something, something ) )" using this instead "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );", "bool inFrustum = all( inFrustumVec );", // don't shadow pixels outside of light frustum // use just first frustum (for cascades) // don't shadow pixels behind far plane of light frustum "#ifdef SHADOWMAP_CASCADE", "inFrustumCount += int( inFrustum );", "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );", "#else", "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );", "#endif", "bool frustumTest = all( frustumTestVec );", "if ( frustumTest ) {", "shadowCoord.z += shadowBias[ i ];", "#if defined( SHADOWMAP_TYPE_PCF )", // Percentage-close filtering // (9 pixel kernel) // http://fabiensanglard.net/shadowmappingPCF/ "float shadow = 0.0;", /* // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL // must enroll loop manually "for ( float y = -1.25; y <= 1.25; y += 1.25 )", "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {", "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );", // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );", "float fDepth = unpackDepth( rgbaDepth );", "if ( fDepth < shadowCoord.z )", "shadow += 1.0;", "}", "shadow /= 9.0;", */ "const float shadowDelta = 1.0 / 9.0;", "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;", "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;", "float dx0 = -1.25 * xPixelOffset;", "float dy0 = -1.25 * yPixelOffset;", "float dx1 = 1.25 * xPixelOffset;", "float dy1 = 1.25 * yPixelOffset;", "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );", "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;", "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );", "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;", "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );", "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;", "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );", "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;", "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );", "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;", "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );", "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;", "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );", "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;", "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );", "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;", "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );", "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;", "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );", "#elif defined( SHADOWMAP_TYPE_PCF_SOFT )", // Percentage-close filtering // (9 pixel kernel) // http://fabiensanglard.net/shadowmappingPCF/ "float shadow = 0.0;", "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;", "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;", "float dx0 = -1.0 * xPixelOffset;", "float dy0 = -1.0 * yPixelOffset;", "float dx1 = 1.0 * xPixelOffset;", "float dy1 = 1.0 * yPixelOffset;", "mat3 shadowKernel;", "mat3 depthKernel;", "depthKernel[0][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );", "depthKernel[0][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );", "depthKernel[0][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );", "depthKernel[1][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );", "depthKernel[1][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );", "depthKernel[1][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );", "depthKernel[2][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );", "depthKernel[2][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );", "depthKernel[2][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );", "vec3 shadowZ = vec3( shadowCoord.z );", "shadowKernel[0] = vec3(lessThan(depthKernel[0], shadowZ ));", "shadowKernel[0] *= vec3(0.25);", "shadowKernel[1] = vec3(lessThan(depthKernel[1], shadowZ ));", "shadowKernel[1] *= vec3(0.25);", "shadowKernel[2] = vec3(lessThan(depthKernel[2], shadowZ ));", "shadowKernel[2] *= vec3(0.25);", "vec2 fractionalCoord = 1.0 - fract( shadowCoord.xy * shadowMapSize[i].xy );", "shadowKernel[0] = mix( shadowKernel[1], shadowKernel[0], fractionalCoord.x );", "shadowKernel[1] = mix( shadowKernel[2], shadowKernel[1], fractionalCoord.x );", "vec4 shadowValues;", "shadowValues.x = mix( shadowKernel[0][1], shadowKernel[0][0], fractionalCoord.y );", "shadowValues.y = mix( shadowKernel[0][2], shadowKernel[0][1], fractionalCoord.y );", "shadowValues.z = mix( shadowKernel[1][1], shadowKernel[1][0], fractionalCoord.y );", "shadowValues.w = mix( shadowKernel[1][2], shadowKernel[1][1], fractionalCoord.y );", "shadow = dot( shadowValues, vec4( 1.0 ) );", "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );", "#else", "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );", "float fDepth = unpackDepth( rgbaDepth );", "if ( fDepth < shadowCoord.z )", // spot with multiple shadows is darker "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );", // spot with multiple shadows has the same color as single shadow spot //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );", "#endif", "}", "#ifdef SHADOWMAP_DEBUG", "#ifdef SHADOWMAP_CASCADE", "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];", "#else", "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];", "#endif", "#endif", "}", "#ifdef GAMMA_OUTPUT", "shadowColor *= shadowColor;", "#endif", "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;", "#endif" ].join("\n"), shadowmap_pars_vertex: [ "#ifdef USE_SHADOWMAP", "varying vec4 vShadowCoord[ MAX_SHADOWS ];", "uniform mat4 shadowMatrix[ MAX_SHADOWS ];", "#endif" ].join("\n"), shadowmap_vertex: [ "#ifdef USE_SHADOWMAP", "for( int i = 0; i < MAX_SHADOWS; i ++ ) {", "vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;", "}", "#endif" ].join("\n"), // ALPHATEST alphatest_fragment: [ "#ifdef ALPHATEST", "if ( gl_FragColor.a < ALPHATEST ) discard;", "#endif" ].join("\n"), // LINEAR SPACE linear_to_gamma_fragment: [ "#ifdef GAMMA_OUTPUT", "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );", "#endif" ].join("\n") }; /** * Uniform Utilities */ THREE.UniformsUtils = { merge: function ( uniforms ) { var u, p, tmp, merged = {}; for ( u = 0; u < uniforms.length; u ++ ) { tmp = this.clone( uniforms[ u ] ); for ( p in tmp ) { merged[ p ] = tmp[ p ]; } } return merged; }, clone: function ( uniforms_src ) { var u, p, parameter, parameter_src, uniforms_dst = {}; for ( u in uniforms_src ) { uniforms_dst[ u ] = {}; for ( p in uniforms_src[ u ] ) { parameter_src = uniforms_src[ u ][ p ]; if ( parameter_src instanceof THREE.Color || parameter_src instanceof THREE.Vector2 || parameter_src instanceof THREE.Vector3 || parameter_src instanceof THREE.Vector4 || parameter_src instanceof THREE.Matrix4 || parameter_src instanceof THREE.Texture ) { uniforms_dst[ u ][ p ] = parameter_src.clone(); } else if ( parameter_src instanceof Array ) { uniforms_dst[ u ][ p ] = parameter_src.slice(); } else { uniforms_dst[ u ][ p ] = parameter_src; } } } return uniforms_dst; } }; /** * Uniforms library for shared webgl shaders */ THREE.UniformsLib = { common: { "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) }, "opacity" : { type: "f", value: 1.0 }, "map" : { type: "t", value: null }, "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) }, "lightMap" : { type: "t", value: null }, "specularMap" : { type: "t", value: null }, "envMap" : { type: "t", value: null }, "flipEnvMap" : { type: "f", value: -1 }, "useRefract" : { type: "i", value: 0 }, "reflectivity" : { type: "f", value: 1.0 }, "refractionRatio" : { type: "f", value: 0.98 }, "combine" : { type: "i", value: 0 }, "morphTargetInfluences" : { type: "f", value: 0 } }, bump: { "bumpMap" : { type: "t", value: null }, "bumpScale" : { type: "f", value: 1 } }, normalmap: { "normalMap" : { type: "t", value: null }, "normalScale" : { type: "v2", value: new THREE.Vector2( 1, 1 ) } }, fog : { "fogDensity" : { type: "f", value: 0.00025 }, "fogNear" : { type: "f", value: 1 }, "fogFar" : { type: "f", value: 2000 }, "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) } }, lights: { "ambientLightColor" : { type: "fv", value: [] }, "directionalLightDirection" : { type: "fv", value: [] }, "directionalLightColor" : { type: "fv", value: [] }, "hemisphereLightDirection" : { type: "fv", value: [] }, "hemisphereLightSkyColor" : { type: "fv", value: [] }, "hemisphereLightGroundColor" : { type: "fv", value: [] }, "pointLightColor" : { type: "fv", value: [] }, "pointLightPosition" : { type: "fv", value: [] }, "pointLightDistance" : { type: "fv1", value: [] }, "spotLightColor" : { type: "fv", value: [] }, "spotLightPosition" : { type: "fv", value: [] }, "spotLightDirection" : { type: "fv", value: [] }, "spotLightDistance" : { type: "fv1", value: [] }, "spotLightAngleCos" : { type: "fv1", value: [] }, "spotLightExponent" : { type: "fv1", value: [] } }, particle: { "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) }, "opacity" : { type: "f", value: 1.0 }, "size" : { type: "f", value: 1.0 }, "scale" : { type: "f", value: 1.0 }, "map" : { type: "t", value: null }, "fogDensity" : { type: "f", value: 0.00025 }, "fogNear" : { type: "f", value: 1 }, "fogFar" : { type: "f", value: 2000 }, "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) } }, shadowmap: { "shadowMap": { type: "tv", value: [] }, "shadowMapSize": { type: "v2v", value: [] }, "shadowBias" : { type: "fv1", value: [] }, "shadowDarkness": { type: "fv1", value: [] }, "shadowMatrix" : { type: "m4v", value: [] } } }; /** * Webgl Shader Library for three.js * * @author alteredq / http://alteredqualia.com/ * @author mrdoob / http://mrdoob.com/ * @author mikael emtinger / http://gomo.se/ */ THREE.ShaderLib = { 'basic': { uniforms: THREE.UniformsUtils.merge( [ THREE.UniformsLib[ "common" ], THREE.UniformsLib[ "fog" ], THREE.UniformsLib[ "shadowmap" ] ] ), vertexShader: [ THREE.ShaderChunk[ "map_pars_vertex" ], THREE.ShaderChunk[ "lightmap_pars_vertex" ], THREE.ShaderChunk[ "envmap_pars_vertex" ], THREE.ShaderChunk[ "color_pars_vertex" ], THREE.ShaderChunk[ "morphtarget_pars_vertex" ], THREE.ShaderChunk[ "skinning_pars_vertex" ], THREE.ShaderChunk[ "shadowmap_pars_vertex" ], "void main() {", THREE.ShaderChunk[ "map_vertex" ], THREE.ShaderChunk[ "lightmap_vertex" ], THREE.ShaderChunk[ "color_vertex" ], THREE.ShaderChunk[ "skinbase_vertex" ], "#ifdef USE_ENVMAP", THREE.ShaderChunk[ "morphnormal_vertex" ], THREE.ShaderChunk[ "skinnormal_vertex" ], THREE.ShaderChunk[ "defaultnormal_vertex" ], "#endif", THREE.ShaderChunk[ "morphtarget_vertex" ], THREE.ShaderChunk[ "skinning_vertex" ], THREE.ShaderChunk[ "default_vertex" ], THREE.ShaderChunk[ "worldpos_vertex" ], THREE.ShaderChunk[ "envmap_vertex" ], THREE.ShaderChunk[ "shadowmap_vertex" ], "}" ].join("\n"), fragmentShader: [ "uniform vec3 diffuse;", "uniform float opacity;", THREE.ShaderChunk[ "color_pars_fragment" ], THREE.ShaderChunk[ "map_pars_fragment" ], THREE.ShaderChunk[ "lightmap_pars_fragment" ], THREE.ShaderChunk[ "envmap_pars_fragment" ], THREE.ShaderChunk[ "fog_pars_fragment" ], THREE.ShaderChunk[ "shadowmap_pars_fragment" ], THREE.ShaderChunk[ "specularmap_pars_fragment" ], "void main() {", "gl_FragColor = vec4( diffuse, opacity );", THREE.ShaderChunk[ "map_fragment" ], THREE.ShaderChunk[ "alphatest_fragment" ], THREE.ShaderChunk[ "specularmap_fragment" ], THREE.ShaderChunk[ "lightmap_fragment" ], THREE.ShaderChunk[ "color_fragment" ], THREE.ShaderChunk[ "envmap_fragment" ], THREE.ShaderChunk[ "shadowmap_fragment" ], THREE.ShaderChunk[ "linear_to_gamma_fragment" ], THREE.ShaderChunk[ "fog_fragment" ], "}" ].join("\n") }, 'lambert': { uniforms: THREE.UniformsUtils.merge( [ THREE.UniformsLib[ "common" ], THREE.UniformsLib[ "fog" ], THREE.UniformsLib[ "lights" ], THREE.UniformsLib[ "shadowmap" ], { "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) }, "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) }, "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) } } ] ), vertexShader: [ "#define LAMBERT", "varying vec3 vLightFront;", "#ifdef DOUBLE_SIDED", "varying vec3 vLightBack;", "#endif", THREE.ShaderChunk[ "map_pars_vertex" ], THREE.ShaderChunk[ "lightmap_pars_vertex" ], THREE.ShaderChunk[ "envmap_pars_vertex" ], THREE.ShaderChunk[ "lights_lambert_pars_vertex" ], THREE.ShaderChunk[ "color_pars_vertex" ], THREE.ShaderChunk[ "morphtarget_pars_vertex" ], THREE.ShaderChunk[ "skinning_pars_vertex" ], THREE.ShaderChunk[ "shadowmap_pars_vertex" ], "void main() {", THREE.ShaderChunk[ "map_vertex" ], THREE.ShaderChunk[ "lightmap_vertex" ], THREE.ShaderChunk[ "color_vertex" ], THREE.ShaderChunk[ "morphnormal_vertex" ], THREE.ShaderChunk[ "skinbase_vertex" ], THREE.ShaderChunk[ "skinnormal_vertex" ], THREE.ShaderChunk[ "defaultnormal_vertex" ], THREE.ShaderChunk[ "morphtarget_vertex" ], THREE.ShaderChunk[ "skinning_vertex" ], THREE.ShaderChunk[ "default_vertex" ], THREE.ShaderChunk[ "worldpos_vertex" ], THREE.ShaderChunk[ "envmap_vertex" ], THREE.ShaderChunk[ "lights_lambert_vertex" ], THREE.ShaderChunk[ "shadowmap_vertex" ], "}" ].join("\n"), fragmentShader: [ "uniform float opacity;", "varying vec3 vLightFront;", "#ifdef DOUBLE_SIDED", "varying vec3 vLightBack;", "#endif", THREE.ShaderChunk[ "color_pars_fragment" ], THREE.ShaderChunk[ "map_pars_fragment" ], THREE.ShaderChunk[ "lightmap_pars_fragment" ], THREE.ShaderChunk[ "envmap_pars_fragment" ], THREE.ShaderChunk[ "fog_pars_fragment" ], THREE.ShaderChunk[ "shadowmap_pars_fragment" ], THREE.ShaderChunk[ "specularmap_pars_fragment" ], "void main() {", "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );", THREE.ShaderChunk[ "map_fragment" ], THREE.ShaderChunk[ "alphatest_fragment" ], THREE.ShaderChunk[ "specularmap_fragment" ], "#ifdef DOUBLE_SIDED", //"float isFront = float( gl_FrontFacing );", //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;", "if ( gl_FrontFacing )", "gl_FragColor.xyz *= vLightFront;", "else", "gl_FragColor.xyz *= vLightBack;", "#else", "gl_FragColor.xyz *= vLightFront;", "#endif", THREE.ShaderChunk[ "lightmap_fragment" ], THREE.ShaderChunk[ "color_fragment" ], THREE.ShaderChunk[ "envmap_fragment" ], THREE.ShaderChunk[ "shadowmap_fragment" ], THREE.ShaderChunk[ "linear_to_gamma_fragment" ], THREE.ShaderChunk[ "fog_fragment" ], "}" ].join("\n") }, 'phong': { uniforms: THREE.UniformsUtils.merge( [ THREE.UniformsLib[ "common" ], THREE.UniformsLib[ "bump" ], THREE.UniformsLib[ "normalmap" ], THREE.UniformsLib[ "fog" ], THREE.UniformsLib[ "lights" ], THREE.UniformsLib[ "shadowmap" ], { "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) }, "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) }, "specular" : { type: "c", value: new THREE.Color( 0x111111 ) }, "shininess": { type: "f", value: 30 }, "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) } } ] ), vertexShader: [ "#define PHONG", "varying vec3 vViewPosition;", "varying vec3 vNormal;", THREE.ShaderChunk[ "map_pars_vertex" ], THREE.ShaderChunk[ "lightmap_pars_vertex" ], THREE.ShaderChunk[ "envmap_pars_vertex" ], THREE.ShaderChunk[ "lights_phong_pars_vertex" ], THREE.ShaderChunk[ "color_pars_vertex" ], THREE.ShaderChunk[ "morphtarget_pars_vertex" ], THREE.ShaderChunk[ "skinning_pars_vertex" ], THREE.ShaderChunk[ "shadowmap_pars_vertex" ], "void main() {", THREE.ShaderChunk[ "map_vertex" ], THREE.ShaderChunk[ "lightmap_vertex" ], THREE.ShaderChunk[ "color_vertex" ], THREE.ShaderChunk[ "morphnormal_vertex" ], THREE.ShaderChunk[ "skinbase_vertex" ], THREE.ShaderChunk[ "skinnormal_vertex" ], THREE.ShaderChunk[ "defaultnormal_vertex" ], "vNormal = normalize( transformedNormal );", THREE.ShaderChunk[ "morphtarget_vertex" ], THREE.ShaderChunk[ "skinning_vertex" ], THREE.ShaderChunk[ "default_vertex" ], "vViewPosition = -mvPosition.xyz;", THREE.ShaderChunk[ "worldpos_vertex" ], THREE.ShaderChunk[ "envmap_vertex" ], THREE.ShaderChunk[ "lights_phong_vertex" ], THREE.ShaderChunk[ "shadowmap_vertex" ], "}" ].join("\n"), fragmentShader: [ "uniform vec3 diffuse;", "uniform float opacity;", "uniform vec3 ambient;", "uniform vec3 emissive;", "uniform vec3 specular;", "uniform float shininess;", THREE.ShaderChunk[ "color_pars_fragment" ], THREE.ShaderChunk[ "map_pars_fragment" ], THREE.ShaderChunk[ "lightmap_pars_fragment" ], THREE.ShaderChunk[ "envmap_pars_fragment" ], THREE.ShaderChunk[ "fog_pars_fragment" ], THREE.ShaderChunk[ "lights_phong_pars_fragment" ], THREE.ShaderChunk[ "shadowmap_pars_fragment" ], THREE.ShaderChunk[ "bumpmap_pars_fragment" ], THREE.ShaderChunk[ "normalmap_pars_fragment" ], THREE.ShaderChunk[ "specularmap_pars_fragment" ], "void main() {", "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );", THREE.ShaderChunk[ "map_fragment" ], THREE.ShaderChunk[ "alphatest_fragment" ], THREE.ShaderChunk[ "specularmap_fragment" ], THREE.ShaderChunk[ "lights_phong_fragment" ], THREE.ShaderChunk[ "lightmap_fragment" ], THREE.ShaderChunk[ "color_fragment" ], THREE.ShaderChunk[ "envmap_fragment" ], THREE.ShaderChunk[ "shadowmap_fragment" ], THREE.ShaderChunk[ "linear_to_gamma_fragment" ], THREE.ShaderChunk[ "fog_fragment" ], "}" ].join("\n") }, 'particle_basic': { uniforms: THREE.UniformsUtils.merge( [ THREE.UniformsLib[ "particle" ], THREE.UniformsLib[ "shadowmap" ] ] ), vertexShader: [ "uniform float size;", "uniform float scale;", THREE.ShaderChunk[ "color_pars_vertex" ], THREE.ShaderChunk[ "shadowmap_pars_vertex" ], "void main() {", THREE.ShaderChunk[ "color_vertex" ], "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );", "#ifdef USE_SIZEATTENUATION", "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );", "#else", "gl_PointSize = size;", "#endif", "gl_Position = projectionMatrix * mvPosition;", THREE.ShaderChunk[ "worldpos_vertex" ], THREE.ShaderChunk[ "shadowmap_vertex" ], "}" ].join("\n"), fragmentShader: [ "uniform vec3 psColor;", "uniform float opacity;", THREE.ShaderChunk[ "color_pars_fragment" ], THREE.ShaderChunk[ "map_particle_pars_fragment" ], THREE.ShaderChunk[ "fog_pars_fragment" ], THREE.ShaderChunk[ "shadowmap_pars_fragment" ], "void main() {", "gl_FragColor = vec4( psColor, opacity );", THREE.ShaderChunk[ "map_particle_fragment" ], THREE.ShaderChunk[ "alphatest_fragment" ], THREE.ShaderChunk[ "color_fragment" ], THREE.ShaderChunk[ "shadowmap_fragment" ], THREE.ShaderChunk[ "fog_fragment" ], "}" ].join("\n") }, 'dashed': { uniforms: THREE.UniformsUtils.merge( [ THREE.UniformsLib[ "common" ], THREE.UniformsLib[ "fog" ], { "scale": { type: "f", value: 1 }, "dashSize": { type: "f", value: 1 }, "totalSize": { type: "f", value: 2 } } ] ), vertexShader: [ "uniform float scale;", "attribute float lineDistance;", "varying float vLineDistance;", THREE.ShaderChunk[ "color_pars_vertex" ], "void main() {", THREE.ShaderChunk[ "color_vertex" ], "vLineDistance = scale * lineDistance;", "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );", "gl_Position = projectionMatrix * mvPosition;", "}" ].join("\n"), fragmentShader: [ "uniform vec3 diffuse;", "uniform float opacity;", "uniform float dashSize;", "uniform float totalSize;", "varying float vLineDistance;", THREE.ShaderChunk[ "color_pars_fragment" ], THREE.ShaderChunk[ "fog_pars_fragment" ], "void main() {", "if ( mod( vLineDistance, totalSize ) > dashSize ) {", "discard;", "}", "gl_FragColor = vec4( diffuse, opacity );", THREE.ShaderChunk[ "color_fragment" ], THREE.ShaderChunk[ "fog_fragment" ], "}" ].join("\n") }, 'depth': { uniforms: { "mNear": { type: "f", value: 1.0 }, "mFar" : { type: "f", value: 2000.0 }, "opacity" : { type: "f", value: 1.0 } }, vertexShader: [ "void main() {", "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );", "}" ].join("\n"), fragmentShader: [ "uniform float mNear;", "uniform float mFar;", "uniform float opacity;", "void main() {", "float depth = gl_FragCoord.z / gl_FragCoord.w;", "float color = 1.0 - smoothstep( mNear, mFar, depth );", "gl_FragColor = vec4( vec3( color ), opacity );", "}" ].join("\n") }, 'normal': { uniforms: { "opacity" : { type: "f", value: 1.0 } }, vertexShader: [ "varying vec3 vNormal;", THREE.ShaderChunk[ "morphtarget_pars_vertex" ], "void main() {", "vNormal = normalize( normalMatrix * normal );", THREE.ShaderChunk[ "morphtarget_vertex" ], THREE.ShaderChunk[ "default_vertex" ], "}" ].join("\n"), fragmentShader: [ "uniform float opacity;", "varying vec3 vNormal;", "void main() {", "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );", "}" ].join("\n") }, /* ------------------------------------------------------------------------- // Normal map shader // - Blinn-Phong // - normal + diffuse + specular + AO + displacement + reflection + shadow maps // - point and directional lights (use with "lights: true" material option) ------------------------------------------------------------------------- */ 'normalmap' : { uniforms: THREE.UniformsUtils.merge( [ THREE.UniformsLib[ "fog" ], THREE.UniformsLib[ "lights" ], THREE.UniformsLib[ "shadowmap" ], { "enableAO" : { type: "i", value: 0 }, "enableDiffuse" : { type: "i", value: 0 }, "enableSpecular" : { type: "i", value: 0 }, "enableReflection": { type: "i", value: 0 }, "enableDisplacement": { type: "i", value: 0 }, "tDisplacement": { type: "t", value: null }, // must go first as this is vertex texture "tDiffuse" : { type: "t", value: null }, "tCube" : { type: "t", value: null }, "tNormal" : { type: "t", value: null }, "tSpecular" : { type: "t", value: null }, "tAO" : { type: "t", value: null }, "uNormalScale": { type: "v2", value: new THREE.Vector2( 1, 1 ) }, "uDisplacementBias": { type: "f", value: 0.0 }, "uDisplacementScale": { type: "f", value: 1.0 }, "diffuse": { type: "c", value: new THREE.Color( 0xffffff ) }, "specular": { type: "c", value: new THREE.Color( 0x111111 ) }, "ambient": { type: "c", value: new THREE.Color( 0xffffff ) }, "shininess": { type: "f", value: 30 }, "opacity": { type: "f", value: 1 }, "useRefract": { type: "i", value: 0 }, "refractionRatio": { type: "f", value: 0.98 }, "reflectivity": { type: "f", value: 0.5 }, "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) }, "uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) }, "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) } } ] ), fragmentShader: [ "uniform vec3 ambient;", "uniform vec3 diffuse;", "uniform vec3 specular;", "uniform float shininess;", "uniform float opacity;", "uniform bool enableDiffuse;", "uniform bool enableSpecular;", "uniform bool enableAO;", "uniform bool enableReflection;", "uniform sampler2D tDiffuse;", "uniform sampler2D tNormal;", "uniform sampler2D tSpecular;", "uniform sampler2D tAO;", "uniform samplerCube tCube;", "uniform vec2 uNormalScale;", "uniform bool useRefract;", "uniform float refractionRatio;", "uniform float reflectivity;", "varying vec3 vTangent;", "varying vec3 vBinormal;", "varying vec3 vNormal;", "varying vec2 vUv;", "uniform vec3 ambientLightColor;", "#if MAX_DIR_LIGHTS > 0", "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];", "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];", "#endif", "#if MAX_HEMI_LIGHTS > 0", "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];", "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];", "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];", "#endif", "#if MAX_POINT_LIGHTS > 0", "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];", "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];", "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];", "#endif", "#if MAX_SPOT_LIGHTS > 0", "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];", "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];", "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];", "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];", "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];", "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];", "#endif", "#ifdef WRAP_AROUND", "uniform vec3 wrapRGB;", "#endif", "varying vec3 vWorldPosition;", "varying vec3 vViewPosition;", THREE.ShaderChunk[ "shadowmap_pars_fragment" ], THREE.ShaderChunk[ "fog_pars_fragment" ], "void main() {", "gl_FragColor = vec4( vec3( 1.0 ), opacity );", "vec3 specularTex = vec3( 1.0 );", "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;", "normalTex.xy *= uNormalScale;", "normalTex = normalize( normalTex );", "if( enableDiffuse ) {", "#ifdef GAMMA_INPUT", "vec4 texelColor = texture2D( tDiffuse, vUv );", "texelColor.xyz *= texelColor.xyz;", "gl_FragColor = gl_FragColor * texelColor;", "#else", "gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );", "#endif", "}", "if( enableAO ) {", "#ifdef GAMMA_INPUT", "vec4 aoColor = texture2D( tAO, vUv );", "aoColor.xyz *= aoColor.xyz;", "gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;", "#else", "gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;", "#endif", "}", "if( enableSpecular )", "specularTex = texture2D( tSpecular, vUv ).xyz;", "mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );", "vec3 finalNormal = tsb * normalTex;", "#ifdef FLIP_SIDED", "finalNormal = -finalNormal;", "#endif", "vec3 normal = normalize( finalNormal );", "vec3 viewPosition = normalize( vViewPosition );", // point lights "#if MAX_POINT_LIGHTS > 0", "vec3 pointDiffuse = vec3( 0.0 );", "vec3 pointSpecular = vec3( 0.0 );", "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {", "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );", "vec3 pointVector = lPosition.xyz + vViewPosition.xyz;", "float pointDistance = 1.0;", "if ( pointLightDistance[ i ] > 0.0 )", "pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );", "pointVector = normalize( pointVector );", // diffuse "#ifdef WRAP_AROUND", "float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );", "float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );", "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );", "#else", "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );", "#endif", "pointDiffuse += pointDistance * pointLightColor[ i ] * diffuse * pointDiffuseWeight;", // specular "vec3 pointHalfVector = normalize( pointVector + viewPosition );", "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );", "float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, shininess ), 0.0 );", // 2.0 => 2.0001 is hack to work around ANGLE bug "float specularNormalization = ( shininess + 2.0001 ) / 8.0;", "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );", "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;", "}", "#endif", // spot lights "#if MAX_SPOT_LIGHTS > 0", "vec3 spotDiffuse = vec3( 0.0 );", "vec3 spotSpecular = vec3( 0.0 );", "for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {", "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );", "vec3 spotVector = lPosition.xyz + vViewPosition.xyz;", "float spotDistance = 1.0;", "if ( spotLightDistance[ i ] > 0.0 )", "spotDistance = 1.0 - min( ( length( spotVector ) / spotLightDistance[ i ] ), 1.0 );", "spotVector = normalize( spotVector );", "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );", "if ( spotEffect > spotLightAngleCos[ i ] ) {", "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );", // diffuse "#ifdef WRAP_AROUND", "float spotDiffuseWeightFull = max( dot( normal, spotVector ), 0.0 );", "float spotDiffuseWeightHalf = max( 0.5 * dot( normal, spotVector ) + 0.5, 0.0 );", "vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );", "#else", "float spotDiffuseWeight = max( dot( normal, spotVector ), 0.0 );", "#endif", "spotDiffuse += spotDistance * spotLightColor[ i ] * diffuse * spotDiffuseWeight * spotEffect;", // specular "vec3 spotHalfVector = normalize( spotVector + viewPosition );", "float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );", "float spotSpecularWeight = specularTex.r * max( pow( spotDotNormalHalf, shininess ), 0.0 );", // 2.0 => 2.0001 is hack to work around ANGLE bug "float specularNormalization = ( shininess + 2.0001 ) / 8.0;", "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( spotVector, spotHalfVector ), 5.0 );", "spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * spotDistance * specularNormalization * spotEffect;", "}", "}", "#endif", // directional lights "#if MAX_DIR_LIGHTS > 0", "vec3 dirDiffuse = vec3( 0.0 );", "vec3 dirSpecular = vec3( 0.0 );", "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {", "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );", "vec3 dirVector = normalize( lDirection.xyz );", // diffuse "#ifdef WRAP_AROUND", "float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );", "float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );", "vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );", "#else", "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );", "#endif", "dirDiffuse += directionalLightColor[ i ] * diffuse * dirDiffuseWeight;", // specular "vec3 dirHalfVector = normalize( dirVector + viewPosition );", "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );", "float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, shininess ), 0.0 );", // 2.0 => 2.0001 is hack to work around ANGLE bug "float specularNormalization = ( shininess + 2.0001 ) / 8.0;", "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );", "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;", "}", "#endif", // hemisphere lights "#if MAX_HEMI_LIGHTS > 0", "vec3 hemiDiffuse = vec3( 0.0 );", "vec3 hemiSpecular = vec3( 0.0 );" , "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {", "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );", "vec3 lVector = normalize( lDirection.xyz );", // diffuse "float dotProduct = dot( normal, lVector );", "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;", "vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );", "hemiDiffuse += diffuse * hemiColor;", // specular (sky light) "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );", "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;", "float hemiSpecularWeightSky = specularTex.r * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );", // specular (ground light) "vec3 lVectorGround = -lVector;", "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );", "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;", "float hemiSpecularWeightGround = specularTex.r * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );", "float dotProductGround = dot( normal, lVectorGround );", // 2.0 => 2.0001 is hack to work around ANGLE bug "float specularNormalization = ( shininess + 2.0001 ) / 8.0;", "vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );", "vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );", "hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );", "}", "#endif", // all lights contribution summation "vec3 totalDiffuse = vec3( 0.0 );", "vec3 totalSpecular = vec3( 0.0 );", "#if MAX_DIR_LIGHTS > 0", "totalDiffuse += dirDiffuse;", "totalSpecular += dirSpecular;", "#endif", "#if MAX_HEMI_LIGHTS > 0", "totalDiffuse += hemiDiffuse;", "totalSpecular += hemiSpecular;", "#endif", "#if MAX_POINT_LIGHTS > 0", "totalDiffuse += pointDiffuse;", "totalSpecular += pointSpecular;", "#endif", "#if MAX_SPOT_LIGHTS > 0", "totalDiffuse += spotDiffuse;", "totalSpecular += spotSpecular;", "#endif", "#ifdef METAL", "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient + totalSpecular );", "#else", "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient ) + totalSpecular;", "#endif", "if ( enableReflection ) {", "vec3 vReflect;", "vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );", "if ( useRefract ) {", "vReflect = refract( cameraToVertex, normal, refractionRatio );", "} else {", "vReflect = reflect( cameraToVertex, normal );", "}", "vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );", "#ifdef GAMMA_INPUT", "cubeColor.xyz *= cubeColor.xyz;", "#endif", "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * reflectivity );", "}", THREE.ShaderChunk[ "shadowmap_fragment" ], THREE.ShaderChunk[ "linear_to_gamma_fragment" ], THREE.ShaderChunk[ "fog_fragment" ], "}" ].join("\n"), vertexShader: [ "attribute vec4 tangent;", "uniform vec2 uOffset;", "uniform vec2 uRepeat;", "uniform bool enableDisplacement;", "#ifdef VERTEX_TEXTURES", "uniform sampler2D tDisplacement;", "uniform float uDisplacementScale;", "uniform float uDisplacementBias;", "#endif", "varying vec3 vTangent;", "varying vec3 vBinormal;", "varying vec3 vNormal;", "varying vec2 vUv;", "varying vec3 vWorldPosition;", "varying vec3 vViewPosition;", THREE.ShaderChunk[ "skinning_pars_vertex" ], THREE.ShaderChunk[ "shadowmap_pars_vertex" ], "void main() {", THREE.ShaderChunk[ "skinbase_vertex" ], THREE.ShaderChunk[ "skinnormal_vertex" ], // normal, tangent and binormal vectors "#ifdef USE_SKINNING", "vNormal = normalize( normalMatrix * skinnedNormal.xyz );", "vec4 skinnedTangent = skinMatrix * vec4( tangent.xyz, 0.0 );", "vTangent = normalize( normalMatrix * skinnedTangent.xyz );", "#else", "vNormal = normalize( normalMatrix * normal );", "vTangent = normalize( normalMatrix * tangent.xyz );", "#endif", "vBinormal = normalize( cross( vNormal, vTangent ) * tangent.w );", "vUv = uv * uRepeat + uOffset;", // displacement mapping "vec3 displacedPosition;", "#ifdef VERTEX_TEXTURES", "if ( enableDisplacement ) {", "vec3 dv = texture2D( tDisplacement, uv ).xyz;", "float df = uDisplacementScale * dv.x + uDisplacementBias;", "displacedPosition = position + normalize( normal ) * df;", "} else {", "#ifdef USE_SKINNING", "vec4 skinVertex = vec4( position, 1.0 );", "vec4 skinned = boneMatX * skinVertex * skinWeight.x;", "skinned += boneMatY * skinVertex * skinWeight.y;", "displacedPosition = skinned.xyz;", "#else", "displacedPosition = position;", "#endif", "}", "#else", "#ifdef USE_SKINNING", "vec4 skinVertex = vec4( position, 1.0 );", "vec4 skinned = boneMatX * skinVertex * skinWeight.x;", "skinned += boneMatY * skinVertex * skinWeight.y;", "displacedPosition = skinned.xyz;", "#else", "displacedPosition = position;", "#endif", "#endif", // "vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );", "vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );", "gl_Position = projectionMatrix * mvPosition;", // "vWorldPosition = worldPosition.xyz;", "vViewPosition = -mvPosition.xyz;", // shadows "#ifdef USE_SHADOWMAP", "for( int i = 0; i < MAX_SHADOWS; i ++ ) {", "vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;", "}", "#endif", "}" ].join("\n") }, /* ------------------------------------------------------------------------- // Cube map shader ------------------------------------------------------------------------- */ 'cube': { uniforms: { "tCube": { type: "t", value: null }, "tFlip": { type: "f", value: -1 } }, vertexShader: [ "varying vec3 vWorldPosition;", "void main() {", "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );", "vWorldPosition = worldPosition.xyz;", "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );", "}" ].join("\n"), fragmentShader: [ "uniform samplerCube tCube;", "uniform float tFlip;", "varying vec3 vWorldPosition;", "void main() {", "gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );", "}" ].join("\n") }, // Depth encoding into RGBA texture // based on SpiderGL shadow map example // http://spidergl.org/example.php?id=6 // originally from // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD // see also here: // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/ 'depthRGBA': { uniforms: {}, vertexShader: [ THREE.ShaderChunk[ "morphtarget_pars_vertex" ], THREE.ShaderChunk[ "skinning_pars_vertex" ], "void main() {", THREE.ShaderChunk[ "skinbase_vertex" ], THREE.ShaderChunk[ "morphtarget_vertex" ], THREE.ShaderChunk[ "skinning_vertex" ], THREE.ShaderChunk[ "default_vertex" ], "}" ].join("\n"), fragmentShader: [ "vec4 pack_depth( const in float depth ) {", "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );", "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );", "vec4 res = fract( depth * bit_shift );", "res -= res.xxyz * bit_mask;", "return res;", "}", "void main() {", "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );", //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );", //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );", //"gl_FragData[ 0 ] = pack_depth( z );", //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );", "}" ].join("\n") } }; /** * @author supereggbert / http://www.paulbrunt.co.uk/ * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * @author szimek / https://github.com/szimek/ */ THREE.WebGLRenderer = function ( parameters ) { console.log( 'THREE.WebGLRenderer', THREE.REVISION ); parameters = parameters || {}; var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ), _context = parameters.context !== undefined ? parameters.context : null, _precision = parameters.precision !== undefined ? parameters.precision : 'highp', _alpha = parameters.alpha !== undefined ? parameters.alpha : false, _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true, _antialias = parameters.antialias !== undefined ? parameters.antialias : false, _stencil = parameters.stencil !== undefined ? parameters.stencil : true, _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false, _clearColor = new THREE.Color( 0x000000 ), _clearAlpha = 0; // public properties this.domElement = _canvas; this.context = null; this.devicePixelRatio = parameters.devicePixelRatio !== undefined ? parameters.devicePixelRatio : self.devicePixelRatio !== undefined ? self.devicePixelRatio : 1; // clearing this.autoClear = true; this.autoClearColor = true; this.autoClearDepth = true; this.autoClearStencil = true; // scene graph this.sortObjects = true; this.autoUpdateObjects = true; // physically based shading this.gammaInput = false; this.gammaOutput = false; // shadow map this.shadowMapEnabled = false; this.shadowMapAutoUpdate = true; this.shadowMapType = THREE.PCFShadowMap; this.shadowMapCullFace = THREE.CullFaceFront; this.shadowMapDebug = false; this.shadowMapCascade = false; // morphs this.maxMorphTargets = 8; this.maxMorphNormals = 4; // flags this.autoScaleCubemaps = true; // custom render plugins this.renderPluginsPre = []; this.renderPluginsPost = []; // info this.info = { memory: { programs: 0, geometries: 0, textures: 0 }, render: { calls: 0, vertices: 0, faces: 0, points: 0 } }; // internal properties var _this = this, _programs = [], _programs_counter = 0, // internal state cache _currentProgram = null, _currentFramebuffer = null, _currentMaterialId = -1, _currentGeometryGroupHash = null, _currentCamera = null, _geometryGroupCounter = 0, _usedTextureUnits = 0, // GL state cache _oldDoubleSided = -1, _oldFlipSided = -1, _oldBlending = -1, _oldBlendEquation = -1, _oldBlendSrc = -1, _oldBlendDst = -1, _oldDepthTest = -1, _oldDepthWrite = -1, _oldPolygonOffset = null, _oldPolygonOffsetFactor = null, _oldPolygonOffsetUnits = null, _oldLineWidth = null, _viewportX = 0, _viewportY = 0, _viewportWidth = _canvas.width, _viewportHeight = _canvas.height, _currentWidth = 0, _currentHeight = 0, _enabledAttributes = {}, // frustum _frustum = new THREE.Frustum(), // camera matrices cache _projScreenMatrix = new THREE.Matrix4(), _projScreenMatrixPS = new THREE.Matrix4(), _vector3 = new THREE.Vector3(), // light arrays cache _direction = new THREE.Vector3(), _lightsNeedUpdate = true, _lights = { ambient: [ 0, 0, 0 ], directional: { length: 0, colors: new Array(), positions: new Array() }, point: { length: 0, colors: new Array(), positions: new Array(), distances: new Array() }, spot: { length: 0, colors: new Array(), positions: new Array(), distances: new Array(), directions: new Array(), anglesCos: new Array(), exponents: new Array() }, hemi: { length: 0, skyColors: new Array(), groundColors: new Array(), positions: new Array() } }; // initialize var _gl; var _glExtensionTextureFloat; var _glExtensionTextureFloatLinear; var _glExtensionStandardDerivatives; var _glExtensionTextureFilterAnisotropic; var _glExtensionCompressedTextureS3TC; initGL(); setDefaultGLState(); this.context = _gl; // GPU capabilities var _maxTextures = _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS ); var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ); var _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE ); var _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE ); var _maxAnisotropy = _glExtensionTextureFilterAnisotropic ? _gl.getParameter( _glExtensionTextureFilterAnisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT ) : 0; var _supportsVertexTextures = ( _maxVertexTextures > 0 ); var _supportsBoneTextures = _supportsVertexTextures && _glExtensionTextureFloat; var _compressedTextureFormats = _glExtensionCompressedTextureS3TC ? _gl.getParameter( _gl.COMPRESSED_TEXTURE_FORMATS ) : []; // var _vertexShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.HIGH_FLOAT ); var _vertexShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.MEDIUM_FLOAT ); var _vertexShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.LOW_FLOAT ); var _fragmentShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.HIGH_FLOAT ); var _fragmentShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.MEDIUM_FLOAT ); var _fragmentShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.LOW_FLOAT ); var _vertexShaderPrecisionHighpInt = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.HIGH_INT ); var _vertexShaderPrecisionMediumpInt = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.MEDIUM_INT ); var _vertexShaderPrecisionLowpInt = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.LOW_INT ); var _fragmentShaderPrecisionHighpInt = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.HIGH_INT ); var _fragmentShaderPrecisionMediumpInt = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.MEDIUM_INT ); var _fragmentShaderPrecisionLowpInt = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.LOW_INT ); // clamp precision to maximum available var highpAvailable = _vertexShaderPrecisionHighpFloat.precision > 0 && _fragmentShaderPrecisionHighpFloat.precision > 0; var mediumpAvailable = _vertexShaderPrecisionMediumpFloat.precision > 0 && _fragmentShaderPrecisionMediumpFloat.precision > 0; if ( _precision === "highp" && ! highpAvailable ) { if ( mediumpAvailable ) { _precision = "mediump"; console.warn( "WebGLRenderer: highp not supported, using mediump" ); } else { _precision = "lowp"; console.warn( "WebGLRenderer: highp and mediump not supported, using lowp" ); } } if ( _precision === "mediump" && ! mediumpAvailable ) { _precision = "lowp"; console.warn( "WebGLRenderer: mediump not supported, using lowp" ); } // API this.getContext = function () { return _gl; }; this.supportsVertexTextures = function () { return _supportsVertexTextures; }; this.supportsFloatTextures = function () { return _glExtensionTextureFloat; }; this.supportsStandardDerivatives = function () { return _glExtensionStandardDerivatives; }; this.supportsCompressedTextureS3TC = function () { return _glExtensionCompressedTextureS3TC; }; this.getMaxAnisotropy = function () { return _maxAnisotropy; }; this.getPrecision = function () { return _precision; }; this.setSize = function ( width, height, updateStyle ) { _canvas.width = width * this.devicePixelRatio; _canvas.height = height * this.devicePixelRatio; if ( this.devicePixelRatio !== 1 && updateStyle !== false ) { _canvas.style.width = width + 'px'; _canvas.style.height = height + 'px'; } this.setViewport( 0, 0, width, height ); }; this.setViewport = function ( x, y, width, height ) { _viewportX = x * this.devicePixelRatio; _viewportY = y * this.devicePixelRatio; _viewportWidth = width * this.devicePixelRatio; _viewportHeight = height * this.devicePixelRatio; _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight ); }; this.setScissor = function ( x, y, width, height ) { _gl.scissor( x * this.devicePixelRatio, y * this.devicePixelRatio, width * this.devicePixelRatio, height * this.devicePixelRatio ); }; this.enableScissorTest = function ( enable ) { enable ? _gl.enable( _gl.SCISSOR_TEST ) : _gl.disable( _gl.SCISSOR_TEST ); }; // Clearing this.setClearColor = function ( color, alpha ) { _clearColor.set( color ); _clearAlpha = alpha !== undefined ? alpha : 1; _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha ); }; this.setClearColorHex = function ( hex, alpha ) { console.warn( 'DEPRECATED: .setClearColorHex() is being removed. Use .setClearColor() instead.' ); this.setClearColor( hex, alpha ); }; this.getClearColor = function () { return _clearColor; }; this.getClearAlpha = function () { return _clearAlpha; }; this.clear = function ( color, depth, stencil ) { var bits = 0; if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT; if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT; if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT; _gl.clear( bits ); }; this.clearColor = function () { _gl.clear( _gl.COLOR_BUFFER_BIT ); }; this.clearDepth = function () { _gl.clear( _gl.DEPTH_BUFFER_BIT ); }; this.clearStencil = function () { _gl.clear( _gl.STENCIL_BUFFER_BIT ); }; this.clearTarget = function ( renderTarget, color, depth, stencil ) { this.setRenderTarget( renderTarget ); this.clear( color, depth, stencil ); }; // Plugins this.addPostPlugin = function ( plugin ) { plugin.init( this ); this.renderPluginsPost.push( plugin ); }; this.addPrePlugin = function ( plugin ) { plugin.init( this ); this.renderPluginsPre.push( plugin ); }; // Rendering this.updateShadowMap = function ( scene, camera ) { _currentProgram = null; _oldBlending = -1; _oldDepthTest = -1; _oldDepthWrite = -1; _currentGeometryGroupHash = -1; _currentMaterialId = -1; _lightsNeedUpdate = true; _oldDoubleSided = -1; _oldFlipSided = -1; this.shadowMapPlugin.update( scene, camera ); }; // Internal functions // Buffer allocation function createParticleBuffers ( geometry ) { geometry.__webglVertexBuffer = _gl.createBuffer(); geometry.__webglColorBuffer = _gl.createBuffer(); _this.info.memory.geometries ++; }; function createLineBuffers ( geometry ) { geometry.__webglVertexBuffer = _gl.createBuffer(); geometry.__webglColorBuffer = _gl.createBuffer(); geometry.__webglLineDistanceBuffer = _gl.createBuffer(); _this.info.memory.geometries ++; }; function createMeshBuffers ( geometryGroup ) { geometryGroup.__webglVertexBuffer = _gl.createBuffer(); geometryGroup.__webglNormalBuffer = _gl.createBuffer(); geometryGroup.__webglTangentBuffer = _gl.createBuffer(); geometryGroup.__webglColorBuffer = _gl.createBuffer(); geometryGroup.__webglUVBuffer = _gl.createBuffer(); geometryGroup.__webglUV2Buffer = _gl.createBuffer(); geometryGroup.__webglSkinIndicesBuffer = _gl.createBuffer(); geometryGroup.__webglSkinWeightsBuffer = _gl.createBuffer(); geometryGroup.__webglFaceBuffer = _gl.createBuffer(); geometryGroup.__webglLineBuffer = _gl.createBuffer(); var m, ml; if ( geometryGroup.numMorphTargets ) { geometryGroup.__webglMorphTargetsBuffers = []; for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) { geometryGroup.__webglMorphTargetsBuffers.push( _gl.createBuffer() ); } } if ( geometryGroup.numMorphNormals ) { geometryGroup.__webglMorphNormalsBuffers = []; for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) { geometryGroup.__webglMorphNormalsBuffers.push( _gl.createBuffer() ); } } _this.info.memory.geometries ++; }; // Events var onGeometryDispose = function ( event ) { var geometry = event.target; geometry.removeEventListener( 'dispose', onGeometryDispose ); deallocateGeometry( geometry ); }; var onTextureDispose = function ( event ) { var texture = event.target; texture.removeEventListener( 'dispose', onTextureDispose ); deallocateTexture( texture ); _this.info.memory.textures --; }; var onRenderTargetDispose = function ( event ) { var renderTarget = event.target; renderTarget.removeEventListener( 'dispose', onRenderTargetDispose ); deallocateRenderTarget( renderTarget ); _this.info.memory.textures --; }; var onMaterialDispose = function ( event ) { var material = event.target; material.removeEventListener( 'dispose', onMaterialDispose ); deallocateMaterial( material ); }; // Buffer deallocation var deleteBuffers = function ( geometry ) { if ( geometry.__webglVertexBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglVertexBuffer ); if ( geometry.__webglNormalBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglNormalBuffer ); if ( geometry.__webglTangentBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglTangentBuffer ); if ( geometry.__webglColorBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglColorBuffer ); if ( geometry.__webglUVBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglUVBuffer ); if ( geometry.__webglUV2Buffer !== undefined ) _gl.deleteBuffer( geometry.__webglUV2Buffer ); if ( geometry.__webglSkinIndicesBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglSkinIndicesBuffer ); if ( geometry.__webglSkinWeightsBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglSkinWeightsBuffer ); if ( geometry.__webglFaceBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglFaceBuffer ); if ( geometry.__webglLineBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglLineBuffer ); if ( geometry.__webglLineDistanceBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglLineDistanceBuffer ); // custom attributes if ( geometry.__webglCustomAttributesList !== undefined ) { for ( var id in geometry.__webglCustomAttributesList ) { _gl.deleteBuffer( geometry.__webglCustomAttributesList[ id ].buffer ); } } _this.info.memory.geometries --; }; var deallocateGeometry = function ( geometry ) { geometry.__webglInit = undefined; if ( geometry instanceof THREE.BufferGeometry ) { var attributes = geometry.attributes; for ( var key in attributes ) { if ( attributes[ key ].buffer !== undefined ) { _gl.deleteBuffer( attributes[ key ].buffer ); } } _this.info.memory.geometries --; } else { if ( geometry.geometryGroups !== undefined ) { for ( var g in geometry.geometryGroups ) { var geometryGroup = geometry.geometryGroups[ g ]; if ( geometryGroup.numMorphTargets !== undefined ) { for ( var m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) { _gl.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] ); } } if ( geometryGroup.numMorphNormals !== undefined ) { for ( var m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) { _gl.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] ); } } deleteBuffers( geometryGroup ); } } else { deleteBuffers( geometry ); } } }; var deallocateTexture = function ( texture ) { if ( texture.image && texture.image.__webglTextureCube ) { // cube texture _gl.deleteTexture( texture.image.__webglTextureCube ); } else { // 2D texture if ( ! texture.__webglInit ) return; texture.__webglInit = false; _gl.deleteTexture( texture.__webglTexture ); } }; var deallocateRenderTarget = function ( renderTarget ) { if ( !renderTarget || ! renderTarget.__webglTexture ) return; _gl.deleteTexture( renderTarget.__webglTexture ); if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) { for ( var i = 0; i < 6; i ++ ) { _gl.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] ); _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] ); } } else { _gl.deleteFramebuffer( renderTarget.__webglFramebuffer ); _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer ); } }; var deallocateMaterial = function ( material ) { var program = material.program; if ( program === undefined ) return; material.program = undefined; // only deallocate GL program if this was the last use of shared program // assumed there is only single copy of any program in the _programs list // (that's how it's constructed) var i, il, programInfo; var deleteProgram = false; for ( i = 0, il = _programs.length; i < il; i ++ ) { programInfo = _programs[ i ]; if ( programInfo.program === program ) { programInfo.usedTimes --; if ( programInfo.usedTimes === 0 ) { deleteProgram = true; } break; } } if ( deleteProgram === true ) { // avoid using array.splice, this is costlier than creating new array from scratch var newPrograms = []; for ( i = 0, il = _programs.length; i < il; i ++ ) { programInfo = _programs[ i ]; if ( programInfo.program !== program ) { newPrograms.push( programInfo ); } } _programs = newPrograms; _gl.deleteProgram( program ); _this.info.memory.programs --; } }; // Buffer initialization function initCustomAttributes ( geometry, object ) { var nvertices = geometry.vertices.length; var material = object.material; if ( material.attributes ) { if ( geometry.__webglCustomAttributesList === undefined ) { geometry.__webglCustomAttributesList = []; } for ( var a in material.attributes ) { var attribute = material.attributes[ a ]; if ( !attribute.__webglInitialized || attribute.createUniqueBuffers ) { attribute.__webglInitialized = true; var size = 1; // "f" and "i" if ( attribute.type === "v2" ) size = 2; else if ( attribute.type === "v3" ) size = 3; else if ( attribute.type === "v4" ) size = 4; else if ( attribute.type === "c" ) size = 3; attribute.size = size; attribute.array = new Float32Array( nvertices * size ); attribute.buffer = _gl.createBuffer(); attribute.buffer.belongsToAttribute = a; attribute.needsUpdate = true; } geometry.__webglCustomAttributesList.push( attribute ); } } }; function initParticleBuffers ( geometry, object ) { var nvertices = geometry.vertices.length; geometry.__vertexArray = new Float32Array( nvertices * 3 ); geometry.__colorArray = new Float32Array( nvertices * 3 ); geometry.__sortArray = []; geometry.__webglParticleCount = nvertices; initCustomAttributes ( geometry, object ); }; function initLineBuffers ( geometry, object ) { var nvertices = geometry.vertices.length; geometry.__vertexArray = new Float32Array( nvertices * 3 ); geometry.__colorArray = new Float32Array( nvertices * 3 ); geometry.__lineDistanceArray = new Float32Array( nvertices * 1 ); geometry.__webglLineCount = nvertices; initCustomAttributes ( geometry, object ); }; function initMeshBuffers ( geometryGroup, object ) { var geometry = object.geometry, faces3 = geometryGroup.faces3, nvertices = faces3.length * 3, ntris = faces3.length * 1, nlines = faces3.length * 3, material = getBufferMaterial( object, geometryGroup ), uvType = bufferGuessUVType( material ), normalType = bufferGuessNormalType( material ), vertexColorType = bufferGuessVertexColorType( material ); // console.log( "uvType", uvType, "normalType", normalType, "vertexColorType", vertexColorType, object, geometryGroup, material ); geometryGroup.__vertexArray = new Float32Array( nvertices * 3 ); if ( normalType ) { geometryGroup.__normalArray = new Float32Array( nvertices * 3 ); } if ( geometry.hasTangents ) { geometryGroup.__tangentArray = new Float32Array( nvertices * 4 ); } if ( vertexColorType ) { geometryGroup.__colorArray = new Float32Array( nvertices * 3 ); } if ( uvType ) { if ( geometry.faceVertexUvs.length > 0 ) { geometryGroup.__uvArray = new Float32Array( nvertices * 2 ); } if ( geometry.faceVertexUvs.length > 1 ) { geometryGroup.__uv2Array = new Float32Array( nvertices * 2 ); } } if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) { geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 ); geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 ); } geometryGroup.__faceArray = new Uint16Array( ntris * 3 ); geometryGroup.__lineArray = new Uint16Array( nlines * 2 ); var m, ml; if ( geometryGroup.numMorphTargets ) { geometryGroup.__morphTargetsArrays = []; for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) { geometryGroup.__morphTargetsArrays.push( new Float32Array( nvertices * 3 ) ); } } if ( geometryGroup.numMorphNormals ) { geometryGroup.__morphNormalsArrays = []; for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) { geometryGroup.__morphNormalsArrays.push( new Float32Array( nvertices * 3 ) ); } } geometryGroup.__webglFaceCount = ntris * 3; geometryGroup.__webglLineCount = nlines * 2; // custom attributes if ( material.attributes ) { if ( geometryGroup.__webglCustomAttributesList === undefined ) { geometryGroup.__webglCustomAttributesList = []; } for ( var a in material.attributes ) { // Do a shallow copy of the attribute object so different geometryGroup chunks use different // attribute buffers which are correctly indexed in the setMeshBuffers function var originalAttribute = material.attributes[ a ]; var attribute = {}; for ( var property in originalAttribute ) { attribute[ property ] = originalAttribute[ property ]; } if ( !attribute.__webglInitialized || attribute.createUniqueBuffers ) { attribute.__webglInitialized = true; var size = 1; // "f" and "i" if( attribute.type === "v2" ) size = 2; else if( attribute.type === "v3" ) size = 3; else if( attribute.type === "v4" ) size = 4; else if( attribute.type === "c" ) size = 3; attribute.size = size; attribute.array = new Float32Array( nvertices * size ); attribute.buffer = _gl.createBuffer(); attribute.buffer.belongsToAttribute = a; originalAttribute.needsUpdate = true; attribute.__original = originalAttribute; } geometryGroup.__webglCustomAttributesList.push( attribute ); } } geometryGroup.__inittedArrays = true; }; function getBufferMaterial( object, geometryGroup ) { return object.material instanceof THREE.MeshFaceMaterial ? object.material.materials[ geometryGroup.materialIndex ] : object.material; }; function materialNeedsSmoothNormals ( material ) { return material && material.shading !== undefined && material.shading === THREE.SmoothShading; }; function bufferGuessNormalType ( material ) { // only MeshBasicMaterial and MeshDepthMaterial don't need normals if ( ( material instanceof THREE.MeshBasicMaterial && !material.envMap ) || material instanceof THREE.MeshDepthMaterial ) { return false; } if ( materialNeedsSmoothNormals( material ) ) { return THREE.SmoothShading; } else { return THREE.FlatShading; } }; function bufferGuessVertexColorType( material ) { if ( material.vertexColors ) { return material.vertexColors; } return false; }; function bufferGuessUVType( material ) { // material must use some texture to require uvs if ( material.map || material.lightMap || material.bumpMap || material.normalMap || material.specularMap || material instanceof THREE.ShaderMaterial ) { return true; } return false; }; // function initDirectBuffers( geometry ) { var a, attribute, type; for ( a in geometry.attributes ) { if ( a === "index" ) { type = _gl.ELEMENT_ARRAY_BUFFER; } else { type = _gl.ARRAY_BUFFER; } attribute = geometry.attributes[ a ]; attribute.buffer = _gl.createBuffer(); _gl.bindBuffer( type, attribute.buffer ); _gl.bufferData( type, attribute.array, _gl.STATIC_DRAW ); } }; // Buffer setting function setParticleBuffers ( geometry, hint, object ) { var v, c, vertex, offset, index, color, vertices = geometry.vertices, vl = vertices.length, colors = geometry.colors, cl = colors.length, vertexArray = geometry.__vertexArray, colorArray = geometry.__colorArray, sortArray = geometry.__sortArray, dirtyVertices = geometry.verticesNeedUpdate, dirtyElements = geometry.elementsNeedUpdate, dirtyColors = geometry.colorsNeedUpdate, customAttributes = geometry.__webglCustomAttributesList, i, il, a, ca, cal, value, customAttribute; if ( object.sortParticles ) { _projScreenMatrixPS.copy( _projScreenMatrix ); _projScreenMatrixPS.multiply( object.matrixWorld ); for ( v = 0; v < vl; v ++ ) { vertex = vertices[ v ]; _vector3.copy( vertex ); _vector3.applyProjection( _projScreenMatrixPS ); sortArray[ v ] = [ _vector3.z, v ]; } sortArray.sort( numericalSort ); for ( v = 0; v < vl; v ++ ) { vertex = vertices[ sortArray[v][1] ]; offset = v * 3; vertexArray[ offset ] = vertex.x; vertexArray[ offset + 1 ] = vertex.y; vertexArray[ offset + 2 ] = vertex.z; } for ( c = 0; c < cl; c ++ ) { offset = c * 3; color = colors[ sortArray[c][1] ]; colorArray[ offset ] = color.r; colorArray[ offset + 1 ] = color.g; colorArray[ offset + 2 ] = color.b; } if ( customAttributes ) { for ( i = 0, il = customAttributes.length; i < il; i ++ ) { customAttribute = customAttributes[ i ]; if ( ! ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) ) continue; offset = 0; cal = customAttribute.value.length; if ( customAttribute.size === 1 ) { for ( ca = 0; ca < cal; ca ++ ) { index = sortArray[ ca ][ 1 ]; customAttribute.array[ ca ] = customAttribute.value[ index ]; } } else if ( customAttribute.size === 2 ) { for ( ca = 0; ca < cal; ca ++ ) { index = sortArray[ ca ][ 1 ]; value = customAttribute.value[ index ]; customAttribute.array[ offset ] = value.x; customAttribute.array[ offset + 1 ] = value.y; offset += 2; } } else if ( customAttribute.size === 3 ) { if ( customAttribute.type === "c" ) { for ( ca = 0; ca < cal; ca ++ ) { index = sortArray[ ca ][ 1 ]; value = customAttribute.value[ index ]; customAttribute.array[ offset ] = value.r; customAttribute.array[ offset + 1 ] = value.g; customAttribute.array[ offset + 2 ] = value.b; offset += 3; } } else { for ( ca = 0; ca < cal; ca ++ ) { index = sortArray[ ca ][ 1 ]; value = customAttribute.value[ index ]; customAttribute.array[ offset ] = value.x; customAttribute.array[ offset + 1 ] = value.y; customAttribute.array[ offset + 2 ] = value.z; offset += 3; } } } else if ( customAttribute.size === 4 ) { for ( ca = 0; ca < cal; ca ++ ) { index = sortArray[ ca ][ 1 ]; value = customAttribute.value[ index ]; customAttribute.array[ offset ] = value.x; customAttribute.array[ offset + 1 ] = value.y; customAttribute.array[ offset + 2 ] = value.z; customAttribute.array[ offset + 3 ] = value.w; offset += 4; } } } } } else { if ( dirtyVertices ) { for ( v = 0; v < vl; v ++ ) { vertex = vertices[ v ]; offset = v * 3; vertexArray[ offset ] = vertex.x; vertexArray[ offset + 1 ] = vertex.y; vertexArray[ offset + 2 ] = vertex.z; } } if ( dirtyColors ) { for ( c = 0; c < cl; c ++ ) { color = colors[ c ]; offset = c * 3; colorArray[ offset ] = color.r; colorArray[ offset + 1 ] = color.g; colorArray[ offset + 2 ] = color.b; } } if ( customAttributes ) { for ( i = 0, il = customAttributes.length; i < il; i ++ ) { customAttribute = customAttributes[ i ]; if ( customAttribute.needsUpdate && ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices") ) { cal = customAttribute.value.length; offset = 0; if ( customAttribute.size === 1 ) { for ( ca = 0; ca < cal; ca ++ ) { customAttribute.array[ ca ] = customAttribute.value[ ca ]; } } else if ( customAttribute.size === 2 ) { for ( ca = 0; ca < cal; ca ++ ) { value = customAttribute.value[ ca ]; customAttribute.array[ offset ] = value.x; customAttribute.array[ offset + 1 ] = value.y; offset += 2; } } else if ( customAttribute.size === 3 ) { if ( customAttribute.type === "c" ) { for ( ca = 0; ca < cal; ca ++ ) { value = customAttribute.value[ ca ]; customAttribute.array[ offset ] = value.r; customAttribute.array[ offset + 1 ] = value.g; customAttribute.array[ offset + 2 ] = value.b; offset += 3; } } else { for ( ca = 0; ca < cal; ca ++ ) { value = customAttribute.value[ ca ]; customAttribute.array[ offset ] = value.x; customAttribute.array[ offset + 1 ] = value.y; customAttribute.array[ offset + 2 ] = value.z; offset += 3; } } } else if ( customAttribute.size === 4 ) { for ( ca = 0; ca < cal; ca ++ ) { value = customAttribute.value[ ca ]; customAttribute.array[ offset ] = value.x; customAttribute.array[ offset + 1 ] = value.y; customAttribute.array[ offset + 2 ] = value.z; customAttribute.array[ offset + 3 ] = value.w; offset += 4; } } } } } } if ( dirtyVertices || object.sortParticles ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint ); } if ( dirtyColors || object.sortParticles ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint ); } if ( customAttributes ) { for ( i = 0, il = customAttributes.length; i < il; i ++ ) { customAttribute = customAttributes[ i ]; if ( customAttribute.needsUpdate || object.sortParticles ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer ); _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint ); } } } }; function setLineBuffers ( geometry, hint ) { var v, c, d, vertex, offset, color, vertices = geometry.vertices, colors = geometry.colors, lineDistances = geometry.lineDistances, vl = vertices.length, cl = colors.length, dl = lineDistances.length, vertexArray = geometry.__vertexArray, colorArray = geometry.__colorArray, lineDistanceArray = geometry.__lineDistanceArray, dirtyVertices = geometry.verticesNeedUpdate, dirtyColors = geometry.colorsNeedUpdate, dirtyLineDistances = geometry.lineDistancesNeedUpdate, customAttributes = geometry.__webglCustomAttributesList, i, il, a, ca, cal, value, customAttribute; if ( dirtyVertices ) { for ( v = 0; v < vl; v ++ ) { vertex = vertices[ v ]; offset = v * 3; vertexArray[ offset ] = vertex.x; vertexArray[ offset + 1 ] = vertex.y; vertexArray[ offset + 2 ] = vertex.z; } _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint ); } if ( dirtyColors ) { for ( c = 0; c < cl; c ++ ) { color = colors[ c ]; offset = c * 3; colorArray[ offset ] = color.r; colorArray[ offset + 1 ] = color.g; colorArray[ offset + 2 ] = color.b; } _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint ); } if ( dirtyLineDistances ) { for ( d = 0; d < dl; d ++ ) { lineDistanceArray[ d ] = lineDistances[ d ]; } _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglLineDistanceBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, lineDistanceArray, hint ); } if ( customAttributes ) { for ( i = 0, il = customAttributes.length; i < il; i ++ ) { customAttribute = customAttributes[ i ]; if ( customAttribute.needsUpdate && ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) ) { offset = 0; cal = customAttribute.value.length; if ( customAttribute.size === 1 ) { for ( ca = 0; ca < cal; ca ++ ) { customAttribute.array[ ca ] = customAttribute.value[ ca ]; } } else if ( customAttribute.size === 2 ) { for ( ca = 0; ca < cal; ca ++ ) { value = customAttribute.value[ ca ]; customAttribute.array[ offset ] = value.x; customAttribute.array[ offset + 1 ] = value.y; offset += 2; } } else if ( customAttribute.size === 3 ) { if ( customAttribute.type === "c" ) { for ( ca = 0; ca < cal; ca ++ ) { value = customAttribute.value[ ca ]; customAttribute.array[ offset ] = value.r; customAttribute.array[ offset + 1 ] = value.g; customAttribute.array[ offset + 2 ] = value.b; offset += 3; } } else { for ( ca = 0; ca < cal; ca ++ ) { value = customAttribute.value[ ca ]; customAttribute.array[ offset ] = value.x; customAttribute.array[ offset + 1 ] = value.y; customAttribute.array[ offset + 2 ] = value.z; offset += 3; } } } else if ( customAttribute.size === 4 ) { for ( ca = 0; ca < cal; ca ++ ) { value = customAttribute.value[ ca ]; customAttribute.array[ offset ] = value.x; customAttribute.array[ offset + 1 ] = value.y; customAttribute.array[ offset + 2 ] = value.z; customAttribute.array[ offset + 3 ] = value.w; offset += 4; } } _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer ); _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint ); } } } }; function setMeshBuffers( geometryGroup, object, hint, dispose, material ) { if ( ! geometryGroup.__inittedArrays ) { return; } var normalType = bufferGuessNormalType( material ), vertexColorType = bufferGuessVertexColorType( material ), uvType = bufferGuessUVType( material ), needsSmoothNormals = ( normalType === THREE.SmoothShading ); var f, fl, fi, face, vertexNormals, faceNormal, normal, vertexColors, faceColor, vertexTangents, uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4, n1, n2, n3, n4, c1, c2, c3, c4, sw1, sw2, sw3, sw4, si1, si2, si3, si4, sa1, sa2, sa3, sa4, sb1, sb2, sb3, sb4, m, ml, i, il, vn, uvi, uv2i, vk, vkl, vka, nka, chf, faceVertexNormals, a, vertexIndex = 0, offset = 0, offset_uv = 0, offset_uv2 = 0, offset_face = 0, offset_normal = 0, offset_tangent = 0, offset_line = 0, offset_color = 0, offset_skin = 0, offset_morphTarget = 0, offset_custom = 0, offset_customSrc = 0, value, vertexArray = geometryGroup.__vertexArray, uvArray = geometryGroup.__uvArray, uv2Array = geometryGroup.__uv2Array, normalArray = geometryGroup.__normalArray, tangentArray = geometryGroup.__tangentArray, colorArray = geometryGroup.__colorArray, skinIndexArray = geometryGroup.__skinIndexArray, skinWeightArray = geometryGroup.__skinWeightArray, morphTargetsArrays = geometryGroup.__morphTargetsArrays, morphNormalsArrays = geometryGroup.__morphNormalsArrays, customAttributes = geometryGroup.__webglCustomAttributesList, customAttribute, faceArray = geometryGroup.__faceArray, lineArray = geometryGroup.__lineArray, geometry = object.geometry, // this is shared for all chunks dirtyVertices = geometry.verticesNeedUpdate, dirtyElements = geometry.elementsNeedUpdate, dirtyUvs = geometry.uvsNeedUpdate, dirtyNormals = geometry.normalsNeedUpdate, dirtyTangents = geometry.tangentsNeedUpdate, dirtyColors = geometry.colorsNeedUpdate, dirtyMorphTargets = geometry.morphTargetsNeedUpdate, vertices = geometry.vertices, chunk_faces3 = geometryGroup.faces3, obj_faces = geometry.faces, obj_uvs = geometry.faceVertexUvs[ 0 ], obj_uvs2 = geometry.faceVertexUvs[ 1 ], obj_colors = geometry.colors, obj_skinIndices = geometry.skinIndices, obj_skinWeights = geometry.skinWeights, morphTargets = geometry.morphTargets, morphNormals = geometry.morphNormals; if ( dirtyVertices ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { face = obj_faces[ chunk_faces3[ f ] ]; v1 = vertices[ face.a ]; v2 = vertices[ face.b ]; v3 = vertices[ face.c ]; vertexArray[ offset ] = v1.x; vertexArray[ offset + 1 ] = v1.y; vertexArray[ offset + 2 ] = v1.z; vertexArray[ offset + 3 ] = v2.x; vertexArray[ offset + 4 ] = v2.y; vertexArray[ offset + 5 ] = v2.z; vertexArray[ offset + 6 ] = v3.x; vertexArray[ offset + 7 ] = v3.y; vertexArray[ offset + 8 ] = v3.z; offset += 9; } _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint ); } if ( dirtyMorphTargets ) { for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) { offset_morphTarget = 0; for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { chf = chunk_faces3[ f ]; face = obj_faces[ chf ]; // morph positions v1 = morphTargets[ vk ].vertices[ face.a ]; v2 = morphTargets[ vk ].vertices[ face.b ]; v3 = morphTargets[ vk ].vertices[ face.c ]; vka = morphTargetsArrays[ vk ]; vka[ offset_morphTarget ] = v1.x; vka[ offset_morphTarget + 1 ] = v1.y; vka[ offset_morphTarget + 2 ] = v1.z; vka[ offset_morphTarget + 3 ] = v2.x; vka[ offset_morphTarget + 4 ] = v2.y; vka[ offset_morphTarget + 5 ] = v2.z; vka[ offset_morphTarget + 6 ] = v3.x; vka[ offset_morphTarget + 7 ] = v3.y; vka[ offset_morphTarget + 8 ] = v3.z; // morph normals if ( material.morphNormals ) { if ( needsSmoothNormals ) { faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ]; n1 = faceVertexNormals.a; n2 = faceVertexNormals.b; n3 = faceVertexNormals.c; } else { n1 = morphNormals[ vk ].faceNormals[ chf ]; n2 = n1; n3 = n1; } nka = morphNormalsArrays[ vk ]; nka[ offset_morphTarget ] = n1.x; nka[ offset_morphTarget + 1 ] = n1.y; nka[ offset_morphTarget + 2 ] = n1.z; nka[ offset_morphTarget + 3 ] = n2.x; nka[ offset_morphTarget + 4 ] = n2.y; nka[ offset_morphTarget + 5 ] = n2.z; nka[ offset_morphTarget + 6 ] = n3.x; nka[ offset_morphTarget + 7 ] = n3.y; nka[ offset_morphTarget + 8 ] = n3.z; } // offset_morphTarget += 9; } _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ vk ] ); _gl.bufferData( _gl.ARRAY_BUFFER, morphTargetsArrays[ vk ], hint ); if ( material.morphNormals ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ vk ] ); _gl.bufferData( _gl.ARRAY_BUFFER, morphNormalsArrays[ vk ], hint ); } } } if ( obj_skinWeights.length ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { face = obj_faces[ chunk_faces3[ f ] ]; // weights sw1 = obj_skinWeights[ face.a ]; sw2 = obj_skinWeights[ face.b ]; sw3 = obj_skinWeights[ face.c ]; skinWeightArray[ offset_skin ] = sw1.x; skinWeightArray[ offset_skin + 1 ] = sw1.y; skinWeightArray[ offset_skin + 2 ] = sw1.z; skinWeightArray[ offset_skin + 3 ] = sw1.w; skinWeightArray[ offset_skin + 4 ] = sw2.x; skinWeightArray[ offset_skin + 5 ] = sw2.y; skinWeightArray[ offset_skin + 6 ] = sw2.z; skinWeightArray[ offset_skin + 7 ] = sw2.w; skinWeightArray[ offset_skin + 8 ] = sw3.x; skinWeightArray[ offset_skin + 9 ] = sw3.y; skinWeightArray[ offset_skin + 10 ] = sw3.z; skinWeightArray[ offset_skin + 11 ] = sw3.w; // indices si1 = obj_skinIndices[ face.a ]; si2 = obj_skinIndices[ face.b ]; si3 = obj_skinIndices[ face.c ]; skinIndexArray[ offset_skin ] = si1.x; skinIndexArray[ offset_skin + 1 ] = si1.y; skinIndexArray[ offset_skin + 2 ] = si1.z; skinIndexArray[ offset_skin + 3 ] = si1.w; skinIndexArray[ offset_skin + 4 ] = si2.x; skinIndexArray[ offset_skin + 5 ] = si2.y; skinIndexArray[ offset_skin + 6 ] = si2.z; skinIndexArray[ offset_skin + 7 ] = si2.w; skinIndexArray[ offset_skin + 8 ] = si3.x; skinIndexArray[ offset_skin + 9 ] = si3.y; skinIndexArray[ offset_skin + 10 ] = si3.z; skinIndexArray[ offset_skin + 11 ] = si3.w; offset_skin += 12; } if ( offset_skin > 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint ); _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint ); } } if ( dirtyColors && vertexColorType ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { face = obj_faces[ chunk_faces3[ f ] ]; vertexColors = face.vertexColors; faceColor = face.color; if ( vertexColors.length === 3 && vertexColorType === THREE.VertexColors ) { c1 = vertexColors[ 0 ]; c2 = vertexColors[ 1 ]; c3 = vertexColors[ 2 ]; } else { c1 = faceColor; c2 = faceColor; c3 = faceColor; } colorArray[ offset_color ] = c1.r; colorArray[ offset_color + 1 ] = c1.g; colorArray[ offset_color + 2 ] = c1.b; colorArray[ offset_color + 3 ] = c2.r; colorArray[ offset_color + 4 ] = c2.g; colorArray[ offset_color + 5 ] = c2.b; colorArray[ offset_color + 6 ] = c3.r; colorArray[ offset_color + 7 ] = c3.g; colorArray[ offset_color + 8 ] = c3.b; offset_color += 9; } if ( offset_color > 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint ); } } if ( dirtyTangents && geometry.hasTangents ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { face = obj_faces[ chunk_faces3[ f ] ]; vertexTangents = face.vertexTangents; t1 = vertexTangents[ 0 ]; t2 = vertexTangents[ 1 ]; t3 = vertexTangents[ 2 ]; tangentArray[ offset_tangent ] = t1.x; tangentArray[ offset_tangent + 1 ] = t1.y; tangentArray[ offset_tangent + 2 ] = t1.z; tangentArray[ offset_tangent + 3 ] = t1.w; tangentArray[ offset_tangent + 4 ] = t2.x; tangentArray[ offset_tangent + 5 ] = t2.y; tangentArray[ offset_tangent + 6 ] = t2.z; tangentArray[ offset_tangent + 7 ] = t2.w; tangentArray[ offset_tangent + 8 ] = t3.x; tangentArray[ offset_tangent + 9 ] = t3.y; tangentArray[ offset_tangent + 10 ] = t3.z; tangentArray[ offset_tangent + 11 ] = t3.w; offset_tangent += 12; } _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint ); } if ( dirtyNormals && normalType ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { face = obj_faces[ chunk_faces3[ f ] ]; vertexNormals = face.vertexNormals; faceNormal = face.normal; if ( vertexNormals.length === 3 && needsSmoothNormals ) { for ( i = 0; i < 3; i ++ ) { vn = vertexNormals[ i ]; normalArray[ offset_normal ] = vn.x; normalArray[ offset_normal + 1 ] = vn.y; normalArray[ offset_normal + 2 ] = vn.z; offset_normal += 3; } } else { for ( i = 0; i < 3; i ++ ) { normalArray[ offset_normal ] = faceNormal.x; normalArray[ offset_normal + 1 ] = faceNormal.y; normalArray[ offset_normal + 2 ] = faceNormal.z; offset_normal += 3; } } } _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint ); } if ( dirtyUvs && obj_uvs && uvType ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { fi = chunk_faces3[ f ]; uv = obj_uvs[ fi ]; if ( uv === undefined ) continue; for ( i = 0; i < 3; i ++ ) { uvi = uv[ i ]; uvArray[ offset_uv ] = uvi.x; uvArray[ offset_uv + 1 ] = uvi.y; offset_uv += 2; } } if ( offset_uv > 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint ); } } if ( dirtyUvs && obj_uvs2 && uvType ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { fi = chunk_faces3[ f ]; uv2 = obj_uvs2[ fi ]; if ( uv2 === undefined ) continue; for ( i = 0; i < 3; i ++ ) { uv2i = uv2[ i ]; uv2Array[ offset_uv2 ] = uv2i.x; uv2Array[ offset_uv2 + 1 ] = uv2i.y; offset_uv2 += 2; } } if ( offset_uv2 > 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer ); _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint ); } } if ( dirtyElements ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { faceArray[ offset_face ] = vertexIndex; faceArray[ offset_face + 1 ] = vertexIndex + 1; faceArray[ offset_face + 2 ] = vertexIndex + 2; offset_face += 3; lineArray[ offset_line ] = vertexIndex; lineArray[ offset_line + 1 ] = vertexIndex + 1; lineArray[ offset_line + 2 ] = vertexIndex; lineArray[ offset_line + 3 ] = vertexIndex + 2; lineArray[ offset_line + 4 ] = vertexIndex + 1; lineArray[ offset_line + 5 ] = vertexIndex + 2; offset_line += 6; vertexIndex += 3; } _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer ); _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint ); _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer ); _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint ); } if ( customAttributes ) { for ( i = 0, il = customAttributes.length; i < il; i ++ ) { customAttribute = customAttributes[ i ]; if ( ! customAttribute.__original.needsUpdate ) continue; offset_custom = 0; offset_customSrc = 0; if ( customAttribute.size === 1 ) { if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { face = obj_faces[ chunk_faces3[ f ] ]; customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ]; customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ]; customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ]; offset_custom += 3; } } else if ( customAttribute.boundTo === "faces" ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { value = customAttribute.value[ chunk_faces3[ f ] ]; customAttribute.array[ offset_custom ] = value; customAttribute.array[ offset_custom + 1 ] = value; customAttribute.array[ offset_custom + 2 ] = value; offset_custom += 3; } } } else if ( customAttribute.size === 2 ) { if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { face = obj_faces[ chunk_faces3[ f ] ]; v1 = customAttribute.value[ face.a ]; v2 = customAttribute.value[ face.b ]; v3 = customAttribute.value[ face.c ]; customAttribute.array[ offset_custom ] = v1.x; customAttribute.array[ offset_custom + 1 ] = v1.y; customAttribute.array[ offset_custom + 2 ] = v2.x; customAttribute.array[ offset_custom + 3 ] = v2.y; customAttribute.array[ offset_custom + 4 ] = v3.x; customAttribute.array[ offset_custom + 5 ] = v3.y; offset_custom += 6; } } else if ( customAttribute.boundTo === "faces" ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { value = customAttribute.value[ chunk_faces3[ f ] ]; v1 = value; v2 = value; v3 = value; customAttribute.array[ offset_custom ] = v1.x; customAttribute.array[ offset_custom + 1 ] = v1.y; customAttribute.array[ offset_custom + 2 ] = v2.x; customAttribute.array[ offset_custom + 3 ] = v2.y; customAttribute.array[ offset_custom + 4 ] = v3.x; customAttribute.array[ offset_custom + 5 ] = v3.y; offset_custom += 6; } } } else if ( customAttribute.size === 3 ) { var pp; if ( customAttribute.type === "c" ) { pp = [ "r", "g", "b" ]; } else { pp = [ "x", "y", "z" ]; } if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { face = obj_faces[ chunk_faces3[ f ] ]; v1 = customAttribute.value[ face.a ]; v2 = customAttribute.value[ face.b ]; v3 = customAttribute.value[ face.c ]; customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ]; customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ]; customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ]; customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ]; customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ]; customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ]; customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ]; customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ]; customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ]; offset_custom += 9; } } else if ( customAttribute.boundTo === "faces" ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { value = customAttribute.value[ chunk_faces3[ f ] ]; v1 = value; v2 = value; v3 = value; customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ]; customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ]; customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ]; customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ]; customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ]; customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ]; customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ]; customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ]; customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ]; offset_custom += 9; } } else if ( customAttribute.boundTo === "faceVertices" ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { value = customAttribute.value[ chunk_faces3[ f ] ]; v1 = value[ 0 ]; v2 = value[ 1 ]; v3 = value[ 2 ]; customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ]; customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ]; customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ]; customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ]; customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ]; customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ]; customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ]; customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ]; customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ]; offset_custom += 9; } } } else if ( customAttribute.size === 4 ) { if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { face = obj_faces[ chunk_faces3[ f ] ]; v1 = customAttribute.value[ face.a ]; v2 = customAttribute.value[ face.b ]; v3 = customAttribute.value[ face.c ]; customAttribute.array[ offset_custom ] = v1.x; customAttribute.array[ offset_custom + 1 ] = v1.y; customAttribute.array[ offset_custom + 2 ] = v1.z; customAttribute.array[ offset_custom + 3 ] = v1.w; customAttribute.array[ offset_custom + 4 ] = v2.x; customAttribute.array[ offset_custom + 5 ] = v2.y; customAttribute.array[ offset_custom + 6 ] = v2.z; customAttribute.array[ offset_custom + 7 ] = v2.w; customAttribute.array[ offset_custom + 8 ] = v3.x; customAttribute.array[ offset_custom + 9 ] = v3.y; customAttribute.array[ offset_custom + 10 ] = v3.z; customAttribute.array[ offset_custom + 11 ] = v3.w; offset_custom += 12; } } else if ( customAttribute.boundTo === "faces" ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { value = customAttribute.value[ chunk_faces3[ f ] ]; v1 = value; v2 = value; v3 = value; customAttribute.array[ offset_custom ] = v1.x; customAttribute.array[ offset_custom + 1 ] = v1.y; customAttribute.array[ offset_custom + 2 ] = v1.z; customAttribute.array[ offset_custom + 3 ] = v1.w; customAttribute.array[ offset_custom + 4 ] = v2.x; customAttribute.array[ offset_custom + 5 ] = v2.y; customAttribute.array[ offset_custom + 6 ] = v2.z; customAttribute.array[ offset_custom + 7 ] = v2.w; customAttribute.array[ offset_custom + 8 ] = v3.x; customAttribute.array[ offset_custom + 9 ] = v3.y; customAttribute.array[ offset_custom + 10 ] = v3.z; customAttribute.array[ offset_custom + 11 ] = v3.w; offset_custom += 12; } } else if ( customAttribute.boundTo === "faceVertices" ) { for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) { value = customAttribute.value[ chunk_faces3[ f ] ]; v1 = value[ 0 ]; v2 = value[ 1 ]; v3 = value[ 2 ]; customAttribute.array[ offset_custom ] = v1.x; customAttribute.array[ offset_custom + 1 ] = v1.y; customAttribute.array[ offset_custom + 2 ] = v1.z; customAttribute.array[ offset_custom + 3 ] = v1.w; customAttribute.array[ offset_custom + 4 ] = v2.x; customAttribute.array[ offset_custom + 5 ] = v2.y; customAttribute.array[ offset_custom + 6 ] = v2.z; customAttribute.array[ offset_custom + 7 ] = v2.w; customAttribute.array[ offset_custom + 8 ] = v3.x; customAttribute.array[ offset_custom + 9 ] = v3.y; customAttribute.array[ offset_custom + 10 ] = v3.z; customAttribute.array[ offset_custom + 11 ] = v3.w; offset_custom += 12; } } } _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer ); _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint ); } } if ( dispose ) { delete geometryGroup.__inittedArrays; delete geometryGroup.__colorArray; delete geometryGroup.__normalArray; delete geometryGroup.__tangentArray; delete geometryGroup.__uvArray; delete geometryGroup.__uv2Array; delete geometryGroup.__faceArray; delete geometryGroup.__vertexArray; delete geometryGroup.__lineArray; delete geometryGroup.__skinIndexArray; delete geometryGroup.__skinWeightArray; } }; // used by renderBufferDirect for THREE.Line function setupLinesVertexAttributes( material, programAttributes, geometryAttributes, startIndex ) { var attributeItem, attributeName, attributePointer, attributeSize; for ( attributeName in programAttributes ) { attributePointer = programAttributes[ attributeName ]; attributeItem = geometryAttributes[ attributeName ]; if ( attributePointer >= 0 ) { if ( attributeItem ) { attributeSize = attributeItem.itemSize; _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer ); enableAttribute( attributePointer ); _gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, startIndex * attributeSize * 4 ); // 4 bytes per Float32 } else if ( material.defaultAttributeValues ) { if ( material.defaultAttributeValues[ attributeName ].length === 2 ) { _gl.vertexAttrib2fv( attributePointer, material.defaultAttributeValues[ attributeName ] ); } else if ( material.defaultAttributeValues[ attributeName ].length === 3 ) { _gl.vertexAttrib3fv( attributePointer, material.defaultAttributeValues[ attributeName ] ); } } } } } function setDirectBuffers ( geometry, hint, dispose ) { var attributes = geometry.attributes; var attributeName, attributeItem; for ( attributeName in attributes ) { attributeItem = attributes[ attributeName ]; if ( attributeItem.needsUpdate ) { if ( attributeName === 'index' ) { _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, attributeItem.buffer ); _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, attributeItem.array, hint ); } else { _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer ); _gl.bufferData( _gl.ARRAY_BUFFER, attributeItem.array, hint ); } attributeItem.needsUpdate = false; } if ( dispose && ! attributeItem.dynamic ) { attributeItem.array = null; } } }; // Buffer rendering this.renderBufferImmediate = function ( object, program, material ) { if ( object.hasPositions && ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer(); if ( object.hasNormals && ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer(); if ( object.hasUvs && ! object.__webglUvBuffer ) object.__webglUvBuffer = _gl.createBuffer(); if ( object.hasColors && ! object.__webglColorBuffer ) object.__webglColorBuffer = _gl.createBuffer(); if ( object.hasPositions ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW ); _gl.enableVertexAttribArray( program.attributes.position ); _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 ); } if ( object.hasNormals ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer ); if ( material.shading === THREE.FlatShading ) { var nx, ny, nz, nax, nbx, ncx, nay, nby, ncy, naz, nbz, ncz, normalArray, i, il = object.count * 3; for( i = 0; i < il; i += 9 ) { normalArray = object.normalArray; nax = normalArray[ i ]; nay = normalArray[ i + 1 ]; naz = normalArray[ i + 2 ]; nbx = normalArray[ i + 3 ]; nby = normalArray[ i + 4 ]; nbz = normalArray[ i + 5 ]; ncx = normalArray[ i + 6 ]; ncy = normalArray[ i + 7 ]; ncz = normalArray[ i + 8 ]; nx = ( nax + nbx + ncx ) / 3; ny = ( nay + nby + ncy ) / 3; nz = ( naz + nbz + ncz ) / 3; normalArray[ i ] = nx; normalArray[ i + 1 ] = ny; normalArray[ i + 2 ] = nz; normalArray[ i + 3 ] = nx; normalArray[ i + 4 ] = ny; normalArray[ i + 5 ] = nz; normalArray[ i + 6 ] = nx; normalArray[ i + 7 ] = ny; normalArray[ i + 8 ] = nz; } } _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW ); _gl.enableVertexAttribArray( program.attributes.normal ); _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 ); } if ( object.hasUvs && material.map ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglUvBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW ); _gl.enableVertexAttribArray( program.attributes.uv ); _gl.vertexAttribPointer( program.attributes.uv, 2, _gl.FLOAT, false, 0, 0 ); } if ( object.hasColors && material.vertexColors !== THREE.NoColors ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglColorBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW ); _gl.enableVertexAttribArray( program.attributes.color ); _gl.vertexAttribPointer( program.attributes.color, 3, _gl.FLOAT, false, 0, 0 ); } _gl.drawArrays( _gl.TRIANGLES, 0, object.count ); object.count = 0; }; this.renderBufferDirect = function ( camera, lights, fog, material, geometry, object ) { if ( material.visible === false ) return; var linewidth, a, attribute; var attributeItem, attributeName, attributePointer, attributeSize; var program = setProgram( camera, lights, fog, material, object ); var programAttributes = program.attributes; var geometryAttributes = geometry.attributes; var updateBuffers = false, wireframeBit = material.wireframe ? 1 : 0, geometryHash = ( geometry.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit; if ( geometryHash !== _currentGeometryGroupHash ) { _currentGeometryGroupHash = geometryHash; updateBuffers = true; } if ( updateBuffers ) { disableAttributes(); } // render mesh if ( object instanceof THREE.Mesh ) { var index = geometryAttributes[ "index" ]; // indexed triangles if ( index ) { var offsets = geometry.offsets; // if there is more than 1 chunk // must set attribute pointers to use new offsets for each chunk // even if geometry and materials didn't change if ( offsets.length > 1 ) updateBuffers = true; for ( var i = 0, il = offsets.length; i < il; i ++ ) { var startIndex = offsets[ i ].index; if ( updateBuffers ) { for ( attributeName in programAttributes ) { attributePointer = programAttributes[ attributeName ]; attributeItem = geometryAttributes[ attributeName ]; if ( attributePointer >= 0 ) { if ( attributeItem ) { attributeSize = attributeItem.itemSize; _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer ); enableAttribute( attributePointer ); _gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, startIndex * attributeSize * 4 ); // 4 bytes per Float32 } else if ( material.defaultAttributeValues ) { if ( material.defaultAttributeValues[ attributeName ].length === 2 ) { _gl.vertexAttrib2fv( attributePointer, material.defaultAttributeValues[ attributeName ] ); } else if ( material.defaultAttributeValues[ attributeName ].length === 3 ) { _gl.vertexAttrib3fv( attributePointer, material.defaultAttributeValues[ attributeName ] ); } } } } // indices _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer ); } // render indexed triangles _gl.drawElements( _gl.TRIANGLES, offsets[ i ].count, _gl.UNSIGNED_SHORT, offsets[ i ].start * 2 ); // 2 bytes per Uint16 _this.info.render.calls ++; _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared _this.info.render.faces += offsets[ i ].count / 3; } // non-indexed triangles } else { if ( updateBuffers ) { for ( attributeName in programAttributes ) { if ( attributeName === 'index') continue; attributePointer = programAttributes[ attributeName ]; attributeItem = geometryAttributes[ attributeName ]; if ( attributePointer >= 0 ) { if ( attributeItem ) { attributeSize = attributeItem.itemSize; _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer ); enableAttribute( attributePointer ); _gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, 0 ); } else if ( material.defaultAttributeValues && material.defaultAttributeValues[ attributeName ] ) { if ( material.defaultAttributeValues[ attributeName ].length === 2 ) { _gl.vertexAttrib2fv( attributePointer, material.defaultAttributeValues[ attributeName ] ); } else if ( material.defaultAttributeValues[ attributeName ].length === 3 ) { _gl.vertexAttrib3fv( attributePointer, material.defaultAttributeValues[ attributeName ] ); } } } } } var position = geometry.attributes[ "position" ]; // render non-indexed triangles _gl.drawArrays( _gl.TRIANGLES, 0, position.array.length / 3 ); _this.info.render.calls ++; _this.info.render.vertices += position.array.length / 3; _this.info.render.faces += position.array.length / 3 / 3; } // render particles } else if ( object instanceof THREE.ParticleSystem ) { if ( updateBuffers ) { for ( attributeName in programAttributes ) { attributePointer = programAttributes[ attributeName ]; attributeItem = geometryAttributes[ attributeName ]; if ( attributePointer >= 0 ) { if ( attributeItem ) { attributeSize = attributeItem.itemSize; _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer ); enableAttribute( attributePointer ); _gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, 0 ); } else if ( material.defaultAttributeValues && material.defaultAttributeValues[ attributeName ] ) { if ( material.defaultAttributeValues[ attributeName ].length === 2 ) { _gl.vertexAttrib2fv( attributePointer, material.defaultAttributeValues[ attributeName ] ); } else if ( material.defaultAttributeValues[ attributeName ].length === 3 ) { _gl.vertexAttrib3fv( attributePointer, material.defaultAttributeValues[ attributeName ] ); } } } } } var position = geometryAttributes[ "position" ]; // render particles _gl.drawArrays( _gl.POINTS, 0, position.array.length / 3 ); _this.info.render.calls ++; _this.info.render.points += position.array.length / 3; } else if ( object instanceof THREE.Line ) { var primitives = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES; setLineWidth( material.linewidth ); var index = geometryAttributes[ "index" ]; // indexed lines if ( index ) { var offsets = geometry.offsets; // if there is more than 1 chunk // must set attribute pointers to use new offsets for each chunk // even if geometry and materials didn't change if ( offsets.length > 1 ) updateBuffers = true; for ( var i = 0, il = offsets.length; i < il; i ++ ) { var startIndex = offsets[ i ].index; if ( updateBuffers ) { setupLinesVertexAttributes(material, programAttributes, geometryAttributes, startIndex); // indices _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer ); } // render indexed lines _gl.drawElements( _gl.LINES, offsets[ i ].count, _gl.UNSIGNED_SHORT, offsets[ i ].start * 2 ); // 2 bytes per Uint16Array _this.info.render.calls ++; _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared } } // non-indexed lines else { if ( updateBuffers ) { setupLinesVertexAttributes(material, programAttributes, geometryAttributes, 0); } var position = geometryAttributes[ "position" ]; _gl.drawArrays( primitives, 0, position.array.length / 3 ); _this.info.render.calls ++; _this.info.render.points += position.array.length; } } }; this.renderBuffer = function ( camera, lights, fog, material, geometryGroup, object ) { if ( material.visible === false ) return; var linewidth, a, attribute, i, il; var program = setProgram( camera, lights, fog, material, object ); var attributes = program.attributes; var updateBuffers = false, wireframeBit = material.wireframe ? 1 : 0, geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit; if ( geometryGroupHash !== _currentGeometryGroupHash ) { _currentGeometryGroupHash = geometryGroupHash; updateBuffers = true; } if ( updateBuffers ) { disableAttributes(); } // vertices if ( !material.morphTargets && attributes.position >= 0 ) { if ( updateBuffers ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer ); enableAttribute( attributes.position ); _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 ); } } else { if ( object.morphTargetBase ) { setupMorphTargets( material, geometryGroup, object ); } } if ( updateBuffers ) { // custom attributes // Use the per-geometryGroup custom attribute arrays which are setup in initMeshBuffers if ( geometryGroup.__webglCustomAttributesList ) { for ( i = 0, il = geometryGroup.__webglCustomAttributesList.length; i < il; i ++ ) { attribute = geometryGroup.__webglCustomAttributesList[ i ]; if ( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer ); enableAttribute( attributes[ attribute.buffer.belongsToAttribute ] ); _gl.vertexAttribPointer( attributes[ attribute.buffer.belongsToAttribute ], attribute.size, _gl.FLOAT, false, 0, 0 ); } } } // colors if ( attributes.color >= 0 ) { if ( object.geometry.colors.length > 0 || object.geometry.faces.length > 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer ); enableAttribute( attributes.color ); _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 ); } else if ( material.defaultAttributeValues ) { _gl.vertexAttrib3fv( attributes.color, material.defaultAttributeValues.color ); } } // normals if ( attributes.normal >= 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer ); enableAttribute( attributes.normal ); _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 ); } // tangents if ( attributes.tangent >= 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer ); enableAttribute( attributes.tangent ); _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 ); } // uvs if ( attributes.uv >= 0 ) { if ( object.geometry.faceVertexUvs[0] ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer ); enableAttribute( attributes.uv ); _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 ); } else if ( material.defaultAttributeValues ) { _gl.vertexAttrib2fv( attributes.uv, material.defaultAttributeValues.uv ); } } if ( attributes.uv2 >= 0 ) { if ( object.geometry.faceVertexUvs[1] ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer ); enableAttribute( attributes.uv2 ); _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 ); } else if ( material.defaultAttributeValues ) { _gl.vertexAttrib2fv( attributes.uv2, material.defaultAttributeValues.uv2 ); } } if ( material.skinning && attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer ); enableAttribute( attributes.skinIndex ); _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 ); _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer ); enableAttribute( attributes.skinWeight ); _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 ); } // line distances if ( attributes.lineDistance >= 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglLineDistanceBuffer ); enableAttribute( attributes.lineDistance ); _gl.vertexAttribPointer( attributes.lineDistance, 1, _gl.FLOAT, false, 0, 0 ); } } // render mesh if ( object instanceof THREE.Mesh ) { // wireframe if ( material.wireframe ) { setLineWidth( material.wireframeLinewidth ); if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer ); _gl.drawElements( _gl.LINES, geometryGroup.__webglLineCount, _gl.UNSIGNED_SHORT, 0 ); // triangles } else { if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer ); _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, _gl.UNSIGNED_SHORT, 0 ); } _this.info.render.calls ++; _this.info.render.vertices += geometryGroup.__webglFaceCount; _this.info.render.faces += geometryGroup.__webglFaceCount / 3; // render lines } else if ( object instanceof THREE.Line ) { var primitives = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES; setLineWidth( material.linewidth ); _gl.drawArrays( primitives, 0, geometryGroup.__webglLineCount ); _this.info.render.calls ++; // render particles } else if ( object instanceof THREE.ParticleSystem ) { _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webglParticleCount ); _this.info.render.calls ++; _this.info.render.points += geometryGroup.__webglParticleCount; } }; function enableAttribute( attribute ) { if ( ! _enabledAttributes[ attribute ] ) { _gl.enableVertexAttribArray( attribute ); _enabledAttributes[ attribute ] = true; } }; function disableAttributes() { for ( var attribute in _enabledAttributes ) { if ( _enabledAttributes[ attribute ] ) { _gl.disableVertexAttribArray( attribute ); _enabledAttributes[ attribute ] = false; } } }; function setupMorphTargets ( material, geometryGroup, object ) { // set base var attributes = material.program.attributes; if ( object.morphTargetBase !== -1 && attributes.position >= 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] ); enableAttribute( attributes.position ); _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 ); } else if ( attributes.position >= 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer ); enableAttribute( attributes.position ); _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 ); } if ( object.morphTargetForcedOrder.length ) { // set forced order var m = 0; var order = object.morphTargetForcedOrder; var influences = object.morphTargetInfluences; while ( m < material.numSupportedMorphTargets && m < order.length ) { if ( attributes[ "morphTarget" + m ] >= 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] ); enableAttribute( attributes[ "morphTarget" + m ] ); _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 ); } if ( attributes[ "morphNormal" + m ] >= 0 && material.morphNormals ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] ); enableAttribute( attributes[ "morphNormal" + m ] ); _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 ); } object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ]; m ++; } } else { // find the most influencing var influence, activeInfluenceIndices = []; var influences = object.morphTargetInfluences; var i, il = influences.length; for ( i = 0; i < il; i ++ ) { influence = influences[ i ]; if ( influence > 0 ) { activeInfluenceIndices.push( [ influence, i ] ); } } if ( activeInfluenceIndices.length > material.numSupportedMorphTargets ) { activeInfluenceIndices.sort( numericalSort ); activeInfluenceIndices.length = material.numSupportedMorphTargets; } else if ( activeInfluenceIndices.length > material.numSupportedMorphNormals ) { activeInfluenceIndices.sort( numericalSort ); } else if ( activeInfluenceIndices.length === 0 ) { activeInfluenceIndices.push( [ 0, 0 ] ); }; var influenceIndex, m = 0; while ( m < material.numSupportedMorphTargets ) { if ( activeInfluenceIndices[ m ] ) { influenceIndex = activeInfluenceIndices[ m ][ 1 ]; if ( attributes[ "morphTarget" + m ] >= 0 ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ influenceIndex ] ); enableAttribute( attributes[ "morphTarget" + m ] ); _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 ); } if ( attributes[ "morphNormal" + m ] >= 0 && material.morphNormals ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ influenceIndex ] ); enableAttribute( attributes[ "morphNormal" + m ] ); _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 ); } object.__webglMorphTargetInfluences[ m ] = influences[ influenceIndex ]; } else { /* _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 ); if ( material.morphNormals ) { _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 ); } */ object.__webglMorphTargetInfluences[ m ] = 0; } m ++; } } // load updated influences uniform if ( material.program.uniforms.morphTargetInfluences !== null ) { _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences ); } }; // Sorting function painterSortStable ( a, b ) { if ( a.z !== b.z ) { return b.z - a.z; } else { return a.id - b.id; } }; function numericalSort ( a, b ) { return b[ 0 ] - a[ 0 ]; }; // Rendering this.render = function ( scene, camera, renderTarget, forceClear ) { if ( camera instanceof THREE.Camera === false ) { console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' ); return; } var i, il, webglObject, object, renderList, lights = scene.__lights, fog = scene.fog; // reset caching for this frame _currentMaterialId = -1; _lightsNeedUpdate = true; // update scene graph if ( scene.autoUpdate === true ) scene.updateMatrixWorld(); // update camera matrices and frustum if ( camera.parent === undefined ) camera.updateMatrixWorld(); camera.matrixWorldInverse.getInverse( camera.matrixWorld ); _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); _frustum.setFromMatrix( _projScreenMatrix ); // update WebGL objects if ( this.autoUpdateObjects ) this.initWebGLObjects( scene ); // custom render plugins (pre pass) renderPlugins( this.renderPluginsPre, scene, camera ); // _this.info.render.calls = 0; _this.info.render.vertices = 0; _this.info.render.faces = 0; _this.info.render.points = 0; this.setRenderTarget( renderTarget ); if ( this.autoClear || forceClear ) { this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil ); } // set matrices for regular objects (frustum culled) renderList = scene.__webglObjects; for ( i = 0, il = renderList.length; i < il; i ++ ) { webglObject = renderList[ i ]; object = webglObject.object; webglObject.id = i; webglObject.render = false; if ( object.visible ) { if ( ! ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem ) || ! ( object.frustumCulled ) || _frustum.intersectsObject( object ) ) { setupMatrices( object, camera ); unrollBufferMaterial( webglObject ); webglObject.render = true; if ( this.sortObjects === true ) { if ( object.renderDepth !== null ) { webglObject.z = object.renderDepth; } else { _vector3.setFromMatrixPosition( object.matrixWorld ); _vector3.applyProjection( _projScreenMatrix ); webglObject.z = _vector3.z; } } } } } if ( this.sortObjects ) { renderList.sort( painterSortStable ); } // set matrices for immediate objects renderList = scene.__webglObjectsImmediate; for ( i = 0, il = renderList.length; i < il; i ++ ) { webglObject = renderList[ i ]; object = webglObject.object; if ( object.visible ) { setupMatrices( object, camera ); unrollImmediateBufferMaterial( webglObject ); } } if ( scene.overrideMaterial ) { var material = scene.overrideMaterial; this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst ); this.setDepthTest( material.depthTest ); this.setDepthWrite( material.depthWrite ); setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits ); renderObjects( scene.__webglObjects, false, "", camera, lights, fog, true, material ); renderObjectsImmediate( scene.__webglObjectsImmediate, "", camera, lights, fog, false, material ); } else { var material = null; // opaque pass (front-to-back order) this.setBlending( THREE.NoBlending ); renderObjects( scene.__webglObjects, true, "opaque", camera, lights, fog, false, material ); renderObjectsImmediate( scene.__webglObjectsImmediate, "opaque", camera, lights, fog, false, material ); // transparent pass (back-to-front order) renderObjects( scene.__webglObjects, false, "transparent", camera, lights, fog, true, material ); renderObjectsImmediate( scene.__webglObjectsImmediate, "transparent", camera, lights, fog, true, material ); } // custom render plugins (post pass) renderPlugins( this.renderPluginsPost, scene, camera ); // Generate mipmap if we're using any kind of mipmap filtering if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) { updateRenderTargetMipmap( renderTarget ); } // Ensure depth buffer writing is enabled so it can be cleared on next render this.setDepthTest( true ); this.setDepthWrite( true ); // _gl.finish(); }; function renderPlugins( plugins, scene, camera ) { if ( ! plugins.length ) return; for ( var i = 0, il = plugins.length; i < il; i ++ ) { // reset state for plugin (to start from clean slate) _currentProgram = null; _currentCamera = null; _oldBlending = -1; _oldDepthTest = -1; _oldDepthWrite = -1; _oldDoubleSided = -1; _oldFlipSided = -1; _currentGeometryGroupHash = -1; _currentMaterialId = -1; _lightsNeedUpdate = true; plugins[ i ].render( scene, camera, _currentWidth, _currentHeight ); // reset state after plugin (anything could have changed) _currentProgram = null; _currentCamera = null; _oldBlending = -1; _oldDepthTest = -1; _oldDepthWrite = -1; _oldDoubleSided = -1; _oldFlipSided = -1; _currentGeometryGroupHash = -1; _currentMaterialId = -1; _lightsNeedUpdate = true; } }; function renderObjects ( renderList, reverse, materialType, camera, lights, fog, useBlending, overrideMaterial ) { var webglObject, object, buffer, material, start, end, delta; if ( reverse ) { start = renderList.length - 1; end = -1; delta = -1; } else { start = 0; end = renderList.length; delta = 1; } for ( var i = start; i !== end; i += delta ) { webglObject = renderList[ i ]; if ( webglObject.render ) { object = webglObject.object; buffer = webglObject.buffer; if ( overrideMaterial ) { material = overrideMaterial; } else { material = webglObject[ materialType ]; if ( ! material ) continue; if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst ); _this.setDepthTest( material.depthTest ); _this.setDepthWrite( material.depthWrite ); setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits ); } _this.setMaterialFaces( material ); if ( buffer instanceof THREE.BufferGeometry ) { _this.renderBufferDirect( camera, lights, fog, material, buffer, object ); } else { _this.renderBuffer( camera, lights, fog, material, buffer, object ); } } } }; function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, useBlending, overrideMaterial ) { var webglObject, object, material, program; for ( var i = 0, il = renderList.length; i < il; i ++ ) { webglObject = renderList[ i ]; object = webglObject.object; if ( object.visible ) { if ( overrideMaterial ) { material = overrideMaterial; } else { material = webglObject[ materialType ]; if ( ! material ) continue; if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst ); _this.setDepthTest( material.depthTest ); _this.setDepthWrite( material.depthWrite ); setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits ); } _this.renderImmediateObject( camera, lights, fog, material, object ); } } }; this.renderImmediateObject = function ( camera, lights, fog, material, object ) { var program = setProgram( camera, lights, fog, material, object ); _currentGeometryGroupHash = -1; _this.setMaterialFaces( material ); if ( object.immediateRenderCallback ) { object.immediateRenderCallback( program, _gl, _frustum ); } else { object.render( function( object ) { _this.renderBufferImmediate( object, program, material ); } ); } }; function unrollImmediateBufferMaterial ( globject ) { var object = globject.object, material = object.material; if ( material.transparent ) { globject.transparent = material; globject.opaque = null; } else { globject.opaque = material; globject.transparent = null; } }; function unrollBufferMaterial ( globject ) { var object = globject.object; var buffer = globject.buffer; var geometry = object.geometry; var material = object.material; if ( material instanceof THREE.MeshFaceMaterial ) { var materialIndex = geometry instanceof THREE.BufferGeometry ? 0 : buffer.materialIndex; material = material.materials[ materialIndex ]; if ( material.transparent ) { globject.transparent = material; globject.opaque = null; } else { globject.opaque = material; globject.transparent = null; } } else { if ( material ) { if ( material.transparent ) { globject.transparent = material; globject.opaque = null; } else { globject.opaque = material; globject.transparent = null; } } } }; // Geometry splitting function sortFacesByMaterial ( geometry, material ) { var f, fl, face, materialIndex, vertices, groupHash, hash_map = {}; var numMorphTargets = geometry.morphTargets.length; var numMorphNormals = geometry.morphNormals.length; var usesFaceMaterial = material instanceof THREE.MeshFaceMaterial; geometry.geometryGroups = {}; for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) { face = geometry.faces[ f ]; materialIndex = usesFaceMaterial ? face.materialIndex : 0; if ( hash_map[ materialIndex ] === undefined ) { hash_map[ materialIndex ] = { 'hash': materialIndex, 'counter': 0 }; } groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter; if ( geometry.geometryGroups[ groupHash ] === undefined ) { geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals }; } vertices = 3; if ( geometry.geometryGroups[ groupHash ].vertices + vertices > 65535 ) { hash_map[ materialIndex ].counter += 1; groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter; if ( geometry.geometryGroups[ groupHash ] === undefined ) { geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals }; } } geometry.geometryGroups[ groupHash ].faces3.push( f ); geometry.geometryGroups[ groupHash ].vertices += vertices; } geometry.geometryGroupsList = []; for ( var g in geometry.geometryGroups ) { geometry.geometryGroups[ g ].id = _geometryGroupCounter ++; geometry.geometryGroupsList.push( geometry.geometryGroups[ g ] ); } }; // Objects refresh this.initWebGLObjects = function ( scene ) { if ( !scene.__webglObjects ) { scene.__webglObjects = []; scene.__webglObjectsImmediate = []; scene.__webglSprites = []; scene.__webglFlares = []; } while ( scene.__objectsAdded.length ) { addObject( scene.__objectsAdded[ 0 ], scene ); scene.__objectsAdded.splice( 0, 1 ); } while ( scene.__objectsRemoved.length ) { removeObject( scene.__objectsRemoved[ 0 ], scene ); scene.__objectsRemoved.splice( 0, 1 ); } // update must be called after objects adding / removal for ( var o = 0, ol = scene.__webglObjects.length; o < ol; o ++ ) { var object = scene.__webglObjects[ o ].object; // TODO: Remove this hack (WebGLRenderer refactoring) if ( object.__webglInit === undefined ) { if ( object.__webglActive !== undefined ) { removeObject( object, scene ); } addObject( object, scene ); } updateObject( object ); } }; // Objects adding function addObject( object, scene ) { var g, geometry, material, geometryGroup; if ( object.__webglInit === undefined ) { object.__webglInit = true; object._modelViewMatrix = new THREE.Matrix4(); object._normalMatrix = new THREE.Matrix3(); if ( object.geometry !== undefined && object.geometry.__webglInit === undefined ) { object.geometry.__webglInit = true; object.geometry.addEventListener( 'dispose', onGeometryDispose ); } geometry = object.geometry; if ( geometry === undefined ) { // fail silently for now } else if ( geometry instanceof THREE.BufferGeometry ) { initDirectBuffers( geometry ); } else if ( object instanceof THREE.Mesh ) { material = object.material; if ( geometry.geometryGroups === undefined ) { sortFacesByMaterial( geometry, material ); } // create separate VBOs per geometry chunk for ( g in geometry.geometryGroups ) { geometryGroup = geometry.geometryGroups[ g ]; // initialise VBO on the first access if ( ! geometryGroup.__webglVertexBuffer ) { createMeshBuffers( geometryGroup ); initMeshBuffers( geometryGroup, object ); geometry.verticesNeedUpdate = true; geometry.morphTargetsNeedUpdate = true; geometry.elementsNeedUpdate = true; geometry.uvsNeedUpdate = true; geometry.normalsNeedUpdate = true; geometry.tangentsNeedUpdate = true; geometry.colorsNeedUpdate = true; } } } else if ( object instanceof THREE.Line ) { if ( ! geometry.__webglVertexBuffer ) { createLineBuffers( geometry ); initLineBuffers( geometry, object ); geometry.verticesNeedUpdate = true; geometry.colorsNeedUpdate = true; geometry.lineDistancesNeedUpdate = true; } } else if ( object instanceof THREE.ParticleSystem ) { if ( ! geometry.__webglVertexBuffer ) { createParticleBuffers( geometry ); initParticleBuffers( geometry, object ); geometry.verticesNeedUpdate = true; geometry.colorsNeedUpdate = true; } } } if ( object.__webglActive === undefined ) { if ( object instanceof THREE.Mesh ) { geometry = object.geometry; if ( geometry instanceof THREE.BufferGeometry ) { addBuffer( scene.__webglObjects, geometry, object ); } else if ( geometry instanceof THREE.Geometry ) { for ( g in geometry.geometryGroups ) { geometryGroup = geometry.geometryGroups[ g ]; addBuffer( scene.__webglObjects, geometryGroup, object ); } } } else if ( object instanceof THREE.Line || object instanceof THREE.ParticleSystem ) { geometry = object.geometry; addBuffer( scene.__webglObjects, geometry, object ); } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) { addBufferImmediate( scene.__webglObjectsImmediate, object ); } else if ( object instanceof THREE.Sprite ) { scene.__webglSprites.push( object ); } else if ( object instanceof THREE.LensFlare ) { scene.__webglFlares.push( object ); } object.__webglActive = true; } }; function addBuffer( objlist, buffer, object ) { objlist.push( { id: null, buffer: buffer, object: object, opaque: null, transparent: null, z: 0 } ); }; function addBufferImmediate( objlist, object ) { objlist.push( { id: null, object: object, opaque: null, transparent: null, z: 0 } ); }; // Objects updates function updateObject( object ) { var geometry = object.geometry, geometryGroup, customAttributesDirty, material; if ( geometry instanceof THREE.BufferGeometry ) { setDirectBuffers( geometry, _gl.DYNAMIC_DRAW, !geometry.dynamic ); } else if ( object instanceof THREE.Mesh ) { // check all geometry groups for( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) { geometryGroup = geometry.geometryGroupsList[ i ]; material = getBufferMaterial( object, geometryGroup ); if ( geometry.buffersNeedUpdate ) { initMeshBuffers( geometryGroup, object ); } customAttributesDirty = material.attributes && areCustomAttributesDirty( material ); if ( geometry.verticesNeedUpdate || geometry.morphTargetsNeedUpdate || geometry.elementsNeedUpdate || geometry.uvsNeedUpdate || geometry.normalsNeedUpdate || geometry.colorsNeedUpdate || geometry.tangentsNeedUpdate || customAttributesDirty ) { setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW, !geometry.dynamic, material ); } } geometry.verticesNeedUpdate = false; geometry.morphTargetsNeedUpdate = false; geometry.elementsNeedUpdate = false; geometry.uvsNeedUpdate = false; geometry.normalsNeedUpdate = false; geometry.colorsNeedUpdate = false; geometry.tangentsNeedUpdate = false; geometry.buffersNeedUpdate = false; material.attributes && clearCustomAttributes( material ); } else if ( object instanceof THREE.Line ) { material = getBufferMaterial( object, geometry ); customAttributesDirty = material.attributes && areCustomAttributesDirty( material ); if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || geometry.lineDistancesNeedUpdate || customAttributesDirty ) { setLineBuffers( geometry, _gl.DYNAMIC_DRAW ); } geometry.verticesNeedUpdate = false; geometry.colorsNeedUpdate = false; geometry.lineDistancesNeedUpdate = false; material.attributes && clearCustomAttributes( material ); } else if ( object instanceof THREE.ParticleSystem ) { material = getBufferMaterial( object, geometry ); customAttributesDirty = material.attributes && areCustomAttributesDirty( material ); if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || object.sortParticles || customAttributesDirty ) { setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object ); } geometry.verticesNeedUpdate = false; geometry.colorsNeedUpdate = false; material.attributes && clearCustomAttributes( material ); } }; // Objects updates - custom attributes check function areCustomAttributesDirty( material ) { for ( var a in material.attributes ) { if ( material.attributes[ a ].needsUpdate ) return true; } return false; }; function clearCustomAttributes( material ) { for ( var a in material.attributes ) { material.attributes[ a ].needsUpdate = false; } }; // Objects removal function removeObject( object, scene ) { if ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem || object instanceof THREE.Line ) { removeInstances( scene.__webglObjects, object ); } else if ( object instanceof THREE.Sprite ) { removeInstancesDirect( scene.__webglSprites, object ); } else if ( object instanceof THREE.LensFlare ) { removeInstancesDirect( scene.__webglFlares, object ); } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) { removeInstances( scene.__webglObjectsImmediate, object ); } delete object.__webglActive; }; function removeInstances( objlist, object ) { for ( var o = objlist.length - 1; o >= 0; o -- ) { if ( objlist[ o ].object === object ) { objlist.splice( o, 1 ); } } }; function removeInstancesDirect( objlist, object ) { for ( var o = objlist.length - 1; o >= 0; o -- ) { if ( objlist[ o ] === object ) { objlist.splice( o, 1 ); } } }; // Materials this.initMaterial = function ( material, lights, fog, object ) { material.addEventListener( 'dispose', onMaterialDispose ); var u, a, identifiers, i, parameters, maxLightCount, maxBones, maxShadows, shaderID; if ( material instanceof THREE.MeshDepthMaterial ) { shaderID = 'depth'; } else if ( material instanceof THREE.MeshNormalMaterial ) { shaderID = 'normal'; } else if ( material instanceof THREE.MeshBasicMaterial ) { shaderID = 'basic'; } else if ( material instanceof THREE.MeshLambertMaterial ) { shaderID = 'lambert'; } else if ( material instanceof THREE.MeshPhongMaterial ) { shaderID = 'phong'; } else if ( material instanceof THREE.LineBasicMaterial ) { shaderID = 'basic'; } else if ( material instanceof THREE.LineDashedMaterial ) { shaderID = 'dashed'; } else if ( material instanceof THREE.ParticleSystemMaterial ) { shaderID = 'particle_basic'; } if ( shaderID ) { setMaterialShaders( material, THREE.ShaderLib[ shaderID ] ); } // heuristics to create shader parameters according to lights in the scene // (not to blow over maxLights budget) maxLightCount = allocateLights( lights ); maxShadows = allocateShadows( lights ); maxBones = allocateBones( object ); parameters = { map: !!material.map, envMap: !!material.envMap, lightMap: !!material.lightMap, bumpMap: !!material.bumpMap, normalMap: !!material.normalMap, specularMap: !!material.specularMap, vertexColors: material.vertexColors, fog: fog, useFog: material.fog, fogExp: fog instanceof THREE.FogExp2, sizeAttenuation: material.sizeAttenuation, skinning: material.skinning, maxBones: maxBones, useVertexTexture: _supportsBoneTextures && object && object.useVertexTexture, morphTargets: material.morphTargets, morphNormals: material.morphNormals, maxMorphTargets: this.maxMorphTargets, maxMorphNormals: this.maxMorphNormals, maxDirLights: maxLightCount.directional, maxPointLights: maxLightCount.point, maxSpotLights: maxLightCount.spot, maxHemiLights: maxLightCount.hemi, maxShadows: maxShadows, shadowMapEnabled: this.shadowMapEnabled && object.receiveShadow, shadowMapType: this.shadowMapType, shadowMapDebug: this.shadowMapDebug, shadowMapCascade: this.shadowMapCascade, alphaTest: material.alphaTest, metal: material.metal, wrapAround: material.wrapAround, doubleSided: material.side === THREE.DoubleSide, flipSided: material.side === THREE.BackSide }; material.program = buildProgram( shaderID, material.fragmentShader, material.vertexShader, material.uniforms, material.attributes, material.defines, parameters, material.index0AttributeName ); var attributes = material.program.attributes; if ( material.morphTargets ) { material.numSupportedMorphTargets = 0; var id, base = "morphTarget"; for ( i = 0; i < this.maxMorphTargets; i ++ ) { id = base + i; if ( attributes[ id ] >= 0 ) { material.numSupportedMorphTargets ++; } } } if ( material.morphNormals ) { material.numSupportedMorphNormals = 0; var id, base = "morphNormal"; for ( i = 0; i < this.maxMorphNormals; i ++ ) { id = base + i; if ( attributes[ id ] >= 0 ) { material.numSupportedMorphNormals ++; } } } material.uniformsList = []; for ( u in material.uniforms ) { material.uniformsList.push( [ material.uniforms[ u ], u ] ); } }; function setMaterialShaders( material, shaders ) { material.uniforms = THREE.UniformsUtils.clone( shaders.uniforms ); material.vertexShader = shaders.vertexShader; material.fragmentShader = shaders.fragmentShader; }; function setProgram( camera, lights, fog, material, object ) { _usedTextureUnits = 0; if ( material.needsUpdate ) { if ( material.program ) deallocateMaterial( material ); _this.initMaterial( material, lights, fog, object ); material.needsUpdate = false; } if ( material.morphTargets ) { if ( ! object.__webglMorphTargetInfluences ) { object.__webglMorphTargetInfluences = new Float32Array( _this.maxMorphTargets ); } } var refreshMaterial = false; var program = material.program, p_uniforms = program.uniforms, m_uniforms = material.uniforms; if ( program !== _currentProgram ) { _gl.useProgram( program ); _currentProgram = program; refreshMaterial = true; } if ( material.id !== _currentMaterialId ) { _currentMaterialId = material.id; refreshMaterial = true; } if ( refreshMaterial || camera !== _currentCamera ) { _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera.projectionMatrix.elements ); if ( camera !== _currentCamera ) _currentCamera = camera; } // skinning uniforms must be set even if material didn't change // auto-setting of texture unit for bone texture must go before other textures // not sure why, but otherwise weird things happen if ( material.skinning ) { if ( _supportsBoneTextures && object.useVertexTexture ) { if ( p_uniforms.boneTexture !== null ) { var textureUnit = getTextureUnit(); _gl.uniform1i( p_uniforms.boneTexture, textureUnit ); _this.setTexture( object.boneTexture, textureUnit ); } if ( p_uniforms.boneTextureWidth !== null ) { _gl.uniform1i( p_uniforms.boneTextureWidth, object.boneTextureWidth ); } if ( p_uniforms.boneTextureHeight !== null ) { _gl.uniform1i( p_uniforms.boneTextureHeight, object.boneTextureHeight ); } } else { if ( p_uniforms.boneGlobalMatrices !== null ) { _gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.boneMatrices ); } } } if ( refreshMaterial ) { // refresh uniforms common to several materials if ( fog && material.fog ) { refreshUniformsFog( m_uniforms, fog ); } if ( material instanceof THREE.MeshPhongMaterial || material instanceof THREE.MeshLambertMaterial || material.lights ) { if ( _lightsNeedUpdate ) { setupLights( program, lights ); _lightsNeedUpdate = false; } refreshUniformsLights( m_uniforms, _lights ); } if ( material instanceof THREE.MeshBasicMaterial || material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) { refreshUniformsCommon( m_uniforms, material ); } // refresh single material specific uniforms if ( material instanceof THREE.LineBasicMaterial ) { refreshUniformsLine( m_uniforms, material ); } else if ( material instanceof THREE.LineDashedMaterial ) { refreshUniformsLine( m_uniforms, material ); refreshUniformsDash( m_uniforms, material ); } else if ( material instanceof THREE.ParticleSystemMaterial ) { refreshUniformsParticle( m_uniforms, material ); } else if ( material instanceof THREE.MeshPhongMaterial ) { refreshUniformsPhong( m_uniforms, material ); } else if ( material instanceof THREE.MeshLambertMaterial ) { refreshUniformsLambert( m_uniforms, material ); } else if ( material instanceof THREE.MeshDepthMaterial ) { m_uniforms.mNear.value = camera.near; m_uniforms.mFar.value = camera.far; m_uniforms.opacity.value = material.opacity; } else if ( material instanceof THREE.MeshNormalMaterial ) { m_uniforms.opacity.value = material.opacity; } if ( object.receiveShadow && ! material._shadowPass ) { refreshUniformsShadow( m_uniforms, lights ); } // load common uniforms loadUniformsGeneric( program, material.uniformsList ); // load material specific uniforms // (shader material also gets them for the sake of genericity) if ( material instanceof THREE.ShaderMaterial || material instanceof THREE.MeshPhongMaterial || material.envMap ) { if ( p_uniforms.cameraPosition !== null ) { _vector3.setFromMatrixPosition( camera.matrixWorld ); _gl.uniform3f( p_uniforms.cameraPosition, _vector3.x, _vector3.y, _vector3.z ); } } if ( material instanceof THREE.MeshPhongMaterial || material instanceof THREE.MeshLambertMaterial || material instanceof THREE.ShaderMaterial || material.skinning ) { if ( p_uniforms.viewMatrix !== null ) { _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera.matrixWorldInverse.elements ); } } } loadUniformsMatrices( p_uniforms, object ); if ( p_uniforms.modelMatrix !== null ) { _gl.uniformMatrix4fv( p_uniforms.modelMatrix, false, object.matrixWorld.elements ); } return program; }; // Uniforms (refresh uniforms objects) function refreshUniformsCommon ( uniforms, material ) { uniforms.opacity.value = material.opacity; if ( _this.gammaInput ) { uniforms.diffuse.value.copyGammaToLinear( material.color ); } else { uniforms.diffuse.value = material.color; } uniforms.map.value = material.map; uniforms.lightMap.value = material.lightMap; uniforms.specularMap.value = material.specularMap; if ( material.bumpMap ) { uniforms.bumpMap.value = material.bumpMap; uniforms.bumpScale.value = material.bumpScale; } if ( material.normalMap ) { uniforms.normalMap.value = material.normalMap; uniforms.normalScale.value.copy( material.normalScale ); } // uv repeat and offset setting priorities // 1. color map // 2. specular map // 3. normal map // 4. bump map var uvScaleMap; if ( material.map ) { uvScaleMap = material.map; } else if ( material.specularMap ) { uvScaleMap = material.specularMap; } else if ( material.normalMap ) { uvScaleMap = material.normalMap; } else if ( material.bumpMap ) { uvScaleMap = material.bumpMap; } if ( uvScaleMap !== undefined ) { var offset = uvScaleMap.offset; var repeat = uvScaleMap.repeat; uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y ); } uniforms.envMap.value = material.envMap; uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : -1; if ( _this.gammaInput ) { //uniforms.reflectivity.value = material.reflectivity * material.reflectivity; uniforms.reflectivity.value = material.reflectivity; } else { uniforms.reflectivity.value = material.reflectivity; } uniforms.refractionRatio.value = material.refractionRatio; uniforms.combine.value = material.combine; uniforms.useRefract.value = material.envMap && material.envMap.mapping instanceof THREE.CubeRefractionMapping; }; function refreshUniformsLine ( uniforms, material ) { uniforms.diffuse.value = material.color; uniforms.opacity.value = material.opacity; }; function refreshUniformsDash ( uniforms, material ) { uniforms.dashSize.value = material.dashSize; uniforms.totalSize.value = material.dashSize + material.gapSize; uniforms.scale.value = material.scale; }; function refreshUniformsParticle ( uniforms, material ) { uniforms.psColor.value = material.color; uniforms.opacity.value = material.opacity; uniforms.size.value = material.size; uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this. uniforms.map.value = material.map; }; function refreshUniformsFog ( uniforms, fog ) { uniforms.fogColor.value = fog.color; if ( fog instanceof THREE.Fog ) { uniforms.fogNear.value = fog.near; uniforms.fogFar.value = fog.far; } else if ( fog instanceof THREE.FogExp2 ) { uniforms.fogDensity.value = fog.density; } }; function refreshUniformsPhong ( uniforms, material ) { uniforms.shininess.value = material.shininess; if ( _this.gammaInput ) { uniforms.ambient.value.copyGammaToLinear( material.ambient ); uniforms.emissive.value.copyGammaToLinear( material.emissive ); uniforms.specular.value.copyGammaToLinear( material.specular ); } else { uniforms.ambient.value = material.ambient; uniforms.emissive.value = material.emissive; uniforms.specular.value = material.specular; } if ( material.wrapAround ) { uniforms.wrapRGB.value.copy( material.wrapRGB ); } }; function refreshUniformsLambert ( uniforms, material ) { if ( _this.gammaInput ) { uniforms.ambient.value.copyGammaToLinear( material.ambient ); uniforms.emissive.value.copyGammaToLinear( material.emissive ); } else { uniforms.ambient.value = material.ambient; uniforms.emissive.value = material.emissive; } if ( material.wrapAround ) { uniforms.wrapRGB.value.copy( material.wrapRGB ); } }; function refreshUniformsLights ( uniforms, lights ) { uniforms.ambientLightColor.value = lights.ambient; uniforms.directionalLightColor.value = lights.directional.colors; uniforms.directionalLightDirection.value = lights.directional.positions; uniforms.pointLightColor.value = lights.point.colors; uniforms.pointLightPosition.value = lights.point.positions; uniforms.pointLightDistance.value = lights.point.distances; uniforms.spotLightColor.value = lights.spot.colors; uniforms.spotLightPosition.value = lights.spot.positions; uniforms.spotLightDistance.value = lights.spot.distances; uniforms.spotLightDirection.value = lights.spot.directions; uniforms.spotLightAngleCos.value = lights.spot.anglesCos; uniforms.spotLightExponent.value = lights.spot.exponents; uniforms.hemisphereLightSkyColor.value = lights.hemi.skyColors; uniforms.hemisphereLightGroundColor.value = lights.hemi.groundColors; uniforms.hemisphereLightDirection.value = lights.hemi.positions; }; function refreshUniformsShadow ( uniforms, lights ) { if ( uniforms.shadowMatrix ) { var j = 0; for ( var i = 0, il = lights.length; i < il; i ++ ) { var light = lights[ i ]; if ( ! light.castShadow ) continue; if ( light instanceof THREE.SpotLight || ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) ) { uniforms.shadowMap.value[ j ] = light.shadowMap; uniforms.shadowMapSize.value[ j ] = light.shadowMapSize; uniforms.shadowMatrix.value[ j ] = light.shadowMatrix; uniforms.shadowDarkness.value[ j ] = light.shadowDarkness; uniforms.shadowBias.value[ j ] = light.shadowBias; j ++; } } } }; // Uniforms (load to GPU) function loadUniformsMatrices ( uniforms, object ) { _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrix.elements ); if ( uniforms.normalMatrix ) { _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrix.elements ); } }; function getTextureUnit() { var textureUnit = _usedTextureUnits; if ( textureUnit >= _maxTextures ) { console.warn( "WebGLRenderer: trying to use " + textureUnit + " texture units while this GPU supports only " + _maxTextures ); } _usedTextureUnits += 1; return textureUnit; }; function loadUniformsGeneric ( program, uniforms ) { var uniform, value, type, location, texture, textureUnit, i, il, j, jl, offset; for ( j = 0, jl = uniforms.length; j < jl; j ++ ) { location = program.uniforms[ uniforms[ j ][ 1 ] ]; if ( !location ) continue; uniform = uniforms[ j ][ 0 ]; type = uniform.type; value = uniform.value; if ( type === "i" ) { // single integer _gl.uniform1i( location, value ); } else if ( type === "f" ) { // single float _gl.uniform1f( location, value ); } else if ( type === "v2" ) { // single THREE.Vector2 _gl.uniform2f( location, value.x, value.y ); } else if ( type === "v3" ) { // single THREE.Vector3 _gl.uniform3f( location, value.x, value.y, value.z ); } else if ( type === "v4" ) { // single THREE.Vector4 _gl.uniform4f( location, value.x, value.y, value.z, value.w ); } else if ( type === "c" ) { // single THREE.Color _gl.uniform3f( location, value.r, value.g, value.b ); } else if ( type === "iv1" ) { // flat array of integers (JS or typed array) _gl.uniform1iv( location, value ); } else if ( type === "iv" ) { // flat array of integers with 3 x N size (JS or typed array) _gl.uniform3iv( location, value ); } else if ( type === "fv1" ) { // flat array of floats (JS or typed array) _gl.uniform1fv( location, value ); } else if ( type === "fv" ) { // flat array of floats with 3 x N size (JS or typed array) _gl.uniform3fv( location, value ); } else if ( type === "v2v" ) { // array of THREE.Vector2 if ( uniform._array === undefined ) { uniform._array = new Float32Array( 2 * value.length ); } for ( i = 0, il = value.length; i < il; i ++ ) { offset = i * 2; uniform._array[ offset ] = value[ i ].x; uniform._array[ offset + 1 ] = value[ i ].y; } _gl.uniform2fv( location, uniform._array ); } else if ( type === "v3v" ) { // array of THREE.Vector3 if ( uniform._array === undefined ) { uniform._array = new Float32Array( 3 * value.length ); } for ( i = 0, il = value.length; i < il; i ++ ) { offset = i * 3; uniform._array[ offset ] = value[ i ].x; uniform._array[ offset + 1 ] = value[ i ].y; uniform._array[ offset + 2 ] = value[ i ].z; } _gl.uniform3fv( location, uniform._array ); } else if ( type === "v4v" ) { // array of THREE.Vector4 if ( uniform._array === undefined ) { uniform._array = new Float32Array( 4 * value.length ); } for ( i = 0, il = value.length; i < il; i ++ ) { offset = i * 4; uniform._array[ offset ] = value[ i ].x; uniform._array[ offset + 1 ] = value[ i ].y; uniform._array[ offset + 2 ] = value[ i ].z; uniform._array[ offset + 3 ] = value[ i ].w; } _gl.uniform4fv( location, uniform._array ); } else if ( type === "m4") { // single THREE.Matrix4 if ( uniform._array === undefined ) { uniform._array = new Float32Array( 16 ); } value.flattenToArray( uniform._array ); _gl.uniformMatrix4fv( location, false, uniform._array ); } else if ( type === "m4v" ) { // array of THREE.Matrix4 if ( uniform._array === undefined ) { uniform._array = new Float32Array( 16 * value.length ); } for ( i = 0, il = value.length; i < il; i ++ ) { value[ i ].flattenToArrayOffset( uniform._array, i * 16 ); } _gl.uniformMatrix4fv( location, false, uniform._array ); } else if ( type === "t" ) { // single THREE.Texture (2d or cube) texture = value; textureUnit = getTextureUnit(); _gl.uniform1i( location, textureUnit ); if ( !texture ) continue; if ( texture.image instanceof Array && texture.image.length === 6 ) { setCubeTexture( texture, textureUnit ); } else if ( texture instanceof THREE.WebGLRenderTargetCube ) { setCubeTextureDynamic( texture, textureUnit ); } else { _this.setTexture( texture, textureUnit ); } } else if ( type === "tv" ) { // array of THREE.Texture (2d) if ( uniform._array === undefined ) { uniform._array = []; } for( i = 0, il = uniform.value.length; i < il; i ++ ) { uniform._array[ i ] = getTextureUnit(); } _gl.uniform1iv( location, uniform._array ); for( i = 0, il = uniform.value.length; i < il; i ++ ) { texture = uniform.value[ i ]; textureUnit = uniform._array[ i ]; if ( !texture ) continue; _this.setTexture( texture, textureUnit ); } } else { console.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type ); } } }; function setupMatrices ( object, camera ) { object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld ); object._normalMatrix.getNormalMatrix( object._modelViewMatrix ); }; // function setColorGamma( array, offset, color, intensitySq ) { array[ offset ] = color.r * color.r * intensitySq; array[ offset + 1 ] = color.g * color.g * intensitySq; array[ offset + 2 ] = color.b * color.b * intensitySq; }; function setColorLinear( array, offset, color, intensity ) { array[ offset ] = color.r * intensity; array[ offset + 1 ] = color.g * intensity; array[ offset + 2 ] = color.b * intensity; }; function setupLights ( program, lights ) { var l, ll, light, n, r = 0, g = 0, b = 0, color, skyColor, groundColor, intensity, intensitySq, position, distance, zlights = _lights, dirColors = zlights.directional.colors, dirPositions = zlights.directional.positions, pointColors = zlights.point.colors, pointPositions = zlights.point.positions, pointDistances = zlights.point.distances, spotColors = zlights.spot.colors, spotPositions = zlights.spot.positions, spotDistances = zlights.spot.distances, spotDirections = zlights.spot.directions, spotAnglesCos = zlights.spot.anglesCos, spotExponents = zlights.spot.exponents, hemiSkyColors = zlights.hemi.skyColors, hemiGroundColors = zlights.hemi.groundColors, hemiPositions = zlights.hemi.positions, dirLength = 0, pointLength = 0, spotLength = 0, hemiLength = 0, dirCount = 0, pointCount = 0, spotCount = 0, hemiCount = 0, dirOffset = 0, pointOffset = 0, spotOffset = 0, hemiOffset = 0; for ( l = 0, ll = lights.length; l < ll; l ++ ) { light = lights[ l ]; if ( light.onlyShadow ) continue; color = light.color; intensity = light.intensity; distance = light.distance; if ( light instanceof THREE.AmbientLight ) { if ( ! light.visible ) continue; if ( _this.gammaInput ) { r += color.r * color.r; g += color.g * color.g; b += color.b * color.b; } else { r += color.r; g += color.g; b += color.b; } } else if ( light instanceof THREE.DirectionalLight ) { dirCount += 1; if ( ! light.visible ) continue; _direction.setFromMatrixPosition( light.matrixWorld ); _vector3.setFromMatrixPosition( light.target.matrixWorld ); _direction.sub( _vector3 ); _direction.normalize(); // skip lights with undefined direction // these create troubles in OpenGL (making pixel black) if ( _direction.x === 0 && _direction.y === 0 && _direction.z === 0 ) continue; dirOffset = dirLength * 3; dirPositions[ dirOffset ] = _direction.x; dirPositions[ dirOffset + 1 ] = _direction.y; dirPositions[ dirOffset + 2 ] = _direction.z; if ( _this.gammaInput ) { setColorGamma( dirColors, dirOffset, color, intensity * intensity ); } else { setColorLinear( dirColors, dirOffset, color, intensity ); } dirLength += 1; } else if ( light instanceof THREE.PointLight ) { pointCount += 1; if ( ! light.visible ) continue; pointOffset = pointLength * 3; if ( _this.gammaInput ) { setColorGamma( pointColors, pointOffset, color, intensity * intensity ); } else { setColorLinear( pointColors, pointOffset, color, intensity ); } _vector3.setFromMatrixPosition( light.matrixWorld ); pointPositions[ pointOffset ] = _vector3.x; pointPositions[ pointOffset + 1 ] = _vector3.y; pointPositions[ pointOffset + 2 ] = _vector3.z; pointDistances[ pointLength ] = distance; pointLength += 1; } else if ( light instanceof THREE.SpotLight ) { spotCount += 1; if ( ! light.visible ) continue; spotOffset = spotLength * 3; if ( _this.gammaInput ) { setColorGamma( spotColors, spotOffset, color, intensity * intensity ); } else { setColorLinear( spotColors, spotOffset, color, intensity ); } _vector3.setFromMatrixPosition( light.matrixWorld ); spotPositions[ spotOffset ] = _vector3.x; spotPositions[ spotOffset + 1 ] = _vector3.y; spotPositions[ spotOffset + 2 ] = _vector3.z; spotDistances[ spotLength ] = distance; _direction.copy( _vector3 ); _vector3.setFromMatrixPosition( light.target.matrixWorld ); _direction.sub( _vector3 ); _direction.normalize(); spotDirections[ spotOffset ] = _direction.x; spotDirections[ spotOffset + 1 ] = _direction.y; spotDirections[ spotOffset + 2 ] = _direction.z; spotAnglesCos[ spotLength ] = Math.cos( light.angle ); spotExponents[ spotLength ] = light.exponent; spotLength += 1; } else if ( light instanceof THREE.HemisphereLight ) { hemiCount += 1; if ( ! light.visible ) continue; _direction.setFromMatrixPosition( light.matrixWorld ); _direction.normalize(); // skip lights with undefined direction // these create troubles in OpenGL (making pixel black) if ( _direction.x === 0 && _direction.y === 0 && _direction.z === 0 ) continue; hemiOffset = hemiLength * 3; hemiPositions[ hemiOffset ] = _direction.x; hemiPositions[ hemiOffset + 1 ] = _direction.y; hemiPositions[ hemiOffset + 2 ] = _direction.z; skyColor = light.color; groundColor = light.groundColor; if ( _this.gammaInput ) { intensitySq = intensity * intensity; setColorGamma( hemiSkyColors, hemiOffset, skyColor, intensitySq ); setColorGamma( hemiGroundColors, hemiOffset, groundColor, intensitySq ); } else { setColorLinear( hemiSkyColors, hemiOffset, skyColor, intensity ); setColorLinear( hemiGroundColors, hemiOffset, groundColor, intensity ); } hemiLength += 1; } } // null eventual remains from removed lights // (this is to avoid if in shader) for ( l = dirLength * 3, ll = Math.max( dirColors.length, dirCount * 3 ); l < ll; l ++ ) dirColors[ l ] = 0.0; for ( l = pointLength * 3, ll = Math.max( pointColors.length, pointCount * 3 ); l < ll; l ++ ) pointColors[ l ] = 0.0; for ( l = spotLength * 3, ll = Math.max( spotColors.length, spotCount * 3 ); l < ll; l ++ ) spotColors[ l ] = 0.0; for ( l = hemiLength * 3, ll = Math.max( hemiSkyColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiSkyColors[ l ] = 0.0; for ( l = hemiLength * 3, ll = Math.max( hemiGroundColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiGroundColors[ l ] = 0.0; zlights.directional.length = dirLength; zlights.point.length = pointLength; zlights.spot.length = spotLength; zlights.hemi.length = hemiLength; zlights.ambient[ 0 ] = r; zlights.ambient[ 1 ] = g; zlights.ambient[ 2 ] = b; }; // GL state setting this.setFaceCulling = function ( cullFace, frontFaceDirection ) { if ( cullFace === THREE.CullFaceNone ) { _gl.disable( _gl.CULL_FACE ); } else { if ( frontFaceDirection === THREE.FrontFaceDirectionCW ) { _gl.frontFace( _gl.CW ); } else { _gl.frontFace( _gl.CCW ); } if ( cullFace === THREE.CullFaceBack ) { _gl.cullFace( _gl.BACK ); } else if ( cullFace === THREE.CullFaceFront ) { _gl.cullFace( _gl.FRONT ); } else { _gl.cullFace( _gl.FRONT_AND_BACK ); } _gl.enable( _gl.CULL_FACE ); } }; this.setMaterialFaces = function ( material ) { var doubleSided = material.side === THREE.DoubleSide; var flipSided = material.side === THREE.BackSide; if ( _oldDoubleSided !== doubleSided ) { if ( doubleSided ) { _gl.disable( _gl.CULL_FACE ); } else { _gl.enable( _gl.CULL_FACE ); } _oldDoubleSided = doubleSided; } if ( _oldFlipSided !== flipSided ) { if ( flipSided ) { _gl.frontFace( _gl.CW ); } else { _gl.frontFace( _gl.CCW ); } _oldFlipSided = flipSided; } }; this.setDepthTest = function ( depthTest ) { if ( _oldDepthTest !== depthTest ) { if ( depthTest ) { _gl.enable( _gl.DEPTH_TEST ); } else { _gl.disable( _gl.DEPTH_TEST ); } _oldDepthTest = depthTest; } }; this.setDepthWrite = function ( depthWrite ) { if ( _oldDepthWrite !== depthWrite ) { _gl.depthMask( depthWrite ); _oldDepthWrite = depthWrite; } }; function setLineWidth ( width ) { if ( width !== _oldLineWidth ) { _gl.lineWidth( width ); _oldLineWidth = width; } }; function setPolygonOffset ( polygonoffset, factor, units ) { if ( _oldPolygonOffset !== polygonoffset ) { if ( polygonoffset ) { _gl.enable( _gl.POLYGON_OFFSET_FILL ); } else { _gl.disable( _gl.POLYGON_OFFSET_FILL ); } _oldPolygonOffset = polygonoffset; } if ( polygonoffset && ( _oldPolygonOffsetFactor !== factor || _oldPolygonOffsetUnits !== units ) ) { _gl.polygonOffset( factor, units ); _oldPolygonOffsetFactor = factor; _oldPolygonOffsetUnits = units; } }; this.setBlending = function ( blending, blendEquation, blendSrc, blendDst ) { if ( blending !== _oldBlending ) { if ( blending === THREE.NoBlending ) { _gl.disable( _gl.BLEND ); } else if ( blending === THREE.AdditiveBlending ) { _gl.enable( _gl.BLEND ); _gl.blendEquation( _gl.FUNC_ADD ); _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE ); } else if ( blending === THREE.SubtractiveBlending ) { // TODO: Find blendFuncSeparate() combination _gl.enable( _gl.BLEND ); _gl.blendEquation( _gl.FUNC_ADD ); _gl.blendFunc( _gl.ZERO, _gl.ONE_MINUS_SRC_COLOR ); } else if ( blending === THREE.MultiplyBlending ) { // TODO: Find blendFuncSeparate() combination _gl.enable( _gl.BLEND ); _gl.blendEquation( _gl.FUNC_ADD ); _gl.blendFunc( _gl.ZERO, _gl.SRC_COLOR ); } else if ( blending === THREE.CustomBlending ) { _gl.enable( _gl.BLEND ); } else { _gl.enable( _gl.BLEND ); _gl.blendEquationSeparate( _gl.FUNC_ADD, _gl.FUNC_ADD ); _gl.blendFuncSeparate( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA, _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA ); } _oldBlending = blending; } if ( blending === THREE.CustomBlending ) { if ( blendEquation !== _oldBlendEquation ) { _gl.blendEquation( paramThreeToGL( blendEquation ) ); _oldBlendEquation = blendEquation; } if ( blendSrc !== _oldBlendSrc || blendDst !== _oldBlendDst ) { _gl.blendFunc( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ) ); _oldBlendSrc = blendSrc; _oldBlendDst = blendDst; } } else { _oldBlendEquation = null; _oldBlendSrc = null; _oldBlendDst = null; } }; // Defines function generateDefines ( defines ) { var value, chunk, chunks = []; for ( var d in defines ) { value = defines[ d ]; if ( value === false ) continue; chunk = "#define " + d + " " + value; chunks.push( chunk ); } return chunks.join( "\n" ); }; // Shaders function buildProgram ( shaderID, fragmentShader, vertexShader, uniforms, attributes, defines, parameters, index0AttributeName ) { var p, pl, d, program, code; var chunks = []; // Generate code if ( shaderID ) { chunks.push( shaderID ); } else { chunks.push( fragmentShader ); chunks.push( vertexShader ); } for ( d in defines ) { chunks.push( d ); chunks.push( defines[ d ] ); } for ( p in parameters ) { chunks.push( p ); chunks.push( parameters[ p ] ); } code = chunks.join(); // Check if code has been already compiled for ( p = 0, pl = _programs.length; p < pl; p ++ ) { var programInfo = _programs[ p ]; if ( programInfo.code === code ) { // console.log( "Code already compiled." /*: \n\n" + code*/ ); programInfo.usedTimes ++; return programInfo.program; } } var shadowMapTypeDefine = "SHADOWMAP_TYPE_BASIC"; if ( parameters.shadowMapType === THREE.PCFShadowMap ) { shadowMapTypeDefine = "SHADOWMAP_TYPE_PCF"; } else if ( parameters.shadowMapType === THREE.PCFSoftShadowMap ) { shadowMapTypeDefine = "SHADOWMAP_TYPE_PCF_SOFT"; } // console.log( "building new program " ); // var customDefines = generateDefines( defines ); // program = _gl.createProgram(); var prefix_vertex = [ "precision " + _precision + " float;", "precision " + _precision + " int;", customDefines, _supportsVertexTextures ? "#define VERTEX_TEXTURES" : "", _this.gammaInput ? "#define GAMMA_INPUT" : "", _this.gammaOutput ? "#define GAMMA_OUTPUT" : "", "#define MAX_DIR_LIGHTS " + parameters.maxDirLights, "#define MAX_POINT_LIGHTS " + parameters.maxPointLights, "#define MAX_SPOT_LIGHTS " + parameters.maxSpotLights, "#define MAX_HEMI_LIGHTS " + parameters.maxHemiLights, "#define MAX_SHADOWS " + parameters.maxShadows, "#define MAX_BONES " + parameters.maxBones, parameters.map ? "#define USE_MAP" : "", parameters.envMap ? "#define USE_ENVMAP" : "", parameters.lightMap ? "#define USE_LIGHTMAP" : "", parameters.bumpMap ? "#define USE_BUMPMAP" : "", parameters.normalMap ? "#define USE_NORMALMAP" : "", parameters.specularMap ? "#define USE_SPECULARMAP" : "", parameters.vertexColors ? "#define USE_COLOR" : "", parameters.skinning ? "#define USE_SKINNING" : "", parameters.useVertexTexture ? "#define BONE_TEXTURE" : "", parameters.morphTargets ? "#define USE_MORPHTARGETS" : "", parameters.morphNormals ? "#define USE_MORPHNORMALS" : "", parameters.wrapAround ? "#define WRAP_AROUND" : "", parameters.doubleSided ? "#define DOUBLE_SIDED" : "", parameters.flipSided ? "#define FLIP_SIDED" : "", parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "", parameters.shadowMapEnabled ? "#define " + shadowMapTypeDefine : "", parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "", parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "", parameters.sizeAttenuation ? "#define USE_SIZEATTENUATION" : "", "uniform mat4 modelMatrix;", "uniform mat4 modelViewMatrix;", "uniform mat4 projectionMatrix;", "uniform mat4 viewMatrix;", "uniform mat3 normalMatrix;", "uniform vec3 cameraPosition;", "attribute vec3 position;", "attribute vec3 normal;", "attribute vec2 uv;", "attribute vec2 uv2;", "#ifdef USE_COLOR", "attribute vec3 color;", "#endif", "#ifdef USE_MORPHTARGETS", "attribute vec3 morphTarget0;", "attribute vec3 morphTarget1;", "attribute vec3 morphTarget2;", "attribute vec3 morphTarget3;", "#ifdef USE_MORPHNORMALS", "attribute vec3 morphNormal0;", "attribute vec3 morphNormal1;", "attribute vec3 morphNormal2;", "attribute vec3 morphNormal3;", "#else", "attribute vec3 morphTarget4;", "attribute vec3 morphTarget5;", "attribute vec3 morphTarget6;", "attribute vec3 morphTarget7;", "#endif", "#endif", "#ifdef USE_SKINNING", "attribute vec4 skinIndex;", "attribute vec4 skinWeight;", "#endif", "" ].join("\n"); var prefix_fragment = [ "precision " + _precision + " float;", "precision " + _precision + " int;", ( parameters.bumpMap || parameters.normalMap ) ? "#extension GL_OES_standard_derivatives : enable" : "", customDefines, "#define MAX_DIR_LIGHTS " + parameters.maxDirLights, "#define MAX_POINT_LIGHTS " + parameters.maxPointLights, "#define MAX_SPOT_LIGHTS " + parameters.maxSpotLights, "#define MAX_HEMI_LIGHTS " + parameters.maxHemiLights, "#define MAX_SHADOWS " + parameters.maxShadows, parameters.alphaTest ? "#define ALPHATEST " + parameters.alphaTest: "", _this.gammaInput ? "#define GAMMA_INPUT" : "", _this.gammaOutput ? "#define GAMMA_OUTPUT" : "", ( parameters.useFog && parameters.fog ) ? "#define USE_FOG" : "", ( parameters.useFog && parameters.fogExp ) ? "#define FOG_EXP2" : "", parameters.map ? "#define USE_MAP" : "", parameters.envMap ? "#define USE_ENVMAP" : "", parameters.lightMap ? "#define USE_LIGHTMAP" : "", parameters.bumpMap ? "#define USE_BUMPMAP" : "", parameters.normalMap ? "#define USE_NORMALMAP" : "", parameters.specularMap ? "#define USE_SPECULARMAP" : "", parameters.vertexColors ? "#define USE_COLOR" : "", parameters.metal ? "#define METAL" : "", parameters.wrapAround ? "#define WRAP_AROUND" : "", parameters.doubleSided ? "#define DOUBLE_SIDED" : "", parameters.flipSided ? "#define FLIP_SIDED" : "", parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "", parameters.shadowMapEnabled ? "#define " + shadowMapTypeDefine : "", parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "", parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "", "uniform mat4 viewMatrix;", "uniform vec3 cameraPosition;", "" ].join("\n"); var glVertexShader = getShader( "vertex", prefix_vertex + vertexShader ); var glFragmentShader = getShader( "fragment", prefix_fragment + fragmentShader ); _gl.attachShader( program, glVertexShader ); _gl.attachShader( program, glFragmentShader ); // Force a particular attribute to index 0. // because potentially expensive emulation is done by browser if attribute 0 is disabled. // And, color, for example is often automatically bound to index 0 so disabling it if ( index0AttributeName !== undefined ) { _gl.bindAttribLocation( program, 0, index0AttributeName ); } else { _gl.bindAttribLocation( program, 0, 'position' ); } _gl.linkProgram( program ); if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) { console.error( "Could not initialise shader\n" + "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + ", gl error [" + _gl.getError() + "]" ); console.error( "Program Info Log: " + _gl.getProgramInfoLog( program ) ); } // clean up _gl.deleteShader( glFragmentShader ); _gl.deleteShader( glVertexShader ); // console.log( prefix_fragment + fragmentShader ); // console.log( prefix_vertex + vertexShader ); program.uniforms = {}; program.attributes = {}; var identifiers, u, a, i; // cache uniform locations identifiers = [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'modelMatrix', 'cameraPosition', 'morphTargetInfluences' ]; if ( parameters.useVertexTexture ) { identifiers.push( 'boneTexture' ); identifiers.push( 'boneTextureWidth' ); identifiers.push( 'boneTextureHeight' ); } else { identifiers.push( 'boneGlobalMatrices' ); } for ( u in uniforms ) { identifiers.push( u ); } cacheUniformLocations( program, identifiers ); // cache attributes locations identifiers = [ "position", "normal", "uv", "uv2", "tangent", "color", "skinIndex", "skinWeight", "lineDistance" ]; for ( i = 0; i < parameters.maxMorphTargets; i ++ ) { identifiers.push( "morphTarget" + i ); } for ( i = 0; i < parameters.maxMorphNormals; i ++ ) { identifiers.push( "morphNormal" + i ); } for ( a in attributes ) { identifiers.push( a ); } cacheAttributeLocations( program, identifiers ); program.id = _programs_counter ++; _programs.push( { program: program, code: code, usedTimes: 1 } ); _this.info.memory.programs = _programs.length; return program; }; // Shader parameters cache function cacheUniformLocations ( program, identifiers ) { var i, l, id; for( i = 0, l = identifiers.length; i < l; i ++ ) { id = identifiers[ i ]; program.uniforms[ id ] = _gl.getUniformLocation( program, id ); } }; function cacheAttributeLocations ( program, identifiers ) { var i, l, id; for( i = 0, l = identifiers.length; i < l; i ++ ) { id = identifiers[ i ]; program.attributes[ id ] = _gl.getAttribLocation( program, id ); } }; function addLineNumbers ( string ) { var chunks = string.split( "\n" ); for ( var i = 0, il = chunks.length; i < il; i ++ ) { // Chrome reports shader errors on lines // starting counting from 1 chunks[ i ] = ( i + 1 ) + ": " + chunks[ i ]; } return chunks.join( "\n" ); }; function getShader ( type, string ) { var shader; if ( type === "fragment" ) { shader = _gl.createShader( _gl.FRAGMENT_SHADER ); } else if ( type === "vertex" ) { shader = _gl.createShader( _gl.VERTEX_SHADER ); } _gl.shaderSource( shader, string ); _gl.compileShader( shader ); if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) { console.error( _gl.getShaderInfoLog( shader ) ); console.error( addLineNumbers( string ) ); return null; } return shader; }; // Textures function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) { if ( isImagePowerOfTwo ) { _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) ); _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) ); _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) ); _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) ); } else { _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE ); _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE ); _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) ); _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) ); } if ( _glExtensionTextureFilterAnisotropic && texture.type !== THREE.FloatType ) { if ( texture.anisotropy > 1 || texture.__oldAnisotropy ) { _gl.texParameterf( textureType, _glExtensionTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _maxAnisotropy ) ); texture.__oldAnisotropy = texture.anisotropy; } } }; this.setTexture = function ( texture, slot ) { if ( texture.needsUpdate ) { if ( ! texture.__webglInit ) { texture.__webglInit = true; texture.addEventListener( 'dispose', onTextureDispose ); texture.__webglTexture = _gl.createTexture(); _this.info.memory.textures ++; } _gl.activeTexture( _gl.TEXTURE0 + slot ); _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture ); _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY ); _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha ); _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment ); var image = texture.image, isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ), glFormat = paramThreeToGL( texture.format ), glType = paramThreeToGL( texture.type ); setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo ); var mipmap, mipmaps = texture.mipmaps; if ( texture instanceof THREE.DataTexture ) { // use manually created mipmaps if available // if there are no manual mipmaps // set 0 level mipmap and then use GL to generate other mipmap levels if ( mipmaps.length > 0 && isImagePowerOfTwo ) { for ( var i = 0, il = mipmaps.length; i < il; i ++ ) { mipmap = mipmaps[ i ]; _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data ); } texture.generateMipmaps = false; } else { _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data ); } } else if ( texture instanceof THREE.CompressedTexture ) { for( var i = 0, il = mipmaps.length; i < il; i ++ ) { mipmap = mipmaps[ i ]; if ( texture.format!==THREE.RGBAFormat ) { _gl.compressedTexImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, mipmap.data ); } else { _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data ); } } } else { // regular Texture (image, video, canvas) // use manually created mipmaps if available // if there are no manual mipmaps // set 0 level mipmap and then use GL to generate other mipmap levels if ( mipmaps.length > 0 && isImagePowerOfTwo ) { for ( var i = 0, il = mipmaps.length; i < il; i ++ ) { mipmap = mipmaps[ i ]; _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap ); } texture.generateMipmaps = false; } else { _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image ); } } if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D ); texture.needsUpdate = false; if ( texture.onUpdate ) texture.onUpdate(); } else { _gl.activeTexture( _gl.TEXTURE0 + slot ); _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture ); } }; function clampToMaxSize ( image, maxSize ) { if ( image.width <= maxSize && image.height <= maxSize ) { return image; } // Warning: Scaling through the canvas will only work with images that use // premultiplied alpha. var maxDimension = Math.max( image.width, image.height ); var newWidth = Math.floor( image.width * maxSize / maxDimension ); var newHeight = Math.floor( image.height * maxSize / maxDimension ); var canvas = document.createElement( 'canvas' ); canvas.width = newWidth; canvas.height = newHeight; var ctx = canvas.getContext( "2d" ); ctx.drawImage( image, 0, 0, image.width, image.height, 0, 0, newWidth, newHeight ); return canvas; } function setCubeTexture ( texture, slot ) { if ( texture.image.length === 6 ) { if ( texture.needsUpdate ) { if ( ! texture.image.__webglTextureCube ) { texture.addEventListener( 'dispose', onTextureDispose ); texture.image.__webglTextureCube = _gl.createTexture(); _this.info.memory.textures ++; } _gl.activeTexture( _gl.TEXTURE0 + slot ); _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube ); _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY ); var isCompressed = texture instanceof THREE.CompressedTexture; var cubeImage = []; for ( var i = 0; i < 6; i ++ ) { if ( _this.autoScaleCubemaps && ! isCompressed ) { cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize ); } else { cubeImage[ i ] = texture.image[ i ]; } } var image = cubeImage[ 0 ], isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ), glFormat = paramThreeToGL( texture.format ), glType = paramThreeToGL( texture.type ); setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo ); for ( var i = 0; i < 6; i ++ ) { if( !isCompressed ) { _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] ); } else { var mipmap, mipmaps = cubeImage[ i ].mipmaps; for( var j = 0, jl = mipmaps.length; j < jl; j ++ ) { mipmap = mipmaps[ j ]; if ( texture.format!==THREE.RGBAFormat ) { _gl.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, mipmap.data ); } else { _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data ); } } } } if ( texture.generateMipmaps && isImagePowerOfTwo ) { _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP ); } texture.needsUpdate = false; if ( texture.onUpdate ) texture.onUpdate(); } else { _gl.activeTexture( _gl.TEXTURE0 + slot ); _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube ); } } }; function setCubeTextureDynamic ( texture, slot ) { _gl.activeTexture( _gl.TEXTURE0 + slot ); _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.__webglTexture ); }; // Render targets function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) { _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer ); _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, renderTarget.__webglTexture, 0 ); }; function setupRenderBuffer ( renderbuffer, renderTarget ) { _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer ); if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) { _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height ); _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer ); /* For some reason this is not working. Defaulting to RGBA4. } else if( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) { _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height ); _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer ); */ } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) { _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height ); _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer ); } else { _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height ); } }; this.setRenderTarget = function ( renderTarget ) { var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube ); if ( renderTarget && ! renderTarget.__webglFramebuffer ) { if ( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true; if ( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true; renderTarget.addEventListener( 'dispose', onRenderTargetDispose ); renderTarget.__webglTexture = _gl.createTexture(); _this.info.memory.textures ++; // Setup texture, create render and frame buffers var isTargetPowerOfTwo = THREE.Math.isPowerOfTwo( renderTarget.width ) && THREE.Math.isPowerOfTwo( renderTarget.height ), glFormat = paramThreeToGL( renderTarget.format ), glType = paramThreeToGL( renderTarget.type ); if ( isCube ) { renderTarget.__webglFramebuffer = []; renderTarget.__webglRenderbuffer = []; _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture ); setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo ); for ( var i = 0; i < 6; i ++ ) { renderTarget.__webglFramebuffer[ i ] = _gl.createFramebuffer(); renderTarget.__webglRenderbuffer[ i ] = _gl.createRenderbuffer(); _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null ); setupFrameBuffer( renderTarget.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i ); setupRenderBuffer( renderTarget.__webglRenderbuffer[ i ], renderTarget ); } if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP ); } else { renderTarget.__webglFramebuffer = _gl.createFramebuffer(); if ( renderTarget.shareDepthFrom ) { renderTarget.__webglRenderbuffer = renderTarget.shareDepthFrom.__webglRenderbuffer; } else { renderTarget.__webglRenderbuffer = _gl.createRenderbuffer(); } _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture ); setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo ); _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null ); setupFrameBuffer( renderTarget.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D ); if ( renderTarget.shareDepthFrom ) { if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) { _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer ); } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) { _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer ); } } else { setupRenderBuffer( renderTarget.__webglRenderbuffer, renderTarget ); } if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D ); } // Release everything if ( isCube ) { _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null ); } else { _gl.bindTexture( _gl.TEXTURE_2D, null ); } _gl.bindRenderbuffer( _gl.RENDERBUFFER, null ); _gl.bindFramebuffer( _gl.FRAMEBUFFER, null ); } var framebuffer, width, height, vx, vy; if ( renderTarget ) { if ( isCube ) { framebuffer = renderTarget.__webglFramebuffer[ renderTarget.activeCubeFace ]; } else { framebuffer = renderTarget.__webglFramebuffer; } width = renderTarget.width; height = renderTarget.height; vx = 0; vy = 0; } else { framebuffer = null; width = _viewportWidth; height = _viewportHeight; vx = _viewportX; vy = _viewportY; } if ( framebuffer !== _currentFramebuffer ) { _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer ); _gl.viewport( vx, vy, width, height ); _currentFramebuffer = framebuffer; } _currentWidth = width; _currentHeight = height; }; function updateRenderTargetMipmap ( renderTarget ) { if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) { _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture ); _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP ); _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null ); } else { _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture ); _gl.generateMipmap( _gl.TEXTURE_2D ); _gl.bindTexture( _gl.TEXTURE_2D, null ); } }; // Fallback filters for non-power-of-2 textures function filterFallback ( f ) { if ( f === THREE.NearestFilter || f === THREE.NearestMipMapNearestFilter || f === THREE.NearestMipMapLinearFilter ) { return _gl.NEAREST; } return _gl.LINEAR; }; // Map three.js constants to WebGL constants function paramThreeToGL ( p ) { if ( p === THREE.RepeatWrapping ) return _gl.REPEAT; if ( p === THREE.ClampToEdgeWrapping ) return _gl.CLAMP_TO_EDGE; if ( p === THREE.MirroredRepeatWrapping ) return _gl.MIRRORED_REPEAT; if ( p === THREE.NearestFilter ) return _gl.NEAREST; if ( p === THREE.NearestMipMapNearestFilter ) return _gl.NEAREST_MIPMAP_NEAREST; if ( p === THREE.NearestMipMapLinearFilter ) return _gl.NEAREST_MIPMAP_LINEAR; if ( p === THREE.LinearFilter ) return _gl.LINEAR; if ( p === THREE.LinearMipMapNearestFilter ) return _gl.LINEAR_MIPMAP_NEAREST; if ( p === THREE.LinearMipMapLinearFilter ) return _gl.LINEAR_MIPMAP_LINEAR; if ( p === THREE.UnsignedByteType ) return _gl.UNSIGNED_BYTE; if ( p === THREE.UnsignedShort4444Type ) return _gl.UNSIGNED_SHORT_4_4_4_4; if ( p === THREE.UnsignedShort5551Type ) return _gl.UNSIGNED_SHORT_5_5_5_1; if ( p === THREE.UnsignedShort565Type ) return _gl.UNSIGNED_SHORT_5_6_5; if ( p === THREE.ByteType ) return _gl.BYTE; if ( p === THREE.ShortType ) return _gl.SHORT; if ( p === THREE.UnsignedShortType ) return _gl.UNSIGNED_SHORT; if ( p === THREE.IntType ) return _gl.INT; if ( p === THREE.UnsignedIntType ) return _gl.UNSIGNED_INT; if ( p === THREE.FloatType ) return _gl.FLOAT; if ( p === THREE.AlphaFormat ) return _gl.ALPHA; if ( p === THREE.RGBFormat ) return _gl.RGB; if ( p === THREE.RGBAFormat ) return _gl.RGBA; if ( p === THREE.LuminanceFormat ) return _gl.LUMINANCE; if ( p === THREE.LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA; if ( p === THREE.AddEquation ) return _gl.FUNC_ADD; if ( p === THREE.SubtractEquation ) return _gl.FUNC_SUBTRACT; if ( p === THREE.ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT; if ( p === THREE.ZeroFactor ) return _gl.ZERO; if ( p === THREE.OneFactor ) return _gl.ONE; if ( p === THREE.SrcColorFactor ) return _gl.SRC_COLOR; if ( p === THREE.OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR; if ( p === THREE.SrcAlphaFactor ) return _gl.SRC_ALPHA; if ( p === THREE.OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA; if ( p === THREE.DstAlphaFactor ) return _gl.DST_ALPHA; if ( p === THREE.OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA; if ( p === THREE.DstColorFactor ) return _gl.DST_COLOR; if ( p === THREE.OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR; if ( p === THREE.SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE; if ( _glExtensionCompressedTextureS3TC !== undefined ) { if ( p === THREE.RGB_S3TC_DXT1_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGB_S3TC_DXT1_EXT; if ( p === THREE.RGBA_S3TC_DXT1_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT1_EXT; if ( p === THREE.RGBA_S3TC_DXT3_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT3_EXT; if ( p === THREE.RGBA_S3TC_DXT5_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT5_EXT; } return 0; }; // Allocations function allocateBones ( object ) { if ( _supportsBoneTextures && object && object.useVertexTexture ) { return 1024; } else { // default for when object is not specified // ( for example when prebuilding shader // to be used with multiple objects ) // // - leave some extra space for other uniforms // - limit here is ANGLE's 254 max uniform vectors // (up to 54 should be safe) var nVertexUniforms = _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS ); var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 ); var maxBones = nVertexMatrices; if ( object !== undefined && object instanceof THREE.SkinnedMesh ) { maxBones = Math.min( object.bones.length, maxBones ); if ( maxBones < object.bones.length ) { console.warn( "WebGLRenderer: too many bones - " + object.bones.length + ", this GPU supports just " + maxBones + " (try OpenGL instead of ANGLE)" ); } } return maxBones; } }; function allocateLights( lights ) { var dirLights = 0; var pointLights = 0; var spotLights = 0; var hemiLights = 0; for ( var l = 0, ll = lights.length; l < ll; l ++ ) { var light = lights[ l ]; if ( light.onlyShadow ) continue; if ( light instanceof THREE.DirectionalLight ) dirLights ++; if ( light instanceof THREE.PointLight ) pointLights ++; if ( light instanceof THREE.SpotLight ) spotLights ++; if ( light instanceof THREE.HemisphereLight ) hemiLights ++; } return { 'directional' : dirLights, 'point' : pointLights, 'spot': spotLights, 'hemi': hemiLights }; }; function allocateShadows( lights ) { var maxShadows = 0; for ( var l = 0, ll = lights.length; l < ll; l++ ) { var light = lights[ l ]; if ( ! light.castShadow ) continue; if ( light instanceof THREE.SpotLight ) maxShadows ++; if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++; } return maxShadows; }; // Initialization function initGL() { try { var attributes = { alpha: _alpha, premultipliedAlpha: _premultipliedAlpha, antialias: _antialias, stencil: _stencil, preserveDrawingBuffer: _preserveDrawingBuffer }; _gl = _context || _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes ); if ( _gl === null ) { throw 'Error creating WebGL context.'; } } catch ( error ) { console.error( error ); } _glExtensionTextureFloat = _gl.getExtension( 'OES_texture_float' ); _glExtensionTextureFloatLinear = _gl.getExtension( 'OES_texture_float_linear' ); _glExtensionStandardDerivatives = _gl.getExtension( 'OES_standard_derivatives' ); _glExtensionTextureFilterAnisotropic = _gl.getExtension( 'EXT_texture_filter_anisotropic' ) || _gl.getExtension( 'MOZ_EXT_texture_filter_anisotropic' ) || _gl.getExtension( 'WEBKIT_EXT_texture_filter_anisotropic' ); _glExtensionCompressedTextureS3TC = _gl.getExtension( 'WEBGL_compressed_texture_s3tc' ) || _gl.getExtension( 'MOZ_WEBGL_compressed_texture_s3tc' ) || _gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_s3tc' ); if ( ! _glExtensionTextureFloat ) { console.log( 'THREE.WebGLRenderer: Float textures not supported.' ); } if ( ! _glExtensionStandardDerivatives ) { console.log( 'THREE.WebGLRenderer: Standard derivatives not supported.' ); } if ( ! _glExtensionTextureFilterAnisotropic ) { console.log( 'THREE.WebGLRenderer: Anisotropic texture filtering not supported.' ); } if ( ! _glExtensionCompressedTextureS3TC ) { console.log( 'THREE.WebGLRenderer: S3TC compressed textures not supported.' ); } if ( _gl.getShaderPrecisionFormat === undefined ) { _gl.getShaderPrecisionFormat = function() { return { "rangeMin" : 1, "rangeMax" : 1, "precision" : 1 }; } } }; function setDefaultGLState () { _gl.clearColor( 0, 0, 0, 1 ); _gl.clearDepth( 1 ); _gl.clearStencil( 0 ); _gl.enable( _gl.DEPTH_TEST ); _gl.depthFunc( _gl.LEQUAL ); _gl.frontFace( _gl.CCW ); _gl.cullFace( _gl.BACK ); _gl.enable( _gl.CULL_FACE ); _gl.enable( _gl.BLEND ); _gl.blendEquation( _gl.FUNC_ADD ); _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA ); _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight ); _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha ); }; // default plugins (order is important) this.shadowMapPlugin = new THREE.ShadowMapPlugin(); this.addPrePlugin( this.shadowMapPlugin ); this.addPostPlugin( new THREE.SpritePlugin() ); this.addPostPlugin( new THREE.LensFlarePlugin() ); }; /** * @author szimek / https://github.com/szimek/ * @author alteredq / http://alteredqualia.com/ */ THREE.WebGLRenderTarget = function ( width, height, options ) { this.width = width; this.height = height; options = options || {}; this.wrapS = options.wrapS !== undefined ? options.wrapS : THREE.ClampToEdgeWrapping; this.wrapT = options.wrapT !== undefined ? options.wrapT : THREE.ClampToEdgeWrapping; this.magFilter = options.magFilter !== undefined ? options.magFilter : THREE.LinearFilter; this.minFilter = options.minFilter !== undefined ? options.minFilter : THREE.LinearMipMapLinearFilter; this.anisotropy = options.anisotropy !== undefined ? options.anisotropy : 1; this.offset = new THREE.Vector2( 0, 0 ); this.repeat = new THREE.Vector2( 1, 1 ); this.format = options.format !== undefined ? options.format : THREE.RGBAFormat; this.type = options.type !== undefined ? options.type : THREE.UnsignedByteType; this.depthBuffer = options.depthBuffer !== undefined ? options.depthBuffer : true; this.stencilBuffer = options.stencilBuffer !== undefined ? options.stencilBuffer : true; this.generateMipmaps = true; this.shareDepthFrom = null; }; THREE.WebGLRenderTarget.prototype = { constructor: THREE.WebGLRenderTarget, clone: function () { var tmp = new THREE.WebGLRenderTarget( this.width, this.height ); tmp.wrapS = this.wrapS; tmp.wrapT = this.wrapT; tmp.magFilter = this.magFilter; tmp.minFilter = this.minFilter; tmp.anisotropy = this.anisotropy; tmp.offset.copy( this.offset ); tmp.repeat.copy( this.repeat ); tmp.format = this.format; tmp.type = this.type; tmp.depthBuffer = this.depthBuffer; tmp.stencilBuffer = this.stencilBuffer; tmp.generateMipmaps = this.generateMipmaps; tmp.shareDepthFrom = this.shareDepthFrom; return tmp; }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } }; THREE.EventDispatcher.prototype.apply( THREE.WebGLRenderTarget.prototype ); /** * @author alteredq / http://alteredqualia.com */ THREE.WebGLRenderTargetCube = function ( width, height, options ) { THREE.WebGLRenderTarget.call( this, width, height, options ); this.activeCubeFace = 0; // PX 0, NX 1, PY 2, NY 3, PZ 4, NZ 5 }; THREE.WebGLRenderTargetCube.prototype = Object.create( THREE.WebGLRenderTarget.prototype ); /** * @author mrdoob / http://mrdoob.com/ */ THREE.RenderableVertex = function () { this.position = new THREE.Vector3(); this.positionWorld = new THREE.Vector3(); this.positionScreen = new THREE.Vector4(); this.visible = true; }; THREE.RenderableVertex.prototype.copy = function ( vertex ) { this.positionWorld.copy( vertex.positionWorld ); this.positionScreen.copy( vertex.positionScreen ); }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.RenderableFace3 = function () { this.id = 0; this.v1 = new THREE.RenderableVertex(); this.v2 = new THREE.RenderableVertex(); this.v3 = new THREE.RenderableVertex(); this.centroidModel = new THREE.Vector3(); this.normalModel = new THREE.Vector3(); this.vertexNormalsModel = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ]; this.vertexNormalsLength = 0; this.color = null; this.material = null; this.uvs = [[]]; this.z = 0; }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.RenderableObject = function () { this.id = 0; this.object = null; this.z = 0; }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.RenderableSprite = function () { this.id = 0; this.object = null; this.x = 0; this.y = 0; this.z = 0; this.rotation = 0; this.scale = new THREE.Vector2(); this.material = null; }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.RenderableLine = function () { this.id = 0; this.v1 = new THREE.RenderableVertex(); this.v2 = new THREE.RenderableVertex(); this.vertexColors = [ new THREE.Color(), new THREE.Color() ]; this.material = null; this.z = 0; }; /** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ THREE.GeometryUtils = { // Merge two geometries or geometry and geometry from object (using object's transform) merge: function ( geometry1, object2 /* mesh | geometry */, materialIndexOffset ) { var matrix, normalMatrix, vertexOffset = geometry1.vertices.length, uvPosition = geometry1.faceVertexUvs[ 0 ].length, geometry2 = object2 instanceof THREE.Mesh ? object2.geometry : object2, vertices1 = geometry1.vertices, vertices2 = geometry2.vertices, faces1 = geometry1.faces, faces2 = geometry2.faces, uvs1 = geometry1.faceVertexUvs[ 0 ], uvs2 = geometry2.faceVertexUvs[ 0 ]; if ( materialIndexOffset === undefined ) materialIndexOffset = 0; if ( object2 instanceof THREE.Mesh ) { object2.matrixAutoUpdate && object2.updateMatrix(); matrix = object2.matrix; normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix ); } // vertices for ( var i = 0, il = vertices2.length; i < il; i ++ ) { var vertex = vertices2[ i ]; var vertexCopy = vertex.clone(); if ( matrix ) vertexCopy.applyMatrix4( matrix ); vertices1.push( vertexCopy ); } // faces for ( i = 0, il = faces2.length; i < il; i ++ ) { var face = faces2[ i ], faceCopy, normal, color, faceVertexNormals = face.vertexNormals, faceVertexColors = face.vertexColors; faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset ); faceCopy.normal.copy( face.normal ); if ( normalMatrix ) { faceCopy.normal.applyMatrix3( normalMatrix ).normalize(); } for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) { normal = faceVertexNormals[ j ].clone(); if ( normalMatrix ) { normal.applyMatrix3( normalMatrix ).normalize(); } faceCopy.vertexNormals.push( normal ); } faceCopy.color.copy( face.color ); for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) { color = faceVertexColors[ j ]; faceCopy.vertexColors.push( color.clone() ); } faceCopy.materialIndex = face.materialIndex + materialIndexOffset; faceCopy.centroid.copy( face.centroid ); if ( matrix ) { faceCopy.centroid.applyMatrix4( matrix ); } faces1.push( faceCopy ); } // uvs for ( i = 0, il = uvs2.length; i < il; i ++ ) { var uv = uvs2[ i ], uvCopy = []; for ( var j = 0, jl = uv.length; j < jl; j ++ ) { uvCopy.push( new THREE.Vector2( uv[ j ].x, uv[ j ].y ) ); } uvs1.push( uvCopy ); } }, // Get random point in triangle (via barycentric coordinates) // (uniform distribution) // http://www.cgafaq.info/wiki/Random_Point_In_Triangle randomPointInTriangle: function () { var vector = new THREE.Vector3(); return function ( vectorA, vectorB, vectorC ) { var point = new THREE.Vector3(); var a = THREE.Math.random16(); var b = THREE.Math.random16(); if ( ( a + b ) > 1 ) { a = 1 - a; b = 1 - b; } var c = 1 - a - b; point.copy( vectorA ); point.multiplyScalar( a ); vector.copy( vectorB ); vector.multiplyScalar( b ); point.add( vector ); vector.copy( vectorC ); vector.multiplyScalar( c ); point.add( vector ); return point; }; }(), // Get random point in face (triangle / quad) // (uniform distribution) randomPointInFace: function ( face, geometry, useCachedAreas ) { var vA, vB, vC, vD; vA = geometry.vertices[ face.a ]; vB = geometry.vertices[ face.b ]; vC = geometry.vertices[ face.c ]; return THREE.GeometryUtils.randomPointInTriangle( vA, vB, vC ); }, // Get uniformly distributed random points in mesh // - create array with cumulative sums of face areas // - pick random number from 0 to total area // - find corresponding place in area array by binary search // - get random point in face randomPointsInGeometry: function ( geometry, n ) { var face, i, faces = geometry.faces, vertices = geometry.vertices, il = faces.length, totalArea = 0, cumulativeAreas = [], vA, vB, vC, vD; // precompute face areas for ( i = 0; i < il; i ++ ) { face = faces[ i ]; vA = vertices[ face.a ]; vB = vertices[ face.b ]; vC = vertices[ face.c ]; face._area = THREE.GeometryUtils.triangleArea( vA, vB, vC ); totalArea += face._area; cumulativeAreas[ i ] = totalArea; } // binary search cumulative areas array function binarySearchIndices( value ) { function binarySearch( start, end ) { // return closest larger index // if exact number is not found if ( end < start ) return start; var mid = start + Math.floor( ( end - start ) / 2 ); if ( cumulativeAreas[ mid ] > value ) { return binarySearch( start, mid - 1 ); } else if ( cumulativeAreas[ mid ] < value ) { return binarySearch( mid + 1, end ); } else { return mid; } } var result = binarySearch( 0, cumulativeAreas.length - 1 ) return result; } // pick random face weighted by face area var r, index, result = []; var stats = {}; for ( i = 0; i < n; i ++ ) { r = THREE.Math.random16() * totalArea; index = binarySearchIndices( r ); result[ i ] = THREE.GeometryUtils.randomPointInFace( faces[ index ], geometry, true ); if ( ! stats[ index ] ) { stats[ index ] = 1; } else { stats[ index ] += 1; } } return result; }, // Get triangle area (half of parallelogram) // http://mathworld.wolfram.com/TriangleArea.html triangleArea: function () { var vector1 = new THREE.Vector3(); var vector2 = new THREE.Vector3(); return function ( vectorA, vectorB, vectorC ) { vector1.subVectors( vectorB, vectorA ); vector2.subVectors( vectorC, vectorA ); vector1.cross( vector2 ); return 0.5 * vector1.length(); }; }(), // Center geometry so that 0,0,0 is in center of bounding box center: function ( geometry ) { geometry.computeBoundingBox(); var bb = geometry.boundingBox; var offset = new THREE.Vector3(); offset.addVectors( bb.min, bb.max ); offset.multiplyScalar( -0.5 ); geometry.applyMatrix( new THREE.Matrix4().makeTranslation( offset.x, offset.y, offset.z ) ); geometry.computeBoundingBox(); return offset; }, triangulateQuads: function ( geometry ) { var i, il, j, jl; var faces = []; var faceVertexUvs = []; for ( i = 0, il = geometry.faceVertexUvs.length; i < il; i ++ ) { faceVertexUvs[ i ] = []; } for ( i = 0, il = geometry.faces.length; i < il; i ++ ) { var face = geometry.faces[ i ]; faces.push( face ); for ( j = 0, jl = geometry.faceVertexUvs.length; j < jl; j ++ ) { faceVertexUvs[ j ].push( geometry.faceVertexUvs[ j ][ i ] ); } } geometry.faces = faces; geometry.faceVertexUvs = faceVertexUvs; geometry.computeCentroids(); geometry.computeFaceNormals(); geometry.computeVertexNormals(); if ( geometry.hasTangents ) geometry.computeTangents(); } }; /** * @author alteredq / http://alteredqualia.com/ * @author mrdoob / http://mrdoob.com/ */ THREE.ImageUtils = { crossOrigin: undefined, loadTexture: function ( url, mapping, onLoad, onError ) { var loader = new THREE.ImageLoader(); loader.crossOrigin = this.crossOrigin; var texture = new THREE.Texture( undefined, mapping ); var image = loader.load( url, function () { texture.needsUpdate = true; if ( onLoad ) onLoad( texture ); } ); texture.image = image; texture.sourceFile = url; return texture; }, loadCompressedTexture: function ( url, mapping, onLoad, onError ) { var texture = new THREE.CompressedTexture(); texture.mapping = mapping; var request = new XMLHttpRequest(); request.onload = function () { var buffer = request.response; var dds = THREE.ImageUtils.parseDDS( buffer, true ); texture.format = dds.format; texture.mipmaps = dds.mipmaps; texture.image.width = dds.width; texture.image.height = dds.height; // gl.generateMipmap fails for compressed textures // mipmaps must be embedded in the DDS file // or texture filters must not use mipmapping texture.generateMipmaps = false; texture.needsUpdate = true; if ( onLoad ) onLoad( texture ); } request.onerror = onError; request.open( 'GET', url, true ); request.responseType = "arraybuffer"; request.send( null ); return texture; }, loadTextureCube: function ( array, mapping, onLoad, onError ) { var images = []; images.loadCount = 0; var texture = new THREE.Texture(); texture.image = images; if ( mapping !== undefined ) texture.mapping = mapping; // no flipping needed for cube textures texture.flipY = false; for ( var i = 0, il = array.length; i < il; ++ i ) { var cubeImage = new Image(); images[ i ] = cubeImage; cubeImage.onload = function () { images.loadCount += 1; if ( images.loadCount === 6 ) { texture.needsUpdate = true; if ( onLoad ) onLoad( texture ); } }; cubeImage.onerror = onError; cubeImage.crossOrigin = this.crossOrigin; cubeImage.src = array[ i ]; } return texture; }, loadCompressedTextureCube: function ( array, mapping, onLoad, onError ) { var images = []; images.loadCount = 0; var texture = new THREE.CompressedTexture(); texture.image = images; if ( mapping !== undefined ) texture.mapping = mapping; // no flipping for cube textures // (also flipping doesn't work for compressed textures ) texture.flipY = false; // can't generate mipmaps for compressed textures // mips must be embedded in DDS files texture.generateMipmaps = false; var generateCubeFaceCallback = function ( rq, img ) { return function () { var buffer = rq.response; var dds = THREE.ImageUtils.parseDDS( buffer, true ); img.format = dds.format; img.mipmaps = dds.mipmaps; img.width = dds.width; img.height = dds.height; images.loadCount += 1; if ( images.loadCount === 6 ) { texture.format = dds.format; texture.needsUpdate = true; if ( onLoad ) onLoad( texture ); } } } // compressed cubemap textures as 6 separate DDS files if ( array instanceof Array ) { for ( var i = 0, il = array.length; i < il; ++ i ) { var cubeImage = {}; images[ i ] = cubeImage; var request = new XMLHttpRequest(); request.onload = generateCubeFaceCallback( request, cubeImage ); request.onerror = onError; var url = array[ i ]; request.open( 'GET', url, true ); request.responseType = "arraybuffer"; request.send( null ); } // compressed cubemap texture stored in a single DDS file } else { var url = array; var request = new XMLHttpRequest(); request.onload = function( ) { var buffer = request.response; var dds = THREE.ImageUtils.parseDDS( buffer, true ); if ( dds.isCubemap ) { var faces = dds.mipmaps.length / dds.mipmapCount; for ( var f = 0; f < faces; f ++ ) { images[ f ] = { mipmaps : [] }; for ( var i = 0; i < dds.mipmapCount; i ++ ) { images[ f ].mipmaps.push( dds.mipmaps[ f * dds.mipmapCount + i ] ); images[ f ].format = dds.format; images[ f ].width = dds.width; images[ f ].height = dds.height; } } texture.format = dds.format; texture.needsUpdate = true; if ( onLoad ) onLoad( texture ); } } request.onerror = onError; request.open( 'GET', url, true ); request.responseType = "arraybuffer"; request.send( null ); } return texture; }, loadDDSTexture: function ( url, mapping, onLoad, onError ) { var images = []; images.loadCount = 0; var texture = new THREE.CompressedTexture(); texture.image = images; if ( mapping !== undefined ) texture.mapping = mapping; // no flipping for cube textures // (also flipping doesn't work for compressed textures ) texture.flipY = false; // can't generate mipmaps for compressed textures // mips must be embedded in DDS files texture.generateMipmaps = false; { var request = new XMLHttpRequest(); request.onload = function( ) { var buffer = request.response; var dds = THREE.ImageUtils.parseDDS( buffer, true ); if ( dds.isCubemap ) { var faces = dds.mipmaps.length / dds.mipmapCount; for ( var f = 0; f < faces; f ++ ) { images[ f ] = { mipmaps : [] }; for ( var i = 0; i < dds.mipmapCount; i ++ ) { images[ f ].mipmaps.push( dds.mipmaps[ f * dds.mipmapCount + i ] ); images[ f ].format = dds.format; images[ f ].width = dds.width; images[ f ].height = dds.height; } } } else { texture.image.width = dds.width; texture.image.height = dds.height; texture.mipmaps = dds.mipmaps; } texture.format = dds.format; texture.needsUpdate = true; if ( onLoad ) onLoad( texture ); } request.onerror = onError; request.open( 'GET', url, true ); request.responseType = "arraybuffer"; request.send( null ); } return texture; }, parseDDS: function ( buffer, loadMipmaps ) { var dds = { mipmaps: [], width: 0, height: 0, format: null, mipmapCount: 1 }; // Adapted from @toji's DDS utils // https://github.com/toji/webgl-texture-utils/blob/master/texture-util/dds.js // All values and structures referenced from: // http://msdn.microsoft.com/en-us/library/bb943991.aspx/ var DDS_MAGIC = 0x20534444; var DDSD_CAPS = 0x1, DDSD_HEIGHT = 0x2, DDSD_WIDTH = 0x4, DDSD_PITCH = 0x8, DDSD_PIXELFORMAT = 0x1000, DDSD_MIPMAPCOUNT = 0x20000, DDSD_LINEARSIZE = 0x80000, DDSD_DEPTH = 0x800000; var DDSCAPS_COMPLEX = 0x8, DDSCAPS_MIPMAP = 0x400000, DDSCAPS_TEXTURE = 0x1000; var DDSCAPS2_CUBEMAP = 0x200, DDSCAPS2_CUBEMAP_POSITIVEX = 0x400, DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800, DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000, DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000, DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000, DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000, DDSCAPS2_VOLUME = 0x200000; var DDPF_ALPHAPIXELS = 0x1, DDPF_ALPHA = 0x2, DDPF_FOURCC = 0x4, DDPF_RGB = 0x40, DDPF_YUV = 0x200, DDPF_LUMINANCE = 0x20000; function fourCCToInt32( value ) { return value.charCodeAt(0) + (value.charCodeAt(1) << 8) + (value.charCodeAt(2) << 16) + (value.charCodeAt(3) << 24); } function int32ToFourCC( value ) { return String.fromCharCode( value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, (value >> 24) & 0xff ); } function loadARGBMip( buffer, dataOffset, width, height ) { var dataLength = width*height*4; var srcBuffer = new Uint8Array( buffer, dataOffset, dataLength ); var byteArray = new Uint8Array( dataLength ); var dst = 0; var src = 0; for ( var y = 0; y < height; y++ ) { for ( var x = 0; x < width; x++ ) { var b = srcBuffer[src]; src++; var g = srcBuffer[src]; src++; var r = srcBuffer[src]; src++; var a = srcBuffer[src]; src++; byteArray[dst] = r; dst++; //r byteArray[dst] = g; dst++; //g byteArray[dst] = b; dst++; //b byteArray[dst] = a; dst++; //a } } return byteArray; } var FOURCC_DXT1 = fourCCToInt32("DXT1"); var FOURCC_DXT3 = fourCCToInt32("DXT3"); var FOURCC_DXT5 = fourCCToInt32("DXT5"); var headerLengthInt = 31; // The header length in 32 bit ints // Offsets into the header array var off_magic = 0; var off_size = 1; var off_flags = 2; var off_height = 3; var off_width = 4; var off_mipmapCount = 7; var off_pfFlags = 20; var off_pfFourCC = 21; var off_RGBBitCount = 22; var off_RBitMask = 23; var off_GBitMask = 24; var off_BBitMask = 25; var off_ABitMask = 26; var off_caps = 27; var off_caps2 = 28; var off_caps3 = 29; var off_caps4 = 30; // Parse header var header = new Int32Array( buffer, 0, headerLengthInt ); if ( header[ off_magic ] !== DDS_MAGIC ) { console.error( "ImageUtils.parseDDS(): Invalid magic number in DDS header" ); return dds; } if ( ! header[ off_pfFlags ] & DDPF_FOURCC ) { console.error( "ImageUtils.parseDDS(): Unsupported format, must contain a FourCC code" ); return dds; } var blockBytes; var fourCC = header[ off_pfFourCC ]; var isRGBAUncompressed = false; switch ( fourCC ) { case FOURCC_DXT1: blockBytes = 8; dds.format = THREE.RGB_S3TC_DXT1_Format; break; case FOURCC_DXT3: blockBytes = 16; dds.format = THREE.RGBA_S3TC_DXT3_Format; break; case FOURCC_DXT5: blockBytes = 16; dds.format = THREE.RGBA_S3TC_DXT5_Format; break; default: if( header[off_RGBBitCount] ==32 && header[off_RBitMask]&0xff0000 && header[off_GBitMask]&0xff00 && header[off_BBitMask]&0xff && header[off_ABitMask]&0xff000000 ) { isRGBAUncompressed = true; blockBytes = 64; dds.format = THREE.RGBAFormat; } else { console.error( "ImageUtils.parseDDS(): Unsupported FourCC code: ", int32ToFourCC( fourCC ) ); return dds; } } dds.mipmapCount = 1; if ( header[ off_flags ] & DDSD_MIPMAPCOUNT && loadMipmaps !== false ) { dds.mipmapCount = Math.max( 1, header[ off_mipmapCount ] ); } //TODO: Verify that all faces of the cubemap are present with DDSCAPS2_CUBEMAP_POSITIVEX, etc. dds.isCubemap = header[ off_caps2 ] & DDSCAPS2_CUBEMAP ? true : false; dds.width = header[ off_width ]; dds.height = header[ off_height ]; var dataOffset = header[ off_size ] + 4; // Extract mipmaps buffers var width = dds.width; var height = dds.height; var faces = dds.isCubemap ? 6 : 1; for ( var face = 0; face < faces; face ++ ) { for ( var i = 0; i < dds.mipmapCount; i ++ ) { if( isRGBAUncompressed ) { var byteArray = loadARGBMip( buffer, dataOffset, width, height ); var dataLength = byteArray.length; } else { var dataLength = Math.max( 4, width ) / 4 * Math.max( 4, height ) / 4 * blockBytes; var byteArray = new Uint8Array( buffer, dataOffset, dataLength ); } var mipmap = { "data": byteArray, "width": width, "height": height }; dds.mipmaps.push( mipmap ); dataOffset += dataLength; width = Math.max( width * 0.5, 1 ); height = Math.max( height * 0.5, 1 ); } width = dds.width; height = dds.height; } return dds; }, getNormalMap: function ( image, depth ) { // Adapted from http://www.paulbrunt.co.uk/lab/heightnormal/ var cross = function ( a, b ) { return [ a[ 1 ] * b[ 2 ] - a[ 2 ] * b[ 1 ], a[ 2 ] * b[ 0 ] - a[ 0 ] * b[ 2 ], a[ 0 ] * b[ 1 ] - a[ 1 ] * b[ 0 ] ]; } var subtract = function ( a, b ) { return [ a[ 0 ] - b[ 0 ], a[ 1 ] - b[ 1 ], a[ 2 ] - b[ 2 ] ]; } var normalize = function ( a ) { var l = Math.sqrt( a[ 0 ] * a[ 0 ] + a[ 1 ] * a[ 1 ] + a[ 2 ] * a[ 2 ] ); return [ a[ 0 ] / l, a[ 1 ] / l, a[ 2 ] / l ]; } depth = depth | 1; var width = image.width; var height = image.height; var canvas = document.createElement( 'canvas' ); canvas.width = width; canvas.height = height; var context = canvas.getContext( '2d' ); context.drawImage( image, 0, 0 ); var data = context.getImageData( 0, 0, width, height ).data; var imageData = context.createImageData( width, height ); var output = imageData.data; for ( var x = 0; x < width; x ++ ) { for ( var y = 0; y < height; y ++ ) { var ly = y - 1 < 0 ? 0 : y - 1; var uy = y + 1 > height - 1 ? height - 1 : y + 1; var lx = x - 1 < 0 ? 0 : x - 1; var ux = x + 1 > width - 1 ? width - 1 : x + 1; var points = []; var origin = [ 0, 0, data[ ( y * width + x ) * 4 ] / 255 * depth ]; points.push( [ - 1, 0, data[ ( y * width + lx ) * 4 ] / 255 * depth ] ); points.push( [ - 1, - 1, data[ ( ly * width + lx ) * 4 ] / 255 * depth ] ); points.push( [ 0, - 1, data[ ( ly * width + x ) * 4 ] / 255 * depth ] ); points.push( [ 1, - 1, data[ ( ly * width + ux ) * 4 ] / 255 * depth ] ); points.push( [ 1, 0, data[ ( y * width + ux ) * 4 ] / 255 * depth ] ); points.push( [ 1, 1, data[ ( uy * width + ux ) * 4 ] / 255 * depth ] ); points.push( [ 0, 1, data[ ( uy * width + x ) * 4 ] / 255 * depth ] ); points.push( [ - 1, 1, data[ ( uy * width + lx ) * 4 ] / 255 * depth ] ); var normals = []; var num_points = points.length; for ( var i = 0; i < num_points; i ++ ) { var v1 = points[ i ]; var v2 = points[ ( i + 1 ) % num_points ]; v1 = subtract( v1, origin ); v2 = subtract( v2, origin ); normals.push( normalize( cross( v1, v2 ) ) ); } var normal = [ 0, 0, 0 ]; for ( var i = 0; i < normals.length; i ++ ) { normal[ 0 ] += normals[ i ][ 0 ]; normal[ 1 ] += normals[ i ][ 1 ]; normal[ 2 ] += normals[ i ][ 2 ]; } normal[ 0 ] /= normals.length; normal[ 1 ] /= normals.length; normal[ 2 ] /= normals.length; var idx = ( y * width + x ) * 4; output[ idx ] = ( ( normal[ 0 ] + 1.0 ) / 2.0 * 255 ) | 0; output[ idx + 1 ] = ( ( normal[ 1 ] + 1.0 ) / 2.0 * 255 ) | 0; output[ idx + 2 ] = ( normal[ 2 ] * 255 ) | 0; output[ idx + 3 ] = 255; } } context.putImageData( imageData, 0, 0 ); return canvas; }, generateDataTexture: function ( width, height, color ) { var size = width * height; var data = new Uint8Array( 3 * size ); var r = Math.floor( color.r * 255 ); var g = Math.floor( color.g * 255 ); var b = Math.floor( color.b * 255 ); for ( var i = 0; i < size; i ++ ) { data[ i * 3 ] = r; data[ i * 3 + 1 ] = g; data[ i * 3 + 2 ] = b; } var texture = new THREE.DataTexture( data, width, height, THREE.RGBFormat ); texture.needsUpdate = true; return texture; } }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.SceneUtils = { createMultiMaterialObject: function ( geometry, materials ) { var group = new THREE.Object3D(); for ( var i = 0, l = materials.length; i < l; i ++ ) { group.add( new THREE.Mesh( geometry, materials[ i ] ) ); } return group; }, detach : function ( child, parent, scene ) { child.applyMatrix( parent.matrixWorld ); parent.remove( child ); scene.add( child ); }, attach: function ( child, scene, parent ) { var matrixWorldInverse = new THREE.Matrix4(); matrixWorldInverse.getInverse( parent.matrixWorld ); child.applyMatrix( matrixWorldInverse ); scene.remove( child ); parent.add( child ); } }; /** * @author zz85 / http://www.lab4games.net/zz85/blog * @author alteredq / http://alteredqualia.com/ * * For Text operations in three.js (See TextGeometry) * * It uses techniques used in: * * typeface.js and canvastext * For converting fonts and rendering with javascript * http://typeface.neocracy.org * * Triangulation ported from AS3 * Simple Polygon Triangulation * http://actionsnippet.com/?p=1462 * * A Method to triangulate shapes with holes * http://www.sakri.net/blog/2009/06/12/an-approach-to-triangulating-polygons-with-holes/ * */ THREE.FontUtils = { faces : {}, // Just for now. face[weight][style] face : "helvetiker", weight: "normal", style : "normal", size : 150, divisions : 10, getFace : function() { return this.faces[ this.face ][ this.weight ][ this.style ]; }, loadFace : function( data ) { var family = data.familyName.toLowerCase(); var ThreeFont = this; ThreeFont.faces[ family ] = ThreeFont.faces[ family ] || {}; ThreeFont.faces[ family ][ data.cssFontWeight ] = ThreeFont.faces[ family ][ data.cssFontWeight ] || {}; ThreeFont.faces[ family ][ data.cssFontWeight ][ data.cssFontStyle ] = data; var face = ThreeFont.faces[ family ][ data.cssFontWeight ][ data.cssFontStyle ] = data; return data; }, drawText : function( text ) { var characterPts = [], allPts = []; // RenderText var i, p, face = this.getFace(), scale = this.size / face.resolution, offset = 0, chars = String( text ).split( '' ), length = chars.length; var fontPaths = []; for ( i = 0; i < length; i ++ ) { var path = new THREE.Path(); var ret = this.extractGlyphPoints( chars[ i ], face, scale, offset, path ); offset += ret.offset; fontPaths.push( ret.path ); } // get the width var width = offset / 2; // // for ( p = 0; p < allPts.length; p++ ) { // // allPts[ p ].x -= width; // // } //var extract = this.extractPoints( allPts, characterPts ); //extract.contour = allPts; //extract.paths = fontPaths; //extract.offset = width; return { paths : fontPaths, offset : width }; }, extractGlyphPoints : function( c, face, scale, offset, path ) { var pts = []; var i, i2, divisions, outline, action, length, scaleX, scaleY, x, y, cpx, cpy, cpx0, cpy0, cpx1, cpy1, cpx2, cpy2, laste, glyph = face.glyphs[ c ] || face.glyphs[ '?' ]; if ( !glyph ) return; if ( glyph.o ) { outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) ); length = outline.length; scaleX = scale; scaleY = scale; for ( i = 0; i < length; ) { action = outline[ i ++ ]; //console.log( action ); switch( action ) { case 'm': // Move To x = outline[ i++ ] * scaleX + offset; y = outline[ i++ ] * scaleY; path.moveTo( x, y ); break; case 'l': // Line To x = outline[ i++ ] * scaleX + offset; y = outline[ i++ ] * scaleY; path.lineTo(x,y); break; case 'q': // QuadraticCurveTo cpx = outline[ i++ ] * scaleX + offset; cpy = outline[ i++ ] * scaleY; cpx1 = outline[ i++ ] * scaleX + offset; cpy1 = outline[ i++ ] * scaleY; path.quadraticCurveTo(cpx1, cpy1, cpx, cpy); laste = pts[ pts.length - 1 ]; if ( laste ) { cpx0 = laste.x; cpy0 = laste.y; for ( i2 = 1, divisions = this.divisions; i2 <= divisions; i2 ++ ) { var t = i2 / divisions; var tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx ); var ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy ); } } break; case 'b': // Cubic Bezier Curve cpx = outline[ i++ ] * scaleX + offset; cpy = outline[ i++ ] * scaleY; cpx1 = outline[ i++ ] * scaleX + offset; cpy1 = outline[ i++ ] * -scaleY; cpx2 = outline[ i++ ] * scaleX + offset; cpy2 = outline[ i++ ] * -scaleY; path.bezierCurveTo( cpx, cpy, cpx1, cpy1, cpx2, cpy2 ); laste = pts[ pts.length - 1 ]; if ( laste ) { cpx0 = laste.x; cpy0 = laste.y; for ( i2 = 1, divisions = this.divisions; i2 <= divisions; i2 ++ ) { var t = i2 / divisions; var tx = THREE.Shape.Utils.b3( t, cpx0, cpx1, cpx2, cpx ); var ty = THREE.Shape.Utils.b3( t, cpy0, cpy1, cpy2, cpy ); } } break; } } } return { offset: glyph.ha*scale, path:path}; } }; THREE.FontUtils.generateShapes = function( text, parameters ) { // Parameters parameters = parameters || {}; var size = parameters.size !== undefined ? parameters.size : 100; var curveSegments = parameters.curveSegments !== undefined ? parameters.curveSegments: 4; var font = parameters.font !== undefined ? parameters.font : "helvetiker"; var weight = parameters.weight !== undefined ? parameters.weight : "normal"; var style = parameters.style !== undefined ? parameters.style : "normal"; THREE.FontUtils.size = size; THREE.FontUtils.divisions = curveSegments; THREE.FontUtils.face = font; THREE.FontUtils.weight = weight; THREE.FontUtils.style = style; // Get a Font data json object var data = THREE.FontUtils.drawText( text ); var paths = data.paths; var shapes = []; for ( var p = 0, pl = paths.length; p < pl; p ++ ) { Array.prototype.push.apply( shapes, paths[ p ].toShapes() ); } return shapes; }; /** * This code is a quick port of code written in C++ which was submitted to * flipcode.com by John W. Ratcliff // July 22, 2000 * See original code and more information here: * http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml * * ported to actionscript by Zevan Rosser * www.actionsnippet.com * * ported to javascript by Joshua Koo * http://www.lab4games.net/zz85/blog * */ ( function( namespace ) { var EPSILON = 0.0000000001; // takes in an contour array and returns var process = function( contour, indices ) { var n = contour.length; if ( n < 3 ) return null; var result = [], verts = [], vertIndices = []; /* we want a counter-clockwise polygon in verts */ var u, v, w; if ( area( contour ) > 0.0 ) { for ( v = 0; v < n; v++ ) verts[ v ] = v; } else { for ( v = 0; v < n; v++ ) verts[ v ] = ( n - 1 ) - v; } var nv = n; /* remove nv - 2 vertices, creating 1 triangle every time */ var count = 2 * nv; /* error detection */ for( v = nv - 1; nv > 2; ) { /* if we loop, it is probably a non-simple polygon */ if ( ( count-- ) <= 0 ) { //** Triangulate: ERROR - probable bad polygon! //throw ( "Warning, unable to triangulate polygon!" ); //return null; // Sometimes warning is fine, especially polygons are triangulated in reverse. console.log( "Warning, unable to triangulate polygon!" ); if ( indices ) return vertIndices; return result; } /* three consecutive vertices in current polygon, <u,v,w> */ u = v; if ( nv <= u ) u = 0; /* previous */ v = u + 1; if ( nv <= v ) v = 0; /* new v */ w = v + 1; if ( nv <= w ) w = 0; /* next */ if ( snip( contour, u, v, w, nv, verts ) ) { var a, b, c, s, t; /* true names of the vertices */ a = verts[ u ]; b = verts[ v ]; c = verts[ w ]; /* output Triangle */ result.push( [ contour[ a ], contour[ b ], contour[ c ] ] ); vertIndices.push( [ verts[ u ], verts[ v ], verts[ w ] ] ); /* remove v from the remaining polygon */ for( s = v, t = v + 1; t < nv; s++, t++ ) { verts[ s ] = verts[ t ]; } nv--; /* reset error detection counter */ count = 2 * nv; } } if ( indices ) return vertIndices; return result; }; // calculate area of the contour polygon var area = function ( contour ) { var n = contour.length; var a = 0.0; for( var p = n - 1, q = 0; q < n; p = q++ ) { a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y; } return a * 0.5; }; var snip = function ( contour, u, v, w, n, verts ) { var p; var ax, ay, bx, by; var cx, cy, px, py; ax = contour[ verts[ u ] ].x; ay = contour[ verts[ u ] ].y; bx = contour[ verts[ v ] ].x; by = contour[ verts[ v ] ].y; cx = contour[ verts[ w ] ].x; cy = contour[ verts[ w ] ].y; if ( EPSILON > (((bx-ax)*(cy-ay)) - ((by-ay)*(cx-ax))) ) return false; var aX, aY, bX, bY, cX, cY; var apx, apy, bpx, bpy, cpx, cpy; var cCROSSap, bCROSScp, aCROSSbp; aX = cx - bx; aY = cy - by; bX = ax - cx; bY = ay - cy; cX = bx - ax; cY = by - ay; for ( p = 0; p < n; p++ ) { px = contour[ verts[ p ] ].x py = contour[ verts[ p ] ].y if ( ( (px === ax) && (py === ay) ) || ( (px === bx) && (py === by) ) || ( (px === cx) && (py === cy) ) ) continue; apx = px - ax; apy = py - ay; bpx = px - bx; bpy = py - by; cpx = px - cx; cpy = py - cy; // see if p is inside triangle abc aCROSSbp = aX*bpy - aY*bpx; cCROSSap = cX*apy - cY*apx; bCROSScp = bX*cpy - bY*cpx; if ( (aCROSSbp >= -EPSILON) && (bCROSScp >= -EPSILON) && (cCROSSap >= -EPSILON) ) return false; } return true; }; namespace.Triangulate = process; namespace.Triangulate.area = area; return namespace; })(THREE.FontUtils); // To use the typeface.js face files, hook up the API self._typeface_js = { faces: THREE.FontUtils.faces, loadFace: THREE.FontUtils.loadFace }; THREE.typeface_js = self._typeface_js; /** * @author zz85 / http://www.lab4games.net/zz85/blog * Extensible curve object * * Some common of Curve methods * .getPoint(t), getTangent(t) * .getPointAt(u), getTagentAt(u) * .getPoints(), .getSpacedPoints() * .getLength() * .updateArcLengths() * * This following classes subclasses THREE.Curve: * * -- 2d classes -- * THREE.LineCurve * THREE.QuadraticBezierCurve * THREE.CubicBezierCurve * THREE.SplineCurve * THREE.ArcCurve * THREE.EllipseCurve * * -- 3d classes -- * THREE.LineCurve3 * THREE.QuadraticBezierCurve3 * THREE.CubicBezierCurve3 * THREE.SplineCurve3 * THREE.ClosedSplineCurve3 * * A series of curves can be represented as a THREE.CurvePath * **/ /************************************************************** * Abstract Curve base class **************************************************************/ THREE.Curve = function () { }; // Virtual base class method to overwrite and implement in subclasses // - t [0 .. 1] THREE.Curve.prototype.getPoint = function ( t ) { console.log( "Warning, getPoint() not implemented!" ); return null; }; // Get point at relative position in curve according to arc length // - u [0 .. 1] THREE.Curve.prototype.getPointAt = function ( u ) { var t = this.getUtoTmapping( u ); return this.getPoint( t ); }; // Get sequence of points using getPoint( t ) THREE.Curve.prototype.getPoints = function ( divisions ) { if ( !divisions ) divisions = 5; var d, pts = []; for ( d = 0; d <= divisions; d ++ ) { pts.push( this.getPoint( d / divisions ) ); } return pts; }; // Get sequence of points using getPointAt( u ) THREE.Curve.prototype.getSpacedPoints = function ( divisions ) { if ( !divisions ) divisions = 5; var d, pts = []; for ( d = 0; d <= divisions; d ++ ) { pts.push( this.getPointAt( d / divisions ) ); } return pts; }; // Get total curve arc length THREE.Curve.prototype.getLength = function () { var lengths = this.getLengths(); return lengths[ lengths.length - 1 ]; }; // Get list of cumulative segment lengths THREE.Curve.prototype.getLengths = function ( divisions ) { if ( !divisions ) divisions = (this.__arcLengthDivisions) ? (this.__arcLengthDivisions): 200; if ( this.cacheArcLengths && ( this.cacheArcLengths.length == divisions + 1 ) && !this.needsUpdate) { //console.log( "cached", this.cacheArcLengths ); return this.cacheArcLengths; } this.needsUpdate = false; var cache = []; var current, last = this.getPoint( 0 ); var p, sum = 0; cache.push( 0 ); for ( p = 1; p <= divisions; p ++ ) { current = this.getPoint ( p / divisions ); sum += current.distanceTo( last ); cache.push( sum ); last = current; } this.cacheArcLengths = cache; return cache; // { sums: cache, sum:sum }; Sum is in the last element. }; THREE.Curve.prototype.updateArcLengths = function() { this.needsUpdate = true; this.getLengths(); }; // Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equi distance THREE.Curve.prototype.getUtoTmapping = function ( u, distance ) { var arcLengths = this.getLengths(); var i = 0, il = arcLengths.length; var targetArcLength; // The targeted u distance value to get if ( distance ) { targetArcLength = distance; } else { targetArcLength = u * arcLengths[ il - 1 ]; } //var time = Date.now(); // binary search for the index with largest value smaller than target u distance var low = 0, high = il - 1, comparison; while ( low <= high ) { i = Math.floor( low + ( high - low ) / 2 ); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats comparison = arcLengths[ i ] - targetArcLength; if ( comparison < 0 ) { low = i + 1; continue; } else if ( comparison > 0 ) { high = i - 1; continue; } else { high = i; break; // DONE } } i = high; //console.log('b' , i, low, high, Date.now()- time); if ( arcLengths[ i ] == targetArcLength ) { var t = i / ( il - 1 ); return t; } // we could get finer grain at lengths, or use simple interpolatation between two points var lengthBefore = arcLengths[ i ]; var lengthAfter = arcLengths[ i + 1 ]; var segmentLength = lengthAfter - lengthBefore; // determine where we are between the 'before' and 'after' points var segmentFraction = ( targetArcLength - lengthBefore ) / segmentLength; // add that fractional amount to t var t = ( i + segmentFraction ) / ( il -1 ); return t; }; // Returns a unit vector tangent at t // In case any sub curve does not implement its tangent derivation, // 2 points a small delta apart will be used to find its gradient // which seems to give a reasonable approximation THREE.Curve.prototype.getTangent = function( t ) { var delta = 0.0001; var t1 = t - delta; var t2 = t + delta; // Capping in case of danger if ( t1 < 0 ) t1 = 0; if ( t2 > 1 ) t2 = 1; var pt1 = this.getPoint( t1 ); var pt2 = this.getPoint( t2 ); var vec = pt2.clone().sub(pt1); return vec.normalize(); }; THREE.Curve.prototype.getTangentAt = function ( u ) { var t = this.getUtoTmapping( u ); return this.getTangent( t ); }; /************************************************************** * Utils **************************************************************/ THREE.Curve.Utils = { tangentQuadraticBezier: function ( t, p0, p1, p2 ) { return 2 * ( 1 - t ) * ( p1 - p0 ) + 2 * t * ( p2 - p1 ); }, // Puay Bing, thanks for helping with this derivative! tangentCubicBezier: function (t, p0, p1, p2, p3 ) { return -3 * p0 * (1 - t) * (1 - t) + 3 * p1 * (1 - t) * (1-t) - 6 *t *p1 * (1-t) + 6 * t * p2 * (1-t) - 3 * t * t * p2 + 3 * t * t * p3; }, tangentSpline: function ( t, p0, p1, p2, p3 ) { // To check if my formulas are correct var h00 = 6 * t * t - 6 * t; // derived from 2t^3 − 3t^2 + 1 var h10 = 3 * t * t - 4 * t + 1; // t^3 − 2t^2 + t var h01 = -6 * t * t + 6 * t; // − 2t3 + 3t2 var h11 = 3 * t * t - 2 * t; // t3 − t2 return h00 + h10 + h01 + h11; }, // Catmull-Rom interpolate: function( p0, p1, p2, p3, t ) { var v0 = ( p2 - p0 ) * 0.5; var v1 = ( p3 - p1 ) * 0.5; var t2 = t * t; var t3 = t * t2; return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1; } }; // TODO: Transformation for Curves? /************************************************************** * 3D Curves **************************************************************/ // A Factory method for creating new curve subclasses THREE.Curve.create = function ( constructor, getPointFunc ) { constructor.prototype = Object.create( THREE.Curve.prototype ); constructor.prototype.getPoint = getPointFunc; return constructor; }; /** * @author zz85 / http://www.lab4games.net/zz85/blog * **/ /************************************************************** * Curved Path - a curve path is simply a array of connected * curves, but retains the api of a curve **************************************************************/ THREE.CurvePath = function () { this.curves = []; this.bends = []; this.autoClose = false; // Automatically closes the path }; THREE.CurvePath.prototype = Object.create( THREE.Curve.prototype ); THREE.CurvePath.prototype.add = function ( curve ) { this.curves.push( curve ); }; THREE.CurvePath.prototype.checkConnection = function() { // TODO // If the ending of curve is not connected to the starting // or the next curve, then, this is not a real path }; THREE.CurvePath.prototype.closePath = function() { // TODO Test // and verify for vector3 (needs to implement equals) // Add a line curve if start and end of lines are not connected var startPoint = this.curves[0].getPoint(0); var endPoint = this.curves[this.curves.length-1].getPoint(1); if (!startPoint.equals(endPoint)) { this.curves.push( new THREE.LineCurve(endPoint, startPoint) ); } }; // To get accurate point with reference to // entire path distance at time t, // following has to be done: // 1. Length of each sub path have to be known // 2. Locate and identify type of curve // 3. Get t for the curve // 4. Return curve.getPointAt(t') THREE.CurvePath.prototype.getPoint = function( t ) { var d = t * this.getLength(); var curveLengths = this.getCurveLengths(); var i = 0, diff, curve; // To think about boundaries points. while ( i < curveLengths.length ) { if ( curveLengths[ i ] >= d ) { diff = curveLengths[ i ] - d; curve = this.curves[ i ]; var u = 1 - diff / curve.getLength(); return curve.getPointAt( u ); break; } i ++; } return null; // loop where sum != 0, sum > d , sum+1 <d }; /* THREE.CurvePath.prototype.getTangent = function( t ) { };*/ // We cannot use the default THREE.Curve getPoint() with getLength() because in // THREE.Curve, getLength() depends on getPoint() but in THREE.CurvePath // getPoint() depends on getLength THREE.CurvePath.prototype.getLength = function() { var lens = this.getCurveLengths(); return lens[ lens.length - 1 ]; }; // Compute lengths and cache them // We cannot overwrite getLengths() because UtoT mapping uses it. THREE.CurvePath.prototype.getCurveLengths = function() { // We use cache values if curves and cache array are same length if ( this.cacheLengths && this.cacheLengths.length == this.curves.length ) { return this.cacheLengths; }; // Get length of subsurve // Push sums into cached array var lengths = [], sums = 0; var i, il = this.curves.length; for ( i = 0; i < il; i ++ ) { sums += this.curves[ i ].getLength(); lengths.push( sums ); } this.cacheLengths = lengths; return lengths; }; // Returns min and max coordinates, as well as centroid THREE.CurvePath.prototype.getBoundingBox = function () { var points = this.getPoints(); var maxX, maxY, maxZ; var minX, minY, minZ; maxX = maxY = Number.NEGATIVE_INFINITY; minX = minY = Number.POSITIVE_INFINITY; var p, i, il, sum; var v3 = points[0] instanceof THREE.Vector3; sum = v3 ? new THREE.Vector3() : new THREE.Vector2(); for ( i = 0, il = points.length; i < il; i ++ ) { p = points[ i ]; if ( p.x > maxX ) maxX = p.x; else if ( p.x < minX ) minX = p.x; if ( p.y > maxY ) maxY = p.y; else if ( p.y < minY ) minY = p.y; if ( v3 ) { if ( p.z > maxZ ) maxZ = p.z; else if ( p.z < minZ ) minZ = p.z; } sum.add( p ); } var ret = { minX: minX, minY: minY, maxX: maxX, maxY: maxY, centroid: sum.divideScalar( il ) }; if ( v3 ) { ret.maxZ = maxZ; ret.minZ = minZ; } return ret; }; /************************************************************** * Create Geometries Helpers **************************************************************/ /// Generate geometry from path points (for Line or ParticleSystem objects) THREE.CurvePath.prototype.createPointsGeometry = function( divisions ) { var pts = this.getPoints( divisions, true ); return this.createGeometry( pts ); }; // Generate geometry from equidistance sampling along the path THREE.CurvePath.prototype.createSpacedPointsGeometry = function( divisions ) { var pts = this.getSpacedPoints( divisions, true ); return this.createGeometry( pts ); }; THREE.CurvePath.prototype.createGeometry = function( points ) { var geometry = new THREE.Geometry(); for ( var i = 0; i < points.length; i ++ ) { geometry.vertices.push( new THREE.Vector3( points[ i ].x, points[ i ].y, points[ i ].z || 0) ); } return geometry; }; /************************************************************** * Bend / Wrap Helper Methods **************************************************************/ // Wrap path / Bend modifiers? THREE.CurvePath.prototype.addWrapPath = function ( bendpath ) { this.bends.push( bendpath ); }; THREE.CurvePath.prototype.getTransformedPoints = function( segments, bends ) { var oldPts = this.getPoints( segments ); // getPoints getSpacedPoints var i, il; if ( !bends ) { bends = this.bends; } for ( i = 0, il = bends.length; i < il; i ++ ) { oldPts = this.getWrapPoints( oldPts, bends[ i ] ); } return oldPts; }; THREE.CurvePath.prototype.getTransformedSpacedPoints = function( segments, bends ) { var oldPts = this.getSpacedPoints( segments ); var i, il; if ( !bends ) { bends = this.bends; } for ( i = 0, il = bends.length; i < il; i ++ ) { oldPts = this.getWrapPoints( oldPts, bends[ i ] ); } return oldPts; }; // This returns getPoints() bend/wrapped around the contour of a path. // Read http://www.planetclegg.com/projects/WarpingTextToSplines.html THREE.CurvePath.prototype.getWrapPoints = function ( oldPts, path ) { var bounds = this.getBoundingBox(); var i, il, p, oldX, oldY, xNorm; for ( i = 0, il = oldPts.length; i < il; i ++ ) { p = oldPts[ i ]; oldX = p.x; oldY = p.y; xNorm = oldX / bounds.maxX; // If using actual distance, for length > path, requires line extrusions //xNorm = path.getUtoTmapping(xNorm, oldX); // 3 styles. 1) wrap stretched. 2) wrap stretch by arc length 3) warp by actual distance xNorm = path.getUtoTmapping( xNorm, oldX ); // check for out of bounds? var pathPt = path.getPoint( xNorm ); var normal = path.getTangent( xNorm ); normal.set( -normal.y, normal.x ).multiplyScalar( oldY ); p.x = pathPt.x + normal.x; p.y = pathPt.y + normal.y; } return oldPts; }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.Gyroscope = function () { THREE.Object3D.call( this ); }; THREE.Gyroscope.prototype = Object.create( THREE.Object3D.prototype ); THREE.Gyroscope.prototype.updateMatrixWorld = function ( force ) { this.matrixAutoUpdate && this.updateMatrix(); // update matrixWorld if ( this.matrixWorldNeedsUpdate || force ) { if ( this.parent ) { this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix ); this.matrixWorld.decompose( this.translationWorld, this.quaternionWorld, this.scaleWorld ); this.matrix.decompose( this.translationObject, this.quaternionObject, this.scaleObject ); this.matrixWorld.compose( this.translationWorld, this.quaternionObject, this.scaleWorld ); } else { this.matrixWorld.copy( this.matrix ); } this.matrixWorldNeedsUpdate = false; force = true; } // update children for ( var i = 0, l = this.children.length; i < l; i ++ ) { this.children[ i ].updateMatrixWorld( force ); } }; THREE.Gyroscope.prototype.translationWorld = new THREE.Vector3(); THREE.Gyroscope.prototype.translationObject = new THREE.Vector3(); THREE.Gyroscope.prototype.quaternionWorld = new THREE.Quaternion(); THREE.Gyroscope.prototype.quaternionObject = new THREE.Quaternion(); THREE.Gyroscope.prototype.scaleWorld = new THREE.Vector3(); THREE.Gyroscope.prototype.scaleObject = new THREE.Vector3(); /** * @author zz85 / http://www.lab4games.net/zz85/blog * Creates free form 2d path using series of points, lines or curves. * **/ THREE.Path = function ( points ) { THREE.CurvePath.call(this); this.actions = []; if ( points ) { this.fromPoints( points ); } }; THREE.Path.prototype = Object.create( THREE.CurvePath.prototype ); THREE.PathActions = { MOVE_TO: 'moveTo', LINE_TO: 'lineTo', QUADRATIC_CURVE_TO: 'quadraticCurveTo', // Bezier quadratic curve BEZIER_CURVE_TO: 'bezierCurveTo', // Bezier cubic curve CSPLINE_THRU: 'splineThru', // Catmull-rom spline ARC: 'arc', // Circle ELLIPSE: 'ellipse' }; // TODO Clean up PATH API // Create path using straight lines to connect all points // - vectors: array of Vector2 THREE.Path.prototype.fromPoints = function ( vectors ) { this.moveTo( vectors[ 0 ].x, vectors[ 0 ].y ); for ( var v = 1, vlen = vectors.length; v < vlen; v ++ ) { this.lineTo( vectors[ v ].x, vectors[ v ].y ); }; }; // startPath() endPath()? THREE.Path.prototype.moveTo = function ( x, y ) { var args = Array.prototype.slice.call( arguments ); this.actions.push( { action: THREE.PathActions.MOVE_TO, args: args } ); }; THREE.Path.prototype.lineTo = function ( x, y ) { var args = Array.prototype.slice.call( arguments ); var lastargs = this.actions[ this.actions.length - 1 ].args; var x0 = lastargs[ lastargs.length - 2 ]; var y0 = lastargs[ lastargs.length - 1 ]; var curve = new THREE.LineCurve( new THREE.Vector2( x0, y0 ), new THREE.Vector2( x, y ) ); this.curves.push( curve ); this.actions.push( { action: THREE.PathActions.LINE_TO, args: args } ); }; THREE.Path.prototype.quadraticCurveTo = function( aCPx, aCPy, aX, aY ) { var args = Array.prototype.slice.call( arguments ); var lastargs = this.actions[ this.actions.length - 1 ].args; var x0 = lastargs[ lastargs.length - 2 ]; var y0 = lastargs[ lastargs.length - 1 ]; var curve = new THREE.QuadraticBezierCurve( new THREE.Vector2( x0, y0 ), new THREE.Vector2( aCPx, aCPy ), new THREE.Vector2( aX, aY ) ); this.curves.push( curve ); this.actions.push( { action: THREE.PathActions.QUADRATIC_CURVE_TO, args: args } ); }; THREE.Path.prototype.bezierCurveTo = function( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) { var args = Array.prototype.slice.call( arguments ); var lastargs = this.actions[ this.actions.length - 1 ].args; var x0 = lastargs[ lastargs.length - 2 ]; var y0 = lastargs[ lastargs.length - 1 ]; var curve = new THREE.CubicBezierCurve( new THREE.Vector2( x0, y0 ), new THREE.Vector2( aCP1x, aCP1y ), new THREE.Vector2( aCP2x, aCP2y ), new THREE.Vector2( aX, aY ) ); this.curves.push( curve ); this.actions.push( { action: THREE.PathActions.BEZIER_CURVE_TO, args: args } ); }; THREE.Path.prototype.splineThru = function( pts /*Array of Vector*/ ) { var args = Array.prototype.slice.call( arguments ); var lastargs = this.actions[ this.actions.length - 1 ].args; var x0 = lastargs[ lastargs.length - 2 ]; var y0 = lastargs[ lastargs.length - 1 ]; //--- var npts = [ new THREE.Vector2( x0, y0 ) ]; Array.prototype.push.apply( npts, pts ); var curve = new THREE.SplineCurve( npts ); this.curves.push( curve ); this.actions.push( { action: THREE.PathActions.CSPLINE_THRU, args: args } ); }; // FUTURE: Change the API or follow canvas API? THREE.Path.prototype.arc = function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) { var lastargs = this.actions[ this.actions.length - 1].args; var x0 = lastargs[ lastargs.length - 2 ]; var y0 = lastargs[ lastargs.length - 1 ]; this.absarc(aX + x0, aY + y0, aRadius, aStartAngle, aEndAngle, aClockwise ); }; THREE.Path.prototype.absarc = function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) { this.absellipse(aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise); }; THREE.Path.prototype.ellipse = function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise ) { var lastargs = this.actions[ this.actions.length - 1].args; var x0 = lastargs[ lastargs.length - 2 ]; var y0 = lastargs[ lastargs.length - 1 ]; this.absellipse(aX + x0, aY + y0, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise ); }; THREE.Path.prototype.absellipse = function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise ) { var args = Array.prototype.slice.call( arguments ); var curve = new THREE.EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise ); this.curves.push( curve ); var lastPoint = curve.getPoint(1); args.push(lastPoint.x); args.push(lastPoint.y); this.actions.push( { action: THREE.PathActions.ELLIPSE, args: args } ); }; THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) { if ( ! divisions ) divisions = 40; var points = []; for ( var i = 0; i < divisions; i ++ ) { points.push( this.getPoint( i / divisions ) ); //if( !this.getPoint( i / divisions ) ) throw "DIE"; } // if ( closedPath ) { // // points.push( points[ 0 ] ); // // } return points; }; /* Return an array of vectors based on contour of the path */ THREE.Path.prototype.getPoints = function( divisions, closedPath ) { if (this.useSpacedPoints) { console.log('tata'); return this.getSpacedPoints( divisions, closedPath ); } divisions = divisions || 12; var points = []; var i, il, item, action, args; var cpx, cpy, cpx2, cpy2, cpx1, cpy1, cpx0, cpy0, laste, j, t, tx, ty; for ( i = 0, il = this.actions.length; i < il; i ++ ) { item = this.actions[ i ]; action = item.action; args = item.args; switch( action ) { case THREE.PathActions.MOVE_TO: points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) ); break; case THREE.PathActions.LINE_TO: points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) ); break; case THREE.PathActions.QUADRATIC_CURVE_TO: cpx = args[ 2 ]; cpy = args[ 3 ]; cpx1 = args[ 0 ]; cpy1 = args[ 1 ]; if ( points.length > 0 ) { laste = points[ points.length - 1 ]; cpx0 = laste.x; cpy0 = laste.y; } else { laste = this.actions[ i - 1 ].args; cpx0 = laste[ laste.length - 2 ]; cpy0 = laste[ laste.length - 1 ]; } for ( j = 1; j <= divisions; j ++ ) { t = j / divisions; tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx ); ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy ); points.push( new THREE.Vector2( tx, ty ) ); } break; case THREE.PathActions.BEZIER_CURVE_TO: cpx = args[ 4 ]; cpy = args[ 5 ]; cpx1 = args[ 0 ]; cpy1 = args[ 1 ]; cpx2 = args[ 2 ]; cpy2 = args[ 3 ]; if ( points.length > 0 ) { laste = points[ points.length - 1 ]; cpx0 = laste.x; cpy0 = laste.y; } else { laste = this.actions[ i - 1 ].args; cpx0 = laste[ laste.length - 2 ]; cpy0 = laste[ laste.length - 1 ]; } for ( j = 1; j <= divisions; j ++ ) { t = j / divisions; tx = THREE.Shape.Utils.b3( t, cpx0, cpx1, cpx2, cpx ); ty = THREE.Shape.Utils.b3( t, cpy0, cpy1, cpy2, cpy ); points.push( new THREE.Vector2( tx, ty ) ); } break; case THREE.PathActions.CSPLINE_THRU: laste = this.actions[ i - 1 ].args; var last = new THREE.Vector2( laste[ laste.length - 2 ], laste[ laste.length - 1 ] ); var spts = [ last ]; var n = divisions * args[ 0 ].length; spts = spts.concat( args[ 0 ] ); var spline = new THREE.SplineCurve( spts ); for ( j = 1; j <= n; j ++ ) { points.push( spline.getPointAt( j / n ) ) ; } break; case THREE.PathActions.ARC: var aX = args[ 0 ], aY = args[ 1 ], aRadius = args[ 2 ], aStartAngle = args[ 3 ], aEndAngle = args[ 4 ], aClockwise = !!args[ 5 ]; var deltaAngle = aEndAngle - aStartAngle; var angle; var tdivisions = divisions * 2; for ( j = 1; j <= tdivisions; j ++ ) { t = j / tdivisions; if ( ! aClockwise ) { t = 1 - t; } angle = aStartAngle + t * deltaAngle; tx = aX + aRadius * Math.cos( angle ); ty = aY + aRadius * Math.sin( angle ); //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty); points.push( new THREE.Vector2( tx, ty ) ); } //console.log(points); break; case THREE.PathActions.ELLIPSE: var aX = args[ 0 ], aY = args[ 1 ], xRadius = args[ 2 ], yRadius = args[ 3 ], aStartAngle = args[ 4 ], aEndAngle = args[ 5 ], aClockwise = !!args[ 6 ]; var deltaAngle = aEndAngle - aStartAngle; var angle; var tdivisions = divisions * 2; for ( j = 1; j <= tdivisions; j ++ ) { t = j / tdivisions; if ( ! aClockwise ) { t = 1 - t; } angle = aStartAngle + t * deltaAngle; tx = aX + xRadius * Math.cos( angle ); ty = aY + yRadius * Math.sin( angle ); //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty); points.push( new THREE.Vector2( tx, ty ) ); } //console.log(points); break; } // end switch } // Normalize to remove the closing point by default. var lastPoint = points[ points.length - 1]; var EPSILON = 0.0000000001; if ( Math.abs(lastPoint.x - points[ 0 ].x) < EPSILON && Math.abs(lastPoint.y - points[ 0 ].y) < EPSILON) points.splice( points.length - 1, 1); if ( closedPath ) { points.push( points[ 0 ] ); } return points; }; // Breaks path into shapes THREE.Path.prototype.toShapes = function( isCCW ) { function isPointInsidePolygon( inPt, inPolygon ) { var EPSILON = 0.0000000001; var polyLen = inPolygon.length; // inPt on polygon contour => immediate success or // toggling of inside/outside at every single! intersection point of an edge // with the horizontal line through inPt, left of inPt // not counting lowerY endpoints of edges and whole edges on that line var inside = false; for( var p = polyLen - 1, q = 0; q < polyLen; p = q++ ) { var edgeLowPt = inPolygon[ p ]; var edgeHighPt = inPolygon[ q ]; var edgeDx = edgeHighPt.x - edgeLowPt.x; var edgeDy = edgeHighPt.y - edgeLowPt.y; if ( Math.abs(edgeDy) > EPSILON ) { // not parallel if ( edgeDy < 0 ) { edgeLowPt = inPolygon[ q ]; edgeDx = -edgeDx; edgeHighPt = inPolygon[ p ]; edgeDy = -edgeDy; } if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue; if ( inPt.y == edgeLowPt.y ) { if ( inPt.x == edgeLowPt.x ) return true; // inPt is on contour ? // continue; // no intersection or edgeLowPt => doesn't count !!! } else { var perpEdge = edgeDy * (inPt.x - edgeLowPt.x) - edgeDx * (inPt.y - edgeLowPt.y); if ( perpEdge == 0 ) return true; // inPt is on contour ? if ( perpEdge < 0 ) continue; inside = !inside; // true intersection left of inPt } } else { // parallel or colinear if ( inPt.y != edgeLowPt.y ) continue; // parallel // egde lies on the same horizontal line as inPt if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) || ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour ! // continue; } } return inside; } var i, il, item, action, args; var subPaths = [], lastPath = new THREE.Path(); for ( i = 0, il = this.actions.length; i < il; i ++ ) { item = this.actions[ i ]; args = item.args; action = item.action; if ( action == THREE.PathActions.MOVE_TO ) { if ( lastPath.actions.length != 0 ) { subPaths.push( lastPath ); lastPath = new THREE.Path(); } } lastPath[ action ].apply( lastPath, args ); } if ( lastPath.actions.length != 0 ) { subPaths.push( lastPath ); } // console.log(subPaths); if ( subPaths.length == 0 ) return []; var solid, tmpPath, tmpShape, shapes = []; if ( subPaths.length == 1) { tmpPath = subPaths[0]; tmpShape = new THREE.Shape(); tmpShape.actions = tmpPath.actions; tmpShape.curves = tmpPath.curves; shapes.push( tmpShape ); return shapes; } var holesFirst = !THREE.Shape.Utils.isClockWise( subPaths[ 0 ].getPoints() ); holesFirst = isCCW ? !holesFirst : holesFirst; // console.log("Holes first", holesFirst); var betterShapeHoles = []; var newShapes = []; var newShapeHoles = []; var mainIdx = 0; var tmpPoints; newShapes[mainIdx] = undefined; newShapeHoles[mainIdx] = []; for ( i = 0, il = subPaths.length; i < il; i ++ ) { tmpPath = subPaths[ i ]; tmpPoints = tmpPath.getPoints(); solid = THREE.Shape.Utils.isClockWise( tmpPoints ); solid = isCCW ? !solid : solid; if ( solid ) { if ( (! holesFirst ) && ( newShapes[mainIdx] ) ) mainIdx++; newShapes[mainIdx] = { s: new THREE.Shape(), p: tmpPoints }; newShapes[mainIdx].s.actions = tmpPath.actions; newShapes[mainIdx].s.curves = tmpPath.curves; if ( holesFirst ) mainIdx++; newShapeHoles[mainIdx] = []; //console.log('cw', i); } else { newShapeHoles[mainIdx].push( { h: tmpPath, p: tmpPoints[0] } ); //console.log('ccw', i); } } if ( newShapes.length > 1 ) { var ambigious = false; var toChange = []; for (var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx++ ) { betterShapeHoles[sIdx] = []; } for (var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx++ ) { var sh = newShapes[sIdx]; var sho = newShapeHoles[sIdx]; for (var hIdx = 0; hIdx < sho.length; hIdx++ ) { var ho = sho[hIdx]; var hole_unassigned = true; for (var s2Idx = 0; s2Idx < newShapes.length; s2Idx++ ) { if ( isPointInsidePolygon( ho.p, newShapes[s2Idx].p ) ) { if ( sIdx != s2Idx ) toChange.push( { froms: sIdx, tos: s2Idx, hole: hIdx } ); if ( hole_unassigned ) { hole_unassigned = false; betterShapeHoles[s2Idx].push( ho ); } else { ambigious = true; } } } if ( hole_unassigned ) { betterShapeHoles[sIdx].push( ho ); } } } // console.log("ambigious: ", ambigious); if ( toChange.length > 0 ) { // console.log("to change: ", toChange); if (! ambigious) newShapeHoles = betterShapeHoles; } } var tmpHoles, j, jl; for ( i = 0, il = newShapes.length; i < il; i ++ ) { tmpShape = newShapes[i].s; shapes.push( tmpShape ); tmpHoles = newShapeHoles[i]; for ( j = 0, jl = tmpHoles.length; j < jl; j ++ ) { tmpShape.holes.push( tmpHoles[j].h ); } } //console.log("shape", shapes); return shapes; }; /** * @author zz85 / http://www.lab4games.net/zz85/blog * Defines a 2d shape plane using paths. **/ // STEP 1 Create a path. // STEP 2 Turn path into shape. // STEP 3 ExtrudeGeometry takes in Shape/Shapes // STEP 3a - Extract points from each shape, turn to vertices // STEP 3b - Triangulate each shape, add faces. THREE.Shape = function () { THREE.Path.apply( this, arguments ); this.holes = []; }; THREE.Shape.prototype = Object.create( THREE.Path.prototype ); // Convenience method to return ExtrudeGeometry THREE.Shape.prototype.extrude = function ( options ) { var extruded = new THREE.ExtrudeGeometry( this, options ); return extruded; }; // Convenience method to return ShapeGeometry THREE.Shape.prototype.makeGeometry = function ( options ) { var geometry = new THREE.ShapeGeometry( this, options ); return geometry; }; // Get points of holes THREE.Shape.prototype.getPointsHoles = function ( divisions ) { var i, il = this.holes.length, holesPts = []; for ( i = 0; i < il; i ++ ) { holesPts[ i ] = this.holes[ i ].getTransformedPoints( divisions, this.bends ); } return holesPts; }; // Get points of holes (spaced by regular distance) THREE.Shape.prototype.getSpacedPointsHoles = function ( divisions ) { var i, il = this.holes.length, holesPts = []; for ( i = 0; i < il; i ++ ) { holesPts[ i ] = this.holes[ i ].getTransformedSpacedPoints( divisions, this.bends ); } return holesPts; }; // Get points of shape and holes (keypoints based on segments parameter) THREE.Shape.prototype.extractAllPoints = function ( divisions ) { return { shape: this.getTransformedPoints( divisions ), holes: this.getPointsHoles( divisions ) }; }; THREE.Shape.prototype.extractPoints = function ( divisions ) { if (this.useSpacedPoints) { return this.extractAllSpacedPoints(divisions); } return this.extractAllPoints(divisions); }; // // THREE.Shape.prototype.extractAllPointsWithBend = function ( divisions, bend ) { // // return { // // shape: this.transform( bend, divisions ), // holes: this.getPointsHoles( divisions, bend ) // // }; // // }; // Get points of shape and holes (spaced by regular distance) THREE.Shape.prototype.extractAllSpacedPoints = function ( divisions ) { return { shape: this.getTransformedSpacedPoints( divisions ), holes: this.getSpacedPointsHoles( divisions ) }; }; /************************************************************** * Utils **************************************************************/ THREE.Shape.Utils = { triangulateShape: function ( contour, holes ) { function point_in_segment_2D_colin( inSegPt1, inSegPt2, inOtherPt ) { // inOtherPt needs to be colinear to the inSegment if ( inSegPt1.x != inSegPt2.x ) { if ( inSegPt1.x < inSegPt2.x ) { return ( ( inSegPt1.x <= inOtherPt.x ) && ( inOtherPt.x <= inSegPt2.x ) ); } else { return ( ( inSegPt2.x <= inOtherPt.x ) && ( inOtherPt.x <= inSegPt1.x ) ); } } else { if ( inSegPt1.y < inSegPt2.y ) { return ( ( inSegPt1.y <= inOtherPt.y ) && ( inOtherPt.y <= inSegPt2.y ) ); } else { return ( ( inSegPt2.y <= inOtherPt.y ) && ( inOtherPt.y <= inSegPt1.y ) ); } } } function intersect_segments_2D( inSeg1Pt1, inSeg1Pt2, inSeg2Pt1, inSeg2Pt2, inExcludeAdjacentSegs ) { var EPSILON = 0.0000000001; var seg1dx = inSeg1Pt2.x - inSeg1Pt1.x, seg1dy = inSeg1Pt2.y - inSeg1Pt1.y; var seg2dx = inSeg2Pt2.x - inSeg2Pt1.x, seg2dy = inSeg2Pt2.y - inSeg2Pt1.y; var seg1seg2dx = inSeg1Pt1.x - inSeg2Pt1.x; var seg1seg2dy = inSeg1Pt1.y - inSeg2Pt1.y; var limit = seg1dy * seg2dx - seg1dx * seg2dy; var perpSeg1 = seg1dy * seg1seg2dx - seg1dx * seg1seg2dy; if ( Math.abs(limit) > EPSILON ) { // not parallel var perpSeg2; if ( limit > 0 ) { if ( ( perpSeg1 < 0 ) || ( perpSeg1 > limit ) ) return []; perpSeg2 = seg2dy * seg1seg2dx - seg2dx * seg1seg2dy; if ( ( perpSeg2 < 0 ) || ( perpSeg2 > limit ) ) return []; } else { if ( ( perpSeg1 > 0 ) || ( perpSeg1 < limit ) ) return []; perpSeg2 = seg2dy * seg1seg2dx - seg2dx * seg1seg2dy; if ( ( perpSeg2 > 0 ) || ( perpSeg2 < limit ) ) return []; } // i.e. to reduce rounding errors // intersection at endpoint of segment#1? if ( perpSeg2 == 0 ) { if ( ( inExcludeAdjacentSegs ) && ( ( perpSeg1 == 0 ) || ( perpSeg1 == limit ) ) ) return []; return [ inSeg1Pt1 ]; } if ( perpSeg2 == limit ) { if ( ( inExcludeAdjacentSegs ) && ( ( perpSeg1 == 0 ) || ( perpSeg1 == limit ) ) ) return []; return [ inSeg1Pt2 ]; } // intersection at endpoint of segment#2? if ( perpSeg1 == 0 ) return [ inSeg2Pt1 ]; if ( perpSeg1 == limit ) return [ inSeg2Pt2 ]; // return real intersection point var factorSeg1 = perpSeg2 / limit; return [ { x: inSeg1Pt1.x + factorSeg1 * seg1dx, y: inSeg1Pt1.y + factorSeg1 * seg1dy } ]; } else { // parallel or colinear if ( ( perpSeg1 != 0 ) || ( seg2dy * seg1seg2dx != seg2dx * seg1seg2dy ) ) return []; // they are collinear or degenerate var seg1Pt = ( (seg1dx == 0) && (seg1dy == 0) ); // segment1 ist just a point? var seg2Pt = ( (seg2dx == 0) && (seg2dy == 0) ); // segment2 ist just a point? // both segments are points if ( seg1Pt && seg2Pt ) { if ( (inSeg1Pt1.x != inSeg2Pt1.x) || (inSeg1Pt1.y != inSeg2Pt1.y) ) return []; // they are distinct points return [ inSeg1Pt1 ]; // they are the same point } // segment#1 is a single point if ( seg1Pt ) { if (! point_in_segment_2D_colin( inSeg2Pt1, inSeg2Pt2, inSeg1Pt1 ) ) return []; // but not in segment#2 return [ inSeg1Pt1 ]; } // segment#2 is a single point if ( seg2Pt ) { if (! point_in_segment_2D_colin( inSeg1Pt1, inSeg1Pt2, inSeg2Pt1 ) ) return []; // but not in segment#1 return [ inSeg2Pt1 ]; } // they are collinear segments, which might overlap var seg1min, seg1max, seg1minVal, seg1maxVal; var seg2min, seg2max, seg2minVal, seg2maxVal; if (seg1dx != 0) { // the segments are NOT on a vertical line if ( inSeg1Pt1.x < inSeg1Pt2.x ) { seg1min = inSeg1Pt1; seg1minVal = inSeg1Pt1.x; seg1max = inSeg1Pt2; seg1maxVal = inSeg1Pt2.x; } else { seg1min = inSeg1Pt2; seg1minVal = inSeg1Pt2.x; seg1max = inSeg1Pt1; seg1maxVal = inSeg1Pt1.x; } if ( inSeg2Pt1.x < inSeg2Pt2.x ) { seg2min = inSeg2Pt1; seg2minVal = inSeg2Pt1.x; seg2max = inSeg2Pt2; seg2maxVal = inSeg2Pt2.x; } else { seg2min = inSeg2Pt2; seg2minVal = inSeg2Pt2.x; seg2max = inSeg2Pt1; seg2maxVal = inSeg2Pt1.x; } } else { // the segments are on a vertical line if ( inSeg1Pt1.y < inSeg1Pt2.y ) { seg1min = inSeg1Pt1; seg1minVal = inSeg1Pt1.y; seg1max = inSeg1Pt2; seg1maxVal = inSeg1Pt2.y; } else { seg1min = inSeg1Pt2; seg1minVal = inSeg1Pt2.y; seg1max = inSeg1Pt1; seg1maxVal = inSeg1Pt1.y; } if ( inSeg2Pt1.y < inSeg2Pt2.y ) { seg2min = inSeg2Pt1; seg2minVal = inSeg2Pt1.y; seg2max = inSeg2Pt2; seg2maxVal = inSeg2Pt2.y; } else { seg2min = inSeg2Pt2; seg2minVal = inSeg2Pt2.y; seg2max = inSeg2Pt1; seg2maxVal = inSeg2Pt1.y; } } if ( seg1minVal <= seg2minVal ) { if ( seg1maxVal < seg2minVal ) return []; if ( seg1maxVal == seg2minVal ) { if ( inExcludeAdjacentSegs ) return []; return [ seg2min ]; } if ( seg1maxVal <= seg2maxVal ) return [ seg2min, seg1max ]; return [ seg2min, seg2max ]; } else { if ( seg1minVal > seg2maxVal ) return []; if ( seg1minVal == seg2maxVal ) { if ( inExcludeAdjacentSegs ) return []; return [ seg1min ]; } if ( seg1maxVal <= seg2maxVal ) return [ seg1min, seg1max ]; return [ seg1min, seg2max ]; } } } function isPointInsideAngle( inVertex, inLegFromPt, inLegToPt, inOtherPt ) { // The order of legs is important var EPSILON = 0.0000000001; // translation of all points, so that Vertex is at (0,0) var legFromPtX = inLegFromPt.x - inVertex.x, legFromPtY = inLegFromPt.y - inVertex.y; var legToPtX = inLegToPt.x - inVertex.x, legToPtY = inLegToPt.y - inVertex.y; var otherPtX = inOtherPt.x - inVertex.x, otherPtY = inOtherPt.y - inVertex.y; // main angle >0: < 180 deg.; 0: 180 deg.; <0: > 180 deg. var from2toAngle = legFromPtX * legToPtY - legFromPtY * legToPtX; var from2otherAngle = legFromPtX * otherPtY - legFromPtY * otherPtX; if ( Math.abs(from2toAngle) > EPSILON ) { // angle != 180 deg. var other2toAngle = otherPtX * legToPtY - otherPtY * legToPtX; // console.log( "from2to: " + from2toAngle + ", from2other: " + from2otherAngle + ", other2to: " + other2toAngle ); if ( from2toAngle > 0 ) { // main angle < 180 deg. return ( ( from2otherAngle >= 0 ) && ( other2toAngle >= 0 ) ); } else { // main angle > 180 deg. return ( ( from2otherAngle >= 0 ) || ( other2toAngle >= 0 ) ); } } else { // angle == 180 deg. // console.log( "from2to: 180 deg., from2other: " + from2otherAngle ); return ( from2otherAngle > 0 ); } } function removeHoles( contour, holes ) { var shape = contour.concat(); // work on this shape var hole; function isCutLineInsideAngles( inShapeIdx, inHoleIdx ) { // Check if hole point lies within angle around shape point var lastShapeIdx = shape.length - 1; var prevShapeIdx = inShapeIdx - 1; if ( prevShapeIdx < 0 ) prevShapeIdx = lastShapeIdx; var nextShapeIdx = inShapeIdx + 1; if ( nextShapeIdx > lastShapeIdx ) nextShapeIdx = 0; var insideAngle = isPointInsideAngle( shape[inShapeIdx], shape[ prevShapeIdx ], shape[ nextShapeIdx ], hole[inHoleIdx] ); if (! insideAngle ) { // console.log( "Vertex (Shape): " + inShapeIdx + ", Point: " + hole[inHoleIdx].x + "/" + hole[inHoleIdx].y ); return false; } // Check if shape point lies within angle around hole point var lastHoleIdx = hole.length - 1; var prevHoleIdx = inHoleIdx - 1; if ( prevHoleIdx < 0 ) prevHoleIdx = lastHoleIdx; var nextHoleIdx = inHoleIdx + 1; if ( nextHoleIdx > lastHoleIdx ) nextHoleIdx = 0; insideAngle = isPointInsideAngle( hole[inHoleIdx], hole[ prevHoleIdx ], hole[ nextHoleIdx ], shape[inShapeIdx] ); if (! insideAngle ) { // console.log( "Vertex (Hole): " + inHoleIdx + ", Point: " + shape[inShapeIdx].x + "/" + shape[inShapeIdx].y ); return false; } return true; } function intersectsShapeEdge( inShapePt, inHolePt ) { // checks for intersections with shape edges var sIdx, nextIdx, intersection; for ( sIdx = 0; sIdx < shape.length; sIdx++ ) { nextIdx = sIdx+1; nextIdx %= shape.length; intersection = intersect_segments_2D( inShapePt, inHolePt, shape[sIdx], shape[nextIdx], true ); if ( intersection.length > 0 ) return true; } return false; } var indepHoles = []; function intersectsHoleEdge( inShapePt, inHolePt ) { // checks for intersections with hole edges var ihIdx, chkHole, hIdx, nextIdx, intersection; for ( ihIdx = 0; ihIdx < indepHoles.length; ihIdx++ ) { chkHole = holes[indepHoles[ihIdx]]; for ( hIdx = 0; hIdx < chkHole.length; hIdx++ ) { nextIdx = hIdx+1; nextIdx %= chkHole.length; intersection = intersect_segments_2D( inShapePt, inHolePt, chkHole[hIdx], chkHole[nextIdx], true ); if ( intersection.length > 0 ) return true; } } return false; } var holeIndex, shapeIndex, shapePt, h, h2, holePt, holeIdx, cutKey, failedCuts = [], tmpShape1, tmpShape2, tmpHole1, tmpHole2; for (h in holes) { indepHoles.push( h ); } var counter = indepHoles.length * 2; while ( indepHoles.length > 0 ) { counter --; if ( counter < 0 ) { console.log( "Infinite Loop! Holes left:" + indepHoles.length + ", Probably Hole outside Shape!" ); break; } // search for shape-vertex and hole-vertex, // which can be connected without intersections for ( shapeIndex = 0; shapeIndex < shape.length; shapeIndex++ ) { shapePt = shape[ shapeIndex ]; holeIndex = -1; // search for hole which can be reached without intersections for ( h = 0; h < indepHoles.length; h++ ) { holeIdx = indepHoles[h]; // prevent multiple checks cutKey = shapePt.x + ":" + shapePt.y + ":" + holeIdx; if ( failedCuts[cutKey] !== undefined ) continue; hole = holes[holeIdx]; for ( h2 = 0; h2 < hole.length; h2 ++ ) { holePt = hole[ h2 ]; if (! isCutLineInsideAngles( shapeIndex, h2 ) ) continue; if ( intersectsShapeEdge( shapePt, holePt ) ) continue; if ( intersectsHoleEdge( shapePt, holePt ) ) continue; holeIndex = h2; indepHoles.splice(h,1); tmpShape1 = shape.slice( 0, shapeIndex+1 ); tmpShape2 = shape.slice( shapeIndex ); tmpHole1 = hole.slice( holeIndex ); tmpHole2 = hole.slice( 0, holeIndex+1 ); shape = tmpShape1.concat( tmpHole1 ).concat( tmpHole2 ).concat( tmpShape2 ); // Debug only, to show the selected cuts // glob_CutLines.push( [ shapePt, holePt ] ); break; } if ( holeIndex >= 0 ) break; // hole-vertex found failedCuts[cutKey] = true; // remember failure } if ( holeIndex >= 0 ) break; // hole-vertex found } } return shape; /* shape with no holes */ } var i, il, f, face, key, index, allPointsMap = {}; // To maintain reference to old shape, one must match coordinates, or offset the indices from original arrays. It's probably easier to do the first. var allpoints = contour.concat(); for (var h in holes) { Array.prototype.push.apply( allpoints, holes[h] ); } //console.log( "allpoints",allpoints, allpoints.length ); // prepare all points map for ( i = 0, il = allpoints.length; i < il; i ++ ) { key = allpoints[ i ].x + ":" + allpoints[ i ].y; if ( allPointsMap[ key ] !== undefined ) { console.log( "Duplicate point", key ); } allPointsMap[ key ] = i; } // remove holes by cutting paths to holes and adding them to the shape var shapeWithoutHoles = removeHoles( contour, holes ); var triangles = THREE.FontUtils.Triangulate( shapeWithoutHoles, false ); // True returns indices for points of spooled shape //console.log( "triangles",triangles, triangles.length ); // check all face vertices against all points map for ( i = 0, il = triangles.length; i < il; i ++ ) { face = triangles[ i ]; for ( f = 0; f < 3; f ++ ) { key = face[ f ].x + ":" + face[ f ].y; index = allPointsMap[ key ]; if ( index !== undefined ) { face[ f ] = index; } } } return triangles.concat(); }, isClockWise: function ( pts ) { return THREE.FontUtils.Triangulate.area( pts ) < 0; }, // Bezier Curves formulas obtained from // http://en.wikipedia.org/wiki/B%C3%A9zier_curve // Quad Bezier Functions b2p0: function ( t, p ) { var k = 1 - t; return k * k * p; }, b2p1: function ( t, p ) { return 2 * ( 1 - t ) * t * p; }, b2p2: function ( t, p ) { return t * t * p; }, b2: function ( t, p0, p1, p2 ) { return this.b2p0( t, p0 ) + this.b2p1( t, p1 ) + this.b2p2( t, p2 ); }, // Cubic Bezier Functions b3p0: function ( t, p ) { var k = 1 - t; return k * k * k * p; }, b3p1: function ( t, p ) { var k = 1 - t; return 3 * k * k * t * p; }, b3p2: function ( t, p ) { var k = 1 - t; return 3 * k * t * t * p; }, b3p3: function ( t, p ) { return t * t * t * p; }, b3: function ( t, p0, p1, p2, p3 ) { return this.b3p0( t, p0 ) + this.b3p1( t, p1 ) + this.b3p2( t, p2 ) + this.b3p3( t, p3 ); } }; /************************************************************** * Line **************************************************************/ THREE.LineCurve = function ( v1, v2 ) { this.v1 = v1; this.v2 = v2; }; THREE.LineCurve.prototype = Object.create( THREE.Curve.prototype ); THREE.LineCurve.prototype.getPoint = function ( t ) { var point = this.v2.clone().sub(this.v1); point.multiplyScalar( t ).add( this.v1 ); return point; }; // Line curve is linear, so we can overwrite default getPointAt THREE.LineCurve.prototype.getPointAt = function ( u ) { return this.getPoint( u ); }; THREE.LineCurve.prototype.getTangent = function( t ) { var tangent = this.v2.clone().sub(this.v1); return tangent.normalize(); }; /************************************************************** * Quadratic Bezier curve **************************************************************/ THREE.QuadraticBezierCurve = function ( v0, v1, v2 ) { this.v0 = v0; this.v1 = v1; this.v2 = v2; }; THREE.QuadraticBezierCurve.prototype = Object.create( THREE.Curve.prototype ); THREE.QuadraticBezierCurve.prototype.getPoint = function ( t ) { var tx, ty; tx = THREE.Shape.Utils.b2( t, this.v0.x, this.v1.x, this.v2.x ); ty = THREE.Shape.Utils.b2( t, this.v0.y, this.v1.y, this.v2.y ); return new THREE.Vector2( tx, ty ); }; THREE.QuadraticBezierCurve.prototype.getTangent = function( t ) { var tx, ty; tx = THREE.Curve.Utils.tangentQuadraticBezier( t, this.v0.x, this.v1.x, this.v2.x ); ty = THREE.Curve.Utils.tangentQuadraticBezier( t, this.v0.y, this.v1.y, this.v2.y ); // returns unit vector var tangent = new THREE.Vector2( tx, ty ); tangent.normalize(); return tangent; }; /************************************************************** * Cubic Bezier curve **************************************************************/ THREE.CubicBezierCurve = function ( v0, v1, v2, v3 ) { this.v0 = v0; this.v1 = v1; this.v2 = v2; this.v3 = v3; }; THREE.CubicBezierCurve.prototype = Object.create( THREE.Curve.prototype ); THREE.CubicBezierCurve.prototype.getPoint = function ( t ) { var tx, ty; tx = THREE.Shape.Utils.b3( t, this.v0.x, this.v1.x, this.v2.x, this.v3.x ); ty = THREE.Shape.Utils.b3( t, this.v0.y, this.v1.y, this.v2.y, this.v3.y ); return new THREE.Vector2( tx, ty ); }; THREE.CubicBezierCurve.prototype.getTangent = function( t ) { var tx, ty; tx = THREE.Curve.Utils.tangentCubicBezier( t, this.v0.x, this.v1.x, this.v2.x, this.v3.x ); ty = THREE.Curve.Utils.tangentCubicBezier( t, this.v0.y, this.v1.y, this.v2.y, this.v3.y ); var tangent = new THREE.Vector2( tx, ty ); tangent.normalize(); return tangent; }; /************************************************************** * Spline curve **************************************************************/ THREE.SplineCurve = function ( points /* array of Vector2 */ ) { this.points = (points == undefined) ? [] : points; }; THREE.SplineCurve.prototype = Object.create( THREE.Curve.prototype ); THREE.SplineCurve.prototype.getPoint = function ( t ) { var v = new THREE.Vector2(); var c = []; var points = this.points, point, intPoint, weight; point = ( points.length - 1 ) * t; intPoint = Math.floor( point ); weight = point - intPoint; c[ 0 ] = intPoint == 0 ? intPoint : intPoint - 1; c[ 1 ] = intPoint; c[ 2 ] = intPoint > points.length - 2 ? points.length -1 : intPoint + 1; c[ 3 ] = intPoint > points.length - 3 ? points.length -1 : intPoint + 2; v.x = THREE.Curve.Utils.interpolate( points[ c[ 0 ] ].x, points[ c[ 1 ] ].x, points[ c[ 2 ] ].x, points[ c[ 3 ] ].x, weight ); v.y = THREE.Curve.Utils.interpolate( points[ c[ 0 ] ].y, points[ c[ 1 ] ].y, points[ c[ 2 ] ].y, points[ c[ 3 ] ].y, weight ); return v; }; /************************************************************** * Ellipse curve **************************************************************/ THREE.EllipseCurve = function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise ) { this.aX = aX; this.aY = aY; this.xRadius = xRadius; this.yRadius = yRadius; this.aStartAngle = aStartAngle; this.aEndAngle = aEndAngle; this.aClockwise = aClockwise; }; THREE.EllipseCurve.prototype = Object.create( THREE.Curve.prototype ); THREE.EllipseCurve.prototype.getPoint = function ( t ) { var angle; var deltaAngle = this.aEndAngle - this.aStartAngle; if ( deltaAngle < 0 ) deltaAngle += Math.PI * 2; if ( deltaAngle > Math.PI * 2 ) deltaAngle -= Math.PI * 2; if ( this.aClockwise === true ) { angle = this.aEndAngle + ( 1 - t ) * ( Math.PI * 2 - deltaAngle ); } else { angle = this.aStartAngle + t * deltaAngle; } var tx = this.aX + this.xRadius * Math.cos( angle ); var ty = this.aY + this.yRadius * Math.sin( angle ); return new THREE.Vector2( tx, ty ); }; /************************************************************** * Arc curve **************************************************************/ THREE.ArcCurve = function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) { THREE.EllipseCurve.call( this, aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise ); }; THREE.ArcCurve.prototype = Object.create( THREE.EllipseCurve.prototype ); /************************************************************** * Line3D **************************************************************/ THREE.LineCurve3 = THREE.Curve.create( function ( v1, v2 ) { this.v1 = v1; this.v2 = v2; }, function ( t ) { var r = new THREE.Vector3(); r.subVectors( this.v2, this.v1 ); // diff r.multiplyScalar( t ); r.add( this.v1 ); return r; } ); /************************************************************** * Quadratic Bezier 3D curve **************************************************************/ THREE.QuadraticBezierCurve3 = THREE.Curve.create( function ( v0, v1, v2 ) { this.v0 = v0; this.v1 = v1; this.v2 = v2; }, function ( t ) { var tx, ty, tz; tx = THREE.Shape.Utils.b2( t, this.v0.x, this.v1.x, this.v2.x ); ty = THREE.Shape.Utils.b2( t, this.v0.y, this.v1.y, this.v2.y ); tz = THREE.Shape.Utils.b2( t, this.v0.z, this.v1.z, this.v2.z ); return new THREE.Vector3( tx, ty, tz ); } ); /************************************************************** * Cubic Bezier 3D curve **************************************************************/ THREE.CubicBezierCurve3 = THREE.Curve.create( function ( v0, v1, v2, v3 ) { this.v0 = v0; this.v1 = v1; this.v2 = v2; this.v3 = v3; }, function ( t ) { var tx, ty, tz; tx = THREE.Shape.Utils.b3( t, this.v0.x, this.v1.x, this.v2.x, this.v3.x ); ty = THREE.Shape.Utils.b3( t, this.v0.y, this.v1.y, this.v2.y, this.v3.y ); tz = THREE.Shape.Utils.b3( t, this.v0.z, this.v1.z, this.v2.z, this.v3.z ); return new THREE.Vector3( tx, ty, tz ); } ); /************************************************************** * Spline 3D curve **************************************************************/ THREE.SplineCurve3 = THREE.Curve.create( function ( points /* array of Vector3 */) { this.points = (points == undefined) ? [] : points; }, function ( t ) { var v = new THREE.Vector3(); var c = []; var points = this.points, point, intPoint, weight; point = ( points.length - 1 ) * t; intPoint = Math.floor( point ); weight = point - intPoint; c[ 0 ] = intPoint == 0 ? intPoint : intPoint - 1; c[ 1 ] = intPoint; c[ 2 ] = intPoint > points.length - 2 ? points.length - 1 : intPoint + 1; c[ 3 ] = intPoint > points.length - 3 ? points.length - 1 : intPoint + 2; var pt0 = points[ c[0] ], pt1 = points[ c[1] ], pt2 = points[ c[2] ], pt3 = points[ c[3] ]; v.x = THREE.Curve.Utils.interpolate(pt0.x, pt1.x, pt2.x, pt3.x, weight); v.y = THREE.Curve.Utils.interpolate(pt0.y, pt1.y, pt2.y, pt3.y, weight); v.z = THREE.Curve.Utils.interpolate(pt0.z, pt1.z, pt2.z, pt3.z, weight); return v; } ); // THREE.SplineCurve3.prototype.getTangent = function(t) { // var v = new THREE.Vector3(); // var c = []; // var points = this.points, point, intPoint, weight; // point = ( points.length - 1 ) * t; // intPoint = Math.floor( point ); // weight = point - intPoint; // c[ 0 ] = intPoint == 0 ? intPoint : intPoint - 1; // c[ 1 ] = intPoint; // c[ 2 ] = intPoint > points.length - 2 ? points.length - 1 : intPoint + 1; // c[ 3 ] = intPoint > points.length - 3 ? points.length - 1 : intPoint + 2; // var pt0 = points[ c[0] ], // pt1 = points[ c[1] ], // pt2 = points[ c[2] ], // pt3 = points[ c[3] ]; // // t = weight; // v.x = THREE.Curve.Utils.tangentSpline( t, pt0.x, pt1.x, pt2.x, pt3.x ); // v.y = THREE.Curve.Utils.tangentSpline( t, pt0.y, pt1.y, pt2.y, pt3.y ); // v.z = THREE.Curve.Utils.tangentSpline( t, pt0.z, pt1.z, pt2.z, pt3.z ); // return v; // } /************************************************************** * Closed Spline 3D curve **************************************************************/ THREE.ClosedSplineCurve3 = THREE.Curve.create( function ( points /* array of Vector3 */) { this.points = (points == undefined) ? [] : points; }, function ( t ) { var v = new THREE.Vector3(); var c = []; var points = this.points, point, intPoint, weight; point = ( points.length - 0 ) * t; // This needs to be from 0-length +1 intPoint = Math.floor( point ); weight = point - intPoint; intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / points.length ) + 1 ) * points.length; c[ 0 ] = ( intPoint - 1 ) % points.length; c[ 1 ] = ( intPoint ) % points.length; c[ 2 ] = ( intPoint + 1 ) % points.length; c[ 3 ] = ( intPoint + 2 ) % points.length; v.x = THREE.Curve.Utils.interpolate( points[ c[ 0 ] ].x, points[ c[ 1 ] ].x, points[ c[ 2 ] ].x, points[ c[ 3 ] ].x, weight ); v.y = THREE.Curve.Utils.interpolate( points[ c[ 0 ] ].y, points[ c[ 1 ] ].y, points[ c[ 2 ] ].y, points[ c[ 3 ] ].y, weight ); v.z = THREE.Curve.Utils.interpolate( points[ c[ 0 ] ].z, points[ c[ 1 ] ].z, points[ c[ 2 ] ].z, points[ c[ 3 ] ].z, weight ); return v; } ); /** * @author mikael emtinger / http://gomo.se/ */ THREE.AnimationHandler = (function() { var playing = []; var library = {}; var that = {}; //--- update --- that.update = function( deltaTimeMS ) { for( var i = 0; i < playing.length; i ++ ) playing[ i ].update( deltaTimeMS ); }; //--- add --- that.addToUpdate = function( animation ) { if ( playing.indexOf( animation ) === -1 ) playing.push( animation ); }; //--- remove --- that.removeFromUpdate = function( animation ) { var index = playing.indexOf( animation ); if( index !== -1 ) playing.splice( index, 1 ); }; //--- add --- that.add = function( data ) { if ( library[ data.name ] !== undefined ) console.log( "THREE.AnimationHandler.add: Warning! " + data.name + " already exists in library. Overwriting." ); library[ data.name ] = data; initData( data ); }; //--- get --- that.get = function( name ) { if ( typeof name === "string" ) { if ( library[ name ] ) { return library[ name ]; } else { console.log( "THREE.AnimationHandler.get: Couldn't find animation " + name ); return null; } } else { // todo: add simple tween library } }; //--- parse --- that.parse = function( root ) { // setup hierarchy var hierarchy = []; if ( root instanceof THREE.SkinnedMesh ) { for( var b = 0; b < root.bones.length; b++ ) { hierarchy.push( root.bones[ b ] ); } } else { parseRecurseHierarchy( root, hierarchy ); } return hierarchy; }; var parseRecurseHierarchy = function( root, hierarchy ) { hierarchy.push( root ); for( var c = 0; c < root.children.length; c++ ) parseRecurseHierarchy( root.children[ c ], hierarchy ); } //--- init data --- var initData = function( data ) { if( data.initialized === true ) return; // loop through all keys for( var h = 0; h < data.hierarchy.length; h ++ ) { for( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) { // remove minus times if( data.hierarchy[ h ].keys[ k ].time < 0 ) data.hierarchy[ h ].keys[ k ].time = 0; // create quaternions if( data.hierarchy[ h ].keys[ k ].rot !== undefined && !( data.hierarchy[ h ].keys[ k ].rot instanceof THREE.Quaternion ) ) { var quat = data.hierarchy[ h ].keys[ k ].rot; data.hierarchy[ h ].keys[ k ].rot = new THREE.Quaternion( quat[0], quat[1], quat[2], quat[3] ); } } // prepare morph target keys if( data.hierarchy[ h ].keys.length && data.hierarchy[ h ].keys[ 0 ].morphTargets !== undefined ) { // get all used var usedMorphTargets = {}; for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) { for ( var m = 0; m < data.hierarchy[ h ].keys[ k ].morphTargets.length; m ++ ) { var morphTargetName = data.hierarchy[ h ].keys[ k ].morphTargets[ m ]; usedMorphTargets[ morphTargetName ] = -1; } } data.hierarchy[ h ].usedMorphTargets = usedMorphTargets; // set all used on all frames for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) { var influences = {}; for ( var morphTargetName in usedMorphTargets ) { for ( var m = 0; m < data.hierarchy[ h ].keys[ k ].morphTargets.length; m ++ ) { if ( data.hierarchy[ h ].keys[ k ].morphTargets[ m ] === morphTargetName ) { influences[ morphTargetName ] = data.hierarchy[ h ].keys[ k ].morphTargetsInfluences[ m ]; break; } } if ( m === data.hierarchy[ h ].keys[ k ].morphTargets.length ) { influences[ morphTargetName ] = 0; } } data.hierarchy[ h ].keys[ k ].morphTargetsInfluences = influences; } } // remove all keys that are on the same time for ( var k = 1; k < data.hierarchy[ h ].keys.length; k ++ ) { if ( data.hierarchy[ h ].keys[ k ].time === data.hierarchy[ h ].keys[ k - 1 ].time ) { data.hierarchy[ h ].keys.splice( k, 1 ); k --; } } // set index for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) { data.hierarchy[ h ].keys[ k ].index = k; } } // done data.initialized = true; }; // interpolation types that.LINEAR = 0; that.CATMULLROM = 1; that.CATMULLROM_FORWARD = 2; return that; }()); /** * @author mikael emtinger / http://gomo.se/ * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ THREE.Animation = function ( root, name ) { this.root = root; this.data = THREE.AnimationHandler.get( name ); this.hierarchy = THREE.AnimationHandler.parse( root ); this.currentTime = 0; this.timeScale = 1; this.isPlaying = false; this.isPaused = true; this.loop = true; this.interpolationType = THREE.AnimationHandler.LINEAR; this.points = []; this.target = new THREE.Vector3(); }; THREE.Animation.prototype.play = function ( startTime ) { this.currentTime = startTime !== undefined ? startTime : 0; if ( this.isPlaying === false ) { this.isPlaying = true; this.reset(); this.update( 0 ); } this.isPaused = false; THREE.AnimationHandler.addToUpdate( this ); }; THREE.Animation.prototype.pause = function() { if ( this.isPaused === true ) { THREE.AnimationHandler.addToUpdate( this ); } else { THREE.AnimationHandler.removeFromUpdate( this ); } this.isPaused = !this.isPaused; }; THREE.Animation.prototype.stop = function() { this.isPlaying = false; this.isPaused = false; THREE.AnimationHandler.removeFromUpdate( this ); }; THREE.Animation.prototype.reset = function () { for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) { var object = this.hierarchy[ h ]; object.matrixAutoUpdate = true; if ( object.animationCache === undefined ) { object.animationCache = {}; object.animationCache.prevKey = { pos: 0, rot: 0, scl: 0 }; object.animationCache.nextKey = { pos: 0, rot: 0, scl: 0 }; object.animationCache.originalMatrix = object instanceof THREE.Bone ? object.skinMatrix : object.matrix; } var prevKey = object.animationCache.prevKey; var nextKey = object.animationCache.nextKey; prevKey.pos = this.data.hierarchy[ h ].keys[ 0 ]; prevKey.rot = this.data.hierarchy[ h ].keys[ 0 ]; prevKey.scl = this.data.hierarchy[ h ].keys[ 0 ]; nextKey.pos = this.getNextKeyWith( "pos", h, 1 ); nextKey.rot = this.getNextKeyWith( "rot", h, 1 ); nextKey.scl = this.getNextKeyWith( "scl", h, 1 ); } }; THREE.Animation.prototype.update = function ( delta ) { if ( this.isPlaying === false ) return; this.currentTime += delta * this.timeScale; // var vector; var types = [ "pos", "rot", "scl" ]; var duration = this.data.length; if ( this.loop === true && this.currentTime > duration ) { this.currentTime %= duration; this.reset(); } this.currentTime = Math.min( this.currentTime, duration ); for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) { var object = this.hierarchy[ h ]; var animationCache = object.animationCache; // loop through pos/rot/scl for ( var t = 0; t < 3; t ++ ) { // get keys var type = types[ t ]; var prevKey = animationCache.prevKey[ type ]; var nextKey = animationCache.nextKey[ type ]; if ( nextKey.time <= this.currentTime ) { prevKey = this.data.hierarchy[ h ].keys[ 0 ]; nextKey = this.getNextKeyWith( type, h, 1 ); while ( nextKey.time < this.currentTime && nextKey.index > prevKey.index ) { prevKey = nextKey; nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 ); } animationCache.prevKey[ type ] = prevKey; animationCache.nextKey[ type ] = nextKey; } object.matrixAutoUpdate = true; object.matrixWorldNeedsUpdate = true; var scale = ( this.currentTime - prevKey.time ) / ( nextKey.time - prevKey.time ); var prevXYZ = prevKey[ type ]; var nextXYZ = nextKey[ type ]; if ( scale < 0 ) scale = 0; if ( scale > 1 ) scale = 1; // interpolate if ( type === "pos" ) { vector = object.position; if ( this.interpolationType === THREE.AnimationHandler.LINEAR ) { vector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale; vector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale; vector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale; } else if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM || this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) { this.points[ 0 ] = this.getPrevKeyWith( "pos", h, prevKey.index - 1 )[ "pos" ]; this.points[ 1 ] = prevXYZ; this.points[ 2 ] = nextXYZ; this.points[ 3 ] = this.getNextKeyWith( "pos", h, nextKey.index + 1 )[ "pos" ]; scale = scale * 0.33 + 0.33; var currentPoint = this.interpolateCatmullRom( this.points, scale ); vector.x = currentPoint[ 0 ]; vector.y = currentPoint[ 1 ]; vector.z = currentPoint[ 2 ]; if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) { var forwardPoint = this.interpolateCatmullRom( this.points, scale * 1.01 ); this.target.set( forwardPoint[ 0 ], forwardPoint[ 1 ], forwardPoint[ 2 ] ); this.target.sub( vector ); this.target.y = 0; this.target.normalize(); var angle = Math.atan2( this.target.x, this.target.z ); object.rotation.set( 0, angle, 0 ); } } } else if ( type === "rot" ) { THREE.Quaternion.slerp( prevXYZ, nextXYZ, object.quaternion, scale ); } else if ( type === "scl" ) { vector = object.scale; vector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale; vector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale; vector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale; } } } if ( this.loop === false && this.currentTime > duration ) { this.stop(); } }; // Catmull-Rom spline THREE.Animation.prototype.interpolateCatmullRom = function ( points, scale ) { var c = [], v3 = [], point, intPoint, weight, w2, w3, pa, pb, pc, pd; point = ( points.length - 1 ) * scale; intPoint = Math.floor( point ); weight = point - intPoint; c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1; c[ 1 ] = intPoint; c[ 2 ] = intPoint > points.length - 2 ? intPoint : intPoint + 1; c[ 3 ] = intPoint > points.length - 3 ? intPoint : intPoint + 2; pa = points[ c[ 0 ] ]; pb = points[ c[ 1 ] ]; pc = points[ c[ 2 ] ]; pd = points[ c[ 3 ] ]; w2 = weight * weight; w3 = weight * w2; v3[ 0 ] = this.interpolate( pa[ 0 ], pb[ 0 ], pc[ 0 ], pd[ 0 ], weight, w2, w3 ); v3[ 1 ] = this.interpolate( pa[ 1 ], pb[ 1 ], pc[ 1 ], pd[ 1 ], weight, w2, w3 ); v3[ 2 ] = this.interpolate( pa[ 2 ], pb[ 2 ], pc[ 2 ], pd[ 2 ], weight, w2, w3 ); return v3; }; THREE.Animation.prototype.interpolate = function ( p0, p1, p2, p3, t, t2, t3 ) { var v0 = ( p2 - p0 ) * 0.5, v1 = ( p3 - p1 ) * 0.5; return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1; }; // Get next key with THREE.Animation.prototype.getNextKeyWith = function ( type, h, key ) { var keys = this.data.hierarchy[ h ].keys; if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM || this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) { key = key < keys.length - 1 ? key : keys.length - 1; } else { key = key % keys.length; } for ( ; key < keys.length; key++ ) { if ( keys[ key ][ type ] !== undefined ) { return keys[ key ]; } } return this.data.hierarchy[ h ].keys[ 0 ]; }; // Get previous key with THREE.Animation.prototype.getPrevKeyWith = function ( type, h, key ) { var keys = this.data.hierarchy[ h ].keys; if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM || this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) { key = key > 0 ? key : 0; } else { key = key >= 0 ? key : key + keys.length; } for ( ; key >= 0; key -- ) { if ( keys[ key ][ type ] !== undefined ) { return keys[ key ]; } } return this.data.hierarchy[ h ].keys[ keys.length - 1 ]; }; /** * @author mikael emtinger / http://gomo.se/ * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ * @author khang duong * @author erik kitson */ THREE.KeyFrameAnimation = function ( root, data ) { this.root = root; this.data = THREE.AnimationHandler.get( data ); this.hierarchy = THREE.AnimationHandler.parse( root ); this.currentTime = 0; this.timeScale = 0.001; this.isPlaying = false; this.isPaused = true; this.loop = true; // initialize to first keyframes for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) { var keys = this.data.hierarchy[h].keys, sids = this.data.hierarchy[h].sids, obj = this.hierarchy[h]; if ( keys.length && sids ) { for ( var s = 0; s < sids.length; s++ ) { var sid = sids[ s ], next = this.getNextKeyWith( sid, h, 0 ); if ( next ) { next.apply( sid ); } } obj.matrixAutoUpdate = false; this.data.hierarchy[h].node.updateMatrix(); obj.matrixWorldNeedsUpdate = true; } } }; // Play THREE.KeyFrameAnimation.prototype.play = function ( startTime ) { this.currentTime = startTime !== undefined ? startTime : 0; if ( this.isPlaying === false ) { this.isPlaying = true; // reset key cache var h, hl = this.hierarchy.length, object, node; for ( h = 0; h < hl; h++ ) { object = this.hierarchy[ h ]; node = this.data.hierarchy[ h ]; if ( node.animationCache === undefined ) { node.animationCache = {}; node.animationCache.prevKey = null; node.animationCache.nextKey = null; node.animationCache.originalMatrix = object instanceof THREE.Bone ? object.skinMatrix : object.matrix; } var keys = this.data.hierarchy[h].keys; if (keys.length) { node.animationCache.prevKey = keys[ 0 ]; node.animationCache.nextKey = keys[ 1 ]; this.startTime = Math.min( keys[0].time, this.startTime ); this.endTime = Math.max( keys[keys.length - 1].time, this.endTime ); } } this.update( 0 ); } this.isPaused = false; THREE.AnimationHandler.addToUpdate( this ); }; // Pause THREE.KeyFrameAnimation.prototype.pause = function() { if( this.isPaused ) { THREE.AnimationHandler.addToUpdate( this ); } else { THREE.AnimationHandler.removeFromUpdate( this ); } this.isPaused = !this.isPaused; }; // Stop THREE.KeyFrameAnimation.prototype.stop = function() { this.isPlaying = false; this.isPaused = false; THREE.AnimationHandler.removeFromUpdate( this ); // reset JIT matrix and remove cache for ( var h = 0; h < this.data.hierarchy.length; h++ ) { var obj = this.hierarchy[ h ]; var node = this.data.hierarchy[ h ]; if ( node.animationCache !== undefined ) { var original = node.animationCache.originalMatrix; if( obj instanceof THREE.Bone ) { original.copy( obj.skinMatrix ); obj.skinMatrix = original; } else { original.copy( obj.matrix ); obj.matrix = original; } delete node.animationCache; } } }; // Update THREE.KeyFrameAnimation.prototype.update = function ( delta ) { if ( this.isPlaying === false ) return; this.currentTime += delta * this.timeScale; // var duration = this.data.length; if ( this.loop === true && this.currentTime > duration ) { this.currentTime %= duration; } this.currentTime = Math.min( this.currentTime, duration ); for ( var h = 0, hl = this.hierarchy.length; h < hl; h++ ) { var object = this.hierarchy[ h ]; var node = this.data.hierarchy[ h ]; var keys = node.keys, animationCache = node.animationCache; if ( keys.length ) { var prevKey = animationCache.prevKey; var nextKey = animationCache.nextKey; if ( nextKey.time <= this.currentTime ) { while ( nextKey.time < this.currentTime && nextKey.index > prevKey.index ) { prevKey = nextKey; nextKey = keys[ prevKey.index + 1 ]; } animationCache.prevKey = prevKey; animationCache.nextKey = nextKey; } if ( nextKey.time >= this.currentTime ) { prevKey.interpolate( nextKey, this.currentTime ); } else { prevKey.interpolate( nextKey, nextKey.time ); } this.data.hierarchy[ h ].node.updateMatrix(); object.matrixWorldNeedsUpdate = true; } } }; // Get next key with THREE.KeyFrameAnimation.prototype.getNextKeyWith = function( sid, h, key ) { var keys = this.data.hierarchy[ h ].keys; key = key % keys.length; for ( ; key < keys.length; key++ ) { if ( keys[ key ].hasTarget( sid ) ) { return keys[ key ]; } } return keys[ 0 ]; }; // Get previous key with THREE.KeyFrameAnimation.prototype.getPrevKeyWith = function( sid, h, key ) { var keys = this.data.hierarchy[ h ].keys; key = key >= 0 ? key : key + keys.length; for ( ; key >= 0; key-- ) { if ( keys[ key ].hasTarget( sid ) ) { return keys[ key ]; } } return keys[ keys.length - 1 ]; }; /** * @author mrdoob / http://mrdoob.com */ THREE.MorphAnimation = function ( mesh ) { this.mesh = mesh; this.frames = mesh.morphTargetInfluences.length; this.currentTime = 0; this.duration = 1000; this.loop = true; this.isPlaying = false; }; THREE.MorphAnimation.prototype = { play: function () { this.isPlaying = true; }, pause: function () { this.isPlaying = false; }, update: ( function () { var lastFrame = 0; var currentFrame = 0; return function ( delta ) { if ( this.isPlaying === false ) return; this.currentTime += delta; if ( this.loop === true && this.currentTime > this.duration ) { this.currentTime %= this.duration; } this.currentTime = Math.min( this.currentTime, this.duration ); var interpolation = this.duration / this.frames; var frame = Math.floor( this.currentTime / interpolation ); if ( frame != currentFrame ) { this.mesh.morphTargetInfluences[ lastFrame ] = 0; this.mesh.morphTargetInfluences[ currentFrame ] = 1; this.mesh.morphTargetInfluences[ frame ] = 0; lastFrame = currentFrame; currentFrame = frame; } this.mesh.morphTargetInfluences[ frame ] = ( this.currentTime % interpolation ) / interpolation; this.mesh.morphTargetInfluences[ lastFrame ] = 1 - this.mesh.morphTargetInfluences[ frame ]; } } )() }; /** * Camera for rendering cube maps * - renders scene into axis-aligned cube * * @author alteredq / http://alteredqualia.com/ */ THREE.CubeCamera = function ( near, far, cubeResolution ) { THREE.Object3D.call( this ); var fov = 90, aspect = 1; var cameraPX = new THREE.PerspectiveCamera( fov, aspect, near, far ); cameraPX.up.set( 0, -1, 0 ); cameraPX.lookAt( new THREE.Vector3( 1, 0, 0 ) ); this.add( cameraPX ); var cameraNX = new THREE.PerspectiveCamera( fov, aspect, near, far ); cameraNX.up.set( 0, -1, 0 ); cameraNX.lookAt( new THREE.Vector3( -1, 0, 0 ) ); this.add( cameraNX ); var cameraPY = new THREE.PerspectiveCamera( fov, aspect, near, far ); cameraPY.up.set( 0, 0, 1 ); cameraPY.lookAt( new THREE.Vector3( 0, 1, 0 ) ); this.add( cameraPY ); var cameraNY = new THREE.PerspectiveCamera( fov, aspect, near, far ); cameraNY.up.set( 0, 0, -1 ); cameraNY.lookAt( new THREE.Vector3( 0, -1, 0 ) ); this.add( cameraNY ); var cameraPZ = new THREE.PerspectiveCamera( fov, aspect, near, far ); cameraPZ.up.set( 0, -1, 0 ); cameraPZ.lookAt( new THREE.Vector3( 0, 0, 1 ) ); this.add( cameraPZ ); var cameraNZ = new THREE.PerspectiveCamera( fov, aspect, near, far ); cameraNZ.up.set( 0, -1, 0 ); cameraNZ.lookAt( new THREE.Vector3( 0, 0, -1 ) ); this.add( cameraNZ ); this.renderTarget = new THREE.WebGLRenderTargetCube( cubeResolution, cubeResolution, { format: THREE.RGBFormat, magFilter: THREE.LinearFilter, minFilter: THREE.LinearFilter } ); this.updateCubeMap = function ( renderer, scene ) { var renderTarget = this.renderTarget; var generateMipmaps = renderTarget.generateMipmaps; renderTarget.generateMipmaps = false; renderTarget.activeCubeFace = 0; renderer.render( scene, cameraPX, renderTarget ); renderTarget.activeCubeFace = 1; renderer.render( scene, cameraNX, renderTarget ); renderTarget.activeCubeFace = 2; renderer.render( scene, cameraPY, renderTarget ); renderTarget.activeCubeFace = 3; renderer.render( scene, cameraNY, renderTarget ); renderTarget.activeCubeFace = 4; renderer.render( scene, cameraPZ, renderTarget ); renderTarget.generateMipmaps = generateMipmaps; renderTarget.activeCubeFace = 5; renderer.render( scene, cameraNZ, renderTarget ); }; }; THREE.CubeCamera.prototype = Object.create( THREE.Object3D.prototype ); /** * @author zz85 / http://twitter.com/blurspline / http://www.lab4games.net/zz85/blog * * A general perpose camera, for setting FOV, Lens Focal Length, * and switching between perspective and orthographic views easily. * Use this only if you do not wish to manage * both a Orthographic and Perspective Camera * */ THREE.CombinedCamera = function ( width, height, fov, near, far, orthoNear, orthoFar ) { THREE.Camera.call( this ); this.fov = fov; this.left = -width / 2; this.right = width / 2 this.top = height / 2; this.bottom = -height / 2; // We could also handle the projectionMatrix internally, but just wanted to test nested camera objects this.cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, orthoNear, orthoFar ); this.cameraP = new THREE.PerspectiveCamera( fov, width / height, near, far ); this.zoom = 1; this.toPerspective(); var aspect = width/height; }; THREE.CombinedCamera.prototype = Object.create( THREE.Camera.prototype ); THREE.CombinedCamera.prototype.toPerspective = function () { // Switches to the Perspective Camera this.near = this.cameraP.near; this.far = this.cameraP.far; this.cameraP.fov = this.fov / this.zoom ; this.cameraP.updateProjectionMatrix(); this.projectionMatrix = this.cameraP.projectionMatrix; this.inPerspectiveMode = true; this.inOrthographicMode = false; }; THREE.CombinedCamera.prototype.toOrthographic = function () { // Switches to the Orthographic camera estimating viewport from Perspective var fov = this.fov; var aspect = this.cameraP.aspect; var near = this.cameraP.near; var far = this.cameraP.far; // The size that we set is the mid plane of the viewing frustum var hyperfocus = ( near + far ) / 2; var halfHeight = Math.tan( fov / 2 ) * hyperfocus; var planeHeight = 2 * halfHeight; var planeWidth = planeHeight * aspect; var halfWidth = planeWidth / 2; halfHeight /= this.zoom; halfWidth /= this.zoom; this.cameraO.left = -halfWidth; this.cameraO.right = halfWidth; this.cameraO.top = halfHeight; this.cameraO.bottom = -halfHeight; // this.cameraO.left = -farHalfWidth; // this.cameraO.right = farHalfWidth; // this.cameraO.top = farHalfHeight; // this.cameraO.bottom = -farHalfHeight; // this.cameraO.left = this.left / this.zoom; // this.cameraO.right = this.right / this.zoom; // this.cameraO.top = this.top / this.zoom; // this.cameraO.bottom = this.bottom / this.zoom; this.cameraO.updateProjectionMatrix(); this.near = this.cameraO.near; this.far = this.cameraO.far; this.projectionMatrix = this.cameraO.projectionMatrix; this.inPerspectiveMode = false; this.inOrthographicMode = true; }; THREE.CombinedCamera.prototype.setSize = function( width, height ) { this.cameraP.aspect = width / height; this.left = -width / 2; this.right = width / 2 this.top = height / 2; this.bottom = -height / 2; }; THREE.CombinedCamera.prototype.setFov = function( fov ) { this.fov = fov; if ( this.inPerspectiveMode ) { this.toPerspective(); } else { this.toOrthographic(); } }; // For mantaining similar API with PerspectiveCamera THREE.CombinedCamera.prototype.updateProjectionMatrix = function() { if ( this.inPerspectiveMode ) { this.toPerspective(); } else { this.toPerspective(); this.toOrthographic(); } }; /* * Uses Focal Length (in mm) to estimate and set FOV * 35mm (fullframe) camera is used if frame size is not specified; * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html */ THREE.CombinedCamera.prototype.setLens = function ( focalLength, frameHeight ) { if ( frameHeight === undefined ) frameHeight = 24; var fov = 2 * THREE.Math.radToDeg( Math.atan( frameHeight / ( focalLength * 2 ) ) ); this.setFov( fov ); return fov; }; THREE.CombinedCamera.prototype.setZoom = function( zoom ) { this.zoom = zoom; if ( this.inPerspectiveMode ) { this.toPerspective(); } else { this.toOrthographic(); } }; THREE.CombinedCamera.prototype.toFrontView = function() { this.rotation.x = 0; this.rotation.y = 0; this.rotation.z = 0; // should we be modifing the matrix instead? this.rotationAutoUpdate = false; }; THREE.CombinedCamera.prototype.toBackView = function() { this.rotation.x = 0; this.rotation.y = Math.PI; this.rotation.z = 0; this.rotationAutoUpdate = false; }; THREE.CombinedCamera.prototype.toLeftView = function() { this.rotation.x = 0; this.rotation.y = - Math.PI / 2; this.rotation.z = 0; this.rotationAutoUpdate = false; }; THREE.CombinedCamera.prototype.toRightView = function() { this.rotation.x = 0; this.rotation.y = Math.PI / 2; this.rotation.z = 0; this.rotationAutoUpdate = false; }; THREE.CombinedCamera.prototype.toTopView = function() { this.rotation.x = - Math.PI / 2; this.rotation.y = 0; this.rotation.z = 0; this.rotationAutoUpdate = false; }; THREE.CombinedCamera.prototype.toBottomView = function() { this.rotation.x = Math.PI / 2; this.rotation.y = 0; this.rotation.z = 0; this.rotationAutoUpdate = false; }; /** * @author mrdoob / http://mrdoob.com/ * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Cube.as */ THREE.BoxGeometry = function ( width, height, depth, widthSegments, heightSegments, depthSegments ) { THREE.Geometry.call( this ); var scope = this; this.width = width; this.height = height; this.depth = depth; this.widthSegments = widthSegments || 1; this.heightSegments = heightSegments || 1; this.depthSegments = depthSegments || 1; var width_half = this.width / 2; var height_half = this.height / 2; var depth_half = this.depth / 2; buildPlane( 'z', 'y', - 1, - 1, this.depth, this.height, width_half, 0 ); // px buildPlane( 'z', 'y', 1, - 1, this.depth, this.height, - width_half, 1 ); // nx buildPlane( 'x', 'z', 1, 1, this.width, this.depth, height_half, 2 ); // py buildPlane( 'x', 'z', 1, - 1, this.width, this.depth, - height_half, 3 ); // ny buildPlane( 'x', 'y', 1, - 1, this.width, this.height, depth_half, 4 ); // pz buildPlane( 'x', 'y', - 1, - 1, this.width, this.height, - depth_half, 5 ); // nz function buildPlane( u, v, udir, vdir, width, height, depth, materialIndex ) { var w, ix, iy, gridX = scope.widthSegments, gridY = scope.heightSegments, width_half = width / 2, height_half = height / 2, offset = scope.vertices.length; if ( ( u === 'x' && v === 'y' ) || ( u === 'y' && v === 'x' ) ) { w = 'z'; } else if ( ( u === 'x' && v === 'z' ) || ( u === 'z' && v === 'x' ) ) { w = 'y'; gridY = scope.depthSegments; } else if ( ( u === 'z' && v === 'y' ) || ( u === 'y' && v === 'z' ) ) { w = 'x'; gridX = scope.depthSegments; } var gridX1 = gridX + 1, gridY1 = gridY + 1, segment_width = width / gridX, segment_height = height / gridY, normal = new THREE.Vector3(); normal[ w ] = depth > 0 ? 1 : - 1; for ( iy = 0; iy < gridY1; iy ++ ) { for ( ix = 0; ix < gridX1; ix ++ ) { var vector = new THREE.Vector3(); vector[ u ] = ( ix * segment_width - width_half ) * udir; vector[ v ] = ( iy * segment_height - height_half ) * vdir; vector[ w ] = depth; scope.vertices.push( vector ); } } for ( iy = 0; iy < gridY; iy++ ) { for ( ix = 0; ix < gridX; ix++ ) { var a = ix + gridX1 * iy; var b = ix + gridX1 * ( iy + 1 ); var c = ( ix + 1 ) + gridX1 * ( iy + 1 ); var d = ( ix + 1 ) + gridX1 * iy; var uva = new THREE.Vector2( ix / gridX, 1 - iy / gridY ); var uvb = new THREE.Vector2( ix / gridX, 1 - ( iy + 1 ) / gridY ); var uvc = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - ( iy + 1 ) / gridY ); var uvd = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - iy / gridY ); var face = new THREE.Face3( a + offset, b + offset, d + offset ); face.normal.copy( normal ); face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() ); face.materialIndex = materialIndex; scope.faces.push( face ); scope.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] ); face = new THREE.Face3( b + offset, c + offset, d + offset ); face.normal.copy( normal ); face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() ); face.materialIndex = materialIndex; scope.faces.push( face ); scope.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] ); } } } this.computeCentroids(); this.mergeVertices(); }; THREE.BoxGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author hughes */ THREE.CircleGeometry = function ( radius, segments, thetaStart, thetaLength ) { THREE.Geometry.call( this ); this.radius = radius = radius || 50; this.segments = segments = segments !== undefined ? Math.max( 3, segments ) : 8; this.thetaStart = thetaStart = thetaStart !== undefined ? thetaStart : 0; this.thetaLength = thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2; var i, uvs = [], center = new THREE.Vector3(), centerUV = new THREE.Vector2( 0.5, 0.5 ); this.vertices.push(center); uvs.push( centerUV ); for ( i = 0; i <= segments; i ++ ) { var vertex = new THREE.Vector3(); var segment = thetaStart + i / segments * thetaLength; vertex.x = radius * Math.cos( segment ); vertex.y = radius * Math.sin( segment ); this.vertices.push( vertex ); uvs.push( new THREE.Vector2( ( vertex.x / radius + 1 ) / 2, ( vertex.y / radius + 1 ) / 2 ) ); } var n = new THREE.Vector3( 0, 0, 1 ); for ( i = 1; i <= segments; i ++ ) { var v1 = i; var v2 = i + 1 ; var v3 = 0; this.faces.push( new THREE.Face3( v1, v2, v3, [ n.clone(), n.clone(), n.clone() ] ) ); this.faceVertexUvs[ 0 ].push( [ uvs[ i ].clone(), uvs[ i + 1 ].clone(), centerUV.clone() ] ); } this.computeCentroids(); this.computeFaceNormals(); this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius ); }; THREE.CircleGeometry.prototype = Object.create( THREE.Geometry.prototype ); // DEPRECATED THREE.CubeGeometry = THREE.BoxGeometry; /** * @author mrdoob / http://mrdoob.com/ */ THREE.CylinderGeometry = function ( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded ) { THREE.Geometry.call( this ); this.radiusTop = radiusTop = radiusTop !== undefined ? radiusTop : 20; this.radiusBottom = radiusBottom = radiusBottom !== undefined ? radiusBottom : 20; this.height = height = height !== undefined ? height : 100; this.radialSegments = radialSegments = radialSegments || 8; this.heightSegments = heightSegments = heightSegments || 1; this.openEnded = openEnded = openEnded !== undefined ? openEnded : false; var heightHalf = height / 2; var x, y, vertices = [], uvs = []; for ( y = 0; y <= heightSegments; y ++ ) { var verticesRow = []; var uvsRow = []; var v = y / heightSegments; var radius = v * ( radiusBottom - radiusTop ) + radiusTop; for ( x = 0; x <= radialSegments; x ++ ) { var u = x / radialSegments; var vertex = new THREE.Vector3(); vertex.x = radius * Math.sin( u * Math.PI * 2 ); vertex.y = - v * height + heightHalf; vertex.z = radius * Math.cos( u * Math.PI * 2 ); this.vertices.push( vertex ); verticesRow.push( this.vertices.length - 1 ); uvsRow.push( new THREE.Vector2( u, 1 - v ) ); } vertices.push( verticesRow ); uvs.push( uvsRow ); } var tanTheta = ( radiusBottom - radiusTop ) / height; var na, nb; for ( x = 0; x < radialSegments; x ++ ) { if ( radiusTop !== 0 ) { na = this.vertices[ vertices[ 0 ][ x ] ].clone(); nb = this.vertices[ vertices[ 0 ][ x + 1 ] ].clone(); } else { na = this.vertices[ vertices[ 1 ][ x ] ].clone(); nb = this.vertices[ vertices[ 1 ][ x + 1 ] ].clone(); } na.setY( Math.sqrt( na.x * na.x + na.z * na.z ) * tanTheta ).normalize(); nb.setY( Math.sqrt( nb.x * nb.x + nb.z * nb.z ) * tanTheta ).normalize(); for ( y = 0; y < heightSegments; y ++ ) { var v1 = vertices[ y ][ x ]; var v2 = vertices[ y + 1 ][ x ]; var v3 = vertices[ y + 1 ][ x + 1 ]; var v4 = vertices[ y ][ x + 1 ]; var n1 = na.clone(); var n2 = na.clone(); var n3 = nb.clone(); var n4 = nb.clone(); var uv1 = uvs[ y ][ x ].clone(); var uv2 = uvs[ y + 1 ][ x ].clone(); var uv3 = uvs[ y + 1 ][ x + 1 ].clone(); var uv4 = uvs[ y ][ x + 1 ].clone(); this.faces.push( new THREE.Face3( v1, v2, v4, [ n1, n2, n4 ] ) ); this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv4 ] ); this.faces.push( new THREE.Face3( v2, v3, v4, [ n2.clone(), n3, n4.clone() ] ) ); this.faceVertexUvs[ 0 ].push( [ uv2.clone(), uv3, uv4.clone() ] ); } } // top cap if ( openEnded === false && radiusTop > 0 ) { this.vertices.push( new THREE.Vector3( 0, heightHalf, 0 ) ); for ( x = 0; x < radialSegments; x ++ ) { var v1 = vertices[ 0 ][ x ]; var v2 = vertices[ 0 ][ x + 1 ]; var v3 = this.vertices.length - 1; var n1 = new THREE.Vector3( 0, 1, 0 ); var n2 = new THREE.Vector3( 0, 1, 0 ); var n3 = new THREE.Vector3( 0, 1, 0 ); var uv1 = uvs[ 0 ][ x ].clone(); var uv2 = uvs[ 0 ][ x + 1 ].clone(); var uv3 = new THREE.Vector2( uv2.x, 0 ); this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) ); this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] ); } } // bottom cap if ( openEnded === false && radiusBottom > 0 ) { this.vertices.push( new THREE.Vector3( 0, - heightHalf, 0 ) ); for ( x = 0; x < radialSegments; x ++ ) { var v1 = vertices[ y ][ x + 1 ]; var v2 = vertices[ y ][ x ]; var v3 = this.vertices.length - 1; var n1 = new THREE.Vector3( 0, - 1, 0 ); var n2 = new THREE.Vector3( 0, - 1, 0 ); var n3 = new THREE.Vector3( 0, - 1, 0 ); var uv1 = uvs[ y ][ x + 1 ].clone(); var uv2 = uvs[ y ][ x ].clone(); var uv3 = new THREE.Vector2( uv2.x, 1 ); this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) ); this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] ); } } this.computeCentroids(); this.computeFaceNormals(); } THREE.CylinderGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author zz85 / http://www.lab4games.net/zz85/blog * * Creates extruded geometry from a path shape. * * parameters = { * * curveSegments: <int>, // number of points on the curves * steps: <int>, // number of points for z-side extrusions / used for subdividing segements of extrude spline too * amount: <int>, // Depth to extrude the shape * * bevelEnabled: <bool>, // turn on bevel * bevelThickness: <float>, // how deep into the original shape bevel goes * bevelSize: <float>, // how far from shape outline is bevel * bevelSegments: <int>, // number of bevel layers * * extrudePath: <THREE.CurvePath> // 3d spline path to extrude shape along. (creates Frames if .frames aren't defined) * frames: <THREE.TubeGeometry.FrenetFrames> // containing arrays of tangents, normals, binormals * * material: <int> // material index for front and back faces * extrudeMaterial: <int> // material index for extrusion and beveled faces * uvGenerator: <Object> // object that provides UV generator functions * * } **/ THREE.ExtrudeGeometry = function ( shapes, options ) { if ( typeof( shapes ) === "undefined" ) { shapes = []; return; } THREE.Geometry.call( this ); shapes = shapes instanceof Array ? shapes : [ shapes ]; this.shapebb = shapes[ shapes.length - 1 ].getBoundingBox(); this.addShapeList( shapes, options ); this.computeCentroids(); this.computeFaceNormals(); // can't really use automatic vertex normals // as then front and back sides get smoothed too // should do separate smoothing just for sides //this.computeVertexNormals(); //console.log( "took", ( Date.now() - startTime ) ); }; THREE.ExtrudeGeometry.prototype = Object.create( THREE.Geometry.prototype ); THREE.ExtrudeGeometry.prototype.addShapeList = function ( shapes, options ) { var sl = shapes.length; for ( var s = 0; s < sl; s ++ ) { var shape = shapes[ s ]; this.addShape( shape, options ); } }; THREE.ExtrudeGeometry.prototype.addShape = function ( shape, options ) { var amount = options.amount !== undefined ? options.amount : 100; var bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 6; // 10 var bevelSize = options.bevelSize !== undefined ? options.bevelSize : bevelThickness - 2; // 8 var bevelSegments = options.bevelSegments !== undefined ? options.bevelSegments : 3; var bevelEnabled = options.bevelEnabled !== undefined ? options.bevelEnabled : true; // false var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12; var steps = options.steps !== undefined ? options.steps : 1; var extrudePath = options.extrudePath; var extrudePts, extrudeByPath = false; var material = options.material; var extrudeMaterial = options.extrudeMaterial; // Use default WorldUVGenerator if no UV generators are specified. var uvgen = options.UVGenerator !== undefined ? options.UVGenerator : THREE.ExtrudeGeometry.WorldUVGenerator; var shapebb = this.shapebb; //shapebb = shape.getBoundingBox(); var splineTube, binormal, normal, position2; if ( extrudePath ) { extrudePts = extrudePath.getSpacedPoints( steps ); extrudeByPath = true; bevelEnabled = false; // bevels not supported for path extrusion // SETUP TNB variables // Reuse TNB from TubeGeomtry for now. // TODO1 - have a .isClosed in spline? splineTube = options.frames !== undefined ? options.frames : new THREE.TubeGeometry.FrenetFrames(extrudePath, steps, false); // console.log(splineTube, 'splineTube', splineTube.normals.length, 'steps', steps, 'extrudePts', extrudePts.length); binormal = new THREE.Vector3(); normal = new THREE.Vector3(); position2 = new THREE.Vector3(); } // Safeguards if bevels are not enabled if ( ! bevelEnabled ) { bevelSegments = 0; bevelThickness = 0; bevelSize = 0; } // Variables initalization var ahole, h, hl; // looping of holes var scope = this; var bevelPoints = []; var shapesOffset = this.vertices.length; var shapePoints = shape.extractPoints( curveSegments ); var vertices = shapePoints.shape; var holes = shapePoints.holes; var reverse = !THREE.Shape.Utils.isClockWise( vertices ) ; if ( reverse ) { vertices = vertices.reverse(); // Maybe we should also check if holes are in the opposite direction, just to be safe ... for ( h = 0, hl = holes.length; h < hl; h ++ ) { ahole = holes[ h ]; if ( THREE.Shape.Utils.isClockWise( ahole ) ) { holes[ h ] = ahole.reverse(); } } reverse = false; // If vertices are in order now, we shouldn't need to worry about them again (hopefully)! } var faces = THREE.Shape.Utils.triangulateShape ( vertices, holes ); /* Vertices */ var contour = vertices; // vertices has all points but contour has only points of circumference for ( h = 0, hl = holes.length; h < hl; h ++ ) { ahole = holes[ h ]; vertices = vertices.concat( ahole ); } function scalePt2 ( pt, vec, size ) { if ( !vec ) console.log( "die" ); return vec.clone().multiplyScalar( size ).add( pt ); } var b, bs, t, z, vert, vlen = vertices.length, face, flen = faces.length, cont, clen = contour.length; // Find directions for point movement var RAD_TO_DEGREES = 180 / Math.PI; function getBevelVec( inPt, inPrev, inNext ) { var EPSILON = 0.0000000001; var sign = THREE.Math.sign; // computes for inPt the corresponding point inPt' on a new contour // shiftet by 1 unit (length of normalized vector) to the left // if we walk along contour clockwise, this new contour is outside the old one // // inPt' is the intersection of the two lines parallel to the two // adjacent edges of inPt at a distance of 1 unit on the left side. var v_trans_x, v_trans_y, shrink_by = 1; // resulting translation vector for inPt // good reading for geometry algorithms (here: line-line intersection) // http://geomalgorithms.com/a05-_intersect-1.html var v_prev_x = inPt.x - inPrev.x, v_prev_y = inPt.y - inPrev.y; var v_next_x = inNext.x - inPt.x, v_next_y = inNext.y - inPt.y; var v_prev_lensq = ( v_prev_x * v_prev_x + v_prev_y * v_prev_y ); // check for colinear edges var colinear0 = ( v_prev_x * v_next_y - v_prev_y * v_next_x ); if ( Math.abs( colinear0 ) > EPSILON ) { // not colinear // length of vectors for normalizing var v_prev_len = Math.sqrt( v_prev_lensq ); var v_next_len = Math.sqrt( v_next_x * v_next_x + v_next_y * v_next_y ); // shift adjacent points by unit vectors to the left var ptPrevShift_x = ( inPrev.x - v_prev_y / v_prev_len ); var ptPrevShift_y = ( inPrev.y + v_prev_x / v_prev_len ); var ptNextShift_x = ( inNext.x - v_next_y / v_next_len ); var ptNextShift_y = ( inNext.y + v_next_x / v_next_len ); // scaling factor for v_prev to intersection point var sf = ( ( ptNextShift_x - ptPrevShift_x ) * v_next_y - ( ptNextShift_y - ptPrevShift_y ) * v_next_x ) / ( v_prev_x * v_next_y - v_prev_y * v_next_x ); // vector from inPt to intersection point v_trans_x = ( ptPrevShift_x + v_prev_x * sf - inPt.x ); v_trans_y = ( ptPrevShift_y + v_prev_y * sf - inPt.y ); // Don't normalize!, otherwise sharp corners become ugly // but prevent crazy spikes var v_trans_lensq = ( v_trans_x * v_trans_x + v_trans_y * v_trans_y ) if ( v_trans_lensq <= 2 ) { return new THREE.Vector2( v_trans_x, v_trans_y ); } else { shrink_by = Math.sqrt( v_trans_lensq / 2 ); } } else { // handle special case of colinear edges var direction_eq = false; // assumes: opposite if ( v_prev_x > EPSILON ) { if ( v_next_x > EPSILON ) { direction_eq = true; } } else { if ( v_prev_x < -EPSILON ) { if ( v_next_x < -EPSILON ) { direction_eq = true; } } else { if ( sign(v_prev_y) == sign(v_next_y) ) { direction_eq = true; } } } if ( direction_eq ) { // console.log("Warning: lines are a straight sequence"); v_trans_x = -v_prev_y; v_trans_y = v_prev_x; shrink_by = Math.sqrt( v_prev_lensq ); } else { // console.log("Warning: lines are a straight spike"); v_trans_x = v_prev_x; v_trans_y = v_prev_y; shrink_by = Math.sqrt( v_prev_lensq / 2 ); } } return new THREE.Vector2( v_trans_x / shrink_by, v_trans_y / shrink_by ); } var contourMovements = []; for ( var i = 0, il = contour.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) { if ( j === il ) j = 0; if ( k === il ) k = 0; // (j)---(i)---(k) // console.log('i,j,k', i, j , k) var pt_i = contour[ i ]; var pt_j = contour[ j ]; var pt_k = contour[ k ]; contourMovements[ i ]= getBevelVec( contour[ i ], contour[ j ], contour[ k ] ); } var holesMovements = [], oneHoleMovements, verticesMovements = contourMovements.concat(); for ( h = 0, hl = holes.length; h < hl; h ++ ) { ahole = holes[ h ]; oneHoleMovements = []; for ( i = 0, il = ahole.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) { if ( j === il ) j = 0; if ( k === il ) k = 0; // (j)---(i)---(k) oneHoleMovements[ i ]= getBevelVec( ahole[ i ], ahole[ j ], ahole[ k ] ); } holesMovements.push( oneHoleMovements ); verticesMovements = verticesMovements.concat( oneHoleMovements ); } // Loop bevelSegments, 1 for the front, 1 for the back for ( b = 0; b < bevelSegments; b ++ ) { //for ( b = bevelSegments; b > 0; b -- ) { t = b / bevelSegments; z = bevelThickness * ( 1 - t ); //z = bevelThickness * t; bs = bevelSize * ( Math.sin ( t * Math.PI/2 ) ) ; // curved //bs = bevelSize * t ; // linear // contract shape for ( i = 0, il = contour.length; i < il; i ++ ) { vert = scalePt2( contour[ i ], contourMovements[ i ], bs ); //vert = scalePt( contour[ i ], contourCentroid, bs, false ); v( vert.x, vert.y, - z ); } // expand holes for ( h = 0, hl = holes.length; h < hl; h++ ) { ahole = holes[ h ]; oneHoleMovements = holesMovements[ h ]; for ( i = 0, il = ahole.length; i < il; i++ ) { vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs ); //vert = scalePt( ahole[ i ], holesCentroids[ h ], bs, true ); v( vert.x, vert.y, -z ); } } } bs = bevelSize; // Back facing vertices for ( i = 0; i < vlen; i ++ ) { vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ]; if ( !extrudeByPath ) { v( vert.x, vert.y, 0 ); } else { // v( vert.x, vert.y + extrudePts[ 0 ].y, extrudePts[ 0 ].x ); normal.copy( splineTube.normals[0] ).multiplyScalar(vert.x); binormal.copy( splineTube.binormals[0] ).multiplyScalar(vert.y); position2.copy( extrudePts[0] ).add(normal).add(binormal); v( position2.x, position2.y, position2.z ); } } // Add stepped vertices... // Including front facing vertices var s; for ( s = 1; s <= steps; s ++ ) { for ( i = 0; i < vlen; i ++ ) { vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ]; if ( !extrudeByPath ) { v( vert.x, vert.y, amount / steps * s ); } else { // v( vert.x, vert.y + extrudePts[ s - 1 ].y, extrudePts[ s - 1 ].x ); normal.copy( splineTube.normals[s] ).multiplyScalar( vert.x ); binormal.copy( splineTube.binormals[s] ).multiplyScalar( vert.y ); position2.copy( extrudePts[s] ).add( normal ).add( binormal ); v( position2.x, position2.y, position2.z ); } } } // Add bevel segments planes //for ( b = 1; b <= bevelSegments; b ++ ) { for ( b = bevelSegments - 1; b >= 0; b -- ) { t = b / bevelSegments; z = bevelThickness * ( 1 - t ); //bs = bevelSize * ( 1-Math.sin ( ( 1 - t ) * Math.PI/2 ) ); bs = bevelSize * Math.sin ( t * Math.PI/2 ) ; // contract shape for ( i = 0, il = contour.length; i < il; i ++ ) { vert = scalePt2( contour[ i ], contourMovements[ i ], bs ); v( vert.x, vert.y, amount + z ); } // expand holes for ( h = 0, hl = holes.length; h < hl; h ++ ) { ahole = holes[ h ]; oneHoleMovements = holesMovements[ h ]; for ( i = 0, il = ahole.length; i < il; i ++ ) { vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs ); if ( !extrudeByPath ) { v( vert.x, vert.y, amount + z ); } else { v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x + z ); } } } } /* Faces */ // Top and bottom faces buildLidFaces(); // Sides faces buildSideFaces(); ///// Internal functions function buildLidFaces() { if ( bevelEnabled ) { var layer = 0 ; // steps + 1 var offset = vlen * layer; // Bottom faces for ( i = 0; i < flen; i ++ ) { face = faces[ i ]; f3( face[ 2 ]+ offset, face[ 1 ]+ offset, face[ 0 ] + offset, true ); } layer = steps + bevelSegments * 2; offset = vlen * layer; // Top faces for ( i = 0; i < flen; i ++ ) { face = faces[ i ]; f3( face[ 0 ] + offset, face[ 1 ] + offset, face[ 2 ] + offset, false ); } } else { // Bottom faces for ( i = 0; i < flen; i++ ) { face = faces[ i ]; f3( face[ 2 ], face[ 1 ], face[ 0 ], true ); } // Top faces for ( i = 0; i < flen; i ++ ) { face = faces[ i ]; f3( face[ 0 ] + vlen * steps, face[ 1 ] + vlen * steps, face[ 2 ] + vlen * steps, false ); } } } // Create faces for the z-sides of the shape function buildSideFaces() { var layeroffset = 0; sidewalls( contour, layeroffset ); layeroffset += contour.length; for ( h = 0, hl = holes.length; h < hl; h ++ ) { ahole = holes[ h ]; sidewalls( ahole, layeroffset ); //, true layeroffset += ahole.length; } } function sidewalls( contour, layeroffset ) { var j, k; i = contour.length; while ( --i >= 0 ) { j = i; k = i - 1; if ( k < 0 ) k = contour.length - 1; //console.log('b', i,j, i-1, k,vertices.length); var s = 0, sl = steps + bevelSegments * 2; for ( s = 0; s < sl; s ++ ) { var slen1 = vlen * s; var slen2 = vlen * ( s + 1 ); var a = layeroffset + j + slen1, b = layeroffset + k + slen1, c = layeroffset + k + slen2, d = layeroffset + j + slen2; f4( a, b, c, d, contour, s, sl, j, k ); } } } function v( x, y, z ) { scope.vertices.push( new THREE.Vector3( x, y, z ) ); } function f3( a, b, c, isBottom ) { a += shapesOffset; b += shapesOffset; c += shapesOffset; // normal, color, material scope.faces.push( new THREE.Face3( a, b, c, null, null, material ) ); var uvs = isBottom ? uvgen.generateBottomUV( scope, shape, options, a, b, c ) : uvgen.generateTopUV( scope, shape, options, a, b, c ); scope.faceVertexUvs[ 0 ].push( uvs ); } function f4( a, b, c, d, wallContour, stepIndex, stepsLength, contourIndex1, contourIndex2 ) { a += shapesOffset; b += shapesOffset; c += shapesOffset; d += shapesOffset; scope.faces.push( new THREE.Face3( a, b, d, null, null, extrudeMaterial ) ); scope.faces.push( new THREE.Face3( b, c, d, null, null, extrudeMaterial ) ); var uvs = uvgen.generateSideWallUV( scope, shape, wallContour, options, a, b, c, d, stepIndex, stepsLength, contourIndex1, contourIndex2 ); scope.faceVertexUvs[ 0 ].push( [ uvs[ 0 ], uvs[ 1 ], uvs[ 3 ] ] ); scope.faceVertexUvs[ 0 ].push( [ uvs[ 1 ], uvs[ 2 ], uvs[ 3 ] ] ); } }; THREE.ExtrudeGeometry.WorldUVGenerator = { generateTopUV: function( geometry, extrudedShape, extrudeOptions, indexA, indexB, indexC ) { var ax = geometry.vertices[ indexA ].x, ay = geometry.vertices[ indexA ].y, bx = geometry.vertices[ indexB ].x, by = geometry.vertices[ indexB ].y, cx = geometry.vertices[ indexC ].x, cy = geometry.vertices[ indexC ].y; return [ new THREE.Vector2( ax, ay ), new THREE.Vector2( bx, by ), new THREE.Vector2( cx, cy ) ]; }, generateBottomUV: function( geometry, extrudedShape, extrudeOptions, indexA, indexB, indexC ) { return this.generateTopUV( geometry, extrudedShape, extrudeOptions, indexA, indexB, indexC ); }, generateSideWallUV: function( geometry, extrudedShape, wallContour, extrudeOptions, indexA, indexB, indexC, indexD, stepIndex, stepsLength, contourIndex1, contourIndex2 ) { var ax = geometry.vertices[ indexA ].x, ay = geometry.vertices[ indexA ].y, az = geometry.vertices[ indexA ].z, bx = geometry.vertices[ indexB ].x, by = geometry.vertices[ indexB ].y, bz = geometry.vertices[ indexB ].z, cx = geometry.vertices[ indexC ].x, cy = geometry.vertices[ indexC ].y, cz = geometry.vertices[ indexC ].z, dx = geometry.vertices[ indexD ].x, dy = geometry.vertices[ indexD ].y, dz = geometry.vertices[ indexD ].z; if ( Math.abs( ay - by ) < 0.01 ) { return [ new THREE.Vector2( ax, 1 - az ), new THREE.Vector2( bx, 1 - bz ), new THREE.Vector2( cx, 1 - cz ), new THREE.Vector2( dx, 1 - dz ) ]; } else { return [ new THREE.Vector2( ay, 1 - az ), new THREE.Vector2( by, 1 - bz ), new THREE.Vector2( cy, 1 - cz ), new THREE.Vector2( dy, 1 - dz ) ]; } } }; THREE.ExtrudeGeometry.__v1 = new THREE.Vector2(); THREE.ExtrudeGeometry.__v2 = new THREE.Vector2(); THREE.ExtrudeGeometry.__v3 = new THREE.Vector2(); THREE.ExtrudeGeometry.__v4 = new THREE.Vector2(); THREE.ExtrudeGeometry.__v5 = new THREE.Vector2(); THREE.ExtrudeGeometry.__v6 = new THREE.Vector2(); /** * @author jonobr1 / http://jonobr1.com * * Creates a one-sided polygonal geometry from a path shape. Similar to * ExtrudeGeometry. * * parameters = { * * curveSegments: <int>, // number of points on the curves. NOT USED AT THE MOMENT. * * material: <int> // material index for front and back faces * uvGenerator: <Object> // object that provides UV generator functions * * } **/ THREE.ShapeGeometry = function ( shapes, options ) { THREE.Geometry.call( this ); if ( shapes instanceof Array === false ) shapes = [ shapes ]; this.shapebb = shapes[ shapes.length - 1 ].getBoundingBox(); this.addShapeList( shapes, options ); this.computeCentroids(); this.computeFaceNormals(); }; THREE.ShapeGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * Add an array of shapes to THREE.ShapeGeometry. */ THREE.ShapeGeometry.prototype.addShapeList = function ( shapes, options ) { for ( var i = 0, l = shapes.length; i < l; i++ ) { this.addShape( shapes[ i ], options ); } return this; }; /** * Adds a shape to THREE.ShapeGeometry, based on THREE.ExtrudeGeometry. */ THREE.ShapeGeometry.prototype.addShape = function ( shape, options ) { if ( options === undefined ) options = {}; var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12; var material = options.material; var uvgen = options.UVGenerator === undefined ? THREE.ExtrudeGeometry.WorldUVGenerator : options.UVGenerator; var shapebb = this.shapebb; // var i, l, hole, s; var shapesOffset = this.vertices.length; var shapePoints = shape.extractPoints( curveSegments ); var vertices = shapePoints.shape; var holes = shapePoints.holes; var reverse = !THREE.Shape.Utils.isClockWise( vertices ); if ( reverse ) { vertices = vertices.reverse(); // Maybe we should also check if holes are in the opposite direction, just to be safe... for ( i = 0, l = holes.length; i < l; i++ ) { hole = holes[ i ]; if ( THREE.Shape.Utils.isClockWise( hole ) ) { holes[ i ] = hole.reverse(); } } reverse = false; } var faces = THREE.Shape.Utils.triangulateShape( vertices, holes ); // Vertices var contour = vertices; for ( i = 0, l = holes.length; i < l; i++ ) { hole = holes[ i ]; vertices = vertices.concat( hole ); } // var vert, vlen = vertices.length; var face, flen = faces.length; var cont, clen = contour.length; for ( i = 0; i < vlen; i++ ) { vert = vertices[ i ]; this.vertices.push( new THREE.Vector3( vert.x, vert.y, 0 ) ); } for ( i = 0; i < flen; i++ ) { face = faces[ i ]; var a = face[ 0 ] + shapesOffset; var b = face[ 1 ] + shapesOffset; var c = face[ 2 ] + shapesOffset; this.faces.push( new THREE.Face3( a, b, c, null, null, material ) ); this.faceVertexUvs[ 0 ].push( uvgen.generateBottomUV( this, shape, options, a, b, c ) ); } }; /** * @author astrodud / http://astrodud.isgreat.org/ * @author zz85 / https://github.com/zz85 * @author bhouston / http://exocortex.com */ // points - to create a closed torus, one must use a set of points // like so: [ a, b, c, d, a ], see first is the same as last. // segments - the number of circumference segments to create // phiStart - the starting radian // phiLength - the radian (0 to 2*PI) range of the lathed section // 2*pi is a closed lathe, less than 2PI is a portion. THREE.LatheGeometry = function ( points, segments, phiStart, phiLength ) { THREE.Geometry.call( this ); segments = segments || 12; phiStart = phiStart || 0; phiLength = phiLength || 2 * Math.PI; var inversePointLength = 1.0 / ( points.length - 1 ); var inverseSegments = 1.0 / segments; for ( var i = 0, il = segments; i <= il; i ++ ) { var phi = phiStart + i * inverseSegments * phiLength; var c = Math.cos( phi ), s = Math.sin( phi ); for ( var j = 0, jl = points.length; j < jl; j ++ ) { var pt = points[ j ]; var vertex = new THREE.Vector3(); vertex.x = c * pt.x - s * pt.y; vertex.y = s * pt.x + c * pt.y; vertex.z = pt.z; this.vertices.push( vertex ); } } var np = points.length; for ( var i = 0, il = segments; i < il; i ++ ) { for ( var j = 0, jl = points.length - 1; j < jl; j ++ ) { var base = j + np * i; var a = base; var b = base + np; var c = base + 1 + np; var d = base + 1; var u0 = i * inverseSegments; var v0 = j * inversePointLength; var u1 = u0 + inverseSegments; var v1 = v0 + inversePointLength; this.faces.push( new THREE.Face3( a, b, d ) ); this.faceVertexUvs[ 0 ].push( [ new THREE.Vector2( u0, v0 ), new THREE.Vector2( u1, v0 ), new THREE.Vector2( u0, v1 ) ] ); this.faces.push( new THREE.Face3( b, c, d ) ); this.faceVertexUvs[ 0 ].push( [ new THREE.Vector2( u1, v0 ), new THREE.Vector2( u1, v1 ), new THREE.Vector2( u0, v1 ) ] ); } } this.mergeVertices(); this.computeCentroids(); this.computeFaceNormals(); this.computeVertexNormals(); }; THREE.LatheGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author mrdoob / http://mrdoob.com/ * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Plane.as */ THREE.PlaneGeometry = function ( width, height, widthSegments, heightSegments ) { THREE.Geometry.call( this ); this.width = width; this.height = height; this.widthSegments = widthSegments || 1; this.heightSegments = heightSegments || 1; var ix, iz; var width_half = width / 2; var height_half = height / 2; var gridX = this.widthSegments; var gridZ = this.heightSegments; var gridX1 = gridX + 1; var gridZ1 = gridZ + 1; var segment_width = this.width / gridX; var segment_height = this.height / gridZ; var normal = new THREE.Vector3( 0, 0, 1 ); for ( iz = 0; iz < gridZ1; iz ++ ) { for ( ix = 0; ix < gridX1; ix ++ ) { var x = ix * segment_width - width_half; var y = iz * segment_height - height_half; this.vertices.push( new THREE.Vector3( x, - y, 0 ) ); } } for ( iz = 0; iz < gridZ; iz ++ ) { for ( ix = 0; ix < gridX; ix ++ ) { var a = ix + gridX1 * iz; var b = ix + gridX1 * ( iz + 1 ); var c = ( ix + 1 ) + gridX1 * ( iz + 1 ); var d = ( ix + 1 ) + gridX1 * iz; var uva = new THREE.Vector2( ix / gridX, 1 - iz / gridZ ); var uvb = new THREE.Vector2( ix / gridX, 1 - ( iz + 1 ) / gridZ ); var uvc = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - ( iz + 1 ) / gridZ ); var uvd = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - iz / gridZ ); var face = new THREE.Face3( a, b, d ); face.normal.copy( normal ); face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() ); this.faces.push( face ); this.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] ); face = new THREE.Face3( b, c, d ); face.normal.copy( normal ); face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() ); this.faces.push( face ); this.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] ); } } this.computeCentroids(); }; THREE.PlaneGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author Kaleb Murphy */ THREE.RingGeometry = function ( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) { THREE.Geometry.call( this ); innerRadius = innerRadius || 0; outerRadius = outerRadius || 50; thetaStart = thetaStart !== undefined ? thetaStart : 0; thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2; thetaSegments = thetaSegments !== undefined ? Math.max( 3, thetaSegments ) : 8; phiSegments = phiSegments !== undefined ? Math.max( 3, phiSegments ) : 8; var i, o, uvs = [], radius = innerRadius, radiusStep = ( ( outerRadius - innerRadius ) / phiSegments ); for ( i = 0; i <= phiSegments; i ++ ) { // concentric circles inside ring for ( o = 0; o <= thetaSegments; o ++ ) { // number of segments per circle var vertex = new THREE.Vector3(); var segment = thetaStart + o / thetaSegments * thetaLength; vertex.x = radius * Math.cos( segment ); vertex.y = radius * Math.sin( segment ); this.vertices.push( vertex ); uvs.push( new THREE.Vector2( ( vertex.x / outerRadius + 1 ) / 2, ( vertex.y / outerRadius + 1 ) / 2 ) ); } radius += radiusStep; } var n = new THREE.Vector3( 0, 0, 1 ); for ( i = 0; i < phiSegments; i ++ ) { // concentric circles inside ring var thetaSegment = i * thetaSegments; for ( o = 0; o <= thetaSegments; o ++ ) { // number of segments per circle var segment = o + thetaSegment; var v1 = segment + i; var v2 = segment + thetaSegments + i; var v3 = segment + thetaSegments + 1 + i; this.faces.push( new THREE.Face3( v1, v2, v3, [ n.clone(), n.clone(), n.clone() ] ) ); this.faceVertexUvs[ 0 ].push( [ uvs[ v1 ].clone(), uvs[ v2 ].clone(), uvs[ v3 ].clone() ]); v1 = segment + i; v2 = segment + thetaSegments + 1 + i; v3 = segment + 1 + i; this.faces.push( new THREE.Face3( v1, v2, v3, [ n.clone(), n.clone(), n.clone() ] ) ); this.faceVertexUvs[ 0 ].push( [ uvs[ v1 ].clone(), uvs[ v2 ].clone(), uvs[ v3 ].clone() ]); } } this.computeCentroids(); this.computeFaceNormals(); this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius ); }; THREE.RingGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author mrdoob / http://mrdoob.com/ */ THREE.SphereGeometry = function ( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) { THREE.Geometry.call( this ); this.radius = radius = radius || 50; this.widthSegments = widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 ); this.heightSegments = heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 ); this.phiStart = phiStart = phiStart !== undefined ? phiStart : 0; this.phiLength = phiLength = phiLength !== undefined ? phiLength : Math.PI * 2; this.thetaStart = thetaStart = thetaStart !== undefined ? thetaStart : 0; this.thetaLength = thetaLength = thetaLength !== undefined ? thetaLength : Math.PI; var x, y, vertices = [], uvs = []; for ( y = 0; y <= heightSegments; y ++ ) { var verticesRow = []; var uvsRow = []; for ( x = 0; x <= widthSegments; x ++ ) { var u = x / widthSegments; var v = y / heightSegments; var vertex = new THREE.Vector3(); vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength ); vertex.y = radius * Math.cos( thetaStart + v * thetaLength ); vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength ); this.vertices.push( vertex ); verticesRow.push( this.vertices.length - 1 ); uvsRow.push( new THREE.Vector2( u, 1 - v ) ); } vertices.push( verticesRow ); uvs.push( uvsRow ); } for ( y = 0; y < this.heightSegments; y ++ ) { for ( x = 0; x < this.widthSegments; x ++ ) { var v1 = vertices[ y ][ x + 1 ]; var v2 = vertices[ y ][ x ]; var v3 = vertices[ y + 1 ][ x ]; var v4 = vertices[ y + 1 ][ x + 1 ]; var n1 = this.vertices[ v1 ].clone().normalize(); var n2 = this.vertices[ v2 ].clone().normalize(); var n3 = this.vertices[ v3 ].clone().normalize(); var n4 = this.vertices[ v4 ].clone().normalize(); var uv1 = uvs[ y ][ x + 1 ].clone(); var uv2 = uvs[ y ][ x ].clone(); var uv3 = uvs[ y + 1 ][ x ].clone(); var uv4 = uvs[ y + 1 ][ x + 1 ].clone(); if ( Math.abs( this.vertices[ v1 ].y ) === this.radius ) { uv1.x = ( uv1.x + uv2.x ) / 2; this.faces.push( new THREE.Face3( v1, v3, v4, [ n1, n3, n4 ] ) ); this.faceVertexUvs[ 0 ].push( [ uv1, uv3, uv4 ] ); } else if ( Math.abs( this.vertices[ v3 ].y ) === this.radius ) { uv3.x = ( uv3.x + uv4.x ) / 2; this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) ); this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] ); } else { this.faces.push( new THREE.Face3( v1, v2, v4, [ n1, n2, n4 ] ) ); this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv4 ] ); this.faces.push( new THREE.Face3( v2, v3, v4, [ n2.clone(), n3, n4.clone() ] ) ); this.faceVertexUvs[ 0 ].push( [ uv2.clone(), uv3, uv4.clone() ] ); } } } this.computeCentroids(); this.computeFaceNormals(); this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius ); }; THREE.SphereGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author zz85 / http://www.lab4games.net/zz85/blog * @author alteredq / http://alteredqualia.com/ * * For creating 3D text geometry in three.js * * Text = 3D Text * * parameters = { * size: <float>, // size of the text * height: <float>, // thickness to extrude text * curveSegments: <int>, // number of points on the curves * * font: <string>, // font name * weight: <string>, // font weight (normal, bold) * style: <string>, // font style (normal, italics) * * bevelEnabled: <bool>, // turn on bevel * bevelThickness: <float>, // how deep into text bevel goes * bevelSize: <float>, // how far from text outline is bevel * } * */ /* Usage Examples // TextGeometry wrapper var text3d = new TextGeometry( text, options ); // Complete manner var textShapes = THREE.FontUtils.generateShapes( text, options ); var text3d = new ExtrudeGeometry( textShapes, options ); */ THREE.TextGeometry = function ( text, parameters ) { parameters = parameters || {}; var textShapes = THREE.FontUtils.generateShapes( text, parameters ); // translate parameters to ExtrudeGeometry API parameters.amount = parameters.height !== undefined ? parameters.height : 50; // defaults if ( parameters.bevelThickness === undefined ) parameters.bevelThickness = 10; if ( parameters.bevelSize === undefined ) parameters.bevelSize = 8; if ( parameters.bevelEnabled === undefined ) parameters.bevelEnabled = false; THREE.ExtrudeGeometry.call( this, textShapes, parameters ); }; THREE.TextGeometry.prototype = Object.create( THREE.ExtrudeGeometry.prototype ); /** * @author oosmoxiecode * @author mrdoob / http://mrdoob.com/ * based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3DLite/src/away3dlite/primitives/Torus.as?r=2888 */ THREE.TorusGeometry = function ( radius, tube, radialSegments, tubularSegments, arc ) { THREE.Geometry.call( this ); var scope = this; this.radius = radius || 100; this.tube = tube || 40; this.radialSegments = radialSegments || 8; this.tubularSegments = tubularSegments || 6; this.arc = arc || Math.PI * 2; var center = new THREE.Vector3(), uvs = [], normals = []; for ( var j = 0; j <= this.radialSegments; j ++ ) { for ( var i = 0; i <= this.tubularSegments; i ++ ) { var u = i / this.tubularSegments * this.arc; var v = j / this.radialSegments * Math.PI * 2; center.x = this.radius * Math.cos( u ); center.y = this.radius * Math.sin( u ); var vertex = new THREE.Vector3(); vertex.x = ( this.radius + this.tube * Math.cos( v ) ) * Math.cos( u ); vertex.y = ( this.radius + this.tube * Math.cos( v ) ) * Math.sin( u ); vertex.z = this.tube * Math.sin( v ); this.vertices.push( vertex ); uvs.push( new THREE.Vector2( i / this.tubularSegments, j / this.radialSegments ) ); normals.push( vertex.clone().sub( center ).normalize() ); } } for ( var j = 1; j <= this.radialSegments; j ++ ) { for ( var i = 1; i <= this.tubularSegments; i ++ ) { var a = ( this.tubularSegments + 1 ) * j + i - 1; var b = ( this.tubularSegments + 1 ) * ( j - 1 ) + i - 1; var c = ( this.tubularSegments + 1 ) * ( j - 1 ) + i; var d = ( this.tubularSegments + 1 ) * j + i; var face = new THREE.Face3( a, b, d, [ normals[ a ].clone(), normals[ b ].clone(), normals[ d ].clone() ] ); this.faces.push( face ); this.faceVertexUvs[ 0 ].push( [ uvs[ a ].clone(), uvs[ b ].clone(), uvs[ d ].clone() ] ); face = new THREE.Face3( b, c, d, [ normals[ b ].clone(), normals[ c ].clone(), normals[ d ].clone() ] ); this.faces.push( face ); this.faceVertexUvs[ 0 ].push( [ uvs[ b ].clone(), uvs[ c ].clone(), uvs[ d ].clone() ] ); } } this.computeCentroids(); this.computeFaceNormals(); }; THREE.TorusGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author oosmoxiecode * based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473 */ THREE.TorusKnotGeometry = function ( radius, tube, radialSegments, tubularSegments, p, q, heightScale ) { THREE.Geometry.call( this ); var scope = this; this.radius = radius || 100; this.tube = tube || 40; this.radialSegments = radialSegments || 64; this.tubularSegments = tubularSegments || 8; this.p = p || 2; this.q = q || 3; this.heightScale = heightScale || 1; this.grid = new Array( this.radialSegments ); var tang = new THREE.Vector3(); var n = new THREE.Vector3(); var bitan = new THREE.Vector3(); for ( var i = 0; i < this.radialSegments; ++ i ) { this.grid[ i ] = new Array( this.tubularSegments ); var u = i / this.radialSegments * 2 * this.p * Math.PI; var p1 = getPos( u, this.q, this.p, this.radius, this.heightScale ); var p2 = getPos( u + 0.01, this.q, this.p, this.radius, this.heightScale ); tang.subVectors( p2, p1 ); n.addVectors( p2, p1 ); bitan.crossVectors( tang, n ); n.crossVectors( bitan, tang ); bitan.normalize(); n.normalize(); for ( var j = 0; j < this.tubularSegments; ++ j ) { var v = j / this.tubularSegments * 2 * Math.PI; var cx = - this.tube * Math.cos( v ); // TODO: Hack: Negating it so it faces outside. var cy = this.tube * Math.sin( v ); var pos = new THREE.Vector3(); pos.x = p1.x + cx * n.x + cy * bitan.x; pos.y = p1.y + cx * n.y + cy * bitan.y; pos.z = p1.z + cx * n.z + cy * bitan.z; this.grid[ i ][ j ] = scope.vertices.push( pos ) - 1; } } for ( var i = 0; i < this.radialSegments; ++ i ) { for ( var j = 0; j < this.tubularSegments; ++ j ) { var ip = ( i + 1 ) % this.radialSegments; var jp = ( j + 1 ) % this.tubularSegments; var a = this.grid[ i ][ j ]; var b = this.grid[ ip ][ j ]; var c = this.grid[ ip ][ jp ]; var d = this.grid[ i ][ jp ]; var uva = new THREE.Vector2( i / this.radialSegments, j / this.tubularSegments ); var uvb = new THREE.Vector2( ( i + 1 ) / this.radialSegments, j / this.tubularSegments ); var uvc = new THREE.Vector2( ( i + 1 ) / this.radialSegments, ( j + 1 ) / this.tubularSegments ); var uvd = new THREE.Vector2( i / this.radialSegments, ( j + 1 ) / this.tubularSegments ); this.faces.push( new THREE.Face3( a, b, d ) ); this.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] ); this.faces.push( new THREE.Face3( b, c, d ) ); this.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] ); } } this.computeCentroids(); this.computeFaceNormals(); this.computeVertexNormals(); function getPos( u, in_q, in_p, radius, heightScale ) { var cu = Math.cos( u ); var su = Math.sin( u ); var quOverP = in_q / in_p * u; var cs = Math.cos( quOverP ); var tx = radius * ( 2 + cs ) * 0.5 * cu; var ty = radius * ( 2 + cs ) * su * 0.5; var tz = heightScale * radius * Math.sin( quOverP ) * 0.5; return new THREE.Vector3( tx, ty, tz ); } }; THREE.TorusKnotGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author WestLangley / https://github.com/WestLangley * @author zz85 / https://github.com/zz85 * @author miningold / https://github.com/miningold * * Modified from the TorusKnotGeometry by @oosmoxiecode * * Creates a tube which extrudes along a 3d spline * * Uses parallel transport frames as described in * http://www.cs.indiana.edu/pub/techreports/TR425.pdf */ THREE.TubeGeometry = function( path, segments, radius, radialSegments, closed ) { THREE.Geometry.call( this ); this.path = path; this.segments = segments || 64; this.radius = radius || 1; this.radialSegments = radialSegments || 8; this.closed = closed || false; this.grid = []; var scope = this, tangent, normal, binormal, numpoints = this.segments + 1, x, y, z, tx, ty, tz, u, v, cx, cy, pos, pos2 = new THREE.Vector3(), i, j, ip, jp, a, b, c, d, uva, uvb, uvc, uvd; var frames = new THREE.TubeGeometry.FrenetFrames( this.path, this.segments, this.closed ), tangents = frames.tangents, normals = frames.normals, binormals = frames.binormals; // proxy internals this.tangents = tangents; this.normals = normals; this.binormals = binormals; function vert( x, y, z ) { return scope.vertices.push( new THREE.Vector3( x, y, z ) ) - 1; } // consruct the grid for ( i = 0; i < numpoints; i++ ) { this.grid[ i ] = []; u = i / ( numpoints - 1 ); pos = path.getPointAt( u ); tangent = tangents[ i ]; normal = normals[ i ]; binormal = binormals[ i ]; for ( j = 0; j < this.radialSegments; j++ ) { v = j / this.radialSegments * 2 * Math.PI; cx = -this.radius * Math.cos( v ); // TODO: Hack: Negating it so it faces outside. cy = this.radius * Math.sin( v ); pos2.copy( pos ); pos2.x += cx * normal.x + cy * binormal.x; pos2.y += cx * normal.y + cy * binormal.y; pos2.z += cx * normal.z + cy * binormal.z; this.grid[ i ][ j ] = vert( pos2.x, pos2.y, pos2.z ); } } // construct the mesh for ( i = 0; i < this.segments; i++ ) { for ( j = 0; j < this.radialSegments; j++ ) { ip = ( this.closed ) ? (i + 1) % this.segments : i + 1; jp = (j + 1) % this.radialSegments; a = this.grid[ i ][ j ]; // *** NOT NECESSARILY PLANAR ! *** b = this.grid[ ip ][ j ]; c = this.grid[ ip ][ jp ]; d = this.grid[ i ][ jp ]; uva = new THREE.Vector2( i / this.segments, j / this.radialSegments ); uvb = new THREE.Vector2( ( i + 1 ) / this.segments, j / this.radialSegments ); uvc = new THREE.Vector2( ( i + 1 ) / this.segments, ( j + 1 ) / this.radialSegments ); uvd = new THREE.Vector2( i / this.segments, ( j + 1 ) / this.radialSegments ); this.faces.push( new THREE.Face3( a, b, d ) ); this.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] ); this.faces.push( new THREE.Face3( b, c, d ) ); this.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] ); } } this.computeCentroids(); this.computeFaceNormals(); this.computeVertexNormals(); }; THREE.TubeGeometry.prototype = Object.create( THREE.Geometry.prototype ); // For computing of Frenet frames, exposing the tangents, normals and binormals the spline THREE.TubeGeometry.FrenetFrames = function(path, segments, closed) { var tangent = new THREE.Vector3(), normal = new THREE.Vector3(), binormal = new THREE.Vector3(), tangents = [], normals = [], binormals = [], vec = new THREE.Vector3(), mat = new THREE.Matrix4(), numpoints = segments + 1, theta, epsilon = 0.0001, smallest, tx, ty, tz, i, u, v; // expose internals this.tangents = tangents; this.normals = normals; this.binormals = binormals; // compute the tangent vectors for each segment on the path for ( i = 0; i < numpoints; i++ ) { u = i / ( numpoints - 1 ); tangents[ i ] = path.getTangentAt( u ); tangents[ i ].normalize(); } initialNormal3(); function initialNormal1(lastBinormal) { // fixed start binormal. Has dangers of 0 vectors normals[ 0 ] = new THREE.Vector3(); binormals[ 0 ] = new THREE.Vector3(); if (lastBinormal===undefined) lastBinormal = new THREE.Vector3( 0, 0, 1 ); normals[ 0 ].crossVectors( lastBinormal, tangents[ 0 ] ).normalize(); binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] ).normalize(); } function initialNormal2() { // This uses the Frenet-Serret formula for deriving binormal var t2 = path.getTangentAt( epsilon ); normals[ 0 ] = new THREE.Vector3().subVectors( t2, tangents[ 0 ] ).normalize(); binormals[ 0 ] = new THREE.Vector3().crossVectors( tangents[ 0 ], normals[ 0 ] ); normals[ 0 ].crossVectors( binormals[ 0 ], tangents[ 0 ] ).normalize(); // last binormal x tangent binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] ).normalize(); } function initialNormal3() { // select an initial normal vector perpenicular to the first tangent vector, // and in the direction of the smallest tangent xyz component normals[ 0 ] = new THREE.Vector3(); binormals[ 0 ] = new THREE.Vector3(); smallest = Number.MAX_VALUE; tx = Math.abs( tangents[ 0 ].x ); ty = Math.abs( tangents[ 0 ].y ); tz = Math.abs( tangents[ 0 ].z ); if ( tx <= smallest ) { smallest = tx; normal.set( 1, 0, 0 ); } if ( ty <= smallest ) { smallest = ty; normal.set( 0, 1, 0 ); } if ( tz <= smallest ) { normal.set( 0, 0, 1 ); } vec.crossVectors( tangents[ 0 ], normal ).normalize(); normals[ 0 ].crossVectors( tangents[ 0 ], vec ); binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] ); } // compute the slowly-varying normal and binormal vectors for each segment on the path for ( i = 1; i < numpoints; i++ ) { normals[ i ] = normals[ i-1 ].clone(); binormals[ i ] = binormals[ i-1 ].clone(); vec.crossVectors( tangents[ i-1 ], tangents[ i ] ); if ( vec.length() > epsilon ) { vec.normalize(); theta = Math.acos( THREE.Math.clamp( tangents[ i-1 ].dot( tangents[ i ] ), -1, 1 ) ); // clamp for floating pt errors normals[ i ].applyMatrix4( mat.makeRotationAxis( vec, theta ) ); } binormals[ i ].crossVectors( tangents[ i ], normals[ i ] ); } // if the curve is closed, postprocess the vectors so the first and last normal vectors are the same if ( closed ) { theta = Math.acos( THREE.Math.clamp( normals[ 0 ].dot( normals[ numpoints-1 ] ), -1, 1 ) ); theta /= ( numpoints - 1 ); if ( tangents[ 0 ].dot( vec.crossVectors( normals[ 0 ], normals[ numpoints-1 ] ) ) > 0 ) { theta = -theta; } for ( i = 1; i < numpoints; i++ ) { // twist a little... normals[ i ].applyMatrix4( mat.makeRotationAxis( tangents[ i ], theta * i ) ); binormals[ i ].crossVectors( tangents[ i ], normals[ i ] ); } } }; /** * @author clockworkgeek / https://github.com/clockworkgeek * @author timothypratley / https://github.com/timothypratley * @author WestLangley / http://github.com/WestLangley */ THREE.PolyhedronGeometry = function ( vertices, faces, radius, detail ) { THREE.Geometry.call( this ); radius = radius || 1; detail = detail || 0; var that = this; for ( var i = 0, l = vertices.length; i < l; i ++ ) { prepare( new THREE.Vector3( vertices[ i ][ 0 ], vertices[ i ][ 1 ], vertices[ i ][ 2 ] ) ); } var midpoints = [], p = this.vertices; var f = []; for ( var i = 0, l = faces.length; i < l; i ++ ) { var v1 = p[ faces[ i ][ 0 ] ]; var v2 = p[ faces[ i ][ 1 ] ]; var v3 = p[ faces[ i ][ 2 ] ]; f[ i ] = new THREE.Face3( v1.index, v2.index, v3.index, [ v1.clone(), v2.clone(), v3.clone() ] ); } for ( var i = 0, l = f.length; i < l; i ++ ) { subdivide(f[ i ], detail); } // Handle case when face straddles the seam for ( var i = 0, l = this.faceVertexUvs[ 0 ].length; i < l; i ++ ) { var uvs = this.faceVertexUvs[ 0 ][ i ]; var x0 = uvs[ 0 ].x; var x1 = uvs[ 1 ].x; var x2 = uvs[ 2 ].x; var max = Math.max( x0, Math.max( x1, x2 ) ); var min = Math.min( x0, Math.min( x1, x2 ) ); if ( max > 0.9 && min < 0.1 ) { // 0.9 is somewhat arbitrary if ( x0 < 0.2 ) uvs[ 0 ].x += 1; if ( x1 < 0.2 ) uvs[ 1 ].x += 1; if ( x2 < 0.2 ) uvs[ 2 ].x += 1; } } // Apply radius for ( var i = 0, l = this.vertices.length; i < l; i ++ ) { this.vertices[ i ].multiplyScalar( radius ); } // Merge vertices this.mergeVertices(); this.computeCentroids(); this.computeFaceNormals(); this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius ); // Project vector onto sphere's surface function prepare( vector ) { var vertex = vector.normalize().clone(); vertex.index = that.vertices.push( vertex ) - 1; // Texture coords are equivalent to map coords, calculate angle and convert to fraction of a circle. var u = azimuth( vector ) / 2 / Math.PI + 0.5; var v = inclination( vector ) / Math.PI + 0.5; vertex.uv = new THREE.Vector2( u, 1 - v ); return vertex; } // Approximate a curved face with recursively sub-divided triangles. function make( v1, v2, v3 ) { var face = new THREE.Face3( v1.index, v2.index, v3.index, [ v1.clone(), v2.clone(), v3.clone() ] ); face.centroid.add( v1 ).add( v2 ).add( v3 ).divideScalar( 3 ); that.faces.push( face ); var azi = azimuth( face.centroid ); that.faceVertexUvs[ 0 ].push( [ correctUV( v1.uv, v1, azi ), correctUV( v2.uv, v2, azi ), correctUV( v3.uv, v3, azi ) ] ); } // Analytically subdivide a face to the required detail level. function subdivide(face, detail ) { var cols = Math.pow(2, detail); var cells = Math.pow(4, detail); var a = prepare( that.vertices[ face.a ] ); var b = prepare( that.vertices[ face.b ] ); var c = prepare( that.vertices[ face.c ] ); var v = []; // Construct all of the vertices for this subdivision. for ( var i = 0 ; i <= cols; i ++ ) { v[ i ] = []; var aj = prepare( a.clone().lerp( c, i / cols ) ); var bj = prepare( b.clone().lerp( c, i / cols ) ); var rows = cols - i; for ( var j = 0; j <= rows; j ++) { if ( j == 0 && i == cols ) { v[ i ][ j ] = aj; } else { v[ i ][ j ] = prepare( aj.clone().lerp( bj, j / rows ) ); } } } // Construct all of the faces. for ( var i = 0; i < cols ; i ++ ) { for ( var j = 0; j < 2 * (cols - i) - 1; j ++ ) { var k = Math.floor( j / 2 ); if ( j % 2 == 0 ) { make( v[ i ][ k + 1], v[ i + 1 ][ k ], v[ i ][ k ] ); } else { make( v[ i ][ k + 1 ], v[ i + 1][ k + 1], v[ i + 1 ][ k ] ); } } } } // Angle around the Y axis, counter-clockwise when looking from above. function azimuth( vector ) { return Math.atan2( vector.z, -vector.x ); } // Angle above the XZ plane. function inclination( vector ) { return Math.atan2( -vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) ); } // Texture fixing helper. Spheres have some odd behaviours. function correctUV( uv, vector, azimuth ) { if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) uv = new THREE.Vector2( uv.x - 1, uv.y ); if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) uv = new THREE.Vector2( azimuth / 2 / Math.PI + 0.5, uv.y ); return uv.clone(); } }; THREE.PolyhedronGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author timothypratley / https://github.com/timothypratley */ THREE.IcosahedronGeometry = function ( radius, detail ) { this.radius = radius; this.detail = detail; var t = ( 1 + Math.sqrt( 5 ) ) / 2; var vertices = [ [ -1, t, 0 ], [ 1, t, 0 ], [ -1, -t, 0 ], [ 1, -t, 0 ], [ 0, -1, t ], [ 0, 1, t ], [ 0, -1, -t ], [ 0, 1, -t ], [ t, 0, -1 ], [ t, 0, 1 ], [ -t, 0, -1 ], [ -t, 0, 1 ] ]; var faces = [ [ 0, 11, 5 ], [ 0, 5, 1 ], [ 0, 1, 7 ], [ 0, 7, 10 ], [ 0, 10, 11 ], [ 1, 5, 9 ], [ 5, 11, 4 ], [ 11, 10, 2 ], [ 10, 7, 6 ], [ 7, 1, 8 ], [ 3, 9, 4 ], [ 3, 4, 2 ], [ 3, 2, 6 ], [ 3, 6, 8 ], [ 3, 8, 9 ], [ 4, 9, 5 ], [ 2, 4, 11 ], [ 6, 2, 10 ], [ 8, 6, 7 ], [ 9, 8, 1 ] ]; THREE.PolyhedronGeometry.call( this, vertices, faces, radius, detail ); }; THREE.IcosahedronGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author timothypratley / https://github.com/timothypratley */ THREE.OctahedronGeometry = function ( radius, detail ) { var vertices = [ [ 1, 0, 0 ], [ -1, 0, 0 ], [ 0, 1, 0 ], [ 0, -1, 0 ], [ 0, 0, 1 ], [ 0, 0, -1 ] ]; var faces = [ [ 0, 2, 4 ], [ 0, 4, 3 ], [ 0, 3, 5 ], [ 0, 5, 2 ], [ 1, 2, 5 ], [ 1, 5, 3 ], [ 1, 3, 4 ], [ 1, 4, 2 ] ]; THREE.PolyhedronGeometry.call( this, vertices, faces, radius, detail ); }; THREE.OctahedronGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author timothypratley / https://github.com/timothypratley */ THREE.TetrahedronGeometry = function ( radius, detail ) { var vertices = [ [ 1, 1, 1 ], [ -1, -1, 1 ], [ -1, 1, -1 ], [ 1, -1, -1 ] ]; var faces = [ [ 2, 1, 0 ], [ 0, 3, 2 ], [ 1, 3, 0 ], [ 2, 3, 1 ] ]; THREE.PolyhedronGeometry.call( this, vertices, faces, radius, detail ); }; THREE.TetrahedronGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author zz85 / https://github.com/zz85 * Parametric Surfaces Geometry * based on the brilliant article by @prideout http://prideout.net/blog/?p=44 * * new THREE.ParametricGeometry( parametricFunction, uSegments, ySegements ); * */ THREE.ParametricGeometry = function ( func, slices, stacks ) { THREE.Geometry.call( this ); var verts = this.vertices; var faces = this.faces; var uvs = this.faceVertexUvs[ 0 ]; var i, il, j, p; var u, v; var stackCount = stacks + 1; var sliceCount = slices + 1; for ( i = 0; i <= stacks; i ++ ) { v = i / stacks; for ( j = 0; j <= slices; j ++ ) { u = j / slices; p = func( u, v ); verts.push( p ); } } var a, b, c, d; var uva, uvb, uvc, uvd; for ( i = 0; i < stacks; i ++ ) { for ( j = 0; j < slices; j ++ ) { a = i * sliceCount + j; b = i * sliceCount + j + 1; c = (i + 1) * sliceCount + j + 1; d = (i + 1) * sliceCount + j; uva = new THREE.Vector2( j / slices, i / stacks ); uvb = new THREE.Vector2( ( j + 1 ) / slices, i / stacks ); uvc = new THREE.Vector2( ( j + 1 ) / slices, ( i + 1 ) / stacks ); uvd = new THREE.Vector2( j / slices, ( i + 1 ) / stacks ); faces.push( new THREE.Face3( a, b, d ) ); uvs.push( [ uva, uvb, uvd ] ); faces.push( new THREE.Face3( b, c, d ) ); uvs.push( [ uvb.clone(), uvc, uvd.clone() ] ); } } // console.log(this); // magic bullet // var diff = this.mergeVertices(); // console.log('removed ', diff, ' vertices by merging'); this.computeCentroids(); this.computeFaceNormals(); this.computeVertexNormals(); }; THREE.ParametricGeometry.prototype = Object.create( THREE.Geometry.prototype ); /** * @author sroucheray / http://sroucheray.org/ * @author mrdoob / http://mrdoob.com/ */ THREE.AxisHelper = function ( size ) { size = size || 1; var geometry = new THREE.Geometry(); geometry.vertices.push( new THREE.Vector3(), new THREE.Vector3( size, 0, 0 ), new THREE.Vector3(), new THREE.Vector3( 0, size, 0 ), new THREE.Vector3(), new THREE.Vector3( 0, 0, size ) ); geometry.colors.push( new THREE.Color( 0xff0000 ), new THREE.Color( 0xffaa00 ), new THREE.Color( 0x00ff00 ), new THREE.Color( 0xaaff00 ), new THREE.Color( 0x0000ff ), new THREE.Color( 0x00aaff ) ); var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } ); THREE.Line.call( this, geometry, material, THREE.LinePieces ); }; THREE.AxisHelper.prototype = Object.create( THREE.Line.prototype ); /** * @author WestLangley / http://github.com/WestLangley * @author zz85 / http://github.com/zz85 * @author bhouston / http://exocortex.com * * Creates an arrow for visualizing directions * * Parameters: * dir - Vector3 * origin - Vector3 * length - Number * hex - color in hex value * headLength - Number * headWidth - Number */ THREE.ArrowHelper = function ( dir, origin, length, hex, headLength, headWidth ) { // dir is assumed to be normalized THREE.Object3D.call( this ); if ( hex === undefined ) hex = 0xffff00; if ( length === undefined ) length = 1; if ( headLength === undefined ) headLength = 0.2 * length; if ( headWidth === undefined ) headWidth = 0.2 * headLength; this.position = origin; var lineGeometry = new THREE.Geometry(); lineGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ) ); lineGeometry.vertices.push( new THREE.Vector3( 0, 1, 0 ) ); this.line = new THREE.Line( lineGeometry, new THREE.LineBasicMaterial( { color: hex } ) ); this.line.matrixAutoUpdate = false; this.add( this.line ); var coneGeometry = new THREE.CylinderGeometry( 0, 0.5, 1, 5, 1 ); coneGeometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, - 0.5, 0 ) ); this.cone = new THREE.Mesh( coneGeometry, new THREE.MeshBasicMaterial( { color: hex } ) ); this.cone.matrixAutoUpdate = false; this.add( this.cone ); this.setDirection( dir ); this.setLength( length, headLength, headWidth ); }; THREE.ArrowHelper.prototype = Object.create( THREE.Object3D.prototype ); THREE.ArrowHelper.prototype.setDirection = function () { var axis = new THREE.Vector3(); var radians; return function ( dir ) { // dir is assumed to be normalized if ( dir.y > 0.99999 ) { this.quaternion.set( 0, 0, 0, 1 ); } else if ( dir.y < - 0.99999 ) { this.quaternion.set( 1, 0, 0, 0 ); } else { axis.set( dir.z, 0, - dir.x ).normalize(); radians = Math.acos( dir.y ); this.quaternion.setFromAxisAngle( axis, radians ); } }; }(); THREE.ArrowHelper.prototype.setLength = function ( length, headLength, headWidth ) { if ( headLength === undefined ) headLength = 0.2 * length; if ( headWidth === undefined ) headWidth = 0.2 * headLength; this.line.scale.set( 1, length, 1 ); this.line.updateMatrix(); this.cone.scale.set( headWidth, headLength, headWidth ); this.cone.position.y = length; this.cone.updateMatrix(); }; THREE.ArrowHelper.prototype.setColor = function ( hex ) { this.line.material.color.setHex( hex ); this.cone.material.color.setHex( hex ); }; /** * @author mrdoob / http://mrdoob.com/ */ THREE.BoxHelper = function ( object ) { // 5____4 // 1/___0/| // | 6__|_7 // 2/___3/ var vertices = [ new THREE.Vector3( 1, 1, 1 ), new THREE.Vector3( - 1, 1, 1 ), new THREE.Vector3( - 1, - 1, 1 ), new THREE.Vector3( 1, - 1, 1 ), new THREE.Vector3( 1, 1, - 1 ), new THREE.Vector3( - 1, 1, - 1 ), new THREE.Vector3( - 1, - 1, - 1 ), new THREE.Vector3( 1, - 1, - 1 ) ]; this.vertices = vertices; // TODO: Wouldn't be nice if Line had .segments? var geometry = new THREE.Geometry(); geometry.vertices.push( vertices[ 0 ], vertices[ 1 ], vertices[ 1 ], vertices[ 2 ], vertices[ 2 ], vertices[ 3 ], vertices[ 3 ], vertices[ 0 ], vertices[ 4 ], vertices[ 5 ], vertices[ 5 ], vertices[ 6 ], vertices[ 6 ], vertices[ 7 ], vertices[ 7 ], vertices[ 4 ], vertices[ 0 ], vertices[ 4 ], vertices[ 1 ], vertices[ 5 ], vertices[ 2 ], vertices[ 6 ], vertices[ 3 ], vertices[ 7 ] ); THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ), THREE.LinePieces ); if ( object !== undefined ) { this.update( object ); } }; THREE.BoxHelper.prototype = Object.create( THREE.Line.prototype ); THREE.BoxHelper.prototype.update = function ( object ) { var geometry = object.geometry; if ( geometry.boundingBox === null ) { geometry.computeBoundingBox(); } var min = geometry.boundingBox.min; var max = geometry.boundingBox.max; var vertices = this.vertices; vertices[ 0 ].set( max.x, max.y, max.z ); vertices[ 1 ].set( min.x, max.y, max.z ); vertices[ 2 ].set( min.x, min.y, max.z ); vertices[ 3 ].set( max.x, min.y, max.z ); vertices[ 4 ].set( max.x, max.y, min.z ); vertices[ 5 ].set( min.x, max.y, min.z ); vertices[ 6 ].set( min.x, min.y, min.z ); vertices[ 7 ].set( max.x, min.y, min.z ); this.geometry.computeBoundingSphere(); this.geometry.verticesNeedUpdate = true; this.matrixAutoUpdate = false; this.matrixWorld = object.matrixWorld; }; /** * @author WestLangley / http://github.com/WestLangley */ // a helper to show the world-axis-aligned bounding box for an object THREE.BoundingBoxHelper = function ( object, hex ) { var color = ( hex !== undefined ) ? hex : 0x888888; this.object = object; this.box = new THREE.Box3(); THREE.Mesh.call( this, new THREE.BoxGeometry( 1, 1, 1 ), new THREE.MeshBasicMaterial( { color: color, wireframe: true } ) ); }; THREE.BoundingBoxHelper.prototype = Object.create( THREE.Mesh.prototype ); THREE.BoundingBoxHelper.prototype.update = function () { this.box.setFromObject( this.object ); this.box.size( this.scale ); this.box.center( this.position ); }; /** * @author alteredq / http://alteredqualia.com/ * * - shows frustum, line of sight and up of the camera * - suitable for fast updates * - based on frustum visualization in lightgl.js shadowmap example * http://evanw.github.com/lightgl.js/tests/shadowmap.html */ THREE.CameraHelper = function ( camera ) { var geometry = new THREE.Geometry(); var material = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: THREE.FaceColors } ); var pointMap = {}; // colors var hexFrustum = 0xffaa00; var hexCone = 0xff0000; var hexUp = 0x00aaff; var hexTarget = 0xffffff; var hexCross = 0x333333; // near addLine( "n1", "n2", hexFrustum ); addLine( "n2", "n4", hexFrustum ); addLine( "n4", "n3", hexFrustum ); addLine( "n3", "n1", hexFrustum ); // far addLine( "f1", "f2", hexFrustum ); addLine( "f2", "f4", hexFrustum ); addLine( "f4", "f3", hexFrustum ); addLine( "f3", "f1", hexFrustum ); // sides addLine( "n1", "f1", hexFrustum ); addLine( "n2", "f2", hexFrustum ); addLine( "n3", "f3", hexFrustum ); addLine( "n4", "f4", hexFrustum ); // cone addLine( "p", "n1", hexCone ); addLine( "p", "n2", hexCone ); addLine( "p", "n3", hexCone ); addLine( "p", "n4", hexCone ); // up addLine( "u1", "u2", hexUp ); addLine( "u2", "u3", hexUp ); addLine( "u3", "u1", hexUp ); // target addLine( "c", "t", hexTarget ); addLine( "p", "c", hexCross ); // cross addLine( "cn1", "cn2", hexCross ); addLine( "cn3", "cn4", hexCross ); addLine( "cf1", "cf2", hexCross ); addLine( "cf3", "cf4", hexCross ); function addLine( a, b, hex ) { addPoint( a, hex ); addPoint( b, hex ); } function addPoint( id, hex ) { geometry.vertices.push( new THREE.Vector3() ); geometry.colors.push( new THREE.Color( hex ) ); if ( pointMap[ id ] === undefined ) { pointMap[ id ] = []; } pointMap[ id ].push( geometry.vertices.length - 1 ); } THREE.Line.call( this, geometry, material, THREE.LinePieces ); this.camera = camera; this.matrixWorld = camera.matrixWorld; this.matrixAutoUpdate = false; this.pointMap = pointMap; this.update(); }; THREE.CameraHelper.prototype = Object.create( THREE.Line.prototype ); THREE.CameraHelper.prototype.update = function () { var vector = new THREE.Vector3(); var camera = new THREE.Camera(); var projector = new THREE.Projector(); return function () { var scope = this; var w = 1, h = 1; // we need just camera projection matrix // world matrix must be identity camera.projectionMatrix.copy( this.camera.projectionMatrix ); // center / target setPoint( "c", 0, 0, -1 ); setPoint( "t", 0, 0, 1 ); // near setPoint( "n1", -w, -h, -1 ); setPoint( "n2", w, -h, -1 ); setPoint( "n3", -w, h, -1 ); setPoint( "n4", w, h, -1 ); // far setPoint( "f1", -w, -h, 1 ); setPoint( "f2", w, -h, 1 ); setPoint( "f3", -w, h, 1 ); setPoint( "f4", w, h, 1 ); // up setPoint( "u1", w * 0.7, h * 1.1, -1 ); setPoint( "u2", -w * 0.7, h * 1.1, -1 ); setPoint( "u3", 0, h * 2, -1 ); // cross setPoint( "cf1", -w, 0, 1 ); setPoint( "cf2", w, 0, 1 ); setPoint( "cf3", 0, -h, 1 ); setPoint( "cf4", 0, h, 1 ); setPoint( "cn1", -w, 0, -1 ); setPoint( "cn2", w, 0, -1 ); setPoint( "cn3", 0, -h, -1 ); setPoint( "cn4", 0, h, -1 ); function setPoint( point, x, y, z ) { vector.set( x, y, z ); projector.unprojectVector( vector, camera ); var points = scope.pointMap[ point ]; if ( points !== undefined ) { for ( var i = 0, il = points.length; i < il; i ++ ) { scope.geometry.vertices[ points[ i ] ].copy( vector ); } } } this.geometry.verticesNeedUpdate = true; }; }(); /** * @author alteredq / http://alteredqualia.com/ * @author mrdoob / http://mrdoob.com/ * @author WestLangley / http://github.com/WestLangley */ THREE.DirectionalLightHelper = function ( light, size ) { THREE.Object3D.call( this ); this.light = light; this.light.updateMatrixWorld(); this.matrixWorld = light.matrixWorld; this.matrixAutoUpdate = false; size = size || 1; var geometry = new THREE.PlaneGeometry( size, size ); var material = new THREE.MeshBasicMaterial( { wireframe: true, fog: false } ); material.color.copy( this.light.color ).multiplyScalar( this.light.intensity ); this.lightPlane = new THREE.Mesh( geometry, material ); this.add( this.lightPlane ); geometry = new THREE.Geometry(); geometry.vertices.push( new THREE.Vector3() ); geometry.vertices.push( new THREE.Vector3() ); material = new THREE.LineBasicMaterial( { fog: false } ); material.color.copy( this.light.color ).multiplyScalar( this.light.intensity ); this.targetLine = new THREE.Line( geometry, material ); this.add( this.targetLine ); this.update(); }; THREE.DirectionalLightHelper.prototype = Object.create( THREE.Object3D.prototype ); THREE.DirectionalLightHelper.prototype.dispose = function () { this.lightPlane.geometry.dispose(); this.lightPlane.material.dispose(); this.targetLine.geometry.dispose(); this.targetLine.material.dispose(); }; THREE.DirectionalLightHelper.prototype.update = function () { var v1 = new THREE.Vector3(); var v2 = new THREE.Vector3(); var v3 = new THREE.Vector3(); return function () { v1.setFromMatrixPosition( this.light.matrixWorld ); v2.setFromMatrixPosition( this.light.target.matrixWorld ); v3.subVectors( v2, v1 ); this.lightPlane.lookAt( v3 ); this.lightPlane.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity ); this.targetLine.geometry.vertices[ 1 ].copy( v3 ); this.targetLine.geometry.verticesNeedUpdate = true; this.targetLine.material.color.copy( this.lightPlane.material.color ); } }(); /** * @author WestLangley / http://github.com/WestLangley */ THREE.EdgesHelper = function ( object, hex ) { var color = ( hex !== undefined ) ? hex : 0xffffff; var edge = [ 0, 0 ], hash = {}; var sortFunction = function ( a, b ) { return a - b }; var keys = [ 'a', 'b', 'c' ]; var geometry = new THREE.BufferGeometry(); var geometry2 = object.geometry.clone(); geometry2.mergeVertices(); geometry2.computeFaceNormals(); var vertices = geometry2.vertices; var faces = geometry2.faces; var numEdges = 0; for ( var i = 0, l = faces.length; i < l; i ++ ) { var face = faces[ i ]; for ( var j = 0; j < 3; j ++ ) { edge[ 0 ] = face[ keys[ j ] ]; edge[ 1 ] = face[ keys[ ( j + 1 ) % 3 ] ]; edge.sort( sortFunction ); var key = edge.toString(); if ( hash[ key ] === undefined ) { hash[ key ] = { vert1: edge[ 0 ], vert2: edge[ 1 ], face1: i, face2: undefined }; numEdges ++; } else { hash[ key ].face2 = i; } } } geometry.addAttribute( 'position', Float32Array, 2 * numEdges, 3 ); var coords = geometry.attributes.position.array; var index = 0; for ( var key in hash ) { var h = hash[ key ]; if ( h.face2 === undefined || faces[ h.face1 ].normal.dot( faces[ h.face2 ].normal ) < 0.9999 ) { // hardwired const OK var vertex = vertices[ h.vert1 ]; coords[ index ++ ] = vertex.x; coords[ index ++ ] = vertex.y; coords[ index ++ ] = vertex.z; vertex = vertices[ h.vert2 ]; coords[ index ++ ] = vertex.x; coords[ index ++ ] = vertex.y; coords[ index ++ ] = vertex.z; } } THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color } ), THREE.LinePieces ); this.matrixAutoUpdate = false; this.matrixWorld = object.matrixWorld; }; THREE.EdgesHelper.prototype = Object.create( THREE.Line.prototype ); /** * @author mrdoob / http://mrdoob.com/ * @author WestLangley / http://github.com/WestLangley */ THREE.FaceNormalsHelper = function ( object, size, hex, linewidth ) { this.object = object; this.size = ( size !== undefined ) ? size : 1; var color = ( hex !== undefined ) ? hex : 0xffff00; var width = ( linewidth !== undefined ) ? linewidth : 1; var geometry = new THREE.Geometry(); var faces = this.object.geometry.faces; for ( var i = 0, l = faces.length; i < l; i ++ ) { geometry.vertices.push( new THREE.Vector3() ); geometry.vertices.push( new THREE.Vector3() ); } THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color, linewidth: width } ), THREE.LinePieces ); this.matrixAutoUpdate = false; this.normalMatrix = new THREE.Matrix3(); this.update(); }; THREE.FaceNormalsHelper.prototype = Object.create( THREE.Line.prototype ); THREE.FaceNormalsHelper.prototype.update = ( function ( object ) { var v1 = new THREE.Vector3(); return function ( object ) { this.object.updateMatrixWorld( true ); this.normalMatrix.getNormalMatrix( this.object.matrixWorld ); var vertices = this.geometry.vertices; var faces = this.object.geometry.faces; var worldMatrix = this.object.matrixWorld; for ( var i = 0, l = faces.length; i < l; i ++ ) { var face = faces[ i ]; v1.copy( face.normal ).applyMatrix3( this.normalMatrix ).normalize().multiplyScalar( this.size ); var idx = 2 * i; vertices[ idx ].copy( face.centroid ).applyMatrix4( worldMatrix ); vertices[ idx + 1 ].addVectors( vertices[ idx ], v1 ); } this.geometry.verticesNeedUpdate = true; return this; } }()); /** * @author mrdoob / http://mrdoob.com/ */ THREE.GridHelper = function ( size, step ) { var geometry = new THREE.Geometry(); var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } ); this.color1 = new THREE.Color( 0x444444 ); this.color2 = new THREE.Color( 0x888888 ); for ( var i = - size; i <= size; i += step ) { geometry.vertices.push( new THREE.Vector3( - size, 0, i ), new THREE.Vector3( size, 0, i ), new THREE.Vector3( i, 0, - size ), new THREE.Vector3( i, 0, size ) ); var color = i === 0 ? this.color1 : this.color2; geometry.colors.push( color, color, color, color ); } THREE.Line.call( this, geometry, material, THREE.LinePieces ); }; THREE.GridHelper.prototype = Object.create( THREE.Line.prototype ); THREE.GridHelper.prototype.setColors = function( colorCenterLine, colorGrid ) { this.color1.set( colorCenterLine ); this.color2.set( colorGrid ); this.geometry.colorsNeedUpdate = true; } /** * @author alteredq / http://alteredqualia.com/ * @author mrdoob / http://mrdoob.com/ */ THREE.HemisphereLightHelper = function ( light, sphereSize, arrowLength, domeSize ) { THREE.Object3D.call( this ); this.light = light; this.light.updateMatrixWorld(); this.matrixWorld = light.matrixWorld; this.matrixAutoUpdate = false; this.colors = [ new THREE.Color(), new THREE.Color() ]; var geometry = new THREE.SphereGeometry( sphereSize, 4, 2 ); geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) ); for ( var i = 0, il = 8; i < il; i ++ ) { geometry.faces[ i ].color = this.colors[ i < 4 ? 0 : 1 ]; } var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors, wireframe: true } ); this.lightSphere = new THREE.Mesh( geometry, material ); this.add( this.lightSphere ); this.update(); }; THREE.HemisphereLightHelper.prototype = Object.create( THREE.Object3D.prototype ); THREE.HemisphereLightHelper.prototype.dispose = function () { this.lightSphere.geometry.dispose(); this.lightSphere.material.dispose(); }; THREE.HemisphereLightHelper.prototype.update = function () { var vector = new THREE.Vector3(); return function () { this.colors[ 0 ].copy( this.light.color ).multiplyScalar( this.light.intensity ); this.colors[ 1 ].copy( this.light.groundColor ).multiplyScalar( this.light.intensity ); this.lightSphere.lookAt( vector.setFromMatrixPosition( this.light.matrixWorld ).negate() ); this.lightSphere.geometry.colorsNeedUpdate = true; } }(); /** * @author alteredq / http://alteredqualia.com/ * @author mrdoob / http://mrdoob.com/ */ THREE.PointLightHelper = function ( light, sphereSize ) { this.light = light; this.light.updateMatrixWorld(); var geometry = new THREE.SphereGeometry( sphereSize, 4, 2 ); var material = new THREE.MeshBasicMaterial( { wireframe: true, fog: false } ); material.color.copy( this.light.color ).multiplyScalar( this.light.intensity ); THREE.Mesh.call( this, geometry, material ); this.matrixWorld = this.light.matrixWorld; this.matrixAutoUpdate = false; /* var distanceGeometry = new THREE.IcosahedronGeometry( 1, 2 ); var distanceMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false, wireframe: true, opacity: 0.1, transparent: true } ); this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial ); this.lightDistance = new THREE.Mesh( distanceGeometry, distanceMaterial ); var d = light.distance; if ( d === 0.0 ) { this.lightDistance.visible = false; } else { this.lightDistance.scale.set( d, d, d ); } this.add( this.lightDistance ); */ }; THREE.PointLightHelper.prototype = Object.create( THREE.Mesh.prototype ); THREE.PointLightHelper.prototype.dispose = function () { this.geometry.dispose(); this.material.dispose(); }; THREE.PointLightHelper.prototype.update = function () { this.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity ); /* var d = this.light.distance; if ( d === 0.0 ) { this.lightDistance.visible = false; } else { this.lightDistance.visible = true; this.lightDistance.scale.set( d, d, d ); } */ }; /** * @author alteredq / http://alteredqualia.com/ * @author mrdoob / http://mrdoob.com/ * @author WestLangley / http://github.com/WestLangley */ THREE.SpotLightHelper = function ( light ) { THREE.Object3D.call( this ); this.light = light; this.light.updateMatrixWorld(); this.matrixWorld = light.matrixWorld; this.matrixAutoUpdate = false; var geometry = new THREE.CylinderGeometry( 0, 1, 1, 8, 1, true ); geometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, -0.5, 0 ) ); geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) ); var material = new THREE.MeshBasicMaterial( { wireframe: true, fog: false } ); this.cone = new THREE.Mesh( geometry, material ); this.add( this.cone ); this.update(); }; THREE.SpotLightHelper.prototype = Object.create( THREE.Object3D.prototype ); THREE.SpotLightHelper.prototype.dispose = function () { this.cone.geometry.dispose(); this.cone.material.dispose(); }; THREE.SpotLightHelper.prototype.update = function () { var vector = new THREE.Vector3(); var vector2 = new THREE.Vector3(); return function () { var coneLength = this.light.distance ? this.light.distance : 10000; var coneWidth = coneLength * Math.tan( this.light.angle ); this.cone.scale.set( coneWidth, coneWidth, coneLength ); vector.setFromMatrixPosition( this.light.matrixWorld ); vector2.setFromMatrixPosition( this.light.target.matrixWorld ); this.cone.lookAt( vector2.sub( vector ) ); this.cone.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity ); }; }(); /** * @author mrdoob / http://mrdoob.com/ * @author WestLangley / http://github.com/WestLangley */ THREE.VertexNormalsHelper = function ( object, size, hex, linewidth ) { this.object = object; this.size = ( size !== undefined ) ? size : 1; var color = ( hex !== undefined ) ? hex : 0xff0000; var width = ( linewidth !== undefined ) ? linewidth : 1; var geometry = new THREE.Geometry(); var vertices = object.geometry.vertices; var faces = object.geometry.faces; for ( var i = 0, l = faces.length; i < l; i ++ ) { var face = faces[ i ]; for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) { geometry.vertices.push( new THREE.Vector3() ); geometry.vertices.push( new THREE.Vector3() ); } } THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color, linewidth: width } ), THREE.LinePieces ); this.matrixAutoUpdate = false; this.normalMatrix = new THREE.Matrix3(); this.update(); }; THREE.VertexNormalsHelper.prototype = Object.create( THREE.Line.prototype ); THREE.VertexNormalsHelper.prototype.update = ( function ( object ) { var v1 = new THREE.Vector3(); return function( object ) { var keys = [ 'a', 'b', 'c', 'd' ]; this.object.updateMatrixWorld( true ); this.normalMatrix.getNormalMatrix( this.object.matrixWorld ); var vertices = this.geometry.vertices; var verts = this.object.geometry.vertices; var faces = this.object.geometry.faces; var worldMatrix = this.object.matrixWorld; var idx = 0; for ( var i = 0, l = faces.length; i < l; i ++ ) { var face = faces[ i ]; for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) { var vertexId = face[ keys[ j ] ]; var vertex = verts[ vertexId ]; var normal = face.vertexNormals[ j ]; vertices[ idx ].copy( vertex ).applyMatrix4( worldMatrix ); v1.copy( normal ).applyMatrix3( this.normalMatrix ).normalize().multiplyScalar( this.size ); v1.add( vertices[ idx ] ); idx = idx + 1; vertices[ idx ].copy( v1 ); idx = idx + 1; } } this.geometry.verticesNeedUpdate = true; return this; } }()); /** * @author mrdoob / http://mrdoob.com/ * @author WestLangley / http://github.com/WestLangley */ THREE.VertexTangentsHelper = function ( object, size, hex, linewidth ) { this.object = object; this.size = ( size !== undefined ) ? size : 1; var color = ( hex !== undefined ) ? hex : 0x0000ff; var width = ( linewidth !== undefined ) ? linewidth : 1; var geometry = new THREE.Geometry(); var vertices = object.geometry.vertices; var faces = object.geometry.faces; for ( var i = 0, l = faces.length; i < l; i ++ ) { var face = faces[ i ]; for ( var j = 0, jl = face.vertexTangents.length; j < jl; j ++ ) { geometry.vertices.push( new THREE.Vector3() ); geometry.vertices.push( new THREE.Vector3() ); } } THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color, linewidth: width } ), THREE.LinePieces ); this.matrixAutoUpdate = false; this.update(); }; THREE.VertexTangentsHelper.prototype = Object.create( THREE.Line.prototype ); THREE.VertexTangentsHelper.prototype.update = ( function ( object ) { var v1 = new THREE.Vector3(); return function( object ) { var keys = [ 'a', 'b', 'c', 'd' ]; this.object.updateMatrixWorld( true ); var vertices = this.geometry.vertices; var verts = this.object.geometry.vertices; var faces = this.object.geometry.faces; var worldMatrix = this.object.matrixWorld; var idx = 0; for ( var i = 0, l = faces.length; i < l; i ++ ) { var face = faces[ i ]; for ( var j = 0, jl = face.vertexTangents.length; j < jl; j ++ ) { var vertexId = face[ keys[ j ] ]; var vertex = verts[ vertexId ]; var tangent = face.vertexTangents[ j ]; vertices[ idx ].copy( vertex ).applyMatrix4( worldMatrix ); v1.copy( tangent ).transformDirection( worldMatrix ).multiplyScalar( this.size ); v1.add( vertices[ idx ] ); idx = idx + 1; vertices[ idx ].copy( v1 ); idx = idx + 1; } } this.geometry.verticesNeedUpdate = true; return this; } }()); /** * @author mrdoob / http://mrdoob.com/ */ THREE.WireframeHelper = function ( object, hex ) { var color = ( hex !== undefined ) ? hex : 0xffffff; var edge = [ 0, 0 ], hash = {}; var sortFunction = function ( a, b ) { return a - b }; var keys = [ 'a', 'b', 'c' ]; var geometry = new THREE.BufferGeometry(); if ( object.geometry instanceof THREE.Geometry ) { var vertices = object.geometry.vertices; var faces = object.geometry.faces; var numEdges = 0; // allocate maximal size var edges = new Uint32Array( 6 * faces.length ); for ( var i = 0, l = faces.length; i < l; i ++ ) { var face = faces[ i ]; for ( var j = 0; j < 3; j ++ ) { edge[ 0 ] = face[ keys[ j ] ]; edge[ 1 ] = face[ keys[ ( j + 1 ) % 3 ] ]; edge.sort( sortFunction ); var key = edge.toString(); if ( hash[ key ] === undefined ) { edges[ 2 * numEdges ] = edge[ 0 ]; edges[ 2 * numEdges + 1 ] = edge[ 1 ]; hash[ key ] = true; numEdges ++; } } } geometry.addAttribute( 'position', Float32Array, 2 * numEdges, 3 ); var coords = geometry.attributes.position.array; for ( var i = 0, l = numEdges; i < l; i ++ ) { for ( var j = 0; j < 2; j ++ ) { var vertex = vertices[ edges [ 2 * i + j] ]; var index = 6 * i + 3 * j; coords[ index + 0 ] = vertex.x; coords[ index + 1 ] = vertex.y; coords[ index + 2 ] = vertex.z; } } } else if ( object.geometry instanceof THREE.BufferGeometry && object.geometry.attributes.index !== undefined ) { // indexed BufferGeometry var vertices = object.geometry.attributes.position.array; var indices = object.geometry.attributes.index.array; var offsets = object.geometry.offsets; var numEdges = 0; // allocate maximal size var edges = new Uint32Array( 2 * indices.length ); for ( var o = 0, ol = offsets.length; o < ol; ++ o ) { var start = offsets[ o ].start; var count = offsets[ o ].count; var index = offsets[ o ].index; for ( var i = start, il = start + count; i < il; i += 3 ) { for ( var j = 0; j < 3; j ++ ) { edge[ 0 ] = index + indices[ i + j ]; edge[ 1 ] = index + indices[ i + ( j + 1 ) % 3 ]; edge.sort( sortFunction ); var key = edge.toString(); if ( hash[ key ] === undefined ) { edges[ 2 * numEdges ] = edge[ 0 ]; edges[ 2 * numEdges + 1 ] = edge[ 1 ]; hash[ key ] = true; numEdges ++; } } } } geometry.addAttribute( 'position', Float32Array, 2 * numEdges, 3 ); var coords = geometry.attributes.position.array; for ( var i = 0, l = numEdges; i < l; i ++ ) { for ( var j = 0; j < 2; j ++ ) { var index = 6 * i + 3 * j; var index2 = 3 * edges[ 2 * i + j]; coords[ index + 0 ] = vertices[ index2 ]; coords[ index + 1 ] = vertices[ index2 + 1 ]; coords[ index + 2 ] = vertices[ index2 + 2 ]; } } } else if ( object.geometry instanceof THREE.BufferGeometry ) { // non-indexed BufferGeometry var vertices = object.geometry.attributes.position.array; var numEdges = vertices.length / 3; var numTris = numEdges / 3; geometry.addAttribute( 'position', Float32Array, 2 * numEdges, 3 ); var coords = geometry.attributes.position.array; for ( var i = 0, l = numTris; i < l; i ++ ) { for ( var j = 0; j < 3; j ++ ) { var index = 18 * i + 6 * j; var index1 = 9 * i + 3 * j; coords[ index + 0 ] = vertices[ index1 ]; coords[ index + 1 ] = vertices[ index1 + 1 ]; coords[ index + 2 ] = vertices[ index1 + 2 ]; var index2 = 9 * i + 3 * ( ( j + 1 ) % 3 ); coords[ index + 3 ] = vertices[ index2 ]; coords[ index + 4 ] = vertices[ index2 + 1 ]; coords[ index + 5 ] = vertices[ index2 + 2 ]; } } } THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color } ), THREE.LinePieces ); this.matrixAutoUpdate = false; this.matrixWorld = object.matrixWorld; }; THREE.WireframeHelper.prototype = Object.create( THREE.Line.prototype ); /** * @author alteredq / http://alteredqualia.com/ */ THREE.ImmediateRenderObject = function () { THREE.Object3D.call( this ); this.render = function ( renderCallback ) { }; }; THREE.ImmediateRenderObject.prototype = Object.create( THREE.Object3D.prototype ); /** * @author mikael emtinger / http://gomo.se/ * @author alteredq / http://alteredqualia.com/ */ THREE.LensFlare = function ( texture, size, distance, blending, color ) { THREE.Object3D.call( this ); this.lensFlares = []; this.positionScreen = new THREE.Vector3(); this.customUpdateCallback = undefined; if( texture !== undefined ) { this.add( texture, size, distance, blending, color ); } }; THREE.LensFlare.prototype = Object.create( THREE.Object3D.prototype ); /* * Add: adds another flare */ THREE.LensFlare.prototype.add = function ( texture, size, distance, blending, color, opacity ) { if( size === undefined ) size = -1; if( distance === undefined ) distance = 0; if( opacity === undefined ) opacity = 1; if( color === undefined ) color = new THREE.Color( 0xffffff ); if( blending === undefined ) blending = THREE.NormalBlending; distance = Math.min( distance, Math.max( 0, distance ) ); this.lensFlares.push( { texture: texture, // THREE.Texture size: size, // size in pixels (-1 = use texture.width) distance: distance, // distance (0-1) from light source (0=at light source) x: 0, y: 0, z: 0, // screen position (-1 => 1) z = 0 is ontop z = 1 is back scale: 1, // scale rotation: 1, // rotation opacity: opacity, // opacity color: color, // color blending: blending } ); // blending }; /* * Update lens flares update positions on all flares based on the screen position * Set myLensFlare.customUpdateCallback to alter the flares in your project specific way. */ THREE.LensFlare.prototype.updateLensFlares = function () { var f, fl = this.lensFlares.length; var flare; var vecX = -this.positionScreen.x * 2; var vecY = -this.positionScreen.y * 2; for( f = 0; f < fl; f ++ ) { flare = this.lensFlares[ f ]; flare.x = this.positionScreen.x + vecX * flare.distance; flare.y = this.positionScreen.y + vecY * flare.distance; flare.wantedRotation = flare.x * Math.PI * 0.25; flare.rotation += ( flare.wantedRotation - flare.rotation ) * 0.25; } }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.MorphBlendMesh = function( geometry, material ) { THREE.Mesh.call( this, geometry, material ); this.animationsMap = {}; this.animationsList = []; // prepare default animation // (all frames played together in 1 second) var numFrames = this.geometry.morphTargets.length; var name = "__default"; var startFrame = 0; var endFrame = numFrames - 1; var fps = numFrames / 1; this.createAnimation( name, startFrame, endFrame, fps ); this.setAnimationWeight( name, 1 ); }; THREE.MorphBlendMesh.prototype = Object.create( THREE.Mesh.prototype ); THREE.MorphBlendMesh.prototype.createAnimation = function ( name, start, end, fps ) { var animation = { startFrame: start, endFrame: end, length: end - start + 1, fps: fps, duration: ( end - start ) / fps, lastFrame: 0, currentFrame: 0, active: false, time: 0, direction: 1, weight: 1, directionBackwards: false, mirroredLoop: false }; this.animationsMap[ name ] = animation; this.animationsList.push( animation ); }; THREE.MorphBlendMesh.prototype.autoCreateAnimations = function ( fps ) { var pattern = /([a-z]+)(\d+)/; var firstAnimation, frameRanges = {}; var geometry = this.geometry; for ( var i = 0, il = geometry.morphTargets.length; i < il; i ++ ) { var morph = geometry.morphTargets[ i ]; var chunks = morph.name.match( pattern ); if ( chunks && chunks.length > 1 ) { var name = chunks[ 1 ]; var num = chunks[ 2 ]; if ( ! frameRanges[ name ] ) frameRanges[ name ] = { start: Infinity, end: -Infinity }; var range = frameRanges[ name ]; if ( i < range.start ) range.start = i; if ( i > range.end ) range.end = i; if ( ! firstAnimation ) firstAnimation = name; } } for ( var name in frameRanges ) { var range = frameRanges[ name ]; this.createAnimation( name, range.start, range.end, fps ); } this.firstAnimation = firstAnimation; }; THREE.MorphBlendMesh.prototype.setAnimationDirectionForward = function ( name ) { var animation = this.animationsMap[ name ]; if ( animation ) { animation.direction = 1; animation.directionBackwards = false; } }; THREE.MorphBlendMesh.prototype.setAnimationDirectionBackward = function ( name ) { var animation = this.animationsMap[ name ]; if ( animation ) { animation.direction = -1; animation.directionBackwards = true; } }; THREE.MorphBlendMesh.prototype.setAnimationFPS = function ( name, fps ) { var animation = this.animationsMap[ name ]; if ( animation ) { animation.fps = fps; animation.duration = ( animation.end - animation.start ) / animation.fps; } }; THREE.MorphBlendMesh.prototype.setAnimationDuration = function ( name, duration ) { var animation = this.animationsMap[ name ]; if ( animation ) { animation.duration = duration; animation.fps = ( animation.end - animation.start ) / animation.duration; } }; THREE.MorphBlendMesh.prototype.setAnimationWeight = function ( name, weight ) { var animation = this.animationsMap[ name ]; if ( animation ) { animation.weight = weight; } }; THREE.MorphBlendMesh.prototype.setAnimationTime = function ( name, time ) { var animation = this.animationsMap[ name ]; if ( animation ) { animation.time = time; } }; THREE.MorphBlendMesh.prototype.getAnimationTime = function ( name ) { var time = 0; var animation = this.animationsMap[ name ]; if ( animation ) { time = animation.time; } return time; }; THREE.MorphBlendMesh.prototype.getAnimationDuration = function ( name ) { var duration = -1; var animation = this.animationsMap[ name ]; if ( animation ) { duration = animation.duration; } return duration; }; THREE.MorphBlendMesh.prototype.playAnimation = function ( name ) { var animation = this.animationsMap[ name ]; if ( animation ) { animation.time = 0; animation.active = true; } else { console.warn( "animation[" + name + "] undefined" ); } }; THREE.MorphBlendMesh.prototype.stopAnimation = function ( name ) { var animation = this.animationsMap[ name ]; if ( animation ) { animation.active = false; } }; THREE.MorphBlendMesh.prototype.update = function ( delta ) { for ( var i = 0, il = this.animationsList.length; i < il; i ++ ) { var animation = this.animationsList[ i ]; if ( ! animation.active ) continue; var frameTime = animation.duration / animation.length; animation.time += animation.direction * delta; if ( animation.mirroredLoop ) { if ( animation.time > animation.duration || animation.time < 0 ) { animation.direction *= -1; if ( animation.time > animation.duration ) { animation.time = animation.duration; animation.directionBackwards = true; } if ( animation.time < 0 ) { animation.time = 0; animation.directionBackwards = false; } } } else { animation.time = animation.time % animation.duration; if ( animation.time < 0 ) animation.time += animation.duration; } var keyframe = animation.startFrame + THREE.Math.clamp( Math.floor( animation.time / frameTime ), 0, animation.length - 1 ); var weight = animation.weight; if ( keyframe !== animation.currentFrame ) { this.morphTargetInfluences[ animation.lastFrame ] = 0; this.morphTargetInfluences[ animation.currentFrame ] = 1 * weight; this.morphTargetInfluences[ keyframe ] = 0; animation.lastFrame = animation.currentFrame; animation.currentFrame = keyframe; } var mix = ( animation.time % frameTime ) / frameTime; if ( animation.directionBackwards ) mix = 1 - mix; this.morphTargetInfluences[ animation.currentFrame ] = mix * weight; this.morphTargetInfluences[ animation.lastFrame ] = ( 1 - mix ) * weight; } }; /** * @author mikael emtinger / http://gomo.se/ * @author alteredq / http://alteredqualia.com/ */ THREE.LensFlarePlugin = function () { var _gl, _renderer, _precision, _lensFlare = {}; this.init = function ( renderer ) { _gl = renderer.context; _renderer = renderer; _precision = renderer.getPrecision(); _lensFlare.vertices = new Float32Array( 8 + 8 ); _lensFlare.faces = new Uint16Array( 6 ); var i = 0; _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = -1; // vertex _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 0; // uv... etc. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 1; i = 0; _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 1; _lensFlare.faces[ i++ ] = 2; _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 2; _lensFlare.faces[ i++ ] = 3; // buffers _lensFlare.vertexBuffer = _gl.createBuffer(); _lensFlare.elementBuffer = _gl.createBuffer(); _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, _lensFlare.vertices, _gl.STATIC_DRAW ); _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer ); _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.faces, _gl.STATIC_DRAW ); // textures _lensFlare.tempTexture = _gl.createTexture(); _lensFlare.occlusionTexture = _gl.createTexture(); _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture ); _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, 16, 16, 0, _gl.RGB, _gl.UNSIGNED_BYTE, null ); _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE ); _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE ); _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST ); _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST ); _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture ); _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, 16, 16, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, null ); _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE ); _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE ); _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST ); _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST ); if ( _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) <= 0 ) { _lensFlare.hasVertexTexture = false; _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlare" ], _precision ); } else { _lensFlare.hasVertexTexture = true; _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlareVertexTexture" ], _precision ); } _lensFlare.attributes = {}; _lensFlare.uniforms = {}; _lensFlare.attributes.vertex = _gl.getAttribLocation ( _lensFlare.program, "position" ); _lensFlare.attributes.uv = _gl.getAttribLocation ( _lensFlare.program, "uv" ); _lensFlare.uniforms.renderType = _gl.getUniformLocation( _lensFlare.program, "renderType" ); _lensFlare.uniforms.map = _gl.getUniformLocation( _lensFlare.program, "map" ); _lensFlare.uniforms.occlusionMap = _gl.getUniformLocation( _lensFlare.program, "occlusionMap" ); _lensFlare.uniforms.opacity = _gl.getUniformLocation( _lensFlare.program, "opacity" ); _lensFlare.uniforms.color = _gl.getUniformLocation( _lensFlare.program, "color" ); _lensFlare.uniforms.scale = _gl.getUniformLocation( _lensFlare.program, "scale" ); _lensFlare.uniforms.rotation = _gl.getUniformLocation( _lensFlare.program, "rotation" ); _lensFlare.uniforms.screenPosition = _gl.getUniformLocation( _lensFlare.program, "screenPosition" ); }; /* * Render lens flares * Method: renders 16x16 0xff00ff-colored points scattered over the light source area, * reads these back and calculates occlusion. * Then _lensFlare.update_lensFlares() is called to re-position and * update transparency of flares. Then they are rendered. * */ this.render = function ( scene, camera, viewportWidth, viewportHeight ) { var flares = scene.__webglFlares, nFlares = flares.length; if ( ! nFlares ) return; var tempPosition = new THREE.Vector3(); var invAspect = viewportHeight / viewportWidth, halfViewportWidth = viewportWidth * 0.5, halfViewportHeight = viewportHeight * 0.5; var size = 16 / viewportHeight, scale = new THREE.Vector2( size * invAspect, size ); var screenPosition = new THREE.Vector3( 1, 1, 0 ), screenPositionPixels = new THREE.Vector2( 1, 1 ); var uniforms = _lensFlare.uniforms, attributes = _lensFlare.attributes; // set _lensFlare program and reset blending _gl.useProgram( _lensFlare.program ); _gl.enableVertexAttribArray( _lensFlare.attributes.vertex ); _gl.enableVertexAttribArray( _lensFlare.attributes.uv ); // loop through all lens flares to update their occlusion and positions // setup gl and common used attribs/unforms _gl.uniform1i( uniforms.occlusionMap, 0 ); _gl.uniform1i( uniforms.map, 1 ); _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer ); _gl.vertexAttribPointer( attributes.vertex, 2, _gl.FLOAT, false, 2 * 8, 0 ); _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 ); _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer ); _gl.disable( _gl.CULL_FACE ); _gl.depthMask( false ); var i, j, jl, flare, sprite; for ( i = 0; i < nFlares; i ++ ) { size = 16 / viewportHeight; scale.set( size * invAspect, size ); // calc object screen position flare = flares[ i ]; tempPosition.set( flare.matrixWorld.elements[12], flare.matrixWorld.elements[13], flare.matrixWorld.elements[14] ); tempPosition.applyMatrix4( camera.matrixWorldInverse ); tempPosition.applyProjection( camera.projectionMatrix ); // setup arrays for gl programs screenPosition.copy( tempPosition ) screenPositionPixels.x = screenPosition.x * halfViewportWidth + halfViewportWidth; screenPositionPixels.y = screenPosition.y * halfViewportHeight + halfViewportHeight; // screen cull if ( _lensFlare.hasVertexTexture || ( screenPositionPixels.x > 0 && screenPositionPixels.x < viewportWidth && screenPositionPixels.y > 0 && screenPositionPixels.y < viewportHeight ) ) { // save current RGB to temp texture _gl.activeTexture( _gl.TEXTURE1 ); _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture ); _gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 ); // render pink quad _gl.uniform1i( uniforms.renderType, 0 ); _gl.uniform2f( uniforms.scale, scale.x, scale.y ); _gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z ); _gl.disable( _gl.BLEND ); _gl.enable( _gl.DEPTH_TEST ); _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 ); // copy result to occlusionMap _gl.activeTexture( _gl.TEXTURE0 ); _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture ); _gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 ); // restore graphics _gl.uniform1i( uniforms.renderType, 1 ); _gl.disable( _gl.DEPTH_TEST ); _gl.activeTexture( _gl.TEXTURE1 ); _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture ); _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 ); // update object positions flare.positionScreen.copy( screenPosition ) if ( flare.customUpdateCallback ) { flare.customUpdateCallback( flare ); } else { flare.updateLensFlares(); } // render flares _gl.uniform1i( uniforms.renderType, 2 ); _gl.enable( _gl.BLEND ); for ( j = 0, jl = flare.lensFlares.length; j < jl; j ++ ) { sprite = flare.lensFlares[ j ]; if ( sprite.opacity > 0.001 && sprite.scale > 0.001 ) { screenPosition.x = sprite.x; screenPosition.y = sprite.y; screenPosition.z = sprite.z; size = sprite.size * sprite.scale / viewportHeight; scale.x = size * invAspect; scale.y = size; _gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z ); _gl.uniform2f( uniforms.scale, scale.x, scale.y ); _gl.uniform1f( uniforms.rotation, sprite.rotation ); _gl.uniform1f( uniforms.opacity, sprite.opacity ); _gl.uniform3f( uniforms.color, sprite.color.r, sprite.color.g, sprite.color.b ); _renderer.setBlending( sprite.blending, sprite.blendEquation, sprite.blendSrc, sprite.blendDst ); _renderer.setTexture( sprite.texture, 1 ); _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 ); } } } } // restore gl _gl.enable( _gl.CULL_FACE ); _gl.enable( _gl.DEPTH_TEST ); _gl.depthMask( true ); }; function createProgram ( shader, precision ) { var program = _gl.createProgram(); var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER ); var vertexShader = _gl.createShader( _gl.VERTEX_SHADER ); var prefix = "precision " + precision + " float;\n"; _gl.shaderSource( fragmentShader, prefix + shader.fragmentShader ); _gl.shaderSource( vertexShader, prefix + shader.vertexShader ); _gl.compileShader( fragmentShader ); _gl.compileShader( vertexShader ); _gl.attachShader( program, fragmentShader ); _gl.attachShader( program, vertexShader ); _gl.linkProgram( program ); return program; }; }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.ShadowMapPlugin = function () { var _gl, _renderer, _depthMaterial, _depthMaterialMorph, _depthMaterialSkin, _depthMaterialMorphSkin, _frustum = new THREE.Frustum(), _projScreenMatrix = new THREE.Matrix4(), _min = new THREE.Vector3(), _max = new THREE.Vector3(), _matrixPosition = new THREE.Vector3(); this.init = function ( renderer ) { _gl = renderer.context; _renderer = renderer; var depthShader = THREE.ShaderLib[ "depthRGBA" ]; var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms ); _depthMaterial = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms } ); _depthMaterialMorph = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, morphTargets: true } ); _depthMaterialSkin = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, skinning: true } ); _depthMaterialMorphSkin = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, morphTargets: true, skinning: true } ); _depthMaterial._shadowPass = true; _depthMaterialMorph._shadowPass = true; _depthMaterialSkin._shadowPass = true; _depthMaterialMorphSkin._shadowPass = true; }; this.render = function ( scene, camera ) { if ( ! ( _renderer.shadowMapEnabled && _renderer.shadowMapAutoUpdate ) ) return; this.update( scene, camera ); }; this.update = function ( scene, camera ) { var i, il, j, jl, n, shadowMap, shadowMatrix, shadowCamera, program, buffer, material, webglObject, object, light, renderList, lights = [], k = 0, fog = null; // set GL state for depth map _gl.clearColor( 1, 1, 1, 1 ); _gl.disable( _gl.BLEND ); _gl.enable( _gl.CULL_FACE ); _gl.frontFace( _gl.CCW ); if ( _renderer.shadowMapCullFace === THREE.CullFaceFront ) { _gl.cullFace( _gl.FRONT ); } else { _gl.cullFace( _gl.BACK ); } _renderer.setDepthTest( true ); // preprocess lights // - skip lights that are not casting shadows // - create virtual lights for cascaded shadow maps for ( i = 0, il = scene.__lights.length; i < il; i ++ ) { light = scene.__lights[ i ]; if ( ! light.castShadow ) continue; if ( ( light instanceof THREE.DirectionalLight ) && light.shadowCascade ) { for ( n = 0; n < light.shadowCascadeCount; n ++ ) { var virtualLight; if ( ! light.shadowCascadeArray[ n ] ) { virtualLight = createVirtualLight( light, n ); virtualLight.originalCamera = camera; var gyro = new THREE.Gyroscope(); gyro.position = light.shadowCascadeOffset; gyro.add( virtualLight ); gyro.add( virtualLight.target ); camera.add( gyro ); light.shadowCascadeArray[ n ] = virtualLight; console.log( "Created virtualLight", virtualLight ); } else { virtualLight = light.shadowCascadeArray[ n ]; } updateVirtualLight( light, n ); lights[ k ] = virtualLight; k ++; } } else { lights[ k ] = light; k ++; } } // render depth map for ( i = 0, il = lights.length; i < il; i ++ ) { light = lights[ i ]; if ( ! light.shadowMap ) { var shadowFilter = THREE.LinearFilter; if ( _renderer.shadowMapType === THREE.PCFSoftShadowMap ) { shadowFilter = THREE.NearestFilter; } var pars = { minFilter: shadowFilter, magFilter: shadowFilter, format: THREE.RGBAFormat }; light.shadowMap = new THREE.WebGLRenderTarget( light.shadowMapWidth, light.shadowMapHeight, pars ); light.shadowMapSize = new THREE.Vector2( light.shadowMapWidth, light.shadowMapHeight ); light.shadowMatrix = new THREE.Matrix4(); } if ( ! light.shadowCamera ) { if ( light instanceof THREE.SpotLight ) { light.shadowCamera = new THREE.PerspectiveCamera( light.shadowCameraFov, light.shadowMapWidth / light.shadowMapHeight, light.shadowCameraNear, light.shadowCameraFar ); } else if ( light instanceof THREE.DirectionalLight ) { light.shadowCamera = new THREE.OrthographicCamera( light.shadowCameraLeft, light.shadowCameraRight, light.shadowCameraTop, light.shadowCameraBottom, light.shadowCameraNear, light.shadowCameraFar ); } else { console.error( "Unsupported light type for shadow" ); continue; } scene.add( light.shadowCamera ); if ( scene.autoUpdate === true ) scene.updateMatrixWorld(); } if ( light.shadowCameraVisible && ! light.cameraHelper ) { light.cameraHelper = new THREE.CameraHelper( light.shadowCamera ); light.shadowCamera.add( light.cameraHelper ); } if ( light.isVirtual && virtualLight.originalCamera == camera ) { updateShadowCamera( camera, light ); } shadowMap = light.shadowMap; shadowMatrix = light.shadowMatrix; shadowCamera = light.shadowCamera; shadowCamera.position.setFromMatrixPosition( light.matrixWorld ); _matrixPosition.setFromMatrixPosition( light.target.matrixWorld ); shadowCamera.lookAt( _matrixPosition ); shadowCamera.updateMatrixWorld(); shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld ); if ( light.cameraHelper ) light.cameraHelper.visible = light.shadowCameraVisible; if ( light.shadowCameraVisible ) light.cameraHelper.update(); // compute shadow matrix shadowMatrix.set( 0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0 ); shadowMatrix.multiply( shadowCamera.projectionMatrix ); shadowMatrix.multiply( shadowCamera.matrixWorldInverse ); // update camera matrices and frustum _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse ); _frustum.setFromMatrix( _projScreenMatrix ); // render shadow map _renderer.setRenderTarget( shadowMap ); _renderer.clear(); // set object matrices & frustum culling renderList = scene.__webglObjects; for ( j = 0, jl = renderList.length; j < jl; j ++ ) { webglObject = renderList[ j ]; object = webglObject.object; webglObject.render = false; if ( object.visible && object.castShadow ) { if ( ! ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem ) || ! ( object.frustumCulled ) || _frustum.intersectsObject( object ) ) { object._modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld ); webglObject.render = true; } } } // render regular objects var objectMaterial, useMorphing, useSkinning; for ( j = 0, jl = renderList.length; j < jl; j ++ ) { webglObject = renderList[ j ]; if ( webglObject.render ) { object = webglObject.object; buffer = webglObject.buffer; // culling is overriden globally for all objects // while rendering depth map // need to deal with MeshFaceMaterial somehow // in that case just use the first of material.materials for now // (proper solution would require to break objects by materials // similarly to regular rendering and then set corresponding // depth materials per each chunk instead of just once per object) objectMaterial = getObjectMaterial( object ); useMorphing = object.geometry.morphTargets !== undefined && object.geometry.morphTargets.length > 0 && objectMaterial.morphTargets; useSkinning = object instanceof THREE.SkinnedMesh && objectMaterial.skinning; if ( object.customDepthMaterial ) { material = object.customDepthMaterial; } else if ( useSkinning ) { material = useMorphing ? _depthMaterialMorphSkin : _depthMaterialSkin; } else if ( useMorphing ) { material = _depthMaterialMorph; } else { material = _depthMaterial; } if ( buffer instanceof THREE.BufferGeometry ) { _renderer.renderBufferDirect( shadowCamera, scene.__lights, fog, material, buffer, object ); } else { _renderer.renderBuffer( shadowCamera, scene.__lights, fog, material, buffer, object ); } } } // set matrices and render immediate objects renderList = scene.__webglObjectsImmediate; for ( j = 0, jl = renderList.length; j < jl; j ++ ) { webglObject = renderList[ j ]; object = webglObject.object; if ( object.visible && object.castShadow ) { object._modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld ); _renderer.renderImmediateObject( shadowCamera, scene.__lights, fog, _depthMaterial, object ); } } } // restore GL state var clearColor = _renderer.getClearColor(), clearAlpha = _renderer.getClearAlpha(); _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha ); _gl.enable( _gl.BLEND ); if ( _renderer.shadowMapCullFace === THREE.CullFaceFront ) { _gl.cullFace( _gl.BACK ); } }; function createVirtualLight( light, cascade ) { var virtualLight = new THREE.DirectionalLight(); virtualLight.isVirtual = true; virtualLight.onlyShadow = true; virtualLight.castShadow = true; virtualLight.shadowCameraNear = light.shadowCameraNear; virtualLight.shadowCameraFar = light.shadowCameraFar; virtualLight.shadowCameraLeft = light.shadowCameraLeft; virtualLight.shadowCameraRight = light.shadowCameraRight; virtualLight.shadowCameraBottom = light.shadowCameraBottom; virtualLight.shadowCameraTop = light.shadowCameraTop; virtualLight.shadowCameraVisible = light.shadowCameraVisible; virtualLight.shadowDarkness = light.shadowDarkness; virtualLight.shadowBias = light.shadowCascadeBias[ cascade ]; virtualLight.shadowMapWidth = light.shadowCascadeWidth[ cascade ]; virtualLight.shadowMapHeight = light.shadowCascadeHeight[ cascade ]; virtualLight.pointsWorld = []; virtualLight.pointsFrustum = []; var pointsWorld = virtualLight.pointsWorld, pointsFrustum = virtualLight.pointsFrustum; for ( var i = 0; i < 8; i ++ ) { pointsWorld[ i ] = new THREE.Vector3(); pointsFrustum[ i ] = new THREE.Vector3(); } var nearZ = light.shadowCascadeNearZ[ cascade ]; var farZ = light.shadowCascadeFarZ[ cascade ]; pointsFrustum[ 0 ].set( -1, -1, nearZ ); pointsFrustum[ 1 ].set( 1, -1, nearZ ); pointsFrustum[ 2 ].set( -1, 1, nearZ ); pointsFrustum[ 3 ].set( 1, 1, nearZ ); pointsFrustum[ 4 ].set( -1, -1, farZ ); pointsFrustum[ 5 ].set( 1, -1, farZ ); pointsFrustum[ 6 ].set( -1, 1, farZ ); pointsFrustum[ 7 ].set( 1, 1, farZ ); return virtualLight; } // Synchronize virtual light with the original light function updateVirtualLight( light, cascade ) { var virtualLight = light.shadowCascadeArray[ cascade ]; virtualLight.position.copy( light.position ); virtualLight.target.position.copy( light.target.position ); virtualLight.lookAt( virtualLight.target ); virtualLight.shadowCameraVisible = light.shadowCameraVisible; virtualLight.shadowDarkness = light.shadowDarkness; virtualLight.shadowBias = light.shadowCascadeBias[ cascade ]; var nearZ = light.shadowCascadeNearZ[ cascade ]; var farZ = light.shadowCascadeFarZ[ cascade ]; var pointsFrustum = virtualLight.pointsFrustum; pointsFrustum[ 0 ].z = nearZ; pointsFrustum[ 1 ].z = nearZ; pointsFrustum[ 2 ].z = nearZ; pointsFrustum[ 3 ].z = nearZ; pointsFrustum[ 4 ].z = farZ; pointsFrustum[ 5 ].z = farZ; pointsFrustum[ 6 ].z = farZ; pointsFrustum[ 7 ].z = farZ; } // Fit shadow camera's ortho frustum to camera frustum function updateShadowCamera( camera, light ) { var shadowCamera = light.shadowCamera, pointsFrustum = light.pointsFrustum, pointsWorld = light.pointsWorld; _min.set( Infinity, Infinity, Infinity ); _max.set( -Infinity, -Infinity, -Infinity ); for ( var i = 0; i < 8; i ++ ) { var p = pointsWorld[ i ]; p.copy( pointsFrustum[ i ] ); THREE.ShadowMapPlugin.__projector.unprojectVector( p, camera ); p.applyMatrix4( shadowCamera.matrixWorldInverse ); if ( p.x < _min.x ) _min.x = p.x; if ( p.x > _max.x ) _max.x = p.x; if ( p.y < _min.y ) _min.y = p.y; if ( p.y > _max.y ) _max.y = p.y; if ( p.z < _min.z ) _min.z = p.z; if ( p.z > _max.z ) _max.z = p.z; } shadowCamera.left = _min.x; shadowCamera.right = _max.x; shadowCamera.top = _max.y; shadowCamera.bottom = _min.y; // can't really fit near/far //shadowCamera.near = _min.z; //shadowCamera.far = _max.z; shadowCamera.updateProjectionMatrix(); } // For the moment just ignore objects that have multiple materials with different animation methods // Only the first material will be taken into account for deciding which depth material to use for shadow maps function getObjectMaterial( object ) { return object.material instanceof THREE.MeshFaceMaterial ? object.material.materials[ 0 ] : object.material; }; }; THREE.ShadowMapPlugin.__projector = new THREE.Projector(); /** * @author mikael emtinger / http://gomo.se/ * @author alteredq / http://alteredqualia.com/ */ THREE.SpritePlugin = function () { var _gl, _renderer, _texture; var vertices, faces, vertexBuffer, elementBuffer; var program, attributes, uniforms; this.init = function ( renderer ) { _gl = renderer.context; _renderer = renderer; vertices = new Float32Array( [ - 0.5, - 0.5, 0, 0, 0.5, - 0.5, 1, 0, 0.5, 0.5, 1, 1, - 0.5, 0.5, 0, 1 ] ); faces = new Uint16Array( [ 0, 1, 2, 0, 2, 3 ] ); vertexBuffer = _gl.createBuffer(); elementBuffer = _gl.createBuffer(); _gl.bindBuffer( _gl.ARRAY_BUFFER, vertexBuffer ); _gl.bufferData( _gl.ARRAY_BUFFER, vertices, _gl.STATIC_DRAW ); _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, elementBuffer ); _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faces, _gl.STATIC_DRAW ); program = createProgram(); attributes = { position: _gl.getAttribLocation ( program, 'position' ), uv: _gl.getAttribLocation ( program, 'uv' ) }; uniforms = { uvOffset: _gl.getUniformLocation( program, 'uvOffset' ), uvScale: _gl.getUniformLocation( program, 'uvScale' ), rotation: _gl.getUniformLocation( program, 'rotation' ), scale: _gl.getUniformLocation( program, 'scale' ), color: _gl.getUniformLocation( program, 'color' ), map: _gl.getUniformLocation( program, 'map' ), opacity: _gl.getUniformLocation( program, 'opacity' ), modelViewMatrix: _gl.getUniformLocation( program, 'modelViewMatrix' ), projectionMatrix: _gl.getUniformLocation( program, 'projectionMatrix' ), fogType: _gl.getUniformLocation( program, 'fogType' ), fogDensity: _gl.getUniformLocation( program, 'fogDensity' ), fogNear: _gl.getUniformLocation( program, 'fogNear' ), fogFar: _gl.getUniformLocation( program, 'fogFar' ), fogColor: _gl.getUniformLocation( program, 'fogColor' ), alphaTest: _gl.getUniformLocation( program, 'alphaTest' ) }; var canvas = document.createElement( 'canvas' ); canvas.width = 8; canvas.height = 8; var context = canvas.getContext( '2d' ); context.fillStyle = '#ffffff'; context.fillRect( 0, 0, canvas.width, canvas.height ); _texture = new THREE.Texture( canvas ); _texture.needsUpdate = true; }; this.render = function ( scene, camera, viewportWidth, viewportHeight ) { var sprites = scene.__webglSprites, nSprites = sprites.length; if ( ! nSprites ) return; // setup gl _gl.useProgram( program ); _gl.enableVertexAttribArray( attributes.position ); _gl.enableVertexAttribArray( attributes.uv ); _gl.disable( _gl.CULL_FACE ); _gl.enable( _gl.BLEND ); _gl.bindBuffer( _gl.ARRAY_BUFFER, vertexBuffer ); _gl.vertexAttribPointer( attributes.position, 2, _gl.FLOAT, false, 2 * 8, 0 ); _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 ); _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, elementBuffer ); _gl.uniformMatrix4fv( uniforms.projectionMatrix, false, camera.projectionMatrix.elements ); _gl.activeTexture( _gl.TEXTURE0 ); _gl.uniform1i( uniforms.map, 0 ); var oldFogType = 0; var sceneFogType = 0; var fog = scene.fog; if ( fog ) { _gl.uniform3f( uniforms.fogColor, fog.color.r, fog.color.g, fog.color.b ); if ( fog instanceof THREE.Fog ) { _gl.uniform1f( uniforms.fogNear, fog.near ); _gl.uniform1f( uniforms.fogFar, fog.far ); _gl.uniform1i( uniforms.fogType, 1 ); oldFogType = 1; sceneFogType = 1; } else if ( fog instanceof THREE.FogExp2 ) { _gl.uniform1f( uniforms.fogDensity, fog.density ); _gl.uniform1i( uniforms.fogType, 2 ); oldFogType = 2; sceneFogType = 2; } } else { _gl.uniform1i( uniforms.fogType, 0 ); oldFogType = 0; sceneFogType = 0; } // update positions and sort var i, sprite, material, fogType, scale = []; for( i = 0; i < nSprites; i ++ ) { sprite = sprites[ i ]; material = sprite.material; if ( sprite.visible === false ) continue; sprite._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, sprite.matrixWorld ); sprite.z = - sprite._modelViewMatrix.elements[ 14 ]; } sprites.sort( painterSortStable ); // render all sprites for( i = 0; i < nSprites; i ++ ) { sprite = sprites[ i ]; if ( sprite.visible === false ) continue; material = sprite.material; _gl.uniform1f( uniforms.alphaTest, material.alphaTest ); _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, sprite._modelViewMatrix.elements ); scale[ 0 ] = sprite.scale.x; scale[ 1 ] = sprite.scale.y; if ( scene.fog && material.fog ) { fogType = sceneFogType; } else { fogType = 0; } if ( oldFogType !== fogType ) { _gl.uniform1i( uniforms.fogType, fogType ); oldFogType = fogType; } if ( material.map !== null ) { _gl.uniform2f( uniforms.uvOffset, material.map.offset.x, material.map.offset.y ); _gl.uniform2f( uniforms.uvScale, material.map.repeat.x, material.map.repeat.y ); } else { _gl.uniform2f( uniforms.uvOffset, 0, 0 ); _gl.uniform2f( uniforms.uvScale, 1, 1 ); } _gl.uniform1f( uniforms.opacity, material.opacity ); _gl.uniform3f( uniforms.color, material.color.r, material.color.g, material.color.b ); _gl.uniform1f( uniforms.rotation, material.rotation ); _gl.uniform2fv( uniforms.scale, scale ); _renderer.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst ); _renderer.setDepthTest( material.depthTest ); _renderer.setDepthWrite( material.depthWrite ); if ( material.map && material.map.image && material.map.image.width ) { _renderer.setTexture( material.map, 0 ); } else { _renderer.setTexture( _texture, 0 ); } _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 ); } // restore gl _gl.enable( _gl.CULL_FACE ); }; function createProgram () { var program = _gl.createProgram(); var vertexShader = _gl.createShader( _gl.VERTEX_SHADER ); var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER ); _gl.shaderSource( vertexShader, [ 'precision ' + _renderer.getPrecision() + ' float;', 'uniform mat4 modelViewMatrix;', 'uniform mat4 projectionMatrix;', 'uniform float rotation;', 'uniform vec2 scale;', 'uniform vec2 uvOffset;', 'uniform vec2 uvScale;', 'attribute vec2 position;', 'attribute vec2 uv;', 'varying vec2 vUV;', 'void main() {', 'vUV = uvOffset + uv * uvScale;', 'vec2 alignedPosition = position * scale;', 'vec2 rotatedPosition;', 'rotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;', 'rotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;', 'vec4 finalPosition;', 'finalPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );', 'finalPosition.xy += rotatedPosition;', 'finalPosition = projectionMatrix * finalPosition;', 'gl_Position = finalPosition;', '}' ].join( '\n' ) ); _gl.shaderSource( fragmentShader, [ 'precision ' + _renderer.getPrecision() + ' float;', 'uniform vec3 color;', 'uniform sampler2D map;', 'uniform float opacity;', 'uniform int fogType;', 'uniform vec3 fogColor;', 'uniform float fogDensity;', 'uniform float fogNear;', 'uniform float fogFar;', 'uniform float alphaTest;', 'varying vec2 vUV;', 'void main() {', 'vec4 texture = texture2D( map, vUV );', 'if ( texture.a < alphaTest ) discard;', 'gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );', 'if ( fogType > 0 ) {', 'float depth = gl_FragCoord.z / gl_FragCoord.w;', 'float fogFactor = 0.0;', 'if ( fogType == 1 ) {', 'fogFactor = smoothstep( fogNear, fogFar, depth );', '} else {', 'const float LOG2 = 1.442695;', 'float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );', 'fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );', '}', 'gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );', '}', '}' ].join( '\n' ) ); _gl.compileShader( vertexShader ); _gl.compileShader( fragmentShader ); _gl.attachShader( program, vertexShader ); _gl.attachShader( program, fragmentShader ); _gl.linkProgram( program ); return program; }; function painterSortStable ( a, b ) { if ( a.z !== b.z ) { return b.z - a.z; } else { return b.id - a.id; } }; }; /** * @author alteredq / http://alteredqualia.com/ */ THREE.DepthPassPlugin = function () { this.enabled = false; this.renderTarget = null; var _gl, _renderer, _depthMaterial, _depthMaterialMorph, _depthMaterialSkin, _depthMaterialMorphSkin, _frustum = new THREE.Frustum(), _projScreenMatrix = new THREE.Matrix4(); this.init = function ( renderer ) { _gl = renderer.context; _renderer = renderer; var depthShader = THREE.ShaderLib[ "depthRGBA" ]; var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms ); _depthMaterial = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms } ); _depthMaterialMorph = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, morphTargets: true } ); _depthMaterialSkin = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, skinning: true } ); _depthMaterialMorphSkin = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, morphTargets: true, skinning: true } ); _depthMaterial._shadowPass = true; _depthMaterialMorph._shadowPass = true; _depthMaterialSkin._shadowPass = true; _depthMaterialMorphSkin._shadowPass = true; }; this.render = function ( scene, camera ) { if ( ! this.enabled ) return; this.update( scene, camera ); }; this.update = function ( scene, camera ) { var i, il, j, jl, n, program, buffer, material, webglObject, object, light, renderList, fog = null; // set GL state for depth map _gl.clearColor( 1, 1, 1, 1 ); _gl.disable( _gl.BLEND ); _renderer.setDepthTest( true ); // update scene if ( scene.autoUpdate === true ) scene.updateMatrixWorld(); // update camera matrices and frustum camera.matrixWorldInverse.getInverse( camera.matrixWorld ); _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); _frustum.setFromMatrix( _projScreenMatrix ); // render depth map _renderer.setRenderTarget( this.renderTarget ); _renderer.clear(); // set object matrices & frustum culling renderList = scene.__webglObjects; for ( j = 0, jl = renderList.length; j < jl; j ++ ) { webglObject = renderList[ j ]; object = webglObject.object; webglObject.render = false; if ( object.visible ) { if ( ! ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem ) || ! ( object.frustumCulled ) || _frustum.intersectsObject( object ) ) { object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld ); webglObject.render = true; } } } // render regular objects var objectMaterial, useMorphing, useSkinning; for ( j = 0, jl = renderList.length; j < jl; j ++ ) { webglObject = renderList[ j ]; if ( webglObject.render ) { object = webglObject.object; buffer = webglObject.buffer; // todo: create proper depth material for particles if ( object instanceof THREE.ParticleSystem && !object.customDepthMaterial ) continue; objectMaterial = getObjectMaterial( object ); if ( objectMaterial ) _renderer.setMaterialFaces( object.material ); useMorphing = object.geometry.morphTargets.length > 0 && objectMaterial.morphTargets; useSkinning = object instanceof THREE.SkinnedMesh && objectMaterial.skinning; if ( object.customDepthMaterial ) { material = object.customDepthMaterial; } else if ( useSkinning ) { material = useMorphing ? _depthMaterialMorphSkin : _depthMaterialSkin; } else if ( useMorphing ) { material = _depthMaterialMorph; } else { material = _depthMaterial; } if ( buffer instanceof THREE.BufferGeometry ) { _renderer.renderBufferDirect( camera, scene.__lights, fog, material, buffer, object ); } else { _renderer.renderBuffer( camera, scene.__lights, fog, material, buffer, object ); } } } // set matrices and render immediate objects renderList = scene.__webglObjectsImmediate; for ( j = 0, jl = renderList.length; j < jl; j ++ ) { webglObject = renderList[ j ]; object = webglObject.object; if ( object.visible ) { object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld ); _renderer.renderImmediateObject( camera, scene.__lights, fog, _depthMaterial, object ); } } // restore GL state var clearColor = _renderer.getClearColor(), clearAlpha = _renderer.getClearAlpha(); _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha ); _gl.enable( _gl.BLEND ); }; // For the moment just ignore objects that have multiple materials with different animation methods // Only the first material will be taken into account for deciding which depth material to use function getObjectMaterial( object ) { return object.material instanceof THREE.MeshFaceMaterial ? object.material.materials[ 0 ] : object.material; }; }; /** * @author mikael emtinger / http://gomo.se/ */ THREE.ShaderFlares = { 'lensFlareVertexTexture': { vertexShader: [ "uniform lowp int renderType;", "uniform vec3 screenPosition;", "uniform vec2 scale;", "uniform float rotation;", "uniform sampler2D occlusionMap;", "attribute vec2 position;", "attribute vec2 uv;", "varying vec2 vUV;", "varying float vVisibility;", "void main() {", "vUV = uv;", "vec2 pos = position;", "if( renderType == 2 ) {", "vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) );", "visibility += texture2D( occlusionMap, vec2( 0.5, 0.1 ) );", "visibility += texture2D( occlusionMap, vec2( 0.9, 0.1 ) );", "visibility += texture2D( occlusionMap, vec2( 0.9, 0.5 ) );", "visibility += texture2D( occlusionMap, vec2( 0.9, 0.9 ) );", "visibility += texture2D( occlusionMap, vec2( 0.5, 0.9 ) );", "visibility += texture2D( occlusionMap, vec2( 0.1, 0.9 ) );", "visibility += texture2D( occlusionMap, vec2( 0.1, 0.5 ) );", "visibility += texture2D( occlusionMap, vec2( 0.5, 0.5 ) );", "vVisibility = visibility.r / 9.0;", "vVisibility *= 1.0 - visibility.g / 9.0;", "vVisibility *= visibility.b / 9.0;", "vVisibility *= 1.0 - visibility.a / 9.0;", "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;", "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;", "}", "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );", "}" ].join( "\n" ), fragmentShader: [ "uniform lowp int renderType;", "uniform sampler2D map;", "uniform float opacity;", "uniform vec3 color;", "varying vec2 vUV;", "varying float vVisibility;", "void main() {", // pink square "if( renderType == 0 ) {", "gl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );", // restore "} else if( renderType == 1 ) {", "gl_FragColor = texture2D( map, vUV );", // flare "} else {", "vec4 texture = texture2D( map, vUV );", "texture.a *= opacity * vVisibility;", "gl_FragColor = texture;", "gl_FragColor.rgb *= color;", "}", "}" ].join( "\n" ) }, 'lensFlare': { vertexShader: [ "uniform lowp int renderType;", "uniform vec3 screenPosition;", "uniform vec2 scale;", "uniform float rotation;", "attribute vec2 position;", "attribute vec2 uv;", "varying vec2 vUV;", "void main() {", "vUV = uv;", "vec2 pos = position;", "if( renderType == 2 ) {", "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;", "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;", "}", "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );", "}" ].join( "\n" ), fragmentShader: [ "precision mediump float;", "uniform lowp int renderType;", "uniform sampler2D map;", "uniform sampler2D occlusionMap;", "uniform float opacity;", "uniform vec3 color;", "varying vec2 vUV;", "void main() {", // pink square "if( renderType == 0 ) {", "gl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );", // restore "} else if( renderType == 1 ) {", "gl_FragColor = texture2D( map, vUV );", // flare "} else {", "float visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 ) ).a;", "visibility += texture2D( occlusionMap, vec2( 0.9, 0.5 ) ).a;", "visibility += texture2D( occlusionMap, vec2( 0.5, 0.9 ) ).a;", "visibility += texture2D( occlusionMap, vec2( 0.1, 0.5 ) ).a;", "visibility = ( 1.0 - visibility / 4.0 );", "vec4 texture = texture2D( map, vUV );", "texture.a *= opacity * visibility;", "gl_FragColor = texture;", "gl_FragColor.rgb *= color;", "}", "}" ].join( "\n" ) } };
/*! * Angular Material Design * https://github.com/angular/material * @license MIT * v0.9.6 */ !(function(e, t, n) { 'use strict'; function r(e, t) { e.decorator('$$rAF', ['$delegate', o]), t .theme('default') .primaryPalette('indigo') .accentPalette('pink') .warnPalette('red') .backgroundPalette('grey'); } function o(e) { return ( (e.throttle = function(t) { var n, r, o, i; return function() { (n = arguments), (i = this), (o = t), r || ((r = !0), e(function() { o.apply(i, n), (r = !1); })); }; }), e ); } function i(e, t) { function n(e) { return r ? 'webkit' + e.charAt(0).toUpperCase() + e.substring(1) : e; } var r = /webkit/i.test(t.vendorPrefix); return { KEY_CODE: { ENTER: 13, ESCAPE: 27, SPACE: 32, LEFT_ARROW: 37, UP_ARROW: 38, RIGHT_ARROW: 39, DOWN_ARROW: 40, TAB: 9, BACKSPACE: 8, DELETE: 46, }, CSS: { TRANSITIONEND: 'transitionend' + (r ? ' webkitTransitionEnd' : ''), ANIMATIONEND: 'animationend' + (r ? ' webkitAnimationEnd' : ''), TRANSFORM: n('transform'), TRANSFORM_ORIGIN: n('transformOrigin'), TRANSITION: n('transition'), TRANSITION_DURATION: n('transitionDuration'), ANIMATION_PLAY_STATE: n('animationPlayState'), ANIMATION_DURATION: n('animationDuration'), ANIMATION_NAME: n('animationName'), ANIMATION_TIMING: n('animationTimingFunction'), ANIMATION_DIRECTION: n('animationDirection'), }, MEDIA: { sm: '(max-width: 600px)', 'gt-sm': '(min-width: 600px)', md: '(min-width: 600px) and (max-width: 960px)', 'gt-md': '(min-width: 960px)', lg: '(min-width: 960px) and (max-width: 1200px)', 'gt-lg': '(min-width: 1200px)', }, MEDIA_PRIORITY: ['gt-lg', 'lg', 'gt-md', 'md', 'gt-sm', 'sm'], }; } function a(e, n) { function r() { return [].concat($); } function o() { return $.length; } function i(e) { return $.length && e > -1 && e < $.length; } function a(e) { return e ? i(d(e) + 1) : !1; } function c(e) { return e ? i(d(e) - 1) : !1; } function s(e) { return i(e) ? $[e] : null; } function f(e, t) { return $.filter(function(n) { return n[e] === t; }); } function l(e, n) { return e ? (t.isNumber(n) || (n = $.length), $.splice(n, 0, e), d(e)) : -1; } function u(e) { h(e) && $.splice(d(e), 1); } function d(e) { return $.indexOf(e); } function h(e) { return e && d(e) > -1; } function p() { return $.length ? $[0] : null; } function m() { return $.length ? $[$.length - 1] : null; } function g(e, r, o, a) { o = o || v; for (var c = d(r); ; ) { if (!i(c)) return null; var s = c + (e ? -1 : 1), f = null; if ( (i(s) ? (f = $[s]) : n && ((f = e ? m() : p()), (s = d(f))), null === f || s === a) ) return null; if (o(f)) return f; t.isUndefined(a) && (a = s), (c = s); } } var v = function() { return !0; }; e && !t.isArray(e) && (e = Array.prototype.slice.call(e)), (n = !!n); var $ = e || []; return { items: r, count: o, inRange: i, contains: h, indexOf: d, itemAt: s, findBy: f, add: l, remove: u, first: p, last: m, next: t.bind(null, g, !1), previous: t.bind(null, g, !0), hasPrevious: c, hasNext: a, }; } function c(e, n, r) { function o(e) { var n = d[e]; t.isUndefined(n) && (n = d[e] = i(e)); var r = p[n]; return t.isUndefined(r) && (r = a(n)), r; } function i(t) { return e.MEDIA[t] || ('(' !== t.charAt(0) ? '(' + t + ')' : t); } function a(e) { var t = (h[e] = r.matchMedia(e)); return t.addListener(c), (p[t.media] = !!t.matches); } function c(e) { n.$evalAsync(function() { p[e.media] = !!e.matches; }); } function s(e) { return h[e]; } function f(t, n) { for (var r = 0; r < e.MEDIA_PRIORITY.length; r++) { var o = e.MEDIA_PRIORITY[r]; if (h[d[o]].matches) { var i = u(t, n + '-' + o); if (t[i]) return t[i]; } } return t[u(t, n)]; } function l(n, r, o) { var i = []; return ( n.forEach(function(n) { var a = u(r, n); r[a] && i.push(r.$observe(a, t.bind(void 0, o, null))); for (var c in e.MEDIA) { if (((a = u(r, n + '-' + c)), !r[a])) return; i.push(r.$observe(a, t.bind(void 0, o, c))); } }), function() { i.forEach(function(e) { e(); }); } ); } function u(e, t) { return m[t] || (m[t] = e.$normalize(t)); } var d = {}, h = {}, p = {}, m = {}; return ( (o.getResponsiveAttribute = f), (o.getQuery = s), (o.watchResponsiveAttributes = l), o ); } function s(e, n, r) { function o(e, r, o) { var i = e[0] || e; !i || (i.hasAttribute(r) && 0 !== i.getAttribute(r).length) || s(i, r) || ((o = t.isString(o) ? o.trim() : ''), o.length ? e.attr(r, o) : n.warn( 'ARIA: Attribute "', r, '", required for accessibility, is missing on node:', i, )); } function i(t, n, r) { e(function() { o(t, n, r()); }); } function a(e, t) { i(e, t, function() { return c(e); }); } function c(e) { return e.text().trim(); } function s(e, t) { function n(e) { var t = e.currentStyle ? e.currentStyle : r.getComputedStyle(e); return 'none' === t.display; } var o = e.hasChildNodes(), i = !1; if (o) for (var a = e.childNodes, c = 0; c < a.length; c++) { var s = a[c]; 1 === s.nodeType && s.hasAttribute(t) && (n(s) || (i = !0)); } return i; } return { expect: o, expectAsync: i, expectWithText: a }; } function f(e, n, r, o, i, a) { this.compile = function(c) { var s = c.templateUrl, f = c.template || '', l = c.controller, u = c.controllerAs, d = c.resolve || {}, h = c.locals || {}, p = c.transformTemplate || t.identity, m = c.bindToController; return ( t.forEach(d, function(e, n) { d[n] = t.isString(e) ? r.get(e) : r.invoke(e); }), t.extend(d, h), (d.$template = s ? n.get(s, { cache: a }).then(function(e) { return e.data; }) : e.when(f)), e.all(d).then(function(e) { var n = p(e.$template), r = c.element || t .element('<div>') .html(n.trim()) .contents(), a = o(r); return { locals: e, element: r, link: function(n) { if (((e.$scope = n), l)) { var o = i(l, e, !0); m && t.extend(o.instance, e); var c = o(); r.data('$ngControllerController', c), r.children().data('$ngControllerController', c), u && (n[u] = c); } return a(n); }, }; }) ); }; } function l() {} function u(n, r, o) { function i(e, t, n) { var r = P[t.replace(/^\$md./, '')]; if (!r) throw new Error( 'Failed to register element with handler ' + t + '. Available handlers: ' + Object.keys(P).join(', '), ); return r.registerElement(e, n); } function a(e, r) { var o = new n(e); return t.extend(o, r), (P[e] = o), u; } var c = navigator.userAgent || navigator.vendor || e.opera, s = c.match(/ipad|iphone|ipod/i), f = c.match(/android/i), l = 'undefined' != typeof e.jQuery && t.element === e.jQuery, u = { handler: a, register: i, isHijackingClicks: (s || f) && !l && !j }; return ( u.isHijackingClicks && u.handler('click', { options: { maxDistance: 6 }, onEnd: function(e, t) { t.distance < this.state.options.maxDistance && this.dispatchEvent(e, 'click'); }, }), u .handler('press', { onStart: function(e, t) { this.dispatchEvent(e, '$md.pressdown'); }, onEnd: function(e, t) { this.dispatchEvent(e, '$md.pressup'); }, }) .handler('hold', { options: { maxDistance: 6, delay: 500 }, onCancel: function() { o.cancel(this.state.timeout); }, onStart: function(e, n) { return this.state.registeredParent ? ((this.state.pos = { x: n.x, y: n.y }), void (this.state.timeout = o( t.bind(this, function() { this.dispatchEvent(e, '$md.hold'), this.cancel(); }), this.state.options.delay, !1, ))) : this.cancel(); }, onMove: function(e, t) { e.preventDefault(); var n = this.state.pos.x - t.x, r = this.state.pos.y - t.y; Math.sqrt(n * n + r * r) > this.options.maxDistance && this.cancel(); }, onEnd: function() { this.onCancel(); }, }) .handler('drag', { options: { minDistance: 6, horizontal: !0, cancelMultiplier: 1.5 }, onStart: function(e) { this.state.registeredParent || this.cancel(); }, onMove: function(e, t) { var n, r; e.preventDefault(), this.state.dragPointer ? this.dispatchDragMove(e) : (this.state.options.horizontal ? ((n = Math.abs(t.distanceX) > this.state.options.minDistance), (r = Math.abs(t.distanceY) > this.state.options.minDistance * this.state.options.cancelMultiplier)) : ((n = Math.abs(t.distanceY) > this.state.options.minDistance), (r = Math.abs(t.distanceX) > this.state.options.minDistance * this.state.options.cancelMultiplier)), n ? ((this.state.dragPointer = m(e)), v(e, this.state.dragPointer), this.dispatchEvent( e, '$md.dragstart', this.state.dragPointer, )) : r && this.cancel()); }, dispatchDragMove: r.throttle(function(e) { this.state.isRunning && (v(e, this.state.dragPointer), this.dispatchEvent(e, '$md.drag', this.state.dragPointer)); }), onEnd: function(e, t) { this.state.dragPointer && (v(e, this.state.dragPointer), this.dispatchEvent(e, '$md.dragend', this.state.dragPointer)); }, }) .handler('swipe', { options: { minVelocity: 0.65, minDistance: 10 }, onEnd: function(e, t) { if ( Math.abs(t.velocityX) > this.state.options.minVelocity && Math.abs(t.distanceX) > this.state.options.minDistance ) { var n = 'left' == t.directionX ? '$md.swipeleft' : '$md.swiperight'; this.dispatchEvent(e, n); } }, }) ); } function d(e) { (this.name = e), (this.state = {}); } function h() { function n(e, n, r) { r = r || N; var o = new t.element.Event(n); (o.$material = !0), (o.pointer = r), (o.srcEvent = e), t.extend(o, { clientX: r.x, clientY: r.y, screenX: r.x, screenY: r.y, pageX: r.x, pageY: r.y, ctrlKey: e.ctrlKey, altKey: e.altKey, shiftKey: e.shiftKey, metaKey: e.metaKey, }), t.element(r.target).trigger(o); } function r(t, n, r) { r = r || N; var o; 'click' === n ? ((o = document.createEvent('MouseEvents')), o.initMouseEvent( 'click', !0, !0, e, t.detail, r.x, r.y, r.x, r.y, t.ctrlKey, t.altKey, t.shiftKey, t.metaKey, t.button, t.relatedTarget || null, )) : ((o = document.createEvent('CustomEvent')), o.initCustomEvent(n, !0, !0, {})), (o.$material = !0), (o.pointer = r), (o.srcEvent = t), r.target.dispatchEvent(o); } var o = 'undefined' != typeof e.jQuery && t.element === e.jQuery; return ( (d.prototype = { options: {}, dispatchEvent: o ? n : r, onStart: t.noop, onMove: t.noop, onEnd: t.noop, onCancel: t.noop, start: function(e, n) { if (!this.state.isRunning) { var r = this.getNearestParent(e.target), o = (r && r.$mdGesture[this.name]) || {}; (this.state = { isRunning: !0, options: t.extend({}, this.options, o), registeredParent: r, }), this.onStart(e, n); } }, move: function(e, t) { this.state.isRunning && this.onMove(e, t); }, end: function(e, t) { this.state.isRunning && (this.onEnd(e, t), (this.state.isRunning = !1)); }, cancel: function(e, t) { this.onCancel(e, t), (this.state = {}); }, getNearestParent: function(e) { for (var t = e; t; ) { if ((t.$mdGesture || {})[this.name]) return t; t = t.parentNode; } return null; }, registerElement: function(e, t) { function n() { delete e[0].$mdGesture[r.name], e.off('$destroy', n); } var r = this; return ( (e[0].$mdGesture = e[0].$mdGesture || {}), (e[0].$mdGesture[this.name] = t || {}), e.on('$destroy', n), n ); }, }), d ); } function p(e, n) { function r(e, t) { var r; for (var o in P) (r = P[o]), r instanceof n && ('start' === e && r.cancel(), r[e](t, N)); } function o(e) { if (!N) { var t = +Date.now(); (O && !g(e, O) && t - O.endTime < 1500) || ((N = m(e)), r('start', e)); } } function i(e) { N && g(e, N) && (v(e, N), r('move', e)); } function a(e) { N && g(e, N) && (v(e, N), (N.endTime = +Date.now()), r('end', e), (O = N), (N = null)); } document.contains || (document.contains = function(e) { return document.body.contains(e); }), !L && e.isHijackingClicks && (document.addEventListener( 'click', function(e) { var t = 0 === e.clientX && 0 === e.clientY; t || e.$material || e.isIonicTap || (e.preventDefault(), e.stopPropagation()); }, !0, ), (L = !0)); var c = 'mousedown touchstart pointerdown', s = 'mousemove touchmove pointermove', f = 'mouseup mouseleave touchend touchcancel pointerup pointercancel'; t.element(document) .on(c, o) .on(s, i) .on(f, a) .on('$$mdGestureReset', function() { O = N = null; }); } function m(e) { var t = $(e), n = { startTime: +Date.now(), target: e.target, type: e.type.charAt(0) }; return (n.startX = n.x = t.pageX), (n.startY = n.y = t.pageY), n; } function g(e, t) { return e && t && e.type.charAt(0) === t.type; } function v(e, t) { var n = $(e), r = (t.x = n.pageX), o = (t.y = n.pageY); (t.distanceX = r - t.startX), (t.distanceY = o - t.startY), (t.distance = Math.sqrt( t.distanceX * t.distanceX + t.distanceY * t.distanceY, )), (t.directionX = t.distanceX > 0 ? 'right' : t.distanceX < 0 ? 'left' : ''), (t.directionY = t.distanceY > 0 ? 'up' : t.distanceY < 0 ? 'down' : ''), (t.duration = +Date.now() - t.startTime), (t.velocityX = t.distanceX / t.duration), (t.velocityY = t.distanceY / t.duration); } function $(e) { return ( (e = e.originalEvent || e), (e.touches && e.touches[0]) || (e.changedTouches && e.changedTouches[0]) || e ); } function b() { function e(e) { function n(e) { return ( (s.optionsFactory = e.options), (s.methods = (e.methods || []).concat(a)), f ); } function r(e, t) { return (c[e] = t), f; } function o(t, n) { if ( ((n = n || {}), (n.methods = n.methods || []), (n.options = n.options || function() { return {}; }), /^cancel|hide|show$/.test(t)) ) throw new Error("Preset '" + t + "' in " + e + ' is reserved!'); if (n.methods.indexOf('_options') > -1) throw new Error("Method '_options' in " + e + ' is reserved!'); return ( (s.presets[t] = { methods: n.methods.concat(a), optionsFactory: n.options, argOption: n.argOption, }), f ); } function i(n, r, o) { function i(e) { return ( e && e._options && (e = e._options), u.show(t.extend({}, l, e)) ); } function a(t, n) { var r = {}; return ( (r[e] = d), o.invoke( t || function() { return n; }, {}, r, ) ); } var f, l, u = n(), d = { hide: u.hide, cancel: u.cancel, show: i }; return ( (f = s.methods || []), (l = a(s.optionsFactory, {})), t.forEach(c, function(e, t) { d[t] = e; }), t.forEach(s.presets, function(e, n) { function r(e) { this._options = t.extend({}, o, e); } var o = a(e.optionsFactory, {}), i = (e.methods || []).concat(f); if ( (t.extend(o, { $type: n }), t.forEach(i, function(e) { r.prototype[e] = function(t) { return (this._options[e] = t), this; }; }), e.argOption) ) { var c = 'show' + n.charAt(0).toUpperCase() + n.slice(1); d[c] = function(e) { var t = d[n](e); return d.show(t); }; } d[n] = function(n) { return arguments.length && e.argOption && !t.isObject(n) && !t.isArray(n) ? new r()[e.argOption](n) : new r(n); }; }), d ); } var a = ['onHide', 'onShow', 'onRemove'], c = {}, s = { presets: {} }, f = { setDefaults: n, addPreset: o, addMethod: r, $get: i }; return ( f.addPreset('build', { methods: [ 'controller', 'controllerAs', 'resolve', 'template', 'templateUrl', 'themable', 'transformTemplate', 'parent', ], }), (i.$inject = ['$$interimElement', '$animate', '$injector']), f ); } function r(e, r, o, i, a, c, s, f, l) { function u(e) { return e && t.isString(e) ? e.replace(/\{\{/g, d).replace(/}}/g, h) : e; } var d = s.startSymbol(), h = s.endSymbol(), p = '{{' === d && '}}' === h, m = p ? t.identity : u; return function() { function s(e) { if (g.length) return p.cancel().then(function() { return s(e); }); var t = new h(e); return ( g.push(t), t.show().then(function() { return t.deferred.promise; }) ); } function u(e) { var t = g.shift(); return ( t && t.remove().then(function() { t.deferred.resolve(e); }) ); } function d(e) { var t = g.shift(); return r.when( t && t.remove().then(function() { t.deferred.reject(e); }), ); } function h(s) { var u, d, h, g, v; return ( (s = s || {}), (s = t.extend( { preserveScope: !1, scope: s.scope || o.$new(s.isolateScope), onShow: function(e, t, n) { return c.enter(t, n.parent); }, onRemove: function(e, t, n) { return (t && c.leave(t)) || r.when(); }, }, s, )), s.template && (s.template = m(s.template)), (u = { options: s, deferred: r.defer(), show: function() { var n; return ( (n = s.skipCompile ? r(function(e) { e({ locals: {}, link: function() { return s.element; }, }); }) : f.compile(s)), (g = n.then( function(n) { function o() { s.hideDelay && (d = i(p.cancel, s.hideDelay)); } if ( (t.extend(n.locals, u.options), (h = n.link(s.scope)), t.isFunction(s.parent) ? (s.parent = s.parent(s.scope, h, s)) : t.isString(s.parent) && (s.parent = t.element( e[0].querySelector(s.parent), )), !(s.parent || {}).length) ) { var c; a[0] && a[0].querySelector && (c = a[0].querySelector(':not(svg) > body')), c || (c = a[0]), '#comment' == c.nodeName && (c = e[0].body), (s.parent = t.element(c)); } s.themable && l(h); var f = s.onShow(s.scope, h, s); return r.when(f).then(function() { (s.onComplete || t.noop)(s.scope, h, s), o(); }); }, function(e) { (g = !0), u.deferred.reject(e); }, )) ); }, cancelTimeout: function() { d && (i.cancel(d), (d = n)); }, remove: function() { return ( u.cancelTimeout(), (v = r.when(g).then(function() { var e = h ? s.onRemove(s.scope, h, s) : !0; return r.when(e).then(function() { s.preserveScope || s.scope.$destroy(), (v = !0); }); })) ); }, }) ); } var p, g = []; return (p = { show: s, hide: u, cancel: d }); }; } return ( (e.$get = r), (r.$inject = [ '$document', '$q', '$rootScope', '$timeout', '$rootElement', '$animate', '$interpolate', '$mdCompiler', '$mdTheming', ]), e ); } function A(e, n) { function r(e) { return e && '' !== e; } var o, i = [], a = {}; return (o = { notFoundError: function(t) { e.error('No instance found for handle', t); }, getInstances: function() { return i; }, get: function(e) { if (!r(e)) return null; var t, n, o; for (t = 0, n = i.length; n > t; t++) if (((o = i[t]), o.$$mdHandle === e)) return o; return null; }, register: function(e, n) { function r() { var t = i.indexOf(e); -1 !== t && i.splice(t, 1); } function o() { var t = a[n]; t && (t.resolve(e), delete a[n]); } return n ? ((e.$$mdHandle = n), i.push(e), o(), r) : t.noop; }, when: function(e) { if (r(e)) { var t = n.defer(), i = o.get(e); return i ? t.resolve(i) : (a[e] = t), t.promise; } return n.reject('Invalid `md-component-id` value.'); }, }); } function y(e, n) { return { controller: t.noop, link: function(t, r, o) { o.hasOwnProperty('mdInkRippleCheckbox') ? n.attach(t, r) : e.attach(t, r); }, }; } function C(e, n) { function r(r, o, i) { function a() { var e = o.data('$mdRippleContainer'); return e ? e : ((e = t.element('<div class="md-ripple-container">')), o.append(e), o.data('$mdRippleContainer', e), e); } function c(e) { function t(e) { var t = '#' === e.charAt(0) ? e.substr(1) : e, n = t.length / 3, r = t.substr(0, n), o = t.substr(n, n), i = t.substr(2 * n); return ( 1 === n && ((r += r), (o += o), (i += i)), 'rgba(' + parseInt(r, 16) + ',' + parseInt(o, 16) + ',' + parseInt(i, 16) + ',0.1)' ); } function n(e) { return e.replace(')', ', 0.1)').replace('(', 'a('); } if (e) return 0 === e.indexOf('rgba') ? e.replace(/\d?\.?\d*\s*\)\s*$/, '0.1)') : 0 === e.indexOf('rgb') ? n(e) : 0 === e.indexOf('#') ? t(e) : void 0; } function s(e, t) { v.splice(v.indexOf(e), 1), 0 === v.length && a().css({ backgroundColor: '' }), n( function() { e.remove(); }, t, !1, ); } function f(e) { var t = v.indexOf(e), n = $[t] || {}, r = v.length > 1 ? !1 : A, o = v.length > 1 ? !1 : y; r || n.animating || o ? e.addClass('md-ripple-visible') : e && (e.removeClass('md-ripple-visible'), i.outline && e.css({ width: p + 'px', height: p + 'px', marginLeft: -1 * p + 'px', marginTop: -1 * p + 'px', }), s(e, i.outline ? 450 : 650)); } function l(r, s) { function l(e) { var n = t.element( '<div class="md-ripple" data-counter="' + g++ + '">', ); return ( v.unshift(n), $.unshift({ animating: !0 }), h.append(n), e && n.css(e), n ); } function u(e, t) { var n, r, o, a = h.prop('offsetWidth'), c = h.prop('offsetHeight'); return ( i.isMenuItem ? (r = Math.sqrt(Math.pow(a, 2) + Math.pow(c, 2))) : i.outline ? ((o = C.getBoundingClientRect()), (e -= o.left), (t -= o.top), (a = Math.max(e, a - e)), (c = Math.max(t, c - t)), (r = 2 * Math.sqrt(Math.pow(a, 2) + Math.pow(c, 2)))) : ((n = i.fullRipple ? 1.1 : 0.8), (r = Math.sqrt(Math.pow(a, 2) + Math.pow(c, 2)) * n), i.fitRipple && (r = Math.min(c, a, r))), r ); } function d(e, t, n) { function r(e) { return e.replace('rgba', 'rgb').replace(/,[^\),]+\)/, ')'); } var o = C.getBoundingClientRect(), a = { backgroundColor: r(E), borderColor: r(E), width: e + 'px', height: e + 'px', }; return ( i.outline ? ((a.width = 0), (a.height = 0)) : (a.marginLeft = a.marginTop = e * -0.5 + 'px'), i.center ? (a.left = a.top = '50%') : ((a.left = Math.round(((t - o.left) / h.prop('offsetWidth')) * 100) + '%'), (a.top = Math.round(((n - o.top) / h.prop('offsetHeight')) * 100) + '%')), a ); } E = c(o.attr('md-ink-ripple')) || c(e.getComputedStyle(i.colorElement[0]).color || 'rgb(0, 0, 0)'); var h = a(), m = u(r, s), b = d(m, r, s), A = l(b), y = v.indexOf(A), w = $[y] || {}; return ( (p = m), (w.animating = !0), n( function() { i.dimBackground && h.css({ backgroundColor: E }), A.addClass('md-ripple-placed md-ripple-scaled'), A.css( i.outline ? { borderWidth: 0.5 * m + 'px', marginLeft: m * -0.5 + 'px', marginTop: m * -0.5 + 'px', } : { left: '50%', top: '50%' }, ), f(A), n( function() { (w.animating = !1), f(A); }, i.outline ? 450 : 225, !1, ); }, 0, !1, ), A ); } function u(e) { h() && (l(e.pointer.x, e.pointer.y), (y = !0)); } function d() { y = !1; var e = v[v.length - 1]; n( function() { f(e); }, 0, !1, ); } function h() { function e(e) { return e && e.hasAttribute && e.hasAttribute('disabled'); } var t = C.parentNode, n = t && t.parentNode, r = n && n.parentNode; return !(e(C) || e(t) || e(n) || e(r)); } if (o.controller('mdNoInk')) return t.noop; i = t.extend( { colorElement: o, mousedown: !0, hover: !0, focus: !0, center: !1, mousedownPauseTime: 150, dimBackground: !1, outline: !1, fullRipple: !0, isMenuItem: !1, fitRipple: !1, }, i, ); var p, m = o.controller('mdInkRipple') || {}, g = 0, v = [], $ = [], b = o.attr('md-highlight'), A = !1, y = !1, C = o[0], w = o.attr('md-ripple-size'), E = c(o.attr('md-ink-ripple')) || c( (i.colorElement.length && e.getComputedStyle(i.colorElement[0]).color) || 'rgb(0, 0, 0)', ); switch (w) { case 'full': i.fullRipple = !0; break; case 'partial': i.fullRipple = !1; } return ( i.mousedown && o.on('$md.pressdown', u).on('$md.pressup', d), (m.createRipple = l), b && r.$watch(b, function(e) { (A = e), A && !v.length && n( function() { l(0, 0); }, 0, !1, ), t.forEach(v, f); }), function() { o.off('$md.pressdown', u).off('$md.pressup', d), a().remove(); } ); } return { attach: r }; } function w() { return function() { return { controller: t.noop }; }; } function E(e) { function r(e, t) { return (t = t || {}), (_[e] = i(e, t)), f; } function o(e, n) { return i(e, t.extend({}, _[e] || {}, n)); } function i(e, t) { var n = Z.filter(function(e) { return !t[e]; }); if (n.length) throw new Error( 'Missing colors %1 in palette %2!' .replace('%1', n.join(', ')) .replace('%2', e), ); return t; } function a(e, n) { if (H[e]) return H[e]; n = n || 'default'; var r = 'string' == typeof n ? H[n] : n, o = new c(e); return ( r && t.forEach(r.colors, function(e, n) { o.colors[n] = { name: e.name, hues: t.extend({}, e.hues) }; }), (H[e] = o), o ); } function c(e) { function n(e) { if (((e = 0 === arguments.length ? !0 : !!e), e !== r.isDark)) { (r.isDark = e), (r.foregroundPalette = r.isDark ? F : B), (r.foregroundShadow = r.isDark ? U : X); var n = r.isDark ? V : Q, o = r.isDark ? Q : V; return ( t.forEach(n, function(e, t) { var n = r.colors[t], i = o[t]; if (n) for (var a in n.hues) n.hues[a] === i[a] && (n.hues[a] = e[a]); }), r ); } } var r = this; (r.name = e), (r.colors = {}), (r.dark = n), n(!1), W.forEach(function(e) { var n = (r.isDark ? V : Q)[e]; (r[e + 'Palette'] = function(o, i) { var a = (r.colors[e] = { name: o, hues: t.extend({}, n, i) }); return ( Object.keys(a.hues).forEach(function(e) { if (!n[e]) throw new Error( "Invalid hue name '%1' in theme %2's %3 color %4. Available hue names: %4" .replace('%1', e) .replace('%2', r.name) .replace('%3', o) .replace('%4', Object.keys(n).join(', ')), ); }), Object.keys(a.hues) .map(function(e) { return a.hues[e]; }) .forEach(function(t) { if (-1 == Z.indexOf(t)) throw new Error( "Invalid hue value '%1' in theme %2's %3 color %4. Available hue values: %5" .replace('%1', t) .replace('%2', r.name) .replace('%3', e) .replace('%4', o) .replace('%5', Z.join(', ')), ); }), r ); }), (r[e + 'Color'] = function() { var t = Array.prototype.slice.call(arguments); return ( console.warn( '$mdThemingProviderTheme.' + e + 'Color() has been deprecated. Use $mdThemingProviderTheme.' + e + 'Palette() instead.', ), r[e + 'Palette'].apply(r, t) ); }); }); } function s(e, r) { function o(e) { return e === n || '' === e ? !0 : i.THEMES[e] !== n; } function i(t, r) { r === n && ((r = t), (t = n)), t === n && (t = e), i.inherit(r, r); } return ( (i.inherit = function(n, i) { function a(e) { o(e) || r.warn( "Attempted to use unregistered theme '" + e + "'. Register it with $mdThemingProvider.theme().", ); var t = n.data('$mdThemeName'); t && n.removeClass('md-' + t + '-theme'), n.addClass('md-' + e + '-theme'), n.data('$mdThemeName', e); } var c = i.controller('mdTheme'), s = n.attr('md-theme-watch'); if ((u || t.isDefined(s)) && 'false' != s) { var f = e.$watch(function() { return (c && c.$mdTheme) || l; }, a); n.on('$destroy', f); } else { var d = (c && c.$mdTheme) || l; a(d); } }), (i.THEMES = t.extend({}, H)), (i.defaultTheme = function() { return l; }), (i.registered = o), i ); } (_ = {}), (H = {}); var f, l = 'default', u = !1; return ( t.extend(_, e), (s.$inject = ['$rootScope', '$log']), (f = { definePalette: r, extendPalette: o, theme: a, setDefaultTheme: function(e) { l = e; }, alwaysWatchTheme: function(e) { u = e; }, $get: s, _LIGHT_DEFAULT_HUES: Q, _DARK_DEFAULT_HUES: V, _PALETTES: _, _THEMES: H, _parseRules: T, _rgba: I, }) ); } function x(e, t, n) { return { priority: 100, link: { pre: function(r, o, i) { var a = { $setTheme: function(t) { e.registered(t) || n.warn("attempted to use unregistered theme '" + t + "'"), (a.$mdTheme = t); }, }; o.data('$mdThemeController', a), a.$setTheme(t(i.mdTheme)(r)), i.$observe('mdTheme', a.$setTheme); }, }, }; } function k(e) { return e; } function T(e, n, r) { D(e, n), (r = r.replace(/THEME_NAME/g, e.name)); var o = [], i = e.colors[n], a = new RegExp('.md-' + e.name + '-theme', 'g'), c = new RegExp( '(\'|")?{{\\s*(' + n + ')-(color|contrast)-?(\\d\\.?\\d*)?\\s*}}("|\')?', 'g', ), s = /'?"?\{\{\s*([a-zA-Z]+)-(A?\d+|hue\-[0-3]|shadow)-?(\d\.?\d*)?\s*\}\}'?"?/g, f = _[i.name]; return ( (r = r.replace(s, function(t, n, r, o) { return 'foreground' === n ? 'shadow' == r ? e.foregroundShadow : e.foregroundPalette[r] || e.foregroundPalette[1] : (0 === r.indexOf('hue') && (r = e.colors[n].hues[r]), I((_[e.colors[n].name][r] || '').value, o)); })), t.forEach(i.hues, function(t, n) { var i = r.replace(c, function(e, n, r, o, i) { return I(f[t]['color' === o ? 'value' : 'contrast'], i); }); 'default' !== n && (i = i.replace(a, '.md-' + e.name + '-theme.md-' + n)), o.push(i); }), o ); } function S(e) { function n(e) { var n = e.contrastDefaultColor, r = e.contrastLightColors || [], o = e.contrastStrongLightColors || [], i = e.contrastDarkColors || []; 'string' == typeof r && (r = r.split(' ')), 'string' == typeof o && (o = o.split(' ')), 'string' == typeof i && (i = i.split(' ')), delete e.contrastDefaultColor, delete e.contrastLightColors, delete e.contrastStrongLightColors, delete e.contrastDarkColors, t.forEach(e, function(a, c) { function s() { return 'light' === n ? i.indexOf(c) > -1 ? G : o.indexOf(c) > -1 ? q : K : r.indexOf(c) > -1 ? o.indexOf(c) > -1 ? q : K : G; } if (!t.isObject(a)) { var f = R(a); if (!f) throw new Error( "Color %1, in palette %2's hue %3, is invalid. Hex or rgb(a) color expected." .replace('%1', a) .replace('%2', e.name) .replace('%3', c), ); e[c] = { value: f, contrast: s() }; } }); } var r = document.getElementsByTagName('head')[0], o = r ? r.firstElementChild : null, i = e.has('$MD_THEME_CSS') ? e.get('$MD_THEME_CSS') : ''; if (o && 0 !== i.length) { t.forEach(_, n); var a = {}, c = i .split(/\}(?!(\}|'|"|;))/) .filter(function(e) { return e && e.length; }) .map(function(e) { return e.trim() + '}'; }), s = new RegExp('md-(' + W.join('|') + ')', 'g'); W.forEach(function(e) { a[e] = ''; }), c.forEach(function(e) { for (var t, n = (e.match(s), 0); (t = W[n]); n++) if (e.indexOf('.md-' + t) > -1) return (a[t] += e); for (n = 0; (t = W[n]); n++) if (e.indexOf(t) > -1) return (a[t] += e); return (a[z] += e); }), t.forEach(H, function(e) { Y[e.name] || (W.forEach(function(t) { for (var n = T(e, t, a[t]); n.length; ) { var i = document.createElement('style'); i.setAttribute('type', 'text/css'), i.appendChild(document.createTextNode(n.shift())), r.insertBefore(i, o); } }), e.colors.primary.name == e.colors.accent.name && console.warn( '$mdThemingProvider: Using the same palette for primary and accent. This violates the material design spec.', ), (Y[e.name] = !0)); }); } } function D(e, t) { if (!_[(e.colors[t] || {}).name]) throw new Error( "You supplied an invalid color palette for theme %1's %2 palette. Available palettes: %3" .replace('%1', e.name) .replace('%2', t) .replace('%3', Object.keys(_).join(', ')), ); } function R(e) { if (t.isArray(e) && 3 == e.length) return e; if (/^rgb/.test(e)) return e .replace(/(^\s*rgba?\(|\)\s*$)/g, '') .split(',') .map(function(e, t) { return 3 == t ? parseFloat(e, 10) : parseInt(e, 10); }); if ( ('#' == e.charAt(0) && (e = e.substring(1)), /^([a-fA-F0-9]{3}){1,2}$/g.test(e)) ) { var n = e.length / 3, r = e.substr(0, n), o = e.substr(n, n), i = e.substr(2 * n); return ( 1 === n && ((r += r), (o += o), (i += i)), [parseInt(r, 16), parseInt(o, 16), parseInt(i, 16)] ); } } function I(e, n) { return e ? (4 == e.length && ((e = t.copy(e)), n ? e.pop() : (n = e.pop())), n && ('number' == typeof n || ('string' == typeof n && n.length)) ? 'rgba(' + e.join(',') + ',' + n + ')' : 'rgb(' + e.join(',') + ')') : "rgb('0,0,0')"; } t .module('material.core', [ 'material.core.gestures', 'material.core.theming', ]) .config(r), (r.$inject = ['$provide', '$mdThemingProvider']), t.module('material.core').factory('$mdConstant', i), (i.$inject = ['$$rAF', '$sniffer']), t.module('material.core').config([ '$provide', function(e) { e.decorator('$mdUtil', [ '$delegate', function(e) { return (e.iterator = a), e; }, ]); }, ]), t.module('material.core').factory('$mdMedia', c), (c.$inject = ['$mdConstant', '$rootScope', '$window']); var M = ['0', '0', '0']; t.module('material.core').factory('$mdUtil', [ '$cacheFactory', '$document', '$timeout', '$q', '$window', '$mdConstant', function(r, o, i, a, c, s) { function f(e) { return e[0] || e; } var l; return (l = { now: e.performance ? t.bind(e.performance, e.performance.now) : Date.now, clientRect: function(e, t, n) { var r = f(e); t = f(t || r.offsetParent || document.body); var o = r.getBoundingClientRect(), i = n ? t.getBoundingClientRect() : { left: 0, top: 0, width: 0, height: 0 }; return { left: o.left - i.left, top: o.top - i.top, width: o.width, height: o.height, }; }, offsetRect: function(e, t) { return l.clientRect(e, t, !0); }, disableScrollAround: function(e) { function n() { function e(e) { s.contains(e.target) && (e.preventDefault(), e.stopImmediatePropagation()); } function n(e) { e.preventDefault(); } var r = t.element( '<div class="md-scroll-mask"><div class="md-scroll-mask-bar"></div></div>', ), a = c.getComputedStyle(s), f = s.getBoundingClientRect(), l = f.width - s.clientWidth; return ( i(r[0], { zIndex: 'auto' == a.zIndex ? 2 : a.zIndex + 1, width: f.width + 'px', height: f.height + 'px', top: f.top + 'px', left: f.left + 'px', }), (r[0].firstElementChild.style.width = l + 'px'), o[0].body.appendChild(r[0]), r.on('wheel', n), r.on('touchmove', n), o.on('keydown', e), function() { r.off('wheel'), r.off('touchmove'), r[0].parentNode.removeChild(r[0]), o.off('keydown', e); } ); } function r() { var e = s.getAttribute('style') || '', t = s.scrollTop; return ( i(s, { position: 'fixed', width: '100%', overflowY: 'scroll', top: -t + 'px', }), function() { s.setAttribute('style', e), (s.scrollTop = t); } ); } function i(e, t) { for (var n in t) e.style[n] = t[n]; } function a(e) { return ( e instanceof t.element && (e = e[0]), e.scrollHeight > e.offsetHeight ); } e = e instanceof t.element ? e[0] : e; for (var s, f = e; (f = this.getClosest(f, 'MD-CONTENT', !0)); ) a(f) && (s = t.element(f)[0]); return s || ((s = o[0].body), a(s)) ? 'BODY' == s.nodeName ? r() : n() : t.noop; }, floatingScrollbars: function() { if (this.floatingScrollbars.cached === n) { var e = t.element( '<div style="width: 100%; z-index: -1; position: absolute; height: 35px; overflow-y: scroll"><div style="height: 60;"></div></div>', ); o[0].body.appendChild(e[0]), (this.floatingScrollbars.cached = e[0].offsetWidth == e[0].childNodes[0].offsetWidth), e.remove(); } return this.floatingScrollbars.cached; }, forceFocus: function(t) { var n = t[0] || t; document.addEventListener( 'click', function o(e) { e.target === n && e.$focus && (n.focus(), e.stopImmediatePropagation(), e.preventDefault(), n.removeEventListener('click', o)); }, !0, ); var r = document.createEvent('MouseEvents'); r.initMouseEvent( 'click', !1, !0, e, {}, 0, 0, 0, 0, !1, !1, !1, !1, 0, null, ), (r.$material = !0), (r.$focus = !0), n.dispatchEvent(r); }, transitionEndPromise: function(e, t) { function n(t) { (t && t.target !== e[0]) || (e.off(s.CSS.TRANSITIONEND, n), r.resolve()); } t = t || {}; var r = a.defer(); return ( e.on(s.CSS.TRANSITIONEND, n), t.timeout && i(n, t.timeout), r.promise ); }, fakeNgModel: function() { return { $fake: !0, $setTouched: t.noop, $setViewValue: function(e) { (this.$viewValue = e), this.$render(e), this.$viewChangeListeners.forEach(function(e) { e(); }); }, $isEmpty: function(e) { return 0 === ('' + e).length; }, $parsers: [], $formatters: [], $viewChangeListeners: [], $render: t.noop, }; }, debounce: function(e, t, r, o) { var a; return function() { var c = r, s = Array.prototype.slice.call(arguments); i.cancel(a), (a = i( function() { (a = n), e.apply(c, s); }, t || 10, o, )); }; }, throttle: function(e, t) { var n; return function() { var r = this, o = arguments, i = l.now(); (!n || i - n > t) && (e.apply(r, o), (n = i)); }; }, time: function(e) { var t = l.now(); return e(), l.now() - t; }, nextUid: function() { for (var e, t = M.length; t; ) { if ((t--, (e = M[t].charCodeAt(0)), 57 == e)) return (M[t] = 'A'), M.join(''); if (90 != e) return (M[t] = String.fromCharCode(e + 1)), M.join(''); M[t] = '0'; } return M.unshift('0'), M.join(''); }, disconnectScope: function(e) { if (e && e.$root !== e && !e.$$destroyed) { var t = e.$parent; (e.$$disconnected = !0), t.$$childHead === e && (t.$$childHead = e.$$nextSibling), t.$$childTail === e && (t.$$childTail = e.$$prevSibling), e.$$prevSibling && (e.$$prevSibling.$$nextSibling = e.$$nextSibling), e.$$nextSibling && (e.$$nextSibling.$$prevSibling = e.$$prevSibling), (e.$$nextSibling = e.$$prevSibling = null); } }, reconnectScope: function(e) { if (e && e.$root !== e && e.$$disconnected) { var t = e, n = t.$parent; (t.$$disconnected = !1), (t.$$prevSibling = n.$$childTail), n.$$childHead ? ((n.$$childTail.$$nextSibling = t), (n.$$childTail = t)) : (n.$$childHead = n.$$childTail = t); } }, getClosest: function(e, n, r) { if ( (e instanceof t.element && (e = e[0]), (n = n.toUpperCase()), r && (e = e.parentNode), !e) ) return null; do if (e.nodeName === n) return e; while ((e = e.parentNode)); return null; }, extractElementByName: function(e, n) { for (var r = 0, o = e.length; o > r; r++) if (e[r].nodeName.toLowerCase() === n) return t.element(e[r]); return e; }, initOptionalProperties: function(e, n, r) { (r = r || {}), t.forEach(e.$$isolateBindings, function(o, i) { if (o.optional && t.isUndefined(e[i])) { var a = n.hasOwnProperty(n.$normalize(o.attrName)); e[i] = t.isDefined(r[i]) ? r[i] : a; } }); }, }); }, ]), (t.element.prototype.focus = t.element.prototype.focus || function() { return this.length && this[0].focus(), this; }), (t.element.prototype.blur = t.element.prototype.blur || function() { return this.length && this[0].blur(), this; }), t.module('material.core').service('$mdAria', s), (s.$inject = ['$$rAF', '$log', '$window']), t.module('material.core').service('$mdCompiler', f), (f.$inject = [ '$q', '$http', '$injector', '$compile', '$controller', '$templateCache', ]); var N, O, P = {}, j = !1, L = !1; t .module('material.core.gestures', []) .provider('$mdGesture', l) .factory('$$MdGestureHandler', h) .run(p), (l.prototype = { skipClickHijack: function() { return (j = !0); }, $get: [ '$$MdGestureHandler', '$$rAF', '$timeout', function(e, t, n) { return new u(e, t, n); }, ], }), (u.$inject = ['$$MdGestureHandler', '$$rAF', '$timeout']), (p.$inject = ['$mdGesture', '$$MdGestureHandler']), t.module('material.core').provider('$$interimElement', b), t.module('material.core').factory('$mdComponentRegistry', A), (A.$inject = ['$log', '$q']), (function() { function e(e) { function n(n, o, i) { var a = r(o); return e.attach(n, o, t.extend(a, i)); } function r(e) { return e.hasClass('md-icon-button') ? { isMenuItem: e.hasClass('md-menu-item'), fitRipple: !0, center: !0, } : { isMenuItem: e.hasClass('md-menu-item'), dimBackground: !0 }; } return { attach: n }; } t.module('material.core').factory('$mdButtonInkRipple', e), (e.$inject = ['$mdInkRipple']); })(), (function() { function e(e) { function n(n, r, o) { return e.attach( n, r, t.extend({ center: !0, dimBackground: !1, fitRipple: !0 }, o), ); } return { attach: n }; } t.module('material.core').factory('$mdCheckboxInkRipple', e), (e.$inject = ['$mdInkRipple']); })(), (function() { function e(e) { function n(n, r, o) { return e.attach( n, r, t.extend( { center: !1, dimBackground: !0, outline: !1, rippleSize: 'full', }, o, ), ); } return { attach: n }; } t.module('material.core').factory('$mdListInkRipple', e), (e.$inject = ['$mdInkRipple']); })(), t .module('material.core') .factory('$mdInkRipple', C) .directive('mdInkRipple', y) .directive('mdNoInk', w()) .directive('mdNoBar', w()) .directive('mdNoStretch', w()), (y.$inject = ['$mdButtonInkRipple', '$mdCheckboxInkRipple']), (C.$inject = ['$window', '$timeout']), (function() { function e(e) { function n(n, r, o) { return e.attach( n, r, t.extend( { center: !1, dimBackground: !0, outline: !1, rippleSize: 'full', }, o, ), ); } return { attach: n }; } t.module('material.core').factory('$mdTabInkRipple', e), (e.$inject = ['$mdInkRipple']); })(), t .module('material.core.theming.palette', []) .constant('$mdColorPalette', { red: { 50: '#ffebee', 100: '#ffcdd2', 200: '#ef9a9a', 300: '#e57373', 400: '#ef5350', 500: '#f44336', 600: '#e53935', 700: '#d32f2f', 800: '#c62828', 900: '#b71c1c', A100: '#ff8a80', A200: '#ff5252', A400: '#ff1744', A700: '#d50000', contrastDefaultColor: 'light', contrastDarkColors: '50 100 200 300 400 A100', contrastStrongLightColors: '500 600 700 A200 A400 A700', }, pink: { 50: '#fce4ec', 100: '#f8bbd0', 200: '#f48fb1', 300: '#f06292', 400: '#ec407a', 500: '#e91e63', 600: '#d81b60', 700: '#c2185b', 800: '#ad1457', 900: '#880e4f', A100: '#ff80ab', A200: '#ff4081', A400: '#f50057', A700: '#c51162', contrastDefaultColor: 'light', contrastDarkColors: '50 100 200 300 400 A100', contrastStrongLightColors: '500 600 A200 A400 A700', }, purple: { 50: '#f3e5f5', 100: '#e1bee7', 200: '#ce93d8', 300: '#ba68c8', 400: '#ab47bc', 500: '#9c27b0', 600: '#8e24aa', 700: '#7b1fa2', 800: '#6a1b9a', 900: '#4a148c', A100: '#ea80fc', A200: '#e040fb', A400: '#d500f9', A700: '#aa00ff', contrastDefaultColor: 'light', contrastDarkColors: '50 100 200 A100', contrastStrongLightColors: '300 400 A200 A400 A700', }, 'deep-purple': { 50: '#ede7f6', 100: '#d1c4e9', 200: '#b39ddb', 300: '#9575cd', 400: '#7e57c2', 500: '#673ab7', 600: '#5e35b1', 700: '#512da8', 800: '#4527a0', 900: '#311b92', A100: '#b388ff', A200: '#7c4dff', A400: '#651fff', A700: '#6200ea', contrastDefaultColor: 'light', contrastDarkColors: '50 100 200 A100', contrastStrongLightColors: '300 400 A200', }, indigo: { 50: '#e8eaf6', 100: '#c5cae9', 200: '#9fa8da', 300: '#7986cb', 400: '#5c6bc0', 500: '#3f51b5', 600: '#3949ab', 700: '#303f9f', 800: '#283593', 900: '#1a237e', A100: '#8c9eff', A200: '#536dfe', A400: '#3d5afe', A700: '#304ffe', contrastDefaultColor: 'light', contrastDarkColors: '50 100 200 A100', contrastStrongLightColors: '300 400 A200 A400', }, blue: { 50: '#e3f2fd', 100: '#bbdefb', 200: '#90caf9', 300: '#64b5f6', 400: '#42a5f5', 500: '#2196f3', 600: '#1e88e5', 700: '#1976d2', 800: '#1565c0', 900: '#0d47a1', A100: '#82b1ff', A200: '#448aff', A400: '#2979ff', A700: '#2962ff', contrastDefaultColor: 'light', contrastDarkColors: '100 200 300 400 A100', contrastStrongLightColors: '500 600 700 A200 A400 A700', }, 'light-blue': { 50: '#e1f5fe', 100: '#b3e5fc', 200: '#81d4fa', 300: '#4fc3f7', 400: '#29b6f6', 500: '#03a9f4', 600: '#039be5', 700: '#0288d1', 800: '#0277bd', 900: '#01579b', A100: '#80d8ff', A200: '#40c4ff', A400: '#00b0ff', A700: '#0091ea', contrastDefaultColor: 'dark', contrastLightColors: '500 600 700 800 900 A700', contrastStrongLightColors: '500 600 700 800 A700', }, cyan: { 50: '#e0f7fa', 100: '#b2ebf2', 200: '#80deea', 300: '#4dd0e1', 400: '#26c6da', 500: '#00bcd4', 600: '#00acc1', 700: '#0097a7', 800: '#00838f', 900: '#006064', A100: '#84ffff', A200: '#18ffff', A400: '#00e5ff', A700: '#00b8d4', contrastDefaultColor: 'dark', contrastLightColors: '500 600 700 800 900', contrastStrongLightColors: '500 600 700 800', }, teal: { 50: '#e0f2f1', 100: '#b2dfdb', 200: '#80cbc4', 300: '#4db6ac', 400: '#26a69a', 500: '#009688', 600: '#00897b', 700: '#00796b', 800: '#00695c', 900: '#004d40', A100: '#a7ffeb', A200: '#64ffda', A400: '#1de9b6', A700: '#00bfa5', contrastDefaultColor: 'dark', contrastLightColors: '500 600 700 800 900', contrastStrongLightColors: '500 600 700', }, green: { 50: '#e8f5e9', 100: '#c8e6c9', 200: '#a5d6a7', 300: '#81c784', 400: '#66bb6a', 500: '#4caf50', 600: '#43a047', 700: '#388e3c', 800: '#2e7d32', 900: '#1b5e20', A100: '#b9f6ca', A200: '#69f0ae', A400: '#00e676', A700: '#00c853', contrastDefaultColor: 'dark', contrastLightColors: '500 600 700 800 900', contrastStrongLightColors: '500 600 700', }, 'light-green': { 50: '#f1f8e9', 100: '#dcedc8', 200: '#c5e1a5', 300: '#aed581', 400: '#9ccc65', 500: '#8bc34a', 600: '#7cb342', 700: '#689f38', 800: '#558b2f', 900: '#33691e', A100: '#ccff90', A200: '#b2ff59', A400: '#76ff03', A700: '#64dd17', contrastDefaultColor: 'dark', contrastLightColors: '800 900', contrastStrongLightColors: '800 900', }, lime: { 50: '#f9fbe7', 100: '#f0f4c3', 200: '#e6ee9c', 300: '#dce775', 400: '#d4e157', 500: '#cddc39', 600: '#c0ca33', 700: '#afb42b', 800: '#9e9d24', 900: '#827717', A100: '#f4ff81', A200: '#eeff41', A400: '#c6ff00', A700: '#aeea00', contrastDefaultColor: 'dark', contrastLightColors: '900', contrastStrongLightColors: '900', }, yellow: { 50: '#fffde7', 100: '#fff9c4', 200: '#fff59d', 300: '#fff176', 400: '#ffee58', 500: '#ffeb3b', 600: '#fdd835', 700: '#fbc02d', 800: '#f9a825', 900: '#f57f17', A100: '#ffff8d', A200: '#ffff00', A400: '#ffea00', A700: '#ffd600', contrastDefaultColor: 'dark', }, amber: { 50: '#fff8e1', 100: '#ffecb3', 200: '#ffe082', 300: '#ffd54f', 400: '#ffca28', 500: '#ffc107', 600: '#ffb300', 700: '#ffa000', 800: '#ff8f00', 900: '#ff6f00', A100: '#ffe57f', A200: '#ffd740', A400: '#ffc400', A700: '#ffab00', contrastDefaultColor: 'dark', }, orange: { 50: '#fff3e0', 100: '#ffe0b2', 200: '#ffcc80', 300: '#ffb74d', 400: '#ffa726', 500: '#ff9800', 600: '#fb8c00', 700: '#f57c00', 800: '#ef6c00', 900: '#e65100', A100: '#ffd180', A200: '#ffab40', A400: '#ff9100', A700: '#ff6d00', contrastDefaultColor: 'dark', contrastLightColors: '800 900', contrastStrongLightColors: '800 900', }, 'deep-orange': { 50: '#fbe9e7', 100: '#ffccbc', 200: '#ffab91', 300: '#ff8a65', 400: '#ff7043', 500: '#ff5722', 600: '#f4511e', 700: '#e64a19', 800: '#d84315', 900: '#bf360c', A100: '#ff9e80', A200: '#ff6e40', A400: '#ff3d00', A700: '#dd2c00', contrastDefaultColor: 'light', contrastDarkColors: '50 100 200 300 400 A100 A200', contrastStrongLightColors: '500 600 700 800 900 A400 A700', }, brown: { 50: '#efebe9', 100: '#d7ccc8', 200: '#bcaaa4', 300: '#a1887f', 400: '#8d6e63', 500: '#795548', 600: '#6d4c41', 700: '#5d4037', 800: '#4e342e', 900: '#3e2723', A100: '#d7ccc8', A200: '#bcaaa4', A400: '#8d6e63', A700: '#5d4037', contrastDefaultColor: 'light', contrastDarkColors: '50 100 200', contrastStrongLightColors: '300 400', }, grey: { 50: '#fafafa', 100: '#f5f5f5', 200: '#eeeeee', 300: '#e0e0e0', 400: '#bdbdbd', 500: '#9e9e9e', 600: '#757575', 700: '#616161', 800: '#424242', 900: '#212121', 1000: '#000000', A100: '#ffffff', A200: '#eeeeee', A400: '#bdbdbd', A700: '#616161', contrastDefaultColor: 'dark', contrastLightColors: '600 700 800 900', }, 'blue-grey': { 50: '#eceff1', 100: '#cfd8dc', 200: '#b0bec5', 300: '#90a4ae', 400: '#78909c', 500: '#607d8b', 600: '#546e7a', 700: '#455a64', 800: '#37474f', 900: '#263238', A100: '#cfd8dc', A200: '#b0bec5', A400: '#78909c', A700: '#455a64', contrastDefaultColor: 'light', contrastDarkColors: '50 100 200 300', contrastStrongLightColors: '400 500', }, }), t .module('material.core.theming', ['material.core.theming.palette']) .directive('mdTheme', x) .directive('mdThemable', k) .provider('$mdTheming', E) .run(S); var _, H, Y = {}, B = { name: 'dark', 1: 'rgba(0,0,0,0.87)', 2: 'rgba(0,0,0,0.54)', 3: 'rgba(0,0,0,0.26)', 4: 'rgba(0,0,0,0.12)', }, F = { name: 'light', 1: 'rgba(255,255,255,1.0)', 2: 'rgba(255,255,255,0.7)', 3: 'rgba(255,255,255,0.3)', 4: 'rgba(255,255,255,0.12)', }, U = '1px 1px 0px rgba(0,0,0,0.4), -1px -1px 0px rgba(0,0,0,0.4)', X = '', G = R('rgba(0,0,0,0.87)'), K = R('rgba(255,255,255,0.87'), q = R('rgb(255,255,255)'), W = ['primary', 'accent', 'warn', 'background'], z = 'primary', Q = { accent: { default: 'A200', 'hue-1': 'A100', 'hue-2': 'A400', 'hue-3': 'A700', }, background: { default: 'A100', 'hue-1': '300', 'hue-2': '800', 'hue-3': '900', }, }, V = { background: { default: '800', 'hue-1': '300', 'hue-2': '600', 'hue-3': '900', }, }; W.forEach(function(e) { var t = { default: '500', 'hue-1': '300', 'hue-2': '800', 'hue-3': 'A100' }; Q[e] || (Q[e] = t), V[e] || (V[e] = t); }); var Z = [ '50', '100', '200', '300', '400', '500', '600', '700', '800', '900', 'A100', 'A200', 'A400', 'A700', ]; (E.$inject = ['$mdColorPalette']), (x.$inject = ['$mdTheming', '$interpolate', '$log']), (k.$inject = ['$mdTheming']), (S.$inject = ['$injector']); })(window, window.angular);
import React, { Component } from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import * as actions from '../qualityCenterActions'; export class QualityCenterQueryViewer extends Component { componentDidMount() { console.log('QualityCenterQueryViewer.componentDidMount, this.props: ', this.props); this.loadData(); } render() { if (this.isLoading()) { return <div>QC Query Viewer - LOADING</div> } let coalescedQueries = this.coalesceQueryData( this.props.qualityCenter.queries, this.props.qualityCenter.queryComponents); const queryComponents = coalescedQueries.map((query) => <li key={query.id}>Query Name: {query.name} <ul> {query.components.map((component) => <li key={query.id + component.fieldName}>{component.fieldName} = {component.expression}</li>)} </ul> </li>); return <div> <ul>{queryComponents}</ul> </div> } /** * Maps data from normalized json-api to an easy-to-use array of objects with the following format: * { id: queryId, name: queryName, components: array of query component objects } * Query components have the following format: {fieldName: field name of component, expression: the query expression * for the field name} * TODO: move to reducer??? */ coalesceQueryData(queries, queryComponents) { let coalescedQueries = Object.keys(queries).map(key => { let rawQuery = queries[key]; let query = {id: rawQuery.id, name: rawQuery.attributes.name}; query.components = rawQuery.relationships.components.data.map(componentId => { let component = queryComponents[componentId.id]; return {fieldName: component.attributes.fieldName, expression: component.attributes.expression}; }); return query; }); return coalescedQueries; } isLoading() { return !this.props.qualityCenter.isLoaded; } loadData() { this.props.getQcQueries(); } } function mapStateToProps(state) { return { qualityCenter: state.qualityCenter } } function mapDispatchToProps(dispatch) { return bindActionCreators({ getQcQueries: actions.loadQcQueries }, dispatch) } export default connect(mapStateToProps, mapDispatchToProps)(QualityCenterQueryViewer);
/** * _____ __ * / ___// /_ _____ * \__ \/ / / / / _ \ * ___/ / / /_/ / __/ * /____/_/\__, /\___/ * /____/ * Copyright 2017 Slye Development Team. All Rights Reserved. * Licence: MIT License */ /** * Events * @param esy */ function events(esy) { const {regex, block, compile} = esy; var pattern = new regex( regex.beginning(), regex.text('#'), regex.identifier().capture(), regex.group( regex.text('<'), regex.callParameters().capture(), regex.text('>') ).optional(), regex.text('('), regex.arguments().optional().capture(), regex.text(')'), regex.group( regex.text('.'), regex.identifier().capture() ).optional(), regex.end() ).autoSpace().toObj(); esy.block(pattern, (matches, block) => { let variable = matches[1], event = matches[2] ? matches[2] + ',' : '', args = matches[3] || '', evFunc = matches[4] ? '.' + matches[4] : '.on', body = compile(block.body); return `${variable}${evFunc}(${event}function(${args}){ ${body} })` }); // Nested event listener let pattern2 = new regex( regex.beginning(), regex.text('#'), // Var name regex.identifier().capture(), // Default `evFunc` regex.group( regex.text('<'), regex.callParameters().capture(), regex.text('>') ).optional(), regex.end() ).autoSpace().toObj(); let pattern3 = new regex( regex.beginning(), regex.group( regex.text('<'), regex.callParameters().capture(), regex.text('>') ).optional(), regex.text('('), regex.arguments().optional().capture(), regex.text(')'), regex.group( regex.text('.'), regex.identifier().capture() ).optional(), regex.end() ).autoSpace().toObj(); esy.block.self(pattern3); esy.block(pattern2, (matches, {body}) => { let variable = matches[1], defEvFunc = matches[2] ? matches[2] : 'on', re = ''; body.forEach(value => { if(typeof value !== 'object') throw new Error('Wrong nested event listener.'); let {head, body} = value, e = new RegExp(pattern3); if(e.test(head)){ let match = e.exec(head); while(match === null){ match = e.exec(head); } let event = match[1] ? match[1] + ',' : '', args = match[2] || '', evFunc = match[3] ? '.' + match[3] : '.' + defEvFunc, b = compile(body); re += `${variable}${evFunc}(${event}function(${args}){ ${b} }) ` }else { throw new Error('Wrong nested event listener.'); } }); return re; }); return { name: "Esy Events", version: "0.0.2", author: "Slye Development Team" }; } module.exports = events;
/** * Created by mlennox on 22/05/2016. */ const stored_consoles = { log: console.log, warn: console.warn, error: console.error } const console_target = { log: 'log', warn: 'warn', error: 'error' } const create_mock = (test_context, target) => { console[target] = (...messages) => { test_context.context[target + '_message'] = messages.length > 1 ? [...messages] : messages[0] } } const mock = (test_context, target) => { let targets = [] if (!target){ targets = Object.keys(stored_consoles) } else { if (console_target[target]){ targets.push(target) } else { throw new Error('please provide one of log, warn or error and not "' + target + '"') } } targets.forEach((tgt) => create_mock(test_context, tgt)) } /** * Restores all mocks */ const restore = () => { console.log = stored_consoles.log console.warn = stored_consoles.warn console.error = stored_consoles.error } export default { mock, restore, log: console_target.log, warn: console_target.warn, error: console_target.error }
var _a, _b; // @ts-ignore var STORAGE_KEY = "SAVED_SAMPLE"; var ENDPOINT_KEY = "SAVED_ENDPOINT"; var existing = localStorage.getItem(STORAGE_KEY); // @ts-ignore var editor = monaco.editor.create(document.getElementById("container"), { value: existing || "", language: "typescript", minimap: { enabled: false } }); var CUSTOM_FILE = "custom.ts"; var PLAYGROUND_ID = "playground"; var endpoints; var selectedEndpoint; var baseProjects = {}; editor.onDidChangeModelContent(debounce(function () { localStorage.setItem(STORAGE_KEY, editor.getValue()); }, 500)); // @ts-ignore (_b = (_a = monaco.languages) === null || _a === void 0 ? void 0 : _a.typescript) === null || _b === void 0 ? void 0 : _b.typescriptDefaults.setDiagnosticsOptions({ noSemanticValidation: true }); var iframe = document.createElement("iframe"); document.getElementById("makecode-editor").appendChild(iframe); initEndpoints(); initSamples(); document.getElementById("run-button").addEventListener("click", function () { var ts = editor.getValue(); sendMessage("importproject", ts); }); window.addEventListener("message", receiveMessage, false); window.addEventListener("resize", function () { var parent = document.getElementById("monaco-editor"); editor.layout({ width: parent.clientWidth, height: parent.clientHeight }); }); function initEndpoints() { var s = document.getElementById("endpoint-select"); xhr("editors.json", function () { endpoints = JSON.parse(this.responseText); for (var _i = 0, _a = Object.keys(endpoints); _i < _a.length; _i++) { var name_1 = _a[_i]; var endpoint = endpoints[name_1]; if (supportedEndpoint(endpoint)) { var opt = document.createElement("option"); opt.value = name_1; opt.innerText = endpoint.name; s.appendChild(opt); } else { delete endpoints[name_1]; } } s.addEventListener("change", function (ev) { loadIframe(ev.target.value); }); loadIframe(null); }); } var PLAY_SAMPLES; // defined in /playground/samples/all.js function initSamples() { var s = document.getElementById("sample-select"); // load sample names into dropdown var sampleChapter; PLAY_SAMPLES.forEach(function (sample) { if (!sampleChapter || sampleChapter.label !== sample.chapter) { sampleChapter = document.createElement('optgroup'); sampleChapter.label = sample.chapter; s.appendChild(sampleChapter); } var sampleOption = document.createElement('option'); sampleOption.value = sample.id; sampleOption.appendChild(document.createTextNode(sample.name)); sampleChapter.appendChild(sampleOption); }); // check if sample js loaded, otherwise send xhr function loadSample(sampleId, callback) { var _a; var sample = PLAY_SAMPLES.find(function (e) { return e.id == sampleId; }); if (!sample) { return callback(new Error('sample not found')); } else if ((_a = sample) === null || _a === void 0 ? void 0 : _a.js) { return callback(null, sample); } var samplePath = 'static/playground/samples/' + sample.path; xhr(samplePath + '/sample.js', function () { sample.js = this.responseText; callback(null, sample); }); } var currentToken = 0; function parseHash(firstTime) { var sampleId = window.location.hash.replace(/^#/, ''); if (!sampleId) { sampleId = PLAY_SAMPLES[0].id; } if (firstTime) { for (var i = 0; i < s.options.length; i++) { var opt = s.options[i]; if (opt.value === sampleId) { s.selectedIndex = i; break; } } } var myToken = (++currentToken); loadSample(sampleId, function (err, sample) { if (err) { alert('Sample not found! ' + err.message); return; } if (myToken !== currentToken) { return; } editor.setValue(sample.js); editor.setScrollTop(0); sendMessage("importproject", sample.js); }); } s.addEventListener("change", function (ev) { var sampleId = s.options[s.selectedIndex].value; window.location.hash = sampleId; }); window.addEventListener("hashchange", parseHash); parseHash(true); } function loadIframe(selected) { if (selected === selectedEndpoint) return; for (var _i = 0, _a = Object.keys(endpoints); _i < _a.length; _i++) { var name_2 = _a[_i]; var endpoint = endpoints[name_2]; if (!selected || selected === name_2) { var separator = endpoint.url.indexOf("?") >= 0 ? "&" : "?"; iframe.setAttribute("src", "" + endpoint.url + separator + "controller=1"); selectedEndpoint = name_2; return; } } } function supportedEndpoint(endpoint) { var _a; return !(((_a = endpoint.unsupported) === null || _a === void 0 ? void 0 : _a.indexOf(PLAYGROUND_ID)) >= 0); } function sendMessage(action, ts) { var msg = { type: "pxteditor", id: Math.random().toString(), action: action }; var project = baseProjects[selectedEndpoint]; if (action == 'importproject') { if (project) { // add custom file name to pxt.json var config = JSON.parse(project.text["pxt.json"]); if (config.files.indexOf(CUSTOM_FILE) < 0) { config.files.push(CUSTOM_FILE); project.text["pxt.json"] = JSON.stringify(config); } // add/update custom file project.text[CUSTOM_FILE] = ts; msg.project = project; } } iframe.contentWindow.postMessage(msg, "*"); } function receiveMessage(ev) { var msg = ev.data; if (msg.type == "pxthost") { if (msg.action == "workspacesync") { // no project msg.projects = []; iframe.contentWindow.postMessage(msg, "*"); return; } else if (msg.action == "workspacesave") { if (!baseProjects[selectedEndpoint]) baseProjects[selectedEndpoint] = msg.project; return; } } } function debounce(func, wait, immediate) { var timeout; return function () { var context = this; var args = arguments; var later = function () { timeout = null; if (!immediate) func.apply(context, args); }; var callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); return timeout; }; } function xhr(url, callback) { var req = new XMLHttpRequest(); req.addEventListener("load", callback); req.open("GET", url); req.send(); }
/* |----------------------------------------------------------------------------- | Screens https://tailwindcss.com/docs/responsive-design |----------------------------------------------------------------------------- | | Screens in Tailwind are translated to CSS media queries. They define the | responsive breakpoints for your project. By default Tailwind takes a | "mobile first" approach, where each screen size represents a minimum | viewport width. Feel free to have as few or as many screens as you | want, naming them in whatever way you'd prefer for your project. | | Tailwind also allows for more complex screen definitions, which can be | useful in certain situations. Be sure to see the full responsive | documentation for a complete list of options. | | Class name: .{screen}:{utility} | */ export default { sm: '576px', md: '768px', lg: '992px', xl: '1200px' };
'use strict'; //Games service used for games REST endpoint angular.module('mean.games').factory('Games', ['$resource', function($resource) { return $resource('games/:gameId', { gameId: '@_id' }, { update: { method: 'PUT' } }); } ]);
'use strict' // delete for real -- KHCL const db = require('APP/db') , {User, Thing, Favorite, Promise} = db , {mapValues} = require('lodash') function seedEverything() { const seeded = { users: users(), things: things(), } seeded.favorites = favorites(seeded) return Promise.props(seeded) } const users = seed(User, { god: { email: '[email protected]', name: 'So many names', password: '1234', }, barack: { name: 'Barack Obama', email: '[email protected]', password: '1234' }, }) const things = seed(Thing, { surfing: {name: 'surfing'}, smiting: {name: 'smiting'}, puppies: {name: 'puppies'}, }) const favorites = seed(Favorite, // We're specifying a function here, rather than just a rows object. // Using a function lets us receive the previously-seeded rows (the seed // function does this wiring for us). // // This lets us reference previously-created rows in order to create the join // rows. We can reference them by the names we used above (which is why we used // Objects above, rather than just arrays). ({users, things}) => ({ // The easiest way to seed associations seems to be to just create rows // in the join table. 'obama loves surfing': { user_id: users.barack.id, // users.barack is an instance of the User model // that we created in the user seed above. // The seed function wires the promises so that it'll // have been created already. thing_id: things.surfing.id // Same thing for things. }, 'god is into smiting': { user_id: users.god.id, thing_id: things.smiting.id }, 'obama loves puppies': { user_id: users.barack.id, thing_id: things.puppies.id }, 'god loves puppies': { user_id: users.god.id, thing_id: things.puppies.id }, }) ) if (module === require.main) { db.didSync .then(() => db.sync({force: true})) .then(seedEverything) .finally(() => process.exit(0)) } class BadRow extends Error { constructor(key, row, error) { super(error) this.cause = error this.row = row this.key = key } toString() { return `[${this.key}] ${this.cause} while creating ${JSON.stringify(this.row, 0, 2)}` } } // seed(Model: Sequelize.Model, rows: Function|Object) -> // (others?: {...Function|Object}) -> Promise<Seeded> // // Takes a model and either an Object describing rows to insert, // or a function that when called, returns rows to insert. returns // a function that will seed the DB when called and resolve with // a Promise of the object of all seeded rows. // // The function form can be used to initialize rows that reference other models. function seed(Model, rows) { return (others={}) => { if (typeof rows === 'function') { rows = Promise.props( mapValues(others, other => // Is other a function? If so, call it. Otherwise, leave it alone. typeof other === 'function' ? other() : other) ).then(rows) } return Promise.resolve(rows) .then(rows => Promise.props( Object.keys(rows) .map(key => { const row = rows[key] return { key, value: Promise.props(row) .then(row => Model.create(row) .catch(error => { throw new BadRow(key, row, error) }) ) } }).reduce( (all, one) => Object.assign({}, all, {[one.key]: one.value}), {} ) ) ) .then(seeded => { console.log(`Seeded ${Object.keys(seeded).length} ${Model.name} OK`) return seeded }).catch(error => { console.error(`Error seeding ${Model.name}: ${error} \n${error.stack}`) }) } } module.exports = Object.assign(seed, {users, things, favorites})
'use strict' const proxyquire = require('proxyquire').noCallThru() describe('bootstrap', () => { let premiddleware, app, db, appEnv, result, mongodbUri, authService, http const controllers = {} const formattedDbUri = 'mongodb formatted uri' beforeEach(() => { mongodbUri = { formatMongoose: env.stub().returns(formattedDbUri) } premiddleware = env.stub().returnsArg(0) db = env.stub() appEnv = { DB_NAME: 'DB_NAME', DB_HOST: 'DB_HOST', DB_PORT: 'DB_PORT', DB_USER: 'DB_USER', DB_PASSWORD: 'DB_PASSWORD' } authService = { getCurrentUser: env.stub(), isAuthenticated: env.stub() } http = { IncomingMessage: function IncomingMessage() {} } const sut = proxyquire('./index', { 'mongodb-uri': mongodbUri, '../controllers': controllers, './preMiddleware': premiddleware, './db': db, '../../../env': appEnv, '../controllers/auth/auth.service': authService, http }) app = { use: env.spy(() => app) } result = sut(app) }) it.skip('should create db uri from env', () => { mongodbUri.formatMongoose.should.been .calledWith({ scheme: 'mongodb', hosts: [ { host: appEnv.DB_HOST, port: appEnv.DB_PORT } ], username: appEnv.DB_USER, password: appEnv.DB_PASSWORD, database: appEnv.DB_NAME }) .and .callCount(1) }) it.skip('should connect to database', () => { db.should.been.calledWith(formattedDbUri) }) it('should add premiddleware to app', () => { premiddleware.should.been.calledWith(app) }) it('should add controllers to app', () => { app.use.should.been.calledWith(controllers) }) it('should return app instance', () => { result.should.equal(app) }) })
import { Palindromes } from './palindrome-products'; describe('Palindromes', () => { test('smallest palindrome from single digit factors', () => { const palindromes = Palindromes.generate({ maxFactor: 9, minFactor: 1 }); const smallest = palindromes.smallest; const expected = {value: 1, factors: [[1, 1]]}; expect(smallest.value).toEqual(expected.value); expect(sortFactors(smallest.factors)).toEqual(expected.factors); }); xtest('largest palindrome from single digit factors', () => { const palindromes = Palindromes.generate({ maxFactor: 9, minFactor: 1 }); const largest = palindromes.largest; const expected = {value: 9, factors: [[1, 9], [3, 3]]}; expect(largest.value).toEqual(expected.value); expect(sortFactors(largest.factors)).toEqual(expected.factors); }); xtest('smallest palindrome from double digit factors', () => { const palindromes = Palindromes.generate({ maxFactor: 99, minFactor: 10 }); const smallest = palindromes.smallest; const expected = {value: 121, factors: [[11, 11]]}; expect(smallest.value).toEqual(expected.value); expect(sortFactors(smallest.factors)).toEqual(expected.factors); }); xtest('largest palindrome from double digit factors', () => { const palindromes = Palindromes.generate({ maxFactor: 99, minFactor: 10 }); const largest = palindromes.largest; const expected = {value: 9009, factors: [[91, 99]]}; expect(largest.value).toEqual(expected.value); expect(sortFactors(largest.factors)).toEqual(expected.factors); }); xtest('smallest palindrome from triple digit factors', () => { const palindromes = Palindromes.generate({ maxFactor: 999, minFactor: 100 }); const smallest = palindromes.smallest; const expected = {value: 10201, factors: [[101, 101]]}; expect(smallest.value).toEqual(expected.value); expect(sortFactors(smallest.factors)).toEqual(expected.factors); }); xtest('largest palindrome from triple digit factors', () => { const palindromes = Palindromes.generate({ maxFactor: 999, minFactor: 100 }); const largest = palindromes.largest; const expected = {value: 906609, factors: [[913, 993]]}; expect(largest.value).toEqual(expected.value); expect(sortFactors(largest.factors)).toEqual(expected.factors); }); xtest('smallest palindrome from four digit factors', () => { const palindromes = Palindromes.generate({ maxFactor: 9999, minFactor: 1000 }); const smallest = palindromes.smallest; const expected = {value: 1002001, factors: [[1001, 1001]]}; expect(smallest.value).toEqual(expected.value); expect(sortFactors(smallest.factors)).toEqual(expected.factors); }); xtest('largest palindrome from four digit factors', () => { const palindromes = Palindromes.generate({ maxFactor: 9999, minFactor: 1000 }); const largest = palindromes.largest; const expected = {value: 99000099, factors: [[9901, 9999]]}; expect(largest.value).toEqual(expected.value); expect(sortFactors(largest.factors)).toEqual(expected.factors); }); xtest('empty result for smallest if no palindrome in range', () => { const palindromes = Palindromes.generate({ maxFactor: 1003, minFactor: 1002 }); const smallest = palindromes.smallest; expect(smallest.value).toBe(null); expect(smallest.factors).toEqual([]); }); xtest('empty result for largest if no palindrome in range', () => { const palindromes = Palindromes.generate({ maxFactor: 15, minFactor: 15 }); const largest = palindromes.largest; expect(largest.value).toBe(null); expect(largest.factors).toEqual([]); }); xtest('error for smallest if min is more than max', () => { expect(() => { const palindromes = Palindromes.generate({ maxFactor: 1, minFactor: 10000 }); palindromes.smallest; }).toThrow(new Error('min must be <= max')); }); xtest('error for largest if min is more than max', () => { expect(() => { const palindromes = Palindromes.generate({ maxFactor: 1, minFactor: 2 }); palindromes.largest; }).toThrow(new Error('min must be <= max')); }); }); function sortFactors(factors) { return factors.map(f => f.sort()).sort(); }
/** * Context menu * @file contextMenu.js * @author Qwant */ 'use strict'; document.addEventListener("contextmenu", handleContextMenu, false); /** * Handle context menu * @function handleContextMenu * @param event */ function handleContextMenu (event) { var sel = window.parent.getSelection().toString(); sel = sel.replace(/^\s+|\s+$/g, ''); safari.self.tab.setContextMenuEventUserInfo(event, { type: 'text', data: sel }); }
const defaults = require('../../src/defaults').init module.exports = { name: 'init', command: 'init [dir]', description: 'initializes a boilerplate elm-factory application', options: { dir: { description: 'the project directory', }, f: { alias: 'force', description: 'ignore existing files', default: defaults.force, }, }, }
'use strict'; require('dotenv').load(); var User = require('../models/users'); var TwitterStrategy = require('passport-twitter').Strategy; module.exports = function(passport){ passport.serializeUser(function (user, done) { done(null, user._id); }); passport.deserializeUser(function (id, done) { User.findById(id, function (err, user) { done(err, user); }); }); passport.use(new TwitterStrategy({ consumerKey: process.env.TWITTER_CONSUMER_KEY, consumerSecret: process.env.TWITTER_CONSUMER_SECRET, callbackURL: process.env.APP_URL + 'auth/twitter/callback' }, function(token, tokenSecret, profile, cb){ User.findOne({'twitter.id': profile.id}, function(err, user){ if (err){ return cb(err, user); } if (user){ return cb(null, user); } else{ var newUser = new User(); newUser.twitter.id = profile.id; newUser.username = profile.username; newUser.save(function (err) { if (err) { throw err; } return cb(null, newUser); }); } }); })); };
const FileSystem = require('./fs'); const SimpleStorageService = require('./s3'); const Storage = (config) => { const storageServices = { fs: FileSystem, s3: SimpleStorageService, }; const storageType = config.storage.type; if (storageServices[storageType] == null) { throw new Error(`Storage service type ${storageType} does not exist`); } const StorageService = storageServices[storageType]; return StorageService(config[storageType]); }; module.exports = Storage;
import 'whatwg-fetch'; import _ from 'lodash'; import { getJSON, deleteObject, putJSON } from "../utils/apiRequest"; // ------------------------------------ // Constants // ------------------------------------ export const SEARCH_GROUPS_SUCCESS = 'SEARCH_GROUPS_SUCCESS'; // ------------------------------------ // Actions // ------------------------------------ export const searchGroups = (search) => { return (dispatch, getState) => { return new Promise((resolve) => { getJSON(`group/searchByName?groupName=${search}`).then((response) => { dispatch({ type: SEARCH_GROUPS_SUCCESS, payload: response }); resolve(); }); }); } }; // ------------------------------------ // Action Handlers // ------------------------------------ const ACTION_HANDLERS = { [SEARCH_GROUPS_SUCCESS]: (state, action) => { return action.payload; } }; // ------------------------------------ // Reducer // ------------------------------------ const initialState = []; export default function groupSearchReducer(state = initialState, action) { const handler = ACTION_HANDLERS[action.type]; return handler ? handler(state, action) : state; };
import Flip from './Flip'; import FlipGroup from './FlipGroup'; jest.mock('./Flip'); Flip.mockImplementation(); describe('FlipGroup', () => { test("It shouldn't be possible to add elements which are not Flip instances", () => { const element = 2; const group = new FlipGroup(); group.addElement(element); expect(group.elements.length).toBe(0); }); test('It should be possible to add a Flip element', () => { const element = new Flip(); const group = new FlipGroup(); group.addElement(element); expect(group.elements.length).toBe(1); }); test('It should be possible to remove a Flip element', () => { const element = new Flip(); const group = new FlipGroup(); const remove = group.addElement(element); remove(); expect(group.elements.length).toBe(0); }); test('It should be possible to add a deferred Flip element using a function', () => { const element = new Flip(); const group = new FlipGroup(); const remove = group.addElement(element, () => true); expect(group.getConcernedElements(true).length).toBe(1); }); test('It should be possible to add a deferred Flip element', () => { const element = new Flip(); const group = new FlipGroup(); const remove = group.addElement(element, true); expect(group.getConcernedElements(true).length).toBe(1); }); test('It should be possible to remove a deferred Flip element', () => { const element = new Flip(); const group = new FlipGroup(); const remove = group.addElement(element, true); remove(); expect(group.getConcernedElements(true).length).toBe(0); }); test('First method on a group should call it on children', () => { const element = new Flip(); const group = new FlipGroup(); group.addElement(element); group.first(); expect(element.first.mock.calls.length).toBe(1); }); test('First method on a group should only call the deferred elements if deferred option is true', () => { const element = new Flip(); const deferredElement = new Flip(); const group = new FlipGroup(); group.addElement(element); group.addElement(deferredElement, true); group.first({ deferred: true }); expect(element.first.mock.calls.length).toBe(0); expect(deferredElement.first.mock.calls.length).toBe(1); }); test('First method on a group should only call the non deferred elements if deferred option is false', () => { const element = new Flip(); const deferredElement = new Flip(); const group = new FlipGroup(); group.addElement(element); group.addElement(deferredElement, true); group.first({ deferred: false }); expect(element.first.mock.calls.length).toBe(1); expect(deferredElement.first.mock.calls.length).toBe(0); }); test('Last method on a group should call it on children', () => { const element = new Flip(); const group = new FlipGroup(); group.addElement(element); group.last(); expect(element.last.mock.calls.length).toBe(1); }); test('Last method on a group should only call the deferred elements if deferred option is true', () => { const element = new Flip(); const deferredElement = new Flip(); const group = new FlipGroup(); group.addElement(element); group.addElement(deferredElement, true); group.last({ deferred: true }); expect(element.last.mock.calls.length).toBe(0); expect(deferredElement.last.mock.calls.length).toBe(1); }); test('Last method on a group should only call the non deferred elements if deferred option is false', () => { const element = new Flip(); const deferredElement = new Flip(); const group = new FlipGroup(); group.addElement(element); group.addElement(deferredElement, true); group.last({ deferred: false }); expect(element.last.mock.calls.length).toBe(1); expect(deferredElement.last.mock.calls.length).toBe(0); }); test('Invert method on a group should call it on children', () => { const element = new Flip(); const group = new FlipGroup(); group.addElement(element); group.first(); group.last(); group.invert(); expect(element.invert.mock.calls.length).toBe(1); }); test('Invert method on a group should call it on children', () => { const element = new Flip(); const deferredElement = new Flip(); const group = new FlipGroup(); group.addElement(element); group.addElement(deferredElement, true); group.first(); group.last(); group.invert({ deferred: false }); expect(element.invert.mock.calls.length).toBe(1); expect(deferredElement.invert.mock.calls.length).toBe(0); }); test('Invert method should return a falsy value if each invert returns a falsy value', () => { const element = new Flip(); element.invert.mockImplementation(() => false); const element2 = new Flip(); element2.invert.mockImplementation(() => false); const group = new FlipGroup(); group.addElement(element); group.addElement(element2); group.first(); group.last(); expect(group.invert()).toBe(false); }); test('Invert method should return a truthy value if at least one element returns a truthy value', () => { const element = new Flip(); element.invert.mockImplementation(() => false); const element2 = new Flip(); element2.invert.mockImplementation(() => true); const group = new FlipGroup(); group.addElement(element); group.addElement(element2); group.first(); group.last(); expect(group.invert()).toBe(true); }); test('Play method should not return a promise if none of the elements are playable', () => { const element = new Flip(); element.play.mockImplementation(() => {}); const element2 = new Flip(); element2.invert.mockImplementation(() => {}); const group = new FlipGroup(); group.addElement(element); group.addElement(element2); group.first(); group.last(); group.invert(); expect(group.play()).not.toBeInstanceOf(Promise); }); test('Play method should return a promise if at least one of the element returns a promise', () => { const element = new Flip(); element.play.mockImplementation( () => new Promise((resolve, reject) => { process.nextTick(() => resolve()); }) ); const element2 = new Flip(); element2.invert.mockImplementation(() => {}); const group = new FlipGroup(); group.addElement(element); group.addElement(element2); group.first(); group.last(); group.invert(); expect(group.play()).toBeInstanceOf(Promise); }); });
var gulp = require('gulp') , sequence = require('run-sequence') gulp.task('build', function(done) { sequence( 'config:prompt' , 'config:print' , 'clean' , [ 'browserify', 'assets', 'manifest:prod' ] , 'compress' , done ) })
'use strict'; define(['d3', 'jquery', 'bootstrap'], function (d3, $) { var drawRMCPChart = function (container, data, sample, titleName, callout) { var containerSelector = container, dataset = data, sampleSize = sample, titleText = titleName, chartObject = {}; var dimensions = { height: 500, width: 800, paddingHorizontal: 10, paddingVertical: 10, yAxisMinWidth: 300, calloutMinWidth: 180, yAxisProportion: 0.4, calloutProportion: 0.18, titleHeight: 54, legendHeight: 100, calloutPaddingLeft: 70, otherInfoHeight: 0, spaceBetweenBars: 10, minWidth: 650, hideCallout: 940, showCalloutBox: true, innerHeight: function () { return this.height - 2 * this.paddingVertical; }, innerWidth: function () { return this.width - 2 * this.paddingHorizontal; }, calloutWidth: function () { return this.showCalloutBox ? Math.round( Math.max( this.calloutMinWidth, (this.innerWidth() * this.calloutProportion) ) ) : 0; }, yAxisWidth: function () { return Math.round( Math.max( this.yAxisMinWidth, (this.innerWidth() - this.calloutWidth()) * this.yAxisProportion ) ); }, dataWidth: function () { return this.innerWidth() - this.calloutWidth() - this.yAxisWidth() - this.calloutPaddingLeft; }, yAxisLeft: function () { return this.paddingHorizontal; }, yAxisRight: function () { return this.yAxisLeft() + this.yAxisWidth(); }, dataLeft: function () { return this.yAxisRight(); }, dataRight: function () { return this.dataLeft() + this.dataWidth(); }, calloutLeft: function () { return this.dataRight() + this.calloutPaddingLeft; }, calloutRight: function () { return this.width - this.paddingHorizontal; }, calloutCenterHorizontal: function () { return Math.round(this.calloutLeft() + this.calloutWidth() / 2); }, centerHorizontal: function () { return Math.round(this.width / 2); }, titleTop: function () { return this.paddingVertical; }, titleBottom: function () { return this.titleTop() + this.titleHeight; }, titleUnderline: function () { return this.titleBottom() + this.paddingVertical * 2; }, legendTop: function () { return this.titleUnderline() + this.paddingVertical; }, legendBottom: function () { return this.legendTop() + this.legendHeight; }, legendCenterVertical: function () { return Math.round((this.legendTop() + this.legendBottom()) / 2); }, otherInfoBottom: function () { return this.height - this.paddingVertical; }, otherInfoTop: function () { return this.otherInfoBottom() - this.otherInfoHeight; }, dataTop: function () { return this.legendBottom() + this.paddingVertical; }, dataBottom: function () { return this.otherInfoTop(); }, dataHeight: function () { return this.dataBottom() - this.dataTop(); }, calloutTop: function () { return this.legendTop(); }, calloutBottom: function () { return this.dataBottom(); }, calloutHeight: function () { return this.calloutBottom() - this.calloutTop(); }, legendLeft: function () { return this.yAxisLeft(); }, legendRight: function () { return this.dataRight(); } }; dimensions.width = Math.max($(containerSelector).width(), dimensions.minWidth); dimensions.height = Math.max(9 * dimensions.width / 16, 640); dimensions.showCalloutBox = (typeof callout !== 'undefined') ? callout : true; var hexagonPath = "M18.536,12.353L2.378,21.681c-1.234,0.714-2.755," + "0.714-3.99,0l-16.157-9.328c-1.235-0.712-1.996-2.03-1.996-3.456" + "V-9.759c0-1.425,0.76-2.743,1.996-3.455l16.157-9.329c1.235-0.712," + "2.756-0.712,3.99,0l16.158,9.329c1.234,0.712,1.994,2.3,1.994,3.455" + "V8.897C20.53,10.323,19.771,11.641,18.536,12.353"; var dataKey = function (d) { return d.id; }; var colorSet = function (number) { switch (number) { case 1: return ['#b9be10']; case 3: default: return ['#203368', '#1B75BB', '#48C6EA']; } }; var svg = d3.select(containerSelector) .append("svg") .attr("height", dimensions.height) .attr("width", dimensions.width); var yScaleOuter = d3.scale.ordinal() .domain(d3.range(dataset.length)) .rangeRoundBands( [dimensions.dataTop(), dimensions.dataBottom()], 0.2, 0.1); var yScaleInner = d3.scale.ordinal() .domain(d3.range(sampleSize.length)) .rangeRoundBands([0, yScaleOuter.rangeBand()], 0.2); var xScale = d3.scale.linear() .domain([0, d3.max(dataset, function (d) { return d3.max(d.data, function (d, i) { return d / sampleSize[i].quantity; }); })]) .range( [0, dimensions.dataWidth()]); var colors = d3.scale.ordinal() .domain(d3.range(sampleSize.length)) .range(colorSet(sampleSize.length)); var legendSpacing = d3.scale.ordinal() .domain(d3.range(sampleSize.length)) .rangeRoundBands( [dimensions.legendLeft(), dimensions.legendRight()], 0.1); var percentFormat = d3.format('%'); var resizeText = function (text, width, max) { text.each(function() { var text = d3.select(this), current = max; do { text.style("font-size", current + "px"); current -= 2; } while (text.node().getComputedTextLength() > width); }); }; var wrapLines = function (text, width) { if (width === 0) return; text.each(function() { var text = d3.select(this), words = text.text().split(/\s+/).reverse(), word, line = [], lineNumber = 0, lineHeight = 1.5, // ems y = text.attr("y"), x = text.attr("x"), dy = parseFloat(text.attr("dy")) || 0, tspan = text.text(null).append("tspan") .attr("x", x) .attr("y", y) .attr("dy", dy + "em"); while (word = words.pop()) { line.push(word); tspan.text(line.join(" ")); if (tspan.node().getComputedTextLength() > width) { line.pop(); tspan.text(line.join(" ")); line = [word]; tspan = text.append("tspan") .attr("x", x) .attr("y", y) .attr("dy", ++lineNumber * lineHeight + dy + "em") .text(word); } } }); }; var reWrapLines = function (text, width) { if (width === 0 || (typeof width === "number" && isNaN(width))) return; text.each(function () { var text = d3.select(this), tspans = text.selectAll("tspan"), content = ''; if (tspans.empty()) return; tspans.each(function () { content += d3.select(this).text() + " "; }); text.text(content.substring(0,content.length-1)); }); wrapLines(text, width); }; var percentRound = function (number) { return Math.round(number * 100) / 100; }; var yAxisStyles = { 'text-anchor': 'end', 'fill': '#58585b', 'font-family': 'Open Sans', 'font-size': '14px', }, dataLabelStyles = { 'font-family': 'Open Sans', 'fill': '#58585b', 'alignment-baseline': 'middle' }, calloutTitleStyles = { 'fill': '#58585b', 'font-family': "Open Sans", 'font-size': '14px', 'text-anchor': 'middle', 'font-weight': 700 }, calloutNumberStyles = { 'fill': '#58585b', 'font-family': "Roboto Slab", 'font-size': '24px', 'text-anchor': 'middle', }; if (!Modernizr.ie9) { yAxisStyles['alignment-baseline'] = 'bottom'; dataLabelStyles['text-anchor'] = 'left'; calloutTitleStyles['alignment-baseline'] = 'top'; calloutNumberStyles['alignment-baseline'] = 'center'; } var title = svg.append("g"); title.append("text") // Title Text .text(titleText) .attr("y", dimensions.titleBottom()) .attr("x", dimensions.centerHorizontal()) .style({ 'font-family': "'Roboto Slab', serif", 'text-anchor': 'middle', 'fill': '#58585b', 'font-size': '42px' }) .call(resizeText, dimensions.innerWidth(), 42); title.append("path") // Underline the title .attr("d", function () { var path = ""; path += "M" + dimensions.yAxisLeft() + "," + dimensions.titleUnderline(); path += "L" + dimensions.calloutRight() + "," + dimensions.titleUnderline(); return path; }) .style({ 'stroke': '#58585b', 'stroke-width': '2px' }); var legend = svg.append("g"); legend.selectAll("path").data(sampleSize).enter() .append("path") .attr("d", hexagonPath) .style("fill", function (d, i) { return colors(i); }) .style({ 'stroke-width': 0, // 'stroke': '#203368' }) .attr('transform', function (d, i) { return 'translate(' + (21 * 0.6 + legendSpacing(i)) + ',' + dimensions.legendCenterVertical() + ')' + 'scale(0.6)'; }); legend.selectAll("text.legend").data(sampleSize).enter() .append("text") .attr("class", "legend") .text(function (d) { return d.title + " (n=" + d.quantity + ")"; }) .attr("x", function (d, i) { return legendSpacing(i) + 41 * 0.6 + dimensions.paddingHorizontal; }) .attr("y", function () { return dimensions.legendCenterVertical(); }) .style({ 'fill': '#58585b', 'font-family': 'Open Sans', 'font-size': '14px', 'alignment-baseline': 'middle' }); var yAxisGroup = svg.append("g"); yAxisGroup.append("path") // Y-axis line .attr("d", function () { var path = ""; path += "M" + (dimensions.yAxisRight() - 1) + "," + dimensions.dataTop(); path += "L" + (dimensions.yAxisRight() - 1) + "," + dimensions.dataBottom(); return path; }) .style({ 'stroke': '#000', 'stroke-width': '1px' }); yAxisGroup.selectAll("text") // Labels .data(dataset, dataKey) .enter() .append("text") .attr("x", function () { return dimensions.yAxisRight() - dimensions.paddingHorizontal; }) .attr("y", function (d, i) { return yScaleOuter(i) + yScaleOuter.rangeBand() / 3; }) .text(function (d) { return d.title; }) .call(wrapLines, (dimensions.yAxisWidth() - dimensions.paddingHorizontal)/1.1) .style(yAxisStyles); var chartData = svg.append("g"); chartData.selectAll("g") // Bar Groups .data(dataset, dataKey) .enter() .append("g") .each(function (dData, iData) { var group = d3.select(this); group.selectAll("rect") .data(sampleSize) .enter() .append("rect") .attr("y", function (d, i) { return yScaleOuter(iData) + yScaleInner(i); }) .attr("x", dimensions.dataLeft()) .attr("height", yScaleInner.rangeBand()) .attr("width", function (d, i) { return xScale(dData.data[i] / d.quantity); }) .attr("data-toggle", "tooltip") .attr("title", function (d, i) { return dData.data[i] + " out of " + d.quantity + " " + d.title.toLowerCase() + " companies surveyed experience pain in this area"; }) .style({ // 'stroke': '#203368', 'stroke-width': 0 }) .style("fill", function (d, i) { return colors(i); }) .on("mouseover", function() { d3.select(this) .style("filter", "url(#drop-shadow)"); }) .on("mouseout", function(d) { d3.select(this) .style("filter", ""); }); group.selectAll("text") .data(sampleSize) .enter() .append("text") .text(function (d, i) { return percentFormat(dData.data[i] / d.quantity); }) .attr("x", function (d, i) { return dimensions.dataLeft() + xScale(dData.data[i] / d.quantity) + dimensions.paddingHorizontal; }) .attr("y", function (d,i) { return yScaleOuter(iData) + yScaleInner(i) + yScaleInner.rangeBand() / 2; }) .style(dataLabelStyles) .style('font-size', function () { return Math.min( Math.round(yScaleInner.rangeBand() * 0.8), 18 ) + "px"; }); }); var calloutBox = svg.append("g"); if (!dimensions.showCalloutBox) { calloutBox.style("display", "none"); } calloutBox.append("rect") .attr("x", function () { return dimensions.calloutLeft(); }) .attr("y", function () { return dimensions.calloutTop(); }) .attr("width", function () { return dimensions.calloutWidth(); }) .attr("height", function () { return dimensions.calloutHeight(); }) .style({ 'fill': 'rgba(88, 88, 91, 0.1)', 'stroke-width': '0' }); calloutBox.append("text") .attr("class", "title") .text("Difference between low and high maturity organizations") .attr("x", dimensions.calloutCenterHorizontal()) .attr("y", (dimensions.legendTop() + dimensions.paddingVertical * 3)) .call(wrapLines, dimensions.calloutWidth() / 1.2) .style(calloutTitleStyles); calloutBox.selectAll("text.difference") .data(dataset, dataKey) .enter() .append("text") .attr("class", "difference") .attr("x", dimensions.calloutCenterHorizontal()) .attr("y", function (d, i) { return yScaleOuter(i) + yScaleOuter.rangeBand() / 2; }) .style(calloutNumberStyles) .text(function (d) { var last = sampleSize.length - 1; return percentFormat(percentRound(d.data[0] / sampleSize[0].quantity) - percentRound(d.data[last] / sampleSize[last].quantity)); }); // filters go in defs element var defs = svg.append("defs"); var filter = defs.append("filter") .attr("id", "drop-shadow") .attr("height", "130%"); filter.append("feGaussianBlur") // .attr("in", "SourceAlpha") .attr("stdDeviation", 3) .attr("result", "blur"); filter.append("feOffset") .attr("in", "blur") .attr("dx", 0) .attr("dy", 0) .attr("result", "offsetBlur"); var feMerge = filter.append("feMerge"); feMerge.append("feMergeNode") .attr("in", "offsetBlur"); feMerge.append("feMergeNode") .attr("in", "SourceGraphic"); chartObject.changeData = function (newDataset, newSampleSize, newTitleText, newCallout) { dataset = newDataset; sampleSize = newSampleSize; titleText = newTitleText; dimensions.showCalloutBox = (typeof newCallout !== 'undefined') ? newCallout : true; if (title.selectAll("text").text() !== newTitleText) { $(title.selectAll("text")[0][0]) .animate({'opacity': 0}, { complete: function () { title.selectAll("text") .text(newTitleText) .call(resizeText, dimensions.innerWidth(), 42); $(this).animate({'opacity': 1}); }}); } if (!dimensions.showCalloutBox) { calloutBox.style("display", "none"); } else { calloutBox.style("display", ""); } xScale.domain([0, d3.max(dataset, function (d) { return d3.max(d.data, function (d, i) { return d / sampleSize[i].quantity; }); }) ]) .range([0, dimensions.dataWidth()]); yScaleOuter.domain(d3.range(newDataset.length)); yScaleInner.domain(d3.range(newSampleSize.length)); legendSpacing.domain(d3.range(sampleSize.length)) .rangeRoundBands( [dimensions.legendLeft(), dimensions.legendRight()], 0.1); colors.range(colorSet(sampleSize.length)); chartData.selectAll("g") .data(newDataset, dataKey) .each(function (dData, iData) { var group = d3.select(this); group.selectAll("rect") .data(newSampleSize) .exit() .remove(); group.selectAll("rect") .data(newSampleSize) .style("fill", function (d, i) { return colors(i); }) .transition() .attr("y", function (d, i) { return yScaleOuter(iData) + yScaleInner(i); }) .attr("x", dimensions.dataLeft()) .attr("height", yScaleInner.rangeBand()) .attr("width", function (d, i) { return xScale(dData.data[i] / d.quantity); }) .attr("data-original-title", function (d, i) { return dData.data[i] + " out of " + d.quantity + " " + d.title.toLowerCase() + " companies surveyed experience pain in this area"; }); group.selectAll("rect") .data(newSampleSize) .enter() .append("rect") .attr("y", function (d, i) { return yScaleOuter(iData) + yScaleInner(i); }) .attr("x", dimensions.dataLeft()) .attr("height", yScaleInner.rangeBand()) .attr("width", 0) .attr("data-toggle", "tooltip") .attr("title", function (d, i) { return dData.data[i] + " out of " + d.quantity + " " + d.title.toLowerCase() + " companies surveyed experience pain in this area"; }) .style({ // 'stroke': '#203368', 'stroke-width': 0 }) .style("fill", function (d, i) { return colors(i); }) .on("mouseover", function() { d3.select(this) .style("filter", "url(#drop-shadow)"); }) .on("mouseout", function(d) { d3.select(this) .style("filter", ""); }) .transition() .attr("width", function (d, i) { return xScale(dData.data[i] / d.quantity); }); group.selectAll("text") .data(newSampleSize) .exit() .remove(); group.selectAll("text") .data(newSampleSize) .style('font-size', function () { return Math.min( Math.round(yScaleInner.rangeBand() * 0.8), 18 ) + "px"; }) .text(function (d, i) { return percentFormat(dData.data[i] / d.quantity); }) .transition() .attr("x", function (d, i) { return dimensions.dataLeft() + xScale(dData.data[i] / d.quantity) + dimensions.paddingHorizontal; }) .attr("y", function (d,i) { return yScaleOuter(iData) + yScaleInner(i) + yScaleInner.rangeBand() / 2; }); group.selectAll("text") .data(newSampleSize) .enter() .append("text") .text(function (d, i) { return percentFormat(dData.data[i] / d.quantity); }) .attr("x", function (d, i) { return dimensions.dataLeft() + xScale(dData.data[i] / d.quantity) + dimensions.paddingHorizontal; }) .attr("y", function (d,i) { return yScaleOuter(iData) + yScaleInner(i) + yScaleInner.rangeBand() / 2; }) .style(dataLabelStyles) .style('font-size', function () { return Math.min( Math.round(yScaleInner.rangeBand() * 0.8), 18 ) + "px"; }); }); yAxisGroup.selectAll("path") // Y-axis line .attr("d", function () { var path = ""; path += "M" + (dimensions.yAxisRight() - 1) + "," + dimensions.dataTop(); path += "L" + (dimensions.yAxisRight() - 1) + "," + dimensions.dataBottom(); return path; }); yAxisGroup.selectAll("text") .call(reWrapLines, (dimensions.yAxisWidth() - dimensions.paddingHorizontal)/1.1) .transition() .attr("x", function () { return dimensions.yAxisRight() - dimensions.paddingHorizontal; }) .selectAll("tspan") .attr("x", function () { return dimensions.yAxisRight() - dimensions.paddingHorizontal; }); calloutBox.selectAll("rect") .attr("x", function () { return dimensions.calloutLeft(); }) .attr("y", function () { return dimensions.calloutTop(); }) .attr("width", function () { return dimensions.calloutWidth(); }) .attr("height", function () { return dimensions.calloutHeight(); }); calloutBox.selectAll("text.title") .attr("x", dimensions.calloutCenterHorizontal()) .attr("y", (dimensions.legendTop() + dimensions.paddingVertical * 3)) .call(reWrapLines, dimensions.calloutWidth() / 1.2); calloutBox.selectAll("text.difference") .attr("x", dimensions.calloutCenterHorizontal()) .attr("y", function (d, i) { return yScaleOuter(i) + yScaleOuter.rangeBand() / 2; }) .data(newDataset, dataKey) .text(function (d) { var last = newSampleSize.length - 1; return percentFormat(percentRound(d.data[0] / newSampleSize[0].quantity) - percentRound(d.data[last] / newSampleSize[last].quantity)); }); legend.selectAll("text.legend").data(sampleSize).exit().remove(); legend.selectAll("text.legend").data(sampleSize) .text(function (d) { return d.title + " (n=" + d.quantity + ")"; }) .attr('x', function (d, i) { return legendSpacing(i) + 41 * 0.6 + dimensions.paddingHorizontal; }); legend.selectAll("text.legend").data(sampleSize).enter() .append("text") .attr("class", "legend") .text(function (d) { return d.title + " (n=" + d.quantity + ")"; }) .attr("x", function (d, i) { return legendSpacing(i) + 41 * 0.6 + dimensions.paddingHorizontal; }) .attr("y", function () { return dimensions.legendCenterVertical(); }) .style({ 'fill': '#58585b', 'font-family': 'Open Sans', 'font-size': '14px', 'alignment-baseline': 'middle' }); legend.selectAll("path").data(sampleSize).exit().remove(); legend.selectAll("path").data(sampleSize) .attr('transform', function (d, i) { return 'translate(' + (21 * 0.6 + legendSpacing(i)) + ',' + dimensions.legendCenterVertical() + ') ' + 'scale(0.6)'; }) .style("fill", function (d, i) { return colors(i); }); legend.selectAll("path").data(sampleSize).enter() .append("path") .attr("d", hexagonPath) .style("fill", function (d, i) { return colors(i); }) .style({ 'stroke-width': 0, // 'stroke': '#203368' }) .attr('transform', function (d, i) { return 'translate(' + (21 * 0.6 + legendSpacing(i)) + ',' + dimensions.legendCenterVertical() + ') ' + 'scale(0.6)'; }); $(containerSelector).find('rect') .tooltip({ 'trigger': 'hover', 'container': '.hovers' }); }; chartObject.resizeChart = function () { var width = $(containerSelector).width(); dimensions.width = Math.max(width, dimensions.minWidth); svg.attr("width", dimensions.width); if (width < dimensions.hideCallout) { calloutBox.style({ 'display': 'none' }); dimensions.calloutMinWidth = 0; dimensions.calloutProportion = 0; } else { if (dimensions.showCalloutBox) { calloutBox.style({ 'display': '' }); } dimensions.calloutMinWidth = 200; dimensions.calloutProportion = 0.2; dimensions.calloutPaddingLeft = 50; } // Adjust the scales xScale.range([0, dimensions.dataWidth()]); legendSpacing.rangeRoundBands( [dimensions.legendLeft(), dimensions.legendRight()], 0.1); title.selectAll("text") // Title Text .attr("y", dimensions.titleBottom()) .attr("x", dimensions.centerHorizontal()) .call(resizeText, dimensions.innerWidth(), 42); title.selectAll("path") // Underline the title .attr("d", function () { var path = ""; path += "M" + dimensions.yAxisLeft() + "," + dimensions.titleUnderline(); path += "L" + dimensions.calloutRight() + "," + dimensions.titleUnderline(); return path; }); legend.selectAll("path") .attr('transform', function (d, i) { return 'translate(' + (21 * 0.6 + legendSpacing(i)) + ',' + dimensions.legendCenterVertical() + ')' + 'scale(0.6)'; }); legend.selectAll("text.legend") .attr("x", function (d, i) { return legendSpacing(i) + 41 * 0.6 + dimensions.paddingHorizontal; }) .attr("y", dimensions.legendCenterVertical()); yAxisGroup.selectAll("path") // Y-axis line .attr("d", function () { var path = ""; path += "M" + (dimensions.yAxisRight() - 1) + "," + dimensions.dataTop(); path += "L" + (dimensions.yAxisRight() - 1) + "," + dimensions.dataBottom(); return path; }); yAxisGroup.selectAll("text") .call(reWrapLines, (dimensions.yAxisWidth() - dimensions.paddingHorizontal)/1.1) .attr("x", function () { return dimensions.yAxisRight() - dimensions.paddingHorizontal; }) .selectAll("tspan") .attr("x", function () { return dimensions.yAxisRight() - dimensions.paddingHorizontal; }); chartData.selectAll("g") .each(function (dData) { var group = d3.select(this); group.selectAll("rect") .attr("x", dimensions.dataLeft()) .attr("width", function (d, i) { return xScale(dData.data[i] / d.quantity); }); group.selectAll("text") .attr("x", function (d, i) { return dimensions.dataLeft() + xScale(dData.data[i] / d.quantity) + dimensions.paddingHorizontal; }); }); calloutBox.selectAll("rect") .attr("x", function () { return dimensions.calloutLeft(); }) .attr("width", function () { return dimensions.calloutWidth(); }); calloutBox.selectAll("text.title") .attr("x", dimensions.calloutCenterHorizontal()) .call(reWrapLines, dimensions.calloutWidth() / 1.5); calloutBox.selectAll("text.difference") .attr("x", dimensions.calloutCenterHorizontal()); }; d3.select(containerSelector).append("div").attr("class", "hovers"); setTimeout(function () { chartObject.resizeChart(); }, 10); $(containerSelector).find('rect') .tooltip({ 'trigger': 'hover', 'container': '.hovers' }); return chartObject; }; return function (container, data, sample, titleName, callout) { return drawRMCPChart(container, data, sample, titleName, callout); }; });
(function(){ var express = require('express'); var bodyParser = require('body-parser'); var methodOverride = require('method-override'); // Create the application. var app = express(); // Add Middleware necessary for REST API's app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.json()); app.use(methodOverride('X-HTTP-Method-Override')); // CORS Support app.use(function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'Content-Type'); next(); }); var port = process.env.PORT || 8989; // Poem_Database var Poem_Database = require('./data/poems/Poem_Database.json'); //Test var testvar = "First call"; //Routes addition require('./routes')(app, testvar, Poem_Database); //start server console.log("listening on port: " + port); app.listen(port); generateBigPoemJSONFileLinks = function() { var Store = require('jfs'); var data_db = new Store('data/poems',{pretty:true}); var data_links_db = new Store('data/poems/links',{pretty:true}); var _ = require('lodash'); var levenshtein = require('fast-levenshtein'); var poems = require('./data/poems/Poem_Database.json'); console.log("links are generating!!!"); _.forEach(poems, function(poem, index) { var linksFileJSON = new Array(); _.forEach(poems, function(otherpoem, otherpoem_index){ var lev_dist = levenshtein.get(poem.poem, otherpoem.poem); linksFileJSON.push({"pid":otherpoem.pid, "distance":lev_dist, "metadata":[]}); }); poems[index].poem.links_generated = true; data_links_db.saveSync(poem.pid, linksFileJSON); //console.log("links_generated", poem.pid); }); console.log("Now writing Poem_Database file..."); data_db.save("Poem_Database", poems); } generatePoemFiles = function() { var Store = require('jfs'); var data_db = new Store('data/poems',{pretty:true}); var _ = require('lodash'); var poems = require('./data/SourcePoems.json'); var poemsJSON = new Array(); _.forEach(poems, function(poem, index) { var indexed_poem = poem; indexed_poem.pid = index; indexed_poem.children = []; indexed_poem.orig_src = index; indexed_poem.parentpid = -1 indexed_poem.links_generated = false; poemsJSON.push(indexed_poem); }); data_db.saveSync("Poem_Database", poemsJSON); console.log("SUCCESS: Poem_Database generated!!!"); } /** var startTime = new Date().valueOf(); generatePoemFiles(); console.log('Time taken for Poem_Database generation: ' + (new Date().valueOf() - startTime)/1000 +'seconds'); var startTime1 = new Date().valueOf(); generateBigPoemJSONFileLinks(); console.log('Time taken for links generation: ' + (new Date().valueOf() - startTime1)/1000 +'seconds'); **/ })();
var path = require('path'); var testInfo = require('ember-cli-test-info'); var stringUtil = require('ember-cli-string-utils'); var isPackageMissing = require('ember-cli-is-package-missing'); var getPathOption = require('ember-cli-get-component-path-option'); var useTestFrameworkDetector = require('../lib/utilities/test-framework-detector'); module.exports = useTestFrameworkDetector({ description: 'Generates a component integration or unit test.', availableOptions: [ { name: 'test-type', type: ['integration', 'unit'], default: 'integration', aliases:[ { 'i': 'integration'}, { 'u': 'unit'}, { 'integration': 'integration' }, { 'unit': 'unit' } ] } ], fileMapTokens: function() { return { __testType__: function(options) { return options.locals.testType || 'integration'; }, __path__: function(options) { if (options.pod) { return path.join(options.podPath, options.locals.path, options.dasherizedModuleName); } return 'components'; } }; }, locals: function(options) { var dasherizedModuleName = stringUtil.dasherize(options.entity.name); var componentPathName = dasherizedModuleName; var testType = options.testType || 'integration'; var friendlyTestDescription = testInfo.description(options.entity.name, 'Integration', 'Component'); if (options.pod && options.path && options.path !== 'components') { componentPathName = [options.path, dasherizedModuleName].join('/'); } if (options.testType === 'unit') { friendlyTestDescription = testInfo.description(options.entity.name, 'Unit', 'Component'); } return { path: getPathOption(options), testType: testType, componentPathName: componentPathName, friendlyTestDescription: friendlyTestDescription }; }, afterInstall: function(options) { if (!options.dryRun && options.testType === 'integration' && isPackageMissing(this, 'ember-cli-htmlbars-inline-precompile')) { return this.addPackagesToProject([ { name: 'ember-cli-htmlbars-inline-precompile', target: '^0.3.1' } ]); } } });
'use strict'; import Emitter from 'utils/Emitter'; import { WINDOW_RESIZE, WEBGL_MESH_LOADED, WEBGL_IS_INTERSECTING, WEBGL_IS_NOT_INTERSECTING, WEBGL_CLICK_ON_OBJECT } from '../../config/messages'; import InfoSectionComponent from 'components/info-section'; import WebglExperienceComponent from 'components/webgl-experience'; import NavigationComponent from 'components/navigation'; import CurrentIntersectIndicatorComponent from 'components/current-intersect-indicator'; import 'gsap'; export default Vue.extend({ template: require('./template.html'), data() { return { isIntersecting: false }; }, created() { this.bind(); }, ready() { this.currentObject = null; this.currentObjectRef = null; this.addEventListeners(); }, beforeDestroy() { this.removeEventListeners(); }, methods: { /* * Binding & Events */ bind() { }, addEventListeners() { Emitter.on(WEBGL_IS_INTERSECTING, this.onIsIntersecting); Emitter.on(WEBGL_IS_NOT_INTERSECTING, this.onIsNotIntersecting); Emitter.on(WEBGL_MESH_LOADED, this.isLoaded); this.$on(WINDOW_RESIZE, this.onWindowResize); }, removeEventListeners() { Emitter.off(WEBGL_IS_INTERSECTING, this.offIsIntersecting); Emitter.off(WEBGL_IS_NOT_INTERSECTING, this.onIsNotIntersecting); Emitter.off(WEBGL_MESH_LOADED, this.isLoaded); this.$off(WINDOW_RESIZE, this.onWindowResize); }, onWindowResize(width, height) { this.$broadcast(WINDOW_RESIZE, width, height); }, onClick() { if(this.isIntersecting) { Emitter.emit(WEBGL_CLICK_ON_OBJECT, this.currentObject); this.isIntersecting = false; } }, onIsIntersecting(intersectObject) { this.currentObject = intersectObject.object; this.currentObjectRef = intersectObject.object.ref; this.isIntersecting = true; }, onIsNotIntersecting() { this.isIntersecting = false; }, isLoaded() { const loader = document.querySelector('.experience_loading'); TweenMax.to(loader, 1, { autoAlpha:0, ease: Expo.easeOut }); } }, transitions: { }, components: { InfoSectionComponent, WebglExperienceComponent, CurrentIntersectIndicatorComponent, NavigationComponent } });
import Config from './config/users'; module.exports = (server, options) => [ // eslint-disable-line no-unused-vars { method: 'GET', path: '/users', config: Config.find, }, { method: 'POST', path: '/users', config: Config.create, }, { method: 'GET', path: '/users/{id}', config: Config.findOneById, }, { method: 'PATCH', path: '/users/{id}', config: Config.edit, }, { method: 'DELETE', path: '/users/{id}', config: Config.destroy, }, ];