transformconstraint.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. var transformConstraintDemo = function(loadingComplete, bgColor) {
  2. var COLOR_INNER = new spine.Color(0.8, 0, 0, 0.5);
  3. var COLOR_OUTER = new spine.Color(0.8, 0, 0, 0.8);
  4. var COLOR_INNER_SELECTED = new spine.Color(0.0, 0, 0.8, 0.5);
  5. var COLOR_OUTER_SELECTED = new spine.Color(0.0, 0, 0.8, 0.8);
  6. var canvas, gl, renderer, input, assetManager;
  7. var skeleton, state, bounds;
  8. var timeKeeper, loadingScreen;
  9. var rotateHandle;
  10. var target = null;
  11. var hoverTargets = [null, null, null];
  12. var controlBones = ["wheel2overlay", "wheel3overlay", "rotate-handle"];
  13. var coords = new spine.webgl.Vector3(), temp = new spine.webgl.Vector3(), temp2 = new spine.Vector2();
  14. var lastRotation = 0;
  15. var mix, lastOffset = 0, lastMix = 0.5;
  16. var DEMO_NAME = "TransformConstraintDemo";
  17. if (!bgColor) bgColor = new spine.Color(0, 0, 0, 1);
  18. function init () {
  19. canvas = document.getElementById("transformdemo-canvas");
  20. canvas.width = canvas.clientWidth; canvas.height = canvas.clientHeight;
  21. gl = canvas.getContext("webgl", { alpha: false }) || canvas.getContext("experimental-webgl", { alpha: false });
  22. renderer = new spine.webgl.SceneRenderer(canvas, gl);
  23. assetManager = spineDemos.assetManager;
  24. var textureLoader = function(img) { return new spine.webgl.GLTexture(gl, img); };
  25. assetManager.loadTexture(DEMO_NAME, textureLoader, "atlas2.png");
  26. assetManager.loadText(DEMO_NAME, "atlas2.atlas");
  27. assetManager.loadJson(DEMO_NAME, "demos.json");
  28. input = new spine.webgl.Input(canvas);
  29. timeKeeper = new spine.TimeKeeper();
  30. loadingScreen = new spine.webgl.LoadingScreen(renderer);
  31. loadingScreen.backgroundColor = bgColor;
  32. requestAnimationFrame(load);
  33. }
  34. function load () {
  35. timeKeeper.update();
  36. if (assetManager.isLoadingComplete(DEMO_NAME)) {
  37. var atlas = new spine.TextureAtlas(assetManager.get(DEMO_NAME, "atlas2.atlas"), function(path) {
  38. return assetManager.get(DEMO_NAME, path);
  39. });
  40. var atlasLoader = new spine.TextureAtlasAttachmentLoader(atlas);
  41. var skeletonJson = new spine.SkeletonJson(atlasLoader);
  42. var skeletonData = skeletonJson.readSkeletonData(assetManager.get(DEMO_NAME, "demos.json").transformConstraint);
  43. skeleton = new spine.Skeleton(skeletonData);
  44. skeleton.setToSetupPose();
  45. skeleton.updateWorldTransform();
  46. var offset = new spine.Vector2();
  47. bounds = new spine.Vector2();
  48. skeleton.getBounds(offset, bounds);
  49. state = new spine.AnimationState(new spine.AnimationStateData(skeleton.data));
  50. skeleton.setToSetupPose();
  51. skeleton.updateWorldTransform();
  52. rotateHandle = skeleton.findBone("rotate-handle");
  53. renderer.camera.position.x = offset.x + bounds.x / 2;
  54. renderer.camera.position.y = offset.y + bounds.y / 2;
  55. renderer.skeletonDebugRenderer.drawRegionAttachments = false;
  56. renderer.skeletonDebugRenderer.drawMeshHull = false;
  57. renderer.skeletonDebugRenderer.drawMeshTriangles = false;
  58. setupUI();
  59. setupInput();
  60. loadingComplete(canvas, render);
  61. } else {
  62. loadingScreen.draw();
  63. requestAnimationFrame(load);
  64. }
  65. }
  66. function setupUI() {
  67. var rotationOffset = $("#transformdemo-rotationoffset").data("slider");
  68. rotationOffset.changed = function (percent) {
  69. var val = percent * 360 - 180;
  70. var delta = val - lastOffset;
  71. lastOffset = val;
  72. skeleton.findTransformConstraint("wheel2").data.offsetRotation += delta;
  73. skeleton.findTransformConstraint("wheel3").data.offsetRotation += delta;
  74. $("#transformdemo-rotationoffset-label").text(Math.round(val) + "°");
  75. };
  76. $("#transformdemo-rotationoffset-label").text("0°");
  77. var translationMix = $("#transformdemo-translationmix").data("slider");
  78. translationMix.set(0.5);
  79. translationMix.changed = function (percent) {
  80. var val = percent;
  81. var delta = val - lastMix;
  82. lastMix = val;
  83. skeleton.findTransformConstraint("wheel1").translateMix += delta;
  84. $("#transformdemo-translationmix-label").text(Math.round(val * 100) + "%");
  85. };
  86. $("#transformdemo-translationmix-label").text("50%");
  87. }
  88. function setupInput() {
  89. var getRotation = function(x, y) {
  90. renderer.camera.screenToWorld(coords.set(x, y, 0), canvas.width, canvas.height);
  91. var wheel1 = skeleton.findBone("wheel1overlay");
  92. var v = coords.sub(new spine.webgl.Vector3(wheel1.worldX, wheel1.worldY, 0)).normalize();
  93. var angle = Math.acos(v.x) * spine.MathUtils.radiansToDegrees;
  94. if (v.y < 0) angle = 360 - angle;
  95. return angle;
  96. }
  97. input.addListener({
  98. down: function(x, y) {
  99. renderer.camera.screenToWorld(coords.set(x, y, 0), canvas.width, canvas.height);
  100. for (var i = 0; i < controlBones.length; i++) {
  101. var bone = skeleton.findBone(controlBones[i]);
  102. if (temp.set(skeleton.x + bone.worldX, skeleton.y + bone.worldY, 0).distance(coords) < 20) {
  103. target = bone;
  104. if (target === rotateHandle) lastRotation = getRotation(x, y);
  105. }
  106. }
  107. },
  108. up: function(x, y) {
  109. target = null;
  110. },
  111. dragged: function(x, y) {
  112. if (target != null) {
  113. if (target === rotateHandle) {
  114. var rotation = getRotation(x, y);
  115. var delta = rotation - lastRotation;
  116. skeleton.findBone("wheel1").rotation += delta;
  117. lastRotation = rotation;
  118. } else {
  119. renderer.camera.screenToWorld(coords.set(x, y, 0), canvas.width, canvas.height);
  120. if (target.parent !== null) {
  121. target.parent.worldToLocal(temp2.set(coords.x - skeleton.x, coords.y - skeleton.y));
  122. target.x = temp2.x;
  123. target.y = temp2.y;
  124. } else {
  125. target.x = coords.x - skeleton.x;
  126. target.y = coords.y - skeleton.y;
  127. }
  128. }
  129. }
  130. },
  131. moved: function (x, y) {
  132. renderer.camera.screenToWorld(coords.set(x, y, 0), canvas.width, canvas.height);
  133. for (var i = 0; i < controlBones.length; i++) {
  134. var bone = skeleton.findBone(controlBones[i]);
  135. if (temp.set(skeleton.x + bone.worldX, skeleton.y + bone.worldY, 0).distance(coords) < 20) {
  136. hoverTargets[i] = bone;
  137. } else {
  138. hoverTargets[i] = null;
  139. }
  140. }
  141. }
  142. })
  143. }
  144. function render () {
  145. timeKeeper.update();
  146. var delta = timeKeeper.delta;
  147. skeleton.updateWorldTransform();
  148. renderer.camera.viewportWidth = bounds.x * 1.2;
  149. renderer.camera.viewportHeight = bounds.y * 1.2;
  150. renderer.resize(spine.webgl.ResizeMode.Fit);
  151. gl.clearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
  152. gl.clear(gl.COLOR_BUFFER_BIT);
  153. renderer.begin();
  154. renderer.drawSkeleton(skeleton, true);
  155. renderer.drawSkeletonDebug(skeleton, false, ["root", "rotate-handle"]);
  156. gl.lineWidth(2);
  157. for (var i = 0; i < controlBones.length; i++) {
  158. var bone = skeleton.findBone(controlBones[i]);
  159. var colorInner = hoverTargets[i] !== null ? spineDemos.HOVER_COLOR_INNER : spineDemos.NON_HOVER_COLOR_INNER;
  160. var colorOuter = hoverTargets[i] !== null ? spineDemos.HOVER_COLOR_OUTER : spineDemos.NON_HOVER_COLOR_OUTER;
  161. renderer.circle(true, skeleton.x + bone.worldX, skeleton.y + bone.worldY, 20, colorInner);
  162. renderer.circle(false, skeleton.x + bone.worldX, skeleton.y + bone.worldY, 20, colorOuter);
  163. }
  164. gl.lineWidth(1);
  165. renderer.end();
  166. }
  167. init();
  168. };