UNPKG

pixi.js

Version:

<p align="center"> <a href="https://pixijs.com" target="_blank" rel="noopener noreferrer"> <img height="150" src="https://files.pixijs.download/branding/pixijs-logo-transparent-dark.svg?v=1" alt="PixiJS logo"> </a> </p> <br/> <p align="center">

1 lines 13 kB
{"version":3,"file":"RopeGeometry.mjs","sources":["../../../src/scene/mesh-simple/RopeGeometry.ts"],"sourcesContent":["import { MeshGeometry } from '../mesh/shared/MeshGeometry';\n\nimport type { PointData } from '../../maths/point/PointData';\nimport type { MeshGeometryOptions } from '../mesh/shared/MeshGeometry';\n\n/**\n * Constructor options used for `RopeGeometry` instances.\n * ```js\n * const ropeGeometry = new RopeGeometry({\n * points: [new Point(0, 0), new Point(100, 0)],\n * width: 10,\n * textureScale: 0,\n * });\n * ```\n * @see {@link RopeGeometry}\n * @category scene\n * @advanced\n */\nexport interface RopeGeometryOptions\n{\n /** The width (i.e., thickness) of the rope. */\n width?: number;\n /** An array of points that determine the rope. */\n points?: PointData[];\n /**\n * Rope texture scale, if zero then the rope texture is stretched.\n * By default the rope texture will be stretched to match\n * rope length. If textureScale is positive this value will be treated as a scaling\n * factor and the texture will preserve its aspect ratio instead. To create a tiling rope\n * set baseTexture.wrapMode to 'repeat' and use a power of two texture,\n * then set textureScale=1 to keep the original texture pixel size.\n * In order to reduce alpha channel artifacts provide a larger texture and downsample -\n * i.e. set textureScale=0.5 to scale it down twice.\n */\n textureScale?: number;\n}\n\n/**\n * RopeGeometry allows you to draw a geometry across several points and then manipulate these points.\n * @example\n * import { Point, RopeGeometry } from 'pixi.js';\n *\n * for (let i = 0; i < 20; i++) {\n * points.push(new Point(i * 50, 0));\n * };\n * const rope = new RopeGeometry(100, points);\n * @category scene\n * @advanced\n */\nexport class RopeGeometry extends MeshGeometry\n{\n /** Default options for RopeGeometry constructor. */\n public static defaultOptions: RopeGeometryOptions & MeshGeometryOptions = {\n /** The width (i.e., thickness) of the rope. */\n width: 200,\n /** An array of points that determine the rope. */\n points: [],\n /** Rope texture scale, if zero then the rope texture is stretched. */\n textureScale: 0,\n };\n\n /** An array of points that determine the rope. */\n public points: PointData[];\n\n /** Rope texture scale, if zero then the rope texture is stretched. */\n public readonly textureScale: number;\n\n /**\n * The width (i.e., thickness) of the rope.\n * @readonly\n * @internal\n */\n public _width: number;\n\n /**\n * @param options - Options to be applied to rope geometry\n */\n constructor(options: RopeGeometryOptions)\n {\n const { width, points, textureScale } = { ...RopeGeometry.defaultOptions, ...options };\n\n super({\n positions: new Float32Array(points.length * 4),\n uvs: new Float32Array(points.length * 4),\n indices: new Uint32Array((points.length - 1) * 6),\n });\n\n this.points = points;\n this._width = width;\n this.textureScale = textureScale;\n\n this._build();\n }\n\n /**\n * The width (i.e., thickness) of the rope.\n * @readonly\n */\n get width(): number\n {\n return this._width;\n }\n\n /** Refreshes Rope indices and uvs */\n private _build(): void\n {\n const points = this.points;\n\n if (!points) return;\n\n const vertexBuffer = this.getBuffer('aPosition');\n const uvBuffer = this.getBuffer('aUV');\n const indexBuffer = this.getIndex();\n\n // if too little points, or texture hasn't got UVs set yet just move on.\n if (points.length < 1)\n {\n return;\n }\n\n // if the number of points has changed we will need to recreate the arraybuffers\n if (vertexBuffer.data.length / 4 !== points.length)\n {\n vertexBuffer.data = new Float32Array(points.length * 4);\n uvBuffer.data = new Float32Array(points.length * 4);\n indexBuffer.data = new Uint16Array((points.length - 1) * 6);\n }\n\n const uvs = uvBuffer.data;\n const indices = indexBuffer.data;\n\n uvs[0] = 0;\n uvs[1] = 0;\n uvs[2] = 0;\n uvs[3] = 1;\n\n let amount = 0;\n let prev = points[0];\n const textureWidth = this._width * this.textureScale;\n const total = points.length; // - 1;\n\n for (let i = 0; i < total; i++)\n {\n // time to do some smart drawing!\n const index = i * 4;\n\n if (this.textureScale > 0)\n {\n // calculate pixel distance from previous point\n const dx = prev.x - points[i].x;\n const dy = prev.y - points[i].y;\n const distance = Math.sqrt((dx * dx) + (dy * dy));\n\n prev = points[i];\n amount += distance / textureWidth;\n }\n else\n {\n // stretch texture\n amount = i / (total - 1);\n }\n\n uvs[index] = amount;\n uvs[index + 1] = 0;\n\n uvs[index + 2] = amount;\n uvs[index + 3] = 1;\n }\n\n let indexCount = 0;\n\n for (let i = 0; i < total - 1; i++)\n {\n const index = i * 2;\n\n indices[indexCount++] = index;\n indices[indexCount++] = index + 1;\n indices[indexCount++] = index + 2;\n\n indices[indexCount++] = index + 2;\n indices[indexCount++] = index + 1;\n indices[indexCount++] = index + 3;\n }\n\n // ensure that the changes are uploaded\n uvBuffer.update();\n indexBuffer.update();\n\n this.updateVertices();\n }\n\n /** refreshes vertices of Rope mesh */\n public updateVertices(): void\n {\n const points = this.points;\n\n if (points.length < 1)\n {\n return;\n }\n\n let lastPoint = points[0];\n let nextPoint;\n let perpX = 0;\n let perpY = 0;\n\n const vertices = this.buffers[0].data;\n const total = points.length;\n const halfWidth = this.textureScale > 0 ? this.textureScale * this._width / 2 : this._width / 2;\n\n for (let i = 0; i < total; i++)\n {\n const point = points[i];\n const index = i * 4;\n\n if (i < points.length - 1)\n {\n nextPoint = points[i + 1];\n }\n else\n {\n nextPoint = point;\n }\n\n perpY = -(nextPoint.x - lastPoint.x);\n perpX = nextPoint.y - lastPoint.y;\n\n let ratio = (1 - (i / (total - 1))) * 10;\n\n if (ratio > 1)\n {\n ratio = 1;\n }\n\n const perpLength = Math.sqrt((perpX * perpX) + (perpY * perpY));\n\n if (perpLength < 1e-6)\n {\n perpX = 0;\n perpY = 0;\n }\n else\n {\n perpX /= perpLength;\n perpY /= perpLength;\n\n perpX *= halfWidth;\n perpY *= halfWidth;\n }\n\n vertices[index] = point.x + perpX;\n vertices[index + 1] = point.y + perpY;\n vertices[index + 2] = point.x - perpX;\n vertices[index + 3] = point.y - perpY;\n\n lastPoint = point;\n }\n\n this.buffers[0].update();\n }\n\n /** Refreshes Rope indices and uvs */\n public update(): void\n {\n if (this.textureScale > 0)\n {\n this._build(); // we need to update UVs\n }\n else\n {\n this.updateVertices();\n }\n }\n}\n"],"names":[],"mapp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