UVsDebug.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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'; // paint background white
  25. ctx.fillStyle = 'rgb( 255, 255, 255 )';
  26. ctx.fillRect( 0, 0, width, height );
  27. const index = geometry.index;
  28. const uvAttribute = geometry.attributes.uv;
  29. if ( index ) {
  30. // indexed geometry
  31. for ( let i = 0, il = index.count; i < il; i += 3 ) {
  32. face[ 0 ] = index.getX( i );
  33. face[ 1 ] = index.getX( i + 1 );
  34. face[ 2 ] = index.getX( i + 2 );
  35. uvs[ 0 ].fromBufferAttribute( uvAttribute, face[ 0 ] );
  36. uvs[ 1 ].fromBufferAttribute( uvAttribute, face[ 1 ] );
  37. uvs[ 2 ].fromBufferAttribute( uvAttribute, face[ 2 ] );
  38. processFace( face, uvs, i / 3 );
  39. }
  40. } else {
  41. // non-indexed geometry
  42. for ( let i = 0, il = uvAttribute.count; i < il; i += 3 ) {
  43. face[ 0 ] = i;
  44. face[ 1 ] = i + 1;
  45. face[ 2 ] = i + 2;
  46. uvs[ 0 ].fromBufferAttribute( uvAttribute, face[ 0 ] );
  47. uvs[ 1 ].fromBufferAttribute( uvAttribute, face[ 1 ] );
  48. uvs[ 2 ].fromBufferAttribute( uvAttribute, face[ 2 ] );
  49. processFace( face, uvs, i / 3 );
  50. }
  51. }
  52. return canvas;
  53. function processFace( face, uvs, index ) {
  54. // draw contour of face
  55. ctx.beginPath();
  56. a.set( 0, 0 );
  57. for ( let j = 0, jl = uvs.length; j < jl; j ++ ) {
  58. const uv = uvs[ j ];
  59. a.x += uv.x;
  60. a.y += uv.y;
  61. if ( j === 0 ) {
  62. ctx.moveTo( uv.x * ( width - 2 ) + 0.5, ( 1 - uv.y ) * ( height - 2 ) + 0.5 );
  63. } else {
  64. ctx.lineTo( uv.x * ( width - 2 ) + 0.5, ( 1 - uv.y ) * ( height - 2 ) + 0.5 );
  65. }
  66. }
  67. ctx.closePath();
  68. ctx.stroke(); // calculate center of face
  69. a.divideScalar( uvs.length ); // label the face number
  70. ctx.font = '18px Arial';
  71. ctx.fillStyle = 'rgb( 63, 63, 63 )';
  72. ctx.fillText( index, a.x * width, ( 1 - a.y ) * height );
  73. if ( a.x > 0.95 ) {
  74. // wrap x // 0.95 is arbitrary
  75. ctx.fillText( index, a.x % 1 * width, ( 1 - a.y ) * height );
  76. } //
  77. ctx.font = '12px Arial';
  78. ctx.fillStyle = 'rgb( 191, 191, 191 )'; // label uv edge orders
  79. for ( let j = 0, jl = uvs.length; j < jl; j ++ ) {
  80. const uv = uvs[ j ];
  81. b.addVectors( a, uv ).divideScalar( 2 );
  82. const vnum = face[ j ];
  83. ctx.fillText( abc[ j ] + vnum, b.x * width, ( 1 - b.y ) * height );
  84. if ( b.x > 0.95 ) {
  85. // wrap x
  86. ctx.fillText( abc[ j ] + vnum, b.x % 1 * width, ( 1 - b.y ) * height );
  87. }
  88. }
  89. }
  90. }
  91. THREE.UVsDebug = UVsDebug;
  92. } )();