UVsUtils.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * @author gyuque / http://github.com/gyuque
  3. * Cylinder Mapping for ExtrudeGeometry
  4. */
  5. THREE.UVsUtils = {
  6. };
  7. THREE.UVsUtils.CylinderUVGenerator = function() {
  8. this.uRepeat = 1;
  9. this.targetGeometry = null;
  10. this.lengthCache = null;
  11. };
  12. THREE.UVsUtils.CylinderUVGenerator.prototype = {
  13. generateTopUV: THREE.ExtrudeGeometry.WorldUVGenerator.generateTopUV,
  14. generateBottomUV: THREE.ExtrudeGeometry.WorldUVGenerator.generateBottomUV,
  15. generateSideWallUV: function( geometry, extrudedShape, wallContour, extrudeOptions,
  16. indexA, indexB, indexC, indexD, stepIndex, stepsLength,
  17. contourIndex1, contourIndex2 ) {
  18. // first call
  19. if (this.targetGeometry !== geometry) {
  20. this.prepare(geometry, wallContour);
  21. }
  22. // generate uv
  23. var u_list = this.lengthCache;
  24. var v1 = stepIndex / stepsLength;
  25. var v2 = ( stepIndex + 1 ) / stepsLength;
  26. var u1 = u_list[contourIndex1];
  27. var u2 = u_list[contourIndex2];
  28. if (u1 < u2) {u1 += 1.0;}
  29. u1 *= this.uRepeat;
  30. u2 *= this.uRepeat;
  31. return [
  32. new THREE.Vector2( u1, v1 ),
  33. new THREE.Vector2( u2, v1 ),
  34. new THREE.Vector2( u2, v2 ),
  35. new THREE.Vector2( u1, v2 )
  36. ];
  37. },
  38. prepare: function(geometry, wallContour) {
  39. var p1, p2;
  40. var u_list = [];
  41. var lengthSum = 0;
  42. var len = wallContour.length;
  43. for (var i = 0;i < len;i++) {
  44. p1 = wallContour[ i ];
  45. p2 = wallContour[ (i+1) % len ];
  46. var dx = p1.x - p2.x;
  47. var dy = p1.y - p2.y;
  48. var segmentLength = Math.sqrt(dx*dx + dy*dy);
  49. u_list.push(lengthSum);
  50. lengthSum += segmentLength;
  51. }
  52. this.normalizeArray(u_list, lengthSum);
  53. this.targetGeometry = geometry;
  54. this.lengthCache = u_list;
  55. },
  56. normalizeArray: function(ls, v) {
  57. var len = ls.length;
  58. for (var i = 0;i < len;i++) {
  59. ls[i] /= v;
  60. }
  61. return ls;
  62. }
  63. };
  64. /*
  65. * @author zz85 / http://github.com/zz85
  66. * @author WestLangley / http://github.com/WestLangley
  67. *
  68. * tool for "unwrapping" and debugging three.js
  69. * geometries UV mapping
  70. *
  71. * Sample usage:
  72. * document.body.appendChild(
  73. * THREE.UVsDebug(
  74. * new THREE.SphereGeometry(10,10,10,10));
  75. *
  76. */
  77. THREE.UVsDebug = function( geometry, size ) {
  78. // handles wrapping of uv.x > 1 only
  79. var abc = 'abcd';
  80. var uv, u, ax, ay;
  81. var i, il, j, jl;
  82. var vnum;
  83. var a = new THREE.Vector2();
  84. var b = new THREE.Vector2();
  85. var faces = geometry.faces;
  86. var uvs = geometry.faceVertexUvs[ 0 ];
  87. var canvas = document.createElement( 'canvas' );
  88. var width = size || 1024; // power of 2 required for wrapping
  89. var height = size || 1024;
  90. canvas.width = width;
  91. canvas.height = height;
  92. var ctx = canvas.getContext( '2d' );
  93. ctx.lineWidth = 2;
  94. ctx.strokeStyle = 'rgba( 0, 0, 0, 1.0 )';
  95. ctx.textAlign = 'center';
  96. // paint background white
  97. ctx.fillStyle = 'rgba( 255, 255, 255, 1.0 )';
  98. ctx.fillRect( 0, 0, width, height );
  99. for ( i = 0, il = uvs.length; i < il; i++ ) {
  100. uv = uvs[ i ];
  101. // draw lines
  102. ctx.beginPath();
  103. a.set( 0, 0 );
  104. for ( j = 0, jl = uv.length; j < jl; j++ ) {
  105. u = uv[ j ];
  106. a.x += u.x;
  107. a.y += u.y;
  108. if ( j == 0 ) {
  109. ctx.moveTo( u.x * width, ( 1 - u.y ) * height );
  110. } else {
  111. ctx.lineTo( u.x * width, ( 1 - u.y ) * height );
  112. }
  113. }
  114. ctx.closePath();
  115. ctx.stroke();
  116. a.divideScalar( jl );
  117. // label the face number
  118. ctx.font = "12pt Arial bold";
  119. ctx.fillStyle = 'rgba( 0, 0, 0, 1.0 )';
  120. ctx.fillText( i, a.x * width, ( 1 - a.y ) * height );
  121. if ( a.x > 0.95 ) { // wrap x // 0.95 is arbitrary
  122. ctx.fillText( i, ( a.x % 1 ) * width, ( 1 - a.y ) * height );
  123. }
  124. ctx.font = "8pt Arial bold";
  125. ctx.fillStyle = 'rgba( 0, 0, 0, 1.0 )';
  126. // label uv edge orders
  127. for ( j = 0, jl = uv.length; j < jl; j++ ) {
  128. u = uv[ j ];
  129. b.addVectors( a, u ).divideScalar( 2 );
  130. vnum = faces[ i ][ abc[ j ] ];
  131. ctx.fillText( abc[ j ] + vnum, b.x * width, ( 1 - b.y ) * height );
  132. if ( b.x > 0.95 ) { // wrap x
  133. ctx.fillText( abc[ j ] + vnum, ( b.x % 1 ) * width, ( 1 - b.y ) * height );
  134. }
  135. }
  136. }
  137. return canvas;
  138. }