renders.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Skin locations are based on the work of Confuser, with 1.8 updates by Jake0oo0
  2. // https://github.com/confuser/serverless-mc-skin-viewer
  3. // Permission to use & distribute https://github.com/confuser/serverless-mc-skin-viewer/blob/master/LICENSE
  4. var logging = require("./logging");
  5. var fs = require("fs");
  6. var Canvas = require("canvas");
  7. var Image = Canvas.Image;
  8. var exp = {};
  9. // draws the helmet on to the +skin+
  10. // using the skin from the +ctx+ at the +scale+
  11. exp.draw_helmet = function(skin, ctx, scale) {
  12. // Helmet - Front
  13. ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
  14. ctx.drawImage(skin, 40, 8, 8, 8, 10 * scale, 13 / 1.2 * scale, 8 * scale, 8 * scale);
  15. // Helmet - Right
  16. ctx.setTransform(1, 0.5, 0, 1.2, 0, 0);
  17. ctx.drawImage(skin, 32, 8, 8, 8, 2 * scale, 3 / 1.2 * scale, 8 * scale, 8 * scale);
  18. // Helmet - Top
  19. ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
  20. ctx.drawImage(skin, 48, 0, -8, 8, -5 * scale, 5 * scale, 8 * scale, 8 * scale);
  21. };
  22. // draws the head on to the +skin+
  23. // using the skin from the +ctx+ at the +scale+
  24. exp.draw_head = function(skin, ctx, scale) {
  25. // Head - Front
  26. ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
  27. ctx.drawImage(skin, 8, 8, 8, 8, 10 * scale, 13 / 1.2 * scale, 8 * scale, 8 * scale);
  28. // Head - Right
  29. ctx.setTransform(1, 0.5, 0, 1.2, 0, 0);
  30. ctx.drawImage(skin, 0, 8, 8, 8, 2 * scale, 3 / 1.2 * scale, 8 * scale, 8 * scale);
  31. // Head - Top
  32. ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
  33. ctx.drawImage(skin, 16, 0, -8, 8, -5 * scale, 5 * scale, 8 * scale, 8 * scale);
  34. };
  35. // draws the body on to the +skin+
  36. // using the skin from the +ctx+ at the +scale+
  37. // parts are labeled as if drawn from the skin's POV
  38. exp.draw_body = function(rid, skin, ctx, scale) {
  39. // Right Leg
  40. // Right Leg - Right
  41. ctx.setTransform(1, 0.5, 0, 1.2, 0, 0);
  42. ctx.drawImage(skin, 0, 20, 4, 12, 4 * scale, 26.4 / 1.2 * scale, 4 * scale, 12 * scale);
  43. // Right Leg - Front
  44. ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
  45. ctx.drawImage(skin, 4, 20, 4, 12, 8 * scale, 34.4 / 1.2 * scale, 4 * scale, 12 * scale);
  46. // Body
  47. // Body - Front
  48. ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
  49. ctx.drawImage(skin, 20, 20, 8, 12, 8 * scale, 20 / 1.2 * scale, 8 * scale, 12 * scale);
  50. // Arm Right
  51. // Arm Right - Right
  52. ctx.setTransform(1, 0.5, 0, 1.2, 0, 0);
  53. ctx.drawImage(skin, 40, 20, 4, 12, 0, 16 / 1.2 * scale, 4 * scale, 12 * scale);
  54. // Arm Right - Front
  55. ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
  56. ctx.drawImage(skin, 44, 20, 4, 12, 4 * scale, 20 / 1.2 * scale, 4 * scale, 12 * scale);
  57. // Arm Right - Top
  58. ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
  59. ctx.drawImage(skin, 48, 16, -4, 4, 12 * scale, 16 * scale, 4 * scale, 4 * scale);
  60. if (skin.height === 32) {
  61. logging.debug(rid, "uses old skin format");
  62. // Left Leg
  63. // Left Leg - Front
  64. ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
  65. ctx.drawImage(skin, 8, 20, -4, 12, 12 * scale, 34.4 / 1.2 * scale, 4 * scale, 12 * scale);
  66. // Arm Left
  67. // Arm Left - Front
  68. ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
  69. ctx.drawImage(skin, 48, 20, -4, 12, 16 * scale, 20 / 1.2 * scale, 4 * scale, 12 * scale);
  70. // Arm Left - Top
  71. ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
  72. ctx.drawImage(skin, 44, 16, 4, 4, 0, 16 * scale, 4 * scale, 4 * scale);
  73. } else {
  74. logging.debug(rid, "uses new skin format");
  75. // Left Leg
  76. // Left Leg - Front
  77. ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
  78. ctx.drawImage(skin, 20, 52, 4, 12, 12 * scale, 34.4 / 1.2 * scale, 4 * scale, 12 * scale);
  79. // Arm Left
  80. // Arm Left - Front
  81. ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
  82. ctx.drawImage(skin, 36, 52, 4, 12, 16 * scale, 20 / 1.2 * scale, 4 * scale, 12 * scale);
  83. // Arm Left - Top
  84. ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
  85. ctx.drawImage(skin, 36, 48, 4, 4, 0, 16 * scale, 4 * scale, 4 * scale);
  86. }
  87. };
  88. // sets up the necessary components to draw the skin model
  89. // uses the +img+ skin with options of drawing
  90. // the +helm+ and the +body+
  91. // callback: error, image buffer
  92. exp.draw_model = function(rid, img, scale, helm, body, callback) {
  93. var skin = new Image();
  94. skin.onerror = function(err) {
  95. logging.error(rid, "render error:", err.stack);
  96. callback(err, null);
  97. };
  98. skin.onload = function() {
  99. var canvas = new Canvas(20 * scale, (body ? 44.8 : 17.6) * scale);
  100. var ctx = canvas.getContext("2d");
  101. ctx.patternQuality = "fast";
  102. if (body) {
  103. exp.draw_body(rid, skin, ctx, scale);
  104. }
  105. exp.draw_head(skin, ctx, scale);
  106. if (helm) {
  107. exp.draw_helmet(skin, ctx, scale);
  108. }
  109. canvas.toBuffer(function(err, buf) {
  110. if (err) {
  111. logging.error(rid, "error creating buffer:", err);
  112. }
  113. callback(err, buf);
  114. });
  115. };
  116. skin.src = img;
  117. };
  118. // helper method to open a render from +renderpath+
  119. // callback: error, image buffer
  120. exp.open_render = function(rid, renderpath, callback) {
  121. fs.readFile(renderpath, function(err, buf) {
  122. if (err) {
  123. logging.error(rid, "error while opening skin file:", err);
  124. }
  125. callback(err, buf);
  126. });
  127. };
  128. module.exports = exp;