babylon.math.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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.equals = function (otherColor) {
  169. return this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  170. };
  171. BABYLON.Color3.prototype.scale = function (scale) {
  172. return new BABYLON.Color3(this.r * scale, this.g * scale, this.b * scale);
  173. };
  174. BABYLON.Color3.prototype.clone = function () {
  175. return new BABYLON.Color3(this.r, this.g, this.b);
  176. };
  177. // Statics
  178. BABYLON.Color3.FromArray = function (array) {
  179. return new BABYLON.Color3(array[0], array[1], array[2]);
  180. };
  181. ////////////////////////////////// Color4 //////////////////////////////////
  182. BABYLON.Color4 = function (initialR, initialG, initialB, initialA) {
  183. this.r = initialR;
  184. this.g = initialG;
  185. this.b = initialB;
  186. this.a = initialA;
  187. };
  188. // Operators
  189. BABYLON.Color4.prototype.add = function (right) {
  190. return new BABYLON.Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  191. };
  192. BABYLON.Color4.prototype.subtract = 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.scale = function (scale) {
  196. return new BABYLON.Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  197. };
  198. BABYLON.Color4.prototype.toString = function () {
  199. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  200. };
  201. BABYLON.Color4.prototype.clone = function () {
  202. return new BABYLON.Color4(this.r, this.g, this.b, this.a);
  203. };
  204. // Statics
  205. BABYLON.Color4.Lerp = function(left, right, amount) {
  206. var r = left.r + (right.r - left.r) * amount;
  207. var g = left.g + (right.g - left.g) * amount;
  208. var b = left.b + (right.b - left.b) * amount;
  209. var a = left.a + (right.a - left.a) * amount;
  210. return new BABYLON.Color4(r, g, b, a);
  211. };
  212. BABYLON.Color4.FromArray = function (array, offset) {
  213. if (!offset) {
  214. offset = 0;
  215. }
  216. return new BABYLON.Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  217. };
  218. ////////////////////////////////// Vector2 //////////////////////////////////
  219. BABYLON.Vector2 = function (initialX, initialY) {
  220. this.x = initialX;
  221. this.y = initialY;
  222. };
  223. BABYLON.Vector2.prototype.toString = function () {
  224. return "{X: " + this.x + " Y:" + this.y + "}";
  225. };
  226. // Operators
  227. BABYLON.Vector2.prototype.add = function (otherVector) {
  228. return new BABYLON.Vector2(this.x + otherVector.x, this.y + otherVector.y);
  229. };
  230. BABYLON.Vector2.prototype.subtract = function (otherVector) {
  231. return new BABYLON.Vector2(this.x - otherVector.x, this.y - otherVector.y);
  232. };
  233. BABYLON.Vector2.prototype.negate = function () {
  234. return new BABYLON.Vector2(-this.x, -this.y);
  235. };
  236. BABYLON.Vector2.prototype.scale = function (scale) {
  237. return new BABYLON.Vector2(this.x * scale, this.y * scale);
  238. };
  239. BABYLON.Vector2.prototype.equals = function (otherVector) {
  240. return this.x === otherVector.x && this.y === otherVector.y;
  241. };
  242. // Properties
  243. BABYLON.Vector2.prototype.length = function () {
  244. return Math.sqrt(this.x * this.x + this.y * this.y);
  245. };
  246. BABYLON.Vector2.prototype.lengthSquared = function () {
  247. return (this.x * this.x + this.y * this.y);
  248. };
  249. // Methods
  250. BABYLON.Vector2.prototype.normalize = function () {
  251. var len = this.length();
  252. if (len === 0)
  253. return;
  254. var num = 1.0 / len;
  255. this.x *= num;
  256. this.y *= num;
  257. };
  258. BABYLON.Vector2.prototype.clone = function () {
  259. return new BABYLON.Vector2(this.x, this.y);
  260. };
  261. // Statics
  262. BABYLON.Vector2.Zero = function () {
  263. return new BABYLON.Vector2(0, 0);
  264. };
  265. BABYLON.Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  266. var squared = amount * amount;
  267. var cubed = amount * squared;
  268. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  269. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  270. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  271. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  272. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  273. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  274. return new BABYLON.Vector2(x, y);
  275. };
  276. BABYLON.Vector2.Clamp = function (value, min, max) {
  277. var x = value.x;
  278. x = (x > max.x) ? max.x : x;
  279. x = (x < min.x) ? min.x : x;
  280. var y = value.y;
  281. y = (y > max.y) ? max.y : y;
  282. y = (y < min.y) ? min.y : y;
  283. return new BABYLON.Vector2(x, y);
  284. };
  285. BABYLON.Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  286. var squared = amount * amount;
  287. var cubed = amount * squared;
  288. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  289. var part2 = (-2.0 * cubed) + (3.0 * squared);
  290. var part3 = (cubed - (2.0 * squared)) + amount;
  291. var part4 = cubed - squared;
  292. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  293. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  294. return new BABYLON.Vector2(x, y);
  295. };
  296. BABYLON.Vector2.Lerp = function (start, end, amount) {
  297. var x = start.x + ((end.x - start.x) * amount);
  298. var y = start.y + ((end.y - start.y) * amount);
  299. return new BABYLON.Vector2(x, y);
  300. };
  301. BABYLON.Vector2.Dot = function (left, right) {
  302. return left.x * right.x + left.y * right.y;
  303. };
  304. BABYLON.Vector2.Normalize = function (vector) {
  305. var newVector = vector.clone();
  306. newVector.normalize();
  307. return newVector;
  308. };
  309. BABYLON.Vector2.Minimize = function (left, right) {
  310. var x = (left.x < right.x) ? left.x : right.x;
  311. var y = (left.y < right.y) ? left.y : right.y;
  312. return new BABYLON.Vector2(x, y);
  313. };
  314. BABYLON.Vector2.Maximize = function (left, right) {
  315. var x = (left.x > right.x) ? left.x : right.x;
  316. var y = (left.y > right.y) ? left.y : right.y;
  317. return new BABYLON.Vector2(x, y);
  318. };
  319. BABYLON.Vector2.Transform = function (vector, transformation) {
  320. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  321. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  322. return new BABYLON.Vector2(x, y);
  323. };
  324. BABYLON.Vector2.Distance = function (value1, value2) {
  325. return Math.sqrt(BABYLON.Vector2.DistanceSquared(value1, value2));
  326. };
  327. BABYLON.Vector2.DistanceSquared = function (value1, value2) {
  328. var x = value1.x - value2.x;
  329. var y = value1.y - value2.y;
  330. return (x * x) + (y * y);
  331. };
  332. ////////////////////////////////// Vector3 //////////////////////////////////
  333. BABYLON.Vector3 = function (initialX, initialY, initialZ) {
  334. this.x = initialX;
  335. this.y = initialY;
  336. this.z = initialZ;
  337. };
  338. BABYLON.Vector3.prototype.toString = function () {
  339. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  340. };
  341. // Operators
  342. BABYLON.Vector3.prototype.add = function (otherVector) {
  343. return new BABYLON.Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  344. };
  345. BABYLON.Vector3.prototype.subtract = function (otherVector) {
  346. return new BABYLON.Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  347. };
  348. BABYLON.Vector3.prototype.negate = function () {
  349. return new BABYLON.Vector3(-this.x, -this.y, -this.z);
  350. };
  351. BABYLON.Vector3.prototype.scale = function (scale) {
  352. return new BABYLON.Vector3(this.x * scale, this.y * scale, this.z * scale);
  353. };
  354. BABYLON.Vector3.prototype.equals = function (otherVector) {
  355. return this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  356. };
  357. BABYLON.Vector3.prototype.multiply = function (otherVector) {
  358. return new BABYLON.Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  359. };
  360. BABYLON.Vector3.prototype.divide = function (otherVector) {
  361. return new BABYLON.Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  362. };
  363. // Properties
  364. BABYLON.Vector3.prototype.length = function () {
  365. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  366. };
  367. BABYLON.Vector3.prototype.lengthSquared = function () {
  368. return (this.x * this.x + this.y * this.y + this.z * this.z);
  369. };
  370. // Methods
  371. BABYLON.Vector3.prototype.normalize = function () {
  372. var len = this.length();
  373. if (len === 0)
  374. return;
  375. var num = 1.0 / len;
  376. this.x *= num;
  377. this.y *= num;
  378. this.z *= num;
  379. };
  380. BABYLON.Vector3.prototype.clone = function () {
  381. return new BABYLON.Vector3(this.x, this.y, this.z);
  382. };
  383. // Statics
  384. BABYLON.Vector3.FromArray = function (array, offset) {
  385. if (!offset) {
  386. offset = 0;
  387. }
  388. return new BABYLON.Vector3(array[offset], array[offset + 1], array[offset + 2]);
  389. };
  390. BABYLON.Vector3.Zero = function () {
  391. return new BABYLON.Vector3(0, 0, 0);
  392. };
  393. BABYLON.Vector3.Up = function () {
  394. return new BABYLON.Vector3(0, 1.0, 0);
  395. };
  396. BABYLON.Vector3.TransformCoordinates = function (vector, transformation) {
  397. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  398. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  399. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  400. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  401. return new BABYLON.Vector3(x / w, y / w, z / w);
  402. };
  403. BABYLON.Vector3.TransformNormal = function (vector, transformation) {
  404. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  405. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  406. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  407. return new BABYLON.Vector3(x, y, z);
  408. };
  409. BABYLON.Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  410. var squared = amount * amount;
  411. var cubed = amount * squared;
  412. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  413. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  414. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  415. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  416. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  417. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  418. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  419. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  420. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  421. return new BABYLON.Vector3(x, y, z);
  422. };
  423. BABYLON.Vector3.Clamp = function (value, min, max) {
  424. var x = value.x;
  425. x = (x > max.x) ? max.x : x;
  426. x = (x < min.x) ? min.x : x;
  427. var y = value.y;
  428. y = (y > max.y) ? max.y : y;
  429. y = (y < min.y) ? min.y : y;
  430. var z = value.z;
  431. z = (z > max.z) ? max.z : z;
  432. z = (z < min.z) ? min.z : z;
  433. return new BABYLON.Vector3(x, y, z);
  434. };
  435. BABYLON.Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  436. var squared = amount * amount;
  437. var cubed = amount * squared;
  438. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  439. var part2 = (-2.0 * cubed) + (3.0 * squared);
  440. var part3 = (cubed - (2.0 * squared)) + amount;
  441. var part4 = cubed - squared;
  442. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  443. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  444. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  445. return new BABYLON.Vector3(x, y, z);
  446. };
  447. BABYLON.Vector3.Lerp = function (start, end, amount) {
  448. var x = start.x + ((end.x - start.x) * amount);
  449. var y = start.y + ((end.y - start.y) * amount);
  450. var z = start.z + ((end.z - start.z) * amount);
  451. return new BABYLON.Vector3(x, y, z);
  452. };
  453. BABYLON.Vector3.Dot = function (left, right) {
  454. return (left.x * right.x + left.y * right.y + left.z * right.z);
  455. };
  456. BABYLON.Vector3.Cross = function (left, right) {
  457. var x = left.y * right.z - left.z * right.y;
  458. var y = left.z * right.x - left.x * right.z;
  459. var z = left.x * right.y - left.y * right.x;
  460. return new BABYLON.Vector3(x, y, z);
  461. };
  462. BABYLON.Vector3.Normalize = function (vector) {
  463. var newVector = vector.clone();
  464. newVector.normalize();
  465. return newVector;
  466. };
  467. BABYLON.Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  468. var matrix = world.multiply(view).multiply(projection);
  469. matrix.invert();
  470. source.x = source.x / viewportWidth * 2 - 1;
  471. source.y = -(source.y / viewportHeight * 2 - 1);
  472. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  473. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  474. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  475. vector = vector.scale(1.0 / num);
  476. }
  477. return vector;
  478. };
  479. BABYLON.Vector3.Minimize = function (left, right) {
  480. var x = (left.x < right.x) ? left.x : right.x;
  481. var y = (left.y < right.y) ? left.y : right.y;
  482. var z = (left.z < right.z) ? left.z : right.z;
  483. return new BABYLON.Vector3(x, y, z);
  484. };
  485. BABYLON.Vector3.Maximize = function (left, right) {
  486. var x = (left.x > right.x) ? left.x : right.x;
  487. var y = (left.y > right.y) ? left.y : right.y;
  488. var z = (left.z > right.z) ? left.z : right.z;
  489. return new BABYLON.Vector3(x, y, z);
  490. };
  491. BABYLON.Vector3.Distance = function (value1, value2) {
  492. return Math.sqrt(BABYLON.Vector3.DistanceSquared(value1, value2));
  493. };
  494. BABYLON.Vector3.DistanceSquared = function (value1, value2) {
  495. var x = value1.x - value2.x;
  496. var y = value1.y - value2.y;
  497. var z = value1.z - value2.z;
  498. return (x * x) + (y * y) + (z * z);
  499. };
  500. ////////////////////////////////// Quaternion //////////////////////////////////
  501. BABYLON.Quaternion = function (initialX, initialY, initialZ, initialW) {
  502. this.x = initialX;
  503. this.y = initialY;
  504. this.z = initialZ;
  505. this.w = initialW;
  506. };
  507. BABYLON.Quaternion.prototype.toString = function () {
  508. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  509. };
  510. BABYLON.Quaternion.prototype.clone = function () {
  511. return new BABYLON.Quaternion(this.x, this.y, this.z, this.w);
  512. };
  513. BABYLON.Quaternion.prototype.add = function(other) {
  514. return new BABYLON.Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  515. };
  516. BABYLON.Quaternion.prototype.scale = function (value) {
  517. return new BABYLON.Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  518. };
  519. BABYLON.Quaternion.prototype.toEulerAngles = function () {
  520. var q0 = this.x;
  521. var q1 = this.y;
  522. var q2 = this.y;
  523. var q3 = this.w;
  524. var x = Math.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2));
  525. var y = Math.asin(2 * (q0 * q2 - q3 * q1));
  526. var z = Math.atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3));
  527. return new BABYLON.Vector3(x, y, z);
  528. };
  529. // Statics
  530. BABYLON.Quaternion.FromArray = function (array, offset) {
  531. if (!offset) {
  532. offset = 0;
  533. }
  534. return new BABYLON.Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  535. };
  536. BABYLON.Quaternion.Slerp = function(left, right, amount) {
  537. var num2;
  538. var num3;
  539. var num = amount;
  540. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  541. var flag = false;
  542. if (num4 < 0)
  543. {
  544. flag = true;
  545. num4 = -num4;
  546. }
  547. if (num4 > 0.999999)
  548. {
  549. num3 = 1 - num;
  550. num2 = flag ? -num : num;
  551. }
  552. else
  553. {
  554. var num5 = Math.acos(num4);
  555. var num6 = (1.0 / Math.sin(num5));
  556. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  557. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  558. }
  559. 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));
  560. };
  561. ////////////////////////////////// Matrix //////////////////////////////////
  562. if(!MatrixType) {
  563. var MatrixType = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  564. }
  565. BABYLON.Matrix = function () {
  566. this.m = new MatrixType(16);
  567. };
  568. // Properties
  569. BABYLON.Matrix.prototype.isIdentity = function () {
  570. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  571. return false;
  572. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  573. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  574. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  575. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  576. return false;
  577. return true;
  578. };
  579. BABYLON.Matrix.prototype.determinant = function () {
  580. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  581. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  582. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  583. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  584. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  585. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  586. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  587. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  588. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  589. };
  590. // Methods
  591. BABYLON.Matrix.prototype.toArray = function () {
  592. return this.m;
  593. };
  594. BABYLON.Matrix.prototype.invert = function () {
  595. var l1 = this.m[0];
  596. var l2 = this.m[1];
  597. var l3 = this.m[2];
  598. var l4 = this.m[3];
  599. var l5 = this.m[4];
  600. var l6 = this.m[5];
  601. var l7 = this.m[6];
  602. var l8 = this.m[7];
  603. var l9 = this.m[8];
  604. var l10 = this.m[9];
  605. var l11 = this.m[10];
  606. var l12 = this.m[11];
  607. var l13 = this.m[12];
  608. var l14 = this.m[13];
  609. var l15 = this.m[14];
  610. var l16 = this.m[15];
  611. var l17 = (l11 * l16) - (l12 * l15);
  612. var l18 = (l10 * l16) - (l12 * l14);
  613. var l19 = (l10 * l15) - (l11 * l14);
  614. var l20 = (l9 * l16) - (l12 * l13);
  615. var l21 = (l9 * l15) - (l11 * l13);
  616. var l22 = (l9 * l14) - (l10 * l13);
  617. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  618. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  619. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  620. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  621. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  622. var l28 = (l7 * l16) - (l8 * l15);
  623. var l29 = (l6 * l16) - (l8 * l14);
  624. var l30 = (l6 * l15) - (l7 * l14);
  625. var l31 = (l5 * l16) - (l8 * l13);
  626. var l32 = (l5 * l15) - (l7 * l13);
  627. var l33 = (l5 * l14) - (l6 * l13);
  628. var l34 = (l7 * l12) - (l8 * l11);
  629. var l35 = (l6 * l12) - (l8 * l10);
  630. var l36 = (l6 * l11) - (l7 * l10);
  631. var l37 = (l5 * l12) - (l8 * l9);
  632. var l38 = (l5 * l11) - (l7 * l9);
  633. var l39 = (l5 * l10) - (l6 * l9);
  634. this.m[0] = l23 * l27;
  635. this.m[4] = l24 * l27;
  636. this.m[8] = l25 * l27;
  637. this.m[12] = l26 * l27;
  638. this.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  639. this.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  640. this.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  641. this.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  642. this.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  643. this.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  644. this.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  645. this.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  646. this.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  647. this.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  648. this.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  649. this.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  650. };
  651. BABYLON.Matrix.prototype.multiply = function (other) {
  652. var result = new BABYLON.Matrix();
  653. 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];
  654. 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];
  655. 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];
  656. 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];
  657. 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];
  658. 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];
  659. 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];
  660. 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];
  661. 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];
  662. 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];
  663. 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];
  664. 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];
  665. 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];
  666. 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];
  667. 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];
  668. 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];
  669. return result;
  670. };
  671. BABYLON.Matrix.prototype.equals = function (value) {
  672. 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] &&
  673. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  674. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  675. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  676. };
  677. BABYLON.Matrix.prototype.clone = function () {
  678. return BABYLON.Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  679. this.m[4], this.m[5], this.m[6], this.m[7],
  680. this.m[8], this.m[9], this.m[10], this.m[11],
  681. this.m[12], this.m[13], this.m[14], this.m[15]);
  682. };
  683. // Statics
  684. BABYLON.Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14,
  685. initialM21, initialM22, initialM23, initialM24,
  686. initialM31, initialM32, initialM33, initialM34,
  687. initialM41, initialM42, initialM43, initialM44) {
  688. var result = new BABYLON.Matrix();
  689. result.m[0] = initialM11;
  690. result.m[1] = initialM12;
  691. result.m[2] = initialM13;
  692. result.m[3] = initialM14;
  693. result.m[4] = initialM21;
  694. result.m[5] = initialM22;
  695. result.m[6] = initialM23;
  696. result.m[7] = initialM24;
  697. result.m[8] = initialM31;
  698. result.m[9] = initialM32;
  699. result.m[10] = initialM33;
  700. result.m[11] = initialM34;
  701. result.m[12] = initialM41;
  702. result.m[13] = initialM42;
  703. result.m[14] = initialM43;
  704. result.m[15] = initialM44;
  705. return result;
  706. };
  707. BABYLON.Matrix.Identity = function () {
  708. return BABYLON.Matrix.FromValues(1.0, 0, 0, 0,
  709. 0, 1.0, 0, 0,
  710. 0, 0, 1.0, 0,
  711. 0, 0, 0, 1.0);
  712. };
  713. BABYLON.Matrix.Zero = function () {
  714. return BABYLON.Matrix.FromValues(0, 0, 0, 0,
  715. 0, 0, 0, 0,
  716. 0, 0, 0, 0,
  717. 0, 0, 0, 0);
  718. };
  719. BABYLON.Matrix.RotationX = function (angle) {
  720. var result = BABYLON.Matrix.Zero();
  721. var s = Math.sin(angle);
  722. var c = Math.cos(angle);
  723. result.m[0] = 1.0;
  724. result.m[15] = 1.0;
  725. result.m[5] = c;
  726. result.m[10] = c;
  727. result.m[9] = -s;
  728. result.m[6] = s;
  729. return result;
  730. };
  731. BABYLON.Matrix.RotationY = function (angle) {
  732. var result = BABYLON.Matrix.Zero();
  733. var s = Math.sin(angle);
  734. var c = Math.cos(angle);
  735. result.m[5] = 1.0;
  736. result.m[15] = 1.0;
  737. result.m[0] = c;
  738. result.m[2] = -s;
  739. result.m[8] = s;
  740. result.m[10] = c;
  741. return result;
  742. };
  743. BABYLON.Matrix.RotationZ = function (angle) {
  744. var result = BABYLON.Matrix.Zero();
  745. var s = Math.sin(angle);
  746. var c = Math.cos(angle);
  747. result.m[10] = 1.0;
  748. result.m[15] = 1.0;
  749. result.m[0] = c;
  750. result.m[1] = s;
  751. result.m[4] = -s;
  752. result.m[5] = c;
  753. return result;
  754. };
  755. BABYLON.Matrix.RotationAxis = function (axis, angle) {
  756. var s = Math.sin(-angle);
  757. var c = Math.cos(-angle);
  758. var c1 = 1 - c;
  759. axis.normalize();
  760. var result = BABYLON.Matrix.Zero();
  761. result.m[0] = (axis.x * axis.x) * c1 + c;
  762. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  763. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  764. result.m[3] = 0.0;
  765. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  766. result.m[5] = (axis.y * axis.y) * c1 + c;
  767. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  768. result.m[7] = 0.0;
  769. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  770. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  771. result.m[10] = (axis.z * axis.z) * c1 + c;
  772. result.m[11] = 0.0;
  773. result.m[15] = 1.0;
  774. return result;
  775. };
  776. BABYLON.Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  777. return BABYLON.Matrix.RotationZ(roll).multiply(BABYLON.Matrix.RotationX(pitch)).multiply(BABYLON.Matrix.RotationY(yaw));
  778. };
  779. BABYLON.Matrix.Scaling = function (x, y, z) {
  780. var result = BABYLON.Matrix.Zero();
  781. result.m[0] = x;
  782. result.m[5] = y;
  783. result.m[10] = z;
  784. result.m[15] = 1.0;
  785. return result;
  786. };
  787. BABYLON.Matrix.Translation = function (x, y, z) {
  788. var result = BABYLON.Matrix.Identity();
  789. result.m[12] = x;
  790. result.m[13] = y;
  791. result.m[14] = z;
  792. return result;
  793. };
  794. BABYLON.Matrix.LookAtLH = function (eye, target, up) {
  795. // Z axis
  796. var zAxis = target.subtract(eye);
  797. zAxis.normalize();
  798. // X axis
  799. var xAxis = BABYLON.Vector3.Cross(up, zAxis);
  800. xAxis.normalize();
  801. // Y axis
  802. var yAxis = BABYLON.Vector3.Cross(zAxis, xAxis);
  803. yAxis.normalize();
  804. // Eye angles
  805. var ex = -BABYLON.Vector3.Dot(xAxis, eye);
  806. var ey = -BABYLON.Vector3.Dot(yAxis, eye);
  807. var ez = -BABYLON.Vector3.Dot(zAxis, eye);
  808. return BABYLON.Matrix.FromValues(xAxis.x, yAxis.x, zAxis.x, 0,
  809. xAxis.y, yAxis.y, zAxis.y, 0,
  810. xAxis.z, yAxis.z, zAxis.z, 0,
  811. ex, ey, ez, 1);
  812. };
  813. BABYLON.Matrix.OrthoLH = function (width, height, znear, zfar) {
  814. var hw = 2.0 / width;
  815. var hh = 2.0 / height;
  816. var id = 1.0 / (zfar - znear);
  817. var nid = znear / (znear - zfar);
  818. return BABYLON.Matrix.FromValues(hw, 0, 0, 0,
  819. 0, hh, 0, 0,
  820. 0, 0, id, 0,
  821. 0, 0, nid, 1);
  822. };
  823. BABYLON.Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  824. var matrix = BABYLON.Matrix.Zero();
  825. matrix.m[0] = 2.0 / (right - left);
  826. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0;
  827. matrix.m[5] = 2.0 / (top - bottom);
  828. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0;
  829. matrix.m[10] = -1.0 / (znear - zfar);
  830. matrix.m[8] = matrix.m[9] = matrix.m[11] = 0;
  831. matrix.m[12] = (left + right) / (left - right);
  832. matrix.m[13] = (top + bottom) / (bottom - top);
  833. matrix.m[14] = znear / (znear - zfar);
  834. matrix.m[15] = 1.0;
  835. return matrix;
  836. };
  837. BABYLON.Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  838. var matrix = BABYLON.Matrix.Zero();
  839. matrix.m[0] = (2.0 * znear) / width;
  840. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  841. matrix.m[5] = (2.0 * znear) / height;
  842. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  843. matrix.m[10] = -zfar / (znear - zfar);
  844. matrix.m[8] = matrix.m[9] = 0.0;
  845. matrix.m[11] = 1.0;
  846. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  847. matrix.m[14] = (znear * zfar) / (znear - zfar);
  848. return matrix;
  849. };
  850. BABYLON.Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  851. var matrix = BABYLON.Matrix.Zero();
  852. var tan = 1.0 / (Math.tan(fov * 0.5));
  853. matrix.m[0] = tan / aspect;
  854. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  855. matrix.m[5] = tan;
  856. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  857. matrix.m[8] = matrix.m[9] = 0.0;
  858. matrix.m[10] = -zfar / (znear - zfar);
  859. matrix.m[11] = 1.0;
  860. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  861. matrix.m[14] = (znear * zfar) / (znear - zfar);
  862. return matrix;
  863. };
  864. BABYLON.Matrix.AffineTransformation = function (scaling, rotationCenter, rotation, translation) {
  865. return BABYLON.Matrix.Scaling(scaling, scaling, scaling) * BABYLON.Matrix.Translation(-rotationCenter) *
  866. BABYLON.Matrix.RotationQuaternion(rotation) * BABYLON.Matrix.Translation(rotationCenter) * BABYLON.Matrix.Translation(translation);
  867. };
  868. BABYLON.Matrix.GetFinalMatrix = function (viewport, world, view, projection) {
  869. var cw = viewport.width;
  870. var ch = viewport.height;
  871. var cx = viewport.x;
  872. var cy = viewport.y;
  873. var zmin = viewport.minZ;
  874. var zmax = viewport.maxZ;
  875. var viewportMatrix = new BABYLON.Matrix(cw / 2.0, 0, 0, 0,
  876. 0, -ch / 2.0, 0, 0,
  877. 0, 0, zmax - zmin, 0,
  878. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  879. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  880. };
  881. BABYLON.Matrix.Transpose = function (matrix) {
  882. var result = new BABYLON.Matrix();
  883. result.m[0] = matrix.m[0];
  884. result.m[1] = matrix.m[4];
  885. result.m[2] = matrix.m[8];
  886. result.m[3] = matrix.m[12];
  887. result.m[4] = matrix.m[1];
  888. result.m[5] = matrix.m[5];
  889. result.m[6] = matrix.m[9];
  890. result.m[7] = matrix.m[13];
  891. result.m[8] = matrix.m[2];
  892. result.m[9] = matrix.m[6];
  893. result.m[10] = matrix.m[10];
  894. result.m[11] = matrix.m[14];
  895. result.m[12] = matrix.m[3];
  896. result.m[13] = matrix.m[7];
  897. result.m[14] = matrix.m[11];
  898. result.m[15] = matrix.m[15];
  899. return result;
  900. };
  901. BABYLON.Matrix.Reflection = function (plane) {
  902. var matrix = new BABYLON.Matrix();
  903. plane.normalize();
  904. var x = plane.normal.x;
  905. var y = plane.normal.y;
  906. var z = plane.normal.z;
  907. var temp = -2 * x;
  908. var temp2 = -2 * y;
  909. var temp3 = -2 * z;
  910. matrix.m[0] = (temp * x) + 1;
  911. matrix.m[1] = temp2 * x;
  912. matrix.m[2] = temp3 * x;
  913. matrix.m[3] = 0.0;
  914. matrix.m[4] = temp * y;
  915. matrix.m[5] = (temp2 * y) + 1;
  916. matrix.m[6] = temp3 * y;
  917. matrix.m[7] = 0.0;
  918. matrix.m[8] = temp * z;
  919. matrix.m[9] = temp2 * z;
  920. matrix.m[10] = (temp3 * z) + 1;
  921. matrix.m[11] = 0.0;
  922. matrix.m[12] = temp * plane.d;
  923. matrix.m[13] = temp2 * plane.d;
  924. matrix.m[14] = temp3 * plane.d;
  925. matrix.m[15] = 1.0;
  926. return matrix;
  927. };
  928. ////////////////////////////////// Plane //////////////////////////////////
  929. BABYLON.Plane = function (a, b, c, d) {
  930. this.normal = new BABYLON.Vector3(a, b, c);
  931. this.d = d;
  932. };
  933. // Methods
  934. BABYLON.Plane.prototype.normalize = function () {
  935. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  936. var magnitude = 0;
  937. if (norm != 0) {
  938. magnitude = 1.0 / norm;
  939. }
  940. this.normal.x *= magnitude;
  941. this.normal.y *= magnitude;
  942. this.normal.z *= magnitude;
  943. this.d *= magnitude;
  944. };
  945. BABYLON.Plane.prototype.transform = function(transformation) {
  946. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  947. var x = this.normal.x;
  948. var y = this.normal.y;
  949. var z = this.normal.z;
  950. var d = this.d;
  951. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  952. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  953. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  954. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  955. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  956. };
  957. BABYLON.Plane.prototype.dotCoordinate = function (point) {
  958. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  959. };
  960. // Statics
  961. BABYLON.Plane.FromArray = function (array) {
  962. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  963. };
  964. BABYLON.Plane.FromPoints = function(point1, point2, point3) {
  965. var x1 = point2.x - point1.x;
  966. var y1 = point2.y - point1.y;
  967. var z1 = point2.z - point1.z;
  968. var x2 = point3.x - point1.x;
  969. var y2 = point3.y - point1.y;
  970. var z2 = point3.z - point1.z;
  971. var yz = (y1 * z2) - (z1 * y2);
  972. var xz = (z1 * x2) - (x1 * z2);
  973. var xy = (x1 * y2) - (y1 * x2);
  974. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  975. var invPyth;
  976. if (pyth != 0)
  977. invPyth = 1.0 / pyth;
  978. else
  979. invPyth = 0;
  980. var normal = new BABYLON.Vector3(yz * invPyth, xz * invPyth, xy * invPyth);
  981. var d = -((normal.x * point1.x) + (normal.y * point1.y) + (normal.z * point1.z));
  982. return new BABYLON.Plane(normal.x, normal.y, normal.z, d);
  983. };
  984. ////////////////////////////////// Frustum //////////////////////////////////
  985. BABYLON.Frustum = {};
  986. // Statics
  987. BABYLON.Frustum.GetPlanes = function (transform) {
  988. var frustumPlanes = [];
  989. frustumPlanes.push(new BABYLON.Plane( // near
  990. transform.m[3] + transform.m[2],
  991. transform.m[7] + transform.m[6],
  992. transform.m[10] + transform.m[10],
  993. transform.m[15] + transform.m[14]));
  994. frustumPlanes[0].normalize();
  995. frustumPlanes.push(new BABYLON.Plane( // far
  996. transform.m[3] - transform.m[2],
  997. transform.m[7] - transform.m[6],
  998. transform.m[11] - transform.m[10],
  999. transform.m[15] - transform.m[14]));
  1000. frustumPlanes[1].normalize();
  1001. frustumPlanes.push(new BABYLON.Plane( // left
  1002. transform.m[3] + transform.m[0],
  1003. transform.m[7] + transform.m[4],
  1004. transform.m[11] + transform.m[8],
  1005. transform.m[15] + transform.m[12]));
  1006. frustumPlanes[2].normalize();
  1007. frustumPlanes.push(new BABYLON.Plane( // right
  1008. transform.m[3] - transform.m[0],
  1009. transform.m[7] - transform.m[4],
  1010. transform.m[11] - transform.m[8],
  1011. transform.m[15] - transform.m[12]));
  1012. frustumPlanes[3].normalize();
  1013. frustumPlanes.push(new BABYLON.Plane( // top
  1014. transform.m[3] - transform.m[1],
  1015. transform.m[7] - transform.m[5],
  1016. transform.m[11] - transform.m[9],
  1017. transform.m[15] - transform.m[13]));
  1018. frustumPlanes[4].normalize();
  1019. frustumPlanes.push(new BABYLON.Plane( // bottom
  1020. transform.m[3] + transform.m[1],
  1021. transform.m[7] + transform.m[5],
  1022. transform.m[11] + transform.m[9],
  1023. transform.m[15] + transform.m[13]));
  1024. frustumPlanes[5].normalize();
  1025. return frustumPlanes;
  1026. };
  1027. })();