1 line
7.0 KiB
Plaintext
1 line
7.0 KiB
Plaintext
{"version":3,"file":"PerspectiveMesh.mjs","sources":["../../../src/scene/mesh-perspective/PerspectiveMesh.ts"],"sourcesContent":["import { Texture } from '../../rendering/renderers/shared/texture/Texture';\nimport { definedProps } from '../container/utils/definedProps';\nimport { Mesh } from '../mesh/shared/Mesh';\nimport { PerspectivePlaneGeometry } from './PerspectivePlaneGeometry';\n\nimport type { MeshPlaneOptions } from '../mesh-plane/MeshPlane';\n/**\n *\n * Constructor options used for `PerspectiveMesh` instances.\n * ```js\n * const meshPlane = new PerspectiveMesh({\n * texture: Texture.from('snake.png'),\n * verticesX: 20,\n * verticesY: 20,\n * x0: 0,\n * y0: 0,\n * x1: 100,\n * y1: 0,\n * x2: 100,\n * y2: 100,\n * x3: 0,\n * y3: 100\n * });\n * @see {@link scene.PerspectiveMesh}\n * @memberof scene\n */\nexport interface PerspectivePlaneOptions extends MeshPlaneOptions\n{\n /** top left corner x value */\n x0?: number,\n /** top left corner y value */\n y0?: number,\n /** top right corner x value */\n x1?: number,\n /** top right corner y value */\n y1?: number,\n /** bottom right corner x value */\n x2?: number,\n /** bottom right corner y value */\n y2?: number,\n /** bottom left corner x value */\n x3?: number,\n /** bottom left corner y value */\n y3?: number\n}\n\n/**\n * A perspective mesh that allows you to draw a 2d plane with perspective. Where ever you move the corners\n * the texture will be projected to look like it is in 3d space. Great for mapping a 2D mesh into a 3D scene.\n *\n * The calculations is done at the uv level. This means that the more vertices you have the more smooth\n * the perspective will be. If you have a low amount of vertices you may see the texture stretch. Too many vertices\n * could be slower. It is a balance between performance and quality! We leave that to you to decide.\n *\n * IMPORTANT: This is not a full 3D mesh, it is a 2D mesh with a perspective projection applied to it :)\n * @example\n * ```js\n * const meshPlane = new PerspectiveMesh({\n * texture: Texture.from('snake.png'),\n * verticesX: 20,\n * verticesY: 20,\n * x0: 0,\n * y0: 0,\n * x1: 100,\n * y1: 0,\n * x2: 100,\n * y2: 100,\n * x3: 0,\n * y3: 100\n * });\n * @see {@link scene.PerspectiveMesh}\n * @memberof scene\n */\nexport class PerspectiveMesh extends Mesh<PerspectivePlaneGeometry>\n{\n /** default options for the mesh */\n public static defaultOptions: PerspectivePlaneOptions = {\n texture: Texture.WHITE,\n verticesX: 10,\n verticesY: 10,\n x0: 0,\n y0: 0,\n x1: 100,\n y1: 0,\n x2: 100,\n y2: 100,\n x3: 0,\n y3: 100\n };\n\n /**\n * @param options - Options to be applied to PerspectiveMesh\n */\n constructor(options: PerspectivePlaneOptions)\n {\n options = { ...PerspectiveMesh.defaultOptions, ...options };\n\n const { texture, verticesX, verticesY, ...rest } = options;\n const planeGeometry = new PerspectivePlaneGeometry(definedProps({\n width: texture.width,\n height: texture.height,\n verticesX,\n verticesY,\n }));\n\n super(definedProps({ ...rest, geometry: planeGeometry }));\n\n this._texture = texture;\n\n this.geometry.setCorners(\n options.x0, options.y0,\n options.x1, options.y1,\n options.x2, options.y2,\n options.x3, options.y3\n );\n }\n\n /** Update the geometry when the texture is updated */\n protected textureUpdated(): void\n {\n const geometry: PerspectivePlaneGeometry = this.geometry as any;\n\n if (!geometry) return;\n\n const { width, height } = this.texture;\n\n if (geometry.width !== width || geometry.height !== height)\n {\n geometry.width = width;\n geometry.height = height;\n geometry.updateProjection();\n }\n }\n\n set texture(value: Texture)\n {\n if (this._texture === value) return;\n\n super.texture = value;\n\n this.textureUpdated();\n }\n\n /** The texture that the mesh uses */\n get texture(): Texture\n {\n return this._texture;\n }\n\n /**\n * Set the corners of the quad to the given coordinates\n * The mesh will then calculate the perspective so it looks correct!\n * @param x0 - x coordinate of the first corner\n * @param y0 - y coordinate of the first corner\n * @param x1 - x coordinate of the second corner\n * @param y1 - y coordinate of the second corner\n * @param x2 - x coordinate of the third corner\n * @param y2 - y coordinate of the third corner\n * @param x3 - x coordinate of the fourth corner\n * @param y3 - y coordinate of the fourth corner\n */\n public setCorners(x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, x3: number, y3: number)\n {\n this.geometry.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