babylon.math.js 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. ////////////////////////////////// Ray //////////////////////////////////
  5. BABYLON.Ray = function (origin, direction) {
  6. this.origin = origin;
  7. this.direction = direction;
  8. };
  9. // Methods
  10. BABYLON.Ray.prototype.intersectsBox = function (box) {
  11. var d = 0.0;
  12. var maxValue = Number.MAX_VALUE;
  13. if (Math.abs(this.direction.x) < 0.0000001) {
  14. if (this.origin.x < box.minimum.x || this.origin.x > box.maximum.x) {
  15. return false;
  16. }
  17. }
  18. else {
  19. var inv = 1.0 / this.direction.x;
  20. var min = (box.minimum.x - this.origin.x) * inv;
  21. var max = (box.maximum.x - this.origin.x) * inv;
  22. if (min > max) {
  23. var temp = min;
  24. min = max;
  25. max = temp;
  26. }
  27. d = Math.max(min, d);
  28. maxValue = Math.min(max, maxValue);
  29. if (d > maxValue) {
  30. return false;
  31. }
  32. }
  33. if (Math.abs(this.direction.y) < 0.0000001) {
  34. if (this.origin.y < box.minimum.y || this.origin.y > box.maximum.y) {
  35. return false;
  36. }
  37. }
  38. else {
  39. var inv = 1.0 / this.direction.y;
  40. var min = (box.minimum.y - this.origin.y) * inv;
  41. var max = (box.maximum.y - this.origin.y) * inv;
  42. if (min > max) {
  43. var temp = min;
  44. min = max;
  45. max = temp;
  46. }
  47. d = Math.max(min, d);
  48. maxValue = Math.min(max, maxValue);
  49. if (d > maxValue) {
  50. return false;
  51. }
  52. }
  53. if (Math.abs(this.direction.z) < 0.0000001) {
  54. if (this.origin.z < box.minimum.z || this.origin.z > box.maximum.z) {
  55. return false;
  56. }
  57. }
  58. else {
  59. var inv = 1.0 / this.direction.z;
  60. var min = (box.minimum.z - this.origin.z) * inv;
  61. var max = (box.maximum.z - this.origin.z) * inv;
  62. if (min > max) {
  63. var temp = min;
  64. min = max;
  65. max = temp;
  66. }
  67. d = Math.max(min, d);
  68. maxValue = Math.min(max, maxValue);
  69. if (d > maxValue) {
  70. return false;
  71. }
  72. }
  73. return true;
  74. };
  75. BABYLON.Ray.prototype.intersectsSphere = function (sphere) {
  76. var x = sphere.center.x - this.origin.x;
  77. var y = sphere.center.y - this.origin.y;
  78. var z = sphere.center.z - this.origin.z;
  79. var pyth = (x * x) + (y * y) + (z * z);
  80. var rr = sphere.radius * sphere.radius;
  81. if (pyth <= rr) {
  82. return true;
  83. }
  84. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  85. if (dot < 0.0) {
  86. return false;
  87. }
  88. var temp = pyth - (dot * dot);
  89. return temp <= rr;
  90. };
  91. BABYLON.Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  92. if (!this._edge1) {
  93. this._edge1 = BABYLON.Vector3.Zero();
  94. this._edge2 = BABYLON.Vector3.Zero();
  95. this._pvec = BABYLON.Vector3.Zero();
  96. this._tvec = BABYLON.Vector3.Zero();
  97. this._qvec = BABYLON.Vector3.Zero();
  98. }
  99. vertex1.subtractToRef(vertex0, this._edge1);
  100. vertex2.subtractToRef(vertex0, this._edge2);
  101. BABYLON.Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  102. var det = BABYLON.Vector3.Dot(this._edge1, this._pvec);
  103. if (det === 0) {
  104. return 0;
  105. }
  106. var invdet = 1 / det;
  107. this.origin.subtractToRef(vertex0, this._tvec);
  108. var bu = BABYLON.Vector3.Dot(this._tvec, this._pvec) * invdet;
  109. if (bu < 0 || bu > 1.0) {
  110. return 0;
  111. }
  112. BABYLON.Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  113. var bv = BABYLON.Vector3.Dot(this.direction, this._qvec) * invdet;
  114. if (bv < 0 || bu + bv > 1.0) {
  115. return 0;
  116. }
  117. return BABYLON.Vector3.Dot(this._edge2, this._qvec) * invdet;
  118. };
  119. // Statics
  120. BABYLON.Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  121. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  122. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  123. var direction = end.subtract(start);
  124. direction.normalize();
  125. return new BABYLON.Ray(start, direction);
  126. };
  127. BABYLON.Ray.Transform = function (ray, matrix) {
  128. var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
  129. var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
  130. return new BABYLON.Ray(newOrigin, newDirection);
  131. };
  132. ////////////////////////////////// Color3 //////////////////////////////////
  133. BABYLON.Color3 = function (initialR, initialG, initialB) {
  134. this.r = initialR;
  135. this.g = initialG;
  136. this.b = initialB;
  137. };
  138. BABYLON.Color3.prototype.toString = function () {
  139. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  140. };
  141. // Operators
  142. BABYLON.Color3.prototype.multiply = function (otherColor) {
  143. return new BABYLON.Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  144. };
  145. BABYLON.Color3.prototype.multiplyToRef = function (otherColor, result) {
  146. result.r = this.r * otherColor.r;
  147. result.g = this.g * otherColor.g;
  148. result.b = this.b * otherColor.b;
  149. };
  150. BABYLON.Color3.prototype.equals = function (otherColor) {
  151. return this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  152. };
  153. BABYLON.Color3.prototype.scale = function (scale) {
  154. return new BABYLON.Color3(this.r * scale, this.g * scale, this.b * scale);
  155. };
  156. BABYLON.Color3.prototype.scaleToRef = function (scale, result) {
  157. result.r = this.r * scale;
  158. result.g = this.g * scale;
  159. result.b = this.b * scale;
  160. };
  161. BABYLON.Color3.prototype.clone = function () {
  162. return new BABYLON.Color3(this.r, this.g, this.b);
  163. };
  164. BABYLON.Color3.prototype.copyFrom = function (source) {
  165. this.r = source.r;
  166. this.g = source.g;
  167. this.b = source.b;
  168. };
  169. BABYLON.Color3.prototype.copyFromFloats = function (r, g, b) {
  170. this.r = r;
  171. this.g = g;
  172. this.b = b;
  173. };
  174. // Statics
  175. BABYLON.Color3.FromArray = function (array) {
  176. return new BABYLON.Color3(array[0], array[1], array[2]);
  177. };
  178. ////////////////////////////////// Color4 //////////////////////////////////
  179. BABYLON.Color4 = function (initialR, initialG, initialB, initialA) {
  180. this.r = initialR;
  181. this.g = initialG;
  182. this.b = initialB;
  183. this.a = initialA;
  184. };
  185. // Operators
  186. BABYLON.Color4.prototype.addInPlace = function (right) {
  187. this.r += right.r;
  188. this.g += right.g;
  189. this.b += right.b;
  190. this.a += right.a;
  191. };
  192. BABYLON.Color4.prototype.add = function (right) {
  193. return new BABYLON.Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  194. };
  195. BABYLON.Color4.prototype.subtract = function (right) {
  196. return new BABYLON.Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  197. };
  198. BABYLON.Color4.prototype.subtractToRef = function (right, result) {
  199. result.r = this.r - right.r;
  200. result.g = this.g - right.g;
  201. result.b = this.b - right.b;
  202. result.a = this.a - right.a;
  203. };
  204. BABYLON.Color4.prototype.scale = function (scale) {
  205. return new BABYLON.Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  206. };
  207. BABYLON.Color4.prototype.scaleToRef = function (scale, result) {
  208. result.r = this.r * scale;
  209. result.g = this.g * scale;
  210. result.b = this.b * scale;
  211. result.a = this.a * scale;
  212. };
  213. BABYLON.Color4.prototype.toString = function () {
  214. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  215. };
  216. BABYLON.Color4.prototype.clone = function () {
  217. return new BABYLON.Color4(this.r, this.g, this.b, this.a);
  218. };
  219. // Statics
  220. BABYLON.Color4.Lerp = function (left, right, amount) {
  221. var result = new BABYLON.Color4(0, 0, 0, 0);
  222. BABYLON.Color4.LerpToRef(left, right, amount, result);
  223. return result;
  224. };
  225. BABYLON.Color4.LerpToRef = function (left, right, amount, result) {
  226. result.r = left.r + (right.r - left.r) * amount;
  227. result.g = left.g + (right.g - left.g) * amount;
  228. result.b = left.b + (right.b - left.b) * amount;
  229. result.a = left.a + (right.a - left.a) * amount;
  230. };
  231. BABYLON.Color4.FromArray = function (array, offset) {
  232. if (!offset) {
  233. offset = 0;
  234. }
  235. return new BABYLON.Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  236. };
  237. ////////////////////////////////// Vector2 //////////////////////////////////
  238. BABYLON.Vector2 = function (initialX, initialY) {
  239. this.x = initialX;
  240. this.y = initialY;
  241. };
  242. BABYLON.Vector2.prototype.toString = function () {
  243. return "{X: " + this.x + " Y:" + this.y + "}";
  244. };
  245. // Operators
  246. BABYLON.Vector2.prototype.add = function (otherVector) {
  247. return new BABYLON.Vector2(this.x + otherVector.x, this.y + otherVector.y);
  248. };
  249. BABYLON.Vector2.prototype.subtract = function (otherVector) {
  250. return new BABYLON.Vector2(this.x - otherVector.x, this.y - otherVector.y);
  251. };
  252. BABYLON.Vector2.prototype.negate = function () {
  253. return new BABYLON.Vector2(-this.x, -this.y);
  254. };
  255. BABYLON.Vector2.prototype.scaleInPlace = function (scale) {
  256. this.x *= scale;
  257. this.y *= scale;
  258. };
  259. BABYLON.Vector2.prototype.scale = function (scale) {
  260. return new BABYLON.Vector2(this.x * scale, this.y * scale);
  261. };
  262. BABYLON.Vector2.prototype.equals = function (otherVector) {
  263. return this.x === otherVector.x && this.y === otherVector.y;
  264. };
  265. // Properties
  266. BABYLON.Vector2.prototype.length = function () {
  267. return Math.sqrt(this.x * this.x + this.y * this.y);
  268. };
  269. BABYLON.Vector2.prototype.lengthSquared = function () {
  270. return (this.x * this.x + this.y * this.y);
  271. };
  272. // Methods
  273. BABYLON.Vector2.prototype.normalize = function () {
  274. var len = this.length();
  275. if (len === 0)
  276. return;
  277. var num = 1.0 / len;
  278. this.x *= num;
  279. this.y *= num;
  280. };
  281. BABYLON.Vector2.prototype.clone = function () {
  282. return new BABYLON.Vector2(this.x, this.y);
  283. };
  284. // Statics
  285. BABYLON.Vector2.Zero = function () {
  286. return new BABYLON.Vector2(0, 0);
  287. };
  288. BABYLON.Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  289. var squared = amount * amount;
  290. var cubed = amount * squared;
  291. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  292. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  293. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  294. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  295. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  296. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  297. return new BABYLON.Vector2(x, y);
  298. };
  299. BABYLON.Vector2.Clamp = function (value, min, max) {
  300. var x = value.x;
  301. x = (x > max.x) ? max.x : x;
  302. x = (x < min.x) ? min.x : x;
  303. var y = value.y;
  304. y = (y > max.y) ? max.y : y;
  305. y = (y < min.y) ? min.y : y;
  306. return new BABYLON.Vector2(x, y);
  307. };
  308. BABYLON.Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  309. var squared = amount * amount;
  310. var cubed = amount * squared;
  311. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  312. var part2 = (-2.0 * cubed) + (3.0 * squared);
  313. var part3 = (cubed - (2.0 * squared)) + amount;
  314. var part4 = cubed - squared;
  315. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  316. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  317. return new BABYLON.Vector2(x, y);
  318. };
  319. BABYLON.Vector2.Lerp = function (start, end, amount) {
  320. var x = start.x + ((end.x - start.x) * amount);
  321. var y = start.y + ((end.y - start.y) * amount);
  322. return new BABYLON.Vector2(x, y);
  323. };
  324. BABYLON.Vector2.Dot = function (left, right) {
  325. return left.x * right.x + left.y * right.y;
  326. };
  327. BABYLON.Vector2.Normalize = function (vector) {
  328. var newVector = vector.clone();
  329. newVector.normalize();
  330. return newVector;
  331. };
  332. BABYLON.Vector2.Minimize = function (left, right) {
  333. var x = (left.x < right.x) ? left.x : right.x;
  334. var y = (left.y < right.y) ? left.y : right.y;
  335. return new BABYLON.Vector2(x, y);
  336. };
  337. BABYLON.Vector2.Maximize = function (left, right) {
  338. var x = (left.x > right.x) ? left.x : right.x;
  339. var y = (left.y > right.y) ? left.y : right.y;
  340. return new BABYLON.Vector2(x, y);
  341. };
  342. BABYLON.Vector2.Transform = function (vector, transformation) {
  343. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  344. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  345. return new BABYLON.Vector2(x, y);
  346. };
  347. BABYLON.Vector2.Distance = function (value1, value2) {
  348. return Math.sqrt(BABYLON.Vector2.DistanceSquared(value1, value2));
  349. };
  350. BABYLON.Vector2.DistanceSquared = function (value1, value2) {
  351. var x = value1.x - value2.x;
  352. var y = value1.y - value2.y;
  353. return (x * x) + (y * y);
  354. };
  355. ////////////////////////////////// Vector3 //////////////////////////////////
  356. BABYLON.Vector3 = function (initialX, initialY, initialZ) {
  357. this.x = initialX;
  358. this.y = initialY;
  359. this.z = initialZ;
  360. };
  361. BABYLON.Vector3.prototype.toString = function () {
  362. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  363. };
  364. // Operators
  365. BABYLON.Vector3.prototype.toArray = function (array, index) {
  366. array[index] = this.x;
  367. array[index + 1] = this.y;
  368. array[index + 2] = this.z;
  369. };
  370. BABYLON.Vector3.prototype.addInPlace = function (otherVector) {
  371. this.x += otherVector.x;
  372. this.y += otherVector.y;
  373. this.z += otherVector.z;
  374. };
  375. BABYLON.Vector3.prototype.add = function (otherVector) {
  376. return new BABYLON.Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  377. };
  378. BABYLON.Vector3.prototype.addToRef = function (otherVector, result) {
  379. result.x = this.x + otherVector.x;
  380. result.y = this.y + otherVector.y;
  381. result.z = this.z + otherVector.z;
  382. };
  383. BABYLON.Vector3.prototype.subtractInPlace = function (otherVector) {
  384. this.x -= otherVector.x;
  385. this.y -= otherVector.y;
  386. this.z -= otherVector.z;
  387. };
  388. BABYLON.Vector3.prototype.subtract = function (otherVector) {
  389. return new BABYLON.Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  390. };
  391. BABYLON.Vector3.prototype.subtractToRef = function (otherVector, result) {
  392. result.x = this.x - otherVector.x;
  393. result.y = this.y - otherVector.y;
  394. result.z = this.z - otherVector.z;
  395. };
  396. BABYLON.Vector3.prototype.subtractFromFloats = function (x, y, z) {
  397. return new BABYLON.Vector3(this.x - x, this.y - y, this.z - z);
  398. };
  399. BABYLON.Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  400. result.x = this.x - x;
  401. result.y = this.y - y;
  402. result.z = this.z - z;
  403. };
  404. BABYLON.Vector3.prototype.negate = function () {
  405. return new BABYLON.Vector3(-this.x, -this.y, -this.z);
  406. };
  407. BABYLON.Vector3.prototype.scaleInPlace = function (scale) {
  408. this.x *= scale;
  409. this.y *= scale;
  410. this.z *= scale;
  411. };
  412. BABYLON.Vector3.prototype.scale = function (scale) {
  413. return new BABYLON.Vector3(this.x * scale, this.y * scale, this.z * scale);
  414. };
  415. BABYLON.Vector3.prototype.scaleToRef = function (scale, result) {
  416. result.x = this.x * scale;
  417. result.y = this.y * scale;
  418. result.z = this.z * scale;
  419. };
  420. BABYLON.Vector3.prototype.equals = function (otherVector) {
  421. return this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  422. };
  423. BABYLON.Vector3.prototype.equalsToFloats = function (x, y, z) {
  424. return this.x === x && this.y === y && this.z === z;
  425. };
  426. BABYLON.Vector3.prototype.multiplyInPlace = function (otherVector) {
  427. this.x *= otherVector.x;
  428. this.y *= otherVector.y;
  429. this.z *= otherVector.z;
  430. };
  431. BABYLON.Vector3.prototype.multiply = function (otherVector) {
  432. return new BABYLON.Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  433. };
  434. BABYLON.Vector3.prototype.multiplyToRef = function (otherVector, result) {
  435. result.x = this.x * otherVector.x;
  436. result.y = this.y * otherVector.y;
  437. result.z = this.z * otherVector.z;
  438. };
  439. BABYLON.Vector3.prototype.multiplyByFloats = function (x, y, z) {
  440. return new BABYLON.Vector3(this.x * x, this.y * y, this.z * z);
  441. };
  442. BABYLON.Vector3.prototype.divide = function (otherVector) {
  443. return new BABYLON.Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  444. };
  445. BABYLON.Vector3.prototype.divideToRef = function (otherVector, result) {
  446. result.x = this.x / otherVector.x;
  447. result.y = this.y / otherVector.y;
  448. result.z = this.z / otherVector.z;
  449. };
  450. // Properties
  451. BABYLON.Vector3.prototype.length = function () {
  452. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  453. };
  454. BABYLON.Vector3.prototype.lengthSquared = function () {
  455. return (this.x * this.x + this.y * this.y + this.z * this.z);
  456. };
  457. // Methods
  458. BABYLON.Vector3.prototype.normalize = function () {
  459. var len = this.length();
  460. if (len === 0)
  461. return;
  462. var num = 1.0 / len;
  463. this.x *= num;
  464. this.y *= num;
  465. this.z *= num;
  466. };
  467. BABYLON.Vector3.prototype.clone = function () {
  468. return new BABYLON.Vector3(this.x, this.y, this.z);
  469. };
  470. BABYLON.Vector3.prototype.copyFrom = function (source) {
  471. this.x = source.x;
  472. this.y = source.y;
  473. this.z = source.z;
  474. };
  475. BABYLON.Vector3.prototype.copyFromFloats = function (x, y, z) {
  476. this.x = x;
  477. this.y = y;
  478. this.z = z;
  479. };
  480. // Statics
  481. BABYLON.Vector3.FromArray = function (array, offset) {
  482. if (!offset) {
  483. offset = 0;
  484. }
  485. return new BABYLON.Vector3(array[offset], array[offset + 1], array[offset + 2]);
  486. };
  487. BABYLON.Vector3.FromArrayToRef = function (array, offset, result) {
  488. if (!offset) {
  489. offset = 0;
  490. }
  491. result.x = array[offset];
  492. result.y = array[offset + 1];
  493. result.z = array[offset + 2];
  494. };
  495. BABYLON.Vector3.FromFloatsToRef = function (x, y, z, result) {
  496. result.x = x;
  497. result.y = y;
  498. result.z = z;
  499. };
  500. BABYLON.Vector3.Zero = function () {
  501. return new BABYLON.Vector3(0, 0, 0);
  502. };
  503. BABYLON.Vector3.Up = function () {
  504. return new BABYLON.Vector3(0, 1.0, 0);
  505. };
  506. BABYLON.Vector3.TransformCoordinates = function (vector, transformation) {
  507. var result = BABYLON.Vector3.Zero();
  508. BABYLON.Vector3.TransformCoordinatesToRef(vector, transformation, result);
  509. return result;
  510. };
  511. BABYLON.Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  512. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  513. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  514. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  515. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  516. result.x = x / w;
  517. result.y = y / w;
  518. result.z = z / w;
  519. };
  520. BABYLON.Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  521. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  522. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  523. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  524. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  525. result.x = rx / rw;
  526. result.y = ry / rw;
  527. result.z = rz / rw;
  528. };
  529. BABYLON.Vector3.TransformNormal = function (vector, transformation) {
  530. var result = BABYLON.Vector3.Zero();
  531. BABYLON.Vector3.TransformNormalToRef(vector, transformation, result);
  532. return result;
  533. };
  534. BABYLON.Vector3.TransformNormalToRef = function (vector, transformation, result) {
  535. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  536. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  537. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  538. };
  539. BABYLON.Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  540. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  541. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  542. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  543. };
  544. BABYLON.Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  545. var squared = amount * amount;
  546. var cubed = amount * squared;
  547. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  548. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  549. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  550. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  551. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  552. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  553. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  554. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  555. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  556. return new BABYLON.Vector3(x, y, z);
  557. };
  558. BABYLON.Vector3.Clamp = function (value, min, max) {
  559. var x = value.x;
  560. x = (x > max.x) ? max.x : x;
  561. x = (x < min.x) ? min.x : x;
  562. var y = value.y;
  563. y = (y > max.y) ? max.y : y;
  564. y = (y < min.y) ? min.y : y;
  565. var z = value.z;
  566. z = (z > max.z) ? max.z : z;
  567. z = (z < min.z) ? min.z : z;
  568. return new BABYLON.Vector3(x, y, z);
  569. };
  570. BABYLON.Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  571. var squared = amount * amount;
  572. var cubed = amount * squared;
  573. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  574. var part2 = (-2.0 * cubed) + (3.0 * squared);
  575. var part3 = (cubed - (2.0 * squared)) + amount;
  576. var part4 = cubed - squared;
  577. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  578. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  579. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  580. return new BABYLON.Vector3(x, y, z);
  581. };
  582. BABYLON.Vector3.Lerp = function (start, end, amount) {
  583. var x = start.x + ((end.x - start.x) * amount);
  584. var y = start.y + ((end.y - start.y) * amount);
  585. var z = start.z + ((end.z - start.z) * amount);
  586. return new BABYLON.Vector3(x, y, z);
  587. };
  588. BABYLON.Vector3.Dot = function (left, right) {
  589. return (left.x * right.x + left.y * right.y + left.z * right.z);
  590. };
  591. BABYLON.Vector3.Cross = function (left, right) {
  592. var result = BABYLON.Vector3.Zero();
  593. BABYLON.Vector3.CrossToRef(left, right, result);
  594. return result;
  595. };
  596. BABYLON.Vector3.CrossToRef = function (left, right, result) {
  597. result.x = left.y * right.z - left.z * right.y;
  598. result.y = left.z * right.x - left.x * right.z;
  599. result.z = left.x * right.y - left.y * right.x;
  600. };
  601. BABYLON.Vector3.Normalize = function (vector) {
  602. var result = BABYLON.Vector3.Zero();
  603. BABYLON.Vector3.NormalizeToRef(vector, result);
  604. return result;
  605. };
  606. BABYLON.Vector3.NormalizeToRef = function (vector, result) {
  607. result.copyFrom(vector);
  608. result.normalize();
  609. };
  610. BABYLON.Vector3.Project = function (vector, world, transform, viewport) {
  611. var cw = viewport.width;
  612. var ch = viewport.height;
  613. var cx = viewport.x;
  614. var cy = viewport.y;
  615. var viewportMatrix = BABYLON.Matrix.FromValues(
  616. cw / 2.0, 0, 0, 0,
  617. 0, -ch / 2.0, 0, 0,
  618. 0, 0, 1, 0,
  619. cx + cw / 2.0, ch / 2.0 + cy, 0, 1);
  620. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  621. return BABYLON.Vector3.TransformCoordinates(vector, finalMatrix);
  622. };
  623. BABYLON.Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  624. var matrix = world.multiply(view).multiply(projection);
  625. matrix.invert();
  626. source.x = source.x / viewportWidth * 2 - 1;
  627. source.y = -(source.y / viewportHeight * 2 - 1);
  628. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  629. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  630. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  631. vector = vector.scale(1.0 / num);
  632. }
  633. return vector;
  634. };
  635. BABYLON.Vector3.Minimize = function (left, right) {
  636. var x = (left.x < right.x) ? left.x : right.x;
  637. var y = (left.y < right.y) ? left.y : right.y;
  638. var z = (left.z < right.z) ? left.z : right.z;
  639. return new BABYLON.Vector3(x, y, z);
  640. };
  641. BABYLON.Vector3.Maximize = function (left, right) {
  642. var x = (left.x > right.x) ? left.x : right.x;
  643. var y = (left.y > right.y) ? left.y : right.y;
  644. var z = (left.z > right.z) ? left.z : right.z;
  645. return new BABYLON.Vector3(x, y, z);
  646. };
  647. BABYLON.Vector3.Distance = function (value1, value2) {
  648. return Math.sqrt(BABYLON.Vector3.DistanceSquared(value1, value2));
  649. };
  650. BABYLON.Vector3.DistanceSquared = function (value1, value2) {
  651. var x = value1.x - value2.x;
  652. var y = value1.y - value2.y;
  653. var z = value1.z - value2.z;
  654. return (x * x) + (y * y) + (z * z);
  655. };
  656. ////////////////////////////////// Quaternion //////////////////////////////////
  657. BABYLON.Quaternion = function (initialX, initialY, initialZ, initialW) {
  658. this.x = initialX;
  659. this.y = initialY;
  660. this.z = initialZ;
  661. this.w = initialW;
  662. };
  663. BABYLON.Quaternion.prototype.toString = function () {
  664. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  665. };
  666. BABYLON.Quaternion.prototype.equals = function (otherQuaternion) {
  667. return this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  668. };
  669. BABYLON.Quaternion.prototype.clone = function () {
  670. return new BABYLON.Quaternion(this.x, this.y, this.z, this.w);
  671. };
  672. BABYLON.Quaternion.prototype.copyFrom = function (other) {
  673. this.x = other.x;
  674. this.y = other.y;
  675. this.z = other.z;
  676. this.w = other.w;
  677. };
  678. BABYLON.Quaternion.prototype.add = function (other) {
  679. return new BABYLON.Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  680. };
  681. BABYLON.Quaternion.prototype.scale = function (value) {
  682. return new BABYLON.Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  683. };
  684. BABYLON.Quaternion.prototype.multiply = function (q1) {
  685. var result = new BABYLON.Quaternion(0, 0, 0, 1.0);
  686. this.multiplyToRef(q1, result);
  687. return result;
  688. };
  689. BABYLON.Quaternion.prototype.multiplyToRef = function(q1, result) {
  690. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  691. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  692. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  693. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  694. };
  695. BABYLON.Quaternion.prototype.length = function () {
  696. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  697. };
  698. BABYLON.Quaternion.prototype.normalize = function () {
  699. var length = 1.0 / this.length();
  700. this.x *= length;
  701. this.y *= length;
  702. this.z *= length;
  703. this.w *= length;
  704. };
  705. BABYLON.Quaternion.prototype.toEulerAngles = function () {
  706. var qx = this.x;
  707. var qy = this.y;
  708. var qz = this.z;
  709. var qw = this.w;
  710. var sqx = qx * qx;
  711. var sqy = qy * qy;
  712. var sqz = qz * qz;
  713. var yaw = Math.atan2(2.0 * (qy * qw - qx * qz), 1.0 - 2.0 * (sqy + sqz));
  714. var pitch = Math.asin(2.0 * (qx * qy + qz * qw));
  715. var roll = Math.atan2(2.0 * (qx * qw - qy * qz), 1.0 - 2.0 * (sqx + sqz));
  716. var gimbaLockTest = qx * qy + qz * qw;
  717. if (gimbaLockTest > 0.499) {
  718. yaw = 2.0 * Math.atan2(qx, qw);
  719. roll = 0;
  720. } else if (gimbaLockTest < -0.499) {
  721. yaw = -2.0 * Math.atan2(qx, qw);
  722. roll = 0;
  723. }
  724. return new BABYLON.Vector3(pitch, yaw, roll);
  725. };
  726. BABYLON.Quaternion.prototype.toRotationMatrix = function (result) {
  727. var xx = this.x * this.x;
  728. var yy = this.y * this.y;
  729. var zz = this.z * this.z;
  730. var xy = this.x * this.y;
  731. var zw = this.z * this.w;
  732. var zx = this.z * this.x;
  733. var yw = this.y * this.w;
  734. var yz = this.y * this.z;
  735. var xw = this.x * this.w;
  736. result.m[0] = 1.0 - (2.0 * (yy + zz));
  737. result.m[1] = 2.0 * (xy + zw);
  738. result.m[2] = 2.0 * (zx - yw);
  739. result.m[3] = 0;
  740. result.m[4] = 2.0 * (xy - zw);
  741. result.m[5] = 1.0 - (2.0 * (zz + xx));
  742. result.m[6] = 2.0 * (yz + xw);
  743. result.m[7] = 0;
  744. result.m[8] = 2.0 * (zx + yw);
  745. result.m[9] = 2.0 * (yz - xw);
  746. result.m[10] = 1.0 - (2.0 * (yy + xx));
  747. result.m[11] = 0;
  748. result.m[12] = 0;
  749. result.m[13] = 0;
  750. result.m[14] = 0;
  751. result.m[15] = 1.0;
  752. };
  753. // Statics
  754. BABYLON.Quaternion.FromArray = function (array, offset) {
  755. if (!offset) {
  756. offset = 0;
  757. }
  758. return new BABYLON.Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  759. };
  760. BABYLON.Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  761. var result = new BABYLON.Quaternion();
  762. BABYLON.Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  763. return result;
  764. };
  765. BABYLON.Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  766. var halfRoll = roll * 0.5;
  767. var halfPitch = pitch * 0.5;
  768. var halfYaw = yaw * 0.5;
  769. var sinRoll = Math.sin(halfRoll);
  770. var cosRoll = Math.cos(halfRoll);
  771. var sinPitch = Math.sin(halfPitch);
  772. var cosPitch = Math.cos(halfPitch);
  773. var sinYaw = Math.sin(halfYaw);
  774. var cosYaw = Math.cos(halfYaw);
  775. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  776. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  777. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  778. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  779. };
  780. BABYLON.Quaternion.Slerp = function (left, right, amount) {
  781. var num2;
  782. var num3;
  783. var num = amount;
  784. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  785. var flag = false;
  786. if (num4 < 0) {
  787. flag = true;
  788. num4 = -num4;
  789. }
  790. if (num4 > 0.999999) {
  791. num3 = 1 - num;
  792. num2 = flag ? -num : num;
  793. }
  794. else {
  795. var num5 = Math.acos(num4);
  796. var num6 = (1.0 / Math.sin(num5));
  797. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  798. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  799. }
  800. return new BABYLON.Quaternion((num3 * left.x) + (num2 * right.x), (num3 * left.y) + (num2 * right.y), (num3 * left.z) + (num2 * right.z), (num3 * left.w) + (num2 * right.w));
  801. };
  802. ////////////////////////////////// Matrix //////////////////////////////////
  803. if (!BABYLON.MatrixType) {
  804. BABYLON.MatrixType = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  805. }
  806. BABYLON.Matrix = function () {
  807. this.m = new BABYLON.MatrixType(16);
  808. };
  809. // Properties
  810. BABYLON.Matrix.prototype.isIdentity = function () {
  811. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  812. return false;
  813. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  814. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  815. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  816. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  817. return false;
  818. return true;
  819. };
  820. BABYLON.Matrix.prototype.determinant = function () {
  821. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  822. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  823. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  824. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  825. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  826. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  827. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  828. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  829. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  830. };
  831. // Methods
  832. BABYLON.Matrix.prototype.toArray = function () {
  833. return this.m;
  834. };
  835. BABYLON.Matrix.prototype.invert = function () {
  836. this.invertToRef(this);
  837. };
  838. BABYLON.Matrix.prototype.invertToRef = function (other) {
  839. var l1 = this.m[0];
  840. var l2 = this.m[1];
  841. var l3 = this.m[2];
  842. var l4 = this.m[3];
  843. var l5 = this.m[4];
  844. var l6 = this.m[5];
  845. var l7 = this.m[6];
  846. var l8 = this.m[7];
  847. var l9 = this.m[8];
  848. var l10 = this.m[9];
  849. var l11 = this.m[10];
  850. var l12 = this.m[11];
  851. var l13 = this.m[12];
  852. var l14 = this.m[13];
  853. var l15 = this.m[14];
  854. var l16 = this.m[15];
  855. var l17 = (l11 * l16) - (l12 * l15);
  856. var l18 = (l10 * l16) - (l12 * l14);
  857. var l19 = (l10 * l15) - (l11 * l14);
  858. var l20 = (l9 * l16) - (l12 * l13);
  859. var l21 = (l9 * l15) - (l11 * l13);
  860. var l22 = (l9 * l14) - (l10 * l13);
  861. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  862. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  863. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  864. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  865. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  866. var l28 = (l7 * l16) - (l8 * l15);
  867. var l29 = (l6 * l16) - (l8 * l14);
  868. var l30 = (l6 * l15) - (l7 * l14);
  869. var l31 = (l5 * l16) - (l8 * l13);
  870. var l32 = (l5 * l15) - (l7 * l13);
  871. var l33 = (l5 * l14) - (l6 * l13);
  872. var l34 = (l7 * l12) - (l8 * l11);
  873. var l35 = (l6 * l12) - (l8 * l10);
  874. var l36 = (l6 * l11) - (l7 * l10);
  875. var l37 = (l5 * l12) - (l8 * l9);
  876. var l38 = (l5 * l11) - (l7 * l9);
  877. var l39 = (l5 * l10) - (l6 * l9);
  878. other.m[0] = l23 * l27;
  879. other.m[4] = l24 * l27;
  880. other.m[8] = l25 * l27;
  881. other.m[12] = l26 * l27;
  882. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  883. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  884. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  885. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  886. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  887. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  888. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  889. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  890. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  891. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  892. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  893. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  894. };
  895. BABYLON.Matrix.prototype.setTranslation = function (vector3) {
  896. this.m[12] = vector3.x;
  897. this.m[13] = vector3.y;
  898. this.m[14] = vector3.z;
  899. };
  900. BABYLON.Matrix.prototype.multiply = function (other) {
  901. var result = new BABYLON.Matrix();
  902. this.multiplyToRef(other, result);
  903. return result;
  904. };
  905. BABYLON.Matrix.prototype.copyFrom = function (other) {
  906. for (var index = 0; index < 16; index++) {
  907. this.m[index] = other.m[index];
  908. }
  909. };
  910. BABYLON.Matrix.prototype.multiplyToRef = function (other, result) {
  911. this.multiplyToArray(other, result.m, 0);
  912. };
  913. BABYLON.Matrix.prototype.multiplyToArray = function (other, result, offset) {
  914. result[offset] = this.m[0] * other.m[0] + this.m[1] * other.m[4] + this.m[2] * other.m[8] + this.m[3] * other.m[12];
  915. result[offset + 1] = this.m[0] * other.m[1] + this.m[1] * other.m[5] + this.m[2] * other.m[9] + this.m[3] * other.m[13];
  916. result[offset + 2] = this.m[0] * other.m[2] + this.m[1] * other.m[6] + this.m[2] * other.m[10] + this.m[3] * other.m[14];
  917. result[offset + 3] = this.m[0] * other.m[3] + this.m[1] * other.m[7] + this.m[2] * other.m[11] + this.m[3] * other.m[15];
  918. result[offset + 4] = this.m[4] * other.m[0] + this.m[5] * other.m[4] + this.m[6] * other.m[8] + this.m[7] * other.m[12];
  919. result[offset + 5] = this.m[4] * other.m[1] + this.m[5] * other.m[5] + this.m[6] * other.m[9] + this.m[7] * other.m[13];
  920. result[offset + 6] = this.m[4] * other.m[2] + this.m[5] * other.m[6] + this.m[6] * other.m[10] + this.m[7] * other.m[14];
  921. result[offset + 7] = this.m[4] * other.m[3] + this.m[5] * other.m[7] + this.m[6] * other.m[11] + this.m[7] * other.m[15];
  922. result[offset + 8] = this.m[8] * other.m[0] + this.m[9] * other.m[4] + this.m[10] * other.m[8] + this.m[11] * other.m[12];
  923. result[offset + 9] = this.m[8] * other.m[1] + this.m[9] * other.m[5] + this.m[10] * other.m[9] + this.m[11] * other.m[13];
  924. result[offset + 10] = this.m[8] * other.m[2] + this.m[9] * other.m[6] + this.m[10] * other.m[10] + this.m[11] * other.m[14];
  925. result[offset + 11] = this.m[8] * other.m[3] + this.m[9] * other.m[7] + this.m[10] * other.m[11] + this.m[11] * other.m[15];
  926. result[offset + 12] = this.m[12] * other.m[0] + this.m[13] * other.m[4] + this.m[14] * other.m[8] + this.m[15] * other.m[12];
  927. result[offset + 13] = this.m[12] * other.m[1] + this.m[13] * other.m[5] + this.m[14] * other.m[9] + this.m[15] * other.m[13];
  928. result[offset + 14] = this.m[12] * other.m[2] + this.m[13] * other.m[6] + this.m[14] * other.m[10] + this.m[15] * other.m[14];
  929. result[offset + 15] = this.m[12] * other.m[3] + this.m[13] * other.m[7] + this.m[14] * other.m[11] + this.m[15] * other.m[15];
  930. };
  931. BABYLON.Matrix.prototype.equals = function (value) {
  932. return (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  933. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  934. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  935. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  936. };
  937. BABYLON.Matrix.prototype.clone = function () {
  938. return BABYLON.Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  939. this.m[4], this.m[5], this.m[6], this.m[7],
  940. this.m[8], this.m[9], this.m[10], this.m[11],
  941. this.m[12], this.m[13], this.m[14], this.m[15]);
  942. };
  943. // Statics
  944. BABYLON.Matrix.FromArray = function (array, offset) {
  945. var result = new BABYLON.Matrix();
  946. BABYLON.Matrix.FromArrayToRef(array, offset, result);
  947. return result;
  948. };
  949. BABYLON.Matrix.FromArrayToRef = function (array, offset, result) {
  950. if (!offset) {
  951. offset = 0;
  952. }
  953. for (var index = 0; index < 16; index++) {
  954. result.m[index] = array[index + offset];
  955. }
  956. };
  957. BABYLON.Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14,
  958. initialM21, initialM22, initialM23, initialM24,
  959. initialM31, initialM32, initialM33, initialM34,
  960. initialM41, initialM42, initialM43, initialM44, result) {
  961. result.m[0] = initialM11;
  962. result.m[1] = initialM12;
  963. result.m[2] = initialM13;
  964. result.m[3] = initialM14;
  965. result.m[4] = initialM21;
  966. result.m[5] = initialM22;
  967. result.m[6] = initialM23;
  968. result.m[7] = initialM24;
  969. result.m[8] = initialM31;
  970. result.m[9] = initialM32;
  971. result.m[10] = initialM33;
  972. result.m[11] = initialM34;
  973. result.m[12] = initialM41;
  974. result.m[13] = initialM42;
  975. result.m[14] = initialM43;
  976. result.m[15] = initialM44;
  977. };
  978. BABYLON.Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14,
  979. initialM21, initialM22, initialM23, initialM24,
  980. initialM31, initialM32, initialM33, initialM34,
  981. initialM41, initialM42, initialM43, initialM44) {
  982. var result = new BABYLON.Matrix();
  983. result.m[0] = initialM11;
  984. result.m[1] = initialM12;
  985. result.m[2] = initialM13;
  986. result.m[3] = initialM14;
  987. result.m[4] = initialM21;
  988. result.m[5] = initialM22;
  989. result.m[6] = initialM23;
  990. result.m[7] = initialM24;
  991. result.m[8] = initialM31;
  992. result.m[9] = initialM32;
  993. result.m[10] = initialM33;
  994. result.m[11] = initialM34;
  995. result.m[12] = initialM41;
  996. result.m[13] = initialM42;
  997. result.m[14] = initialM43;
  998. result.m[15] = initialM44;
  999. return result;
  1000. };
  1001. BABYLON.Matrix.Identity = function () {
  1002. return BABYLON.Matrix.FromValues(1.0, 0, 0, 0,
  1003. 0, 1.0, 0, 0,
  1004. 0, 0, 1.0, 0,
  1005. 0, 0, 0, 1.0);
  1006. };
  1007. BABYLON.Matrix.IdentityToRef = function (result) {
  1008. BABYLON.Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1009. 0, 1.0, 0, 0,
  1010. 0, 0, 1.0, 0,
  1011. 0, 0, 0, 1.0, result);
  1012. };
  1013. BABYLON.Matrix.Zero = function () {
  1014. return BABYLON.Matrix.FromValues(0, 0, 0, 0,
  1015. 0, 0, 0, 0,
  1016. 0, 0, 0, 0,
  1017. 0, 0, 0, 0);
  1018. };
  1019. BABYLON.Matrix.RotationX = function (angle) {
  1020. var result = new BABYLON.Matrix();
  1021. BABYLON.Matrix.RotationXToRef(angle, result);
  1022. return result;
  1023. };
  1024. BABYLON.Matrix.RotationXToRef = function (angle, result) {
  1025. var s = Math.sin(angle);
  1026. var c = Math.cos(angle);
  1027. result.m[0] = 1.0;
  1028. result.m[15] = 1.0;
  1029. result.m[5] = c;
  1030. result.m[10] = c;
  1031. result.m[9] = -s;
  1032. result.m[6] = s;
  1033. result.m[1] = 0;
  1034. result.m[2] = 0;
  1035. result.m[3] = 0;
  1036. result.m[4] = 0;
  1037. result.m[7] = 0;
  1038. result.m[8] = 0;
  1039. result.m[11] = 0;
  1040. result.m[12] = 0;
  1041. result.m[13] = 0;
  1042. result.m[14] = 0;
  1043. };
  1044. BABYLON.Matrix.RotationY = function (angle) {
  1045. var result = new BABYLON.Matrix();
  1046. BABYLON.Matrix.RotationYToRef(angle, result);
  1047. return result;
  1048. };
  1049. BABYLON.Matrix.RotationYToRef = function (angle, result) {
  1050. var s = Math.sin(angle);
  1051. var c = Math.cos(angle);
  1052. result.m[5] = 1.0;
  1053. result.m[15] = 1.0;
  1054. result.m[0] = c;
  1055. result.m[2] = -s;
  1056. result.m[8] = s;
  1057. result.m[10] = c;
  1058. result.m[1] = 0;
  1059. result.m[3] = 0;
  1060. result.m[4] = 0;
  1061. result.m[6] = 0;
  1062. result.m[7] = 0;
  1063. result.m[9] = 0;
  1064. result.m[11] = 0;
  1065. result.m[12] = 0;
  1066. result.m[13] = 0;
  1067. result.m[14] = 0;
  1068. };
  1069. BABYLON.Matrix.RotationZ = function (angle) {
  1070. var result = new BABYLON.Matrix();
  1071. BABYLON.Matrix.RotationZToRef(angle, result);
  1072. return result;
  1073. };
  1074. BABYLON.Matrix.RotationZToRef = function (angle, result) {
  1075. var s = Math.sin(angle);
  1076. var c = Math.cos(angle);
  1077. result.m[10] = 1.0;
  1078. result.m[15] = 1.0;
  1079. result.m[0] = c;
  1080. result.m[1] = s;
  1081. result.m[4] = -s;
  1082. result.m[5] = c;
  1083. result.m[2] = 0;
  1084. result.m[3] = 0;
  1085. result.m[6] = 0;
  1086. result.m[7] = 0;
  1087. result.m[8] = 0;
  1088. result.m[9] = 0;
  1089. result.m[11] = 0;
  1090. result.m[12] = 0;
  1091. result.m[13] = 0;
  1092. result.m[14] = 0;
  1093. };
  1094. BABYLON.Matrix.RotationAxis = function (axis, angle) {
  1095. var s = Math.sin(-angle);
  1096. var c = Math.cos(-angle);
  1097. var c1 = 1 - c;
  1098. axis.normalize();
  1099. var result = BABYLON.Matrix.Zero();
  1100. result.m[0] = (axis.x * axis.x) * c1 + c;
  1101. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1102. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1103. result.m[3] = 0.0;
  1104. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1105. result.m[5] = (axis.y * axis.y) * c1 + c;
  1106. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1107. result.m[7] = 0.0;
  1108. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1109. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1110. result.m[10] = (axis.z * axis.z) * c1 + c;
  1111. result.m[11] = 0.0;
  1112. result.m[15] = 1.0;
  1113. return result;
  1114. };
  1115. BABYLON.Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1116. var result = new BABYLON.Matrix();
  1117. BABYLON.Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1118. return result;
  1119. };
  1120. var tempQuaternion = new BABYLON.Quaternion(); // For RotationYawPitchRoll
  1121. BABYLON.Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1122. BABYLON.Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, tempQuaternion);
  1123. tempQuaternion.toRotationMatrix(result);
  1124. };
  1125. BABYLON.Matrix.Scaling = function (x, y, z) {
  1126. var result = BABYLON.Matrix.Zero();
  1127. BABYLON.Matrix.ScalingToRef(x, y, z, result);
  1128. return result;
  1129. };
  1130. BABYLON.Matrix.ScalingToRef = function (x, y, z, result) {
  1131. result.m[0] = x;
  1132. result.m[1] = 0;
  1133. result.m[2] = 0;
  1134. result.m[3] = 0;
  1135. result.m[4] = 0;
  1136. result.m[5] = y;
  1137. result.m[6] = 0;
  1138. result.m[7] = 0;
  1139. result.m[8] = 0;
  1140. result.m[9] = 0;
  1141. result.m[10] = z;
  1142. result.m[11] = 0;
  1143. result.m[12] = 0;
  1144. result.m[13] = 0;
  1145. result.m[14] = 0;
  1146. result.m[15] = 1.0;
  1147. };
  1148. BABYLON.Matrix.Translation = function (x, y, z) {
  1149. var result = BABYLON.Matrix.Identity();
  1150. BABYLON.Matrix.TranslationToRef(x, y, z, result);
  1151. return result;
  1152. };
  1153. BABYLON.Matrix.TranslationToRef = function (x, y, z, result) {
  1154. BABYLON.Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1155. 0, 1.0, 0, 0,
  1156. 0, 0, 1.0, 0,
  1157. x, y, z, 1.0, result);
  1158. };
  1159. BABYLON.Matrix.LookAtLH = function (eye, target, up) {
  1160. var result = BABYLON.Matrix.Zero();
  1161. BABYLON.Matrix.LookAtLHToRef(eye, target, up, result);
  1162. return result;
  1163. };
  1164. var xAxis = BABYLON.Vector3.Zero();
  1165. var yAxis = BABYLON.Vector3.Zero();
  1166. var zAxis = BABYLON.Vector3.Zero();
  1167. BABYLON.Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1168. // Z axis
  1169. target.subtractToRef(eye, zAxis);
  1170. zAxis.normalize();
  1171. // X axis
  1172. BABYLON.Vector3.CrossToRef(up, zAxis, xAxis);
  1173. xAxis.normalize();
  1174. // Y axis
  1175. BABYLON.Vector3.CrossToRef(zAxis, xAxis, yAxis);
  1176. yAxis.normalize();
  1177. // Eye angles
  1178. var ex = -BABYLON.Vector3.Dot(xAxis, eye);
  1179. var ey = -BABYLON.Vector3.Dot(yAxis, eye);
  1180. var ez = -BABYLON.Vector3.Dot(zAxis, eye);
  1181. return BABYLON.Matrix.FromValuesToRef(xAxis.x, yAxis.x, zAxis.x, 0,
  1182. xAxis.y, yAxis.y, zAxis.y, 0,
  1183. xAxis.z, yAxis.z, zAxis.z, 0,
  1184. ex, ey, ez, 1, result);
  1185. };
  1186. BABYLON.Matrix.OrthoLH = function (width, height, znear, zfar) {
  1187. var hw = 2.0 / width;
  1188. var hh = 2.0 / height;
  1189. var id = 1.0 / (zfar - znear);
  1190. var nid = znear / (znear - zfar);
  1191. return BABYLON.Matrix.FromValues(hw, 0, 0, 0,
  1192. 0, hh, 0, 0,
  1193. 0, 0, id, 0,
  1194. 0, 0, nid, 1);
  1195. };
  1196. BABYLON.Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1197. var matrix = BABYLON.Matrix.Zero();
  1198. BABYLON.Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1199. return matrix;
  1200. };
  1201. BABYLON.Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1202. result.m[0] = 2.0 / (right - left);
  1203. result.m[1] = result.m[2] = result.m[3] = 0;
  1204. result.m[5] = 2.0 / (top - bottom);
  1205. result.m[4] = result.m[6] = result.m[7] = 0;
  1206. result.m[10] = -1.0 / (znear - zfar);
  1207. result.m[8] = result.m[9] = result.m[11] = 0;
  1208. result.m[12] = (left + right) / (left - right);
  1209. result.m[13] = (top + bottom) / (bottom - top);
  1210. result.m[14] = znear / (znear - zfar);
  1211. result.m[15] = 1.0;
  1212. };
  1213. BABYLON.Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1214. var matrix = BABYLON.Matrix.Zero();
  1215. matrix.m[0] = (2.0 * znear) / width;
  1216. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1217. matrix.m[5] = (2.0 * znear) / height;
  1218. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1219. matrix.m[10] = -zfar / (znear - zfar);
  1220. matrix.m[8] = matrix.m[9] = 0.0;
  1221. matrix.m[11] = 1.0;
  1222. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1223. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1224. return matrix;
  1225. };
  1226. BABYLON.Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1227. var matrix = BABYLON.Matrix.Zero();
  1228. BABYLON.Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1229. return matrix;
  1230. };
  1231. BABYLON.Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result) {
  1232. var tan = 1.0 / (Math.tan(fov * 0.5));
  1233. result.m[0] = tan / aspect;
  1234. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1235. result.m[5] = tan;
  1236. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1237. result.m[8] = result.m[9] = 0.0;
  1238. result.m[10] = -zfar / (znear - zfar);
  1239. result.m[11] = 1.0;
  1240. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1241. result.m[14] = (znear * zfar) / (znear - zfar);
  1242. };
  1243. BABYLON.Matrix.AffineTransformation = function (scaling, rotationCenter, rotation, translation) {
  1244. return BABYLON.Matrix.Scaling(scaling, scaling, scaling) * BABYLON.Matrix.Translation(-rotationCenter) *
  1245. BABYLON.Matrix.RotationQuaternion(rotation) * BABYLON.Matrix.Translation(rotationCenter) * BABYLON.Matrix.Translation(translation);
  1246. };
  1247. BABYLON.Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  1248. var cw = viewport.width;
  1249. var ch = viewport.height;
  1250. var cx = viewport.x;
  1251. var cy = viewport.y;
  1252. var viewportMatrix = new BABYLON.Matrix(cw / 2.0, 0, 0, 0,
  1253. 0, -ch / 2.0, 0, 0,
  1254. 0, 0, zmax - zmin, 0,
  1255. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  1256. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1257. };
  1258. BABYLON.Matrix.Transpose = function (matrix) {
  1259. var result = new BABYLON.Matrix();
  1260. result.m[0] = matrix.m[0];
  1261. result.m[1] = matrix.m[4];
  1262. result.m[2] = matrix.m[8];
  1263. result.m[3] = matrix.m[12];
  1264. result.m[4] = matrix.m[1];
  1265. result.m[5] = matrix.m[5];
  1266. result.m[6] = matrix.m[9];
  1267. result.m[7] = matrix.m[13];
  1268. result.m[8] = matrix.m[2];
  1269. result.m[9] = matrix.m[6];
  1270. result.m[10] = matrix.m[10];
  1271. result.m[11] = matrix.m[14];
  1272. result.m[12] = matrix.m[3];
  1273. result.m[13] = matrix.m[7];
  1274. result.m[14] = matrix.m[11];
  1275. result.m[15] = matrix.m[15];
  1276. return result;
  1277. };
  1278. BABYLON.Matrix.Reflection = function (plane) {
  1279. var matrix = new BABYLON.Matrix();
  1280. BABYLON.Matrix.ReflectionToRef(plane, matrix);
  1281. return matrix;
  1282. };
  1283. BABYLON.Matrix.ReflectionToRef = function (plane, result) {
  1284. plane.normalize();
  1285. var x = plane.normal.x;
  1286. var y = plane.normal.y;
  1287. var z = plane.normal.z;
  1288. var temp = -2 * x;
  1289. var temp2 = -2 * y;
  1290. var temp3 = -2 * z;
  1291. result.m[0] = (temp * x) + 1;
  1292. result.m[1] = temp2 * x;
  1293. result.m[2] = temp3 * x;
  1294. result.m[3] = 0.0;
  1295. result.m[4] = temp * y;
  1296. result.m[5] = (temp2 * y) + 1;
  1297. result.m[6] = temp3 * y;
  1298. result.m[7] = 0.0;
  1299. result.m[8] = temp * z;
  1300. result.m[9] = temp2 * z;
  1301. result.m[10] = (temp3 * z) + 1;
  1302. result.m[11] = 0.0;
  1303. result.m[12] = temp * plane.d;
  1304. result.m[13] = temp2 * plane.d;
  1305. result.m[14] = temp3 * plane.d;
  1306. result.m[15] = 1.0;
  1307. };
  1308. ////////////////////////////////// Plane //////////////////////////////////
  1309. BABYLON.Plane = function (a, b, c, d) {
  1310. this.normal = new BABYLON.Vector3(a, b, c);
  1311. this.d = d;
  1312. };
  1313. // Methods
  1314. BABYLON.Plane.prototype.normalize = function () {
  1315. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1316. var magnitude = 0;
  1317. if (norm != 0) {
  1318. magnitude = 1.0 / norm;
  1319. }
  1320. this.normal.x *= magnitude;
  1321. this.normal.y *= magnitude;
  1322. this.normal.z *= magnitude;
  1323. this.d *= magnitude;
  1324. };
  1325. BABYLON.Plane.prototype.transform = function (transformation) {
  1326. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  1327. var x = this.normal.x;
  1328. var y = this.normal.y;
  1329. var z = this.normal.z;
  1330. var d = this.d;
  1331. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1332. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1333. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1334. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1335. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  1336. };
  1337. BABYLON.Plane.prototype.dotCoordinate = function (point) {
  1338. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1339. };
  1340. BABYLON.Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  1341. var x1 = point2.x - point1.x;
  1342. var y1 = point2.y - point1.y;
  1343. var z1 = point2.z - point1.z;
  1344. var x2 = point3.x - point1.x;
  1345. var y2 = point3.y - point1.y;
  1346. var z2 = point3.z - point1.z;
  1347. var yz = (y1 * z2) - (z1 * y2);
  1348. var xz = (z1 * x2) - (x1 * z2);
  1349. var xy = (x1 * y2) - (y1 * x2);
  1350. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1351. var invPyth;
  1352. if (pyth != 0) {
  1353. invPyth = 1.0 / pyth;
  1354. }
  1355. else {
  1356. invPyth = 0;
  1357. }
  1358. this.normal.x = yz * invPyth;
  1359. this.normal.y = xz * invPyth;
  1360. this.normal.z = xy * invPyth;
  1361. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1362. };
  1363. BABYLON.Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  1364. var dot = BABYLON.Vector3.Dot(this.normal, direction);
  1365. return (dot <= epsilon);
  1366. };
  1367. BABYLON.Plane.prototype.signedDistanceTo = function (point) {
  1368. return BABYLON.Vector3.Dot(point, this.normal) + this.d;
  1369. };
  1370. // Statics
  1371. BABYLON.Plane.FromArray = function (array) {
  1372. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  1373. };
  1374. BABYLON.Plane.FromPoints = function (point1, point2, point3) {
  1375. var result = new BABYLON.Plane(0, 0, 0, 0);
  1376. result.copyFromPoints(point1, point2, point3);
  1377. return result;
  1378. };
  1379. BABYLON.Plane.FromPositionAndNormal = function (origin, normal) {
  1380. var result = new BABYLON.Plane(0, 0, 0, 0);
  1381. normal.normalize();
  1382. result.normal = normal;
  1383. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1384. return result;
  1385. };
  1386. BABYLON.Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  1387. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1388. return BABYLON.Vector3.Dot(point, normal) + d;
  1389. };
  1390. ////////////////////////////////// Frustum //////////////////////////////////
  1391. BABYLON.Frustum = {};
  1392. // Statics
  1393. BABYLON.Frustum.GetPlanes = function (transform) {
  1394. var frustumPlanes = [];
  1395. for (var index = 0; index < 6; index++) {
  1396. frustumPlanes.push(new BABYLON.Plane(0, 0, 0, 0));
  1397. }
  1398. BABYLON.Frustum.GetPlanesToRef(transform, frustumPlanes);
  1399. return frustumPlanes;
  1400. };
  1401. BABYLON.Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  1402. // Near
  1403. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1404. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1405. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1406. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1407. frustumPlanes[0].normalize();
  1408. // Far
  1409. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1410. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1411. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1412. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1413. frustumPlanes[1].normalize();
  1414. // Left
  1415. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1416. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1417. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1418. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1419. frustumPlanes[2].normalize();
  1420. // Right
  1421. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1422. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1423. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1424. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1425. frustumPlanes[3].normalize();
  1426. // Top
  1427. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1428. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1429. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1430. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1431. frustumPlanes[4].normalize();
  1432. // Bottom
  1433. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1434. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1435. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1436. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1437. frustumPlanes[5].normalize();
  1438. };
  1439. ////////////////////////////////// Viewport //////////////////////////////////
  1440. BABYLON.Viewport = {};
  1441. BABYLON.Viewport = function (x, y, width, height) {
  1442. this.width = width;
  1443. this.height = height;
  1444. this.x = x;
  1445. this.y = y;
  1446. };
  1447. BABYLON.Viewport.prototype.toGlobal = function (engine) {
  1448. var width = engine.getRenderWidth() * engine.getHardwareScalingLevel();
  1449. var height = engine.getRenderHeight() * engine.getHardwareScalingLevel();
  1450. return new BABYLON.Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  1451. };
  1452. })();