A method for efficient display of large quantities of 3D text based on WebGL
Through HTML5 canvas and fast Euclidean distance calculation, SDF texture images are generated in real time. Combined with texture maps and shader material objects, it solves the problem of low performance of large-scale text rendering in WebGL technology and achieves efficient and clear text display and interactive experience. , adapt to the needs of various fonts.
Patent Information
- Application Number
- CN202110453472.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-04-26
- Publication Date
- 2024-09-27
- Estimated Expiration
- 2041-04-26
AI Technical Summary
When the existing technology uses WebGL technology to display CAD models and drawings, it faces problems such as low rendering performance and interactive lag caused by large batches of text, especially during zoom and move operations, and cannot effectively handle the display requirements of various fonts.
Use HTML5 canvas to perform fast Euclidean distance calculations, generate SDF texture images in real time, and draw text based on SDF information through texture mapping. Combined with shader material objects and geometry objects, an efficient three-dimensional text rendering method is constructed. System fonts are directly used to adapt to Display of different fonts.
It achieves the ability to efficiently draw large batches of text on the web page, maintains a clear scaling effect, reduces loading and memory overhead, adapts to the display needs of various fonts, and improves rendering performance and interactive experience.
Smart Images

Figure CN113177172B
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of computer graphics Web display, and in particular relates to a method for efficiently displaying large quantities of three-dimensional text based on WebGL. Background Art
[0002] With the development and maturity of HTML5 / WebGL technology, we have a new option for displaying CAD models and drawings on the Web and mobile terminals. WebGL combines OpenGL ES with JavaScript to provide hardware 3D accelerated rendering for HTML5 Canvas, and provides a browser-based plug-in-free 3D rendering cross-platform solution. It is increasingly widely used for lightweight display and interaction of 3D models and 2D drawings on the Web. The product manufacturing information (PMI) of 3D CAD models and 2D engineering drawings often contain a large amount of text information. How to efficiently render these large quantities of text with different fonts, styles, and sizes, and support scaling operations is a difficult point. WebGL itself does not provide an interface for direct text drawing, and cannot directly access system fonts. Currently, there are several ways to render text in 3D scenes on web pages:
[0003] (1) Use DOM API to add HTML elements to represent text, project 3D coordinate points to screen coordinates as the text position, and recalculate the screen coordinates of the text element each time the camera or coordinates change. This method is suitable for displaying static text of a small amount and fixed size. When the amount of text increases, the interactive performance will be very poor. It is not suitable for displaying large amounts of text in CAD models and drawings.
[0004] (2) Get the vector geometry representation of the text from the font file and create a 3D display grid for the text. In this way, the text directly becomes a geometric object in the 3D scene, which can well adapt to interactive operations such as zooming, panning, and rotating. However, each text contains many triangles. When there are many texts in the scene, the number of triangles will increase dramatically, thereby increasing the rendering burden and causing lag.
[0005] (3) Use text bitmap mapping to pre-render the character set onto one or more images, record the position of each character, represent each character in the scene with a rectangle, and paste the text image as a texture onto the text object. This method has high performance when drawing large quantities of text, but is only suitable for displaying text of a fixed size. Bitmap fonts are not suitable for scaling and will produce obvious jagged effects when enlarged.
[0006] (4) The use of vector information storage and rendering methods based on Signed Distance Field (SDF) can effectively solve the blur problem caused by bitmap font scaling. The SDF method replaces the color value stored in each pixel in the font bitmap with the shortest distance from the edge of the text. When the pixel is inside the text, a positive distance is used, and when it is outside the text, a negative distance is used. The distance to the edge of the text is zero. In this way, the edge of the character is described by the vector distance. In the shader, the edge is smoothed by smooth interpolation, which can easily solve the edge problem. The calculation of SDF is a time-consuming process. This method generally requires the generation of an SDF image for the character set in advance. If it is only English, it is not bad. The number of letters and characters is only a few dozen, which makes it easy to generate an SDF texture image in advance. However, there are more than 6,000 commonly used Chinese characters. The SDF image generated in advance will be very large, and different images must be generated for different fonts, which increases the loading overhead and memory overhead when displayed on the web page. Summary of the invention
[0007] In order to overcome the shortcomings of the prior art, the present invention provides a method for efficiently displaying large quantities of three-dimensional text based on WebGL, which solves the problem of obvious lag in interactive browsing such as displaying, moving, and zooming of models due to the presence of a large amount of text when viewing CAD models and drawings on a web page using WebGL technology.
[0008] The present invention is based on the idea of movable type printing. When loading a model, the present invention adopts HTML5 canvas rendering and a fast Euclidean distance calculation method to generate an SDF texture image containing characters used by the current model in real time, and draws text based on SDF information in a texture mapping manner. This method can not only efficiently draw a large number of texts and maintain clear text effects when zooming, but also HTML5 canvas can directly use system fonts without the need to load additional huge font files, thus meeting various font display requirements.
[0009] In order to achieve the above object, the technical solution adopted by the present invention is:
[0010] A method for efficiently displaying a large number of three-dimensional texts based on WebGL comprises the following steps:
[0011] (1) Traverse all text objects in the model and build a character hash table;
[0012] (2) Use Canvas to draw each character in the hash table, calculate the SDF information, generate the SDF texture image, and record the texture coordinate value of each character;
[0013] (3) Create a shader material object and implement SDF-based shading in the fragment shader, using the SDF image as the input texture;
[0014] (4) Construct a geometry object based on the geometric position, size, and color of the text. Each character is represented by a rectangle consisting of two triangles.
[0015] (5) Merge geometry objects with the same material to create a display mesh object and add it to the scene tree for drawing.
[0016] Preferably, step (1) is specifically:
[0017] (1-1) Divide the fonts into four types: normal, bold, italic, and italic bold, and each type corresponds to a hash table;
[0018] (1-2) Traverse and obtain each string object in the model and its corresponding font information, and find the hash table corresponding to this string;
[0019] (1-3) Traverse each character, use "font name + character" as the key value, and determine whether it already exists in the hash table. If not, add it to the hash table.
[0020] Preferably, step (2) is specifically:
[0021] (2-1) Calculate the size of the SDF image based on the number of characters in the hash table and create an image;
[0022] (2-2) Traverse each character:
[0023] (i) According to the hash table type and character font, set the canvas brush style and draw the characters to the canvas;
[0024] (ii) Obtain the character bitmap pixel value from the canvas and use the Euclidean distance transformation algorithm proposed by Felzenszwalb et al. to calculate the SDF value of each pixel. This algorithm can be performed in linear time.
[0025] (iii) Calculate the position of the current character in the SDF image, copy the SDF value of each pixel of the character to the SDF image, and save the SDF value only in the alpha component of the image;
[0026] (iv) Record the texture coordinate values of the four corner points of the character rectangle in the SDF image and store them in the hash table as the hash values corresponding to the characters;
[0027] (2-3) Each hash table generates an SDF image. A maximum of four SDF images are generated, corresponding to the four character tables of normal, bold, italic, and italic bold. If the size of a hash table is 0, no SDF image is generated.
[0028] Preferably, step (3) is specifically:
[0029] (3-1) Create a corresponding number of shader material objects according to the number of SDF images, and bind each material to the corresponding SDF image as the input texture object;
[0030] (3-2) In the fragment shader, the distance value of each pixel is obtained, and smoothing is performed at the edge through smooth interpolation to eliminate the jagged problem at the edge of the font.
[0031] Preferably, step (4) is specifically:
[0032] (4-1) The height of the character rectangle is determined according to the font size. For non-Chinese characters, the width-to-height ratio is calculated based on the pixel width of the character itself, thereby determining the width of the character rectangle. Chinese characters are monospaced characters, and the width and height of the character rectangle are equal.
[0033] (4-2) Calculate the position values of the four vertices of the character rectangle based on the position, width and height information of the text;
[0034] (4-3) Set the color value of the character rectangle vertex according to the color of the text;
[0035] (4-4) Get the texture coordinate value corresponding to the character from the character hash table and set the UV value of the character rectangle vertex;
[0036] (4-5) If double-sided rendering is set, you can see the same content from the back as from the front, so you need to construct a geometry object in the same position.
[0037] Preferably, step (5) is specifically:
[0038] (5-1) When creating text geometry objects, they are separated by material and placed in different arrays;
[0039] (5-2) Further merge the position, UV, and color element values of multiple geometry objects with the same material to generate a new geometry, and create a display mesh object to add to the scene.
[0040] The beneficial effects of the present invention are:
[0041] The present invention adopts HTML5 canvas rendering and a fast distance field calculation method in linear time to generate an SDF texture image containing characters used by the current model in real time, draws text based on SDF information in a texture mapping manner, and replaces color values in a bitmap with vector information represented by a directed distance field. The present invention has both the high efficiency of drawing text by a bitmap method and the anti-scaling property of a vector representation method. The system font can be directly used through canvas to adapt to the drawing of text in various fonts such as English and Chinese, and there is no need to prepare a large texture image containing all the text in the font in advance. A large number of texts with different fonts, styles and colors can be efficiently drawn in a three-dimensional scene of a web page, and a good scaling effect can be maintained. The present invention can not only efficiently draw a large number of texts and maintain a clear text effect when scaling, but also meet various font display requirements. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 It is an operation flow chart of the present invention. DETAILED DESCRIPTION
[0043] The present invention will be further described below in conjunction with specific embodiments, but the scope of the present invention is not limited thereto.
[0044] Reference Figure 1 A method for efficiently displaying a large number of three-dimensional texts based on WebGL comprises the following steps:
[0045] (1) Traverse all text objects in the model and build a character hash table:
[0046] (1-1) Divide the fonts into four types: normal, bold, italic, and italic bold, and each type corresponds to a hash table;
[0047] (1-2) Traverse and obtain each string object in the model and its corresponding font information, and find the hash table corresponding to this string;
[0048] (1-3) Traverse each character, use "font name + character" as the key value, and determine whether it already exists in the hash table. If not, add it to the hash table.
[0049] (2) Use Canvas to draw each character in the hash table, calculate the SDF information, generate the SDF texture image, and record the texture coordinate value of each character:
[0050] (2-1) Calculate the size of the SDF image based on the number of characters in the hash table and create an image;
[0051] (2-2) Traverse each character:
[0052] (i) According to the hash table type and character font, set the canvas brush style and draw the characters to the canvas;
[0053] (ii) Obtain the character bitmap pixel value from the canvas and use the Euclidean distance transformation algorithm proposed by Felzenszwalb et al. to calculate the SDF value of each pixel. This algorithm can be performed in linear time.
[0054] (iii) Calculate the position of the current character in the SDF image, copy the SDF value of each pixel of the character to the SDF image, and save the SDF value only in the alpha component of the image;
[0055] (iv) Record the texture coordinate values of the four corner points of the character rectangle in the SDF image and store them in the hash table as the hash values corresponding to the characters;
[0056] (2-3) Each hash table generates an SDF image. A maximum of four SDF images are generated, corresponding to the four character tables of normal, bold, italic, and italic bold. If the size of a hash table is 0, no SDF image is generated.
[0057] (3) Create a shader material object and implement SDF-based shading in the fragment shader, using the SDF image as the input texture:
[0058] (3-1) Create a corresponding number of shader material objects according to the number of SDF images, and bind each material to the corresponding SDF image as the input texture object;
[0059] (3-2) In the fragment shader, the distance value of each pixel is obtained, and smoothing is performed at the edge through smooth interpolation to eliminate the jagged problem at the edge of the font.
[0060] (4) Construct a geometry object based on the geometric position, size, and color of the text. Each character is represented by a rectangle consisting of two triangles:
[0061] (4-1) The height of the character rectangle is determined according to the font size. For non-Chinese characters, the width-to-height ratio is calculated based on the pixel width of the character itself, thereby determining the width of the character rectangle. Chinese characters are monospaced characters, and the width and height of the character rectangle are equal.
[0062] (4-2) Calculate the position values of the four vertices of the character rectangle based on the position, width and height information of the text;
[0063] (4-3) Set the color value of the character rectangle vertex according to the color of the text;
[0064] (4-4) Get the texture coordinate value corresponding to the character from the character hash table and set the UV value of the character rectangle vertex;
[0065] (4-5) If double-sided rendering is set, you can see the same content from the back as from the front, so you need to construct a geometry object in the same position.
[0066] (5) Merge geometry objects with the same material to create a display mesh object and add it to the scene tree for drawing:
[0067] (5-1) When creating text geometry objects, they are separated by material and placed in different arrays;
[0068] (5-2) Further merge the position, UV, and color element values of multiple geometry objects with the same material to generate a new geometry, and create a display mesh object to add to the scene to reduce rendering batches and improve rendering efficiency.
[0069] The present invention adopts HTML5 canvas rendering and a fast distance field calculation method of linear time to generate an SDF texture picture containing characters used by the current model in real time, and draws text based on SDF information in a texture mapping manner, and replaces the color value in the bitmap with the vector information represented by the directed distance field, and has both the efficiency of drawing text by the bitmap method and the anti-scaling property of the vector representation method. The system font can be directly used through canvas, and it is suitable for drawing various fonts such as English and Chinese, and it is no longer necessary to prepare a large texture picture containing all the text of the font in advance, and a large number of texts with different fonts, styles, and colors can be efficiently drawn in a three-dimensional scene of a web page, and a good scaling effect can be maintained. The present invention is measured on an i5 CPU, 8G memory, and a 2G independent display PC, and the display frame rate can reach 50FPS when displaying a large-scale dwg set model containing more than 10,000 words.
[0070] The present invention is described in detail above in conjunction with the embodiments, but the contents are only specific implementations of the present invention, and should not be understood as limiting the scope of the present invention. It should be pointed out that for ordinary technicians in this field, without departing from the concept of the present invention, all modifications and improvements made according to the scope of the present invention should still fall within the scope of the present invention.
Claims
1. A method for efficient display of large quantities of 3D text based on WebGL. Features The steps include: (1) Traverse all text objects in the model and build a character hash table; (2) Use Canvas to draw each character in the hash table, calculate the SDF information, generate the SDF image, and record the texture coordinate value of each character; (3) Create a shader material object and implement SDF-based shading in the fragment shader, using the SDF image as the input texture; (4) Construct a geometry object based on the geometric position, size, and color of the text. Each character is represented by a rectangle consisting of two triangles. (5) Merge geometry objects with the same material to create a display mesh object and add it to the scene tree for drawing; Among them, step (2) is specifically as follows: (2-1) Calculate the size of the SDF image based on the number of characters in the hash table and create an image; (2-2) Traverse each character: (i) Set the canvas brush style according to the hash table type and character font, and draw the characters to the canvas; (ii) Obtain the character bitmap pixel value from the canvas and use the Euclidean distance transformation algorithm proposed by Felzenszwalb et al. to calculate the SDF value of each pixel; (iii) Calculate the position of the current character in the SDF image, copy the SDF value of each pixel of the character to the SDF image, and save the SDF value only in the alpha component of the image; (iv) Record the texture coordinate values of the four corner points of the character rectangle in the SDF image and store them in the hash table as the hash values corresponding to the characters; (2-3) One SDF image is generated for each hash table. A maximum of four SDF images are generated, corresponding to the four character tables: normal, bold, italic, and italic bold. If the size of a hash table is 0, no SDF image is generated. Step (3) is as follows: (3-1) Create a corresponding number of shader material objects according to the number of SDF images, and bind each material to the corresponding SDF image as the input texture object; (3-2) Get the distance value for each pixel in the fragment shader, and smooth the edges through smooth interpolation to eliminate the jagged edges of the font.
2. According to the method for efficient display of large quantities of three-dimensional text based on WebGL according to claim 1, Features Step (1) is as follows: (1-1) Divide the fonts into four types: normal, bold, italic, and italic bold, each of which corresponds to a hash table; (1-2) Traverse and obtain each string object in the model and its corresponding font information, and find the hash table corresponding to this string; (1-3) Traverse each character, use "font name + character" as the key value, and determine whether it already exists in the hash table. If not, add it to the hash table.
3. According to the method for efficient display of large quantities of three-dimensional text based on WebGL according to claim 2, Features Step (4) is as follows: (4-1) The height of the character rectangle is determined according to the font size. For non-Chinese characters, the width-to-height ratio is calculated based on the pixel width of the character itself, thereby determining the width of the character rectangle. Chinese characters are monospaced characters, and the width and height of the character rectangle are equal; (4-2) Calculate the position values of the four vertices of the character rectangle based on the position, width and height information of the text; (4-3) Set the color value of the character rectangle vertex according to the color of the text; (4-4) Get the texture coordinate value corresponding to the character from the character hash table and set the UV value of the character rectangle vertex; (4-5) If double-sided rendering is set, you can see the same content from the back as from the front, so you need to construct a geometry object in the same position.
4. According to the method for efficient display of large quantities of three-dimensional text based on WebGL as claimed in claim 3, Features Step (5) is as follows: (5-1) When creating text geometry objects, they are separated by material and placed in different arrays; (5-2) Further merge the position, UV, and color element values of multiple geometry objects with the same material to generate a new geometry, and create a display mesh object to add to the scene, reducing rendering batches and improving rendering efficiency.
Citation Information
Patent Citations
Method and apparatus for rendering cell-based distance fields using texture mapping
US20040189655A1
System and method for executing non-graphical algorithms on a GPU (graphics processing unit)
US20200098161A1
Method and system for 3D scene change detection
WO2006027339A2
