babylon.math.js 60 KB

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