"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const BufferGeometry_js = require("three/src/core/BufferGeometry.js"); const Uniform_js = require("three/src/core/Uniform.js"); const BufferAttribute_js = require("three/src/core/BufferAttribute.js"); const Sphere_js = require("three/src/math/Sphere.js"); const Box3_js = require("three/src/math/Box3.js"); const Vector4_js = require("three/src/math/Vector4.js"); function pages(glyphs) { var pages2 = new Float32Array(glyphs.length * 4 * 1); var i = 0; glyphs.forEach(function(glyph) { var id = glyph.data.page || 0; pages2[i++] = id; pages2[i++] = id; pages2[i++] = id; pages2[i++] = id; }); return pages2; } function generateUV(glyph, uvs, i, gridX, gridY, opt) { let gridX1 = gridX + 1; let gridY1 = gridY + 1; var bitmap = glyph.data; let texWidth = opt.texWidth; let texHeight = opt.texHeight; bitmap.x + bitmap.width; bitmap.y + bitmap.height; let startX = bitmap.x / texWidth; let startY = bitmap.y / texHeight; let w = bitmap.width / texWidth; let h = bitmap.height / texHeight; const segment_width = w / gridX; const segment_height = h / gridY; for (let iy = 0; iy < gridY1; iy++) { let v = iy * segment_height + startY; v = 1 - v; for (let ix = 0; ix < gridX1; ix++) { const u = ix * segment_width + startX; uvs[i++] = u; uvs[i++] = v; } } } function generateExtendedUV(glyph, uvs, i, gridX, gridY, opt) { let extend = opt.extend; var bitmap = glyph.data; let texWidth = opt.texWidth; let texHeight = opt.texHeight; let right = bitmap.x + bitmap.width; let left = bitmap.x; let top = bitmap.y; let bottom = bitmap.y + bitmap.height; let extendTop = extend.top; let extendBottom = extend.bottom; if (opt.flipY) { top = texHeight - top; if (!!extend.top) { extendTop = -extend.top; } bottom = texHeight - bottom; if (!!extend.bottom) { extendBottom = -extend.bottom; } } if (!!extendTop) { uvs[i++] = left / texWidth; uvs[i++] = (top - extendTop) / texHeight; uvs[i++] = right / texWidth; uvs[i++] = (top - extendTop) / texHeight; } if (!!extend.right) { if (!!extendTop) { uvs[i++] = (right + extend.right) / texWidth; uvs[i++] = (top - extendTop) / texHeight; } uvs[i++] = (right + extend.right) / texWidth; uvs[i++] = top / texHeight; uvs[i++] = (right + extend.right) / texWidth; uvs[i++] = bottom / texHeight; } if (!!extendBottom) { if (!!extend.right) { uvs[i++] = (right + extend.right) / texWidth; uvs[i++] = (bottom + extendBottom) / texHeight; } uvs[i++] = right / texWidth; uvs[i++] = (bottom + extendBottom) / texHeight; uvs[i++] = left / texWidth; uvs[i++] = (bottom + extendBottom) / texHeight; } if (!!extend.left) { if (!!extendBottom) { uvs[i++] = (left - extend.left) / texWidth; uvs[i++] = (bottom + extendBottom) / texHeight; } uvs[i++] = (left - extend.left) / texWidth; uvs[i++] = bottom / texHeight; uvs[i++] = (left - extend.left) / texWidth; uvs[i++] = top / texHeight; if (!!extendTop) { uvs[i++] = (left - extend.left) / texWidth; uvs[i++] = (top - extendTop) / texWidth; } } return i; } function generatePositions(glyph, positions, i, gridX, gridY) { let gridX1 = gridX + 1; let gridY1 = gridY + 1; var bitmap = glyph.data; var x = glyph.position[0] + bitmap.xoffset; var y = glyph.position[1] + bitmap.yoffset; var w = bitmap.width; var h = bitmap.height; const segment_width = w / gridX; const segment_height = h / gridY; for (let iy = 0; iy < gridY1; iy++) { const vy = iy * segment_height + y; for (let ix = 0; ix < gridX1; ix++) { const vx = ix * segment_width + x; positions[i++] = vx; positions[i++] = vy; } } } function generateLayoutUVs(glyph, layoutUVs, i, gridX, gridY, opt, layout) { let gridX1 = gridX + 1; let gridY1 = gridY + 1; let lineUV = glyph.line / (layout._linesTotal - 1); let letterUV = glyph.index / (layout.glyphs.length - 1); let wordUV = glyph.wordIndex / layout._wordsTotal; for (let iy = 0; iy < gridY1; iy++) { for (let ix = 0; ix < gridX1; ix++) { layoutUVs[i++] = letterUV; layoutUVs[i++] = wordUV; layoutUVs[i++] = lineUV; } } } function generateExtension(glyph, positions, i, gridX, gridY, opt) { var bitmap = glyph.data; let extend = opt.extend; var x = glyph.position[0] + bitmap.xoffset; var y = glyph.position[1] + bitmap.yoffset; var w = bitmap.width; var h = bitmap.height; if (!!extend.top) { positions[i++] = x; positions[i++] = y - extend.top; positions[i++] = x + w; positions[i++] = y - extend.top; } if (!!extend.right) { if (!!extend.top) { positions[i++] = x + w + extend.right; positions[i++] = y - extend.top; } positions[i++] = x + w + extend.right; positions[i++] = y; positions[i++] = x + w + extend.right; positions[i++] = y + h; } if (!!extend.bottom) { if (!!extend.right) { positions[i++] = x + w + extend.right; positions[i++] = y + h + extend.bottom; } positions[i++] = x + w; positions[i++] = y + h + extend.bottom; positions[i++] = x; positions[i++] = y + h + extend.bottom; } if (!!extend.left) { if (!!extend.bottom) { positions[i++] = x - extend.left; positions[i++] = y + h + extend.bottom; } positions[i++] = x - extend.left; positions[i++] = y + h; positions[i++] = x - extend.left; positions[i++] = y; if (!!extend.top) { positions[i++] = x - extend.left; positions[i++] = y - extend.top; } } return i; } function getExtendedVerticesCount(top = 0, right = 0, bottom = 0, left = 0) { top = !!top; right = !!right; bottom = !!bottom; left = !!left; return (top + right + bottom + left) * 2 + top * right + right * bottom + bottom * left + left * top; } function getAttributes(layout, opt) { const glyphs = layout.glyphs; const gridX = Math.floor(opt.widthSegments); const gridY = Math.floor(opt.heightSegments); const gridX1 = gridX + 1; const gridY1 = gridY + 1; let nExtendedVertices = 0; if (opt.extend) { nExtendedVertices = getExtendedVerticesCount( opt.extend.top, opt.extend.right, opt.extend.bottom, opt.extend.left ); } var uvs = new Float32Array( glyphs.length * 2 * (gridX1 * gridY1 + nExtendedVertices) ); var positions = new Float32Array( glyphs.length * 2 * (gridX1 * gridY1 + nExtendedVertices) ); let layoutUVs = null; opt.createLayoutUVs = true; if (opt.createLayoutUVs) { layoutUVs = new Float32Array( glyphs.length * 3 * (gridX1 * gridY1 + nExtendedVertices) ); } let uvI = 0; let posI = 0; let layoutUVI = 0; for (let i = 0; i < glyphs.length; i++) { let glyph = glyphs[i]; generateUV(glyph, uvs, uvI, gridX, gridY, opt); generatePositions(glyph, positions, posI, gridX, gridY); if (opt.createLayoutUVs) { generateLayoutUVs(glyph, layoutUVs, layoutUVI, gridX, gridY, opt, layout); layoutUVI += 3 * gridX1 * gridY1; } uvI += 2 * gridX1 * gridY1; posI += 2 * gridX1 * gridY1; if (opt.extend != null) { posI = generateExtension(glyph, positions, posI, gridX, gridY, opt); uvI = generateExtendedUV(glyph, uvs, uvI, gridX, gridY, opt); } } return { uvs, positions, layoutUVs }; } function indices(opt) { const gridX = Math.floor(opt.widthSegments); const gridY = Math.floor(opt.heightSegments); const gridX1 = gridX + 1; let indices2 = []; let verticesPerGlyph = (gridX + 1) * (gridY + 1); let glyphVertexStart = 0; for (let ig = 0; ig < opt.count; ig++) { for (let iy = 0; iy < gridY; iy++) { for (let ix = 0; ix < gridX; ix++) { const a = glyphVertexStart + ix + gridX1 * iy; const b = glyphVertexStart + ix + gridX1 * (iy + 1); const c = glyphVertexStart + ix + 1 + gridX1 * (iy + 1); const d = glyphVertexStart + ix + 1 + gridX1 * iy; indices2.push(a, b, d); indices2.push(b, c, d); } } if (opt.extend == null) { glyphVertexStart += verticesPerGlyph; continue; } let tl = glyphVertexStart; let tr = glyphVertexStart + gridX; let br = glyphVertexStart + verticesPerGlyph - 1; let bl = glyphVertexStart + verticesPerGlyph - gridX - 1; let extendStart = glyphVertexStart + verticesPerGlyph; let currentExtendIndex = extendStart; let extend = opt.extend; if (!!extend.top) { indices2.push(currentExtendIndex, currentExtendIndex + 1, tl); indices2.push(currentExtendIndex + 1, tr, tl); currentExtendIndex += 2; } if (!!extend.right) { if (!!extend.top) { indices2.push(currentExtendIndex - 1, currentExtendIndex, tr); indices2.push(currentExtendIndex, currentExtendIndex + 1, tr); currentExtendIndex++; } indices2.push( tr, currentExtendIndex, br // last of glyphs vertices ); indices2.push( currentExtendIndex, currentExtendIndex + 1, br // this - 1 is fine here because it starts at 0 ); currentExtendIndex += 2; } if (!!extend.bottom) { if (!!extend.right) { indices2.push(br, currentExtendIndex - 1, currentExtendIndex + 1); indices2.push( currentExtendIndex - 1, currentExtendIndex, currentExtendIndex + 1 ); currentExtendIndex++; } indices2.push( bl, // bl corner of square br, // br corner of square currentExtendIndex + 1 // bl of bottom extension ); indices2.push(br, currentExtendIndex, currentExtendIndex + 1); currentExtendIndex += 2; } if (!!extend.left) { if (!!extend.bottom) { indices2.push(currentExtendIndex + 1, bl, currentExtendIndex); indices2.push(bl, currentExtendIndex - 1, currentExtendIndex); currentExtendIndex++; } indices2.push( currentExtendIndex + 1, // bl corner of square tl, // br corner of square currentExtendIndex // bl of bottom extension ); indices2.push(tl, bl, currentExtendIndex); currentExtendIndex += 2; if (!!extend.top) { indices2.push(currentExtendIndex, extendStart, currentExtendIndex - 1); indices2.push(extendStart, tl, currentExtendIndex - 1); currentExtendIndex++; } } glyphVertexStart = currentExtendIndex; } return indices2; } const vertices = { indices, pages, getAttributes }; var itemSize = 2; var box = { min: [0, 0], max: [0, 0] }; function bounds(positions) { var count = positions.length / itemSize; box.min[0] = positions[0]; box.min[1] = positions[1]; box.max[0] = positions[0]; box.max[1] = positions[1]; for (var i = 0; i < count; i++) { var x = positions[i * itemSize + 0]; var y = positions[i * itemSize + 1]; box.min[0] = Math.min(x, box.min[0]); box.min[1] = Math.min(y, box.min[1]); box.max[0] = Math.max(x, box.max[0]); box.max[1] = Math.max(y, box.max[1]); } } function computeBox(positions, output) { bounds(positions); output.min.set(box.min[0], box.min[1], 0); output.max.set(box.max[0], box.max[1], 0); } function computeSphere(positions, output) { bounds(positions); var minX = box.min[0]; var minY = box.min[1]; var maxX = box.max[0]; var maxY = box.max[1]; var width = maxX - minX; var height = maxY - minY; var length = Math.sqrt(width * width + height * height); output.center.set(minX + width / 2, minY + height / 2, 0); output.radius = length / 2; } var createLayout = require("layout-bmfont-text"); require("inherits"); require("quad-indices"); function createTextGeometry(opt) { return new TextGeometry2(opt); } class TextGeometry2 extends BufferGeometry_js.BufferGeometry { constructor(opt) { super(); if (typeof opt === "string") { opt = { text: opt }; } this._opt = Object.assign({ widthSegments: 1, heightSegments: 1 }, opt); if (opt) this.update(opt); } update(opt) { if (typeof opt === "string") { opt = { text: opt }; } opt = Object.assign({}, this._opt, opt); if (!opt.font) { throw new TypeError("must specify a { font } in options"); } this.layout = createLayout(opt); var flipY = opt.flipY !== false; var font = opt.font; var texWidth = font.common.scaleW; var texHeight = font.common.scaleH; var glyphs = this.layout.glyphs.filter(function(glyph2) { var bitmap2 = glyph2.data; return bitmap2.width * bitmap2.height > 0; }); let lastIndex = 0; let wordCount = -1; glyphs.forEach((glyph2, i) => { if (lastIndex != glyph2.index - 1) { wordCount++; } lastIndex = glyph2.index; glyph2.wordIndex = wordCount; glyph2.index = i; }); this.layout.glyphs = glyphs; this.layout._wordsTotal = wordCount; this.visibleGlyphs = glyphs; var attributes = vertices.getAttributes(this.layout, { texWidth, texHeight, flipY, ...opt }); let glyph = glyphs[0]; let bitmap = glyph.data; this.extendUniforms = new Uniform_js.Uniform( new Vector4_js.Vector4( bitmap.x / texWidth, 1 - (bitmap.y + bitmap.height) / texHeight, (bitmap.x + bitmap.width) / texWidth, 1 - bitmap.y / texHeight ) //minx, // minY, // maxX, //maxY ); var uvs = attributes.uvs; var positions = attributes.positions; var layoutUVs = attributes.layoutUVs; var indices2 = vertices.indices({ clockwise: true, widthSegments: opt.widthSegments, heightSegments: opt.heightSegments, type: "uint16", count: glyphs.length, ...opt }); this.setIndex(indices2); this.setAttribute("position", new BufferAttribute_js.BufferAttribute(positions, 2)); this.setAttribute("layoutUV", new BufferAttribute_js.BufferAttribute(layoutUVs, 3)); this.setAttribute("uv", new BufferAttribute_js.BufferAttribute(uvs, 2)); if (!opt.multipage && "page" in this.attributes) { this.removeAttribute("page"); } else if (opt.multipage) { var pages2 = vertices.pages(glyphs); this.setAttribute("page", new BufferAttribute_js.BufferAttribute(pages2, 1)); } } computeBoundingSphere() { if (this.boundingSphere === null) { this.boundingSphere = new Sphere_js.Sphere(); } var positions = this.attributes.position.array; var itemSize2 = this.attributes.position.itemSize; if (!positions || !itemSize2 || positions.length < 2) { this.boundingSphere.radius = 0; this.boundingSphere.center.set(0, 0, 0); return; } computeSphere(positions, this.boundingSphere); if (isNaN(this.boundingSphere.radius)) { console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.' ); } } computeBoundingBox() { if (this.boundingBox === null) { this.boundingBox = new Box3_js.Box3(); } var bbox = this.boundingBox; var positions = this.attributes.position.array; var itemSize2 = this.attributes.position.itemSize; if (!positions || !itemSize2 || positions.length < 2) { bbox.makeEmpty(); return; } computeBox(positions, bbox); } } exports.createTextGeometry = createTextGeometry;