var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.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"], []); this.id = name + "." + source.id; // Material this.material = source.material; var index; if (!doNotCloneChildren) { // Children for (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); } } } // Particles 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 {Mesh} mesh - the mesh to be added as LOD level * @return {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 {Mesh} mesh - the mesh to be removed. * @return {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; // Ensure that subdivisionSize is a multiple of 3 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 (!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) { BABYLON.Tools.Warn("Mesh.updateVerticesDataDirectly deprecated since 2.3."); 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; if (this._unIndexed) { indexToBind = null; } else { 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._unIndexed ? null : 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(); // Draw order switch (fillMode) { case BABYLON.Material.PointFillMode: engine.drawPointClouds(subMesh.verticesStart, subMesh.verticesCount, instancesCount); break; case BABYLON.Material.WireFrameFillMode: if (this._unIndexed) { engine.drawUnIndexed(false, subMesh.verticesStart, subMesh.verticesCount, instancesCount); } else { engine.draw(false, 0, subMesh.linesIndexCount, instancesCount); } break; default: if (this._unIndexed) { engine.drawUnIndexed(true, subMesh.verticesStart, subMesh.verticesCount, instancesCount); } else { 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, enableAlphaMode) { 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; } var callbackIndex; for (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); // Alpha mode if (enableAlphaMode) { engine.setAlphaMode(effectiveMaterial.alphaMode); } // 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; var index; for (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 = []; var index; for (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).normalize().toArray(temp, index); } this.setVerticesData(BABYLON.VertexBuffer.NormalKind, temp, this.getVertexBuffer(BABYLON.VertexBuffer.NormalKind).isUpdatable()); // flip faces? if (transform.m[0] * transform.m[5] * transform.m[10] < 0) { this.flipFaces(); } }; // Will apply current transform to mesh and reset world matrix Mesh.prototype.bakeCurrentTransformIntoVertices = function () { this.bakeTransformIntoVertices(this.computeWorldMatrix(true)); this.scaling.copyFromFloats(1, 1, 1); this.position.copyFromFloats(0, 0, 0); this.rotation.copyFromFloats(0, 0, 0); //only if quaternion is already set if (this.rotationQuaternion) { this.rotationQuaternion = BABYLON.Quaternion.Identity(); } this._worldMatrix = BABYLON.Matrix.Identity(); }; // 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 () { /// Update normals and vertices to get a flat shading rendering. /// Warning: This may imply adding vertices to the mesh in order to get exactly 3 vertices per face var kinds = this.getVerticesDataKinds(); var vbs = []; var data = []; var newdata = []; var updatableNormals = false; var kindIndex; var kind; for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) { 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(); // Generating unique vertices per face var index; for (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)); // Store same normals for every vertex 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); // Updating vertex buffers 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(); }; Mesh.prototype.convertToUnIndexedMesh = function () { /// Remove indices by unfolding faces into buffers /// Warning: This implies adding vertices to the mesh in order to get exactly 3 vertices per face var kinds = this.getVerticesDataKinds(); var vbs = []; var data = []; var newdata = []; var updatableNormals = false; var kindIndex; var kind; for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) { kind = kinds[kindIndex]; var vertexBuffer = this.getVertexBuffer(kind); 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(); // Generating unique vertices per face var index; for (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 indices for (index = 0; index < totalIndices; index += 3) { indices[index] = index; indices[index + 1] = index + 1; indices[index + 2] = index + 2; } this.setIndices(indices); // Updating vertex buffers 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._unIndexed = true; this.synchronizeInstances(); }; // will inverse faces orientations, and invert normals too if specified Mesh.prototype.flipFaces = function (flipNormals) { if (flipNormals === void 0) { flipNormals = false; } var vertex_data = BABYLON.VertexData.ExtractFromMesh(this); var i; if (flipNormals && this.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) { for (i = 0; i < vertex_data.normals.length; i++) { vertex_data.normals[i] *= -1; } } var temp; for (i = 0; i < vertex_data.indices.length; i += 3) { // reassign indices temp = vertex_data.indices[i + 1]; vertex_data.indices[i + 1] = vertex_data.indices[i + 2]; vertex_data.indices[i + 2] = temp; } vertex_data.applyToMesh(this); }; // 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 = BABYLON.SimplificationType.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.Parse = function (parsedMesh, scene, rootUrl) { var mesh = new Mesh(parsedMesh.name, scene); mesh.id = parsedMesh.id; BABYLON.Tags.AddTagsTo(mesh, parsedMesh.tags); mesh.position = BABYLON.Vector3.FromArray(parsedMesh.position); if (parsedMesh.rotationQuaternion) { mesh.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedMesh.rotationQuaternion); } else if (parsedMesh.rotation) { mesh.rotation = BABYLON.Vector3.FromArray(parsedMesh.rotation); } mesh.scaling = BABYLON.Vector3.FromArray(parsedMesh.scaling); if (parsedMesh.localMatrix) { mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.localMatrix)); } else if (parsedMesh.pivotMatrix) { mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.pivotMatrix)); } mesh.setEnabled(parsedMesh.isEnabled); mesh.isVisible = parsedMesh.isVisible; mesh.infiniteDistance = parsedMesh.infiniteDistance; mesh.showBoundingBox = parsedMesh.showBoundingBox; mesh.showSubMeshesBoundingBox = parsedMesh.showSubMeshesBoundingBox; if (parsedMesh.applyFog !== undefined) { mesh.applyFog = parsedMesh.applyFog; } if (parsedMesh.pickable !== undefined) { mesh.isPickable = parsedMesh.pickable; } if (parsedMesh.alphaIndex !== undefined) { mesh.alphaIndex = parsedMesh.alphaIndex; } mesh.receiveShadows = parsedMesh.receiveShadows; mesh.billboardMode = parsedMesh.billboardMode; if (parsedMesh.visibility !== undefined) { mesh.visibility = parsedMesh.visibility; } mesh.checkCollisions = parsedMesh.checkCollisions; mesh._shouldGenerateFlatShading = parsedMesh.useFlatShading; // freezeWorldMatrix if (parsedMesh.freezeWorldMatrix) { mesh._waitingFreezeWorldMatrix = parsedMesh.freezeWorldMatrix; } // Parent if (parsedMesh.parentId) { mesh._waitingParentId = parsedMesh.parentId; } // Actions if (parsedMesh.actions !== undefined) { mesh._waitingActions = parsedMesh.actions; } // Geometry mesh.hasVertexAlpha = parsedMesh.hasVertexAlpha; if (parsedMesh.delayLoadingFile) { mesh.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED; mesh.delayLoadingFile = rootUrl + parsedMesh.delayLoadingFile; mesh._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMaximum)); if (parsedMesh._binaryInfo) { mesh._binaryInfo = parsedMesh._binaryInfo; } mesh._delayInfo = []; if (parsedMesh.hasUVs) { mesh._delayInfo.push(BABYLON.VertexBuffer.UVKind); } if (parsedMesh.hasUVs2) { mesh._delayInfo.push(BABYLON.VertexBuffer.UV2Kind); } if (parsedMesh.hasUVs3) { mesh._delayInfo.push(BABYLON.VertexBuffer.UV3Kind); } if (parsedMesh.hasUVs4) { mesh._delayInfo.push(BABYLON.VertexBuffer.UV4Kind); } if (parsedMesh.hasUVs5) { mesh._delayInfo.push(BABYLON.VertexBuffer.UV5Kind); } if (parsedMesh.hasUVs6) { mesh._delayInfo.push(BABYLON.VertexBuffer.UV6Kind); } if (parsedMesh.hasColors) { mesh._delayInfo.push(BABYLON.VertexBuffer.ColorKind); } if (parsedMesh.hasMatricesIndices) { mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind); } if (parsedMesh.hasMatricesWeights) { mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind); } mesh._delayLoadingFunction = BABYLON.Geometry.ImportGeometry; if (BABYLON.SceneLoader.ForceFullSceneLoadingForIncremental) { mesh._checkDelayState(); } } else { BABYLON.Geometry.ImportGeometry(parsedMesh, mesh); } // Material if (parsedMesh.materialId) { mesh.setMaterialByID(parsedMesh.materialId); } else { mesh.material = null; } // Skeleton if (parsedMesh.skeletonId > -1) { mesh.skeleton = scene.getLastSkeletonByID(parsedMesh.skeletonId); if (parsedMesh.numBoneInfluencers) { mesh.numBoneInfluencers = parsedMesh.numBoneInfluencers; } } // Physics if (parsedMesh.physicsImpostor) { if (!scene.isPhysicsEnabled()) { scene.enablePhysics(); } mesh.setPhysicsState({ impostor: parsedMesh.physicsImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution }); } // Animations if (parsedMesh.animations) { for (var animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) { var parsedAnimation = parsedMesh.animations[animationIndex]; mesh.animations.push(BABYLON.Animation.Parse(parsedAnimation)); } } if (parsedMesh.autoAnimate) { scene.beginAnimation(mesh, parsedMesh.autoAnimateFrom, parsedMesh.autoAnimateTo, parsedMesh.autoAnimateLoop, 1.0); } // Layer Mask if (parsedMesh.layerMask && (!isNaN(parsedMesh.layerMask))) { mesh.layerMask = Math.abs(parseInt(parsedMesh.layerMask)); } else { mesh.layerMask = 0x0FFFFFFF; } // Instances if (parsedMesh.instances) { for (var index = 0; index < parsedMesh.instances.length; index++) { var parsedInstance = parsedMesh.instances[index]; var instance = mesh.createInstance(parsedInstance.name); BABYLON.Tags.AddTagsTo(instance, parsedInstance.tags); instance.position = BABYLON.Vector3.FromArray(parsedInstance.position); if (parsedInstance.rotationQuaternion) { instance.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedInstance.rotationQuaternion); } else if (parsedInstance.rotation) { instance.rotation = BABYLON.Vector3.FromArray(parsedInstance.rotation); } instance.scaling = BABYLON.Vector3.FromArray(parsedInstance.scaling); instance.checkCollisions = mesh.checkCollisions; if (parsedMesh.animations) { for (animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) { parsedAnimation = parsedMesh.animations[animationIndex]; instance.animations.push(BABYLON.Animation.Parse(parsedAnimation)); } } } } return mesh; }; Mesh.CreateRibbon = function (name, pathArray, closeArray, closePath, offset, scene, updatable, sideOrientation, instance) { return BABYLON.MeshBuilder.CreateRibbon(name, { pathArray: pathArray, closeArray: closeArray, closePath: closePath, offset: offset, updatable: updatable, sideOrientation: sideOrientation, instance: instance }, scene); }; Mesh.CreateDisc = function (name, radius, tessellation, scene, updatable, sideOrientation) { var options = { radius: radius, tessellation: tessellation, sideOrientation: sideOrientation, updatable: updatable }; return BABYLON.MeshBuilder.CreateDisc(name, options, scene); }; Mesh.CreateBox = function (name, size, scene, updatable, sideOrientation) { var options = { size: size, sideOrientation: sideOrientation, updatable: updatable }; return BABYLON.MeshBuilder.CreateBox(name, options, scene); }; Mesh.CreateSphere = function (name, segments, diameter, scene, updatable, sideOrientation) { var options = { segments: segments, diameterX: diameter, diameterY: diameter, diameterZ: diameter, sideOrientation: sideOrientation, updatable: updatable }; return BABYLON.MeshBuilder.CreateSphere(name, options, scene); }; // Cylinder and cone Mesh.CreateCylinder = function (name, height, diameterTop, diameterBottom, tessellation, subdivisions, scene, updatable, sideOrientation) { if (scene === undefined || !(scene instanceof BABYLON.Scene)) { if (scene !== undefined) { sideOrientation = updatable || Mesh.DEFAULTSIDE; updatable = scene; } scene = subdivisions; subdivisions = 1; } var options = { height: height, diameterTop: diameterTop, diameterBottom: diameterBottom, tessellation: tessellation, subdivisions: subdivisions, sideOrientation: sideOrientation, updatable: updatable }; return BABYLON.MeshBuilder.CreateCylinder(name, options, scene); }; // Torus (Code from SharpDX.org) Mesh.CreateTorus = function (name, diameter, thickness, tessellation, scene, updatable, sideOrientation) { var options = { diameter: diameter, thickness: thickness, tessellation: tessellation, sideOrientation: sideOrientation, updatable: updatable }; return BABYLON.MeshBuilder.CreateTorus(name, options, scene); }; Mesh.CreateTorusKnot = function (name, radius, tube, radialSegments, tubularSegments, p, q, scene, updatable, sideOrientation) { var options = { radius: radius, tube: tube, radialSegments: radialSegments, tubularSegments: tubularSegments, p: p, q: q, sideOrientation: sideOrientation, updatable: updatable }; return BABYLON.MeshBuilder.CreateTorusKnot(name, options, scene); }; // Lines Mesh.CreateLines = function (name, points, scene, updatable, instance) { var options = { points: points, updatable: updatable, instance: instance }; return BABYLON.MeshBuilder.CreateLines(name, options, scene); }; // Dashed Lines Mesh.CreateDashedLines = function (name, points, dashSize, gapSize, dashNb, scene, updatable, instance) { var options = { points: points, dashSize: dashSize, gapSize: gapSize, dashNb: dashNb, updatable: updatable }; return BABYLON.MeshBuilder.CreateDashedLines(name, options, scene); }; // Extrusion Mesh.ExtrudeShape = function (name, shape, path, scale, rotation, cap, scene, updatable, sideOrientation, instance) { var options = { shape: shape, path: path, scale: scale, rotation: rotation, cap: (cap === 0) ? 0 : cap || Mesh.NO_CAP, sideOrientation: sideOrientation, instance: instance, updatable: updatable }; return BABYLON.MeshBuilder.ExtrudeShape(name, options, scene); }; Mesh.ExtrudeShapeCustom = function (name, shape, path, scaleFunction, rotationFunction, ribbonCloseArray, ribbonClosePath, cap, scene, updatable, sideOrientation, instance) { var options = { shape: shape, path: path, scaleFunction: scaleFunction, rotationFunction: rotationFunction, ribbonCloseArray: ribbonCloseArray, ribbonClosePath: ribbonClosePath, cap: (cap === 0) ? 0 : cap || Mesh.NO_CAP, sideOrientation: sideOrientation, instance: instance, updatable: updatable }; return BABYLON.MeshBuilder.ExtrudeShapeCustom(name, options, scene); }; // Lathe Mesh.CreateLathe = function (name, shape, radius, tessellation, scene, updatable, sideOrientation) { var options = { shape: shape, radius: radius, tessellation: tessellation, sideOrientation: sideOrientation, updatable: updatable }; return BABYLON.MeshBuilder.CreateLathe(name, options, scene); }; // Plane & ground Mesh.CreatePlane = function (name, size, scene, updatable, sideOrientation) { var options = { size: size, width: size, height: size, sideOrientation: sideOrientation, updatable: updatable }; return BABYLON.MeshBuilder.CreatePlane(name, options, scene); }; Mesh.CreateGround = function (name, width, height, subdivisions, scene, updatable) { var options = { width: width, height: height, subdivisions: subdivisions, updatable: updatable }; return BABYLON.MeshBuilder.CreateGround(name, options, scene); }; Mesh.CreateTiledGround = function (name, xmin, zmin, xmax, zmax, subdivisions, precision, scene, updatable) { var options = { xmin: xmin, zmin: zmin, xmax: xmax, zmax: zmax, subdivisions: subdivisions, precision: precision, updatable: updatable }; return BABYLON.MeshBuilder.CreateTiledGround(name, options, scene); }; Mesh.CreateGroundFromHeightMap = function (name, url, width, height, subdivisions, minHeight, maxHeight, scene, updatable, onReady) { var options = { width: width, height: height, subdivisions: subdivisions, minHeight: minHeight, maxHeight: maxHeight, updatable: updatable, onReady: onReady }; return BABYLON.MeshBuilder.CreateGroundFromHeightMap(name, url, options, scene); }; Mesh.CreateTube = function (name, path, radius, tessellation, radiusFunction, cap, scene, updatable, sideOrientation, instance) { var options = { path: path, radius: radius, tessellation: tessellation, radiusFunction: radiusFunction, arc: 1, cap: cap, updatable: updatable, sideOrientation: sideOrientation, instance: instance }; return BABYLON.MeshBuilder.CreateTube(name, options, scene); }; Mesh.CreatePolyhedron = function (name, options, scene) { return BABYLON.MeshBuilder.CreatePolyhedron(name, options, scene); }; Mesh.CreateIcoSphere = function (name, options, scene) { return BABYLON.MeshBuilder.CreateIcoSphere(name, options, scene); }; // Decals Mesh.CreateDecal = function (name, sourceMesh, position, normal, size, angle) { var options = { position: position, normal: normal, size: size, angle: angle }; return BABYLON.MeshBuilder.CreateDecal(name, sourceMesh, options); }; // Skeletons /** * @returns original positions used for CPU skinning. Useful for integrating Morphing with skeletons in same mesh. */ Mesh.prototype.setPositionsForCPUSkinning = function () { var source; if (!this._sourcePositions) { source = this.getVerticesData(BABYLON.VertexBuffer.PositionKind); this._sourcePositions = new Float32Array(source); if (!this.getVertexBuffer(BABYLON.VertexBuffer.PositionKind).isUpdatable()) { this.setVerticesData(BABYLON.VertexBuffer.PositionKind, source, true); } } return this._sourcePositions; }; /** * @returns original normals used for CPU skinning. Useful for integrating Morphing with skeletons in same mesh. */ Mesh.prototype.setNormalsForCPUSkinning = function () { var source; if (!this._sourceNormals) { source = this.getVerticesData(BABYLON.VertexBuffer.NormalKind); this._sourceNormals = new Float32Array(source); if (!this.getVertexBuffer(BABYLON.VertexBuffer.NormalKind).isUpdatable()) { this.setVerticesData(BABYLON.VertexBuffer.NormalKind, source, true); } } return this._sourceNormals; }; /** * Update the vertex buffers by applying transformation from the bones * @param {skeleton} skeleton to apply */ Mesh.prototype.applySkeleton = function (skeleton) { if (!this.geometry) { return; } if (this.geometry._softwareSkinningRenderId == this.getScene().getRenderId()) { return; } this.geometry._softwareSkinningRenderId = this.getScene().getRenderId(); if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) { return this; } if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) { return this; } if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind)) { return this; } if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) { return this; } if (!this._sourcePositions) { this.setPositionsForCPUSkinning(); } if (!this._sourceNormals) { this.setNormalsForCPUSkinning(); } // positionsData checks for not being Float32Array will only pass at most once var positionsData = this.getVerticesData(BABYLON.VertexBuffer.PositionKind); if (!(positionsData instanceof Float32Array)) { positionsData = new Float32Array(positionsData); } // normalsData checks for not being Float32Array will only pass at most once var normalsData = this.getVerticesData(BABYLON.VertexBuffer.NormalKind); if (!(normalsData instanceof Float32Array)) { normalsData = new Float32Array(normalsData); } var matricesIndicesData = this.getVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind); var matricesWeightsData = this.getVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind); var needExtras = this.numBoneInfluencers > 4; var matricesIndicesExtraData = needExtras ? this.getVerticesData(BABYLON.VertexBuffer.MatricesIndicesExtraKind) : null; var matricesWeightsExtraData = needExtras ? this.getVerticesData(BABYLON.VertexBuffer.MatricesWeightsExtraKind) : null; var skeletonMatrices = skeleton.getTransformMatrices(); var tempVector3 = BABYLON.Vector3.Zero(); var finalMatrix = new BABYLON.Matrix(); var tempMatrix = new BABYLON.Matrix(); var matWeightIdx = 0; var inf; for (var index = 0; index < positionsData.length; index += 3, matWeightIdx += 4) { var weight; for (inf = 0; inf < 4; inf++) { weight = matricesWeightsData[matWeightIdx + inf]; if (weight > 0) { BABYLON.Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesData[matWeightIdx + inf] * 16, weight, tempMatrix); finalMatrix.addToSelf(tempMatrix); } else break; } if (needExtras) { for (inf = 0; inf < 4; inf++) { weight = matricesWeightsExtraData[matWeightIdx + inf]; if (weight > 0) { BABYLON.Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesExtraData[matWeightIdx + inf] * 16, weight, tempMatrix); finalMatrix.addToSelf(tempMatrix); } else break; } } BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(this._sourcePositions[index], this._sourcePositions[index + 1], this._sourcePositions[index + 2], finalMatrix, tempVector3); tempVector3.toArray(positionsData, index); BABYLON.Vector3.TransformNormalFromFloatsToRef(this._sourceNormals[index], this._sourceNormals[index + 1], this._sourceNormals[index + 2], finalMatrix, tempVector3); tempVector3.toArray(normalsData, index); finalMatrix.reset(); } this.updateVerticesData(BABYLON.VertexBuffer.PositionKind, positionsData); this.updateVerticesData(BABYLON.VertexBuffer.NormalKind, normalsData); return this; }; // 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} 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; } var index; if (!allow32BitsIndices) { var totalVertices = 0; // Counting vertices for (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 = {}));