babylon.mesh.ts 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. module BABYLON {
  2. export class Mesh extends Node {
  3. // Statics
  4. public static BILLBOARDMODE_NONE = 0;
  5. public static BILLBOARDMODE_X = 1;
  6. public static BILLBOARDMODE_Y = 2;
  7. public static BILLBOARDMODE_Z = 4;
  8. public static BILLBOARDMODE_ALL = 7;
  9. // Members
  10. public position = new BABYLON.Vector3(0, 0, 0);
  11. public rotation = new BABYLON.Vector3(0, 0, 0);
  12. public rotationQuaternion = null;
  13. public scaling = new BABYLON.Vector3(1, 1, 1);
  14. public delayLoadState = 0; //ANY; BABYLON.Engine.DELAYLOADSTATE_NONE
  15. public material = null;
  16. public isVisible = true;
  17. public isPickable = true;
  18. public visibility = 1.0;
  19. public billboardMode = BABYLON.Mesh.BILLBOARDMODE_NONE;
  20. public checkCollisions = false;
  21. public receiveShadows = false;
  22. public _isDisposed = false;
  23. public onDispose = null;
  24. public skeleton = null;
  25. public renderingGroupId = 0;
  26. public infiniteDistance = false;
  27. public showBoundingBox = false;
  28. public subMeshes; //ANY
  29. public delayLoadingFile: string;
  30. // Cache
  31. private _positions = null;
  32. private _localScaling = BABYLON.Matrix.Zero();
  33. private _localRotation = BABYLON.Matrix.Zero();
  34. private _localTranslation = BABYLON.Matrix.Zero();
  35. private _localBillboard = BABYLON.Matrix.Zero();
  36. private _localPivotScaling = BABYLON.Matrix.Zero();
  37. private _localPivotScalingRotation = BABYLON.Matrix.Zero();
  38. private _localWorld = BABYLON.Matrix.Zero();
  39. private _worldMatrix = BABYLON.Matrix.Zero();
  40. private _rotateYByPI = BABYLON.Matrix.RotationY(Math.PI);
  41. private _collisionsTransformMatrix = BABYLON.Matrix.Zero();
  42. private _collisionsScalingMatrix = BABYLON.Matrix.Zero();
  43. private _absolutePosition = BABYLON.Vector3.Zero();
  44. // Physics
  45. private _physicImpostor = PhysicsEngine.NoImpostor;
  46. private _physicsMass: number;
  47. private _physicsFriction: number;
  48. private _physicRestitution: number;
  49. // Private
  50. private _boundingInfo: BoundingInfo;
  51. private _totalVertices = 0;
  52. private _pivotMatrix = BABYLON.Matrix.Identity();
  53. private _indices = [];
  54. private _renderId = 0;
  55. private _onBeforeRenderCallbacks = [];
  56. private _delayInfo; //ANY
  57. private _vertexStrideSize = 0;
  58. private _animationStarted = false;
  59. private _vertexBuffers;
  60. private _indexBuffer;
  61. private _delayLoadingFunction: (any, Mesh) => void;
  62. //ANY
  63. constructor(name: string, scene) {
  64. super(name, scene);
  65. scene.meshes.push(this);
  66. }
  67. public getBoundingInfo(): BoundingInfo {
  68. return this._boundingInfo;
  69. }
  70. public getWorldMatrix(): Matrix {
  71. if (this._currentRenderId !== this.getScene().getRenderId()) {
  72. this.computeWorldMatrix();
  73. }
  74. return this._worldMatrix;
  75. }
  76. public rotate(axis: Vector3, amount: number, space: Space): void {
  77. if (!this.rotationQuaternion) {
  78. this.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z);
  79. this.rotation = BABYLON.Vector3.Zero();
  80. }
  81. if (!space || space == BABYLON.Space.LOCAL) {
  82. var rotationQuaternion = BABYLON.Quaternion.RotationAxis(axis, amount);
  83. this.rotationQuaternion = this.rotationQuaternion.multiply(rotationQuaternion);
  84. }
  85. else {
  86. if (this.parent) {
  87. var invertParentWorldMatrix = this.parent.getWorldMatrix().clone();
  88. invertParentWorldMatrix.invert();
  89. axis = BABYLON.Vector3.TransformNormal(axis, invertParentWorldMatrix);
  90. }
  91. rotationQuaternion = BABYLON.Quaternion.RotationAxis(axis, amount);
  92. this.rotationQuaternion = rotationQuaternion.multiply(this.rotationQuaternion);
  93. }
  94. }
  95. public translate(axis: Vector3, distance: number, space: Space): void {
  96. var displacementVector = axis.scale(distance);
  97. if (!space || space == BABYLON.Space.LOCAL) {
  98. var tempV3 = this.getPositionExpressedInLocalSpace().add(displacementVector);
  99. this.setPositionWithLocalVector(tempV3);
  100. }
  101. else {
  102. this.setAbsolutePosition(this.getAbsolutePosition().add(displacementVector));
  103. }
  104. }
  105. public getAbsolutePosition(): Vector3 {
  106. this.computeWorldMatrix();
  107. return this._absolutePosition;
  108. }
  109. public setAbsolutePosition(absolutePosition: Vector3): void {
  110. if (!absolutePosition) {
  111. return;
  112. }
  113. var absolutePositionX;
  114. var absolutePositionY;
  115. var absolutePositionZ;
  116. if (absolutePosition.x === undefined) {
  117. if (arguments.length < 3) {
  118. return;
  119. }
  120. absolutePositionX = arguments[0];
  121. absolutePositionY = arguments[1];
  122. absolutePositionZ = arguments[2];
  123. }
  124. else {
  125. absolutePositionX = absolutePosition.x;
  126. absolutePositionY = absolutePosition.y;
  127. absolutePositionZ = absolutePosition.z;
  128. }
  129. if (this.parent) {
  130. var invertParentWorldMatrix = this.parent.getWorldMatrix().clone();
  131. invertParentWorldMatrix.invert();
  132. var worldPosition = new BABYLON.Vector3(absolutePositionX, absolutePositionY, absolutePositionZ);
  133. this.position = BABYLON.Vector3.TransformCoordinates(worldPosition, invertParentWorldMatrix);
  134. } else {
  135. this.position.x = absolutePositionX;
  136. this.position.y = absolutePositionY;
  137. this.position.z = absolutePositionZ;
  138. }
  139. }
  140. public getTotalVertices(): number {
  141. return this._totalVertices;
  142. }
  143. public getVerticesData(kind): number[] {
  144. return this._vertexBuffers[kind].getData();
  145. }
  146. public getVertexBuffer(kind): VertexBuffer {
  147. return this._vertexBuffers[kind];
  148. }
  149. public isVerticesDataPresent(kind: string): boolean {
  150. if (!this._vertexBuffers) {
  151. if (this._delayInfo) {
  152. return this._delayInfo.indexOf(kind) !== -1;
  153. }
  154. return false;
  155. }
  156. return this._vertexBuffers[kind] !== undefined;
  157. }
  158. public getVerticesDataKinds(): string[] {
  159. var result = [];
  160. if (!this._vertexBuffers && this._delayInfo) {
  161. for (var kind in this._delayInfo) {
  162. result.push(kind);
  163. }
  164. } else {
  165. for (kind in this._vertexBuffers) {
  166. result.push(kind);
  167. }
  168. }
  169. return result;
  170. }
  171. public getTotalIndices(): number {
  172. return this._indices.length;
  173. }
  174. public getIndices(): number[] {
  175. return this._indices;
  176. }
  177. public getVertexStrideSize(): number {
  178. return this._vertexStrideSize;
  179. }
  180. public setPivotMatrix(matrix: Matrix): void {
  181. this._pivotMatrix = matrix;
  182. this._cache.pivotMatrixUpdated = true;
  183. }
  184. public getPivotMatrix(): Matrix {
  185. return this._pivotMatrix;
  186. }
  187. public _isSynchronized(): boolean {
  188. if (this.billboardMode !== Mesh.BILLBOARDMODE_NONE)
  189. return false;
  190. if (this._cache.pivotMatrixUpdated) {
  191. return false;
  192. }
  193. if (this.infiniteDistance) {
  194. return false;
  195. }
  196. if (!this._cache.position.equals(this.position))
  197. return false;
  198. if (this.rotationQuaternion) {
  199. if (!this._cache.rotationQuaternion.equals(this.rotationQuaternion))
  200. return false;
  201. } else {
  202. if (!this._cache.rotation.equals(this.rotation))
  203. return false;
  204. }
  205. if (!this._cache.scaling.equals(this.scaling))
  206. return false;
  207. return true;
  208. }
  209. public isReady(): boolean {
  210. return this._isReady;
  211. }
  212. public isAnimated(): boolean {
  213. return this._animationStarted;
  214. }
  215. public isDisposed(): boolean {
  216. return this._isDisposed;
  217. }
  218. // Methods
  219. public _initCache() {
  220. super._initCache();
  221. this._cache.localMatrixUpdated = false;
  222. this._cache.position = BABYLON.Vector3.Zero();
  223. this._cache.scaling = BABYLON.Vector3.Zero();
  224. this._cache.rotation = BABYLON.Vector3.Zero();
  225. this._cache.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 0);
  226. }
  227. public markAsDirty(property: string): void {
  228. if (property === "rotation") {
  229. this.rotationQuaternion = null;
  230. }
  231. this._currentRenderId = -1;
  232. }
  233. public refreshBoundingInfo(): void {
  234. var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  235. if (!data) {
  236. return;
  237. }
  238. var extend = BABYLON.Tools.ExtractMinAndMax(data, 0, this._totalVertices);
  239. this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  240. for (var index = 0; index < this.subMeshes.length; index++) {
  241. this.subMeshes[index].refreshBoundingInfo();
  242. }
  243. this._updateBoundingInfo();
  244. }
  245. public _updateBoundingInfo(): void {
  246. this._boundingInfo = this._boundingInfo || new BABYLON.BoundingInfo(this._absolutePosition, this._absolutePosition);
  247. this._boundingInfo._update(this._worldMatrix);
  248. if (!this.subMeshes) {
  249. return;
  250. }
  251. for (var subIndex = 0; subIndex < this.subMeshes.length; subIndex++) {
  252. var subMesh = this.subMeshes[subIndex];
  253. subMesh.updateBoundingInfo(this._worldMatrix);
  254. }
  255. }
  256. public computeWorldMatrix(force?: boolean): Matrix {
  257. if (!force && (this._currentRenderId == this.getScene().getRenderId() || this.isSynchronized(true))) {
  258. return this._worldMatrix;
  259. }
  260. this._cache.position.copyFrom(this.position);
  261. this._cache.scaling.copyFrom(this.scaling);
  262. this._cache.pivotMatrixUpdated = false;
  263. this._currentRenderId = this.getScene().getRenderId();
  264. // Scaling
  265. BABYLON.Matrix.ScalingToRef(this.scaling.x, this.scaling.y, this.scaling.z, this._localScaling);
  266. // Rotation
  267. if (this.rotationQuaternion) {
  268. this.rotationQuaternion.toRotationMatrix(this._localRotation);
  269. this._cache.rotationQuaternion.copyFrom(this.rotationQuaternion);
  270. } else {
  271. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._localRotation);
  272. this._cache.rotation.copyFrom(this.rotation);
  273. }
  274. // Translation
  275. if (this.infiniteDistance && !this.parent) {
  276. var camera = this.getScene().activeCamera;
  277. var cameraWorldMatrix = camera.getWorldMatrix();
  278. var cameraGlobalPosition = new BABYLON.Vector3(cameraWorldMatrix.m[12], cameraWorldMatrix.m[13], cameraWorldMatrix.m[14]);
  279. BABYLON.Matrix.TranslationToRef(this.position.x + cameraGlobalPosition.x, this.position.y + cameraGlobalPosition.y, this.position.z + cameraGlobalPosition.z, this._localTranslation);
  280. } else {
  281. BABYLON.Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._localTranslation);
  282. }
  283. // Composing transformations
  284. this._pivotMatrix.multiplyToRef(this._localScaling, this._localPivotScaling);
  285. this._localPivotScaling.multiplyToRef(this._localRotation, this._localPivotScalingRotation);
  286. // Billboarding
  287. if (this.billboardMode !== Mesh.BILLBOARDMODE_NONE) {
  288. var localPosition = this.position.clone();
  289. var zero = this.getScene().activeCamera.position.clone();
  290. if (this.parent && (<any>this.parent).position) {
  291. localPosition.addInPlace((<any>this.parent).position);
  292. BABYLON.Matrix.TranslationToRef(localPosition.x, localPosition.y, localPosition.z, this._localTranslation);
  293. }
  294. if ((this.billboardMode & Mesh.BILLBOARDMODE_ALL) === Mesh.BILLBOARDMODE_ALL) {
  295. zero = this.getScene().activeCamera.position;
  296. } else {
  297. if (this.billboardMode & BABYLON.Mesh.BILLBOARDMODE_X)
  298. zero.x = localPosition.x + 0.001;//ANY:BABYLON.Engine.epsilon
  299. if (this.billboardMode & BABYLON.Mesh.BILLBOARDMODE_Y)
  300. zero.y = localPosition.y + 0.001;
  301. if (this.billboardMode & BABYLON.Mesh.BILLBOARDMODE_Z)
  302. zero.z = localPosition.z + 0.001;
  303. }
  304. BABYLON.Matrix.LookAtLHToRef(localPosition, zero, BABYLON.Vector3.Up(), this._localBillboard);
  305. this._localBillboard.m[12] = this._localBillboard.m[13] = this._localBillboard.m[14] = 0;
  306. this._localBillboard.invert();
  307. this._localPivotScalingRotation.multiplyToRef(this._localBillboard, this._localWorld);
  308. this._rotateYByPI.multiplyToRef(this._localWorld, this._localPivotScalingRotation);
  309. }
  310. // Local world
  311. this._localPivotScalingRotation.multiplyToRef(this._localTranslation, this._localWorld);
  312. // Parent
  313. if (this.parent && this.parent.getWorldMatrix && this.billboardMode === BABYLON.Mesh.BILLBOARDMODE_NONE) {
  314. this._localWorld.multiplyToRef(this.parent.getWorldMatrix(), this._worldMatrix);
  315. } else {
  316. this._worldMatrix.copyFrom(this._localWorld);
  317. }
  318. // Bounding info
  319. this._updateBoundingInfo();
  320. // Absolute position
  321. this._absolutePosition.copyFromFloats(this._worldMatrix.m[12], this._worldMatrix.m[13], this._worldMatrix.m[14]);
  322. return this._worldMatrix;
  323. }
  324. public _createGlobalSubMesh(): SubMesh {
  325. if (!this._totalVertices || !this._indices) {
  326. return null;
  327. }
  328. this.subMeshes = [];
  329. return new BABYLON.SubMesh(0, 0, this._totalVertices, 0, this._indices.length, this);
  330. }
  331. public subdivide(count: number): void {
  332. if (count < 1) {
  333. return;
  334. }
  335. var subdivisionSize = this._indices.length / count;
  336. var offset = 0;
  337. this.subMeshes = [];
  338. for (var index = 0; index < count; index++) {
  339. BABYLON.SubMesh.CreateFromIndices(0, offset, Math.min(subdivisionSize, this._indices.length - offset), this);
  340. offset += subdivisionSize;
  341. }
  342. }
  343. public setVerticesData(data: number[], kind: string, updatable?: boolean): void {
  344. if (!this._vertexBuffers) {
  345. this._vertexBuffers = {};
  346. }
  347. if (this._vertexBuffers[kind]) {
  348. this._vertexBuffers[kind].dispose();
  349. }
  350. this._vertexBuffers[kind] = new BABYLON.VertexBuffer(this, data, kind, updatable);
  351. if (kind === BABYLON.VertexBuffer.PositionKind) {
  352. this._resetPointsArrayCache();
  353. var stride = this._vertexBuffers[kind].getStrideSize();
  354. this._totalVertices = data.length / stride;
  355. var extend = BABYLON.Tools.ExtractMinAndMax(data, 0, this._totalVertices);
  356. this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  357. this._createGlobalSubMesh();
  358. }
  359. }
  360. public updateVerticesData(kind: string, data: number[], updateExtends?: boolean): void {
  361. if (this._vertexBuffers[kind]) {
  362. this._vertexBuffers[kind].update(data);
  363. if (kind === BABYLON.VertexBuffer.PositionKind) {
  364. this._resetPointsArrayCache();
  365. if (updateExtends) {
  366. var stride = this._vertexBuffers[kind].getStrideSize();
  367. this._totalVertices = data.length / stride;
  368. var extend = BABYLON.Tools.ExtractMinAndMax(data, 0, this._totalVertices);
  369. this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  370. }
  371. }
  372. }
  373. }
  374. public setIndices(indices: number[]): void {
  375. if (this._indexBuffer) {
  376. this.getScene().getEngine()._releaseBuffer(this._indexBuffer);
  377. }
  378. this._indexBuffer = this.getScene().getEngine().createIndexBuffer(indices);
  379. this._indices = indices;
  380. this._createGlobalSubMesh();
  381. }
  382. // ANY
  383. public bindAndDraw(subMesh: SubMesh, effect, wireframe?: boolean): void {
  384. var engine = this.getScene().getEngine();
  385. // Wireframe
  386. var indexToBind = this._indexBuffer;
  387. var useTriangles = true;
  388. if (wireframe) {
  389. indexToBind = subMesh.getLinesIndexBuffer(this._indices, engine);
  390. useTriangles = false;
  391. }
  392. // VBOs
  393. engine.bindMultiBuffers(this._vertexBuffers, indexToBind, effect);
  394. // Draw order
  395. engine.draw(useTriangles, useTriangles ? subMesh.indexStart : 0, useTriangles ? subMesh.indexCount : subMesh.linesIndexCount);
  396. }
  397. public registerBeforeRender(func: () => void): void {
  398. this._onBeforeRenderCallbacks.push(func);
  399. }
  400. public unregisterBeforeRender(func: () => void): void {
  401. var index = this._onBeforeRenderCallbacks.indexOf(func);
  402. if (index > -1) {
  403. this._onBeforeRenderCallbacks.splice(index, 1);
  404. }
  405. }
  406. public render(subMesh: SubMesh): void {
  407. if (!this._vertexBuffers || !this._indexBuffer) {
  408. return;
  409. }
  410. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  411. this._onBeforeRenderCallbacks[callbackIndex]();
  412. }
  413. // World
  414. var world = this.getWorldMatrix();
  415. // Material
  416. var effectiveMaterial = subMesh.getMaterial();
  417. if (!effectiveMaterial || !effectiveMaterial.isReady(this)) {
  418. return;
  419. }
  420. effectiveMaterial._preBind();
  421. effectiveMaterial.bind(world, this);
  422. // Bind and draw
  423. var engine = this.getScene().getEngine();
  424. this.bindAndDraw(subMesh, effectiveMaterial.getEffect(), engine.forceWireframe || effectiveMaterial.wireframe);
  425. // Unbind
  426. effectiveMaterial.unbind();
  427. }
  428. //ANY
  429. public getEmittedParticleSystems() {
  430. var results = [];
  431. for (var index = 0; index < this.getScene().particleSystems.length; index++) {
  432. var particleSystem = this.getScene().particleSystems[index];
  433. if (particleSystem.emitter === this) {
  434. results.push(particleSystem);
  435. }
  436. }
  437. return results;
  438. }
  439. //ANY
  440. public getHierarchyEmittedParticleSystems() {
  441. var results = [];
  442. var descendants = this.getDescendants();
  443. descendants.push(this);
  444. for (var index = 0; index < this.getScene().particleSystems.length; index++) {
  445. var particleSystem = this.getScene().particleSystems[index];
  446. if (descendants.indexOf(particleSystem.emitter) !== -1) {
  447. results.push(particleSystem);
  448. }
  449. }
  450. return results;
  451. }
  452. public getChildren(): Node[] {
  453. var results = [];
  454. for (var index = 0; index < this.getScene().meshes.length; index++) {
  455. var mesh = this.getScene().meshes[index];
  456. if (mesh.parent == this) {
  457. results.push(mesh);
  458. }
  459. }
  460. return results;
  461. }
  462. public isInFrustum(frustumPlanes: Plane[]): boolean {
  463. if (this.delayLoadState === 2) { //ANY BABYLON.Engine.DELAYLOADSTATE_LOADING
  464. return false;
  465. }
  466. var result = this._boundingInfo.isInFrustum(frustumPlanes);
  467. if (result && this.delayLoadState === 4) { //ANY BABYLON.Engine.DELAYLOADSTATE_NOTLOADED
  468. this.delayLoadState = 2; //ANY BABYLON.Engine.DELAYLOADSTATE_LOADING
  469. this.getScene()._addPendingData(this);
  470. Tools.LoadFile(this.delayLoadingFile, data => {
  471. this._delayLoadingFunction(JSON.parse(data), this);
  472. this.delayLoadState = 1; //ANY BABYLON.Engine.DELAYLOADSTATE_LOADED;
  473. this.getScene()._removePendingData(this);
  474. }, () => { }, this.getScene().database);
  475. }
  476. return result;
  477. }
  478. public setMaterialByID(id: string): void {
  479. var materials = this.getScene().materials;
  480. for (var index = 0; index < materials.length; index++) {
  481. if (materials[index].id == id) {
  482. this.material = materials[index];
  483. return;
  484. }
  485. }
  486. // Multi
  487. var multiMaterials = this.getScene().multiMaterials;
  488. for (index = 0; index < multiMaterials.length; index++) {
  489. if (multiMaterials[index].id == id) {
  490. this.material = multiMaterials[index];
  491. return;
  492. }
  493. }
  494. }
  495. public getAnimatables(): IAnimatable[] {
  496. var results = [];
  497. if (this.material) {
  498. results.push(this.material);
  499. }
  500. return results;
  501. }
  502. // Geometry
  503. public setPositionWithLocalVector(vector3: Vector3): void {
  504. this.computeWorldMatrix();
  505. this.position = BABYLON.Vector3.TransformNormal(vector3, this._localWorld);
  506. }
  507. public getPositionExpressedInLocalSpace(): Vector3 {
  508. this.computeWorldMatrix();
  509. var invLocalWorldMatrix = this._localWorld.clone();
  510. invLocalWorldMatrix.invert();
  511. return BABYLON.Vector3.TransformNormal(this.position, invLocalWorldMatrix);
  512. }
  513. public locallyTranslate(vector3: Vector3): void {
  514. this.computeWorldMatrix();
  515. this.position = BABYLON.Vector3.TransformCoordinates(vector3, this._localWorld);
  516. }
  517. public bakeTransformIntoVertices(transform: Matrix): void {
  518. // Position
  519. if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  520. return;
  521. }
  522. this._resetPointsArrayCache();
  523. var data = this._vertexBuffers[BABYLON.VertexBuffer.PositionKind].getData();
  524. var temp = [];
  525. for (var index = 0; index < data.length; index += 3) {
  526. BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.FromArray(data, index), transform).toArray(temp, index);
  527. }
  528. this.setVerticesData(temp, BABYLON.VertexBuffer.PositionKind, this._vertexBuffers[BABYLON.VertexBuffer.PositionKind].isUpdatable());
  529. // Normals
  530. if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  531. return;
  532. }
  533. data = this._vertexBuffers[BABYLON.VertexBuffer.NormalKind].getData();
  534. for (index = 0; index < data.length; index += 3) {
  535. BABYLON.Vector3.TransformNormal(BABYLON.Vector3.FromArray(data, index), transform).toArray(temp, index);
  536. }
  537. this.setVerticesData(temp, BABYLON.VertexBuffer.NormalKind, this._vertexBuffers[BABYLON.VertexBuffer.NormalKind].isUpdatable());
  538. }
  539. public lookAt(targetPoint: Vector3, yawCor: number, pitchCor: number, rollCor: number): void {
  540. /// <summary>Orients a mesh towards a target point. Mesh must be drawn facing user.</summary>
  541. /// <param name="targetPoint" type="BABYLON.Vector3">The position (must be in same space as current mesh) to look at</param>
  542. /// <param name="yawCor" type="Number">optional yaw (y-axis) correction in radians</param>
  543. /// <param name="pitchCor" type="Number">optional pitch (x-axis) correction in radians</param>
  544. /// <param name="rollCor" type="Number">optional roll (z-axis) correction in radians</param>
  545. /// <returns>Mesh oriented towards targetMesh</returns>
  546. yawCor = yawCor || 0; // default to zero if undefined
  547. pitchCor = pitchCor || 0;
  548. rollCor = rollCor || 0;
  549. var dv = targetPoint.subtract(this.position);
  550. var yaw = -Math.atan2(dv.z, dv.x) - Math.PI / 2;
  551. var len = Math.sqrt(dv.x * dv.x + dv.z * dv.z);
  552. var pitch = Math.atan2(dv.y, len);
  553. this.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(yaw + yawCor, pitch + pitchCor, rollCor);
  554. }
  555. // Cache
  556. public _resetPointsArrayCache(): void {
  557. this._positions = null;
  558. }
  559. public _generatePointsArray(): void {
  560. if (this._positions)
  561. return;
  562. this._positions = [];
  563. var data = this._vertexBuffers[BABYLON.VertexBuffer.PositionKind].getData();
  564. for (var index = 0; index < data.length; index += 3) {
  565. this._positions.push(BABYLON.Vector3.FromArray(data, index));
  566. }
  567. }
  568. // Collisions
  569. public _collideForSubMesh(subMesh: SubMesh, transformMatrix: Matrix, collider: Collider): void {
  570. this._generatePointsArray();
  571. // Transformation
  572. if (!subMesh._lastColliderWorldVertices || !subMesh._lastColliderTransformMatrix.equals(transformMatrix)) {
  573. subMesh._lastColliderTransformMatrix = transformMatrix.clone();
  574. subMesh._lastColliderWorldVertices = [];
  575. subMesh._trianglePlanes = [];
  576. var start = subMesh.verticesStart;
  577. var end = (subMesh.verticesStart + subMesh.verticesCount);
  578. for (var i = start; i < end; i++) {
  579. subMesh._lastColliderWorldVertices.push(BABYLON.Vector3.TransformCoordinates(this._positions[i], transformMatrix));
  580. }
  581. }
  582. // Collide
  583. collider._collide(subMesh, subMesh._lastColliderWorldVertices, this._indices, subMesh.indexStart, subMesh.indexStart + subMesh.indexCount, subMesh.verticesStart);
  584. }
  585. public _processCollisionsForSubModels(collider: Collider, transformMatrix: Matrix): void {
  586. for (var index = 0; index < this.subMeshes.length; index++) {
  587. var subMesh = this.subMeshes[index];
  588. // Bounding test
  589. if (this.subMeshes.length > 1 && !subMesh._checkCollision(collider))
  590. continue;
  591. this._collideForSubMesh(subMesh, transformMatrix, collider);
  592. }
  593. }
  594. public _checkCollision(collider: Collider): void {
  595. // Bounding box test
  596. if (!this._boundingInfo._checkCollision(collider))
  597. return;
  598. // Transformation matrix
  599. BABYLON.Matrix.ScalingToRef(1.0 / collider.radius.x, 1.0 / collider.radius.y, 1.0 / collider.radius.z, this._collisionsScalingMatrix);
  600. this._worldMatrix.multiplyToRef(this._collisionsScalingMatrix, this._collisionsTransformMatrix);
  601. this._processCollisionsForSubModels(collider, this._collisionsTransformMatrix);
  602. }
  603. public intersectsMesh(mesh: Mesh, precise?: boolean): boolean {
  604. if (!this._boundingInfo || !mesh._boundingInfo) {
  605. return false;
  606. }
  607. return this._boundingInfo.intersects(mesh._boundingInfo, precise);
  608. }
  609. public intersectsPoint(point: Vector3): boolean {
  610. if (!this._boundingInfo) {
  611. return false;
  612. }
  613. return this._boundingInfo.intersectsPoint(point);
  614. }
  615. // Picking
  616. public intersects(ray: Ray, fastCheck?: boolean): PickingInfo {
  617. var pickingInfo = new BABYLON.PickingInfo();
  618. if (!this._boundingInfo || !ray.intersectsSphere(this._boundingInfo.boundingSphere) || !ray.intersectsBox(this._boundingInfo.boundingBox)) {
  619. return pickingInfo;
  620. }
  621. this._generatePointsArray();
  622. var intersectInfo = null;
  623. for (var index = 0; index < this.subMeshes.length; index++) {
  624. var subMesh = this.subMeshes[index];
  625. // Bounding test
  626. if (this.subMeshes.length > 1 && !subMesh.canIntersects(ray))
  627. continue;
  628. var currentIntersectInfo = subMesh.intersects(ray, this._positions, this._indices, fastCheck);
  629. if (currentIntersectInfo) {
  630. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  631. intersectInfo = currentIntersectInfo;
  632. if (fastCheck) {
  633. break;
  634. }
  635. }
  636. }
  637. }
  638. if (intersectInfo) {
  639. // Get picked point
  640. var world = this.getWorldMatrix();
  641. var worldOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, world);
  642. var direction = ray.direction.clone();
  643. direction.normalize();
  644. direction = direction.scale(intersectInfo.distance);
  645. var worldDirection = BABYLON.Vector3.TransformNormal(direction, world);
  646. var pickedPoint = worldOrigin.add(worldDirection);
  647. // Return result
  648. pickingInfo.hit = true;
  649. pickingInfo.distance = BABYLON.Vector3.Distance(worldOrigin, pickedPoint);
  650. pickingInfo.pickedPoint = pickedPoint;
  651. pickingInfo.pickedMesh = this;
  652. pickingInfo.bu = intersectInfo.bu;
  653. pickingInfo.bv = intersectInfo.bv;
  654. pickingInfo.faceId = intersectInfo.faceId;
  655. return pickingInfo;
  656. }
  657. return pickingInfo;
  658. }
  659. // Clone
  660. public clone(name: string, newParent: Node, doNotCloneChildren?: boolean): Mesh {
  661. var result = new BABYLON.Mesh(name, this.getScene());
  662. // Buffers
  663. result._vertexBuffers = this._vertexBuffers;
  664. for (var kind in result._vertexBuffers) {
  665. result._vertexBuffers[kind]._buffer.references++;
  666. }
  667. result._indexBuffer = this._indexBuffer;
  668. this._indexBuffer.references++;
  669. // Deep copy
  670. BABYLON.Tools.DeepCopy(this, result, ["name", "material", "skeleton"], ["_indices", "_totalVertices"]);
  671. // Bounding info
  672. var extend = BABYLON.Tools.ExtractMinAndMax(this.getVerticesData(BABYLON.VertexBuffer.PositionKind), 0, this._totalVertices);
  673. result._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  674. // Material
  675. result.material = this.material;
  676. // Parent
  677. if (newParent) {
  678. result.parent = newParent;
  679. }
  680. if (!doNotCloneChildren) {
  681. // Children
  682. for (var index = 0; index < this.getScene().meshes.length; index++) {
  683. var mesh = this.getScene().meshes[index];
  684. if (mesh.parent == this) {
  685. mesh.clone(mesh.name, result);
  686. }
  687. }
  688. }
  689. // Particles
  690. for (index = 0; index < this.getScene().particleSystems.length; index++) {
  691. var system = this.getScene().particleSystems[index];
  692. if (system.emitter == this) {
  693. system.clone(system.name, result);
  694. }
  695. }
  696. result.computeWorldMatrix(true);
  697. return result;
  698. }
  699. // Dispose
  700. public dispose(doNotRecurse?: boolean): void {
  701. if (this._vertexBuffers) {
  702. for (var vbKind in this._vertexBuffers) {
  703. this._vertexBuffers[vbKind].dispose();
  704. }
  705. this._vertexBuffers = null;
  706. }
  707. if (this._indexBuffer) {
  708. this.getScene().getEngine()._releaseBuffer(this._indexBuffer);
  709. this._indexBuffer = null;
  710. }
  711. // Physics
  712. if (this.getPhysicsImpostor() != PhysicsEngine.NoImpostor) {
  713. this.setPhysicsState({ impostor: PhysicsEngine.NoImpostor });
  714. }
  715. // Remove from scene
  716. var index = this.getScene().meshes.indexOf(this);
  717. this.getScene().meshes.splice(index, 1);
  718. if (!doNotRecurse) {
  719. // Particles
  720. for (index = 0; index < this.getScene().particleSystems.length; index++) {
  721. if (this.getScene().particleSystems[index].emitter == this) {
  722. this.getScene().particleSystems[index].dispose();
  723. index--;
  724. }
  725. }
  726. // Children
  727. var objects = this.getScene().meshes.slice(0);
  728. for (index = 0; index < objects.length; index++) {
  729. if (objects[index].parent == this) {
  730. objects[index].dispose();
  731. }
  732. }
  733. } else {
  734. for (index = 0; index < this.getScene().meshes.length; index++) {
  735. var obj = this.getScene().meshes[index];
  736. if (obj.parent === this) {
  737. obj.parent = null;
  738. obj.computeWorldMatrix(true);
  739. }
  740. }
  741. }
  742. this._isDisposed = true;
  743. // Callback
  744. if (this.onDispose) {
  745. this.onDispose();
  746. }
  747. }
  748. // Physics
  749. public setPhysicsState(options): void {
  750. if (!this.getScene()._physicsEngine) {
  751. return;
  752. }
  753. options.impostor = options.impostor || PhysicsEngine.NoImpostor;
  754. options.mass = options.mass || 0;
  755. options.friction = options.friction || 0.2;
  756. options.restitution = options.restitution || 0.9;
  757. this._physicImpostor = options.impostor;
  758. this._physicsMass = options.mass;
  759. this._physicsFriction = options.friction;
  760. this._physicRestitution = options.restitution;
  761. if (options.impostor === BABYLON.PhysicsEngine.NoImpostor) {
  762. this.getScene()._physicsEngine._unregisterMesh(this);
  763. return;
  764. }
  765. this.getScene()._physicsEngine._registerMesh(this, options);
  766. }
  767. public getPhysicsImpostor(): number {
  768. if (!this._physicImpostor) {
  769. return BABYLON.PhysicsEngine.NoImpostor;
  770. }
  771. return this._physicImpostor;
  772. }
  773. public getPhysicsMass(): number {
  774. if (!this._physicsMass) {
  775. return 0;
  776. }
  777. return this._physicsMass;
  778. }
  779. public getPhysicsFriction(): number {
  780. if (!this._physicsFriction) {
  781. return 0;
  782. }
  783. return this._physicsFriction;
  784. }
  785. public getPhysicsRestitution(): number {
  786. if (!this._physicRestitution) {
  787. return 0;
  788. }
  789. return this._physicRestitution;
  790. }
  791. public applyImpulse(force: Vector3, contactPoint: Vector3): void {
  792. if (!this._physicImpostor) {
  793. return;
  794. }
  795. this.getScene()._physicsEngine._applyImpulse(this, force, contactPoint);
  796. }
  797. public setPhysicsLinkWith(otherMesh: Mesh, pivot1: Vector3, pivot2: Vector3): void {
  798. if (!this._physicImpostor) {
  799. return;
  800. }
  801. this.getScene()._physicsEngine._createLink(this, otherMesh, pivot1, pivot2);
  802. }
  803. // Geometric tools
  804. public convertToFlatShadedMesh(): void {
  805. /// <summary>Update normals and vertices to get a flat shading rendering.</summary>
  806. /// <summary>Warning: This may imply adding vertices to the mesh in order to get exactly 3 vertices per face</summary>
  807. var kinds = this.getVerticesDataKinds();
  808. var vbs = [];
  809. var data = [];
  810. var newdata = [];
  811. var updatableNormals = false;
  812. for (var kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  813. var kind = kinds[kindIndex];
  814. if (kind === BABYLON.VertexBuffer.NormalKind) {
  815. updatableNormals = this.getVertexBuffer(kind).isUpdatable();
  816. kinds.splice(kindIndex, 1);
  817. kindIndex--;
  818. continue;
  819. }
  820. vbs[kind] = this.getVertexBuffer(kind);
  821. data[kind] = vbs[kind].getData();
  822. newdata[kind] = [];
  823. }
  824. // Save previous submeshes
  825. var previousSubmeshes = this.subMeshes.slice(0);
  826. var indices = this.getIndices();
  827. // Generating unique vertices per face
  828. for (index = 0; index < indices.length; index++) {
  829. var vertexIndex = indices[index];
  830. for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  831. kind = kinds[kindIndex];
  832. var stride = vbs[kind].getStrideSize();
  833. for (var offset = 0; offset < stride; offset++) {
  834. newdata[kind].push(data[kind][vertexIndex * stride + offset]);
  835. }
  836. }
  837. }
  838. // Updating faces & normal
  839. var normals = [];
  840. var positions = newdata[BABYLON.VertexBuffer.PositionKind];
  841. for (var index = 0; index < indices.length; index += 3) {
  842. indices[index] = index;
  843. indices[index + 1] = index + 1;
  844. indices[index + 2] = index + 2;
  845. var p1 = BABYLON.Vector3.FromArray(positions, index * 3);
  846. var p2 = BABYLON.Vector3.FromArray(positions, (index + 1) * 3);
  847. var p3 = BABYLON.Vector3.FromArray(positions, (index + 2) * 3);
  848. var p1p2 = p1.subtract(p2);
  849. var p3p2 = p3.subtract(p2);
  850. var normal = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
  851. // Store same normals for every vertex
  852. for (var localIndex = 0; localIndex < 3; localIndex++) {
  853. normals.push(normal.x);
  854. normals.push(normal.y);
  855. normals.push(normal.z);
  856. }
  857. }
  858. this.setIndices(indices);
  859. this.setVerticesData(normals, BABYLON.VertexBuffer.NormalKind, updatableNormals);
  860. // Updating vertex buffers
  861. for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  862. kind = kinds[kindIndex];
  863. this.setVerticesData(newdata[kind], kind, vbs[kind].isUpdatable());
  864. }
  865. // Updating submeshes
  866. this.subMeshes = [];
  867. for (var submeshIndex = 0; submeshIndex < previousSubmeshes.length; submeshIndex++) {
  868. var previousOne = previousSubmeshes[submeshIndex];
  869. var subMesh = new BABYLON.SubMesh(previousOne.materialIndex, previousOne.indexStart, previousOne.indexCount, previousOne.indexStart, previousOne.indexCount, this);
  870. }
  871. }
  872. // Statics
  873. //ANY
  874. public static CreateBox(name: string, size: number, scene, updatable?: boolean): Mesh {
  875. var box = new BABYLON.Mesh(name, scene);
  876. var vertexData = BABYLON.VertexData.CreateBox(size);
  877. vertexData.applyToMesh(box, updatable);
  878. return box;
  879. }
  880. //ANY
  881. public static CreateSphere(name: string, segments: number, diameter: number, scene, updatable?: boolean): Mesh {
  882. var sphere = new BABYLON.Mesh(name, scene);
  883. var vertexData = BABYLON.VertexData.CreateSphere(segments, diameter);
  884. vertexData.applyToMesh(sphere, updatable);
  885. return sphere;
  886. }
  887. // Cylinder and cone (Code inspired by SharpDX.org)
  888. //ANY
  889. public static CreateCylinder(name: string, height: number, diameterTop: number, diameterBottom: number, tessellation: number, scene, updatable?: boolean): Mesh {
  890. var cylinder = new BABYLON.Mesh(name, scene);
  891. var vertexData = BABYLON.VertexData.CreateCylinder(height, diameterTop, diameterBottom, tessellation);
  892. vertexData.applyToMesh(cylinder, updatable);
  893. return cylinder;
  894. }
  895. // Torus (Code from SharpDX.org)
  896. //ANY
  897. public static CreateTorus(name: string, diameter, thickness: number, tessellation: number, scene, updatable?: boolean): Mesh {
  898. var torus = new BABYLON.Mesh(name, scene);
  899. var vertexData = BABYLON.VertexData.CreateTorus(diameter, thickness, tessellation);
  900. vertexData.applyToMesh(torus, updatable);
  901. return torus;
  902. }
  903. //ANY
  904. public static CreateTorusKnot(name: string, radius: number, tube: number, radialSegments: number, tubularSegments: number, p: number, q: number, scene, updatable?: boolean): Mesh {
  905. var torusKnot = new BABYLON.Mesh(name, scene);
  906. var vertexData = BABYLON.VertexData.CreateTorusKnot(radius, tube, radialSegments, tubularSegments, p, q);
  907. vertexData.applyToMesh(torusKnot, updatable);
  908. return torusKnot;
  909. }
  910. // Plane & ground
  911. //ANY
  912. public static CreatePlane(name: string, size: number, scene, updatable?: boolean): Mesh {
  913. var plane = new BABYLON.Mesh(name, scene);
  914. var vertexData = BABYLON.VertexData.CreatePlane(size);
  915. vertexData.applyToMesh(plane, updatable);
  916. return plane;
  917. }
  918. //ANY
  919. public static CreateGround(name: string, width: number, height: number, subdivisions: number, scene, updatable?: boolean): Mesh {
  920. var ground = new BABYLON.Mesh(name, scene);
  921. var vertexData = BABYLON.VertexData.CreateGround(width, height, subdivisions);
  922. vertexData.applyToMesh(ground, updatable);
  923. return ground;
  924. }
  925. //ANY
  926. public static CreateGroundFromHeightMap(name: string, url: string, width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, scene, updatable?: boolean): Mesh {
  927. var ground = new BABYLON.Mesh(name, scene);
  928. var onload = img => {
  929. var indices = [];
  930. var positions = [];
  931. var normals = [];
  932. var uvs = [];
  933. var row, col;
  934. // Getting height map data
  935. var canvas = document.createElement("canvas");
  936. var context = canvas.getContext("2d");
  937. var heightMapWidth = img.width;
  938. var heightMapHeight = img.height;
  939. canvas.width = heightMapWidth;
  940. canvas.height = heightMapHeight;
  941. context.drawImage(img, 0, 0);
  942. var buffer = context.getImageData(0, 0, heightMapWidth, heightMapHeight).data;
  943. // Vertices
  944. for (row = 0; row <= subdivisions; row++) {
  945. for (col = 0; col <= subdivisions; col++) {
  946. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  947. // Compute height
  948. var heightMapX = (((position.x + width / 2) / width) * (heightMapWidth - 1)) | 0;
  949. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (heightMapHeight - 1)) | 0;
  950. var pos = (heightMapX + heightMapY * heightMapWidth) * 4;
  951. var r = buffer[pos] / 255.0;
  952. var g = buffer[pos + 1] / 255.0;
  953. var b = buffer[pos + 2] / 255.0;
  954. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  955. position.y = minHeight + (maxHeight - minHeight) * gradient;
  956. // Add vertex
  957. positions.push(position.x, position.y, position.z);
  958. normals.push(0, 0, 0);
  959. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  960. }
  961. }
  962. // Indices
  963. for (row = 0; row < subdivisions; row++) {
  964. for (col = 0; col < subdivisions; col++) {
  965. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  966. indices.push(col + 1 + row * (subdivisions + 1));
  967. indices.push(col + row * (subdivisions + 1));
  968. indices.push(col + (row + 1) * (subdivisions + 1));
  969. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  970. indices.push(col + row * (subdivisions + 1));
  971. }
  972. }
  973. // Normals
  974. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  975. // Transfer
  976. ground.setVerticesData(positions, BABYLON.VertexBuffer.PositionKind, updatable);
  977. ground.setVerticesData(normals, BABYLON.VertexBuffer.NormalKind, updatable);
  978. ground.setVerticesData(uvs, BABYLON.VertexBuffer.UVKind, updatable);
  979. ground.setIndices(indices);
  980. ground._isReady = true;
  981. };
  982. Tools.LoadImage(url, onload, () => {}, scene.database);
  983. ground._isReady = false;
  984. return ground;
  985. }
  986. // Tools
  987. public static MinMax(meshes: Mesh[]): {min: Vector3; max: Vector3} {
  988. var minVector;
  989. var maxVector;
  990. for (var i in meshes) {
  991. var mesh = meshes[i];
  992. var boundingBox = mesh.getBoundingInfo().boundingBox;
  993. if (!minVector) {
  994. minVector = boundingBox.minimumWorld;
  995. maxVector = boundingBox.maximumWorld;
  996. continue;
  997. }
  998. minVector.MinimizeInPlace(boundingBox.minimumWorld);
  999. maxVector.MaximizeInPlace(boundingBox.maximumWorld);
  1000. }
  1001. return {
  1002. min: minVector,
  1003. max: maxVector
  1004. };
  1005. }
  1006. public static Center(meshesOrMinMaxVector): Vector3 {
  1007. var minMaxVector = meshesOrMinMaxVector.min !== undefined ? meshesOrMinMaxVector : BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
  1008. return BABYLON.Vector3.Center(minMaxVector.min, minMaxVector.max);
  1009. }
  1010. }
  1011. }