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