123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662 |
- var __extends = this.__extends || function (d, b) {
- for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
- function __() { this.constructor = d; }
- __.prototype = b.prototype;
- d.prototype = new __();
- };
- var BABYLON;
- (function (BABYLON) {
- var _InstancesBatch = (function () {
- function _InstancesBatch() {
- this.mustReturn = false;
- this.visibleInstances = new Array();
- this.renderSelf = new Array();
- }
- return _InstancesBatch;
- })();
- BABYLON._InstancesBatch = _InstancesBatch;
- var Mesh = (function (_super) {
- __extends(Mesh, _super);
- /**
- * @constructor
- * @param {string} name - The value used by scene.getMeshByName() to do a lookup.
- * @param {Scene} scene - The scene to add this mesh to.
- * @param {Node} parent - The parent of this mesh, if it has one
- * @param {Mesh} source - An optional Mesh from which geometry is shared, cloned.
- * @param {boolean} doNotCloneChildren - When cloning, skip cloning child meshes of source, default False.
- * When false, achieved by calling a clone(), also passing False.
- * This will make creation of children, recursive.
- */
- function Mesh(name, scene, parent, source, doNotCloneChildren) {
- if (parent === void 0) { parent = null; }
- _super.call(this, name, scene);
- // Members
- this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
- this.instances = new Array();
- this._LODLevels = new Array();
- this._onBeforeRenderCallbacks = new Array();
- this._onAfterRenderCallbacks = new Array();
- this._visibleInstances = {};
- this._renderIdForInstances = new Array();
- this._batchCache = new _InstancesBatch();
- this._instancesBufferSize = 32 * 16 * 4; // let's start with a maximum of 32 instances
- this._sideOrientation = Mesh._DEFAULTSIDE;
- this._areNormalsFrozen = false; // Will be used by ribbons mainly
- if (source) {
- // Geometry
- if (source._geometry) {
- source._geometry.applyToMesh(this);
- }
- // Deep copy
- BABYLON.Tools.DeepCopy(source, this, ["name", "material", "skeleton", "instances"], []);
- // Material
- this.material = source.material;
- if (!doNotCloneChildren) {
- for (var index = 0; index < scene.meshes.length; index++) {
- var mesh = scene.meshes[index];
- if (mesh.parent === source) {
- // doNotCloneChildren is always going to be False
- var newChild = mesh.clone(name + "." + mesh.name, this, doNotCloneChildren);
- }
- }
- }
- for (index = 0; index < scene.particleSystems.length; index++) {
- var system = scene.particleSystems[index];
- if (system.emitter === source) {
- system.clone(system.name, this);
- }
- }
- this.computeWorldMatrix(true);
- }
- // Parent
- if (parent !== null) {
- this.parent = parent;
- }
- }
- Object.defineProperty(Mesh, "FRONTSIDE", {
- get: function () {
- return Mesh._FRONTSIDE;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(Mesh, "BACKSIDE", {
- get: function () {
- return Mesh._BACKSIDE;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(Mesh, "DOUBLESIDE", {
- get: function () {
- return Mesh._DOUBLESIDE;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(Mesh, "DEFAULTSIDE", {
- get: function () {
- return Mesh._DEFAULTSIDE;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(Mesh, "NO_CAP", {
- get: function () {
- return Mesh._NO_CAP;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(Mesh, "CAP_START", {
- get: function () {
- return Mesh._CAP_START;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(Mesh, "CAP_END", {
- get: function () {
- return Mesh._CAP_END;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(Mesh, "CAP_ALL", {
- get: function () {
- return Mesh._CAP_ALL;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(Mesh.prototype, "hasLODLevels", {
- // Methods
- get: function () {
- return this._LODLevels.length > 0;
- },
- enumerable: true,
- configurable: true
- });
- Mesh.prototype._sortLODLevels = function () {
- this._LODLevels.sort(function (a, b) {
- if (a.distance < b.distance) {
- return 1;
- }
- if (a.distance > b.distance) {
- return -1;
- }
- return 0;
- });
- };
- /**
- * Add a mesh as LOD level triggered at the given distance.
- * @param {number} distance - the distance from the center of the object to show this level
- * @param {BABYLON.Mesh} mesh - the mesh to be added as LOD level
- * @return {BABYLON.Mesh} this mesh (for chaining)
- */
- Mesh.prototype.addLODLevel = function (distance, mesh) {
- if (mesh && mesh._masterMesh) {
- BABYLON.Tools.Warn("You cannot use a mesh as LOD level twice");
- return this;
- }
- var level = new BABYLON.Internals.MeshLODLevel(distance, mesh);
- this._LODLevels.push(level);
- if (mesh) {
- mesh._masterMesh = this;
- }
- this._sortLODLevels();
- return this;
- };
- Mesh.prototype.getLODLevelAtDistance = function (distance) {
- for (var index = 0; index < this._LODLevels.length; index++) {
- var level = this._LODLevels[index];
- if (level.distance === distance) {
- return level.mesh;
- }
- }
- return null;
- };
- /**
- * Remove a mesh from the LOD array
- * @param {BABYLON.Mesh} mesh - the mesh to be removed.
- * @return {BABYLON.Mesh} this mesh (for chaining)
- */
- Mesh.prototype.removeLODLevel = function (mesh) {
- for (var index = 0; index < this._LODLevels.length; index++) {
- if (this._LODLevels[index].mesh === mesh) {
- this._LODLevels.splice(index, 1);
- if (mesh) {
- mesh._masterMesh = null;
- }
- }
- }
- this._sortLODLevels();
- return this;
- };
- Mesh.prototype.getLOD = function (camera, boundingSphere) {
- if (!this._LODLevels || this._LODLevels.length === 0) {
- return this;
- }
- var distanceToCamera = (boundingSphere ? boundingSphere : this.getBoundingInfo().boundingSphere).centerWorld.subtract(camera.position).length();
- if (this._LODLevels[this._LODLevels.length - 1].distance > distanceToCamera) {
- if (this.onLODLevelSelection) {
- this.onLODLevelSelection(distanceToCamera, this, this._LODLevels[this._LODLevels.length - 1].mesh);
- }
- return this;
- }
- for (var index = 0; index < this._LODLevels.length; index++) {
- var level = this._LODLevels[index];
- if (level.distance < distanceToCamera) {
- if (level.mesh) {
- level.mesh._preActivate();
- level.mesh._updateSubMeshesBoundingInfo(this.worldMatrixFromCache);
- }
- if (this.onLODLevelSelection) {
- this.onLODLevelSelection(distanceToCamera, this, level.mesh);
- }
- return level.mesh;
- }
- }
- if (this.onLODLevelSelection) {
- this.onLODLevelSelection(distanceToCamera, this, this);
- }
- return this;
- };
- Object.defineProperty(Mesh.prototype, "geometry", {
- get: function () {
- return this._geometry;
- },
- enumerable: true,
- configurable: true
- });
- Mesh.prototype.getTotalVertices = function () {
- if (!this._geometry) {
- return 0;
- }
- return this._geometry.getTotalVertices();
- };
- Mesh.prototype.getVerticesData = function (kind, copyWhenShared) {
- if (!this._geometry) {
- return null;
- }
- return this._geometry.getVerticesData(kind, copyWhenShared);
- };
- Mesh.prototype.getVertexBuffer = function (kind) {
- if (!this._geometry) {
- return undefined;
- }
- return this._geometry.getVertexBuffer(kind);
- };
- Mesh.prototype.isVerticesDataPresent = function (kind) {
- if (!this._geometry) {
- if (this._delayInfo) {
- return this._delayInfo.indexOf(kind) !== -1;
- }
- return false;
- }
- return this._geometry.isVerticesDataPresent(kind);
- };
- Mesh.prototype.getVerticesDataKinds = function () {
- if (!this._geometry) {
- var result = [];
- if (this._delayInfo) {
- for (var kind in this._delayInfo) {
- result.push(kind);
- }
- }
- return result;
- }
- return this._geometry.getVerticesDataKinds();
- };
- Mesh.prototype.getTotalIndices = function () {
- if (!this._geometry) {
- return 0;
- }
- return this._geometry.getTotalIndices();
- };
- Mesh.prototype.getIndices = function (copyWhenShared) {
- if (!this._geometry) {
- return [];
- }
- return this._geometry.getIndices(copyWhenShared);
- };
- Object.defineProperty(Mesh.prototype, "isBlocked", {
- get: function () {
- return this._masterMesh !== null && this._masterMesh !== undefined;
- },
- enumerable: true,
- configurable: true
- });
- Mesh.prototype.isReady = function () {
- if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
- return false;
- }
- return _super.prototype.isReady.call(this);
- };
- Mesh.prototype.isDisposed = function () {
- return this._isDisposed;
- };
- Object.defineProperty(Mesh.prototype, "sideOrientation", {
- get: function () {
- return this._sideOrientation;
- },
- set: function (sideO) {
- this._sideOrientation = sideO;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(Mesh.prototype, "areNormalsFrozen", {
- get: function () {
- return this._areNormalsFrozen;
- },
- enumerable: true,
- configurable: true
- });
- /** This function affects parametric shapes on update only : ribbons, tubes, etc. It has no effect at all on other shapes */
- Mesh.prototype.freezeNormals = function () {
- this._areNormalsFrozen = true;
- };
- /** This function affects parametric shapes on update only : ribbons, tubes, etc. It has no effect at all on other shapes */
- Mesh.prototype.unfreezeNormals = function () {
- this._areNormalsFrozen = false;
- };
- // Methods
- Mesh.prototype._preActivate = function () {
- var sceneRenderId = this.getScene().getRenderId();
- if (this._preActivateId === sceneRenderId) {
- return;
- }
- this._preActivateId = sceneRenderId;
- this._visibleInstances = null;
- };
- Mesh.prototype._registerInstanceForRenderId = function (instance, renderId) {
- if (!this._visibleInstances) {
- this._visibleInstances = {};
- this._visibleInstances.defaultRenderId = renderId;
- this._visibleInstances.selfDefaultRenderId = this._renderId;
- }
- if (!this._visibleInstances[renderId]) {
- this._visibleInstances[renderId] = new Array();
- }
- this._visibleInstances[renderId].push(instance);
- };
- Mesh.prototype.refreshBoundingInfo = function () {
- var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
- if (data) {
- var extend = BABYLON.Tools.ExtractMinAndMax(data, 0, this.getTotalVertices());
- this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
- }
- if (this.subMeshes) {
- for (var index = 0; index < this.subMeshes.length; index++) {
- this.subMeshes[index].refreshBoundingInfo();
- }
- }
- this._updateBoundingInfo();
- };
- Mesh.prototype._createGlobalSubMesh = function () {
- var totalVertices = this.getTotalVertices();
- if (!totalVertices || !this.getIndices()) {
- return null;
- }
- this.releaseSubMeshes();
- return new BABYLON.SubMesh(0, 0, totalVertices, 0, this.getTotalIndices(), this);
- };
- Mesh.prototype.subdivide = function (count) {
- if (count < 1) {
- return;
- }
- var totalIndices = this.getTotalIndices();
- var subdivisionSize = (totalIndices / count) | 0;
- var offset = 0;
- while (subdivisionSize % 3 !== 0) {
- subdivisionSize++;
- }
- this.releaseSubMeshes();
- for (var index = 0; index < count; index++) {
- if (offset >= totalIndices) {
- break;
- }
- BABYLON.SubMesh.CreateFromIndices(0, offset, Math.min(subdivisionSize, totalIndices - offset), this);
- offset += subdivisionSize;
- }
- this.synchronizeInstances();
- };
- Mesh.prototype.setVerticesData = function (kind, data, updatable, stride) {
- if (kind instanceof Array) {
- var temp = data;
- data = kind;
- kind = temp;
- BABYLON.Tools.Warn("Deprecated usage of setVerticesData detected (since v1.12). Current signature is setVerticesData(kind, data, updatable).");
- }
- if (!this._geometry) {
- var vertexData = new BABYLON.VertexData();
- vertexData.set(data, kind);
- var scene = this.getScene();
- new BABYLON.Geometry(BABYLON.Geometry.RandomId(), scene, vertexData, updatable, this);
- }
- else {
- this._geometry.setVerticesData(kind, data, updatable, stride);
- }
- };
- Mesh.prototype.updateVerticesData = function (kind, data, updateExtends, makeItUnique) {
- if (!this._geometry) {
- return;
- }
- if (!makeItUnique) {
- this._geometry.updateVerticesData(kind, data, updateExtends);
- }
- else {
- this.makeGeometryUnique();
- this.updateVerticesData(kind, data, updateExtends, false);
- }
- };
- Mesh.prototype.updateVerticesDataDirectly = function (kind, data, offset, makeItUnique) {
- if (!this._geometry) {
- return;
- }
- if (!makeItUnique) {
- this._geometry.updateVerticesDataDirectly(kind, data, offset);
- }
- else {
- this.makeGeometryUnique();
- this.updateVerticesDataDirectly(kind, data, offset, false);
- }
- };
- // Mesh positions update function :
- // updates the mesh positions according to the positionFunction returned values.
- // The positionFunction argument must be a javascript function accepting the mesh "positions" array as parameter.
- // This dedicated positionFunction computes new mesh positions according to the given mesh type.
- Mesh.prototype.updateMeshPositions = function (positionFunction, computeNormals) {
- if (computeNormals === void 0) { computeNormals = true; }
- var positions = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
- positionFunction(positions);
- this.updateVerticesData(BABYLON.VertexBuffer.PositionKind, positions, false, false);
- if (computeNormals) {
- var indices = this.getIndices();
- var normals = this.getVerticesData(BABYLON.VertexBuffer.NormalKind);
- BABYLON.VertexData.ComputeNormals(positions, indices, normals);
- this.updateVerticesData(BABYLON.VertexBuffer.NormalKind, normals, false, false);
- }
- };
- Mesh.prototype.makeGeometryUnique = function () {
- if (!this._geometry) {
- return;
- }
- var geometry = this._geometry.copy(BABYLON.Geometry.RandomId());
- geometry.applyToMesh(this);
- };
- Mesh.prototype.setIndices = function (indices, totalVertices) {
- if (!this._geometry) {
- var vertexData = new BABYLON.VertexData();
- vertexData.indices = indices;
- var scene = this.getScene();
- new BABYLON.Geometry(BABYLON.Geometry.RandomId(), scene, vertexData, false, this);
- }
- else {
- this._geometry.setIndices(indices, totalVertices);
- }
- };
- Mesh.prototype._bind = function (subMesh, effect, fillMode) {
- var engine = this.getScene().getEngine();
- // Wireframe
- var indexToBind;
- switch (fillMode) {
- case BABYLON.Material.PointFillMode:
- indexToBind = null;
- break;
- case BABYLON.Material.WireFrameFillMode:
- indexToBind = subMesh.getLinesIndexBuffer(this.getIndices(), engine);
- break;
- default:
- case BABYLON.Material.TriangleFillMode:
- indexToBind = this._geometry.getIndexBuffer();
- break;
- }
- // VBOs
- engine.bindMultiBuffers(this._geometry.getVertexBuffers(), indexToBind, effect);
- };
- Mesh.prototype._draw = function (subMesh, fillMode, instancesCount) {
- if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
- return;
- }
- var engine = this.getScene().getEngine();
- switch (fillMode) {
- case BABYLON.Material.PointFillMode:
- engine.drawPointClouds(subMesh.verticesStart, subMesh.verticesCount, instancesCount);
- break;
- case BABYLON.Material.WireFrameFillMode:
- engine.draw(false, 0, subMesh.linesIndexCount, instancesCount);
- break;
- default:
- engine.draw(true, subMesh.indexStart, subMesh.indexCount, instancesCount);
- }
- };
- Mesh.prototype.registerBeforeRender = function (func) {
- this._onBeforeRenderCallbacks.push(func);
- };
- Mesh.prototype.unregisterBeforeRender = function (func) {
- var index = this._onBeforeRenderCallbacks.indexOf(func);
- if (index > -1) {
- this._onBeforeRenderCallbacks.splice(index, 1);
- }
- };
- Mesh.prototype.registerAfterRender = function (func) {
- this._onAfterRenderCallbacks.push(func);
- };
- Mesh.prototype.unregisterAfterRender = function (func) {
- var index = this._onAfterRenderCallbacks.indexOf(func);
- if (index > -1) {
- this._onAfterRenderCallbacks.splice(index, 1);
- }
- };
- Mesh.prototype._getInstancesRenderList = function (subMeshId) {
- var scene = this.getScene();
- this._batchCache.mustReturn = false;
- this._batchCache.renderSelf[subMeshId] = this.isEnabled() && this.isVisible;
- this._batchCache.visibleInstances[subMeshId] = null;
- if (this._visibleInstances) {
- var currentRenderId = scene.getRenderId();
- this._batchCache.visibleInstances[subMeshId] = this._visibleInstances[currentRenderId];
- var selfRenderId = this._renderId;
- if (!this._batchCache.visibleInstances[subMeshId] && this._visibleInstances.defaultRenderId) {
- this._batchCache.visibleInstances[subMeshId] = this._visibleInstances[this._visibleInstances.defaultRenderId];
- currentRenderId = Math.max(this._visibleInstances.defaultRenderId, currentRenderId);
- selfRenderId = Math.max(this._visibleInstances.selfDefaultRenderId, currentRenderId);
- }
- if (this._batchCache.visibleInstances[subMeshId] && this._batchCache.visibleInstances[subMeshId].length) {
- if (this._renderIdForInstances[subMeshId] === currentRenderId) {
- this._batchCache.mustReturn = true;
- return this._batchCache;
- }
- if (currentRenderId !== selfRenderId) {
- this._batchCache.renderSelf[subMeshId] = false;
- }
- }
- this._renderIdForInstances[subMeshId] = currentRenderId;
- }
- return this._batchCache;
- };
- Mesh.prototype._renderWithInstances = function (subMesh, fillMode, batch, effect, engine) {
- var visibleInstances = batch.visibleInstances[subMesh._id];
- var matricesCount = visibleInstances.length + 1;
- var bufferSize = matricesCount * 16 * 4;
- while (this._instancesBufferSize < bufferSize) {
- this._instancesBufferSize *= 2;
- }
- if (!this._worldMatricesInstancesBuffer || this._worldMatricesInstancesBuffer.capacity < this._instancesBufferSize) {
- if (this._worldMatricesInstancesBuffer) {
- engine.deleteInstancesBuffer(this._worldMatricesInstancesBuffer);
- }
- this._worldMatricesInstancesBuffer = engine.createInstancesBuffer(this._instancesBufferSize);
- this._worldMatricesInstancesArray = new Float32Array(this._instancesBufferSize / 4);
- }
- var offset = 0;
- var instancesCount = 0;
- var world = this.getWorldMatrix();
- if (batch.renderSelf[subMesh._id]) {
- world.copyToArray(this._worldMatricesInstancesArray, offset);
- offset += 16;
- instancesCount++;
- }
- if (visibleInstances) {
- for (var instanceIndex = 0; instanceIndex < visibleInstances.length; instanceIndex++) {
- var instance = visibleInstances[instanceIndex];
- instance.getWorldMatrix().copyToArray(this._worldMatricesInstancesArray, offset);
- offset += 16;
- instancesCount++;
- }
- }
- var offsetLocation0 = effect.getAttributeLocationByName("world0");
- var offsetLocation1 = effect.getAttributeLocationByName("world1");
- var offsetLocation2 = effect.getAttributeLocationByName("world2");
- var offsetLocation3 = effect.getAttributeLocationByName("world3");
- var offsetLocations = [offsetLocation0, offsetLocation1, offsetLocation2, offsetLocation3];
- engine.updateAndBindInstancesBuffer(this._worldMatricesInstancesBuffer, this._worldMatricesInstancesArray, offsetLocations);
- this._draw(subMesh, fillMode, instancesCount);
- engine.unBindInstancesBuffer(this._worldMatricesInstancesBuffer, offsetLocations);
- };
- Mesh.prototype._processRendering = function (subMesh, effect, fillMode, batch, hardwareInstancedRendering, onBeforeDraw) {
- var scene = this.getScene();
- var engine = scene.getEngine();
- if (hardwareInstancedRendering) {
- this._renderWithInstances(subMesh, fillMode, batch, effect, engine);
- }
- else {
- if (batch.renderSelf[subMesh._id]) {
- // Draw
- if (onBeforeDraw) {
- onBeforeDraw(false, this.getWorldMatrix());
- }
- this._draw(subMesh, fillMode);
- }
- if (batch.visibleInstances[subMesh._id]) {
- for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
- var instance = batch.visibleInstances[subMesh._id][instanceIndex];
- // World
- var world = instance.getWorldMatrix();
- if (onBeforeDraw) {
- onBeforeDraw(true, world);
- }
- // Draw
- this._draw(subMesh, fillMode);
- }
- }
- }
- };
- Mesh.prototype.render = function (subMesh) {
- var scene = this.getScene();
- // Managing instances
- var batch = this._getInstancesRenderList(subMesh._id);
- if (batch.mustReturn) {
- return;
- }
- // Checking geometry state
- if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
- return;
- }
- for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
- this._onBeforeRenderCallbacks[callbackIndex](this);
- }
- var engine = scene.getEngine();
- var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null) && (batch.visibleInstances[subMesh._id] !== undefined);
- // Material
- var effectiveMaterial = subMesh.getMaterial();
- if (!effectiveMaterial || !effectiveMaterial.isReady(this, hardwareInstancedRendering)) {
- return;
- }
- // Outline - step 1
- var savedDepthWrite = engine.getDepthWrite();
- if (this.renderOutline) {
- engine.setDepthWrite(false);
- scene.getOutlineRenderer().render(subMesh, batch);
- engine.setDepthWrite(savedDepthWrite);
- }
- effectiveMaterial._preBind();
- var effect = effectiveMaterial.getEffect();
- // Bind
- var fillMode = scene.forcePointsCloud ? BABYLON.Material.PointFillMode : (scene.forceWireframe ? BABYLON.Material.WireFrameFillMode : effectiveMaterial.fillMode);
- this._bind(subMesh, effect, fillMode);
- var world = this.getWorldMatrix();
- effectiveMaterial.bind(world, this);
- // Draw
- this._processRendering(subMesh, effect, fillMode, batch, hardwareInstancedRendering, function (isInstance, world) {
- if (isInstance) {
- effectiveMaterial.bindOnlyWorldMatrix(world);
- }
- });
- // Unbind
- effectiveMaterial.unbind();
- // Outline - step 2
- if (this.renderOutline && savedDepthWrite) {
- engine.setDepthWrite(true);
- engine.setColorWrite(false);
- scene.getOutlineRenderer().render(subMesh, batch);
- engine.setColorWrite(true);
- }
- // Overlay
- if (this.renderOverlay) {
- var currentMode = engine.getAlphaMode();
- engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
- scene.getOutlineRenderer().render(subMesh, batch, true);
- engine.setAlphaMode(currentMode);
- }
- for (callbackIndex = 0; callbackIndex < this._onAfterRenderCallbacks.length; callbackIndex++) {
- this._onAfterRenderCallbacks[callbackIndex](this);
- }
- };
- Mesh.prototype.getEmittedParticleSystems = function () {
- var results = new Array();
- for (var index = 0; index < this.getScene().particleSystems.length; index++) {
- var particleSystem = this.getScene().particleSystems[index];
- if (particleSystem.emitter === this) {
- results.push(particleSystem);
- }
- }
- return results;
- };
- Mesh.prototype.getHierarchyEmittedParticleSystems = function () {
- var results = new Array();
- var descendants = this.getDescendants();
- descendants.push(this);
- for (var index = 0; index < this.getScene().particleSystems.length; index++) {
- var particleSystem = this.getScene().particleSystems[index];
- if (descendants.indexOf(particleSystem.emitter) !== -1) {
- results.push(particleSystem);
- }
- }
- return results;
- };
- Mesh.prototype.getChildren = function () {
- var results = [];
- for (var index = 0; index < this.getScene().meshes.length; index++) {
- var mesh = this.getScene().meshes[index];
- if (mesh.parent === this) {
- results.push(mesh);
- }
- }
- return results;
- };
- Mesh.prototype._checkDelayState = function () {
- var _this = this;
- var that = this;
- var scene = this.getScene();
- if (this._geometry) {
- this._geometry.load(scene);
- }
- else if (that.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
- that.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADING;
- scene._addPendingData(that);
- var getBinaryData = (this.delayLoadingFile.indexOf(".babylonbinarymeshdata") !== -1);
- BABYLON.Tools.LoadFile(this.delayLoadingFile, function (data) {
- if (data instanceof ArrayBuffer) {
- _this._delayLoadingFunction(data, _this);
- }
- else {
- _this._delayLoadingFunction(JSON.parse(data), _this);
- }
- _this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
- scene._removePendingData(_this);
- }, function () {
- }, scene.database, getBinaryData);
- }
- };
- Mesh.prototype.isInFrustum = function (frustumPlanes) {
- if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
- return false;
- }
- if (!_super.prototype.isInFrustum.call(this, frustumPlanes)) {
- return false;
- }
- this._checkDelayState();
- return true;
- };
- Mesh.prototype.setMaterialByID = function (id) {
- var materials = this.getScene().materials;
- for (var index = 0; index < materials.length; index++) {
- if (materials[index].id === id) {
- this.material = materials[index];
- return;
- }
- }
- // Multi
- var multiMaterials = this.getScene().multiMaterials;
- for (index = 0; index < multiMaterials.length; index++) {
- if (multiMaterials[index].id === id) {
- this.material = multiMaterials[index];
- return;
- }
- }
- };
- Mesh.prototype.getAnimatables = function () {
- var results = [];
- if (this.material) {
- results.push(this.material);
- }
- if (this.skeleton) {
- results.push(this.skeleton);
- }
- return results;
- };
- // Geometry
- Mesh.prototype.bakeTransformIntoVertices = function (transform) {
- // Position
- if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
- return;
- }
- this._resetPointsArrayCache();
- var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
- var temp = [];
- for (var index = 0; index < data.length; index += 3) {
- BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.FromArray(data, index), transform).toArray(temp, index);
- }
- this.setVerticesData(BABYLON.VertexBuffer.PositionKind, temp, this.getVertexBuffer(BABYLON.VertexBuffer.PositionKind).isUpdatable());
- // Normals
- if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
- return;
- }
- data = this.getVerticesData(BABYLON.VertexBuffer.NormalKind);
- temp = [];
- for (index = 0; index < data.length; index += 3) {
- BABYLON.Vector3.TransformNormal(BABYLON.Vector3.FromArray(data, index), transform).toArray(temp, index);
- }
- this.setVerticesData(BABYLON.VertexBuffer.NormalKind, temp, this.getVertexBuffer(BABYLON.VertexBuffer.NormalKind).isUpdatable());
- };
- // Cache
- Mesh.prototype._resetPointsArrayCache = function () {
- this._positions = null;
- };
- Mesh.prototype._generatePointsArray = function () {
- if (this._positions)
- return true;
- this._positions = [];
- var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
- if (!data) {
- return false;
- }
- for (var index = 0; index < data.length; index += 3) {
- this._positions.push(BABYLON.Vector3.FromArray(data, index));
- }
- return true;
- };
- // Clone
- Mesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
- return new Mesh(name, this.getScene(), newParent, this, doNotCloneChildren);
- };
- // Dispose
- Mesh.prototype.dispose = function (doNotRecurse) {
- if (this._geometry) {
- this._geometry.releaseForMesh(this, true);
- }
- // Instances
- if (this._worldMatricesInstancesBuffer) {
- this.getEngine().deleteInstancesBuffer(this._worldMatricesInstancesBuffer);
- this._worldMatricesInstancesBuffer = null;
- }
- while (this.instances.length) {
- this.instances[0].dispose();
- }
- _super.prototype.dispose.call(this, doNotRecurse);
- };
- // Geometric tools
- Mesh.prototype.applyDisplacementMap = function (url, minHeight, maxHeight, onSuccess) {
- var _this = this;
- var scene = this.getScene();
- var onload = function (img) {
- // Getting height map data
- var canvas = document.createElement("canvas");
- var context = canvas.getContext("2d");
- var heightMapWidth = img.width;
- var heightMapHeight = img.height;
- canvas.width = heightMapWidth;
- canvas.height = heightMapHeight;
- context.drawImage(img, 0, 0);
- // Create VertexData from map data
- //Cast is due to wrong definition in lib.d.ts from ts 1.3 - https://github.com/Microsoft/TypeScript/issues/949
- var buffer = context.getImageData(0, 0, heightMapWidth, heightMapHeight).data;
- _this.applyDisplacementMapFromBuffer(buffer, heightMapWidth, heightMapHeight, minHeight, maxHeight);
- //execute success callback, if set
- if (onSuccess) {
- onSuccess(_this);
- }
- };
- BABYLON.Tools.LoadImage(url, onload, function () {
- }, scene.database);
- };
- Mesh.prototype.applyDisplacementMapFromBuffer = function (buffer, heightMapWidth, heightMapHeight, minHeight, maxHeight) {
- if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind) || !this.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind) || !this.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
- BABYLON.Tools.Warn("Cannot call applyDisplacementMap: Given mesh is not complete. Position, Normal or UV are missing");
- return;
- }
- var positions = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
- var normals = this.getVerticesData(BABYLON.VertexBuffer.NormalKind);
- var uvs = this.getVerticesData(BABYLON.VertexBuffer.UVKind);
- var position = BABYLON.Vector3.Zero();
- var normal = BABYLON.Vector3.Zero();
- var uv = BABYLON.Vector2.Zero();
- for (var index = 0; index < positions.length; index += 3) {
- BABYLON.Vector3.FromArrayToRef(positions, index, position);
- BABYLON.Vector3.FromArrayToRef(normals, index, normal);
- BABYLON.Vector2.FromArrayToRef(uvs, (index / 3) * 2, uv);
- // Compute height
- var u = ((Math.abs(uv.x) * heightMapWidth) % heightMapWidth) | 0;
- var v = ((Math.abs(uv.y) * heightMapHeight) % heightMapHeight) | 0;
- var pos = (u + v * heightMapWidth) * 4;
- var r = buffer[pos] / 255.0;
- var g = buffer[pos + 1] / 255.0;
- var b = buffer[pos + 2] / 255.0;
- var gradient = r * 0.3 + g * 0.59 + b * 0.11;
- normal.normalize();
- normal.scaleInPlace(minHeight + (maxHeight - minHeight) * gradient);
- position = position.add(normal);
- position.toArray(positions, index);
- }
- BABYLON.VertexData.ComputeNormals(positions, this.getIndices(), normals);
- this.updateVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
- this.updateVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
- };
- Mesh.prototype.convertToFlatShadedMesh = function () {
- /// <summary>Update normals and vertices to get a flat shading rendering.</summary>
- /// <summary>Warning: This may imply adding vertices to the mesh in order to get exactly 3 vertices per face</summary>
- var kinds = this.getVerticesDataKinds();
- var vbs = [];
- var data = [];
- var newdata = [];
- var updatableNormals = false;
- for (var kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
- var kind = kinds[kindIndex];
- var vertexBuffer = this.getVertexBuffer(kind);
- if (kind === BABYLON.VertexBuffer.NormalKind) {
- updatableNormals = vertexBuffer.isUpdatable();
- kinds.splice(kindIndex, 1);
- kindIndex--;
- continue;
- }
- vbs[kind] = vertexBuffer;
- data[kind] = vbs[kind].getData();
- newdata[kind] = [];
- }
- // Save previous submeshes
- var previousSubmeshes = this.subMeshes.slice(0);
- var indices = this.getIndices();
- var totalIndices = this.getTotalIndices();
- for (var index = 0; index < totalIndices; index++) {
- var vertexIndex = indices[index];
- for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
- kind = kinds[kindIndex];
- var stride = vbs[kind].getStrideSize();
- for (var offset = 0; offset < stride; offset++) {
- newdata[kind].push(data[kind][vertexIndex * stride + offset]);
- }
- }
- }
- // Updating faces & normal
- var normals = [];
- var positions = newdata[BABYLON.VertexBuffer.PositionKind];
- for (index = 0; index < totalIndices; index += 3) {
- indices[index] = index;
- indices[index + 1] = index + 1;
- indices[index + 2] = index + 2;
- var p1 = BABYLON.Vector3.FromArray(positions, index * 3);
- var p2 = BABYLON.Vector3.FromArray(positions, (index + 1) * 3);
- var p3 = BABYLON.Vector3.FromArray(positions, (index + 2) * 3);
- var p1p2 = p1.subtract(p2);
- var p3p2 = p3.subtract(p2);
- var normal = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
- for (var localIndex = 0; localIndex < 3; localIndex++) {
- normals.push(normal.x);
- normals.push(normal.y);
- normals.push(normal.z);
- }
- }
- this.setIndices(indices);
- this.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals, updatableNormals);
- for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
- kind = kinds[kindIndex];
- this.setVerticesData(kind, newdata[kind], vbs[kind].isUpdatable());
- }
- // Updating submeshes
- this.releaseSubMeshes();
- for (var submeshIndex = 0; submeshIndex < previousSubmeshes.length; submeshIndex++) {
- var previousOne = previousSubmeshes[submeshIndex];
- var subMesh = new BABYLON.SubMesh(previousOne.materialIndex, previousOne.indexStart, previousOne.indexCount, previousOne.indexStart, previousOne.indexCount, this);
- }
- this.synchronizeInstances();
- };
- // Instances
- Mesh.prototype.createInstance = function (name) {
- return new BABYLON.InstancedMesh(name, this);
- };
- Mesh.prototype.synchronizeInstances = function () {
- for (var instanceIndex = 0; instanceIndex < this.instances.length; instanceIndex++) {
- var instance = this.instances[instanceIndex];
- instance._syncSubMeshes();
- }
- };
- /**
- * Simplify the mesh according to the given array of settings.
- * Function will return immediately and will simplify async.
- * @param settings a collection of simplification settings.
- * @param parallelProcessing should all levels calculate parallel or one after the other.
- * @param type the type of simplification to run.
- * @param successCallback optional success callback to be called after the simplification finished processing all settings.
- */
- Mesh.prototype.simplify = function (settings, parallelProcessing, simplificationType, successCallback) {
- if (parallelProcessing === void 0) { parallelProcessing = true; }
- if (simplificationType === void 0) { simplificationType = 0 /* QUADRATIC */; }
- this.getScene().simplificationQueue.addTask({
- settings: settings,
- parallelProcessing: parallelProcessing,
- mesh: this,
- simplificationType: simplificationType,
- successCallback: successCallback
- });
- };
- /**
- * Optimization of the mesh's indices, in case a mesh has duplicated vertices.
- * The function will only reorder the indices and will not remove unused vertices to avoid problems with submeshes.
- * This should be used together with the simplification to avoid disappearing triangles.
- * @param successCallback an optional success callback to be called after the optimization finished.
- */
- Mesh.prototype.optimizeIndices = function (successCallback) {
- var _this = this;
- var indices = this.getIndices();
- var positions = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
- var vectorPositions = [];
- for (var pos = 0; pos < positions.length; pos = pos + 3) {
- vectorPositions.push(BABYLON.Vector3.FromArray(positions, pos));
- }
- var dupes = [];
- BABYLON.AsyncLoop.SyncAsyncForLoop(vectorPositions.length, 40, function (iteration) {
- var realPos = vectorPositions.length - 1 - iteration;
- var testedPosition = vectorPositions[realPos];
- for (var j = 0; j < realPos; ++j) {
- var againstPosition = vectorPositions[j];
- if (testedPosition.equals(againstPosition)) {
- dupes[realPos] = j;
- break;
- }
- }
- }, function () {
- for (var i = 0; i < indices.length; ++i) {
- indices[i] = dupes[indices[i]] || indices[i];
- }
- //indices are now reordered
- var originalSubMeshes = _this.subMeshes.slice(0);
- _this.setIndices(indices);
- _this.subMeshes = originalSubMeshes;
- if (successCallback) {
- successCallback(_this);
- }
- });
- };
- // Statics
- Mesh.CreateRibbon = function (name, pathArray, closeArray, closePath, offset, scene, updatable, sideOrientation, ribbonInstance) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- if (ribbonInstance === void 0) { ribbonInstance = null; }
- if (ribbonInstance) {
- // positionFunction : ribbon case
- // only pathArray and sideOrientation parameters are taken into account for positions update
- var positionFunction = function (positions) {
- var minlg = pathArray[0].length;
- var i = 0;
- var ns = (ribbonInstance.sideOrientation === BABYLON.Mesh.DOUBLESIDE) ? 2 : 1;
- for (var si = 1; si <= ns; si++) {
- for (var p = 0; p < pathArray.length; p++) {
- var path = pathArray[p];
- var l = path.length;
- minlg = (minlg < l) ? minlg : l;
- var j = 0;
- while (j < minlg) {
- positions[i] = path[j].x;
- positions[i + 1] = path[j].y;
- positions[i + 2] = path[j].z;
- j++;
- i += 3;
- }
- }
- }
- };
- var computeNormals = !(ribbonInstance.areNormalsFrozen);
- ribbonInstance.updateMeshPositions(positionFunction, computeNormals);
- return ribbonInstance;
- }
- else {
- var ribbon = new Mesh(name, scene);
- ribbon.sideOrientation = sideOrientation;
- var vertexData = BABYLON.VertexData.CreateRibbon(pathArray, closeArray, closePath, offset, sideOrientation);
- vertexData.applyToMesh(ribbon, updatable);
- return ribbon;
- }
- };
- Mesh.CreateDisc = function (name, radius, tessellation, scene, updatable, sideOrientation) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- var disc = new Mesh(name, scene);
- var vertexData = BABYLON.VertexData.CreateDisc(radius, tessellation, sideOrientation);
- vertexData.applyToMesh(disc, updatable);
- return disc;
- };
- Mesh.CreateBox = function (name, size, scene, updatable, sideOrientation) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- var box = new Mesh(name, scene);
- var vertexData = BABYLON.VertexData.CreateBox(size, sideOrientation);
- vertexData.applyToMesh(box, updatable);
- return box;
- };
- Mesh.CreateSphere = function (name, segments, diameter, scene, updatable, sideOrientation) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- var sphere = new Mesh(name, scene);
- var vertexData = BABYLON.VertexData.CreateSphere(segments, diameter, sideOrientation);
- vertexData.applyToMesh(sphere, updatable);
- return sphere;
- };
- // Cylinder and cone (Code inspired by SharpDX.org)
- Mesh.CreateCylinder = function (name, height, diameterTop, diameterBottom, tessellation, subdivisions, scene, updatable, sideOrientation) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- // subdivisions is a new parameter, we need to support old signature
- if (scene === undefined || !(scene instanceof BABYLON.Scene)) {
- if (scene !== undefined) {
- updatable = scene;
- }
- scene = subdivisions;
- subdivisions = 1;
- }
- var cylinder = new Mesh(name, scene);
- var vertexData = BABYLON.VertexData.CreateCylinder(height, diameterTop, diameterBottom, tessellation, subdivisions);
- vertexData.applyToMesh(cylinder, updatable);
- return cylinder;
- };
- // Torus (Code from SharpDX.org)
- Mesh.CreateTorus = function (name, diameter, thickness, tessellation, scene, updatable, sideOrientation) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- var torus = new Mesh(name, scene);
- var vertexData = BABYLON.VertexData.CreateTorus(diameter, thickness, tessellation, sideOrientation);
- vertexData.applyToMesh(torus, updatable);
- return torus;
- };
- Mesh.CreateTorusKnot = function (name, radius, tube, radialSegments, tubularSegments, p, q, scene, updatable, sideOrientation) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- var torusKnot = new Mesh(name, scene);
- var vertexData = BABYLON.VertexData.CreateTorusKnot(radius, tube, radialSegments, tubularSegments, p, q, sideOrientation);
- vertexData.applyToMesh(torusKnot, updatable);
- return torusKnot;
- };
- // Lines
- Mesh.CreateLines = function (name, points, scene, updatable, linesInstance) {
- if (linesInstance === void 0) { linesInstance = null; }
- if (linesInstance) {
- var positionFunction = function (positions) {
- var i = 0;
- for (var p = 0; p < points.length; p++) {
- positions[i] = points[p].x;
- positions[i + 1] = points[p].y;
- positions[i + 2] = points[p].z;
- i += 3;
- }
- };
- linesInstance.updateMeshPositions(positionFunction, false);
- return linesInstance;
- }
- // lines creation
- var lines = new BABYLON.LinesMesh(name, scene, updatable);
- var vertexData = BABYLON.VertexData.CreateLines(points);
- vertexData.applyToMesh(lines, updatable);
- return lines;
- };
- // Dashed Lines
- Mesh.CreateDashedLines = function (name, points, dashSize, gapSize, dashNb, scene, updatable, linesInstance) {
- if (linesInstance === void 0) { linesInstance = null; }
- if (linesInstance) {
- var positionFunction = function (positions) {
- var curvect = BABYLON.Vector3.Zero();
- var nbSeg = positions.length / 6;
- var lg = 0;
- var nb = 0;
- var shft = 0;
- var dashshft = 0;
- var curshft = 0;
- var p = 0;
- var i = 0;
- var j = 0;
- for (i = 0; i < points.length - 1; i++) {
- points[i + 1].subtractToRef(points[i], curvect);
- lg += curvect.length();
- }
- shft = lg / nbSeg;
- dashshft = linesInstance.dashSize * shft / (linesInstance.dashSize + linesInstance.gapSize);
- for (i = 0; i < points.length - 1; i++) {
- points[i + 1].subtractToRef(points[i], curvect);
- nb = Math.floor(curvect.length() / shft);
- curvect.normalize();
- j = 0;
- while (j < nb && p < positions.length) {
- curshft = shft * j;
- positions[p] = points[i].x + curshft * curvect.x;
- positions[p + 1] = points[i].y + curshft * curvect.y;
- positions[p + 2] = points[i].z + curshft * curvect.z;
- positions[p + 3] = points[i].x + (curshft + dashshft) * curvect.x;
- positions[p + 4] = points[i].y + (curshft + dashshft) * curvect.y;
- positions[p + 5] = points[i].z + (curshft + dashshft) * curvect.z;
- p += 6;
- j++;
- }
- }
- while (p < positions.length) {
- positions[p] = points[i].x;
- positions[p + 1] = points[i].y;
- positions[p + 2] = points[i].z;
- p += 3;
- }
- };
- linesInstance.updateMeshPositions(positionFunction, false);
- return linesInstance;
- }
- // dashed lines creation
- var dashedLines = new BABYLON.LinesMesh(name, scene, updatable);
- var vertexData = BABYLON.VertexData.CreateDashedLines(points, dashSize, gapSize, dashNb);
- vertexData.applyToMesh(dashedLines, updatable);
- dashedLines.dashSize = dashSize;
- dashedLines.gapSize = gapSize;
- return dashedLines;
- };
- // Extrusion
- Mesh.ExtrudeShape = function (name, shape, path, scale, rotation, cap, scene, updatable, sideOrientation, extrudedInstance) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- if (extrudedInstance === void 0) { extrudedInstance = null; }
- scale = scale || 1;
- rotation = rotation || 0;
- var extruded = Mesh._ExtrudeShapeGeneric(name, shape, path, scale, rotation, null, null, false, false, cap, false, scene, updatable, sideOrientation, extrudedInstance);
- return extruded;
- };
- Mesh.ExtrudeShapeCustom = function (name, shape, path, scaleFunction, rotationFunction, ribbonCloseArray, ribbonClosePath, cap, scene, updatable, sideOrientation, extrudedInstance) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- if (extrudedInstance === void 0) { extrudedInstance = null; }
- var extrudedCustom = Mesh._ExtrudeShapeGeneric(name, shape, path, null, null, scaleFunction, rotationFunction, ribbonCloseArray, ribbonClosePath, cap, true, scene, updatable, sideOrientation, extrudedInstance);
- return extrudedCustom;
- };
- Mesh._ExtrudeShapeGeneric = function (name, shape, curve, scale, rotation, scaleFunction, rotateFunction, rbCA, rbCP, cap, custom, scene, updtbl, side, instance) {
- // extrusion geometry
- var extrusionPathArray = function (shape, curve, path3D, shapePaths, scale, rotation, scaleFunction, rotateFunction, cap, custom) {
- var tangents = path3D.getTangents();
- var normals = path3D.getNormals();
- var binormals = path3D.getBinormals();
- var distances = path3D.getDistances();
- var angle = 0;
- var returnScale = function (i, distance) {
- return scale;
- };
- var returnRotation = function (i, distance) {
- return rotation;
- };
- var rotate = custom ? rotateFunction : returnRotation;
- var scl = custom ? scaleFunction : returnScale;
- var index = 0;
- for (var i = 0; i < curve.length; i++) {
- var shapePath = new Array();
- var angleStep = rotate(i, distances[i]);
- var scaleRatio = scl(i, distances[i]);
- for (var p = 0; p < shape.length; p++) {
- var rotationMatrix = BABYLON.Matrix.RotationAxis(tangents[i], angle);
- var planed = ((tangents[i].scale(shape[p].z)).add(normals[i].scale(shape[p].x)).add(binormals[i].scale(shape[p].y)));
- var rotated = BABYLON.Vector3.TransformCoordinates(planed, rotationMatrix).scaleInPlace(scaleRatio).add(curve[i]);
- shapePath.push(rotated);
- }
- shapePaths[index] = shapePath;
- angle += angleStep;
- index++;
- }
- // cap
- var capPath = function (shapePath) {
- var pointCap = Array();
- var barycenter = BABYLON.Vector3.Zero();
- var i;
- for (i = 0; i < shapePath.length; i++) {
- barycenter.addInPlace(shapePath[i]);
- }
- barycenter.scaleInPlace(1 / shapePath.length);
- for (i = 0; i < shapePath.length; i++) {
- pointCap.push(barycenter);
- }
- return pointCap;
- };
- switch (cap) {
- case BABYLON.Mesh.NO_CAP:
- break;
- case BABYLON.Mesh.CAP_START:
- shapePaths.unshift(capPath(shapePaths[0]));
- break;
- case BABYLON.Mesh.CAP_END:
- shapePaths.push(capPath(shapePaths[shapePaths.length - 1]));
- break;
- case BABYLON.Mesh.CAP_ALL:
- shapePaths.unshift(capPath(shapePaths[0]));
- shapePaths.push(capPath(shapePaths[shapePaths.length - 1]));
- break;
- default:
- break;
- }
- return shapePaths;
- };
- if (instance) {
- var path3D = (instance.path3D).update(curve);
- var pathArray = extrusionPathArray(shape, curve, instance.path3D, instance.pathArray, scale, rotation, scaleFunction, rotateFunction, instance.cap, custom);
- instance = Mesh.CreateRibbon(null, pathArray, null, null, null, null, null, null, instance);
- return instance;
- }
- // extruded shape creation
- var path3D = new BABYLON.Path3D(curve);
- var newShapePaths = new Array();
- cap = (cap < 0 || cap > 3) ? 0 : cap;
- var pathArray = extrusionPathArray(shape, curve, path3D, newShapePaths, scale, rotation, scaleFunction, rotateFunction, cap, custom);
- var extrudedGeneric = Mesh.CreateRibbon(name, pathArray, rbCA, rbCP, 0, scene, updtbl, side);
- extrudedGeneric.pathArray = pathArray;
- extrudedGeneric.path3D = path3D;
- extrudedGeneric.cap = cap;
- return extrudedGeneric;
- };
- // Plane & ground
- Mesh.CreatePlane = function (name, size, scene, updatable, sideOrientation) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- var plane = new Mesh(name, scene);
- var vertexData = BABYLON.VertexData.CreatePlane(size, sideOrientation);
- vertexData.applyToMesh(plane, updatable);
- return plane;
- };
- Mesh.CreateGround = function (name, width, height, subdivisions, scene, updatable) {
- var ground = new BABYLON.GroundMesh(name, scene);
- ground._setReady(false);
- ground._subdivisions = subdivisions;
- var vertexData = BABYLON.VertexData.CreateGround(width, height, subdivisions);
- vertexData.applyToMesh(ground, updatable);
- ground._setReady(true);
- return ground;
- };
- Mesh.CreateTiledGround = function (name, xmin, zmin, xmax, zmax, subdivisions, precision, scene, updatable) {
- var tiledGround = new Mesh(name, scene);
- var vertexData = BABYLON.VertexData.CreateTiledGround(xmin, zmin, xmax, zmax, subdivisions, precision);
- vertexData.applyToMesh(tiledGround, updatable);
- return tiledGround;
- };
- Mesh.CreateGroundFromHeightMap = function (name, url, width, height, subdivisions, minHeight, maxHeight, scene, updatable, onReady) {
- var ground = new BABYLON.GroundMesh(name, scene);
- ground._subdivisions = subdivisions;
- ground._setReady(false);
- var onload = function (img) {
- // Getting height map data
- var canvas = document.createElement("canvas");
- var context = canvas.getContext("2d");
- var heightMapWidth = img.width;
- var heightMapHeight = img.height;
- canvas.width = heightMapWidth;
- canvas.height = heightMapHeight;
- context.drawImage(img, 0, 0);
- // Create VertexData from map data
- // Cast is due to wrong definition in lib.d.ts from ts 1.3 - https://github.com/Microsoft/TypeScript/issues/949
- var buffer = context.getImageData(0, 0, heightMapWidth, heightMapHeight).data;
- var vertexData = BABYLON.VertexData.CreateGroundFromHeightMap(width, height, subdivisions, minHeight, maxHeight, buffer, heightMapWidth, heightMapHeight);
- vertexData.applyToMesh(ground, updatable);
- ground._setReady(true);
- //execute ready callback, if set
- if (onReady) {
- onReady(ground);
- }
- };
- BABYLON.Tools.LoadImage(url, onload, function () {
- }, scene.database);
- return ground;
- };
- Mesh.CreateTube = function (name, path, radius, tessellation, radiusFunction, cap, scene, updatable, sideOrientation, tubeInstance) {
- if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
- if (tubeInstance === void 0) { tubeInstance = null; }
- // tube geometry
- var tubePathArray = function (path, path3D, circlePaths, radius, tessellation, radiusFunction, cap) {
- var tangents = path3D.getTangents();
- var normals = path3D.getNormals();
- var distances = path3D.getDistances();
- var pi2 = Math.PI * 2;
- var step = pi2 / tessellation;
- var returnRadius = function (i, distance) { return radius; };
- var radiusFunctionFinal = radiusFunction || returnRadius;
- var circlePath;
- var rad;
- var normal;
- var rotated;
- var rotationMatrix;
- var index = 0;
- for (var i = 0; i < path.length; i++) {
- rad = radiusFunctionFinal(i, distances[i]); // current radius
- circlePath = Array(); // current circle array
- normal = normals[i]; // current normal
- for (var t = 0; t < tessellation; t++) {
- rotationMatrix = BABYLON.Matrix.RotationAxis(tangents[i], step * t);
- rotated = BABYLON.Vector3.TransformCoordinates(normal, rotationMatrix).scaleInPlace(rad).add(path[i]);
- circlePath.push(rotated);
- }
- circlePath.push(circlePath[0]);
- circlePaths[index] = circlePath;
- index++;
- }
- // cap
- var capPath = function (nbPoints, pathIndex) {
- var pointCap = Array();
- for (var i = 0; i < nbPoints; i++) {
- pointCap.push(path[pathIndex]);
- }
- return pointCap;
- };
- switch (cap) {
- case BABYLON.Mesh.NO_CAP:
- break;
- case BABYLON.Mesh.CAP_START:
- circlePaths.unshift(capPath(tessellation + 1, 0));
- break;
- case BABYLON.Mesh.CAP_END:
- circlePaths.push(capPath(tessellation + 1, path.length - 1));
- break;
- case BABYLON.Mesh.CAP_ALL:
- circlePaths.unshift(capPath(tessellation + 1, 0));
- circlePaths.push(capPath(tessellation + 1, path.length - 1));
- break;
- default:
- break;
- }
- return circlePaths;
- };
- if (tubeInstance) {
- var path3D = (tubeInstance.path3D).update(path);
- var pathArray = tubePathArray(path, path3D, tubeInstance.pathArray, radius, tubeInstance.tessellation, radiusFunction, tubeInstance.cap);
- tubeInstance = Mesh.CreateRibbon(null, pathArray, null, null, null, null, null, null, tubeInstance);
- return tubeInstance;
- }
- // tube creation
- var path3D = new BABYLON.Path3D(path);
- var newPathArray = new Array();
- cap = (cap < 0 || cap > 3) ? 0 : cap;
- var pathArray = tubePathArray(path, path3D, newPathArray, radius, tessellation, radiusFunction, cap);
- var tube = Mesh.CreateRibbon(name, pathArray, false, true, 0, scene, updatable, sideOrientation);
- tube.pathArray = pathArray;
- tube.path3D = path3D;
- tube.tessellation = tessellation;
- tube.cap = cap;
- return tube;
- };
- // Decals
- Mesh.CreateDecal = function (name, sourceMesh, position, normal, size, angle) {
- if (angle === void 0) { angle = 0; }
- var indices = sourceMesh.getIndices();
- var positions = sourceMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
- var normals = sourceMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
- // Getting correct rotation
- if (!normal) {
- var target = new BABYLON.Vector3(0, 0, 1);
- var camera = sourceMesh.getScene().activeCamera;
- var cameraWorldTarget = BABYLON.Vector3.TransformCoordinates(target, camera.getWorldMatrix());
- normal = camera.globalPosition.subtract(cameraWorldTarget);
- }
- var yaw = -Math.atan2(normal.z, normal.x) - Math.PI / 2;
- var len = Math.sqrt(normal.x * normal.x + normal.z * normal.z);
- var pitch = Math.atan2(normal.y, len);
- // Matrix
- var decalWorldMatrix = BABYLON.Matrix.RotationYawPitchRoll(yaw, pitch, angle).multiply(BABYLON.Matrix.Translation(position.x, position.y, position.z));
- var inverseDecalWorldMatrix = BABYLON.Matrix.Invert(decalWorldMatrix);
- var meshWorldMatrix = sourceMesh.getWorldMatrix();
- var transformMatrix = meshWorldMatrix.multiply(inverseDecalWorldMatrix);
- var vertexData = new BABYLON.VertexData();
- vertexData.indices = [];
- vertexData.positions = [];
- vertexData.normals = [];
- vertexData.uvs = [];
- var currentVertexDataIndex = 0;
- var extractDecalVector3 = function (indexId) {
- var vertexId = indices[indexId];
- var result = new BABYLON.PositionNormalVertex();
- result.position = new BABYLON.Vector3(positions[vertexId * 3], positions[vertexId * 3 + 1], positions[vertexId * 3 + 2]);
- // Send vector to decal local world
- result.position = BABYLON.Vector3.TransformCoordinates(result.position, transformMatrix);
- // Get normal
- result.normal = new BABYLON.Vector3(normals[vertexId * 3], normals[vertexId * 3 + 1], normals[vertexId * 3 + 2]);
- return result;
- };
- // Inspired by https://github.com/mrdoob/three.js/blob/eee231960882f6f3b6113405f524956145148146/examples/js/geometries/DecalGeometry.js
- var clip = function (vertices, axis) {
- if (vertices.length === 0) {
- return vertices;
- }
- var clipSize = 0.5 * Math.abs(BABYLON.Vector3.Dot(size, axis));
- var clipVertices = function (v0, v1) {
- var clipFactor = BABYLON.Vector3.GetClipFactor(v0.position, v1.position, axis, clipSize);
- return new BABYLON.PositionNormalVertex(BABYLON.Vector3.Lerp(v0.position, v1.position, clipFactor), BABYLON.Vector3.Lerp(v0.normal, v1.normal, clipFactor));
- };
- var result = new Array();
- for (var index = 0; index < vertices.length; index += 3) {
- var v1Out;
- var v2Out;
- var v3Out;
- var total = 0;
- var nV1, nV2, nV3, nV4;
- var d1 = BABYLON.Vector3.Dot(vertices[index].position, axis) - clipSize;
- var d2 = BABYLON.Vector3.Dot(vertices[index + 1].position, axis) - clipSize;
- var d3 = BABYLON.Vector3.Dot(vertices[index + 2].position, axis) - clipSize;
- v1Out = d1 > 0;
- v2Out = d2 > 0;
- v3Out = d3 > 0;
- total = (v1Out ? 1 : 0) + (v2Out ? 1 : 0) + (v3Out ? 1 : 0);
- switch (total) {
- case 0:
- result.push(vertices[index]);
- result.push(vertices[index + 1]);
- result.push(vertices[index + 2]);
- break;
- case 1:
- if (v1Out) {
- nV1 = vertices[index + 1];
- nV2 = vertices[index + 2];
- nV3 = clipVertices(vertices[index], nV1);
- nV4 = clipVertices(vertices[index], nV2);
- }
- if (v2Out) {
- nV1 = vertices[index];
- nV2 = vertices[index + 2];
- nV3 = clipVertices(vertices[index + 1], nV1);
- nV4 = clipVertices(vertices[index + 1], nV2);
- result.push(nV3);
- result.push(nV2.clone());
- result.push(nV1.clone());
- result.push(nV2.clone());
- result.push(nV3.clone());
- result.push(nV4);
- break;
- }
- if (v3Out) {
- nV1 = vertices[index];
- nV2 = vertices[index + 1];
- nV3 = clipVertices(vertices[index + 2], nV1);
- nV4 = clipVertices(vertices[index + 2], nV2);
- }
- result.push(nV1.clone());
- result.push(nV2.clone());
- result.push(nV3);
- result.push(nV4);
- result.push(nV3.clone());
- result.push(nV2.clone());
- break;
- case 2:
- if (!v1Out) {
- nV1 = vertices[index].clone();
- nV2 = clipVertices(nV1, vertices[index + 1]);
- nV3 = clipVertices(nV1, vertices[index + 2]);
- result.push(nV1);
- result.push(nV2);
- result.push(nV3);
- }
- if (!v2Out) {
- nV1 = vertices[index + 1].clone();
- nV2 = clipVertices(nV1, vertices[index + 2]);
- nV3 = clipVertices(nV1, vertices[index]);
- result.push(nV1);
- result.push(nV2);
- result.push(nV3);
- }
- if (!v3Out) {
- nV1 = vertices[index + 2].clone();
- nV2 = clipVertices(nV1, vertices[index]);
- nV3 = clipVertices(nV1, vertices[index + 1]);
- result.push(nV1);
- result.push(nV2);
- result.push(nV3);
- }
- break;
- case 3:
- break;
- }
- }
- return result;
- };
- for (var index = 0; index < indices.length; index += 3) {
- var faceVertices = new Array();
- faceVertices.push(extractDecalVector3(index));
- faceVertices.push(extractDecalVector3(index + 1));
- faceVertices.push(extractDecalVector3(index + 2));
- // Clip
- faceVertices = clip(faceVertices, new BABYLON.Vector3(1, 0, 0));
- faceVertices = clip(faceVertices, new BABYLON.Vector3(-1, 0, 0));
- faceVertices = clip(faceVertices, new BABYLON.Vector3(0, 1, 0));
- faceVertices = clip(faceVertices, new BABYLON.Vector3(0, -1, 0));
- faceVertices = clip(faceVertices, new BABYLON.Vector3(0, 0, 1));
- faceVertices = clip(faceVertices, new BABYLON.Vector3(0, 0, -1));
- if (faceVertices.length === 0) {
- continue;
- }
- // Add UVs and get back to world
- var localRotationMatrix = BABYLON.Matrix.RotationYawPitchRoll(yaw, pitch, angle);
- for (var vIndex = 0; vIndex < faceVertices.length; vIndex++) {
- var vertex = faceVertices[vIndex];
- vertexData.indices.push(currentVertexDataIndex);
- vertex.position.toArray(vertexData.positions, currentVertexDataIndex * 3);
- vertex.normal.toArray(vertexData.normals, currentVertexDataIndex * 3);
- vertexData.uvs.push(0.5 + vertex.position.x / size.x);
- vertexData.uvs.push(0.5 + vertex.position.y / size.y);
- currentVertexDataIndex++;
- }
- }
- // Return mesh
- var decal = new Mesh(name, sourceMesh.getScene());
- vertexData.applyToMesh(decal);
- decal.position = position.clone();
- decal.rotation = new BABYLON.Vector3(pitch, yaw, angle);
- return decal;
- };
- // Tools
- Mesh.MinMax = function (meshes) {
- var minVector = null;
- var maxVector = null;
- for (var i in meshes) {
- var mesh = meshes[i];
- var boundingBox = mesh.getBoundingInfo().boundingBox;
- if (!minVector) {
- minVector = boundingBox.minimumWorld;
- maxVector = boundingBox.maximumWorld;
- continue;
- }
- minVector.MinimizeInPlace(boundingBox.minimumWorld);
- maxVector.MaximizeInPlace(boundingBox.maximumWorld);
- }
- return {
- min: minVector,
- max: maxVector
- };
- };
- Mesh.Center = function (meshesOrMinMaxVector) {
- var minMaxVector = meshesOrMinMaxVector.min !== undefined ? meshesOrMinMaxVector : Mesh.MinMax(meshesOrMinMaxVector);
- return BABYLON.Vector3.Center(minMaxVector.min, minMaxVector.max);
- };
- /**
- * Merge the array of meshes into a single mesh for performance reasons.
- * @param {Array<Mesh>} meshes - The vertices source. They should all be of the same material. Entries can empty
- * @param {boolean} disposeSource - When true (default), dispose of the vertices from the source meshes
- * @param {boolean} allow32BitsIndices - When the sum of the vertices > 64k, this must be set to true.
- * @param {Mesh} meshSubclass - When set, vertices inserted into this Mesh. Meshes can then be merged into a Mesh sub-class.
- */
- Mesh.MergeMeshes = function (meshes, disposeSource, allow32BitsIndices, meshSubclass) {
- if (disposeSource === void 0) { disposeSource = true; }
- if (!allow32BitsIndices) {
- var totalVertices = 0;
- for (var index = 0; index < meshes.length; index++) {
- if (meshes[index]) {
- totalVertices += meshes[index].getTotalVertices();
- if (totalVertices > 65536) {
- BABYLON.Tools.Warn("Cannot merge meshes because resulting mesh will have more than 65536 vertices. Please use allow32BitsIndices = true to use 32 bits indices");
- return null;
- }
- }
- }
- }
- // Merge
- var vertexData;
- var otherVertexData;
- var source;
- for (index = 0; index < meshes.length; index++) {
- if (meshes[index]) {
- meshes[index].computeWorldMatrix(true);
- otherVertexData = BABYLON.VertexData.ExtractFromMesh(meshes[index], true);
- otherVertexData.transform(meshes[index].getWorldMatrix());
- if (vertexData) {
- vertexData.merge(otherVertexData);
- }
- else {
- vertexData = otherVertexData;
- source = meshes[index];
- }
- }
- }
- if (!meshSubclass) {
- meshSubclass = new Mesh(source.name + "_merged", source.getScene());
- }
- vertexData.applyToMesh(meshSubclass);
- // Setting properties
- meshSubclass.material = source.material;
- meshSubclass.checkCollisions = source.checkCollisions;
- // Cleaning
- if (disposeSource) {
- for (index = 0; index < meshes.length; index++) {
- if (meshes[index]) {
- meshes[index].dispose();
- }
- }
- }
- return meshSubclass;
- };
- // Consts
- Mesh._FRONTSIDE = 0;
- Mesh._BACKSIDE = 1;
- Mesh._DOUBLESIDE = 2;
- Mesh._DEFAULTSIDE = 0;
- Mesh._NO_CAP = 0;
- Mesh._CAP_START = 1;
- Mesh._CAP_END = 2;
- Mesh._CAP_ALL = 3;
- return Mesh;
- })(BABYLON.AbstractMesh);
- BABYLON.Mesh = Mesh;
- })(BABYLON || (BABYLON = {}));
- //# sourceMappingURL=babylon.mesh.js.map
|