2D platform fluid simulation method and device based on color channel
Through the 2D platform fluid simulation method based on color channels, the fluid world intervals are meshed and the view space matrix is constructed, which solves the problem of low fluid rendering efficiency in the prior art, and achieves efficient liquid flow simulation and refined rendering effects.
Patent Information
- Application Number
- CN202411313367.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-20
- Publication Date
- 2025-01-03
AI Technical Summary
The existing fluid rendering methods are low in drawing capabilities and efficiency when truly simulating liquid flow, and the small square collider simulation method consumes too much computer resources.
Using a 2D platform fluid simulation method based on color channels, a view space matrix is constructed by meshing the world interval of the fluid to be rendered, and a liquid state sampling diagram is constructed based on the mesh state. The ARGB channel is used to store liquid information, reduce the number of samples, and improve the Draw efficiency and fine rendering for different flow states.
In the case of truly simulating liquid flow, the drawing efficiency of the fluid is greatly improved, the fidelity of the rendering effect is improved, and the consumption of computer resources is reduced.
Smart Images

Figure CN119251366A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of fluid simulation, and in particular to a 2D platform fluid simulation method and device based on a color channel. Background Art
[0002] In the field of modern graphics processing and physical simulation, fluid rendering and simulation is a complex and critical task. Especially in application scenarios such as computer games, virtual reality, animation production, and engineering simulation, the realism and computational efficiency of fluid effects have a significant impact on user experience and system performance.
[0003] At present, there are two common methods for rendering fluids: drawing with limited fluidity, rendering pseudo-fluids through texture colors, and simulating water flow using enough small square collision bodies. For the first method, since the fluid effect is generated by using pre-set textures or simple mathematical functions, there is a disadvantage that the liquid cannot flow realistically; for the second method, although the authenticity of the simulated water flow can be improved by continuously increasing the number of small square collision bodies, when the number of small square collision bodies is large, the consumption of computer system resources is too large.
[0004] Therefore, there is an urgent need for a 2D platform fluid simulation method and device based on color channels to improve the fluid drawing ability and drawing efficiency while realistically simulating liquid flow. Summary of the invention
[0005] The present application provides a 2D platform fluid simulation method and device based on a color channel, which solves the problem of low fluid drawing ability and drawing efficiency in the case of real simulation of liquid flow.
[0006] In a first aspect of the present application, a 2D platform fluid simulation method based on a color channel is provided, the method comprising: obtaining a grid size and a view space matrix size corresponding to a fluid to be rendered, the grid size and the view space matrix size being determined according to a spatial region where the fluid to be rendered is located; constructing a view space matrix corresponding to the fluid to be rendered according to the grid size and the view space matrix size; traversing the view space matrix in a preset order to obtain a grid state corresponding to each grid in the view space matrix; constructing a liquid state sampling diagram corresponding to the fluid to be rendered according to the view space matrix and the grid state corresponding to each grid, the liquid state sampling diagram storing liquid information corresponding to each grid through an ARGB channel, wherein the A channel is used to store the liquid's own water volume information, the R channel stores the left water volume information in the liquid information, the G channel stores the right water volume information in the liquid information, and the B channel stores the bottom water volume information in the liquid information; obtaining a drawing state corresponding to the fluid to be rendered according to the liquid state sampling diagram and the ARGB channels corresponding to each grid; confirming a drawing mode corresponding to the fluid to be rendered according to the drawing state, and performing a drawing operation on the fluid to be rendered according to the drawing mode.
[0007] Optionally, before constructing the view space matrix corresponding to the fluid to be rendered according to the grid size and the view space matrix size, the method further includes: determining whether there is remaining space in the view space matrix according to the following formula:
[0008]
[0009] Where i is the number of rows in the view space matrix, j is the number of columns in the view space matrix, C is the grid size, G W is the height of the view space matrix, G h is the width of the view space matrix; if i is not an integer calculated according to the above formula, it is confirmed that there is remaining space in the view space matrix, and the number of rows of the view space matrix is increased according to the calculation result; if j is not an integer calculated according to the above formula, it is confirmed that there is remaining space in the view space matrix, and the number of columns of the view space matrix is increased according to the calculation result.
[0010] Optionally, the grid state includes a solid state and a flow state. After traversing the view space matrix in a preset order to obtain the grid state corresponding to each grid in the view space matrix, the method further includes: if it is confirmed that the grid state is a flow state, judging whether the flow state satisfies a first judgment condition; if it is confirmed that the flow state satisfies the first judgment condition, the specific flow state corresponding to the flow state is a liquid downward flow state, and the first judgment condition is: the self water volume information corresponding to the target grid is greater than the bottom water volume information corresponding to the target grid, and the target grid is any grid corresponding to the flow state in the view space matrix; if it is confirmed that the flow state satisfies the second judgment condition, the specific flow state corresponding to the flow state is a liquid left-right flow state, and the second judgment condition is that the flow state corresponding to the grid below the target grid is a solid state, or the bottom water volume information corresponding to the target grid is a maximum value of 255; and the left water volume information corresponding to the target grid is less than the self water volume information corresponding to the target grid, or the right water volume information corresponding to the target grid is less than the self water volume information corresponding to the target grid; if the first judgment condition is not satisfied, and the second judgment condition is not satisfied, then it is confirmed that the specific flow state corresponding to the flow state is a liquid static state.
[0011] Optionally, according to the view space matrix and the grid state corresponding to each grid, a liquid state sampling map corresponding to the fluid to be rendered is constructed, specifically including:
[0012] The liquid information corresponding to each grid is constructed according to the following formula:
[0013] S i,j =Vector4(Cr,Cg,Cb,Ca);
[0014] In the above formula
[0015] Among them, S i,j is the unit pixel corresponding to the grid in row i and column j in the liquid state sampling diagram. Vector4(Cr, Cg, Cb, Ca) is the ARGB channel corresponding to the unit pixel. Ca is the A channel, Cr is the R channel, Cg is the G channel, Cb is the B channel, and L is the i,j is the water volume information corresponding to the unit pixel, L i,j-1 is the water volume information on the left side corresponding to the unit pixel, L i,j+1 is the water volume information on the right side corresponding to the unit pixel, L i+1,j is the bottom water volume information corresponding to the unit pixel, L max The maximum value of liquid information corresponding to each grid.
[0016] Optionally, according to the liquid state sampling diagram and the ARGB channels corresponding to each grid, the drawing state corresponding to the fluid to be rendered is obtained, specifically including: obtaining the channel values corresponding to each grid by the following formula:
[0017]
[0018] Among them, L, L1, L r , L b The channel values corresponding to each grid are respectively; according to the channel values corresponding to each grid, the different drawing state values corresponding to each grid are determined according to the following formula:
[0019]
[0020] Among them, empty, flowting, idle, and wawe are different drawing state values corresponding to each grid; according to the drawing state value corresponding to each grid, the drawing state corresponding to each grid is confirmed to confirm the drawing state corresponding to the fluid to be rendered, wherein, when the drawing state value is empty, the drawing state corresponding to the single grid is an empty state; when the drawing state value is flowing, the drawing state corresponding to the single grid is a suspended state; when the drawing state value is idle, the drawing state corresponding to the single grid is a maximum value state; when the drawing state value is wawe, the drawing state corresponding to the single grid is a plane flow state.
[0021] Optionally, according to the drawing state, the drawing method corresponding to the fluid to be rendered is confirmed, specifically including: according to the drawing state corresponding to each grid, the drawing method corresponding to each grid is confirmed, so as to confirm the drawing method corresponding to the fluid to be rendered, wherein, when the drawing state is an empty state, the drawing method corresponding to a single grid is to stop drawing; when the drawing state is a suspended state, the drawing method corresponding to a single grid is to draw a square liquid texture corresponding to the size of a single grid; when the drawing state is a maximum value state, the drawing method corresponding to a single grid is to draw a picture of falling fluid particles; when the drawing state is a plane flow state, the drawing method corresponding to a single grid is to use double wave interpolation to draw the liquid surface flow effect.
[0022] Optionally, using double wave interpolation to draw the flow effect of the liquid surface specifically includes: using double wave interpolation to draw the flow effect of the liquid surface according to the following formula:
[0023]
[0024] Among them, interceptor1 and interceptor2 are floating point numbers, UV is a two-dimensional coordinate vector, where UV.x is a horizontal coordinate vector, Time.x is the time variable moving forward, LineH is the baseline height of the liquid surface, LineH is calculated by L and Lr, and flowLine is the flow effect on the liquid surface.
[0025] In a second aspect of the present application, a 2D platform fluid simulation device based on a color channel is provided, the device comprising a view space matrix construction module, a liquid state sampling map construction module and a fluid rendering drawing module, wherein:
[0026] A view space matrix construction module is used to obtain a grid size and a view space matrix size corresponding to the fluid to be rendered, wherein the grid size and the view space matrix size are determined according to the spatial area where the fluid to be rendered is located; and construct a view space matrix corresponding to the fluid to be rendered according to the grid size and the view space matrix size.
[0027] A liquid state sampling map construction module is used to traverse the view space matrix in a preset order to obtain the grid state corresponding to each grid in the view space matrix; according to the view space matrix and the grid state corresponding to each grid, a liquid state sampling map corresponding to the fluid to be rendered is constructed, and the liquid state sampling map stores the liquid information corresponding to each grid through ARGB channels, wherein the A channel is used to store the liquid's own water volume information, the R channel stores the left water volume information in the liquid information, the G channel stores the right water volume information in the liquid information, and the B channel stores the bottom water volume information in the liquid information.
[0028] The fluid rendering and drawing module is used to obtain the drawing state corresponding to the fluid to be rendered according to the liquid state sampling map and the ARGB channels corresponding to each of the grids; according to the drawing state, confirm the drawing method corresponding to the fluid to be rendered, and perform a drawing operation on the fluid to be rendered according to the drawing method.
[0029] In the third aspect of the present application, an electronic device is provided, including a processor, a memory, a user interface and a network interface, the memory is used to store instructions, the user interface and the network interface are used to communicate with other devices, and the processor is used to execute the instructions stored in the memory so that the electronic device executes any one of the methods described above.
[0030] In a fourth aspect of the present application, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program, and the computer program is executed by a processor to perform any of the above methods.
[0031] One or more technical solutions provided in the embodiments of the present application have at least the following technical effects or advantages:
[0032] 1. The present application grids the world interval where the fluid to be rendered is located, constructs a view space matrix corresponding to the fluid to be rendered, and traverses the view space matrix in a preset order to obtain the grid state corresponding to each grid in the view space matrix, thereby constructing a liquid state sampling map corresponding to the fluid to be rendered according to the view space matrix and the grid state corresponding to each grid. The liquid state sampling map stores the liquid information corresponding to each grid through the ARGB channel, and then only one sampling can be performed to determine the drawing state according to the ARGB channel corresponding to each grid, confirm the drawing method corresponding to the fluid to be rendered, and perform drawing operations on the fluid to be rendered according to the drawing method, thereby reducing the number of sampling times. Under the condition of real simulation of liquid flow, the drawing efficiency of the fluid to be rendered is greatly improved.
[0033] 2. By obtaining the flow state corresponding to each grid, including the downward flow state of the liquid, the left and right flow state of the liquid, and the static state, refined rendering processing can be performed for different flow states, so that the fluid simulation can more accurately reflect the flow characteristics of the liquid in the real world, making the final simulation effect more realistic.
[0034] 3. When the drawing state is a plane flow state, the liquid surface flow effect is drawn by using double wave interpolation, making the fluid surface to be rendered look more real and dynamic. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] Figure 1 It is a flow chart of a 2D platform fluid simulation method based on a color channel provided in an embodiment of the present application;
[0036] Figure 2 is a schematic diagram of a grid fluid flow simulation provided in an embodiment of the present application;
[0037] Figure 3 It is a module schematic diagram of a 2D platform fluid simulation device based on a color channel provided in an embodiment of the present application;
[0038] Figure 4 It is a structural schematic diagram of an electronic device provided in an embodiment of the present application.
[0039] Explanation of the reference numerals: 21, A grid; 22, B grid; 23, C grid; 24, D grid; 25, E grid; 31, view space matrix construction module; 32, liquid state sampling map construction module; 33, fluid rendering and drawing module; 401, processor; 402, communication bus; 403, user interface; 404, network interface; 405, memory. DETAILED DESCRIPTION
[0040] In order to enable technicians in this field to better understand the technical solutions in this specification, the technical solutions in the embodiments of this specification will be clearly and completely described below in conjunction with the drawings in the embodiments of this specification. Obviously, the described embodiments are only part of the embodiments of this application, not all of the embodiments.
[0041] The terms used in the following embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to be used as limitations to the present application. As used in the specification of the present application, the singular expressions "one", "a kind of", "said", "above", "the" and "this" are intended to also include plural expressions, unless there is a clear indication to the contrary in the context. It should also be understood that the term "and / or" used in the present application refers to and includes any or all possible combinations of one or more listed items.
[0042] In the following, the terms "first" and "second" are used for descriptive purposes only and are not to be understood as suggesting or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features, and in the description of the embodiments of the present application, unless otherwise specified, "plurality" means two or more.
[0043] In order to enable those skilled in the art to better understand the technical solution of the present invention, the present invention will be further described in detail below with reference to the accompanying drawings.
[0044] At present, there are two common methods for rendering fluids: drawing with limited fluidity, rendering pseudo-fluids through texture colors, and simulating water flow using enough small square collision bodies. For the first method, since the fluid effect is generated by using pre-set textures or simple mathematical functions, there is a disadvantage that the liquid cannot flow realistically; for the second method, although the authenticity of the simulated water flow can be improved by continuously increasing the number of small square collision bodies, when the number of small square collision bodies is large, the consumption of computer system resources is too large.
[0045] Therefore, there is an urgent need for a 2D platform fluid simulation method and device based on color channels.
[0046] Please refer to Figure 1, which shows a flow chart of a 2D platform fluid simulation method based on a color channel provided in an embodiment of the present application. The method is applied to a server. The flow chart mainly includes the following steps: S101 to S106.
[0047] Step S101 , obtaining a grid size and a view space matrix size corresponding to the fluid to be rendered, wherein the grid size and the view space matrix size are determined according to a spatial region where the fluid to be rendered is located.
[0048] Specifically, the view space matrix size of the fluid to be rendered in the entire three-dimensional space is determined, that is, the "world interval". The world interval defines the physical boundary and spatial range of the fluid to be rendered, and the world interval where the fluid to be rendered is located is gridded by controlling the CPU for preprocessing. Gridding is to discretize the continuous world interval into a grid of a certain size, that is, the grid size corresponding to the fluid to be rendered. The grid size determines the rendering accuracy and the delicacy of the fluid simulation. This application does not limit the division of the grid.
[0049] Step S102: constructing a view space matrix corresponding to the fluid to be rendered according to the grid size and the view space matrix size.
[0050] Specifically, according to the predetermined grid size and view space matrix size corresponding to the fluid to be rendered, the view space matrix corresponding to the fluid to be rendered is constructed. In this application, for the sake of convenience, the grid size is C and the view space matrix size is G. W , G h , the view space matrix is R(m,n) for illustration, where G W is the height of the view space matrix, G h is the width of the view space matrix, where m and n are the number of rows and columns of the view space matrix R, respectively.
[0051] In a possible implementation, step S102 further includes: determining whether there is remaining space in the view space matrix according to the following formula:
[0052]
[0053] Where i is the number of rows in the view space matrix, j is the number of columns in the view space matrix, C is the grid size, G W is the height of the view space matrix, G h is the width of the view space matrix; if i is not an integer calculated according to the above formula, it is confirmed that there is remaining space in the view space matrix, and the number of rows of the view space matrix is increased according to the calculation result; if j is not an integer calculated according to the above formula, it is confirmed that there is remaining space in the view space matrix, and the number of columns of the view space matrix is increased according to the calculation result.
[0054] Specifically, when constructing the view space matrix, it is also necessary to check whether there is any remaining space in the view space matrix. If so, one more row / column needs to be added to cover the remaining space. The specific steps are as follows: Determine whether there is any remaining space in the view space matrix according to the following formula:
[0055]
[0056] i represents the number of grids in the vertical direction of the view space matrix, that is, the number of rows; j represents the number of grids in the horizontal direction of the view space matrix, that is, the number of columns, G h is the height of the view space matrix, C is the grid size, and the height of the view space matrix G h Divide by the grid size C to get the number of rows that can be completely covered in the vertical direction. If G h If it cannot be divided by C (i.e. there is a remainder), you need to add a row to cover the remaining space; set the width of the view space matrix G W Divide by the grid size C to get the number of columns that can be completely covered in the horizontal direction. If G W If it is not divisible by C (i.e. there is a remainder), you need to add a column to cover the remaining space.
[0057] Step S103, traversing the view space matrix in a preset order to obtain a grid state corresponding to each grid in the view space matrix.
[0058] Specifically, the view space matrix is traversed in a preset order, wherein, in order to traverse each grid in the view space matrix, the preset order may be to traverse each row from left to right, and then move down to the next row in turn for traversal, so as to divide the grid state corresponding to each grid in the view space matrix, and each grid may be a solid state or a flow state after division, wherein the solid state is a grid that is inaccessible to the fluid to be rendered, such as a grid where a wall, an obstacle, etc. are located, and the flow state is a grid that is accessible to the fluid to be rendered, and the matrix form corresponding to the classified view space matrix is as follows:
[0059]
[0060] Among them, S is the grid corresponding to the entity state, L i,j is the grid corresponding to the flow state.
[0061] In a possible implementation, step S103 further includes: if it is confirmed that the grid state is a flow state, determining whether the flow state satisfies a first determination condition; if it is confirmed that the flow state satisfies the first determination condition, the specific flow state corresponding to the flow state is a liquid downward flow state, and the first determination condition is: the self-water volume information corresponding to the target grid is greater than the bottom-layer water volume information corresponding to the target grid, and the target grid is any grid in the view space matrix; if it is confirmed that the flow state satisfies a second determination condition, the specific flow state corresponding to the flow state is a liquid left-right flow state, and the second determination condition is that the flow state corresponding to the grid below the target grid is a solid state, or the bottom-layer water volume information corresponding to the target grid is a maximum value of 255; and the left-side water volume information corresponding to the target grid is less than the self-water volume information corresponding to the target grid, or the right-side water volume information corresponding to the target grid is less than the self-water volume information corresponding to the target grid; if the first determination condition is not satisfied, and the second determination condition is not satisfied, then it is confirmed that the specific flow state corresponding to the flow state is a liquid static state.
[0062] Specifically, after dividing the grid state, it is necessary to divide the grids corresponding to the flow state more finely, that is, to determine whether the grid corresponding to each flow state is any one of the downward flow state, the left-right flow state or the static state. The specific determination steps are as follows: first, confirm whether the target grid meets the first determination condition. The target grid here can be any grid in the flow state grid. Determine whether the above target grid meets the first determination condition R i+1,j ! =S&L i,j >L i+1,j , if satisfied, then the specific flow state corresponding to the flow state is the liquid flowing downward. At this time, the fluid in the target grid has a tendency to flow downward. Among them, S is the entity state. If R i,j is the flow state corresponding to the grid in row i and column j in the view space matrix, then R i+1,j is the flow state corresponding to the grid below the grid in row i and column j, R i+1,j =S means that the flow state corresponding to the lower grid is not a solid state, L i,j is the water volume information corresponding to the grid in row i and column j, L i+1,j is the bottom water volume information corresponding to the grid in row i and column j, L i,j >L i+1,j The water volume information corresponding to the grid in row i and column j is greater than the bottom water volume information corresponding to the grid in row i and column j; if the first judgment condition is not met, then it is judged whether the target grid meets the second judgment condition R i+1,j ==S|L i+1,j =255)&&(L i,j+1 <L i,j |L i,j-1 <L i,j), if it is satisfied, then the specific flow state corresponding to the flow state is the left-right flow state of the liquid. At this time, the fluid in the target grid has a tendency to flow left / right, where R i+1,j ==S|L i+1,j = 255 means that the flow state corresponding to the grid below is solid state, or the bottom water volume information corresponding to the grid in row i and column j is the maximum value 255. i,j+1 is the water volume information on the right side corresponding to the grid in row i and column j, L i,j-1 is the water volume information on the left side of the grid corresponding to row i and column j, L i,j+1 <L i,j |L i,j-1 <L i,j It means that the left water volume information corresponding to the grid of row i and column j is less than the self water volume information corresponding to the grid of row i and column j, or the right water volume information corresponding to the grid of row i and column j is less than the self water volume information corresponding to the grid of row i and column j. If the target grid satisfies neither the first judgment condition nor the second judgment condition, then the specific flow state corresponding to the flow state is confirmed to be the liquid static state. At this time, the fluid in the target grid will not move in any direction and remain relatively static. This embodiment does not limit the execution order of the first judgment condition and the second judgment condition.
[0063] Please refer to Figure 2 , which shows a grid fluid flow simulation schematic diagram provided by an embodiment of the present application, wherein the black solid grid is a solid state grid, and the other grids are fluid state grids. A grid 21, B grid 22, and C grid 23 are liquid left-right flow state grids, D grid 24 is a liquid downward flow state grid, and E grid 25 is a liquid static state grid. The fluid in grids A grid 21 and B grid 22 will flow to the right while flowing to the left, and the fluid in grid C 23 will only flow to the right.
[0064] Step S104, constructing a liquid state sampling map corresponding to the fluid to be rendered according to the view space matrix and the grid state corresponding to each grid, and the liquid state sampling map stores the liquid information corresponding to each grid through the ARGB channel.
[0065] Specifically, according to the view space matrix and the grid state corresponding to each grid, the CPU performs flow simulation and constructs a liquid state sampling map corresponding to the fluid to be rendered. The liquid state sampling map stores the liquid information corresponding to each grid through the ARGB channels, where the A channel is used to store the liquid's own water volume information, the R channel stores the left water volume information in the liquid information, the G channel stores the right water volume information in the liquid information, and the B channel stores the bottom water volume information in the liquid information. Afterwards, the liquid state sampling map is passed to the GPU for subsequent processing by the GPU.
[0066] In a possible implementation, step S104 further includes: constructing liquid information corresponding to each grid according to the following formula:
[0067] S i,j =Vector4(Cr,Cg,Cb,Ca);
[0068] In the above formula
[0069] Among them, S i,j is the unit pixel corresponding to the grid in row i and column j in the liquid state sampling diagram. Vector4(Cr, Cg, Cb, Ca) is the ARGB channel corresponding to the unit pixel. Ca is the A channel, Cr is the R channel, Cg is the G channel, Cb is the B channel, and L is the i,j is the water volume information corresponding to the unit pixel, L i,j-1 is the water volume information on the left side corresponding to the unit pixel, L i,j+1 is the water volume information on the right side corresponding to the unit pixel, L i+1,j is the bottom water volume information corresponding to the unit pixel, L max The maximum value of liquid information corresponding to each grid.
[0070] Specifically, the liquid information corresponding to each grid is calculated by the above formula, thereby calculating the corresponding to each grid.
[0071] Step S105 , obtaining the drawing state corresponding to the fluid to be rendered according to the liquid state sampling diagram and the ARGB channels corresponding to each grid.
[0072] Specifically, the GPU obtains the generated liquid state sampling map, calculates the ARGB channels corresponding to each grid in the liquid state sampling map, and obtains the channel values corresponding to each grid through the following formula:
[0073]
[0074] Among them, L, L1, L r , L b The channel values corresponding to each grid are respectively; according to the channel values corresponding to each grid, the different drawing state values corresponding to each grid are determined according to the following formula:
[0075]
[0076] Among them, empty, flowting, idle, and wawe are different drawing state values corresponding to each grid; according to the drawing state value corresponding to each grid, the drawing state corresponding to each grid is confirmed to confirm the drawing state corresponding to the fluid to be rendered, wherein, when the drawing state value is empty, the drawing state corresponding to the single grid is an empty state; when the drawing state value is flowing, the drawing state corresponding to the single grid is a suspended state; when the drawing state value is idle, the drawing state corresponding to the single grid is a maximum value state; when the drawing state value is wawe, the drawing state corresponding to the single grid is a plane flow state.
[0077] Step S106: confirming the drawing mode corresponding to the fluid to be rendered according to the drawing state, and performing a drawing operation on the fluid to be rendered according to the drawing mode.
[0078] Specifically, the GPU confirms the drawing method corresponding to each grid through the drawing state corresponding to each grid, thereby forming the drawing method corresponding to the overall fluid to be rendered, and then performs drawing operations on the fluid to be rendered according to the drawing method. The specific operation steps are as follows: according to the drawing state corresponding to each grid, confirm the drawing method corresponding to each grid to confirm the drawing method corresponding to the fluid to be rendered. Among them, when the drawing state is empty, the drawing method corresponding to a single grid is to stop drawing; when the drawing state is suspended, the drawing method corresponding to a single grid is to draw a square liquid texture corresponding to the size of a single grid; when the drawing state is the maximum value state, the drawing method corresponding to a single grid is to draw a picture of falling fluid particles; when the drawing state is a plane flow state, the drawing method corresponding to a single grid is to use double wave interpolation to draw the liquid surface flow effect. Among them, for using double wave interpolation to draw the liquid surface flow effect, drawing is performed according to the following formula:
[0079]
[0080] Among them, interceptor1 and interceptor2 are floating point numbers, UV is a two-dimensional coordinate vector, where UV.x is a horizontal coordinate vector, Time.x is a time variable that moves forward, and the value of Time.x changes with time, so that the fluctuation effect in the formula changes continuously with time, thereby simulating the dynamic flow of the fluid, LineH is the baseline height of the liquid surface, LineH is calculated by L and Lr, flowLine is the flow effect on the liquid surface, interceptor1 and interceptor2 produce different fluctuation effects, and flowLine is the fluctuation effect superimposed on the baseline height LineH of the liquid surface, making the fluid surface to be rendered look more real and dynamic.
[0081] The present application grids the world interval where the fluid to be rendered is located by adopting the above method, and constructs a view space matrix corresponding to the fluid to be rendered, and traverses the view space matrix in a preset order to obtain the grid state corresponding to each grid in the view space matrix, so as to construct a liquid state sampling map corresponding to the fluid to be rendered according to the view space matrix and the grid state corresponding to each grid, and the liquid state sampling map stores the liquid information corresponding to each grid through the ARGB channel, wherein the A channel is used to store the liquid information itself in the liquid information, the R channel stores the left water volume information in the liquid information, the G channel stores the right water volume information in the liquid information, and the B channel stores the bottom water volume information in the liquid information, and then only one sampling can be performed to determine the drawing state according to the ARGB channel corresponding to each grid, confirm the drawing method corresponding to the fluid to be rendered, and perform drawing operations on the fluid to be rendered according to the drawing method, thereby reducing the number of sampling times. In the case of real simulation of liquid flow, the drawing efficiency of the fluid to be rendered is greatly improved.
[0082] Please refer to Figure 3 , which shows a module schematic diagram of a 2D platform fluid simulation device based on a color channel provided in an embodiment of the present application. The device is a server, and the server includes a view space matrix construction module 31, a liquid state sampling map construction module 32, and a fluid rendering and drawing module 33, wherein,
[0083] The view space matrix construction module 31 is used to obtain the grid size and view space matrix size corresponding to the fluid to be rendered, wherein the grid size and the view space matrix size are determined according to the spatial area where the fluid to be rendered is located; and construct the view space matrix corresponding to the fluid to be rendered according to the grid size and the view space matrix size.
[0084] The liquid state sampling map construction module 32 is used to traverse the view space matrix in a preset order to obtain the grid state corresponding to each grid in the view space matrix; according to the view space matrix and the grid state corresponding to each grid, construct the liquid state sampling map corresponding to the fluid to be rendered, and the liquid state sampling map stores the liquid information corresponding to each grid through the ARGB channels, wherein the A channel is used to store the liquid water volume information itself in the liquid information, the R channel stores the left water volume information in the liquid information, the G channel stores the right water volume information in the liquid information, and the B channel stores the bottom water volume information in the liquid information.
[0085] The fluid rendering and drawing module 33 is used to obtain the drawing state corresponding to the fluid to be rendered according to the liquid state sampling map and the ARGB channels corresponding to each of the grids; according to the drawing state, confirm the drawing method corresponding to the fluid to be rendered, and perform a drawing operation on the fluid to be rendered according to the drawing method.
[0086] In a possible implementation, the view space matrix construction module 31 is used to determine whether there is remaining space in the view space matrix according to the following formula before constructing the view space matrix corresponding to the fluid to be rendered according to the grid size and the view space matrix size:
[0087]
[0088] Where i is the number of rows in the view space matrix, j is the number of columns in the view space matrix, C is the grid size, G W is the height of the view space matrix, G h is the width of the view space matrix; if i is not an integer calculated according to the above formula, it is confirmed that there is remaining space in the view space matrix, and the number of rows of the view space matrix is increased according to the calculation result; if j is not an integer calculated according to the above formula, it is confirmed that there is remaining space in the view space matrix, and the number of columns of the view space matrix is increased according to the calculation result.
[0089] In a possible implementation, the grid state includes a solid state and a flow state. The liquid state sampling diagram construction module 32 is used to traverse the view space matrix in a preset order to obtain the grid state corresponding to each grid in the view space matrix. The method further includes: if it is confirmed that the grid state is a flow state, judging whether the flow state satisfies a first judgment condition; if it is confirmed that the flow state satisfies the first judgment condition, then the specific flow state corresponding to the flow state is a liquid downward flow state, and the first judgment condition is: the self water volume information corresponding to the target grid is greater than the bottom water volume information corresponding to the target grid, and the target grid is any grid corresponding to the flow state in the view space matrix; if it is confirmed that the flow state satisfies the second judgment condition, then the specific flow state corresponding to the flow state is a liquid left-right flow state, and the second judgment condition is that the flow state corresponding to the grid below the target grid is a solid state, or the bottom water volume information corresponding to the target grid is a maximum value of 255; and the left water volume information corresponding to the target grid is less than the self water volume information corresponding to the target grid, or the right water volume information corresponding to the target grid is less than the self water volume information corresponding to the target grid; if the first judgment condition is not satisfied, and the second judgment condition is not satisfied, then it is confirmed that the specific flow state corresponding to the flow state is a liquid static state.
[0090] In a possible implementation, the liquid state sampling map construction module 32 is used to construct a liquid state sampling map corresponding to the fluid to be rendered according to the view space matrix and the grid state corresponding to each grid, specifically including:
[0091] The liquid information corresponding to each grid is constructed according to the following formula:
[0092] S i,j =Vector4(Cr,Cg,Cb,Ca);
[0093] In the above formula
[0094] Among them, S i,j is the unit pixel corresponding to the grid in row i and column j in the liquid state sampling diagram. Vector4(Cr, Cg, Cb, Ca) is the ARGB channel corresponding to the unit pixel. Ca is the A channel, Cr is the R channel, Cg is the G channel, Cb is the B channel, and L is the i,j is the water volume information corresponding to the unit pixel, L i,j-1 is the water volume information on the left side corresponding to the unit pixel, L i,j+1 is the water volume information on the right side corresponding to the unit pixel, L i+1,j is the bottom water volume information corresponding to the unit pixel, L max The maximum value of liquid information corresponding to each grid.
[0095] In a possible implementation, the fluid rendering module 33 is used to obtain the rendering state corresponding to the fluid to be rendered according to the liquid state sampling diagram and the ARGB channels corresponding to each grid, specifically including: obtaining the channel values corresponding to each grid by the following formula:
[0096]
[0097] Among them, L, L1, L r , L b The channel values corresponding to each grid are respectively; according to the channel values corresponding to each grid, the different drawing state values corresponding to each grid are determined according to the following formula:
[0098]
[0099] Among them, empty, flowting, idle, and wawe are different drawing state values corresponding to each grid; according to the drawing state value corresponding to each grid, the drawing state corresponding to each grid is confirmed to confirm the drawing state corresponding to the fluid to be rendered, wherein, when the drawing state value is empty, the drawing state corresponding to the single grid is an empty state; when the drawing state value is flowing, the drawing state corresponding to the single grid is a suspended state; when the drawing state value is idle, the drawing state corresponding to the single grid is a maximum value state; when the drawing state value is wawe, the drawing state corresponding to the single grid is a plane flow state.
[0100] In a possible implementation, the fluid rendering and drawing module 33 is used to confirm the drawing method corresponding to the fluid to be rendered according to the drawing state, specifically including: confirming the drawing method corresponding to each grid according to the drawing state corresponding to each grid, so as to confirm the drawing method corresponding to the fluid to be rendered, wherein, when the drawing state is an empty state, the drawing method corresponding to a single grid is to stop drawing; when the drawing state is a suspended state, the drawing method corresponding to a single grid is to draw a square liquid texture corresponding to the size of a single grid; when the drawing state is a maximum value state, the drawing method corresponding to a single grid is to draw a picture of falling fluid particles; when the drawing state is a plane flow state, the drawing method corresponding to a single grid is to use double wave interpolation to draw the liquid surface flow effect.
[0101] In a possible implementation, the fluid rendering module 33 is used to use double wave interpolation to render the liquid surface flow effect, specifically including: using double wave interpolation to render the liquid surface flow effect according to the following formula:
[0102]
[0103] Among them, interceptor1 and interceptor2 are floating point numbers, UV is a two-dimensional coordinate vector, where UV.x is a horizontal coordinate vector, Time.x is the time variable moving forward, LineH is the baseline height of the liquid surface, LineH is calculated by L and Lr, and flowLine is the flow effect on the liquid surface.
[0104] It should be noted that: when the device provided in the above embodiment realizes its function, only the division of the above functional modules is used as an example. In actual application, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the device and method embodiments provided in the above embodiment belong to the same concept, and the specific implementation process is detailed in the method embodiment, which will not be repeated here.
[0105] The present application also provides an electronic device. Figure 4 , Figure 4 4 is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. The electronic device may include: at least one processor 401, at least one communication bus 402, a user interface 403, at least one network interface 404, and a memory 405.
[0106] The communication bus 402 is used to realize the connection and communication between these components.
[0107] The user interface 403 may include a display screen (Display) and a camera (Camera), and the optional user interface 403 may also include a standard wired interface and a wireless interface.
[0108] The network interface 404 may optionally include a standard wired interface or a wireless interface (such as a WI-FI interface).
[0109] Among them, the processor 401 may include one or more processing cores. The processor 401 uses various interfaces and lines to connect various parts in the entire server, and executes various functions of the server and processes data by running or executing instructions, programs, code sets or instruction sets stored in the memory 405, and calling data stored in the memory 405. Optionally, the processor 401 can be implemented in at least one hardware form of digital signal processing (Digital Signal Processing, DSP), field programmable gate array (Field-Programmable Gate Array, FPGA), and programmable logic array (Programmable Logic Array, PLA). The processor 401 can integrate one or more combinations of a central processing unit (Central Processing Unit, CPU), a graphics processing unit (Graphics Processing Unit, GPU) and a modem. Among them, the CPU mainly processes the operating system, user interface and application programs; the GPU is responsible for rendering and drawing the content to be displayed on the display screen; the modem is used to process wireless communications. It can be understood that the above-mentioned modem may not be integrated into the processor 401, and it can be implemented by a single chip.
[0110] Among them, the memory 405 may include a random access memory (Random Access Memory, RAM) and may also include a read-only memory (Read-Only Memory). Optionally, the memory 405 includes a non-transitory computer-readable storage medium. The memory 405 can be used to store instructions, programs, codes, code sets or instruction sets. The memory 405 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the above-mentioned various method embodiments, etc.; the data storage area may store data involved in the above-mentioned various method embodiments, etc. The memory 405 may also be optionally at least one storage device located away from the aforementioned processor 401. Refer to Figure 4 , the memory 405 as a computer storage medium may include an operating system, a network communication module, a user interface module, and a 2D platform fluid simulation application based on a color channel.
[0111] exist Figure 4 In the electronic device shown, the user interface 403 is mainly used to provide an input interface for the user and obtain the data input by the user; and the processor 401 can be used to call the 2D platform fluid simulation application based on the color channel stored in the memory 405. When executed by one or more processors 401, the electronic device executes one or more of the methods described in the above embodiments. It should be noted that for the aforementioned method embodiments, for the sake of simple description, they are all expressed as a series of action combinations, but those skilled in the art should know that the present application is not limited to the described order of actions, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required for the present application.
[0112] The present application also provides a computer-readable storage medium, which stores instructions. When executed by one or more processors, the electronic device executes one or more of the methods described in the above embodiments.
[0113] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0114] In the several embodiments provided in this application, it should be understood that the disclosed devices can be implemented in other ways. For example, the device embodiments described above are only schematic, such as the division of units, which is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some service interfaces, and the indirect coupling or communication connection of devices or units can be electrical or other forms.
[0115] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0116] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0117] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a memory and includes several instructions for a computer device (which can be a personal computer, server or network device, etc.) to perform all or part of the steps of the various embodiments of the present application. The aforementioned memory includes: various media that can store program codes, such as USB flash drives, mobile hard drives, magnetic disks or optical disks.
[0118] The above are only exemplary embodiments disclosed in this application, and the scope of the disclosure of this application cannot be limited by this. That is, any equivalent changes and modifications made according to the teachings disclosed in this application are still within the scope of the disclosure of this application. After considering the disclosure of the specification and the truth of practice, those skilled in the art will easily think of other implementation schemes disclosed in this application.
[0119] This application is intended to cover any modifications, uses or adaptive changes disclosed in this application, which follow the general principles disclosed in this application and include common knowledge or customary technical means in the technical field not recorded in this application.
Claims
1. A 2D platform fluid simulation method based on color channel, characterized in that: The method comprises: Obtaining a grid size and a view space matrix size corresponding to the fluid to be rendered, wherein the grid size and the view space matrix size are determined according to a spatial region where the fluid to be rendered is located; Constructing a view space matrix corresponding to the fluid to be rendered according to the grid size and the view space matrix size; Traversing the view space matrix in a preset order to obtain a grid state corresponding to each grid in the view space matrix; According to the view space matrix and the grid state corresponding to each of the grids, a liquid state sampling map corresponding to the fluid to be rendered is constructed, wherein the liquid state sampling map stores the liquid information corresponding to each of the grids through ARGB channels, wherein the A channel is used to store the liquid's own water volume information, the R channel stores the left water volume information in the liquid information, the G channel stores the right water volume information in the liquid information, and the B channel stores the bottom water volume information in the liquid information; According to the liquid state sampling graph and the ARGB channels respectively corresponding to each of the grids, obtaining a drawing state corresponding to the fluid to be rendered; According to the drawing state, a drawing mode corresponding to the fluid to be rendered is confirmed, and according to the drawing mode, a drawing operation is performed on the fluid to be rendered.
2. The method according to claim 1, characterized in that Before constructing the view space matrix corresponding to the fluid to be rendered according to the grid size and the view space matrix size, the method further includes: The following formula is used to determine whether there is remaining space in the view space matrix: Wherein, i is the number of rows of the view space matrix, j is the number of columns of the view space matrix, C is the grid size, G W is the height of the view space matrix, G h is the width of the view space matrix; If i calculated according to the above formula is not an integer, it is confirmed that the view space matrix has the remaining space, and the number of rows of the view space matrix is increased according to the calculation result; If j calculated according to the above formula is not an integer, it is confirmed that the view space matrix has the remaining space, and the number of columns of the view space matrix is increased according to the calculation result.
3. The method according to claim 1, characterized in that: The grid state includes a solid state and a flow state. After traversing the view space matrix in a preset order to obtain a grid state corresponding to each grid in the view space matrix, the method further includes: If it is confirmed that the grid state is the flow state, determine whether the flow state satisfies a first determination condition; if it is confirmed that the flow state satisfies the first determination condition, then the specific flow state corresponding to the flow state is a liquid downward flow state, and the first determination condition is that the self water volume information corresponding to the target grid is greater than the bottom water volume information corresponding to the target grid, and the target grid is any grid corresponding to the flow state in the view space matrix; If it is confirmed that the flow state satisfies the second judgment condition, the specific flow state corresponding to the flow state is the left-right flow state of the liquid, and the second judgment condition is that the flow state corresponding to the grid below the target grid is the entity state, or the bottom water volume information corresponding to the target grid is the maximum value of 255; and the left water volume information corresponding to the target grid is less than the own water volume information corresponding to the target grid, or the right water volume information corresponding to the target grid is less than the own water volume information corresponding to the target grid; If the first judgment condition is not satisfied, and the second judgment condition is not satisfied, it is confirmed that the specific flow state corresponding to the flow state is a liquid static state.
4. The method according to claim 1, characterized in that The step of constructing a liquid state sampling map corresponding to the fluid to be rendered according to the view space matrix and the grid state corresponding to each of the grids specifically includes: The liquid information corresponding to each of the grids is constructed according to the following formula: S i,j =Vector4(C.r,C.g,C.b,C.a); In the above formula Among them, S i,j is the unit pixel corresponding to the grid in row i and column j of the liquid state sampling diagram, Vector4(Cr, Cg, Cb, Ca) is the ARGB channel corresponding to the unit pixel, Ca is the A channel, Cr is the R channel, Cg is the G channel, Cb is the B channel, L i,j is the water volume information corresponding to the unit pixel, L i,j-1 is the left water volume information corresponding to the unit pixel, L i,j+1 is the water volume information on the right side corresponding to the unit pixel, L i+1,j is the bottom water volume information corresponding to the unit pixel, L max The maximum value of liquid information corresponding to each of the grids.
5. The method according to claim 4, characterized in that The obtaining, according to the liquid state sampling graph and the ARGB channels respectively corresponding to each of the grids, the drawing state corresponding to the fluid to be rendered specifically includes: The channel values corresponding to each grid are obtained by the following formula: Among them, L, L1, L r , L b are the channel values corresponding to each of the grids respectively; According to the channel values corresponding to each of the grids, the different drawing state values corresponding to each of the grids are determined according to the following formula: Wherein, empty, flowing, idle, and wawe are different drawing state values corresponding to each of the grids; According to the drawing state value corresponding to each of the grids, the drawing state corresponding to each of the grids is confirmed to confirm the drawing state corresponding to the fluid to be rendered, wherein when the drawing state value is empty, the drawing state corresponding to the single grid is an empty state; when the drawing state value is flowing, the drawing state corresponding to the single grid is a suspended state; when the drawing state value is idle, the drawing state corresponding to the single grid is a maximum value state; when the drawing state value is wawe, the drawing state corresponding to the single grid is a planar flow state.
6. The method according to claim 5, characterized in that The determining, according to the drawing state, a drawing method corresponding to the fluid to be rendered specifically includes: According to the drawing state corresponding to each of the grids, the drawing method corresponding to each of the grids is confirmed to confirm the drawing method corresponding to the fluid to be rendered, wherein when the drawing state is the empty state, the drawing method corresponding to the single grid is to stop drawing; when the drawing state is the suspended state, the drawing method corresponding to the single grid is to draw a square liquid texture corresponding to the size of the single grid; when the drawing state is the maximum value state, the drawing method corresponding to the single grid is to draw a picture of falling fluid particles; when the drawing state is the plane flow state, the drawing method corresponding to the single grid is to use double wave interpolation to draw the liquid surface flow effect.
7. The method according to claim 6, characterized in that The use of double wave interpolation to draw the liquid surface flow effect specifically includes: According to the following formula, the double wave interpolation is used to draw the flow effect of the liquid surface: Among them, interceptor1 and interceptor2 are floating point numbers, UV is a two-dimensional coordinate vector, wherein UV.x is a horizontal coordinate vector, Time.x is a time variable moving forward, LineH is the baseline height of the liquid surface, LineH is calculated by L and Lr, and flowLine is the flow effect of the liquid surface.
8. A 2D platform fluid simulation device based on color channel, characterized in that: The device comprises a view space matrix construction module, a liquid state sampling map construction module and a fluid rendering drawing module, wherein: The view space matrix construction module is used to obtain a grid size and a view space matrix size corresponding to the fluid to be rendered, wherein the grid size and the view space matrix size are determined according to the spatial region where the fluid to be rendered is located; and construct the view space matrix corresponding to the fluid to be rendered according to the grid size and the view space matrix size; The liquid state sampling map construction module is used to traverse the view space matrix in a preset order to obtain the grid state corresponding to each grid in the view space matrix; according to the view space matrix and the grid state corresponding to each grid, construct a liquid state sampling map corresponding to the fluid to be rendered, and the liquid state sampling map stores the liquid information corresponding to each grid through ARGB channels, wherein the A channel is used to store the liquid water volume information, the R channel stores the left water volume information, the G channel stores the right water volume information, and the B channel stores the bottom water volume information in the liquid information; The fluid rendering and drawing module is used to obtain the drawing state corresponding to the fluid to be rendered according to the liquid state sampling map and the ARGB channels corresponding to each of the grids; according to the drawing state, confirm the drawing method corresponding to the fluid to be rendered, and perform a drawing operation on the fluid to be rendered according to the drawing method.
9. An electronic device, characterized in that: It includes a processor, a communication bus, a user interface, a network interface and a memory, wherein the memory is used to store instructions, the user interface and the network interface are used to communicate with other devices, and the processor is used to execute the instructions stored in the memory so that the electronic device executes the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores instructions, and when the instructions are executed, the method according to any one of claims 1 to 7 is performed.
