Skip to content

Commit

Permalink
Fix #6526
Browse files Browse the repository at this point in the history
  • Loading branch information
deltakosh committed Jun 25, 2019
1 parent 2ea0f34 commit 3b7c0f8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
26 changes: 23 additions & 3 deletions src/Bones/skeleton.ts
Expand Up @@ -74,7 +74,7 @@ export class Skeleton implements IAnimatable {
private _useTextureToStoreBoneMatrices = true;
/**
* Gets or sets a boolean indicating that bone matrices should be stored as a texture instead of using shader uniforms (default is true).
* Please note that this option is not available when needInitialSkinMatrix === true or if the hardware does not support it
* Please note that this option is not available if the hardware does not support it
*/
public get useTextureToStoreBoneMatrices(): boolean {
return this._useTextureToStoreBoneMatrices;
Expand Down Expand Up @@ -118,7 +118,7 @@ export class Skeleton implements IAnimatable {
* Gets a boolean indicating that the skeleton effectively stores matrices into a texture
*/
public get isUsingTextureForMatrices() {
return this.useTextureToStoreBoneMatrices && this._canUseTextureForBones && !this.needInitialSkinMatrix;
return this.useTextureToStoreBoneMatrices && this._canUseTextureForBones;
}

/**
Expand Down Expand Up @@ -191,7 +191,11 @@ export class Skeleton implements IAnimatable {
* Gets the list of transform matrices to send to shaders inside a texture (one matrix per bone)
* @returns a raw texture containing the data
*/
public getTransformMatrixTexture(): Nullable<RawTexture> {
public getTransformMatrixTexture(mesh: AbstractMesh): Nullable<RawTexture> {
if (this.needInitialSkinMatrix && mesh._transformMatrixTexture) {
return mesh._transformMatrixTexture;
}

return this._transformMatrixTexture;
}

Expand Down Expand Up @@ -475,9 +479,25 @@ export class Skeleton implements IAnimatable {
bone._updateDifferenceMatrix(Tmp.Matrix[1]);
}
}

if (this.isUsingTextureForMatrices) {
const textureWidth = (this.bones.length + 1) * 4;
if (!mesh._transformMatrixTexture || mesh._transformMatrixTexture.getSize().width !== textureWidth) {

if (mesh._transformMatrixTexture) {
mesh._transformMatrixTexture.dispose();
}

mesh._transformMatrixTexture = RawTexture.CreateRGBATexture(mesh._bonesTransformMatrices, (this.bones.length + 1) * 4, 1, this._scene, false, false, Constants.TEXTURE_NEAREST_SAMPLINGMODE, Constants.TEXTURETYPE_FLOAT);
}
}
}

this._computeTransformMatrices(mesh._bonesTransformMatrices, poseMatrix);

if (this.isUsingTextureForMatrices && mesh._transformMatrixTexture) {
mesh._transformMatrixTexture.update(mesh._bonesTransformMatrices);
}
}
} else {
if (!this._transformMatrices || this._transformMatrices.length !== 16 * (this.bones.length + 1)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Lights/Shadows/shadowGenerator.ts
Expand Up @@ -1001,7 +1001,7 @@ export class ShadowGenerator implements IShadowGenerator {
const skeleton = mesh.skeleton;

if (skeleton.isUsingTextureForMatrices) {
const boneTexture = skeleton.getTransformMatrixTexture();
const boneTexture = skeleton.getTransformMatrixTexture(mesh);

if (!boneTexture) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Materials/materialHelper.ts
Expand Up @@ -762,7 +762,7 @@ export class MaterialHelper {
const skeleton = mesh.skeleton;

if (skeleton.isUsingTextureForMatrices && effect.getUniformIndex("boneTextureWidth") > -1) {
const boneTexture = skeleton.getTransformMatrixTexture();
const boneTexture = skeleton.getTransformMatrixTexture(mesh);
effect.setTexture("boneSampler", boneTexture);
effect.setFloat("boneTextureWidth", 4.0 * (skeleton.bones.length + 1));
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/Meshes/abstractMesh.ts
Expand Up @@ -23,6 +23,7 @@ import { Constants } from "../Engines/constants";
import { AbstractActionManager } from '../Actions/abstractActionManager';
import { _MeshCollisionData } from '../Collisions/meshCollisionData';
import { _DevTools } from '../Misc/devTools';
import { RawTexture } from '../Materials/Textures/rawTexture';

declare type Ray = import("../Culling/ray").Ray;
declare type Collider = import("../Collisions/collider").Collider;
Expand Down Expand Up @@ -608,6 +609,9 @@ export class AbstractMesh extends TransformNode implements IDisposable, ICullabl
/** @hidden */
public _bonesTransformMatrices: Nullable<Float32Array> = null;

/** @hidden */
public _transformMatrixTexture: Nullable<RawTexture> = null;

/**
* Gets or sets a skeleton to apply skining transformations
* @see http://doc.babylonjs.com/how_to/how_to_use_bones_and_skeletons
Expand Down Expand Up @@ -1570,6 +1574,11 @@ export class AbstractMesh extends TransformNode implements IDisposable, ICullabl
// Skeleton
this._internalAbstractMeshDataInfo._skeleton = null;

if (this._transformMatrixTexture) {
this._transformMatrixTexture.dispose();
this._transformMatrixTexture = null;
}

// Intersections in progress
for (index = 0; index < this._intersectionsInProgress.length; index++) {
var other = this._intersectionsInProgress[index];
Expand Down

0 comments on commit 3b7c0f8

Please sign in to comment.