UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

81 lines (78 loc) 3.3 kB
import { Vec2 } from '../../core/math/vec2.js'; import { calculateTangents } from './geometry-utils.js'; import { Geometry } from './geometry.js'; /** * A procedural plane-shaped geometry. * * The size and tesselation properties of the plane can be controlled via constructor parameters. By * default, the function will create a plane centered on the object space origin with a width and * length of 1.0 and 5 segments in either axis (50 triangles). The normal vector of the plane is * aligned along the positive Y axis. * * Note that the plane is created with UVs in the range of 0 to 1. * * @category Graphics */ class PlaneGeometry extends Geometry { /** * Create a new PlaneGeometry instance. * * @param {object} [opts] - An object that specifies optional inputs for the function as follows: * @param {Vec2} [opts.halfExtents] - The half dimensions of the plane in the X and Z axes * (defaults to [0.5, 0.5]). * @param {number} [opts.widthSegments] - The number of divisions along the X axis of the plane * (defaults to 5). * @param {number} [opts.lengthSegments] - The number of divisions along the Z axis of the plane * (defaults to 5). * @param {boolean} [opts.calculateTangents] - Generate tangent information (defaults to false). */ constructor(opts = {}){ super(); var _opts_halfExtents; // Check the supplied options and provide defaults for unspecified ones var he = (_opts_halfExtents = opts.halfExtents) != null ? _opts_halfExtents : new Vec2(0.5, 0.5); var _opts_widthSegments; var ws = (_opts_widthSegments = opts.widthSegments) != null ? _opts_widthSegments : 5; var _opts_lengthSegments; var ls = (_opts_lengthSegments = opts.lengthSegments) != null ? _opts_lengthSegments : 5; // Variable declarations var positions = []; var normals = []; var uvs = []; var indices = []; // Generate plane as follows (assigned UVs denoted at corners): // (0,1)x---------x(1,1) // | | // | | // | O--X |length // | | | // | Z | // (0,0)x---------x(1,0) // width var vcounter = 0; for(var i = 0; i <= ws; i++){ for(var j = 0; j <= ls; j++){ var x = -he.x + 2 * he.x * i / ws; var y = 0.0; var z = -(-he.y + 2 * he.y * j / ls); var u = i / ws; var v = j / ls; positions.push(x, y, z); normals.push(0, 1, 0); uvs.push(u, 1 - v); if (i < ws && j < ls) { indices.push(vcounter + ls + 1, vcounter + 1, vcounter); indices.push(vcounter + ls + 1, vcounter + ls + 2, vcounter + 1); } vcounter++; } } this.positions = positions; this.normals = normals; this.uvs = uvs; this.uvs1 = uvs; // UV1 = UV0 for plane this.indices = indices; if (opts.calculateTangents) { this.tangents = calculateTangents(positions, normals, uvs, indices); } } } export { PlaneGeometry };