Grassland rendering method and device, computing equipment and computer readable storage medium
By using specific UV coordinate projection and perturbing noise maps in the grass model, combined with instantiation and batch processing technology, the performance problems of mobile devices when rendering large-scale grasslands are solved, and efficient grassland rendering effects are achieved.
Patent Information
- Application Number
- CN202510456668.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-12
- Publication Date
- 2025-08-01
AI Technical Summary
Mobile devices face hardware performance pressure and frame rate decline caused by excessive number of vertices when rendering large-scale grasslands, which affects the user experience.
By creating a grass model, using specific UV coordinate projection and perturbing noise maps, combining instantiation and batching techniques, the grass rendering process is optimized, the number of vertices is reduced, and the rendering efficiency is improved.
While maintaining high visual effects, it reduces hardware resource consumption and improves the performance and user experience of mobile grass rendering.
Smart Images

Figure CN120411280A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer graphics rendering technology, and particularly to a grassland rendering method and apparatus, a computing device, and a computer-readable storage medium. Background Art
[0002] In the field of computer graphics technology that has developed rapidly in recent years, realistic grassland rendering has become an important part of enhancing the virtual reality experience and improving the visual effects of computer games. However, large-scale grassland rendering faces many technical challenges. Especially when dealing with a large amount of vertex data, each grass in the grassland is defined by multiple vertices. When high-visual grassland effects need to be rendered, the number of these vertices increases sharply, forming a huge data processing burden. When the grass model is made to show a swaying effect with the wind by offsetting the vertex positions, this problem is exacerbated.
[0003] Due to the hardware performance limitations of mobile devices, they are more vulnerable to this impact. These rendering tasks not only increase the rendering pressure on mobile devices but also directly affect the frame rate performance of the devices. A decrease in the frame rate will result in image stuttering, latency, and an unsmooth user experience, seriously affecting the user's satisfaction with the application or game. Summary of the Invention
[0004] In view of this, embodiments of this application provide a grassland rendering method and apparatus, a computing device, and a computer-readable storage medium to solve the technical defects existing in the prior art.
[0005] According to a first aspect of the embodiments of this application, a grassland rendering method is provided, including:
[0006] Obtain a grass cluster model, where the grass cluster model is composed of n grass plants; the grass cluster model includes a first UV coordinate and a second UV coordinate; the first UV coordinate is obtained by projecting the grass cluster model in the horizontal axis direction; the second UV coordinate is obtained by projecting the grass cluster model in the vertical axis direction and moving all the UV coordinates of each grass plant to the same UV coordinate position respectively;
[0007] In the vertex shader, convert the second UV coordinate of the grass plant vertex to the world space to obtain a first perturbation sampling coordinate; convert the grass plant vertex to the world space coordinate, and select the corresponding coordinate component as the second perturbation sampling coordinate;
[0008] Sample a perturbation noise map according to the first perturbation sampling coordinate and the second perturbation sampling coordinate to obtain a perturbation vector, and adjust the vertex position of the grass plant according to the perturbation vector;
[0009] Interpolate a predefined color according to the first UV coordinate to obtain the grass plant vertex color, and complete the rendering of the grassland.
[0010] According to a second aspect of the embodiments of the present application, a grassland rendering device is provided, including:
[0011] A loading module, configured to obtain a grass cluster model, where the grass cluster model is composed of n grass plants; the grass cluster model includes a first UV coordinate and a second UV coordinate; the first UV coordinate is obtained by projecting the grass cluster model in the horizontal axis direction; the second UV coordinate is obtained by projecting the grass cluster model in the vertical axis direction and moving all the UV coordinates of each grass plant to the same UV coordinate position respectively;
[0012] A calculation module, configured to, in a vertex shader, convert the second UV coordinate of a grass plant vertex to world space to obtain a first perturbation sampling coordinate; convert the grass plant vertex to world space coordinates, and select corresponding coordinate components as the second perturbation sampling coordinate;
[0013] A sampling module, configured to sample a perturbation noise map according to the first perturbation sampling coordinate and the second perturbation sampling coordinate to obtain a perturbation vector; adjust the vertex position of the grass plant according to the perturbation vector;
[0014] A rendering module, configured to interpolate a predefined color according to the first UV coordinate to obtain the grass plant vertex color, and complete the rendering of the grassland.
[0015] According to a third aspect of the embodiments of the present application, a computing device is provided, including a memory, a processor, and computer instructions stored on the memory and executable on the processor, where when the processor executes the instructions, the steps of the grassland rendering method are implemented.
[0016] According to a fourth aspect of the embodiments of the present application, a computer-readable storage medium is provided, which stores computer instructions, and when the instructions are executed by a processor, the steps of the grassland rendering method are implemented.
[0017] Through the method provided by the embodiments of the present application, a grass cluster model composed of a small number of grass plants and having specific UV coordinates is made; further, in a vertex shader, the overall perturbation vector on each grass plant and the perturbation vectors on different vertices of each grass plant are calculated according to the UV coordinates, and the vertex position of the grass plant is calculated by combining the two; the gradient color of each grass plant is also calculated through the UV coordinates. The embodiments of the present application not only simulate various effects of the grassland in the vertex shader, but also batch-render the grass cluster model by using the instancing and batching techniques, creating a complex visual effect while maintaining a good performance balance, and are suitable for the grassland rendering scenario in mobile applications. Description of the Drawings
[0018] Figure 1It is a structural block diagram of a computing device provided by an embodiment of the present application;
[0019] Figure 2 It is a schematic flowchart of a grassland rendering method provided by an embodiment of the present application;
[0020] Figure 3 It is a schematic diagram of the effect of a grass cluster model provided by an embodiment of the present application;
[0021] Figure 4 It is a schematic diagram of the first UV coordinates of a grass cluster model provided by an embodiment of the present application;
[0022] Figure 5 It is a schematic diagram of the second UV coordinates of a grass cluster model provided by an embodiment of the present application;
[0023] Figure 6 It is a schematic diagram of the grassland rendering effect achieved according to the method provided by an embodiment of the present application; [[ID=2l]]
[0024] Figure 7 It is a structural schematic diagram of a grassland rendering device provided by an embodiment of the present application. Detailed implementation manners
[0025] In the following description, many specific details are set forth in order to provide a thorough understanding of the present application. However, the present application can be implemented in many other ways different from those described herein, and those skilled in the art can make similar generalizations without departing from the connotation of the present application. Therefore, the present application is not limited by the specific implementations disclosed below.
[0026] The terms used in one or more embodiments of the present application are for the purpose of describing specific embodiments only and are not intended to limit one or more embodiments of the present application. The singular forms "a", "the" and "said" used in one or more embodiments of the present application and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term "and / or" used in one or more embodiments of the present application refers to and includes any or all possible combinations of one or more of the associated listed items.
[0027] It should be understood that although the terms first, second, etc. may be used in one or more embodiments of the present application to describe various information, such information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of the present application, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein can be interpreted as "in response to determining".
[0028] In the present application, a grassland rendering method and apparatus, a computing device, and a computer-readable storage medium are provided, which will be described in detail one by one in the following embodiments.
[0029] Figure 1 FIG. 4 shows a structural block diagram of a computing device 100 according to an embodiment of the present application. The components of the computing device 100 include, but are not limited to, a memory 110 and a processor 120. The processor 120 is connected to the memory 110 through a bus 130, and a database 150 is used to store data.
[0030] The computing device 100 further includes an access device 140, and the access device 140 enables the computing device 100 to communicate via one or more networks 160. Examples of these networks include a Public Switched Telephone Network (PSTN), a Local Area Network (LAN), a Wide Area Network (WAN), a Personal Area Network (PAN), or a combination of communication networks such as the Internet. The access device 140 may include one or more of any type of wired or wireless network interfaces (for example, a Network Interface Card (NIC)), such as an IEEE802.11 Wireless Local Area Network (WLAN) wireless interface, a Worldwide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a Universal Serial Bus (USB) interface, a cellular network interface, a Bluetooth interface, a Near Field Communication (NFC) interface, and so on.
[0031] In an embodiment of the present application, the above components of the computing device 100 and Figure 1 other components not shown in FIG. 4 may also be connected to each other, for example, through a bus. It should be understood that Figure 1 the shown structural block diagram of the computing device is only for illustrative purposes and is not a limitation on the scope of the present application. Those skilled in the art can add or replace other components as needed.
[0032] The computing device 100 can be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (for example, a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (for example, a smart phone), a wearable computing device (for example, a smart watch, smart glasses, etc.) or other types of mobile devices, or a stationary computing device such as a desktop computer or a PC.
[0033] In the embodiments of the present application, Figure 2 FIG. 22 shows a flowchart of a grassland rendering method provided by the present application, including steps 202 to 208.
[0034] Step 202: Obtain a grass cluster model, which is composed of n grass plants, where n > 0; the grass cluster model includes a first UV coordinate and a second UV coordinate; the first UV coordinate is obtained by projecting the grass cluster model along the horizontal axis direction; the second UV coordinate is obtained by projecting the grass cluster model along the vertical axis direction and moving all the UV coordinates of each grass plant to the same UV coordinate position respectively.
[0035] In the embodiments of the present application, in order to avoid an excessive number of vertices during large-scale grassland rendering, when making the grass cluster model, the grass cluster model is only composed of a limited number of individual grass plants.
[0036] In a feasible implementation manner, the grass cluster model is composed of more than a dozen individual grass plant models, as Figure 3 shown. And, the bottoms of the above grass cluster models are flush, that is, the roots of each grass plant are located on a certain plane, such as the xz plane.
[0037] Further, the grass cluster model contains 2 UV coordinates, a first UV coordinate and a second UV coordinate. Among them, the first UV coordinate is generated by projecting the grass cluster model in a certain horizontal axis direction, such as by projecting in the x-axis direction or the z-axis direction, as Figure 4 shown; the second UV coordinate is obtained by projecting the grass cluster model in the vertical axis direction, such as by projecting in the y-axis direction, and moving all the vertices of a grass plant to the same position, as Figure 5 shown. In Figure 5 , projecting in the vertical axis direction means observing the grass cluster model from the perspective of a top view and moving all the UV points of each grass plant to the same position respectively, Figure 5 and each point in
[0038] Step 204: In the vertex shader, after converting the second UV coordinate of the grass plant vertex to the world space, obtain the first perturbation sampling coordinate; convert the grass plant vertex to the world space coordinate, and select the corresponding coordinate component as the second perturbation sampling coordinate.
[0039] In the embodiments of the present application, all the rendering calculations of the grass cluster model are completed through the vertex shader.
[0040] In this step, the calculation of the sampling coordinates of two perturbation noises is completed in the vertex shader. Specifically, first, calculate the first perturbation sampling coordinate of each grass plant according to the second UV coordinate. This step includes:
[0041] Step 302: Convert the second UV coordinates of the grass plant vertices into a three-dimensional vector, with the X and Z components using the components of the second UV coordinates; the schematic code includes:
[0042] float3 grassPos = float3(v.uv2.x, 0, v.uv2.y); / / Convert the second UV coordinates of the grass plant vertices into a three-dimensional vector, with the X and Z components using the components of the second UV coordinates, and the Y component hard-coded as 0.
[0043] Step 304: Calculate the X and Z components of the three-dimensional vector in world coordinates; the schematic code includes:
[0044] float grassPosX = dot(grassPos, unity_ObjectToWorld[0].xyz); / / Calculate the X component of the grassPos vector in world coordinates
[0045] float grassPosZ = dot(grassPos, unity_ObjectToWorld[2].xyz); / / Calculate the Z component of the grassPos vector in world coordinates
[0046] Step 306: Combine the calculated X and Z components into a two-dimensional vector, which represents the first perturbation sampling coordinates of the grass plant where the grass plant vertex is located in the world coordinate system. The schematic code includes:
[0047] float2 LocalGrassUV = float2(grassPosX, grassPosZ); / / Combine the calculated X and Z components into a two-dimensional vector LocalGrassUV, which is the first perturbation sampling coordinates in the world coordinate system.
[0048] Since each point in the second UV coordinates represents a grass plant, the first perturbation sampling coordinates calculated according to the second UV coordinates can be used to ensure the independent randomness of each grass plant under natural conditions, such as the influence of wind, that is, the random value of each grass plant is independent of the random values of other grass plants, but the random values on the same grass plant are consistent.
[0049] Furthermore, convert the grass plant vertex to world space coordinates and select the corresponding coordinate components as the second perturbation sampling coordinates. The schematic code includes:
[0050] float3 posWS = mul(unity_ObjectToWorld, v.vertex).xyz; / / Convert the grass plant vertex to world space coordinates through a matrix.
[0051] float2 GlobalGrassUV = posWS.xz;
[0052] Among them, GlobalGrassUV is used as the second perturbation sampling coordinate to ensure the randomness of each part on each grass plant. posWS.xz refers to the X and Z coordinates of each vertex of the grass plant in the world space. These coordinates can be used to ensure that the vertex positions of each grass plant are not completely regular, increasing the natural effect. For example, a random offset can be applied to these coordinates so that different parts of different grasses have different random values to simulate the different effects of wind on each part of the grass plant, so that the wind has a continuous effect on the grass in an area.
[0053] Step 206: Sample the perturbation noise map according to the first perturbation sampling coordinate and the second perturbation sampling coordinate to obtain a perturbation vector, and adjust the vertex position of the grass plant according to the perturbation vector.
[0054] In the embodiment of the present application, the overall perturbation vector on each grass plant is calculated according to the first perturbation sampling coordinate, and the perturbation vectors on different vertices of each grass plant are calculated according to the second perturbation sampling coordinate, and the vertex position of the grass plant under the perturbation is calculated by combining the two.
[0055] Specifically, calculate the UV coordinates of the perturbation noise map. Preferably, this perturbation can be used for wind perturbation. The steps for calculating the UV coordinates of the perturbation noise map include:
[0056] Multiply the previously calculated first perturbation sampling coordinate by a preset scaling factor _NoiseTilling1 to control the scaling degree of the noise map UV.
[0057] Multiply the second perturbation sampling coordinate by a preset scaling factor _NoiseTilling2 to further control the scaling of the noise map UV.
[0058] Add the two to obtain the scaled UV coordinates of the perturbation noise map:
[0059] windTilling = LocalGrassUV * _NoiseTilling1 + GlobalGrassUV * _NoiseTilling2; / / uv scaling
[0060] Furthermore, obtain the wind UV coordinates according to the UV coordinates of the perturbation noise map and the wind direction and wind speed that change with time:
[0061] float2 wind_uv = windTilling + _Time.y * WindDirection * _WindSpeed;
[0062] Furthermore, sample the wind noise map using the wind UV coordinates to obtain the wind disturbance vector:
[0063] float2 wind2D = tex2Dlod(_NoiseTex, float4(wind_uv, 0, 0)).xy / / Sample the noise texture _NoiseTex at the lod level using wind_uv, and obtain the X and Y components of the sampling result as the wind disturbance vector.
[0064] Preferably, combine the wind disturbance vector and the random wind intensity to control the final output of the wind effect. Specifically, multiply the wind disturbance vector wind2D by the superimposed value of the wind intensity _WindIntensity and the random wind intensity FragmentWindInt to obtain the final wind disturbance vector:
[0065] wind2D = clamp(wind2D * (_WindIntensity + FragmentWindInt), -1, 1);
[0066] Among them, use the sin function and the fractional part of the time to obtain a continuously changing value to get the random wind intensity FragmentWindInt.
[0067] Furthermore, adjust the position of the grass blade vertices according to the wind disturbance vector to simulate the left - right swaying effect of the grass blade under the wind:
[0068] posWS += float3(wind2D.x, 0, wind2D.y)
[0069] Preferably, when the grass blade is affected by the wind, in addition to swaying left and right, it will also bend in the vertical direction. Therefore, in another embodiment of the present application, the reduction value in the vertical direction is calculated according to the value of the left - right swaying of the grass blade, which is used to simulate the influence of the wind on the grass blade in the vertical direction.
[0070] Among them, the height value of the grass blade reduced according to the wind intensity can be calculated by using the curve simulation method:
[0071] float heightReduce = windSquare * 0.5 + windSquare * windSquare * 0.15;
[0072] Among them, windSquare is a predefined parameter related to the wind magnitude, for example:
[0073] float windSquare = dot(wind2D, wind2D); / / wind2D is the aforementioned wind disturbance vector. Further, adjust the position of the grass blade vertices according to the wind disturbance vector and the reduced height value:
[0074] posWS += float3(wind2D.x, -heightReduce, wind2D.y) * v.vertex.z;
[0075] float3(wind2D.x, -heightReduce, wind2D.y) represents the influence of the wind on the position of the grass blade vertices, where wind2D.x and wind2D.y respectively represent the influence of the wind in the x and z directions, and -heightReduce represents the change in the height of the grass blade in the y-axis direction, i.e., the grass blade height, caused by the wind.
[0076] v.vertex.z is the original z coordinate of the vertex. Multiplying by this value can be used to adjust the intensity of the wind influence, so that the position of the bottom of the grass blade is less affected by the wind, while the position of the top is more affected by the wind, thereby updating the position of the grass blade in all directions in the world space after being affected by the wind.
[0077] Step 208: Interpolate the predefined color according to the first UV coordinate to obtain the grass blade vertex color, and complete the rendering of the grassland.
[0078] In the embodiment of the present application, interpolate and calculate the predefined color according to the first UV coordinate of the vertex, and complete the rendering of the entire grass blade after obtaining the color of the grass blade.
[0079] Specifically, first use the y component of the first UV coordinate of the vertex as the input value for the gradient;
[0080] fixed rampUV = v.uv1.y;
[0081] Further, define the color of the tip of the grass blade, the middle color, and the root color, and obtain the basic gradient color from the root to the tip of the grass cluster through interpolation to complete the color rendering of the grass blade. Specifically, this step includes:
[0082] Step 402: Calculate the scale factor of the vertex in the entire grass blade.
[0083] fixed ramp1 = (rampUV - _PositionMiddle) / (_PositionTop - _PositionMiddle);
[0084] Among them, _PositionMiddle and _PositionTop are the predefined y coordinates at the middle and top positions of the grass.
[0085] Step 404: Calculate the base color of the vertex according to the scale factor and the predefined grass color interpolation.
[0086] fixed4 grassCol_smoothness = lerp(_ColorMiddle, _ColorTop, ramp1); / / Get the base color of the current vertex position
[0087] Among them, _ColorMiddle and _ColorTop are the predefined middle and top colors.
[0088] lerp(_ColorMiddle, _ColorTop, ramp1) performs linear interpolation between the middle and top colors using the previously calculated scale factor ramp1 to obtain the base color of the current vertex position.
[0089] Step 406: Calculate the final vertex color by combining the base color and the wind influence.
[0090] o.GrassColor = grassCol_smoothness * lerp(_GrassDarkness, _GrassBrightness, heightReduce);
[0091] heightReduce is the height value by which the grass stalk reduces according to the wind intensity. It is obtained from the aforementioned calculation. Use heightReduce to perform linear interpolation between the dark and bright colors to simulate the color change when the wind blows the grass. Thus, the color after combining the base color and the wind influence is calculated, and the final grass color is assigned to the variable o.GrassColor of the vertex shader.
[0092] Step 408: After the vertex shader calculates the color of each vertex, use the vertex color as the input of the fragment shader.
[0093] float4 frag(VertexOutputIN):SV_Target{
[0094] return IN.GrassColor; / / The fragment shader directly uses the interpolated vertex color
[0095] }
[0096] Thus, through GPU interpolation calculation, the position of each fragment within the triangle will determine the final color it interpolates from the three vertex colors, thus visually presenting a smooth color transition.
[0097] Similarly, the final color of the vertices of the middle and lower grass plants can be calculated and will not be elaborated here.
[0098] Preferably, the lighting effect of the grass plants is adjusted according to the first UV coordinate in the vertex shader.
[0099] 1. Ambient light processing
[0100] o.GrassColor *= 1.0 + rampUV;
[0101] rampUV is the value of the aforementioned first UV coordinate based on the vertex, which fades from the root to the tip of the grass.
[0102] Adjust the grass color o.GrassColor by 1.0 + rampUV to make the grass color change with height, thereby adding some ambient light effects.
[0103] 2. Specular highlight calculation
[0104] For the specular highlights of the grassland, it is necessary to show the specular highlight degrees of different parts of the grass. For example, the reflection degree of the grass tip is large, and the reflection degree of the grass root is small. At the same time, when the wind blows, the specular highlights will also change. Therefore, when calculating the specular reflection intensity of the specular highlights, the transparency of the grass (o.GrassColor.a) and the value of heightReduce are subtracted to adjust the specular highlight intensity:
[0105] float specPower = saturate(1 - o.GrassColor.a - heightReduce);
[0106] Calculate the final specular highlight color, and use the predefined specular reflection color _SpecularColor multiplied by the specular highlight intensity calculated by the dot product.
[0107] fixed3 specualrCol = _SpecularColor.xyz * saturate(pow(nDoth, specPower));
[0108] 3. Calculate the final color according to the specular highlights
[0109] fixed3 finalCol = o.GrassColor.xyz + specualrCol.xyz;
[0110] finalCol is the final color of the grass leaf, which is obtained by adding the base color o.GrassColor and the specular reflection color specualrCol.
[0111] 4. Perform shadow calculation in the vertex shader
[0112] TRANSFER_SHADOW(o);
[0113] fixed shadow = SHADOW_ATTENUATION(o);
[0114] o.GrassColor = fixed4(finalCol.xyz * shadow, 1.0);
[0115] Among them, TRANSFER_SHADOW(o) transfers shadow data to the output structure o; SHADOW_ATTENUATION(o) calculates the shadow attenuation value; finally, the shadow effect is applied to the final color, and the final color with the shadow effect is obtained by multiplying by shadow.
[0116] Preferably, in order to render a vast grassland, a large number of grass plants need to be drawn. In this case, the traditional method of drawing each grass plant individually will result in a large number of draw calls, thereby increasing the communication overhead between the CPU and the GPU and reducing the rendering performance. To solve this problem, GPU instancing and batching techniques are also used in the embodiments of the present application.
[0117] GPU instancing is a technique that can render multiple grass plant instances in a single draw call. Each instance shares the same geometric vertex data but can have different transformation matrices, materials, or other attributes. By instancing, the number of draw calls issued by the CPU to the GPU can be significantly reduced, thereby improving the rendering performance.
[0118] Specifically, each grass plant instance is assigned a unique instance identifier, which is used to distinguish different grass plant instances and apply different transformations and materials during the rendering process. Further, each grass plant instance includes:
[0119] Transformation matrix: Each grass plant instance has a corresponding transformation matrix, which defines the position, scale, and rotation of the instance in the world space. These transformation matrices enable the appearance of each grass plant instance to be different even though they share the same geometric vertex data.
[0120] Color calculation: Through texture coordinates, we calculate the color based on the gradient effect of the grass plant from the root to the tip, which can make the grass plant more natural. The influence of ambient light is also considered to adjust the overall color of the grass plant to match the surrounding environment and lighting.
[0121] Lighting and Shadows: For lighting effects, calculate the normal of each grass blade instance and perform a dot product calculation with the light source and view direction to achieve specular highlights. By combining the light source position and the camera position, calculate the intensity of the light received by the grass blades at different angles. Finally, calculate the shadow effect of each grass blade instance through the shadow processing function and apply the lighting and shadows to the final color of the grass blades.
[0122] Batching: Multiple grass blade instances that meet the conditions are grouped into the same draw batch for rendering. This batching technique can further reduce the number of draw calls and improve rendering efficiency. Through batching, thousands of grass blades can be drawn within a large batch, greatly optimizing the rendering performance.
[0123] The instancing and batching techniques greatly optimize the rendering performance of the grassland by sharing geometric data, applying different transformation matrices, calculating the colors, lighting, and shadows of the grass blades, and combining multiple grass blade instances into the same draw batch. Finally, a vast grassland can be rendered efficiently, ensuring the speed and quality of graphics processing, and the final grassland rendering effect Figure 6 is shown.
[0124] In the above embodiments of the present application, in order to efficiently achieve a large-scale grassland rendering effect on mobile devices, while ensuring that the rendering results are as realistic as possible without consuming too much hardware resources, first, a grass cluster model composed of a small number of grass blades is created. This grass cluster model has 2 specific UV coordinates; further, in the vertex shader, calculate the overall perturbation vector on each grass blade and the perturbation vectors at different vertices of each grass blade according to the UV coordinates, and combine the two to calculate the vertex positions of the grass blades; also calculate the gradient color of each grass blade through the UV coordinates. The embodiments of the present application not only simulate various effects of the grassland in the vertex shader, but also batch-render the grass cluster model by using instancing and batching techniques, creating complex visual effects while maintaining a good performance balance, and are suitable for grassland rendering scenarios in mobile applications.
[0125] Corresponding to the above method embodiments, the present application also provides an embodiment of a grassland rendering device, as Figure 7 shown, the device includes:
[0126] A loading module, configured to obtain a grass cluster model, where the grass cluster model is composed of n grass blades; the grass cluster model includes a first UV coordinate and a second UV coordinate; the first UV coordinate is obtained by projecting the grass cluster model along the horizontal coordinate axis direction; the second UV coordinate is obtained by projecting the grass cluster model along the vertical coordinate axis direction and moving all vertices of each grass blade to the same position respectively;
[0127] A calculation module, configured to, in a vertex shader, convert the second UV coordinate of a grass plant vertex to the world space to obtain a first perturbation sampling coordinate; convert the grass plant vertex to the world space coordinate, and select a corresponding coordinate component as the second perturbation sampling coordinate;
[0128] A sampling module, configured to sample a perturbation noise map according to the first perturbation sampling coordinate and the second perturbation sampling coordinate to obtain a perturbation vector; adjust the vertex position of the grass plant according to the perturbation vector;
[0129] A rendering module, configured to interpolate a predefined color according to the first UV coordinate to obtain the grass plant vertex color, and complete the rendering of the grassland.
[0130] The above is a schematic solution of a grassland rendering device in this embodiment. It should be noted that the technical solution of this grassland rendering device and the technical solution of the above grassland rendering method belong to the same concept. For the details not described in detail in the technical solution of this grassland rendering device, reference can be made to the description of the technical solution of the above grassland rendering method.
[0131] An embodiment of the present application further provides a computing device, including a memory, a processor, and computer instructions stored on the memory and executable on the processor. When the processor executes the instructions, the steps of the above-mentioned grassland rendering method are implemented.
[0132] The above is a schematic solution of a computing device in this embodiment. It should be noted that the technical solution of this computing device and the technical solution of the above grassland rendering method belong to the same concept. For the details not described in detail in the technical solution of this computing device, reference can be made to the description of the technical solution of the above grassland rendering method.
[0133] An embodiment of the present application further provides a computer-readable storage medium, which stores computer instructions. When the instructions are executed by a processor, the steps of the grassland rendering method as described above are implemented.
[0134] The above is a schematic solution of a computer-readable storage medium in this embodiment. It should be noted that the technical solution of this storage medium and the technical solution of the above grassland rendering method belong to the same concept. For the details not described in detail in the technical solution of this storage medium, reference can be made to the description of the technical solution of the above grassland rendering method.
[0135] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be performed in a different order than in the embodiments and still achieve the desired result. Additionally, the processes depicted in the figures do not necessarily require the particular order or sequential order shown to achieve the desired result. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0136] The computer instructions include computer program code, which may be in the form of source code, object code, an executable file, or some intermediate form, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, a recording medium, a USB flash drive, a portable hard drive, a magnetic disk, an optical disk, a computer memory, a read-only memory (ROM), a random access memory (RAM), an electrical carrier signal, a telecommunication signal, and a software distribution medium, etc. It should be noted that the content included in the computer-readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice within the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, the computer-readable medium does not include electrical carrier signals and telecommunication signals.
[0137] It should be noted that for the foregoing method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations. However, those skilled in the art should know that the present application is not limited by the described order of actions, because according to the present application, certain steps may 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 essential to the present application.
[0138] In the above embodiments, the descriptions of the various embodiments have their own emphases. For the parts not detailed in a certain embodiment, reference may be made to the relevant descriptions of other embodiments.
[0139] The preferred embodiments of the present application disclosed above are only used to help explain the present application. The optional embodiments do not elaborate on all the details and do not limit the invention to only the specific embodiments described. Obviously, many modifications and variations can be made according to the content of the present application. The present application selects and specifically describes these embodiments to better explain the principles and practical applications of the present application, so that those skilled in the art can well understand and utilize the present application. The present application is only limited by the claims and their full scope and equivalents.
Claims
1. A grassland rendering method, characterized in that, Comprising: Obtain a grass model, which is composed of n grass plants, where n > 0; the grass model includes a first UV coordinate and a second UV coordinate; the first UV coordinate is obtained by projecting the grass model along the horizontal axis direction; the second UV coordinate is obtained by projecting the grass model along the vertical axis direction and moving all the UV coordinates of each grass plant to the same UV coordinate position respectively; In the vertex shader, after converting the second UV coordinate of the grass plant vertex to the world space, obtain the first perturbation sampling coordinate; Convert the grass plant vertex to the world space coordinate, and select the corresponding coordinate component as the second perturbation sampling coordinate; Sample the perturbation noise map according to the first perturbation sampling coordinate and the second perturbation sampling coordinate to obtain a perturbation vector, and adjust the vertex position of the grass plant according to the perturbation vector; Interpolate the predefined color according to the first UV coordinate to obtain the grass plant vertex color, and complete the rendering of the grassland.
2. The method according to claim 1, wherein, Obtaining the first perturbation sampling coordinate after converting the second UV coordinate of the grass plant vertex to the world space includes: Convert the second UV coordinate of the grass plant vertex into a three-dimensional vector; Calculate the X and Z components of the three-dimensional vector in the world coordinate; Combine the calculated X and Z components into a two-dimensional vector, which represents the first perturbation sampling coordinate of the grass plant where the grass plant vertex is located.
3. The method according to claim 2, wherein Converting the grass plant vertex to the world space coordinate and selecting the corresponding coordinate component as the second perturbation sampling coordinate includes: Convert the grass plant vertex to the world space coordinate, and its X and Z coordinate components represent the second perturbation sampling coordinate of the grass plant vertex.
4. The method according to claim 1, wherein Sampling the perturbation noise map according to the first perturbation sampling coordinate and the second perturbation sampling coordinate to obtain a perturbation vector, and adjusting the vertex position of the grass plant according to the perturbation vector includes: Multiply the first perturbation sampling coordinate and the second perturbation sampling coordinate by a preset scaling factor respectively and then add them to obtain the UV coordinate of the perturbed noise map after scaling processing; Obtain the wind force UV coordinate according to the UV coordinate of the perturbed noise map and the wind force direction and wind speed that change with time; Sample the wind force noise map using the wind force UV coordinate to obtain a wind force perturbation vector; and adjust the position of the grass plant vertex according to the wind force perturbation vector.
5. The method according to claim 4, wherein Adjusting the position of the grass plant vertex according to the wind force perturbation vector includes: Multiply the wind force perturbation vector by the superposition value of the wind force intensity and the random wind force intensity to obtain the final wind force perturbation vector; adjust the position of the grass plant vertex according to the final wind force perturbation vector.
6. The method according to claim 5, wherein Adjusting the position of the grass plant vertex according to the final wind force perturbation vector includes: Calculate the reduced height value of the grass plant using a curve simulation method according to the wind force intensity; Adjust the position of the grass plant vertex according to the final wind force perturbation vector and the height value, so that the position of the bottom of the grass plant is less affected by the wind, and the position of the top is more affected by the wind.
7. The method according to claim 1, wherein Interpolating the predefined color according to the first UV coordinate to obtain the grass plant vertex color and completing the rendering of the grassland includes: Use the y component of the first UV coordinate of the vertex as a longitudinal mask, and obtain the basic gradient color of the grass plant from the root to the tip by interpolating the predefined colors of the tip, middle, and root of the grass plant.
8. The method according to claim 7, wherein, Using the y - component of the first UV coordinate of the vertex as the longitudinal mask, the basic gradient color of the grass plant from the root to the tip is obtained by interpolating the predefined grass plant color, including: Calculating the scale factor of the vertex at the entire height of the grass plant; Interpolating the basic color of the vertex according to the scale factor and the predefined grass plant tip, middle, and root colors.
9. The method according to claim 7, wherein, Interpolating the predefined color according to the first UV coordinate to obtain the grass plant vertex color. Completing the rendering of the grassland further includes: Combining the basic color and the wind influence to calculate the final vertex color; after the vertex shader calculates the color of each vertex, using the vertex color as the input of the fragment shader.
10. The method according to claim 7, wherein, Interpolating the predefined color according to the first UV coordinate to obtain the grass plant vertex color. Completing the rendering of the grassland further includes: After adjusting the lighting effect of the grass plant according to the first UV coordinate in the vertex shader, completing the shadow calculation of the grass plant; generating multiple grass plant instances and combining the eligible multiple grass plant instances into the same batch for rendering.
11. A grassland rendering device, characterized in that, Including: A loading module for obtaining a grass cluster model, where the grass cluster model is composed of n grass plants, n > 0; the grass cluster model includes a first UV coordinate and a second UV coordinate; the first UV coordinate is obtained by projecting the grass cluster model in the horizontal axis direction; the second UV coordinate is obtained by projecting the grass cluster model in the vertical axis direction and moving all the UV coordinates of each grass plant to the same UV coordinate position respectively; A calculation module for converting the second UV coordinate of the grass plant vertex to the world space in the vertex shader to obtain the first perturbation sampling coordinate; Converting the grass plant vertex to the world space coordinate and selecting the corresponding coordinate component as the second perturbation sampling coordinate; A sampling module for sampling the perturbation noise map according to the first perturbation sampling coordinate and the second perturbation sampling coordinate to obtain a perturbation vector; adjusting the vertex position of the grass plant according to the perturbation vector; A rendering module for interpolating the predefined color according to the first UV coordinate to obtain the grass plant vertex color and completing the rendering of the grassland.
12. A computing device, comprising a memory, a processor, and computer instructions stored on the memory and executable on the processor, characterized in that, When the processor executes the instructions, it implements the steps of the method according to any one of claims 1 - 10.
13. A computer-readable storage medium storing computer instructions, characterized in that, When the instructions are executed by the processor, it implements the steps of the method according to any one of claims 1 - 10.