UVsUtils.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * @author https://github.com/zz85 | @blurspline
  3. *
  4. * tool for "unwrapping" and debugging three.js
  5. * geometries UV mapping
  6. *
  7. * Sample usage:
  8. * document.body.appendChild(
  9. * THREE.UVsDebug(
  10. * new THREE.SphereGeometry(10,10,10,10));
  11. *
  12. */
  13. THREE.UVsDebug = function(geometry) {
  14. var verts = geometry.vertices,
  15. faces = geometry.faces,
  16. uvs = geometry.faceVertexUvs[0];
  17. console.log('debugging geometry', geometry);
  18. var canvas = document.createElement('canvas');
  19. var width = 1024;
  20. var height = 1024;
  21. canvas.width = width;
  22. canvas.height = height;
  23. var ctx = canvas.getContext('2d');
  24. ctx.lineWidth = 1;
  25. ctx.strokeStyle = 'rgba(0,0,0,0.8)';
  26. // paint background white
  27. ctx.fillStyle = 'rgba(255,255,255, 1.0)';
  28. ctx.fillRect(0, 0, width, height);
  29. var abc = 'abcd';
  30. var uv, u, ax, ay;
  31. var i, il, j, jl;
  32. var a = new THREE.Vector2();
  33. var b = new THREE.Vector2();
  34. for (i = 0, il = uvs.length; i < il; i++) {
  35. uv = uvs[i];
  36. // draw lines
  37. ctx.beginPath();
  38. a.set(0, 0);
  39. for (j = 0, jl = uv.length; j < jl; j++) {
  40. u = uv[j];
  41. a.x += u.u;
  42. a.y += u.v;
  43. if (j == 0) {
  44. ctx.moveTo(u.u * width, u.v * height);
  45. } else {
  46. ctx.lineTo(u.u * width, u.v * height);
  47. }
  48. }
  49. ctx.closePath();
  50. ctx.stroke();
  51. a.divideScalar(jl);
  52. // label the face number
  53. ctx.font = "12pt Arial bold";
  54. ctx.fillStyle = 'rgba(0,0,0,0.8)';
  55. ctx.fillText(i, a.x * width, a.y * height);
  56. ctx.font = "8pt Arial bold";
  57. ctx.fillStyle = 'rgba(30,30,0,0.8)';
  58. // label uv edge orders
  59. for (j = 0, jl = uv.length; j < jl; j++) {
  60. u = uv[j];
  61. b.set(u.u, u.v).subSelf(a).divideScalar(4);
  62. b.x = u.u - b.x;
  63. b.y = u.v - b.y;
  64. ctx.fillText(abc[j]
  65. + ':' + faces[i][abc[j]], b.x * width, b.y * height);
  66. }
  67. }
  68. return canvas;
  69. }