PlaneTypedGeometry.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Plane.as
  4. */
  5. THREE.PlaneTypedGeometry = function ( width, height, widthSegments, heightSegments ) {
  6. this.parameters = {
  7. width: width,
  8. height: height,
  9. widthSegments: widthSegments,
  10. heightSegments: heightSegments
  11. };
  12. var width_half = width / 2;
  13. var height_half = height / 2;
  14. var gridX = widthSegments || 1;
  15. var gridY = heightSegments || 1;
  16. var gridX1 = gridX + 1;
  17. var gridY1 = gridY + 1;
  18. var segment_width = width / gridX;
  19. var segment_height = height / gridY;
  20. var vertices = new Float32Array( gridX1 * gridY1 * 3 );
  21. var normals = new Float32Array( gridX1 * gridY1 * 3 );
  22. var uvs = new Float32Array( gridX1 * gridY1 * 2 );
  23. var offset = 0;
  24. var offset2 = 0;
  25. for ( var iy = 0; iy < gridY1; iy ++ ) {
  26. var y = iy * segment_height - height_half;
  27. for ( var ix = 0; ix < gridX1; ix ++ ) {
  28. var x = ix * segment_width - width_half;
  29. vertices[ offset ] = x;
  30. vertices[ offset + 1 ] = - y;
  31. normals[ offset + 2 ] = 1;
  32. uvs[ offset2 ] = ix / gridX;
  33. uvs[ offset2 + 1 ] = 1 - ( iy / gridY );
  34. offset += 3;
  35. offset2 += 2;
  36. }
  37. }
  38. offset = 0;
  39. var indices = new ( ( vertices.length / 3 ) > 65535 ? Uint32Array : Uint16Array )( gridX * gridY * 6 );
  40. for ( var iy = 0; iy < gridY; iy ++ ) {
  41. for ( var ix = 0; ix < gridX; ix ++ ) {
  42. var a = ix + gridX1 * iy;
  43. var b = ix + gridX1 * ( iy + 1 );
  44. var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  45. var d = ( ix + 1 ) + gridX1 * iy;
  46. indices[ offset ] = a;
  47. indices[ offset + 1 ] = b;
  48. indices[ offset + 2 ] = d;
  49. indices[ offset + 3 ] = b;
  50. indices[ offset + 4 ] = c;
  51. indices[ offset + 5 ] = d;
  52. offset += 6;
  53. }
  54. }
  55. THREE.IndexedTypedGeometry.call( this );
  56. this.setArrays( indices, vertices, normals, uvs );
  57. this.computeBoundingSphere();
  58. };
  59. THREE.PlaneTypedGeometry.prototype = Object.create( THREE.IndexedTypedGeometry.prototype );
  60. THREE.PlaneTypedGeometry.prototype.constructor = THREE.PlaneTypedGeometry;