UVsDebug.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ( function () {
  2. /**
  3. * tool for "unwrapping" and debugging three.js geometries UV mapping
  4. *
  5. * Sample usage:
  6. * document.body.appendChild( UVsDebug( new THREE.SphereGeometry( 10, 10, 10, 10 ) );
  7. *
  8. */
  9. function UVsDebug( geometry, size = 1024 ) {
  10. // handles wrapping of uv.x > 1 only
  11. const abc = 'abc';
  12. const a = new THREE.Vector2();
  13. const b = new THREE.Vector2();
  14. const uvs = [ new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() ];
  15. const face = [];
  16. const canvas = document.createElement( 'canvas' );
  17. const width = size; // power of 2 required for wrapping
  18. const height = size;
  19. canvas.width = width;
  20. canvas.height = height;
  21. const ctx = canvas.getContext( '2d' );
  22. ctx.lineWidth = 1;
  23. ctx.strokeStyle = 'rgb( 63, 63, 63 )';
  24. ctx.textAlign = 'center';
  25. // paint background white
  26. ctx.fillStyle = 'rgb( 255, 255, 255 )';
  27. ctx.fillRect( 0, 0, width, height );
  28. const index = geometry.index;
  29. const uvAttribute = geometry.attributes.uv;
  30. if ( index ) {
  31. // indexed geometry
  32. for ( let i = 0, il = index.count; i < il; i += 3 ) {
  33. face[ 0 ] = index.getX( i );
  34. face[ 1 ] = index.getX( i + 1 );
  35. face[ 2 ] = index.getX( i + 2 );
  36. uvs[ 0 ].fromBufferAttribute( uvAttribute, face[ 0 ] );
  37. uvs[ 1 ].fromBufferAttribute( uvAttribute, face[ 1 ] );
  38. uvs[ 2 ].fromBufferAttribute( uvAttribute, face[ 2 ] );
  39. processFace( face, uvs, i / 3 );
  40. }
  41. } else {
  42. // non-indexed geometry
  43. for ( let i = 0, il = uvAttribute.count; i < il; i += 3 ) {
  44. face[ 0 ] = i;
  45. face[ 1 ] = i + 1;
  46. face[ 2 ] = i + 2;
  47. uvs[ 0 ].fromBufferAttribute( uvAttribute, face[ 0 ] );
  48. uvs[ 1 ].fromBufferAttribute( uvAttribute, face[ 1 ] );
  49. uvs[ 2 ].fromBufferAttribute( uvAttribute, face[ 2 ] );
  50. processFace( face, uvs, i / 3 );
  51. }
  52. }
  53. return canvas;
  54. function processFace( face, uvs, index ) {
  55. // draw contour of face
  56. ctx.beginPath();
  57. a.set( 0, 0 );
  58. for ( let j = 0, jl = uvs.length; j < jl; j ++ ) {
  59. const uv = uvs[ j ];
  60. a.x += uv.x;
  61. a.y += uv.y;
  62. if ( j === 0 ) {
  63. ctx.moveTo( uv.x * ( width - 2 ) + 0.5, ( 1 - uv.y ) * ( height - 2 ) + 0.5 );
  64. } else {
  65. ctx.lineTo( uv.x * ( width - 2 ) + 0.5, ( 1 - uv.y ) * ( height - 2 ) + 0.5 );
  66. }
  67. }
  68. ctx.closePath();
  69. ctx.stroke();
  70. // calculate center of face
  71. a.divideScalar( uvs.length );
  72. // label the face number
  73. ctx.font = '18px Arial';
  74. ctx.fillStyle = 'rgb( 63, 63, 63 )';
  75. ctx.fillText( index, a.x * width, ( 1 - a.y ) * height );
  76. if ( a.x > 0.95 ) {
  77. // wrap x // 0.95 is arbitrary
  78. ctx.fillText( index, a.x % 1 * width, ( 1 - a.y ) * height );
  79. }
  80. //
  81. ctx.font = '12px Arial';
  82. ctx.fillStyle = 'rgb( 191, 191, 191 )';
  83. // label uv edge orders
  84. for ( let j = 0, jl = uvs.length; j < jl; j ++ ) {
  85. const uv = uvs[ j ];
  86. b.addVectors( a, uv ).divideScalar( 2 );
  87. const vnum = face[ j ];
  88. ctx.fillText( abc[ j ] + vnum, b.x * width, ( 1 - b.y ) * height );
  89. if ( b.x > 0.95 ) {
  90. // wrap x
  91. ctx.fillText( abc[ j ] + vnum, b.x % 1 * width, ( 1 - b.y ) * height );
  92. }
  93. }
  94. }
  95. }
  96. THREE.UVsDebug = UVsDebug;
  97. } )();