skeletonViewer.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. import { Vector3, Matrix, TmpVectors } from "../Maths/math.vector";
  2. import { Color3 } from '../Maths/math.color';
  3. import { Scene } from "../scene";
  4. import { Nullable } from "../types";
  5. import { Bone } from "../Bones/bone";
  6. import { Skeleton } from "../Bones/skeleton";
  7. import { AbstractMesh } from "../Meshes/abstractMesh";
  8. import { Mesh } from "../Meshes/mesh";
  9. import { LinesMesh } from "../Meshes/linesMesh";
  10. import { LinesBuilder } from "../Meshes/Builders/linesBuilder";
  11. import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer";
  12. import { Material } from '../Materials/material';
  13. import { ShaderMaterial } from '../Materials/shaderMaterial';
  14. import { DynamicTexture } from '../Materials/Textures/dynamicTexture';
  15. import { VertexBuffer } from '../Meshes/buffer';
  16. import { Effect } from '../Materials/effect';
  17. import { ISkeletonViewerOptions, IBoneWeightShaderOptions, ISkeletonMapShaderOptions, ISkeletonMapShaderColorMapKnot } from './ISkeletonViewer';
  18. import { Observer } from '../Misc/observable';
  19. import { SphereBuilder } from '../Meshes/Builders/sphereBuilder';
  20. import { ShapeBuilder } from '../Meshes/Builders/shapeBuilder';
  21. /**
  22. * Class used to render a debug view of a given skeleton
  23. * @see http://www.babylonjs-playground.com/#1BZJVJ#8
  24. */
  25. export class SkeletonViewer {
  26. /** public Display constants BABYLON.SkeletonViewer.DISPLAY_LINES */
  27. public static readonly DISPLAY_LINES = 0;
  28. /** public Display constants BABYLON.SkeletonViewer.DISPLAY_SPHERES */
  29. public static readonly DISPLAY_SPHERES = 1;
  30. /** public Display constants BABYLON.SkeletonViewer.DISPLAY_SPHERE_AND_SPURS */
  31. public static readonly DISPLAY_SPHERE_AND_SPURS = 2;
  32. /** public static method to create a BoneWeight Shader
  33. * @param options The constructor options
  34. * @param scene The scene that the shader is scoped to
  35. * @returns The created ShaderMaterial
  36. * @see http://www.babylonjs-playground.com/#1BZJVJ#395
  37. */
  38. static CreateBoneWeightShader(options: IBoneWeightShaderOptions, scene: Scene): ShaderMaterial {
  39. let skeleton: Skeleton = options.skeleton;
  40. let colorBase: Color3 = options.colorBase ?? Color3.Black();
  41. let colorZero: Color3 = options.colorZero ?? Color3.Blue();
  42. let colorQuarter: Color3 = options.colorQuarter ?? Color3.Green();
  43. let colorHalf: Color3 = options.colorHalf ?? Color3.Yellow();
  44. let colorFull: Color3 = options.colorFull ?? Color3.Red();
  45. let targetBoneIndex: number = options.targetBoneIndex ?? 0;
  46. Effect.ShadersStore['boneWeights:' + skeleton.name + "VertexShader"] =
  47. `precision highp float;
  48. attribute vec3 position;
  49. attribute vec2 uv;
  50. uniform mat4 view;
  51. uniform mat4 projection;
  52. uniform mat4 worldViewProjection;
  53. #include<bonesDeclaration>
  54. #include<instancesDeclaration>
  55. varying vec3 vColor;
  56. uniform vec3 colorBase;
  57. uniform vec3 colorZero;
  58. uniform vec3 colorQuarter;
  59. uniform vec3 colorHalf;
  60. uniform vec3 colorFull;
  61. uniform float targetBoneIndex;
  62. void main() {
  63. vec3 positionUpdated = position;
  64. #include<instancesVertex>
  65. #include<bonesVertex>
  66. vec4 worldPos = finalWorld * vec4(positionUpdated, 1.0);
  67. vec3 color = colorBase;
  68. float totalWeight = 0.;
  69. if(matricesIndices[0] == targetBoneIndex && matricesWeights[0] > 0.){
  70. totalWeight += matricesWeights[0];
  71. }
  72. if(matricesIndices[1] == targetBoneIndex && matricesWeights[1] > 0.){
  73. totalWeight += matricesWeights[1];
  74. }
  75. if(matricesIndices[2] == targetBoneIndex && matricesWeights[2] > 0.){
  76. totalWeight += matricesWeights[2];
  77. }
  78. if(matricesIndices[3] == targetBoneIndex && matricesWeights[3] > 0.){
  79. totalWeight += matricesWeights[3];
  80. }
  81. color = mix(color, colorZero, smoothstep(0., 0.25, totalWeight));
  82. color = mix(color, colorQuarter, smoothstep(0.25, 0.5, totalWeight));
  83. color = mix(color, colorHalf, smoothstep(0.5, 0.75, totalWeight));
  84. color = mix(color, colorFull, smoothstep(0.75, 1.0, totalWeight));
  85. vColor = color;
  86. gl_Position = projection * view * worldPos;
  87. }`;
  88. Effect.ShadersStore['boneWeights:' + skeleton.name + "FragmentShader"] =
  89. `
  90. precision highp float;
  91. varying vec3 vPosition;
  92. varying vec3 vColor;
  93. void main() {
  94. vec4 color = vec4(vColor, 1.0);
  95. gl_FragColor = color;
  96. }
  97. `;
  98. let shader: ShaderMaterial = new ShaderMaterial('boneWeight:' + skeleton.name, scene,
  99. {
  100. vertex: 'boneWeights:' + skeleton.name,
  101. fragment: 'boneWeights:' + skeleton.name
  102. },
  103. {
  104. attributes: ['position', 'normal'],
  105. uniforms: [
  106. 'world', 'worldView', 'worldViewProjection', 'view', 'projection', 'viewProjection',
  107. 'colorBase', 'colorZero', 'colorQuarter', 'colorHalf', 'colorFull', 'targetBoneIndex'
  108. ]
  109. });
  110. shader.setColor3('colorBase', colorBase);
  111. shader.setColor3('colorZero', colorZero);
  112. shader.setColor3('colorQuarter', colorQuarter);
  113. shader.setColor3('colorHalf', colorHalf);
  114. shader.setColor3('colorFull', colorFull);
  115. shader.setFloat('targetBoneIndex', targetBoneIndex);
  116. shader.getClassName = (): string => {
  117. return "BoneWeightShader";
  118. };
  119. shader.transparencyMode = Material.MATERIAL_OPAQUE;
  120. return shader;
  121. }
  122. /** public static method to create a BoneWeight Shader
  123. * @param options The constructor options
  124. * @param scene The scene that the shader is scoped to
  125. * @returns The created ShaderMaterial
  126. */
  127. static CreateSkeletonMapShader(options: ISkeletonMapShaderOptions, scene: Scene) {
  128. let skeleton: Skeleton = options.skeleton;
  129. let colorMap: ISkeletonMapShaderColorMapKnot[] = options.colorMap ?? [
  130. {
  131. color: new Color3(1, 0.38, 0.18),
  132. location : 0
  133. },
  134. {
  135. color: new Color3(.59, 0.18, 1.00),
  136. location : 0.2
  137. },
  138. {
  139. color: new Color3(0.59, 1, 0.18),
  140. location : 0.4
  141. },
  142. {
  143. color: new Color3(1, 0.87, 0.17),
  144. location : 0.6
  145. },
  146. {
  147. color: new Color3(1, 0.17, 0.42),
  148. location : 0.8
  149. },
  150. {
  151. color: new Color3(0.17, 0.68, 1.0),
  152. location : 1.0
  153. }
  154. ];
  155. let bufferWidth: number = skeleton.bones.length + 1;
  156. let colorMapBuffer: number[] = SkeletonViewer._CreateBoneMapColorBuffer(bufferWidth, colorMap, scene);
  157. let shader = new ShaderMaterial('boneWeights:' + skeleton.name, scene,
  158. {
  159. vertexSource:
  160. `precision highp float;
  161. attribute vec3 position;
  162. attribute vec2 uv;
  163. uniform mat4 view;
  164. uniform mat4 projection;
  165. uniform mat4 worldViewProjection;
  166. uniform float colorMap[` + ((skeleton.bones.length) * 4) + `];
  167. #include<bonesDeclaration>
  168. #include<instancesDeclaration>
  169. varying vec3 vColor;
  170. void main() {
  171. vec3 positionUpdated = position;
  172. #include<instancesVertex>
  173. #include<bonesVertex>
  174. vec3 color = vec3(0.);
  175. bool first = true;
  176. for (int i = 0; i < 4; i++) {
  177. int boneIdx = int(matricesIndices[i]);
  178. float boneWgt = matricesWeights[i];
  179. vec3 c = vec3(colorMap[boneIdx * 4 + 0], colorMap[boneIdx * 4 + 1], colorMap[boneIdx * 4 + 2]);
  180. if (boneWgt > 0.) {
  181. if (first) {
  182. first = false;
  183. color = c;
  184. } else {
  185. color = mix(color, c, boneWgt);
  186. }
  187. }
  188. }
  189. vColor = color;
  190. vec4 worldPos = finalWorld * vec4(positionUpdated, 1.0);
  191. gl_Position = projection * view * worldPos;
  192. }`,
  193. fragmentSource:
  194. `
  195. precision highp float;
  196. varying vec3 vColor;
  197. void main() {
  198. vec4 color = vec4( vColor, 1.0 );
  199. gl_FragColor = color;
  200. }
  201. `
  202. },
  203. {
  204. attributes: ['position', 'normal'],
  205. uniforms: [
  206. 'world', 'worldView', 'worldViewProjection', 'view', 'projection', 'viewProjection',
  207. 'colorMap'
  208. ]
  209. });
  210. shader.setFloats('colorMap', colorMapBuffer);
  211. shader.getClassName = (): string => {
  212. return "SkeletonMapShader";
  213. };
  214. shader.transparencyMode = Material.MATERIAL_OPAQUE;
  215. return shader;
  216. }
  217. /** private static method to create a BoneWeight Shader
  218. * @param size The size of the buffer to create (usually the bone count)
  219. * @param colorMap The gradient data to generate
  220. * @param scene The scene that the shader is scoped to
  221. * @returns an Array of floats from the color gradient values
  222. */
  223. private static _CreateBoneMapColorBuffer(size: number, colorMap: ISkeletonMapShaderColorMapKnot[], scene: Scene) {
  224. let tempGrad = new DynamicTexture('temp', {width: size, height: 1}, scene, false);
  225. let ctx = tempGrad.getContext();
  226. let grad = ctx.createLinearGradient(0, 0, size, 0);
  227. colorMap.forEach((stop) => {
  228. grad.addColorStop(stop.location, stop.color.toHexString());
  229. });
  230. ctx.fillStyle = grad;
  231. ctx.fillRect(0, 0, size, 1);
  232. tempGrad.update();
  233. let buffer: number[] = [];
  234. let data: Uint8ClampedArray = ctx.getImageData(0, 0, size, 1).data;
  235. let rUnit = 1 / 255;
  236. for (let i = 0; i < data.length; i++) {
  237. buffer.push(data[i] * rUnit);
  238. }
  239. tempGrad.dispose();
  240. return buffer;
  241. }
  242. /** If SkeletonViewer scene scope. */
  243. private _scene : Scene;
  244. /** Gets or sets the color used to render the skeleton */
  245. public color: Color3 = Color3.White();
  246. /** Array of the points of the skeleton fo the line view. */
  247. private _debugLines = new Array<Array<Vector3>>();
  248. /** The SkeletonViewers Mesh. */
  249. private _debugMesh: Nullable<LinesMesh>;
  250. /** If SkeletonViewer is enabled. */
  251. private _isEnabled = false;
  252. /** If SkeletonViewer is ready. */
  253. private _ready : boolean;
  254. /** SkeletonViewer render observable. */
  255. private _obs: Nullable<Observer<Scene>> = null;
  256. /** The Utility Layer to render the gizmos in. */
  257. private _utilityLayer: Nullable<UtilityLayerRenderer>;
  258. private _boneIndices: Set<number>;
  259. /** Gets the Scene. */
  260. get scene(): Scene {
  261. return this._scene;
  262. }
  263. /** Gets the utilityLayer. */
  264. get utilityLayer(): Nullable<UtilityLayerRenderer> {
  265. return this._utilityLayer;
  266. }
  267. /** Checks Ready Status. */
  268. get isReady(): Boolean {
  269. return this._ready;
  270. }
  271. /** Sets Ready Status. */
  272. set ready(value: boolean) {
  273. this._ready = value;
  274. }
  275. /** Gets the debugMesh */
  276. get debugMesh(): Nullable<AbstractMesh> | Nullable<LinesMesh> {
  277. return this._debugMesh;
  278. }
  279. /** Sets the debugMesh */
  280. set debugMesh(value: Nullable<AbstractMesh> | Nullable<LinesMesh>) {
  281. this._debugMesh = (value as any);
  282. }
  283. /** Gets the displayMode */
  284. get displayMode(): number {
  285. return this.options.displayMode || SkeletonViewer.DISPLAY_LINES;
  286. }
  287. /** Sets the displayMode */
  288. set displayMode(value: number) {
  289. if (value > SkeletonViewer.DISPLAY_SPHERE_AND_SPURS) {
  290. value = SkeletonViewer.DISPLAY_LINES;
  291. }
  292. this.options.displayMode = value;
  293. }
  294. /**
  295. * Creates a new SkeletonViewer
  296. * @param skeleton defines the skeleton to render
  297. * @param mesh defines the mesh attached to the skeleton
  298. * @param scene defines the hosting scene
  299. * @param autoUpdateBonesMatrices defines a boolean indicating if bones matrices must be forced to update before rendering (true by default)
  300. * @param renderingGroupId defines the rendering group id to use with the viewer
  301. * @param options All of the extra constructor options for the SkeletonViewer
  302. */
  303. constructor(
  304. /** defines the skeleton to render */
  305. public skeleton: Skeleton,
  306. /** defines the mesh attached to the skeleton */
  307. public mesh: AbstractMesh,
  308. /** The Scene scope*/
  309. scene: Scene,
  310. /** defines a boolean indicating if bones matrices must be forced to update before rendering (true by default) */
  311. public autoUpdateBonesMatrices: boolean = true,
  312. /** defines the rendering group id to use with the viewer */
  313. public renderingGroupId: number = 3,
  314. /** is the options for the viewer */
  315. public options: Partial<ISkeletonViewerOptions> = {}
  316. ) {
  317. this._scene = scene;
  318. this._ready = false;
  319. //Defaults
  320. options.pauseAnimations = options.pauseAnimations ?? true;
  321. options.returnToRest = options.returnToRest ?? false;
  322. options.displayMode = options.displayMode ?? SkeletonViewer.DISPLAY_LINES;
  323. options.displayOptions = options.displayOptions ?? {};
  324. options.displayOptions.midStep = options.displayOptions.midStep ?? 0.235;
  325. options.displayOptions.midStepFactor = options.displayOptions.midStepFactor ?? 0.155;
  326. options.displayOptions.sphereBaseSize = options.displayOptions.sphereBaseSize ?? 0.15;
  327. options.displayOptions.sphereScaleUnit = options.displayOptions.sphereScaleUnit ?? 2;
  328. options.displayOptions.sphereFactor = options.displayOptions.sphereFactor ?? 0.865;
  329. options.computeBonesUsingShaders = options.computeBonesUsingShaders ?? true;
  330. options.useAllBones = options.useAllBones ?? true;
  331. const initialMeshBoneIndices = mesh.getVerticesData(VertexBuffer.MatricesIndicesKind);
  332. const initialMeshBoneWeights = mesh.getVerticesData(VertexBuffer.MatricesWeightsKind);
  333. this._boneIndices = new Set();
  334. if (!options.useAllBones) {
  335. if (initialMeshBoneIndices && initialMeshBoneWeights) {
  336. for (let i = 0; i < initialMeshBoneIndices.length; ++i) {
  337. const index = initialMeshBoneIndices[i], weight = initialMeshBoneWeights[i];
  338. if (weight !== 0) {
  339. this._boneIndices.add(index);
  340. }
  341. }
  342. }
  343. }
  344. /* Create Utility Layer */
  345. this._utilityLayer = new UtilityLayerRenderer(this._scene, false);
  346. this._utilityLayer.pickUtilitySceneFirst = false;
  347. this._utilityLayer.utilityLayerScene.autoClearDepthAndStencil = true;
  348. let displayMode = this.options.displayMode || 0;
  349. if (displayMode > SkeletonViewer.DISPLAY_SPHERE_AND_SPURS) {
  350. displayMode = SkeletonViewer.DISPLAY_LINES;
  351. }
  352. this.displayMode = displayMode;
  353. //Prep the Systems
  354. this.update();
  355. this._bindObs();
  356. }
  357. /** The Dynamic bindings for the update functions */
  358. private _bindObs(): void {
  359. switch (this.displayMode){
  360. case SkeletonViewer.DISPLAY_LINES: {
  361. this._obs = this.scene.onBeforeRenderObservable.add(() => {
  362. this._displayLinesUpdate();
  363. });
  364. break;
  365. }
  366. }
  367. }
  368. /** Update the viewer to sync with current skeleton state, only used to manually update. */
  369. public update(): void {
  370. switch (this.displayMode){
  371. case SkeletonViewer.DISPLAY_LINES: {
  372. this._displayLinesUpdate();
  373. break;
  374. }
  375. case SkeletonViewer.DISPLAY_SPHERES: {
  376. this._buildSpheresAndSpurs(true);
  377. break;
  378. }
  379. case SkeletonViewer.DISPLAY_SPHERE_AND_SPURS: {
  380. this._buildSpheresAndSpurs(false);
  381. break;
  382. }
  383. }
  384. }
  385. /** Gets or sets a boolean indicating if the viewer is enabled */
  386. public set isEnabled(value: boolean) {
  387. if (this.isEnabled === value) {
  388. return;
  389. }
  390. this._isEnabled = value;
  391. if (this.debugMesh) {
  392. this.debugMesh.setEnabled(value);
  393. }
  394. if (value && !this._obs) {
  395. this._bindObs();
  396. } else if (!value && this._obs) {
  397. this.scene.onBeforeRenderObservable.remove(this._obs);
  398. this._obs = null;
  399. }
  400. }
  401. public get isEnabled(): boolean {
  402. return this._isEnabled;
  403. }
  404. private _getBonePosition(position: Vector3, bone: Bone, meshMat: Matrix, x = 0, y = 0, z = 0): void {
  405. var tmat = TmpVectors.Matrix[0];
  406. var parentBone = bone.getParent();
  407. tmat.copyFrom(bone.getLocalMatrix());
  408. if (x !== 0 || y !== 0 || z !== 0) {
  409. var tmat2 = TmpVectors.Matrix[1];
  410. Matrix.IdentityToRef(tmat2);
  411. tmat2.setTranslationFromFloats(x, y, z);
  412. tmat2.multiplyToRef(tmat, tmat);
  413. }
  414. if (parentBone) {
  415. tmat.multiplyToRef(parentBone.getAbsoluteTransform(), tmat);
  416. }
  417. tmat.multiplyToRef(meshMat, tmat);
  418. position.x = tmat.m[12];
  419. position.y = tmat.m[13];
  420. position.z = tmat.m[14];
  421. }
  422. private _getLinesForBonesWithLength(bones: Bone[], meshMat: Matrix): void {
  423. var len = bones.length;
  424. let mesh = this.mesh._effectiveMesh;
  425. var meshPos = mesh.position;
  426. let idx = 0;
  427. for (var i = 0; i < len; i++) {
  428. var bone = bones[i];
  429. var points = this._debugLines[idx];
  430. if (bone._index === -1 || (!this._boneIndices.has(bone.getIndex()) && !this.options.useAllBones)) {
  431. continue;
  432. }
  433. if (!points) {
  434. points = [Vector3.Zero(), Vector3.Zero()];
  435. this._debugLines[idx] = points;
  436. }
  437. this._getBonePosition(points[0], bone, meshMat);
  438. this._getBonePosition(points[1], bone, meshMat, 0, bone.length, 0);
  439. points[0].subtractInPlace(meshPos);
  440. points[1].subtractInPlace(meshPos);
  441. idx++;
  442. }
  443. }
  444. private _getLinesForBonesNoLength(bones: Bone[]): void {
  445. var len = bones.length;
  446. var boneNum = 0;
  447. let mesh = this.mesh._effectiveMesh;
  448. var meshPos = mesh.position;
  449. for (var i = len - 1; i >= 0; i--) {
  450. var childBone = bones[i];
  451. var parentBone = childBone.getParent();
  452. if (!parentBone || (!this._boneIndices.has(childBone.getIndex()) && !this.options.useAllBones)) {
  453. continue;
  454. }
  455. var points = this._debugLines[boneNum];
  456. if (!points) {
  457. points = [Vector3.Zero(), Vector3.Zero()];
  458. this._debugLines[boneNum] = points;
  459. }
  460. childBone.getAbsolutePositionToRef(mesh, points[0]);
  461. parentBone.getAbsolutePositionToRef(mesh, points[1]);
  462. points[0].subtractInPlace(meshPos);
  463. points[1].subtractInPlace(meshPos);
  464. boneNum++;
  465. }
  466. }
  467. /** function to revert the mesh and scene back to the initial state. */
  468. private _revert(animationState: boolean): void {
  469. if (this.options.pauseAnimations) {
  470. this.scene.animationsEnabled = animationState;
  471. }
  472. }
  473. /** function to build and bind sphere joint points and spur bone representations. */
  474. private _buildSpheresAndSpurs(spheresOnly = true): void {
  475. if (this._debugMesh) {
  476. this._debugMesh.dispose();
  477. this._debugMesh = null;
  478. this.ready = false;
  479. }
  480. this._ready = false;
  481. let scene = this.scene;
  482. let bones: Bone[] = this.skeleton.bones;
  483. let spheres: Array<[Mesh, Bone]> = [];
  484. let spurs: Mesh[] = [];
  485. const animationState = scene.animationsEnabled;
  486. try {
  487. if (this.options.pauseAnimations) {
  488. scene.animationsEnabled = false;
  489. }
  490. if (this.options.returnToRest) {
  491. this.skeleton.returnToRest();
  492. }
  493. if (this.autoUpdateBonesMatrices) {
  494. this.skeleton.computeAbsoluteTransforms();
  495. }
  496. let longestBoneLength = Number.NEGATIVE_INFINITY;
  497. let getAbsoluteRestPose = function(bone: Nullable<Bone>, matrix: Matrix) {
  498. if (bone === null || bone._index === -1) {
  499. matrix.copyFrom(Matrix.Identity());
  500. return;
  501. }
  502. getAbsoluteRestPose(bone.getParent(), matrix);
  503. bone.getBindPose().multiplyToRef(matrix, matrix);
  504. return;
  505. };
  506. let displayOptions = this.options.displayOptions || {};
  507. for (let i = 0; i < bones.length; i++) {
  508. let bone = bones[i];
  509. if (bone._index === -1 || (!this._boneIndices.has(bone.getIndex()) && !this.options.useAllBones)) {
  510. continue;
  511. }
  512. let boneAbsoluteRestTransform = new Matrix();
  513. getAbsoluteRestPose(bone, boneAbsoluteRestTransform);
  514. let anchorPoint = new Vector3();
  515. boneAbsoluteRestTransform.decompose(undefined, undefined, anchorPoint);
  516. bone.children.forEach((bc, i) => {
  517. let childAbsoluteRestTransform : Matrix = new Matrix();
  518. bc.getRestPose().multiplyToRef(boneAbsoluteRestTransform, childAbsoluteRestTransform);
  519. let childPoint = new Vector3();
  520. childAbsoluteRestTransform.decompose(undefined, undefined, childPoint);
  521. let distanceFromParent = Vector3.Distance(anchorPoint, childPoint);
  522. if (distanceFromParent > longestBoneLength) {
  523. longestBoneLength = distanceFromParent;
  524. }
  525. if (spheresOnly) {
  526. return;
  527. }
  528. let dir = childPoint.clone().subtract(anchorPoint.clone());
  529. let h = dir.length();
  530. let up = dir.normalize().scale(h);
  531. let midStep = displayOptions.midStep || 0.165;
  532. let midStepFactor = displayOptions.midStepFactor || 0.215;
  533. let up0 = up.scale(midStep);
  534. let spur = ShapeBuilder.ExtrudeShapeCustom('skeletonViewer',
  535. {
  536. shape: [
  537. new Vector3(1, -1, 0),
  538. new Vector3(1, 1, 0),
  539. new Vector3(-1, 1, 0),
  540. new Vector3(-1, -1, 0),
  541. new Vector3(1, -1, 0)
  542. ],
  543. path: [ Vector3.Zero(), up0, up ],
  544. scaleFunction:
  545. (i: number) => {
  546. switch (i){
  547. case 0:
  548. case 2:
  549. return 0;
  550. case 1:
  551. return h * midStepFactor;
  552. }
  553. return 0;
  554. },
  555. sideOrientation: Mesh.DEFAULTSIDE,
  556. updatable: false
  557. }, scene);
  558. spur.convertToFlatShadedMesh();
  559. let numVertices = spur.getTotalVertices();
  560. let mwk: number[] = [], mik: number[] = [];
  561. for (let i = 0; i < numVertices; i++) {
  562. mwk.push(1, 0, 0, 0);
  563. mik.push(bone.getIndex(), 0, 0, 0);
  564. }
  565. spur.position = anchorPoint.clone();
  566. spur.setVerticesData(VertexBuffer.MatricesWeightsKind, mwk, false);
  567. spur.setVerticesData(VertexBuffer.MatricesIndicesKind, mik, false);
  568. spurs.push(spur);
  569. });
  570. let sphereBaseSize = displayOptions.sphereBaseSize || 0.2;
  571. let sphere = SphereBuilder.CreateSphere('skeletonViewer', {
  572. segments: 6,
  573. diameter: sphereBaseSize,
  574. updatable: true
  575. }, scene);
  576. const numVertices = sphere.getTotalVertices();
  577. let mwk: number[] = [], mik: number[] = [];
  578. for (let i = 0; i < numVertices; i++) {
  579. mwk.push(1, 0, 0, 0);
  580. mik.push(bone.getIndex(), 0, 0, 0);
  581. }
  582. sphere.setVerticesData(VertexBuffer.MatricesWeightsKind, mwk, false);
  583. sphere.setVerticesData(VertexBuffer.MatricesIndicesKind, mik, false);
  584. sphere.position = anchorPoint.clone();
  585. spheres.push([sphere, bone]);
  586. }
  587. let sphereScaleUnit = displayOptions.sphereScaleUnit || 2;
  588. let sphereFactor = displayOptions.sphereFactor || 0.85;
  589. const meshes = [];
  590. for (let i = 0; i < spheres.length; i++) {
  591. let [sphere, bone] = spheres[i];
  592. let scale = 1 / (sphereScaleUnit / longestBoneLength);
  593. let _stepsOut = 0;
  594. let _b = bone;
  595. while ((_b.getParent()) && (_b.getParent() as Bone).getIndex() !== -1) {
  596. _stepsOut++;
  597. _b = (_b.getParent() as Bone);
  598. }
  599. sphere.scaling.scaleInPlace(scale * Math.pow(sphereFactor, _stepsOut));
  600. meshes.push(sphere);
  601. }
  602. this.debugMesh = Mesh.MergeMeshes(meshes.concat(spurs), true, true);
  603. if (this.debugMesh) {
  604. this.debugMesh.renderingGroupId = this.renderingGroupId;
  605. this.debugMesh.skeleton = this.skeleton;
  606. this.debugMesh.parent = this.mesh;
  607. this.debugMesh.computeBonesUsingShaders = this.options.computeBonesUsingShaders ?? true;
  608. this.debugMesh.alwaysSelectAsActiveMesh = true;
  609. }
  610. this._revert(animationState);
  611. this.ready = true;
  612. } catch (err) {
  613. console.error(err);
  614. this._revert(animationState);
  615. this.dispose();
  616. }
  617. }
  618. /** Update the viewer to sync with current skeleton state, only used for the line display. */
  619. private _displayLinesUpdate(): void {
  620. if (!this._utilityLayer) {
  621. return;
  622. }
  623. if (this.autoUpdateBonesMatrices) {
  624. this.skeleton.computeAbsoluteTransforms();
  625. }
  626. let mesh = this.mesh._effectiveMesh;
  627. if (this.skeleton.bones[0].length === undefined) {
  628. this._getLinesForBonesNoLength(this.skeleton.bones);
  629. } else {
  630. this._getLinesForBonesWithLength(this.skeleton.bones, mesh.getWorldMatrix());
  631. }
  632. const targetScene = this._utilityLayer.utilityLayerScene;
  633. if (targetScene) {
  634. if (!this._debugMesh) {
  635. this._debugMesh = LinesBuilder.CreateLineSystem("", { lines: this._debugLines, updatable: true, instance: null }, targetScene);
  636. this._debugMesh.renderingGroupId = this.renderingGroupId;
  637. } else {
  638. LinesBuilder.CreateLineSystem("", { lines: this._debugLines, updatable: true, instance: this._debugMesh }, targetScene);
  639. }
  640. this._debugMesh.position.copyFrom(this.mesh.position);
  641. this._debugMesh.color = this.color;
  642. }
  643. }
  644. /** Changes the displayMode of the skeleton viewer
  645. * @param mode The displayMode numerical value
  646. */
  647. public changeDisplayMode(mode: number): void {
  648. let wasEnabled = (this.isEnabled) ? true : false;
  649. if (this.displayMode !== mode) {
  650. this.isEnabled = false;
  651. if (this._debugMesh) {
  652. this._debugMesh.dispose();
  653. this._debugMesh = null;
  654. this.ready = false;
  655. }
  656. this.displayMode = mode;
  657. this.update();
  658. this._bindObs();
  659. this.isEnabled = wasEnabled;
  660. }
  661. }
  662. /** Changes the displayMode of the skeleton viewer
  663. * @param option String of the option name
  664. * @param value The numerical option value
  665. */
  666. public changeDisplayOptions(option: string, value: number): void {
  667. let wasEnabled = (this.isEnabled) ? true : false;
  668. (this.options.displayOptions as any)[option] = value;
  669. this.isEnabled = false;
  670. if (this._debugMesh) {
  671. this._debugMesh.dispose();
  672. this._debugMesh = null;
  673. this.ready = false;
  674. }
  675. this.update();
  676. this._bindObs();
  677. this.isEnabled = wasEnabled;
  678. }
  679. /** Release associated resources */
  680. public dispose(): void {
  681. this.isEnabled = false;
  682. if (this._debugMesh) {
  683. this._debugMesh.dispose();
  684. this._debugMesh = null;
  685. }
  686. if (this._utilityLayer) {
  687. this._utilityLayer.dispose();
  688. this._utilityLayer = null;
  689. }
  690. this.ready = false;
  691. }
  692. }