plane.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Plane.as
  4. */
  5. var Plane = function ( width, height, segments_width, segments_height ) {
  6. THREE.Geometry.call( this );
  7. var ix, iy,
  8. width_half = width / 2,
  9. height_half = height / 2,
  10. gridX = segments_width || 1,
  11. gridY = segments_height || 1,
  12. gridX1 = gridX + 1,
  13. gridY1 = gridY + 1,
  14. segment_width = width / gridX,
  15. segment_height = height / gridY;
  16. for( iy = 0; iy < gridY1; iy++ ) {
  17. for( ix = 0; ix < gridX1; ix++ ) {
  18. var x = ix * segment_width - width_half;
  19. var y = iy * segment_height - height_half;
  20. this.vertices.push( new THREE.Vertex( new THREE.Vector3( x, - y, 0 ) ) );
  21. }
  22. }
  23. for( iy = 0; iy < gridY; iy++ ) {
  24. for( ix = 0; ix < gridX; ix++ ) {
  25. var a = ix + gridX1 * iy;
  26. var b = ix + gridX1 * ( iy + 1 );
  27. var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  28. var d = ( ix + 1 ) + gridX1 * iy;
  29. this.faces.push( new THREE.Face4( a, b, c, d ) );
  30. this.uvs.push( [
  31. new THREE.UV( ix / gridX, iy / gridY ),
  32. new THREE.UV( ix / gridX, ( iy + 1 ) / gridY ),
  33. new THREE.UV( ( ix + 1 ) / gridX, ( iy + 1 ) / gridY ),
  34. new THREE.UV( ( ix + 1 ) / gridX, iy / gridY )
  35. ] );
  36. }
  37. }
  38. this.computeCentroids();
  39. this.computeFaceNormals();
  40. this.sortFacesByMaterial();
  41. };
  42. Plane.prototype = new THREE.Geometry();
  43. Plane.prototype.constructor = Plane;