babylon.abstractMesh.ts 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  1. module BABYLON {
  2. export class AbstractMesh extends Node implements IDisposable, ICullable, IGetSetVerticesData {
  3. // Statics
  4. private static _BILLBOARDMODE_NONE = 0;
  5. private static _BILLBOARDMODE_X = 1;
  6. private static _BILLBOARDMODE_Y = 2;
  7. private static _BILLBOARDMODE_Z = 4;
  8. private static _BILLBOARDMODE_ALL = 7;
  9. public static OCCLUSION_TYPE_NONE = 0;
  10. public static OCCLUSION_TYPE_OPTIMISTIC = 1;
  11. public static OCCLUSION_TYPE_STRICT = 2;
  12. public static OCCLUSION_ALGORITHM_TYPE_ACCURATE = 0;
  13. public static OCCLUSION_ALGORITHM_TYPE_CONSERVATIVE = 1;
  14. public static get BILLBOARDMODE_NONE(): number {
  15. return AbstractMesh._BILLBOARDMODE_NONE;
  16. }
  17. public static get BILLBOARDMODE_X(): number {
  18. return AbstractMesh._BILLBOARDMODE_X;
  19. }
  20. public static get BILLBOARDMODE_Y(): number {
  21. return AbstractMesh._BILLBOARDMODE_Y;
  22. }
  23. public static get BILLBOARDMODE_Z(): number {
  24. return AbstractMesh._BILLBOARDMODE_Z;
  25. }
  26. public static get BILLBOARDMODE_ALL(): number {
  27. return AbstractMesh._BILLBOARDMODE_ALL;
  28. }
  29. // facetData private properties
  30. private _facetPositions: Vector3[]; // facet local positions
  31. private _facetNormals: Vector3[]; // facet local normals
  32. private _facetPartitioning: number[][]; // partitioning array of facet index arrays
  33. private _facetNb: number = 0; // facet number
  34. private _partitioningSubdivisions: number = 10; // number of subdivisions per axis in the partioning space
  35. private _partitioningBBoxRatio: number = 1.01; // the partioning array space is by default 1% bigger than the bounding box
  36. private _facetDataEnabled: boolean = false; // is the facet data feature enabled on this mesh ?
  37. private _facetParameters: any = {}; // keep a reference to the object parameters to avoid memory re-allocation
  38. private _bbSize: Vector3 = Vector3.Zero(); // bbox size approximated for facet data
  39. private _subDiv = { // actual number of subdivisions per axis for ComputeNormals()
  40. max: 1,
  41. X: 1,
  42. Y: 1,
  43. Z: 1
  44. };
  45. /**
  46. * Read-only : the number of facets in the mesh
  47. */
  48. public get facetNb(): number {
  49. return this._facetNb;
  50. }
  51. /**
  52. * The number (integer) of subdivisions per axis in the partioning space
  53. */
  54. public get partitioningSubdivisions(): number {
  55. return this._partitioningSubdivisions;
  56. }
  57. public set partitioningSubdivisions(nb: number) {
  58. this._partitioningSubdivisions = nb;
  59. }
  60. /**
  61. * The ratio (float) to apply to the bouding box size to set to the partioning space.
  62. * Ex : 1.01 (default) the partioning space is 1% bigger than the bounding box.
  63. */
  64. public get partitioningBBoxRatio(): number {
  65. return this._partitioningBBoxRatio;
  66. }
  67. public set partitioningBBoxRatio(ratio: number) {
  68. this._partitioningBBoxRatio = ratio;
  69. }
  70. /**
  71. * Read-only boolean : is the feature facetData enabled ?
  72. */
  73. public get isFacetDataEnabled(): boolean {
  74. return this._facetDataEnabled;
  75. }
  76. // Events
  77. /**
  78. * An event triggered when this mesh collides with another one
  79. * @type {BABYLON.Observable}
  80. */
  81. public onCollideObservable = new Observable<AbstractMesh>();
  82. private _onCollideObserver: Observer<AbstractMesh>;
  83. public set onCollide(callback: () => void) {
  84. if (this._onCollideObserver) {
  85. this.onCollideObservable.remove(this._onCollideObserver);
  86. }
  87. this._onCollideObserver = this.onCollideObservable.add(callback);
  88. }
  89. /**
  90. * An event triggered when the collision's position changes
  91. * @type {BABYLON.Observable}
  92. */
  93. public onCollisionPositionChangeObservable = new Observable<Vector3>();
  94. private _onCollisionPositionChangeObserver: Observer<Vector3>;
  95. public set onCollisionPositionChange(callback: () => void) {
  96. if (this._onCollisionPositionChangeObserver) {
  97. this.onCollisionPositionChangeObservable.remove(this._onCollisionPositionChangeObserver);
  98. }
  99. this._onCollisionPositionChangeObserver = this.onCollisionPositionChangeObservable.add(callback);
  100. }
  101. /**
  102. * An event triggered after the world matrix is updated
  103. * @type {BABYLON.Observable}
  104. */
  105. public onAfterWorldMatrixUpdateObservable = new Observable<AbstractMesh>();
  106. /**
  107. * An event triggered when material is changed
  108. * @type {BABYLON.Observable}
  109. */
  110. public onMaterialChangedObservable = new Observable<AbstractMesh>();
  111. // Properties
  112. public definedFacingForward = true; // orientation for POV movement & rotation
  113. public position = Vector3.Zero();
  114. /**
  115. * This property determines the type of occlusion query algorithm to run in WebGl, you can use:
  116. * AbstractMesh.OCCLUSION_ALGORITHM_TYPE_ACCURATE which is mapped to GL_ANY_SAMPLES_PASSED.
  117. * or
  118. * AbstractMesh.OCCLUSION_ALGORITHM_TYPE_CONSERVATIVE (Default Value) which is mapped to GL_ANY_SAMPLES_PASSED_CONSERVATIVE which is a false positive algorithm that is faster than GL_ANY_SAMPLES_PASSED but less accurate.
  119. * for more info check WebGl documentations
  120. */
  121. public occlusionQueryAlgorithmType = AbstractMesh.OCCLUSION_ALGORITHM_TYPE_CONSERVATIVE;
  122. /**
  123. * This property is responsible for starting the occlusion query within the Mesh or not, this property is also used to determine what should happen when the occlusionRetryCount is reached. It has supports 3 values:
  124. * OCCLUSION_TYPE_NONE (Default Value): this option means no occlusion query whith the Mesh.
  125. * OCCLUSION_TYPE_OPTIMISTIC: this option is means use occlusion query and if occlusionRetryCount is reached and the query is broken show the mesh.
  126. * OCCLUSION_TYPE_STRICT: this option is means use occlusion query and if occlusionRetryCount is reached and the query is broken restore the last state of the mesh occlusion if the mesh was visible then show the mesh if was hidden then hide don't show.
  127. */
  128. public occlusionType = AbstractMesh.OCCLUSION_TYPE_NONE;
  129. /**
  130. * This number indicates the number of allowed retries before stop the occlusion query, this is useful if the occlusion query is taking long time before to the query result is retireved, the query result indicates if the object is visible within the scene or not and based on that Babylon.Js engine decideds to show or hide the object.
  131. * The default value is -1 which means don't break the query and wait till the result.
  132. */
  133. public occlusionRetryCount = -1;
  134. private _occlusionInternalRetryCounter = 0;
  135. protected _isOccluded = false;
  136. /**
  137. * Property isOccluded : Gets or sets whether the mesh is occluded or not, it is used also to set the intial state of the mesh to be occluded or not.
  138. */
  139. public get isOccluded(): boolean {
  140. return this._isOccluded;
  141. }
  142. public set isOccluded(value: boolean) {
  143. this._isOccluded = value;
  144. }
  145. private _isOcclusionQueryInProgress = false;
  146. /**
  147. * Flag to check the progress status of the query
  148. */
  149. public get isOcclusionQueryInProgress(): boolean {
  150. return this._isOcclusionQueryInProgress;
  151. }
  152. private _occlusionQuery: WebGLQuery;
  153. private _rotation = Vector3.Zero();
  154. private _rotationQuaternion: Quaternion;
  155. private _scaling = Vector3.One();
  156. public billboardMode = AbstractMesh.BILLBOARDMODE_NONE;
  157. public visibility = 1.0;
  158. public alphaIndex = Number.MAX_VALUE;
  159. public infiniteDistance = false;
  160. public isVisible = true;
  161. public isPickable = true;
  162. public showBoundingBox = false;
  163. public showSubMeshesBoundingBox = false;
  164. public isBlocker = false;
  165. public enablePointerMoveEvents = false;
  166. public renderingGroupId = 0;
  167. private _material: Material
  168. public get material(): Material {
  169. return this._material;
  170. }
  171. public set material(value: Material) {
  172. if (this._material === value) {
  173. return;
  174. }
  175. this._material = value;
  176. if (this.onMaterialChangedObservable.hasObservers) {
  177. this.onMaterialChangedObservable.notifyObservers(this);
  178. }
  179. if (!this.subMeshes) {
  180. return;
  181. }
  182. for (var subMesh of this.subMeshes) {
  183. subMesh.setEffect(null);
  184. }
  185. }
  186. private _receiveShadows = false;
  187. public get receiveShadows(): boolean {
  188. return this._receiveShadows;
  189. }
  190. public set receiveShadows(value: boolean) {
  191. if (this._receiveShadows === value) {
  192. return;
  193. }
  194. this._receiveShadows = value;
  195. this._markSubMeshesAsLightDirty();
  196. }
  197. public renderOutline = false;
  198. public outlineColor = Color3.Red();
  199. public outlineWidth = 0.02;
  200. public renderOverlay = false;
  201. public overlayColor = Color3.Red();
  202. public overlayAlpha = 0.5;
  203. private _hasVertexAlpha = false;
  204. public get hasVertexAlpha(): boolean {
  205. return this._hasVertexAlpha;
  206. }
  207. public set hasVertexAlpha(value: boolean) {
  208. if (this._hasVertexAlpha === value) {
  209. return;
  210. }
  211. this._hasVertexAlpha = value;
  212. this._markSubMeshesAsAttributesDirty();
  213. }
  214. private _useVertexColors = true;
  215. public get useVertexColors(): boolean {
  216. return this._useVertexColors;
  217. }
  218. public set useVertexColors(value: boolean) {
  219. if (this._useVertexColors === value) {
  220. return;
  221. }
  222. this._useVertexColors = value;
  223. this._markSubMeshesAsAttributesDirty();
  224. }
  225. private _computeBonesUsingShaders = true;
  226. public get computeBonesUsingShaders(): boolean {
  227. return this._computeBonesUsingShaders;
  228. }
  229. public set computeBonesUsingShaders(value: boolean) {
  230. if (this._computeBonesUsingShaders === value) {
  231. return;
  232. }
  233. this._computeBonesUsingShaders = value;
  234. this._markSubMeshesAsAttributesDirty();
  235. }
  236. private _numBoneInfluencers = 4;
  237. public get numBoneInfluencers(): number {
  238. return this._numBoneInfluencers;
  239. }
  240. public set numBoneInfluencers(value: number) {
  241. if (this._numBoneInfluencers === value) {
  242. return;
  243. }
  244. this._numBoneInfluencers = value;
  245. this._markSubMeshesAsAttributesDirty();
  246. }
  247. private _applyFog = true;
  248. public get applyFog(): boolean {
  249. return this._applyFog;
  250. }
  251. public set applyFog(value: boolean) {
  252. if (this._applyFog === value) {
  253. return;
  254. }
  255. this._applyFog = value;
  256. this._markSubMeshesAsMiscDirty();
  257. }
  258. public scalingDeterminant = 1;
  259. public useOctreeForRenderingSelection = true;
  260. public useOctreeForPicking = true;
  261. public useOctreeForCollisions = true;
  262. private _layerMask: number = 0x0FFFFFFF;
  263. public get layerMask(): number {
  264. return this._layerMask;
  265. }
  266. public set layerMask(value: number) {
  267. if (value === this._layerMask) {
  268. return;
  269. }
  270. this._layerMask = value;
  271. this._resyncLightSources();
  272. }
  273. /**
  274. * True if the mesh must be rendered in any case.
  275. */
  276. public alwaysSelectAsActiveMesh = false;
  277. /**
  278. * This scene's action manager
  279. * @type {BABYLON.ActionManager}
  280. */
  281. public actionManager: ActionManager;
  282. // Physics
  283. public physicsImpostor: BABYLON.PhysicsImpostor;
  284. // Collisions
  285. private _checkCollisions = false;
  286. private _collisionMask = -1;
  287. private _collisionGroup = -1;
  288. public ellipsoid = new Vector3(0.5, 1, 0.5);
  289. public ellipsoidOffset = new Vector3(0, 0, 0);
  290. private _collider: Collider;
  291. private _oldPositionForCollisions = new Vector3(0, 0, 0);
  292. private _diffPositionForCollisions = new Vector3(0, 0, 0);
  293. private _newPositionForCollisions = new Vector3(0, 0, 0);
  294. public get collisionMask(): number {
  295. return this._collisionMask;
  296. }
  297. public set collisionMask(mask: number) {
  298. this._collisionMask = !isNaN(mask) ? mask : -1;
  299. }
  300. public get collisionGroup(): number {
  301. return this._collisionGroup;
  302. }
  303. public set collisionGroup(mask: number) {
  304. this._collisionGroup = !isNaN(mask) ? mask : -1;
  305. }
  306. // Attach to bone
  307. private _meshToBoneReferal: AbstractMesh;
  308. // Edges
  309. public edgesWidth = 1;
  310. public edgesColor = new Color4(1, 0, 0, 1);
  311. public _edgesRenderer: EdgesRenderer;
  312. // Cache
  313. private _localWorld = Matrix.Zero();
  314. public _worldMatrix = Matrix.Zero();
  315. private _absolutePosition = Vector3.Zero();
  316. private _collisionsTransformMatrix = Matrix.Zero();
  317. private _collisionsScalingMatrix = Matrix.Zero();
  318. private _isDirty = false;
  319. public _masterMesh: AbstractMesh;
  320. public _boundingInfo: BoundingInfo;
  321. private _pivotMatrix = Matrix.Identity();
  322. public _isDisposed = false;
  323. public _renderId = 0;
  324. public subMeshes: SubMesh[];
  325. public _submeshesOctree: Octree<SubMesh>;
  326. public _intersectionsInProgress = new Array<AbstractMesh>();
  327. private _isWorldMatrixFrozen = false;
  328. public _unIndexed = false;
  329. public _poseMatrix: Matrix;
  330. public _lightSources = new Array<Light>();
  331. public get _positions(): Vector3[] {
  332. return null;
  333. }
  334. // Loading properties
  335. public _waitingActions: any;
  336. public _waitingFreezeWorldMatrix: boolean;
  337. // Skeleton
  338. private _skeleton: Skeleton;
  339. public _bonesTransformMatrices: Float32Array;
  340. public set skeleton(value: Skeleton) {
  341. if (this._skeleton && this._skeleton.needInitialSkinMatrix) {
  342. this._skeleton._unregisterMeshWithPoseMatrix(this);
  343. }
  344. if (value && value.needInitialSkinMatrix) {
  345. value._registerMeshWithPoseMatrix(this);
  346. }
  347. this._skeleton = value;
  348. if (!this._skeleton) {
  349. this._bonesTransformMatrices = null;
  350. }
  351. this._markSubMeshesAsAttributesDirty();
  352. }
  353. public get skeleton(): Skeleton {
  354. return this._skeleton;
  355. }
  356. // Constructor
  357. constructor(name: string, scene: Scene) {
  358. super(name, scene);
  359. this.getScene().addMesh(this);
  360. this._resyncLightSources();
  361. }
  362. /**
  363. * Boolean : true if the mesh has been disposed.
  364. */
  365. public isDisposed(): boolean {
  366. return this._isDisposed;
  367. }
  368. /**
  369. * Returns the string "AbstractMesh"
  370. */
  371. public getClassName(): string {
  372. return "AbstractMesh";
  373. }
  374. /**
  375. * @param {boolean} fullDetails - support for multiple levels of logging within scene loading
  376. */
  377. public toString(fullDetails?: boolean): string {
  378. var ret = "Name: " + this.name + ", isInstance: " + (this instanceof InstancedMesh ? "YES" : "NO");
  379. ret += ", # of submeshes: " + (this.subMeshes ? this.subMeshes.length : 0);
  380. if (this._skeleton) {
  381. ret += ", skeleton: " + this._skeleton.name;
  382. }
  383. if (fullDetails) {
  384. ret += ", billboard mode: " + (["NONE", "X", "Y", null, "Z", null, null, "ALL"])[this.billboardMode];
  385. ret += ", freeze wrld mat: " + (this._isWorldMatrixFrozen || this._waitingFreezeWorldMatrix ? "YES" : "NO");
  386. }
  387. return ret;
  388. }
  389. public _rebuild(): void {
  390. if (this._occlusionQuery) {
  391. this._occlusionQuery = null;
  392. }
  393. if (this._edgesRenderer) {
  394. this._edgesRenderer._rebuild();
  395. }
  396. if (!this.subMeshes) {
  397. return;
  398. }
  399. for (var subMesh of this.subMeshes) {
  400. subMesh._rebuild();
  401. }
  402. }
  403. public _resyncLightSources(): void {
  404. this._lightSources.length = 0;
  405. for (var light of this.getScene().lights) {
  406. if (!light.isEnabled()) {
  407. continue;
  408. }
  409. if (light.canAffectMesh(this)) {
  410. this._lightSources.push(light);
  411. }
  412. }
  413. this._markSubMeshesAsLightDirty();
  414. }
  415. public _resyncLighSource(light: Light): void {
  416. var isIn = light.isEnabled() && light.canAffectMesh(this);
  417. var index = this._lightSources.indexOf(light);
  418. if (index === -1) {
  419. if (!isIn) {
  420. return;
  421. }
  422. this._lightSources.push(light);
  423. } else {
  424. if (isIn) {
  425. return;
  426. }
  427. this._lightSources.splice(index, 1);
  428. }
  429. this._markSubMeshesAsLightDirty();
  430. }
  431. public _removeLightSource(light: Light): void {
  432. var index = this._lightSources.indexOf(light);
  433. if (index === -1) {
  434. return;
  435. }
  436. this._lightSources.splice(index, 1);
  437. }
  438. private _markSubMeshesAsDirty(func: (defines: MaterialDefines) => void) {
  439. if (!this.subMeshes) {
  440. return;
  441. }
  442. for (var subMesh of this.subMeshes) {
  443. if (subMesh._materialDefines) {
  444. func(subMesh._materialDefines);
  445. }
  446. }
  447. }
  448. public _markSubMeshesAsLightDirty() {
  449. this._markSubMeshesAsDirty(defines => defines.markAsLightDirty());
  450. }
  451. public _markSubMeshesAsAttributesDirty() {
  452. this._markSubMeshesAsDirty(defines => defines.markAsAttributesDirty());
  453. }
  454. public _markSubMeshesAsMiscDirty() {
  455. if (!this.subMeshes) {
  456. return;
  457. }
  458. for (var subMesh of this.subMeshes) {
  459. var material = subMesh.getMaterial();
  460. if (material) {
  461. material.markAsDirty(Material.MiscDirtyFlag);
  462. }
  463. }
  464. }
  465. /**
  466. * Rotation property : a Vector3 depicting the rotation value in radians around each local axis X, Y, Z.
  467. * If rotation quaternion is set, this Vector3 will (almost always) be the Zero vector!
  468. * Default : (0.0, 0.0, 0.0)
  469. */
  470. public get rotation(): Vector3 {
  471. return this._rotation;
  472. }
  473. public set rotation(newRotation: Vector3) {
  474. this._rotation = newRotation;
  475. }
  476. /**
  477. * Scaling property : a Vector3 depicting the mesh scaling along each local axis X, Y, Z.
  478. * Default : (1.0, 1.0, 1.0)
  479. */
  480. public get scaling(): Vector3 {
  481. return this._scaling;
  482. }
  483. public set scaling(newScaling: Vector3) {
  484. this._scaling = newScaling;
  485. if (this.physicsImpostor) {
  486. this.physicsImpostor.forceUpdate();
  487. }
  488. }
  489. /**
  490. * Rotation Quaternion property : this a Quaternion object depicting the mesh rotation by using a unit quaternion.
  491. * It's null by default.
  492. * If set, only the rotationQuaternion is then used to compute the mesh rotation and its property `.rotation\ is then ignored and set to (0.0, 0.0, 0.0)
  493. */
  494. public get rotationQuaternion() {
  495. return this._rotationQuaternion;
  496. }
  497. public set rotationQuaternion(quaternion: Quaternion) {
  498. this._rotationQuaternion = quaternion;
  499. //reset the rotation vector.
  500. if (quaternion && this.rotation.length()) {
  501. this.rotation.copyFromFloats(0.0, 0.0, 0.0);
  502. }
  503. }
  504. // Methods
  505. /**
  506. * Copies the paramater passed Matrix into the mesh Pose matrix.
  507. * Returns the AbstractMesh.
  508. */
  509. public updatePoseMatrix(matrix: Matrix): AbstractMesh {
  510. this._poseMatrix.copyFrom(matrix);
  511. return this;
  512. }
  513. /**
  514. * Returns the mesh Pose matrix.
  515. * Returned object : Matrix
  516. */
  517. public getPoseMatrix(): Matrix {
  518. return this._poseMatrix;
  519. }
  520. /**
  521. * Disables the mesh edger rendering mode.
  522. * Returns the AbstractMesh.
  523. */
  524. public disableEdgesRendering(): AbstractMesh {
  525. if (this._edgesRenderer !== undefined) {
  526. this._edgesRenderer.dispose();
  527. this._edgesRenderer = undefined;
  528. }
  529. return this;
  530. }
  531. /**
  532. * Enables the edge rendering mode on the mesh.
  533. * This mode makes the mesh edges visible.
  534. * Returns the AbstractMesh.
  535. */
  536. public enableEdgesRendering(epsilon = 0.95, checkVerticesInsteadOfIndices = false): AbstractMesh {
  537. this.disableEdgesRendering();
  538. this._edgesRenderer = new EdgesRenderer(this, epsilon, checkVerticesInsteadOfIndices);
  539. return this;
  540. }
  541. /**
  542. * Returns true if the mesh is blocked. Used by the class Mesh.
  543. * Returns the boolean `false` by default.
  544. */
  545. public get isBlocked(): boolean {
  546. return false;
  547. }
  548. /**
  549. * Returns the mesh itself by default, used by the class Mesh.
  550. * Returned type : AbstractMesh
  551. */
  552. public getLOD(camera: Camera): AbstractMesh {
  553. return this;
  554. }
  555. /**
  556. * Returns 0 by default, used by the class Mesh.
  557. * Returns an integer.
  558. */
  559. public getTotalVertices(): number {
  560. return 0;
  561. }
  562. /**
  563. * Returns null by default, used by the class Mesh.
  564. * Returned type : integer array
  565. */
  566. public getIndices(): IndicesArray {
  567. return null;
  568. }
  569. /**
  570. * Returns the array of the requested vertex data kind. Used by the class Mesh. Returns null here.
  571. * Returned type : float array or Float32Array
  572. */
  573. public getVerticesData(kind: string): number[] | Float32Array {
  574. return null;
  575. }
  576. /**
  577. * Sets the vertex data of the mesh geometry for the requested `kind`.
  578. * If the mesh has no geometry, a new Geometry object is set to the mesh and then passed this vertex data.
  579. * The `data` are either a numeric array either a Float32Array.
  580. * The parameter `updatable` is passed as is to the underlying Geometry object constructor (if initianilly none) or updater.
  581. * The parameter `stride` is an optional positive integer, it is usually automatically deducted from the `kind` (3 for positions or normals, 2 for UV, etc).
  582. * Note that a new underlying VertexBuffer object is created each call.
  583. * If the `kind` is the `PositionKind`, the mesh BoundingInfo is renewed, so the bounding box and sphere, and the mesh World Matrix is recomputed.
  584. *
  585. * Possible `kind` values :
  586. * - BABYLON.VertexBuffer.PositionKind
  587. * - BABYLON.VertexBuffer.UVKind
  588. * - BABYLON.VertexBuffer.UV2Kind
  589. * - BABYLON.VertexBuffer.UV3Kind
  590. * - BABYLON.VertexBuffer.UV4Kind
  591. * - BABYLON.VertexBuffer.UV5Kind
  592. * - BABYLON.VertexBuffer.UV6Kind
  593. * - BABYLON.VertexBuffer.ColorKind
  594. * - BABYLON.VertexBuffer.MatricesIndicesKind
  595. * - BABYLON.VertexBuffer.MatricesIndicesExtraKind
  596. * - BABYLON.VertexBuffer.MatricesWeightsKind
  597. * - BABYLON.VertexBuffer.MatricesWeightsExtraKind
  598. *
  599. * Returns the Mesh.
  600. */
  601. public setVerticesData(kind: string, data: number[] | Float32Array, updatable?: boolean, stride?: number): Mesh {
  602. return null;
  603. }
  604. /**
  605. * Updates the existing vertex data of the mesh geometry for the requested `kind`.
  606. * If the mesh has no geometry, it is simply returned as it is.
  607. * The `data` are either a numeric array either a Float32Array.
  608. * No new underlying VertexBuffer object is created.
  609. * If the `kind` is the `PositionKind` and if `updateExtends` is true, the mesh BoundingInfo is renewed, so the bounding box and sphere, and the mesh World Matrix is recomputed.
  610. * If the parameter `makeItUnique` is true, a new global geometry is created from this positions and is set to the mesh.
  611. *
  612. * Possible `kind` values :
  613. * - BABYLON.VertexBuffer.PositionKind
  614. * - BABYLON.VertexBuffer.UVKind
  615. * - BABYLON.VertexBuffer.UV2Kind
  616. * - BABYLON.VertexBuffer.UV3Kind
  617. * - BABYLON.VertexBuffer.UV4Kind
  618. * - BABYLON.VertexBuffer.UV5Kind
  619. * - BABYLON.VertexBuffer.UV6Kind
  620. * - BABYLON.VertexBuffer.ColorKind
  621. * - BABYLON.VertexBuffer.MatricesIndicesKind
  622. * - BABYLON.VertexBuffer.MatricesIndicesExtraKind
  623. * - BABYLON.VertexBuffer.MatricesWeightsKind
  624. * - BABYLON.VertexBuffer.MatricesWeightsExtraKind
  625. *
  626. * Returns the Mesh.
  627. */
  628. public updateVerticesData(kind: string, data: number[] | Float32Array, updateExtends?: boolean, makeItUnique?: boolean): Mesh {
  629. return null;
  630. }
  631. /**
  632. * Sets the mesh indices.
  633. * Expects an array populated with integers or a typed array (Int32Array, Uint32Array, Uint16Array).
  634. * If the mesh has no geometry, a new Geometry object is created and set to the mesh.
  635. * This method creates a new index buffer each call.
  636. * Returns the Mesh.
  637. */
  638. public setIndices(indices: IndicesArray, totalVertices?: number): Mesh {
  639. return null;
  640. }
  641. /** Returns false by default, used by the class Mesh.
  642. * Returns a boolean
  643. */
  644. public isVerticesDataPresent(kind: string): boolean {
  645. return false;
  646. }
  647. /**
  648. * Returns the mesh BoundingInfo object or creates a new one and returns it if undefined.
  649. * Returns a BoundingInfo
  650. */
  651. public getBoundingInfo(): BoundingInfo {
  652. if (this._masterMesh) {
  653. return this._masterMesh.getBoundingInfo();
  654. }
  655. if (!this._boundingInfo) {
  656. this._updateBoundingInfo();
  657. }
  658. return this._boundingInfo;
  659. }
  660. /**
  661. * Sets a mesh new object BoundingInfo.
  662. * Returns the AbstractMesh.
  663. */
  664. public setBoundingInfo(boundingInfo: BoundingInfo): AbstractMesh {
  665. this._boundingInfo = boundingInfo;
  666. return this;
  667. }
  668. public get useBones(): boolean {
  669. return this.skeleton && this.getScene().skeletonsEnabled && this.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind) && this.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind);
  670. }
  671. public _preActivate(): void {
  672. }
  673. public _preActivateForIntermediateRendering(renderId: number): void {
  674. }
  675. public _activate(renderId: number): void {
  676. this._renderId = renderId;
  677. }
  678. /**
  679. * Returns the last update of the World matrix
  680. * Returns a Matrix.
  681. */
  682. public getWorldMatrix(): Matrix {
  683. if (this._masterMesh) {
  684. return this._masterMesh.getWorldMatrix();
  685. }
  686. if (this._currentRenderId !== this.getScene().getRenderId() || !this.isSynchronized()) {
  687. this.computeWorldMatrix();
  688. }
  689. return this._worldMatrix;
  690. }
  691. /**
  692. * Returns directly the last state of the mesh World matrix.
  693. * A Matrix is returned.
  694. */
  695. public get worldMatrixFromCache(): Matrix {
  696. return this._worldMatrix;
  697. }
  698. /**
  699. * Returns the current mesh absolute position.
  700. * Retuns a Vector3.
  701. */
  702. public get absolutePosition(): Vector3 {
  703. return this._absolutePosition;
  704. }
  705. /**
  706. * Prevents the World matrix to be computed any longer.
  707. * Returns the AbstractMesh.
  708. */
  709. public freezeWorldMatrix(): AbstractMesh {
  710. this._isWorldMatrixFrozen = false; // no guarantee world is not already frozen, switch off temporarily
  711. this.computeWorldMatrix(true);
  712. this._isWorldMatrixFrozen = true;
  713. return this;
  714. }
  715. /**
  716. * Allows back the World matrix computation.
  717. * Returns the AbstractMesh.
  718. */
  719. public unfreezeWorldMatrix() {
  720. this._isWorldMatrixFrozen = false;
  721. this.computeWorldMatrix(true);
  722. return this;
  723. }
  724. /**
  725. * True if the World matrix has been frozen.
  726. * Returns a boolean.
  727. */
  728. public get isWorldMatrixFrozen(): boolean {
  729. return this._isWorldMatrixFrozen;
  730. }
  731. private static _rotationAxisCache = new Quaternion();
  732. /**
  733. * Rotates the mesh around the axis vector for the passed angle (amount) expressed in radians, in the given space.
  734. * space (default LOCAL) can be either BABYLON.Space.LOCAL, either BABYLON.Space.WORLD.
  735. * Note that the property `rotationQuaternion` is then automatically updated and the property `rotation` is set to (0,0,0) and no longer used.
  736. * The passed axis is also normalized.
  737. * Returns the AbstractMesh.
  738. */
  739. public rotate(axis: Vector3, amount: number, space?: Space): AbstractMesh {
  740. axis.normalize();
  741. if (!this.rotationQuaternion) {
  742. this.rotationQuaternion = Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z);
  743. this.rotation = Vector3.Zero();
  744. }
  745. var rotationQuaternion: Quaternion;
  746. if (!space || (space as any) === Space.LOCAL) {
  747. rotationQuaternion = Quaternion.RotationAxisToRef(axis, amount, AbstractMesh._rotationAxisCache);
  748. this.rotationQuaternion.multiplyToRef(rotationQuaternion, this.rotationQuaternion);
  749. }
  750. else {
  751. if (this.parent) {
  752. var invertParentWorldMatrix = this.parent.getWorldMatrix().clone();
  753. invertParentWorldMatrix.invert();
  754. axis = Vector3.TransformNormal(axis, invertParentWorldMatrix);
  755. }
  756. rotationQuaternion = Quaternion.RotationAxisToRef(axis, amount, AbstractMesh._rotationAxisCache);
  757. rotationQuaternion.multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
  758. }
  759. return this;
  760. }
  761. /**
  762. * Rotates the mesh around the axis vector for the passed angle (amount) expressed in radians, in world space.
  763. * Note that the property `rotationQuaternion` is then automatically updated and the property `rotation` is set to (0,0,0) and no longer used.
  764. * The passed axis is also normalized.
  765. * Returns the AbstractMesh.
  766. * Method is based on http://www.euclideanspace.com/maths/geometry/affine/aroundPoint/index.htm
  767. */
  768. public rotateAround(point: Vector3, axis: Vector3, amount: number): AbstractMesh {
  769. axis.normalize();
  770. if (!this.rotationQuaternion) {
  771. this.rotationQuaternion = Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z);
  772. this.rotation.copyFromFloats(0, 0, 0);
  773. }
  774. point.subtractToRef(this.position, Tmp.Vector3[0]);
  775. Matrix.TranslationToRef(Tmp.Vector3[0].x, Tmp.Vector3[0].y, Tmp.Vector3[0].z, Tmp.Matrix[0]);
  776. Tmp.Matrix[0].invertToRef(Tmp.Matrix[2]);
  777. Matrix.RotationAxisToRef(axis, amount, Tmp.Matrix[1]);
  778. Tmp.Matrix[2].multiplyToRef(Tmp.Matrix[1], Tmp.Matrix[2]);
  779. Tmp.Matrix[2].multiplyToRef(Tmp.Matrix[0], Tmp.Matrix[2]);
  780. Tmp.Matrix[2].decompose(Tmp.Vector3[0], Tmp.Quaternion[0], Tmp.Vector3[1]);
  781. this.position.addInPlace(Tmp.Vector3[1]);
  782. Tmp.Quaternion[0].multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
  783. return this;
  784. }
  785. /**
  786. * Translates the mesh along the axis vector for the passed distance in the given space.
  787. * space (default LOCAL) can be either BABYLON.Space.LOCAL, either BABYLON.Space.WORLD.
  788. * Returns the AbstractMesh.
  789. */
  790. public translate(axis: Vector3, distance: number, space?: Space): AbstractMesh {
  791. var displacementVector = axis.scale(distance);
  792. if (!space || (space as any) === Space.LOCAL) {
  793. var tempV3 = this.getPositionExpressedInLocalSpace().add(displacementVector);
  794. this.setPositionWithLocalVector(tempV3);
  795. }
  796. else {
  797. this.setAbsolutePosition(this.getAbsolutePosition().add(displacementVector));
  798. }
  799. return this;
  800. }
  801. /**
  802. * Adds a rotation step to the mesh current rotation.
  803. * x, y, z are Euler angles expressed in radians.
  804. * This methods updates the current mesh rotation, either mesh.rotation, either mesh.rotationQuaternion if it's set.
  805. * This means this rotation is made in the mesh local space only.
  806. * It's useful to set a custom rotation order different from the BJS standard one YXZ.
  807. * Example : this rotates the mesh first around its local X axis, then around its local Z axis, finally around its local Y axis.
  808. * ```javascript
  809. * mesh.addRotation(x1, 0, 0).addRotation(0, 0, z2).addRotation(0, 0, y3);
  810. * ```
  811. * Note that `addRotation()` accumulates the passed rotation values to the current ones and computes the .rotation or .rotationQuaternion updated values.
  812. * Under the hood, only quaternions are used. So it's a little faster is you use .rotationQuaternion because it doesn't need to translate them back to Euler angles.
  813. * Returns the AbstractMesh.
  814. */
  815. public addRotation(x: number, y: number, z: number): AbstractMesh {
  816. var rotationQuaternion;
  817. if (this.rotationQuaternion) {
  818. rotationQuaternion = this.rotationQuaternion;
  819. }
  820. else {
  821. rotationQuaternion = Tmp.Quaternion[1];
  822. Quaternion.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, rotationQuaternion);
  823. }
  824. var accumulation = BABYLON.Tmp.Quaternion[0];
  825. Quaternion.RotationYawPitchRollToRef(y, x, z, accumulation);
  826. rotationQuaternion.multiplyInPlace(accumulation);
  827. if (!this.rotationQuaternion) {
  828. rotationQuaternion.toEulerAnglesToRef(this.rotation);
  829. }
  830. return this;
  831. }
  832. /**
  833. * Retuns the mesh absolute position in the World.
  834. * Returns a Vector3.
  835. */
  836. public getAbsolutePosition(): Vector3 {
  837. this.computeWorldMatrix();
  838. return this._absolutePosition;
  839. }
  840. /**
  841. * Sets the mesh absolute position in the World from a Vector3 or an Array(3).
  842. * Returns the AbstractMesh.
  843. */
  844. public setAbsolutePosition(absolutePosition: Vector3): AbstractMesh {
  845. if (!absolutePosition) {
  846. return;
  847. }
  848. var absolutePositionX;
  849. var absolutePositionY;
  850. var absolutePositionZ;
  851. if (absolutePosition.x === undefined) {
  852. if (arguments.length < 3) {
  853. return;
  854. }
  855. absolutePositionX = arguments[0];
  856. absolutePositionY = arguments[1];
  857. absolutePositionZ = arguments[2];
  858. }
  859. else {
  860. absolutePositionX = absolutePosition.x;
  861. absolutePositionY = absolutePosition.y;
  862. absolutePositionZ = absolutePosition.z;
  863. }
  864. if (this.parent) {
  865. var invertParentWorldMatrix = this.parent.getWorldMatrix().clone();
  866. invertParentWorldMatrix.invert();
  867. var worldPosition = new Vector3(absolutePositionX, absolutePositionY, absolutePositionZ);
  868. this.position = Vector3.TransformCoordinates(worldPosition, invertParentWorldMatrix);
  869. } else {
  870. this.position.x = absolutePositionX;
  871. this.position.y = absolutePositionY;
  872. this.position.z = absolutePositionZ;
  873. }
  874. return this;
  875. }
  876. // ================================== Point of View Movement =================================
  877. /**
  878. * Perform relative position change from the point of view of behind the front of the mesh.
  879. * This is performed taking into account the meshes current rotation, so you do not have to care.
  880. * Supports definition of mesh facing forward or backward.
  881. * @param {number} amountRight
  882. * @param {number} amountUp
  883. * @param {number} amountForward
  884. *
  885. * Returns the AbstractMesh.
  886. */
  887. public movePOV(amountRight: number, amountUp: number, amountForward: number): AbstractMesh {
  888. this.position.addInPlace(this.calcMovePOV(amountRight, amountUp, amountForward));
  889. return this;
  890. }
  891. /**
  892. * Calculate relative position change from the point of view of behind the front of the mesh.
  893. * This is performed taking into account the meshes current rotation, so you do not have to care.
  894. * Supports definition of mesh facing forward or backward.
  895. * @param {number} amountRight
  896. * @param {number} amountUp
  897. * @param {number} amountForward
  898. *
  899. * Returns a new Vector3.
  900. */
  901. public calcMovePOV(amountRight: number, amountUp: number, amountForward: number): Vector3 {
  902. var rotMatrix = new Matrix();
  903. var rotQuaternion = (this.rotationQuaternion) ? this.rotationQuaternion : Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z);
  904. rotQuaternion.toRotationMatrix(rotMatrix);
  905. var translationDelta = Vector3.Zero();
  906. var defForwardMult = this.definedFacingForward ? -1 : 1;
  907. Vector3.TransformCoordinatesFromFloatsToRef(amountRight * defForwardMult, amountUp, amountForward * defForwardMult, rotMatrix, translationDelta);
  908. return translationDelta;
  909. }
  910. // ================================== Point of View Rotation =================================
  911. /**
  912. * Perform relative rotation change from the point of view of behind the front of the mesh.
  913. * Supports definition of mesh facing forward or backward.
  914. * @param {number} flipBack
  915. * @param {number} twirlClockwise
  916. * @param {number} tiltRight
  917. *
  918. * Returns the AbstractMesh.
  919. */
  920. public rotatePOV(flipBack: number, twirlClockwise: number, tiltRight: number): AbstractMesh {
  921. this.rotation.addInPlace(this.calcRotatePOV(flipBack, twirlClockwise, tiltRight));
  922. return this;
  923. }
  924. /**
  925. * Calculate relative rotation change from the point of view of behind the front of the mesh.
  926. * Supports definition of mesh facing forward or backward.
  927. * @param {number} flipBack
  928. * @param {number} twirlClockwise
  929. * @param {number} tiltRight
  930. *
  931. * Returns a new Vector3.
  932. */
  933. public calcRotatePOV(flipBack: number, twirlClockwise: number, tiltRight: number): Vector3 {
  934. var defForwardMult = this.definedFacingForward ? 1 : -1;
  935. return new Vector3(flipBack * defForwardMult, twirlClockwise, tiltRight * defForwardMult);
  936. }
  937. /**
  938. * Sets a new pivot matrix to the mesh.
  939. * Returns the AbstractMesh.
  940. */
  941. public setPivotMatrix(matrix: Matrix): AbstractMesh {
  942. this._pivotMatrix = matrix;
  943. this._cache.pivotMatrixUpdated = true;
  944. return this;
  945. }
  946. /**
  947. * Returns the mesh pivot matrix.
  948. * Default : Identity.
  949. * A Matrix is returned.
  950. */
  951. public getPivotMatrix(): Matrix {
  952. return this._pivotMatrix;
  953. }
  954. public _isSynchronized(): boolean {
  955. if (this._isDirty) {
  956. return false;
  957. }
  958. if (this.billboardMode !== this._cache.billboardMode || this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE)
  959. return false;
  960. if (this._cache.pivotMatrixUpdated) {
  961. return false;
  962. }
  963. if (this.infiniteDistance) {
  964. return false;
  965. }
  966. if (!this._cache.position.equals(this.position))
  967. return false;
  968. if (this.rotationQuaternion) {
  969. if (!this._cache.rotationQuaternion.equals(this.rotationQuaternion))
  970. return false;
  971. }
  972. if (!this._cache.rotation.equals(this.rotation))
  973. return false;
  974. if (!this._cache.scaling.equals(this.scaling))
  975. return false;
  976. return true;
  977. }
  978. public _initCache() {
  979. super._initCache();
  980. this._cache.localMatrixUpdated = false;
  981. this._cache.position = Vector3.Zero();
  982. this._cache.scaling = Vector3.Zero();
  983. this._cache.rotation = Vector3.Zero();
  984. this._cache.rotationQuaternion = new Quaternion(0, 0, 0, 0);
  985. this._cache.billboardMode = -1;
  986. }
  987. public markAsDirty(property: string): AbstractMesh {
  988. if (property === "rotation") {
  989. this.rotationQuaternion = null;
  990. }
  991. this._currentRenderId = Number.MAX_VALUE;
  992. this._isDirty = true;
  993. return this;
  994. }
  995. /**
  996. * Updates the mesh BoundingInfo object and all its children BoundingInfo objects also.
  997. * Returns the AbstractMesh.
  998. */
  999. public _updateBoundingInfo(): AbstractMesh {
  1000. this._boundingInfo = this._boundingInfo || new BoundingInfo(this.absolutePosition, this.absolutePosition);
  1001. this._boundingInfo.update(this.worldMatrixFromCache);
  1002. this._updateSubMeshesBoundingInfo(this.worldMatrixFromCache);
  1003. return this;
  1004. }
  1005. /**
  1006. * Update a mesh's children BoundingInfo objects only.
  1007. * Returns the AbstractMesh.
  1008. */
  1009. public _updateSubMeshesBoundingInfo(matrix: Matrix): AbstractMesh {
  1010. if (!this.subMeshes) {
  1011. return;
  1012. }
  1013. for (var subIndex = 0; subIndex < this.subMeshes.length; subIndex++) {
  1014. var subMesh = this.subMeshes[subIndex];
  1015. if (!subMesh.IsGlobal) {
  1016. subMesh.updateBoundingInfo(matrix);
  1017. }
  1018. }
  1019. return this;
  1020. }
  1021. /**
  1022. * Computes the mesh World matrix and returns it.
  1023. * If the mesh world matrix is frozen, this computation does nothing more than returning the last frozen values.
  1024. * If the parameter `force` is let to `false` (default), the current cached World matrix is returned.
  1025. * If the parameter `force`is set to `true`, the actual computation is done.
  1026. * Returns the mesh World Matrix.
  1027. */
  1028. public computeWorldMatrix(force?: boolean): Matrix {
  1029. if (this._isWorldMatrixFrozen) {
  1030. return this._worldMatrix;
  1031. }
  1032. if (!force && this.isSynchronized(true)) {
  1033. this._currentRenderId = this.getScene().getRenderId();
  1034. return this._worldMatrix;
  1035. }
  1036. this._cache.position.copyFrom(this.position);
  1037. this._cache.scaling.copyFrom(this.scaling);
  1038. this._cache.pivotMatrixUpdated = false;
  1039. this._cache.billboardMode = this.billboardMode;
  1040. this._currentRenderId = this.getScene().getRenderId();
  1041. this._isDirty = false;
  1042. // Scaling
  1043. Matrix.ScalingToRef(this.scaling.x * this.scalingDeterminant, this.scaling.y * this.scalingDeterminant, this.scaling.z * this.scalingDeterminant, Tmp.Matrix[1]);
  1044. // Rotation
  1045. //rotate, if quaternion is set and rotation was used
  1046. if (this.rotationQuaternion) {
  1047. var len = this.rotation.length();
  1048. if (len) {
  1049. this.rotationQuaternion.multiplyInPlace(BABYLON.Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z))
  1050. this.rotation.copyFromFloats(0, 0, 0);
  1051. }
  1052. }
  1053. if (this.rotationQuaternion) {
  1054. this.rotationQuaternion.toRotationMatrix(Tmp.Matrix[0]);
  1055. this._cache.rotationQuaternion.copyFrom(this.rotationQuaternion);
  1056. } else {
  1057. Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, Tmp.Matrix[0]);
  1058. this._cache.rotation.copyFrom(this.rotation);
  1059. }
  1060. // Translation
  1061. if (this.infiniteDistance && !this.parent) {
  1062. var camera = this.getScene().activeCamera;
  1063. if (camera) {
  1064. var cameraWorldMatrix = camera.getWorldMatrix();
  1065. var cameraGlobalPosition = new Vector3(cameraWorldMatrix.m[12], cameraWorldMatrix.m[13], cameraWorldMatrix.m[14]);
  1066. Matrix.TranslationToRef(this.position.x + cameraGlobalPosition.x, this.position.y + cameraGlobalPosition.y,
  1067. this.position.z + cameraGlobalPosition.z, Tmp.Matrix[2]);
  1068. }
  1069. } else {
  1070. Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, Tmp.Matrix[2]);
  1071. }
  1072. // Composing transformations
  1073. this._pivotMatrix.multiplyToRef(Tmp.Matrix[1], Tmp.Matrix[4]);
  1074. Tmp.Matrix[4].multiplyToRef(Tmp.Matrix[0], Tmp.Matrix[5]);
  1075. // Billboarding (testing PG:http://www.babylonjs-playground.com/#UJEIL#13)
  1076. if (this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE && this.getScene().activeCamera) {
  1077. if ((this.billboardMode & AbstractMesh.BILLBOARDMODE_ALL) !== AbstractMesh.BILLBOARDMODE_ALL) {
  1078. // Need to decompose each rotation here
  1079. var currentPosition = Tmp.Vector3[3];
  1080. if (this.parent && this.parent.getWorldMatrix) {
  1081. if (this._meshToBoneReferal) {
  1082. this.parent.getWorldMatrix().multiplyToRef(this._meshToBoneReferal.getWorldMatrix(), Tmp.Matrix[6]);
  1083. Vector3.TransformCoordinatesToRef(this.position, Tmp.Matrix[6], currentPosition);
  1084. } else {
  1085. Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), currentPosition);
  1086. }
  1087. } else {
  1088. currentPosition.copyFrom(this.position);
  1089. }
  1090. currentPosition.subtractInPlace(this.getScene().activeCamera.globalPosition);
  1091. var finalEuler = Tmp.Vector3[4].copyFromFloats(0, 0, 0);
  1092. if ((this.billboardMode & AbstractMesh.BILLBOARDMODE_X) === AbstractMesh.BILLBOARDMODE_X) {
  1093. finalEuler.x = Math.atan2(-currentPosition.y, currentPosition.z);
  1094. }
  1095. if ((this.billboardMode & AbstractMesh.BILLBOARDMODE_Y) === AbstractMesh.BILLBOARDMODE_Y) {
  1096. finalEuler.y = Math.atan2(currentPosition.x, currentPosition.z);
  1097. }
  1098. if ((this.billboardMode & AbstractMesh.BILLBOARDMODE_Z) === AbstractMesh.BILLBOARDMODE_Z) {
  1099. finalEuler.z = Math.atan2(currentPosition.y, currentPosition.x);
  1100. }
  1101. Matrix.RotationYawPitchRollToRef(finalEuler.y, finalEuler.x, finalEuler.z, Tmp.Matrix[0]);
  1102. } else {
  1103. Tmp.Matrix[1].copyFrom(this.getScene().activeCamera.getViewMatrix());
  1104. Tmp.Matrix[1].setTranslationFromFloats(0, 0, 0);
  1105. Tmp.Matrix[1].invertToRef(Tmp.Matrix[0]);
  1106. }
  1107. Tmp.Matrix[1].copyFrom(Tmp.Matrix[5]);
  1108. Tmp.Matrix[1].multiplyToRef(Tmp.Matrix[0], Tmp.Matrix[5]);
  1109. }
  1110. // Local world
  1111. Tmp.Matrix[5].multiplyToRef(Tmp.Matrix[2], this._localWorld);
  1112. // Parent
  1113. if (this.parent && this.parent.getWorldMatrix) {
  1114. if (this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE) {
  1115. if (this._meshToBoneReferal) {
  1116. this.parent.getWorldMatrix().multiplyToRef(this._meshToBoneReferal.getWorldMatrix(), Tmp.Matrix[6]);
  1117. Tmp.Matrix[5].copyFrom(Tmp.Matrix[6]);
  1118. } else {
  1119. Tmp.Matrix[5].copyFrom(this.parent.getWorldMatrix());
  1120. }
  1121. this._localWorld.getTranslationToRef(Tmp.Vector3[5]);
  1122. Vector3.TransformCoordinatesToRef(Tmp.Vector3[5], Tmp.Matrix[5], Tmp.Vector3[5]);
  1123. this._worldMatrix.copyFrom(this._localWorld);
  1124. this._worldMatrix.setTranslation(Tmp.Vector3[5]);
  1125. } else {
  1126. if (this._meshToBoneReferal) {
  1127. this._localWorld.multiplyToRef(this.parent.getWorldMatrix(), Tmp.Matrix[6]);
  1128. Tmp.Matrix[6].multiplyToRef(this._meshToBoneReferal.getWorldMatrix(), this._worldMatrix);
  1129. } else {
  1130. this._localWorld.multiplyToRef(this.parent.getWorldMatrix(), this._worldMatrix);
  1131. }
  1132. }
  1133. this._markSyncedWithParent();
  1134. } else {
  1135. this._worldMatrix.copyFrom(this._localWorld);
  1136. }
  1137. // Bounding info
  1138. this._updateBoundingInfo();
  1139. // Absolute position
  1140. this._absolutePosition.copyFromFloats(this._worldMatrix.m[12], this._worldMatrix.m[13], this._worldMatrix.m[14]);
  1141. // Callbacks
  1142. this.onAfterWorldMatrixUpdateObservable.notifyObservers(this);
  1143. if (!this._poseMatrix) {
  1144. this._poseMatrix = Matrix.Invert(this._worldMatrix);
  1145. }
  1146. return this._worldMatrix;
  1147. }
  1148. /**
  1149. * If you'd like to be called back after the mesh position, rotation or scaling has been updated.
  1150. * @param func: callback function to add
  1151. *
  1152. * Returns the AbstractMesh.
  1153. */
  1154. public registerAfterWorldMatrixUpdate(func: (mesh: AbstractMesh) => void): AbstractMesh {
  1155. this.onAfterWorldMatrixUpdateObservable.add(func);
  1156. return this;
  1157. }
  1158. /**
  1159. * Removes a registered callback function.
  1160. * Returns the AbstractMesh.
  1161. */
  1162. public unregisterAfterWorldMatrixUpdate(func: (mesh: AbstractMesh) => void): AbstractMesh {
  1163. this.onAfterWorldMatrixUpdateObservable.removeCallback(func);
  1164. return this;
  1165. }
  1166. /**
  1167. * Sets the mesh position in its local space.
  1168. * Returns the AbstractMesh.
  1169. */
  1170. public setPositionWithLocalVector(vector3: Vector3): AbstractMesh {
  1171. this.computeWorldMatrix();
  1172. this.position = Vector3.TransformNormal(vector3, this._localWorld);
  1173. return this;
  1174. }
  1175. /**
  1176. * Returns the mesh position in the local space from the current World matrix values.
  1177. * Returns a new Vector3.
  1178. */
  1179. public getPositionExpressedInLocalSpace(): Vector3 {
  1180. this.computeWorldMatrix();
  1181. var invLocalWorldMatrix = this._localWorld.clone();
  1182. invLocalWorldMatrix.invert();
  1183. return Vector3.TransformNormal(this.position, invLocalWorldMatrix);
  1184. }
  1185. /**
  1186. * Translates the mesh along the passed Vector3 in its local space.
  1187. * Returns the AbstractMesh.
  1188. */
  1189. public locallyTranslate(vector3: Vector3): AbstractMesh {
  1190. this.computeWorldMatrix(true);
  1191. this.position = Vector3.TransformCoordinates(vector3, this._localWorld);
  1192. return this;
  1193. }
  1194. private static _lookAtVectorCache = new Vector3(0, 0, 0);
  1195. public lookAt(targetPoint: Vector3, yawCor: number = 0, pitchCor: number = 0, rollCor: number = 0, space: Space = Space.LOCAL): AbstractMesh {
  1196. /// <summary>Orients a mesh towards a target point. Mesh must be drawn facing user.</summary>
  1197. /// <param name="targetPoint" type="Vector3">The position (must be in same space as current mesh) to look at</param>
  1198. /// <param name="yawCor" type="Number">optional yaw (y-axis) correction in radians</param>
  1199. /// <param name="pitchCor" type="Number">optional pitch (x-axis) correction in radians</param>
  1200. /// <param name="rollCor" type="Number">optional roll (z-axis) correction in radians</param>
  1201. /// <returns>Mesh oriented towards targetMesh</returns>
  1202. var dv = AbstractMesh._lookAtVectorCache;
  1203. var pos = space === Space.LOCAL ? this.position : this.getAbsolutePosition();
  1204. targetPoint.subtractToRef(pos, dv);
  1205. var yaw = -Math.atan2(dv.z, dv.x) - Math.PI / 2;
  1206. var len = Math.sqrt(dv.x * dv.x + dv.z * dv.z);
  1207. var pitch = Math.atan2(dv.y, len);
  1208. this.rotationQuaternion = this.rotationQuaternion || new Quaternion();
  1209. Quaternion.RotationYawPitchRollToRef(yaw + yawCor, pitch + pitchCor, rollCor, this.rotationQuaternion);
  1210. return this;
  1211. }
  1212. public attachToBone(bone: Bone, affectedMesh: AbstractMesh): AbstractMesh {
  1213. this._meshToBoneReferal = affectedMesh;
  1214. this.parent = bone;
  1215. if (bone.getWorldMatrix().determinant() < 0) {
  1216. this.scalingDeterminant *= -1;
  1217. }
  1218. return this;
  1219. }
  1220. public detachFromBone(): AbstractMesh {
  1221. if (this.parent.getWorldMatrix().determinant() < 0) {
  1222. this.scalingDeterminant *= -1;
  1223. }
  1224. this._meshToBoneReferal = null;
  1225. this.parent = null;
  1226. return this;
  1227. }
  1228. /**
  1229. * Returns `true` if the mesh is within the frustum defined by the passed array of planes.
  1230. * A mesh is in the frustum if its bounding box intersects the frustum.
  1231. * Boolean returned.
  1232. */
  1233. public isInFrustum(frustumPlanes: Plane[]): boolean {
  1234. return this._boundingInfo.isInFrustum(frustumPlanes);
  1235. }
  1236. /**
  1237. * Returns `true` if the mesh is completely in the frustum defined be the passed array of planes.
  1238. * A mesh is completely in the frustum if its bounding box it completely inside the frustum.
  1239. * Boolean returned.
  1240. */
  1241. public isCompletelyInFrustum(frustumPlanes: Plane[]): boolean {
  1242. return this._boundingInfo.isCompletelyInFrustum(frustumPlanes);;
  1243. }
  1244. /**
  1245. * True if the mesh intersects another mesh or a SolidParticle object.
  1246. * Unless the parameter `precise` is set to `true` the intersection is computed according to Axis Aligned Bounding Boxes (AABB), else according to OBB (Oriented BBoxes)
  1247. * Returns a boolean.
  1248. */
  1249. public intersectsMesh(mesh: AbstractMesh | SolidParticle, precise?: boolean): boolean {
  1250. if (!this._boundingInfo || !mesh._boundingInfo) {
  1251. return false;
  1252. }
  1253. return this._boundingInfo.intersects(mesh._boundingInfo, precise);
  1254. }
  1255. /**
  1256. * Returns true if the passed point (Vector3) is inside the mesh bounding box.
  1257. * Returns a boolean.
  1258. */
  1259. public intersectsPoint(point: Vector3): boolean {
  1260. if (!this._boundingInfo) {
  1261. return false;
  1262. }
  1263. return this._boundingInfo.intersectsPoint(point);
  1264. }
  1265. public getPhysicsImpostor(): PhysicsImpostor {
  1266. return this.physicsImpostor;
  1267. }
  1268. public getPositionInCameraSpace(camera?: Camera): Vector3 {
  1269. if (!camera) {
  1270. camera = this.getScene().activeCamera;
  1271. }
  1272. return Vector3.TransformCoordinates(this.absolutePosition, camera.getViewMatrix());
  1273. }
  1274. /**
  1275. * Returns the distance from the mesh to the active camera.
  1276. * Returns a float.
  1277. */
  1278. public getDistanceToCamera(camera?: Camera): number {
  1279. if (!camera) {
  1280. camera = this.getScene().activeCamera;
  1281. }
  1282. return this.absolutePosition.subtract(camera.position).length();
  1283. }
  1284. public applyImpulse(force: Vector3, contactPoint: Vector3): AbstractMesh {
  1285. if (!this.physicsImpostor) {
  1286. return;
  1287. }
  1288. this.physicsImpostor.applyImpulse(force, contactPoint);
  1289. return this;
  1290. }
  1291. public setPhysicsLinkWith(otherMesh: Mesh, pivot1: Vector3, pivot2: Vector3, options?: any): AbstractMesh {
  1292. if (!this.physicsImpostor || !otherMesh.physicsImpostor) {
  1293. return;
  1294. }
  1295. this.physicsImpostor.createJoint(otherMesh.physicsImpostor, PhysicsJoint.HingeJoint, {
  1296. mainPivot: pivot1,
  1297. connectedPivot: pivot2,
  1298. nativeParams: options
  1299. });
  1300. return this;
  1301. }
  1302. // Collisions
  1303. /**
  1304. * Property checkCollisions : Boolean, whether the camera should check the collisions against the mesh.
  1305. * Default `false`.
  1306. */
  1307. public get checkCollisions(): boolean {
  1308. return this._checkCollisions;
  1309. }
  1310. public set checkCollisions(collisionEnabled: boolean) {
  1311. this._checkCollisions = collisionEnabled;
  1312. if (this.getScene().workerCollisions) {
  1313. this.getScene().collisionCoordinator.onMeshUpdated(this);
  1314. }
  1315. }
  1316. public moveWithCollisions(direction: Vector3): AbstractMesh {
  1317. var globalPosition = this.getAbsolutePosition();
  1318. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPositionForCollisions);
  1319. this._oldPositionForCollisions.addInPlace(this.ellipsoidOffset);
  1320. if (!this._collider) {
  1321. this._collider = new Collider();
  1322. }
  1323. this._collider.radius = this.ellipsoid;
  1324. this.getScene().collisionCoordinator.getNewPosition(this._oldPositionForCollisions, direction, this._collider, 3, this, this._onCollisionPositionChange, this.uniqueId);
  1325. return this;
  1326. }
  1327. private _onCollisionPositionChange = (collisionId: number, newPosition: Vector3, collidedMesh: AbstractMesh = null) => {
  1328. //TODO move this to the collision coordinator!
  1329. if (this.getScene().workerCollisions)
  1330. newPosition.multiplyInPlace(this._collider.radius);
  1331. newPosition.subtractToRef(this._oldPositionForCollisions, this._diffPositionForCollisions);
  1332. if (this._diffPositionForCollisions.length() > Engine.CollisionsEpsilon) {
  1333. this.position.addInPlace(this._diffPositionForCollisions);
  1334. }
  1335. if (collidedMesh) {
  1336. this.onCollideObservable.notifyObservers(collidedMesh);
  1337. }
  1338. this.onCollisionPositionChangeObservable.notifyObservers(this.position);
  1339. }
  1340. // Submeshes octree
  1341. /**
  1342. * This function will create an octree to help to select the right submeshes for rendering, picking and collision computations.
  1343. * Please note that you must have a decent number of submeshes to get performance improvements when using an octree.
  1344. * Returns an Octree of submeshes.
  1345. */
  1346. public createOrUpdateSubmeshesOctree(maxCapacity = 64, maxDepth = 2): Octree<SubMesh> {
  1347. if (!this._submeshesOctree) {
  1348. this._submeshesOctree = new Octree<SubMesh>(Octree.CreationFuncForSubMeshes, maxCapacity, maxDepth);
  1349. }
  1350. this.computeWorldMatrix(true);
  1351. // Update octree
  1352. var bbox = this.getBoundingInfo().boundingBox;
  1353. this._submeshesOctree.update(bbox.minimumWorld, bbox.maximumWorld, this.subMeshes);
  1354. return this._submeshesOctree;
  1355. }
  1356. // Collisions
  1357. public _collideForSubMesh(subMesh: SubMesh, transformMatrix: Matrix, collider: Collider): AbstractMesh {
  1358. this._generatePointsArray();
  1359. // Transformation
  1360. if (!subMesh._lastColliderWorldVertices || !subMesh._lastColliderTransformMatrix.equals(transformMatrix)) {
  1361. subMesh._lastColliderTransformMatrix = transformMatrix.clone();
  1362. subMesh._lastColliderWorldVertices = [];
  1363. subMesh._trianglePlanes = [];
  1364. var start = subMesh.verticesStart;
  1365. var end = (subMesh.verticesStart + subMesh.verticesCount);
  1366. for (var i = start; i < end; i++) {
  1367. subMesh._lastColliderWorldVertices.push(Vector3.TransformCoordinates(this._positions[i], transformMatrix));
  1368. }
  1369. }
  1370. // Collide
  1371. collider._collide(subMesh._trianglePlanes, subMesh._lastColliderWorldVertices, this.getIndices(), subMesh.indexStart, subMesh.indexStart + subMesh.indexCount, subMesh.verticesStart, !!subMesh.getMaterial());
  1372. if (collider.collisionFound) {
  1373. collider.collidedMesh = this;
  1374. }
  1375. return this;
  1376. }
  1377. public _processCollisionsForSubMeshes(collider: Collider, transformMatrix: Matrix): AbstractMesh {
  1378. var subMeshes: SubMesh[];
  1379. var len: number;
  1380. // Octrees
  1381. if (this._submeshesOctree && this.useOctreeForCollisions) {
  1382. var radius = collider.velocityWorldLength + Math.max(collider.radius.x, collider.radius.y, collider.radius.z);
  1383. var intersections = this._submeshesOctree.intersects(collider.basePointWorld, radius);
  1384. len = intersections.length;
  1385. subMeshes = intersections.data;
  1386. } else {
  1387. subMeshes = this.subMeshes;
  1388. len = subMeshes.length;
  1389. }
  1390. for (var index = 0; index < len; index++) {
  1391. var subMesh = subMeshes[index];
  1392. // Bounding test
  1393. if (len > 1 && !subMesh._checkCollision(collider))
  1394. continue;
  1395. this._collideForSubMesh(subMesh, transformMatrix, collider);
  1396. }
  1397. return this;
  1398. }
  1399. public _checkCollision(collider: Collider): AbstractMesh {
  1400. // Bounding box test
  1401. if (!this._boundingInfo._checkCollision(collider))
  1402. return this;
  1403. // Transformation matrix
  1404. Matrix.ScalingToRef(1.0 / collider.radius.x, 1.0 / collider.radius.y, 1.0 / collider.radius.z, this._collisionsScalingMatrix);
  1405. this.worldMatrixFromCache.multiplyToRef(this._collisionsScalingMatrix, this._collisionsTransformMatrix);
  1406. this._processCollisionsForSubMeshes(collider, this._collisionsTransformMatrix);
  1407. return this;
  1408. }
  1409. // Picking
  1410. public _generatePointsArray(): boolean {
  1411. return false;
  1412. }
  1413. /**
  1414. * Checks if the passed Ray intersects with the mesh.
  1415. * Returns an object PickingInfo.
  1416. */
  1417. public intersects(ray: Ray, fastCheck?: boolean): PickingInfo {
  1418. var pickingInfo = new PickingInfo();
  1419. if (!this.subMeshes || !this._boundingInfo || !ray.intersectsSphere(this._boundingInfo.boundingSphere) || !ray.intersectsBox(this._boundingInfo.boundingBox)) {
  1420. return pickingInfo;
  1421. }
  1422. if (!this._generatePointsArray()) {
  1423. return pickingInfo;
  1424. }
  1425. var intersectInfo: IntersectionInfo = null;
  1426. // Octrees
  1427. var subMeshes: SubMesh[];
  1428. var len: number;
  1429. if (this._submeshesOctree && this.useOctreeForPicking) {
  1430. var worldRay = Ray.Transform(ray, this.getWorldMatrix());
  1431. var intersections = this._submeshesOctree.intersectsRay(worldRay);
  1432. len = intersections.length;
  1433. subMeshes = intersections.data;
  1434. } else {
  1435. subMeshes = this.subMeshes;
  1436. len = subMeshes.length;
  1437. }
  1438. for (var index = 0; index < len; index++) {
  1439. var subMesh = subMeshes[index];
  1440. // Bounding test
  1441. if (len > 1 && !subMesh.canIntersects(ray))
  1442. continue;
  1443. var currentIntersectInfo = subMesh.intersects(ray, this._positions, this.getIndices(), fastCheck);
  1444. if (currentIntersectInfo) {
  1445. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  1446. intersectInfo = currentIntersectInfo;
  1447. intersectInfo.subMeshId = index;
  1448. if (fastCheck) {
  1449. break;
  1450. }
  1451. }
  1452. }
  1453. }
  1454. if (intersectInfo) {
  1455. // Get picked point
  1456. var world = this.getWorldMatrix();
  1457. var worldOrigin = Vector3.TransformCoordinates(ray.origin, world);
  1458. var direction = ray.direction.clone();
  1459. direction = direction.scale(intersectInfo.distance);
  1460. var worldDirection = Vector3.TransformNormal(direction, world);
  1461. var pickedPoint = worldOrigin.add(worldDirection);
  1462. // Return result
  1463. pickingInfo.hit = true;
  1464. pickingInfo.distance = Vector3.Distance(worldOrigin, pickedPoint);
  1465. pickingInfo.pickedPoint = pickedPoint;
  1466. pickingInfo.pickedMesh = this;
  1467. pickingInfo.bu = intersectInfo.bu;
  1468. pickingInfo.bv = intersectInfo.bv;
  1469. pickingInfo.faceId = intersectInfo.faceId;
  1470. pickingInfo.subMeshId = intersectInfo.subMeshId;
  1471. return pickingInfo;
  1472. }
  1473. return pickingInfo;
  1474. }
  1475. /**
  1476. * Clones the mesh, used by the class Mesh.
  1477. * Just returns `null` for an AbstractMesh.
  1478. */
  1479. public clone(name: string, newParent: Node, doNotCloneChildren?: boolean): AbstractMesh {
  1480. return null;
  1481. }
  1482. /**
  1483. * Disposes all the mesh submeshes.
  1484. * Returns the AbstractMesh.
  1485. */
  1486. public releaseSubMeshes(): AbstractMesh {
  1487. if (this.subMeshes) {
  1488. while (this.subMeshes.length) {
  1489. this.subMeshes[0].dispose();
  1490. }
  1491. } else {
  1492. this.subMeshes = new Array<SubMesh>();
  1493. }
  1494. return this;
  1495. }
  1496. /**
  1497. * Disposes the AbstractMesh.
  1498. * Some internal references are kept for further use.
  1499. * By default, all the mesh children are also disposed unless the parameter `doNotRecurse` is set to `true`.
  1500. * Returns nothing.
  1501. */
  1502. public dispose(doNotRecurse?: boolean): void {
  1503. var index: number;
  1504. // Action manager
  1505. if (this.actionManager) {
  1506. this.actionManager.dispose();
  1507. this.actionManager = null;
  1508. }
  1509. // Skeleton
  1510. this.skeleton = null;
  1511. // Animations
  1512. this.getScene().stopAnimation(this);
  1513. // Physics
  1514. if (this.physicsImpostor) {
  1515. this.physicsImpostor.dispose(/*!doNotRecurse*/);
  1516. }
  1517. // Intersections in progress
  1518. for (index = 0; index < this._intersectionsInProgress.length; index++) {
  1519. var other = this._intersectionsInProgress[index];
  1520. var pos = other._intersectionsInProgress.indexOf(this);
  1521. other._intersectionsInProgress.splice(pos, 1);
  1522. }
  1523. this._intersectionsInProgress = [];
  1524. // Lights
  1525. var lights = this.getScene().lights;
  1526. lights.forEach((light: Light) => {
  1527. var meshIndex = light.includedOnlyMeshes.indexOf(this);
  1528. if (meshIndex !== -1) {
  1529. light.includedOnlyMeshes.splice(meshIndex, 1);
  1530. }
  1531. meshIndex = light.excludedMeshes.indexOf(this);
  1532. if (meshIndex !== -1) {
  1533. light.excludedMeshes.splice(meshIndex, 1);
  1534. }
  1535. // Shadow generators
  1536. var generator = light.getShadowGenerator();
  1537. if (generator) {
  1538. var shadowMap = generator.getShadowMap();
  1539. meshIndex = shadowMap.renderList.indexOf(this);
  1540. if (meshIndex !== -1) {
  1541. shadowMap.renderList.splice(meshIndex, 1);
  1542. }
  1543. }
  1544. });
  1545. // Edges
  1546. if (this._edgesRenderer) {
  1547. this._edgesRenderer.dispose();
  1548. this._edgesRenderer = null;
  1549. }
  1550. // SubMeshes
  1551. if (this.getClassName() !== "InstancedMesh") {
  1552. this.releaseSubMeshes();
  1553. }
  1554. // Octree
  1555. var sceneOctree = this.getScene().selectionOctree;
  1556. if (sceneOctree) {
  1557. var index = sceneOctree.dynamicContent.indexOf(this);
  1558. if (index !== -1) {
  1559. sceneOctree.dynamicContent.splice(index, 1);
  1560. }
  1561. }
  1562. // Query
  1563. let engine = this.getScene().getEngine();
  1564. if (this._occlusionQuery) {
  1565. this._isOcclusionQueryInProgress = false;
  1566. engine.deleteQuery(this._occlusionQuery);
  1567. this._occlusionQuery = null;
  1568. }
  1569. // Engine
  1570. engine.wipeCaches();
  1571. // Remove from scene
  1572. this.getScene().removeMesh(this);
  1573. if (!doNotRecurse) {
  1574. // Particles
  1575. for (index = 0; index < this.getScene().particleSystems.length; index++) {
  1576. if (this.getScene().particleSystems[index].emitter === this) {
  1577. this.getScene().particleSystems[index].dispose();
  1578. index--;
  1579. }
  1580. }
  1581. // Children
  1582. var objects = this.getDescendants(true);
  1583. for (index = 0; index < objects.length; index++) {
  1584. objects[index].dispose();
  1585. }
  1586. } else {
  1587. var childMeshes = this.getChildMeshes(true);
  1588. for (index = 0; index < childMeshes.length; index++) {
  1589. var child = childMeshes[index];
  1590. child.parent = null;
  1591. child.computeWorldMatrix(true);
  1592. }
  1593. }
  1594. // facet data
  1595. if (this._facetDataEnabled) {
  1596. this.disableFacetData();
  1597. }
  1598. this.onAfterWorldMatrixUpdateObservable.clear();
  1599. this.onCollideObservable.clear();
  1600. this.onCollisionPositionChangeObservable.clear();
  1601. this._isDisposed = true;
  1602. super.dispose();
  1603. }
  1604. /**
  1605. * Returns a new Vector3 what is the localAxis, expressed in the mesh local space, rotated like the mesh.
  1606. * This Vector3 is expressed in the World space.
  1607. */
  1608. public getDirection(localAxis: Vector3): Vector3 {
  1609. var result = Vector3.Zero();
  1610. this.getDirectionToRef(localAxis, result);
  1611. return result;
  1612. }
  1613. /**
  1614. * Sets the Vector3 "result" as the rotated Vector3 "localAxis" in the same rotation than the mesh.
  1615. * localAxis is expressed in the mesh local space.
  1616. * result is computed in the Wordl space from the mesh World matrix.
  1617. * Returns the AbstractMesh.
  1618. */
  1619. public getDirectionToRef(localAxis: Vector3, result: Vector3): AbstractMesh {
  1620. Vector3.TransformNormalToRef(localAxis, this.getWorldMatrix(), result);
  1621. return this;
  1622. }
  1623. public setPivotPoint(point: Vector3, space: Space = Space.LOCAL): AbstractMesh {
  1624. if (this.getScene().getRenderId() == 0) {
  1625. this.computeWorldMatrix(true);
  1626. }
  1627. var wm = this.getWorldMatrix();
  1628. if (space == Space.WORLD) {
  1629. var tmat = Tmp.Matrix[0];
  1630. wm.invertToRef(tmat);
  1631. point = Vector3.TransformCoordinates(point, tmat);
  1632. }
  1633. Vector3.TransformCoordinatesToRef(point, wm, this.position);
  1634. this._pivotMatrix.m[12] = -point.x;
  1635. this._pivotMatrix.m[13] = -point.y;
  1636. this._pivotMatrix.m[14] = -point.z;
  1637. this._cache.pivotMatrixUpdated = true;
  1638. return this;
  1639. }
  1640. /**
  1641. * Returns a new Vector3 set with the mesh pivot point coordinates in the local space.
  1642. */
  1643. public getPivotPoint(): Vector3 {
  1644. var point = Vector3.Zero();
  1645. this.getPivotPointToRef(point);
  1646. return point;
  1647. }
  1648. /**
  1649. * Sets the passed Vector3 "result" with the coordinates of the mesh pivot point in the local space.
  1650. * Returns the AbstractMesh.
  1651. */
  1652. public getPivotPointToRef(result: Vector3): AbstractMesh {
  1653. result.x = -this._pivotMatrix.m[12];
  1654. result.y = -this._pivotMatrix.m[13];
  1655. result.z = -this._pivotMatrix.m[14];
  1656. return this;
  1657. }
  1658. /**
  1659. * Returns a new Vector3 set with the mesh pivot point World coordinates.
  1660. */
  1661. public getAbsolutePivotPoint(): Vector3 {
  1662. var point = Vector3.Zero();
  1663. this.getAbsolutePivotPointToRef(point);
  1664. return point;
  1665. }
  1666. /**
  1667. * Defines the passed mesh as the parent of the current mesh.
  1668. * Returns the AbstractMesh.
  1669. */
  1670. public setParent(mesh: AbstractMesh): AbstractMesh {
  1671. var child = this;
  1672. var parent = mesh;
  1673. if (mesh == null) {
  1674. var rotation = Tmp.Quaternion[0];
  1675. var position = Tmp.Vector3[0];
  1676. var scale = Tmp.Vector3[1];
  1677. child.getWorldMatrix().decompose(scale, rotation, position);
  1678. if (child.rotationQuaternion) {
  1679. child.rotationQuaternion.copyFrom(rotation);
  1680. } else {
  1681. rotation.toEulerAnglesToRef(child.rotation);
  1682. }
  1683. child.position.x = position.x;
  1684. child.position.y = position.y;
  1685. child.position.z = position.z;
  1686. } else {
  1687. var rotation = Tmp.Quaternion[0];
  1688. var position = Tmp.Vector3[0];
  1689. var scale = Tmp.Vector3[1];
  1690. var m1 = Tmp.Matrix[0];
  1691. var m2 = Tmp.Matrix[1];
  1692. parent.getWorldMatrix().decompose(scale, rotation, position);
  1693. rotation.toRotationMatrix(m1);
  1694. m2.setTranslation(position);
  1695. m2.multiplyToRef(m1, m1);
  1696. var invParentMatrix = Matrix.Invert(m1);
  1697. var m = child.getWorldMatrix().multiply(invParentMatrix);
  1698. m.decompose(scale, rotation, position);
  1699. if (child.rotationQuaternion) {
  1700. child.rotationQuaternion.copyFrom(rotation);
  1701. } else {
  1702. rotation.toEulerAnglesToRef(child.rotation);
  1703. }
  1704. invParentMatrix = Matrix.Invert(parent.getWorldMatrix());
  1705. var m = child.getWorldMatrix().multiply(invParentMatrix);
  1706. m.decompose(scale, rotation, position);
  1707. child.position.x = position.x;
  1708. child.position.y = position.y;
  1709. child.position.z = position.z;
  1710. }
  1711. child.parent = parent;
  1712. return this;
  1713. }
  1714. /**
  1715. * Adds the passed mesh as a child to the current mesh.
  1716. * Returns the AbstractMesh.
  1717. */
  1718. public addChild(mesh: AbstractMesh): AbstractMesh {
  1719. mesh.setParent(this);
  1720. return this;
  1721. }
  1722. /**
  1723. * Removes the passed mesh from the current mesh children list.
  1724. * Returns the AbstractMesh.
  1725. */
  1726. public removeChild(mesh: AbstractMesh): AbstractMesh {
  1727. mesh.setParent(null);
  1728. return this;
  1729. }
  1730. /**
  1731. * Sets the Vector3 "result" coordinates with the mesh pivot point World coordinates.
  1732. * Returns the AbstractMesh.
  1733. */
  1734. public getAbsolutePivotPointToRef(result: Vector3): AbstractMesh {
  1735. result.x = this._pivotMatrix.m[12];
  1736. result.y = this._pivotMatrix.m[13];
  1737. result.z = this._pivotMatrix.m[14];
  1738. this.getPivotPointToRef(result);
  1739. Vector3.TransformCoordinatesToRef(result, this.getWorldMatrix(), result);
  1740. return this;
  1741. }
  1742. // Facet data
  1743. /**
  1744. * Initialize the facet data arrays : facetNormals, facetPositions and facetPartitioning.
  1745. * Returns the AbstractMesh.
  1746. */
  1747. private _initFacetData(): AbstractMesh {
  1748. if (!this._facetNormals) {
  1749. this._facetNormals = new Array<Vector3>();
  1750. }
  1751. if (!this._facetPositions) {
  1752. this._facetPositions = new Array<Vector3>();
  1753. }
  1754. if (!this._facetPartitioning) {
  1755. this._facetPartitioning = new Array<number[]>();
  1756. }
  1757. this._facetNb = this.getIndices().length / 3;
  1758. this._partitioningSubdivisions = (this._partitioningSubdivisions) ? this._partitioningSubdivisions : 10; // default nb of partitioning subdivisions = 10
  1759. this._partitioningBBoxRatio = (this._partitioningBBoxRatio) ? this._partitioningBBoxRatio : 1.01; // default ratio 1.01 = the partitioning is 1% bigger than the bounding box
  1760. for (var f = 0; f < this._facetNb; f++) {
  1761. this._facetNormals[f] = Vector3.Zero();
  1762. this._facetPositions[f] = Vector3.Zero();
  1763. }
  1764. this._facetDataEnabled = true;
  1765. return this;
  1766. }
  1767. /**
  1768. * Updates the mesh facetData arrays and the internal partitioning when the mesh is morphed or updated.
  1769. * This method can be called within the render loop.
  1770. * You don't need to call this method by yourself in the render loop when you update/morph a mesh with the methods CreateXXX() as they automatically manage this computation.
  1771. * Returns the AbstractMesh.
  1772. */
  1773. public updateFacetData(): AbstractMesh {
  1774. if (!this._facetDataEnabled) {
  1775. this._initFacetData();
  1776. }
  1777. var positions = this.getVerticesData(VertexBuffer.PositionKind);
  1778. var indices = this.getIndices();
  1779. var normals = this.getVerticesData(VertexBuffer.NormalKind);
  1780. var bInfo = this.getBoundingInfo();
  1781. this._bbSize.x = (bInfo.maximum.x - bInfo.minimum.x > Epsilon) ? bInfo.maximum.x - bInfo.minimum.x : Epsilon;
  1782. this._bbSize.y = (bInfo.maximum.y - bInfo.minimum.y > Epsilon) ? bInfo.maximum.y - bInfo.minimum.y : Epsilon;
  1783. this._bbSize.z = (bInfo.maximum.z - bInfo.minimum.z > Epsilon) ? bInfo.maximum.z - bInfo.minimum.z : Epsilon;
  1784. var bbSizeMax = (this._bbSize.x > this._bbSize.y) ? this._bbSize.x : this._bbSize.y;
  1785. bbSizeMax = (bbSizeMax > this._bbSize.z) ? bbSizeMax : this._bbSize.z;
  1786. this._subDiv.max = this._partitioningSubdivisions;
  1787. this._subDiv.X = Math.floor(this._subDiv.max * this._bbSize.x / bbSizeMax); // adjust the number of subdivisions per axis
  1788. this._subDiv.Y = Math.floor(this._subDiv.max * this._bbSize.y / bbSizeMax); // according to each bbox size per axis
  1789. this._subDiv.Z = Math.floor(this._subDiv.max * this._bbSize.z / bbSizeMax);
  1790. this._subDiv.X = this._subDiv.X < 1 ? 1 : this._subDiv.X; // at least one subdivision
  1791. this._subDiv.Y = this._subDiv.Y < 1 ? 1 : this._subDiv.Y;
  1792. this._subDiv.Z = this._subDiv.Z < 1 ? 1 : this._subDiv.Z;
  1793. // set the parameters for ComputeNormals()
  1794. this._facetParameters.facetNormals = this.getFacetLocalNormals();
  1795. this._facetParameters.facetPositions = this.getFacetLocalPositions();
  1796. this._facetParameters.facetPartitioning = this.getFacetLocalPartitioning();
  1797. this._facetParameters.bInfo = bInfo;
  1798. this._facetParameters.bbSize = this._bbSize;
  1799. this._facetParameters.subDiv = this._subDiv;
  1800. this._facetParameters.ratio = this.partitioningBBoxRatio;
  1801. VertexData.ComputeNormals(positions, indices, normals, this._facetParameters);
  1802. return this;
  1803. }
  1804. /**
  1805. * Returns the facetLocalNormals array.
  1806. * The normals are expressed in the mesh local space.
  1807. */
  1808. public getFacetLocalNormals(): Vector3[] {
  1809. if (!this._facetNormals) {
  1810. this.updateFacetData();
  1811. }
  1812. return this._facetNormals;
  1813. }
  1814. /**
  1815. * Returns the facetLocalPositions array.
  1816. * The facet positions are expressed in the mesh local space.
  1817. */
  1818. public getFacetLocalPositions(): Vector3[] {
  1819. if (!this._facetPositions) {
  1820. this.updateFacetData();
  1821. }
  1822. return this._facetPositions;
  1823. }
  1824. /**
  1825. * Returns the facetLocalPartioning array.
  1826. */
  1827. public getFacetLocalPartitioning(): number[][] {
  1828. if (!this._facetPartitioning) {
  1829. this.updateFacetData();
  1830. }
  1831. return this._facetPartitioning;
  1832. }
  1833. /**
  1834. * Returns the i-th facet position in the world system.
  1835. * This method allocates a new Vector3 per call.
  1836. */
  1837. public getFacetPosition(i: number): Vector3 {
  1838. var pos = Vector3.Zero();
  1839. this.getFacetPositionToRef(i, pos);
  1840. return pos;
  1841. }
  1842. /**
  1843. * Sets the reference Vector3 with the i-th facet position in the world system.
  1844. * Returns the AbstractMesh.
  1845. */
  1846. public getFacetPositionToRef(i: number, ref: Vector3): AbstractMesh {
  1847. var localPos = (this.getFacetLocalPositions())[i];
  1848. var world = this.getWorldMatrix();
  1849. Vector3.TransformCoordinatesToRef(localPos, world, ref);
  1850. return this;
  1851. }
  1852. /**
  1853. * Returns the i-th facet normal in the world system.
  1854. * This method allocates a new Vector3 per call.
  1855. */
  1856. public getFacetNormal(i: number): Vector3 {
  1857. var norm = Vector3.Zero();
  1858. this.getFacetNormalToRef(i, norm);
  1859. return norm;
  1860. }
  1861. /**
  1862. * Sets the reference Vector3 with the i-th facet normal in the world system.
  1863. * Returns the AbstractMesh.
  1864. */
  1865. public getFacetNormalToRef(i: number, ref: Vector3) {
  1866. var localNorm = (this.getFacetLocalNormals())[i];
  1867. Vector3.TransformNormalToRef(localNorm, this.getWorldMatrix(), ref);
  1868. return this;
  1869. }
  1870. /**
  1871. * Returns the facets (in an array) in the same partitioning block than the one the passed coordinates are located (expressed in the mesh local system).
  1872. */
  1873. public getFacetsAtLocalCoordinates(x: number, y: number, z: number): number[] {
  1874. var bInfo = this.getBoundingInfo();
  1875. var ox = Math.floor((x - bInfo.minimum.x * this._partitioningBBoxRatio) * this._subDiv.X * this._partitioningBBoxRatio / this._bbSize.x);
  1876. var oy = Math.floor((y - bInfo.minimum.y * this._partitioningBBoxRatio) * this._subDiv.Y * this._partitioningBBoxRatio / this._bbSize.y);
  1877. var oz = Math.floor((z - bInfo.minimum.z * this._partitioningBBoxRatio) * this._subDiv.Z * this._partitioningBBoxRatio / this._bbSize.z);
  1878. if (ox < 0 || ox > this._subDiv.max || oy < 0 || oy > this._subDiv.max || oz < 0 || oz > this._subDiv.max) {
  1879. return null;
  1880. }
  1881. return this._facetPartitioning[ox + this._subDiv.max * oy + this._subDiv.max * this._subDiv.max * oz];
  1882. }
  1883. /**
  1884. * Returns the closest mesh facet index at (x,y,z) World coordinates, null if not found.
  1885. * If the parameter projected (vector3) is passed, it is set as the (x,y,z) World projection on the facet.
  1886. * If checkFace is true (default false), only the facet "facing" to (x,y,z) or only the ones "turning their backs", according to the parameter "facing" are returned.
  1887. * If facing and checkFace are true, only the facet "facing" to (x, y, z) are returned : positive dot (x, y, z) * facet position.
  1888. * If facing si false and checkFace is true, only the facet "turning their backs" to (x, y, z) are returned : negative dot (x, y, z) * facet position.
  1889. */
  1890. public getClosestFacetAtCoordinates(x: number, y: number, z: number, projected?: Vector3, checkFace: boolean = false, facing: boolean = true): number {
  1891. var world = this.getWorldMatrix();
  1892. var invMat = Tmp.Matrix[5];
  1893. world.invertToRef(invMat);
  1894. var invVect = Tmp.Vector3[8];
  1895. var closest = null;
  1896. Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, invMat, invVect); // transform (x,y,z) to coordinates in the mesh local space
  1897. closest = this.getClosestFacetAtLocalCoordinates(invVect.x, invVect.y, invVect.z, projected, checkFace, facing);
  1898. if (projected) {
  1899. // tranform the local computed projected vector to world coordinates
  1900. Vector3.TransformCoordinatesFromFloatsToRef(projected.x, projected.y, projected.z, world, projected);
  1901. }
  1902. return closest;
  1903. }
  1904. /**
  1905. * Returns the closest mesh facet index at (x,y,z) local coordinates, null if not found.
  1906. * If the parameter projected (vector3) is passed, it is set as the (x,y,z) local projection on the facet.
  1907. * If checkFace is true (default false), only the facet "facing" to (x,y,z) or only the ones "turning their backs", according to the parameter "facing" are returned.
  1908. * If facing and checkFace are true, only the facet "facing" to (x, y, z) are returned : positive dot (x, y, z) * facet position.
  1909. * If facing si false and checkFace is true, only the facet "turning their backs" to (x, y, z) are returned : negative dot (x, y, z) * facet position.
  1910. */
  1911. public getClosestFacetAtLocalCoordinates(x: number, y: number, z: number, projected?: Vector3, checkFace: boolean = false, facing: boolean = true): number {
  1912. var closest = null;
  1913. var tmpx = 0.0;
  1914. var tmpy = 0.0;
  1915. var tmpz = 0.0;
  1916. var d = 0.0; // tmp dot facet normal * facet position
  1917. var t0 = 0.0;
  1918. var projx = 0.0;
  1919. var projy = 0.0;
  1920. var projz = 0.0;
  1921. // Get all the facets in the same partitioning block than (x, y, z)
  1922. var facetPositions = this.getFacetLocalPositions();
  1923. var facetNormals = this.getFacetLocalNormals();
  1924. var facetsInBlock = this.getFacetsAtLocalCoordinates(x, y, z);
  1925. if (!facetsInBlock) {
  1926. return null;
  1927. }
  1928. // Get the closest facet to (x, y, z)
  1929. var shortest = Number.MAX_VALUE; // init distance vars
  1930. var tmpDistance = shortest;
  1931. var fib; // current facet in the block
  1932. var norm; // current facet normal
  1933. var p0; // current facet barycenter position
  1934. // loop on all the facets in the current partitioning block
  1935. for (var idx = 0; idx < facetsInBlock.length; idx++) {
  1936. fib = facetsInBlock[idx];
  1937. norm = facetNormals[fib];
  1938. p0 = facetPositions[fib];
  1939. d = (x - p0.x) * norm.x + (y - p0.y) * norm.y + (z - p0.z) * norm.z;
  1940. if (!checkFace || (checkFace && facing && d >= 0.0) || (checkFace && !facing && d <= 0.0)) {
  1941. // compute (x,y,z) projection on the facet = (projx, projy, projz)
  1942. d = norm.x * p0.x + norm.y * p0.y + norm.z * p0.z;
  1943. t0 = -(norm.x * x + norm.y * y + norm.z * z - d) / (norm.x * norm.x + norm.y * norm.y + norm.z * norm.z);
  1944. projx = x + norm.x * t0;
  1945. projy = y + norm.y * t0;
  1946. projz = z + norm.z * t0;
  1947. tmpx = projx - x;
  1948. tmpy = projy - y;
  1949. tmpz = projz - z;
  1950. tmpDistance = tmpx * tmpx + tmpy * tmpy + tmpz * tmpz; // compute length between (x, y, z) and its projection on the facet
  1951. if (tmpDistance < shortest) { // just keep the closest facet to (x, y, z)
  1952. shortest = tmpDistance;
  1953. closest = fib;
  1954. if (projected) {
  1955. projected.x = projx;
  1956. projected.y = projy;
  1957. projected.z = projz;
  1958. }
  1959. }
  1960. }
  1961. }
  1962. return closest;
  1963. }
  1964. /**
  1965. * Returns the object "parameter" set with all the expected parameters for facetData computation by ComputeNormals()
  1966. */
  1967. public getFacetDataParameters(): any {
  1968. return this._facetParameters;
  1969. }
  1970. /**
  1971. * Disables the feature FacetData and frees the related memory.
  1972. * Returns the AbstractMesh.
  1973. */
  1974. public disableFacetData(): AbstractMesh {
  1975. if (this._facetDataEnabled) {
  1976. this._facetDataEnabled = false;
  1977. this._facetPositions = null;
  1978. this._facetNormals = null;
  1979. this._facetPartitioning = null;
  1980. this._facetParameters = null;
  1981. }
  1982. return this;
  1983. }
  1984. /**
  1985. * Creates new normals data for the mesh.
  1986. * @param updatable.
  1987. */
  1988. public createNormals(updatable: boolean) {
  1989. var positions = this.getVerticesData(VertexBuffer.PositionKind);
  1990. var indices = this.getIndices();
  1991. var normals: number[] | Float32Array;
  1992. if (this.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  1993. normals = this.getVerticesData(VertexBuffer.NormalKind);
  1994. } else {
  1995. normals = [];
  1996. }
  1997. VertexData.ComputeNormals(positions, indices, normals, { useRightHandedSystem: this.getScene().useRightHandedSystem });
  1998. this.setVerticesData(VertexBuffer.NormalKind, normals, updatable);
  1999. }
  2000. protected checkOcclusionQuery() {
  2001. var engine = this.getEngine();
  2002. if (engine.webGLVersion < 2 || this.occlusionType === AbstractMesh.OCCLUSION_TYPE_NONE) {
  2003. this._isOccluded = false;
  2004. return;
  2005. }
  2006. if (this.isOcclusionQueryInProgress) {
  2007. var isOcclusionQueryAvailable = engine.isQueryResultAvailable(this._occlusionQuery);
  2008. if (isOcclusionQueryAvailable) {
  2009. var occlusionQueryResult = engine.getQueryResult(this._occlusionQuery);
  2010. this._isOcclusionQueryInProgress = false;
  2011. this._occlusionInternalRetryCounter = 0;
  2012. this._isOccluded = occlusionQueryResult === 1 ? false : true;
  2013. }
  2014. else {
  2015. this._occlusionInternalRetryCounter++;
  2016. if (this.occlusionRetryCount !== -1 && this._occlusionInternalRetryCounter > this.occlusionRetryCount) {
  2017. this._isOcclusionQueryInProgress = false;
  2018. this._occlusionInternalRetryCounter = 0;
  2019. // if optimistic set isOccluded to false regardless of the status of isOccluded. (Render in the current render loop)
  2020. // if strict continue the last state of the object.
  2021. this._isOccluded = this.occlusionType === AbstractMesh.OCCLUSION_TYPE_OPTIMISTIC ? false : this._isOccluded;
  2022. }
  2023. else {
  2024. return;
  2025. }
  2026. }
  2027. }
  2028. var scene = this.getScene();
  2029. var occlusionBoundingBoxRenderer = scene.getBoundingBoxRenderer();
  2030. if (!this._occlusionQuery) {
  2031. this._occlusionQuery = engine.createQuery();
  2032. }
  2033. engine.beginQuery(this.occlusionQueryAlgorithmType, this._occlusionQuery);
  2034. occlusionBoundingBoxRenderer.renderOcclusionBoundingBox(this);
  2035. engine.endQuery(this.occlusionQueryAlgorithmType);
  2036. this._isOcclusionQueryInProgress = true;
  2037. }
  2038. }
  2039. }