PlaneGeometry.js 2.4 KB

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