PlaneGeometry.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. import { Geometry } from '../core/Geometry.js';
  6. import { BufferGeometry } from '../core/BufferGeometry.js';
  7. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  8. // PlaneGeometry
  9. function PlaneGeometry( width, height, widthSegments, heightSegments ) {
  10. Geometry.call( this );
  11. this.type = 'PlaneGeometry';
  12. this.parameters = {
  13. width: width,
  14. height: height,
  15. widthSegments: widthSegments,
  16. heightSegments: heightSegments
  17. };
  18. this.fromBufferGeometry( new PlaneBufferGeometry( width, height, widthSegments, heightSegments ) );
  19. this.mergeVertices();
  20. }
  21. PlaneGeometry.prototype = Object.create( Geometry.prototype );
  22. PlaneGeometry.prototype.constructor = PlaneGeometry;
  23. // PlaneBufferGeometry
  24. function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) {
  25. BufferGeometry.call( this );
  26. this.type = 'PlaneBufferGeometry';
  27. this.parameters = {
  28. width: width,
  29. height: height,
  30. widthSegments: widthSegments,
  31. heightSegments: heightSegments
  32. };
  33. width = width || 1;
  34. height = height || 1;
  35. var width_half = width / 2;
  36. var height_half = height / 2;
  37. var gridX = Math.floor( widthSegments ) || 1;
  38. var gridY = Math.floor( heightSegments ) || 1;
  39. var gridX1 = gridX + 1;
  40. var gridY1 = gridY + 1;
  41. var segment_width = width / gridX;
  42. var segment_height = height / gridY;
  43. var ix, iy;
  44. // buffers
  45. var indices = [];
  46. var vertices = [];
  47. var normals = [];
  48. var uvs = [];
  49. // generate vertices, normals and uvs
  50. for ( iy = 0; iy < gridY1; iy ++ ) {
  51. var y = iy * segment_height - height_half;
  52. for ( ix = 0; ix < gridX1; ix ++ ) {
  53. var x = ix * segment_width - width_half;
  54. vertices.push( x, - y, 0 );
  55. normals.push( 0, 0, 1 );
  56. uvs.push( ix / gridX );
  57. uvs.push( 1 - ( iy / gridY ) );
  58. }
  59. }
  60. // indices
  61. for ( iy = 0; iy < gridY; iy ++ ) {
  62. for ( ix = 0; ix < gridX; ix ++ ) {
  63. var a = ix + gridX1 * iy;
  64. var b = ix + gridX1 * ( iy + 1 );
  65. var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  66. var d = ( ix + 1 ) + gridX1 * iy;
  67. // faces
  68. indices.push( a, b, d );
  69. indices.push( b, c, d );
  70. }
  71. }
  72. // build geometry
  73. this.setIndex( indices );
  74. this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  75. this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  76. this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  77. }
  78. PlaneBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  79. PlaneBufferGeometry.prototype.constructor = PlaneBufferGeometry;
  80. export { PlaneGeometry, PlaneBufferGeometry };