UVsDebug.js 2.5 KB

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