* Transforms the geometry's vertices to fit snugly within a 100×100×100 box * centered at the origin. * * Calling `myGeometry.normalize()` translates the geometry's vertices so that * they're centered at the origin `(0, 0, 0)`. Then it scales the vertices so * that they fill a 100×100
()
| 1663 | * } |
| 1664 | */ |
| 1665 | normalize() { |
| 1666 | if (this.vertices.length > 0) { |
| 1667 | // Find the corners of our bounding box |
| 1668 | const maxPosition = this.vertices[0].copy(); |
| 1669 | const minPosition = this.vertices[0].copy(); |
| 1670 | |
| 1671 | for (let i = 0; i < this.vertices.length; i++) { |
| 1672 | maxPosition.x = Math.max(maxPosition.x, this.vertices[i].x); |
| 1673 | minPosition.x = Math.min(minPosition.x, this.vertices[i].x); |
| 1674 | maxPosition.y = Math.max(maxPosition.y, this.vertices[i].y); |
| 1675 | minPosition.y = Math.min(minPosition.y, this.vertices[i].y); |
| 1676 | maxPosition.z = Math.max(maxPosition.z, this.vertices[i].z); |
| 1677 | minPosition.z = Math.min(minPosition.z, this.vertices[i].z); |
| 1678 | } |
| 1679 | |
| 1680 | const center = Vector.lerp(maxPosition, minPosition, 0.5); |
| 1681 | const dist = Vector.sub(maxPosition, minPosition); |
| 1682 | const longestDist = Math.max(Math.max(dist.x, dist.y), dist.z); |
| 1683 | const scale = 200 / longestDist; |
| 1684 | |
| 1685 | for (let i = 0; i < this.vertices.length; i++) { |
| 1686 | this.vertices[i].sub(center); |
| 1687 | this.vertices[i].mult(scale); |
| 1688 | } |
| 1689 | } |
| 1690 | return this; |
| 1691 | } |
| 1692 | |
| 1693 | /** Sets the shader's vertex property or attribute variables. |
| 1694 | * |
no test coverage detected