coordinate-transform.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>spine-threejs</title>
  5. <script src="https://unpkg.com/[email protected]/build/three.js"></script>
  6. <script src="../dist/iife/spine-threejs.js"></script>
  7. <script src="./OrbitalControls.js"></script>
  8. </head>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. }
  14. body,
  15. html {
  16. height: 100%
  17. }
  18. canvas {
  19. position: absolute;
  20. width: 100%;
  21. height: 100%;
  22. }
  23. </style>
  24. <body>
  25. <script>
  26. (function () {
  27. let scene, camera, renderer;
  28. let skeletonMesh, cube, tailBone;
  29. let assetManager;
  30. let canvas;
  31. let controls;
  32. let lastFrameTime = Date.now() / 1000;
  33. let baseUrl = "assets/";
  34. let skeletonFile = "raptor-pro.json";
  35. let atlasFile = skeletonFile.replace("-pro", "").replace("-ess", "").replace(".json", ".atlas");
  36. let animation = "walk";
  37. function init() {
  38. // create the THREE.JS camera, scene and renderer (WebGL)
  39. let width = window.innerWidth, height = window.innerHeight;
  40. camera = new THREE.PerspectiveCamera(75, width / height, 1, 3000);
  41. camera.position.y = 200;
  42. camera.position.z = 800;
  43. scene = new THREE.Scene();
  44. renderer = new THREE.WebGLRenderer();
  45. renderer.setSize(width, height);
  46. document.body.appendChild(renderer.domElement);
  47. canvas = renderer.domElement;
  48. controls = new OrbitControls(camera, renderer.domElement);
  49. // load the assets required to display the Raptor model
  50. assetManager = new spine.AssetManager(baseUrl);
  51. assetManager.loadText(skeletonFile);
  52. assetManager.loadTextureAtlas(atlasFile);
  53. requestAnimationFrame(load);
  54. }
  55. function load(name, scale) {
  56. if (assetManager.isLoadingComplete()) {
  57. // Load the texture atlas using name.atlas and name.png from the AssetManager.
  58. // The function passed to TextureAtlas is used to resolve relative paths.
  59. atlas = assetManager.require(atlasFile);
  60. // Create a AtlasAttachmentLoader that resolves region, mesh, boundingbox and path attachments
  61. atlasLoader = new spine.AtlasAttachmentLoader(atlas);
  62. // Create a SkeletonJson instance for parsing the .json file.
  63. let skeletonJson = new spine.SkeletonJson(atlasLoader);
  64. // Set the scale to apply during parsing, parse the file, and create a new skeleton.
  65. skeletonJson.scale = 0.4;
  66. let skeletonData = skeletonJson.readSkeletonData(assetManager.require(skeletonFile));
  67. // Create a SkeletonMesh from the data and attach it to the scene
  68. skeletonMesh = new spine.SkeletonMesh(skeletonData, (parameters) => {
  69. parameters.depthTest = true;
  70. parameters.depthWrite = true;
  71. parameters.alphaTest = 0.001;
  72. });
  73. skeletonMesh.state.setAnimation(0, animation, true);
  74. skeletonMesh.position.x = 120;
  75. skeletonMesh.position.y = -20;
  76. skeletonMesh.position.z = 10;
  77. scene.add(skeletonMesh);
  78. // Add a wireframe cube which will be positioned at the tail bone
  79. geometry = new THREE.BoxGeometry(20, 20, 20);
  80. material = new THREE.MeshBasicMaterial({ color: 0xff00, wireframe: true });
  81. cube = new THREE.Mesh(geometry, material);
  82. scene.add(cube);
  83. // Get the tail bone
  84. tailBone = skeletonMesh.skeleton.findBone("tail10");
  85. requestAnimationFrame(render);
  86. } else requestAnimationFrame(load);
  87. }
  88. let lastTime = Date.now();
  89. function render() {
  90. // calculate delta time for animation purposes
  91. let now = Date.now() / 1000;
  92. let delta = now - lastFrameTime;
  93. lastFrameTime = now;
  94. // resize canvas to use full page, adjust camera/renderer
  95. resize();
  96. // Update orbital controls
  97. controls.update();
  98. // update the animation
  99. skeletonMesh.update(delta);
  100. // Get the tail bone coordinates in the skeleton's local coordinate space.
  101. let position = new THREE.Vector3(tailBone.worldX, tailBone.worldY, 0);
  102. // Convert the tail bone coordinates to world coordinates.
  103. skeletonMesh.localToWorld(position)
  104. // Set the tail bone coordinates as the cube's position.
  105. cube.position.set(position.x, position.y, position.z);
  106. // render the scene
  107. renderer.render(scene, camera);
  108. requestAnimationFrame(render);
  109. }
  110. function resize() {
  111. let w = window.innerWidth;
  112. let h = window.innerHeight;
  113. if (canvas.width != w || canvas.height != h) {
  114. canvas.width = w;
  115. canvas.height = h;
  116. }
  117. camera.aspect = w / h;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize(w, h);
  120. }
  121. init();
  122. }());
  123. </script>
  124. </body>
  125. </html>