Diamond real-time rendering method and electronic product terminal
By obtaining collision point information of the diamond model mesh through pre-rendering and combining it with the lighting model to calculate the refraction color, the computational resource and visual effect problems of real-time diamond rendering are solved, achieving a smooth rendering effect on electronic product terminals.
Patent Information
- Application Number
- CN202510997194.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-18
- Publication Date
- 2025-11-18
AI Technical Summary
Existing technologies struggle to achieve real-time rendering of diamonds on electronic devices, failing to meet the demands for computing resources and the requirements for smooth visual effects, particularly in simulating the refraction and reflection of light in diamonds.
By pre-rendering the diamond to obtain collision point information between light and the model mesh, and calling this information during real-time rendering, the surface reflection and internal refraction colors of the diamond are calculated in combination with the lighting model. The collision point data is stored using an environment cube map, reducing the amount of computation required for real-time rendering.
It achieves smooth real-time diamond rendering on electronic devices, and can run on the vast majority of mobile and electronic devices on the market, providing a near-realistic diamond effect.
Smart Images

Figure CN120976392A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of jewelry rendering, and in particular to a real-time rendering method of a diamond and an electronic product terminal. BACKGROUND
[0002] In today's digital age, the graphics rendering capabilities of electronic products are constantly improving, and people have increasingly high requirements for the visual presentation effects of virtual items. As a gem with unique charm and high value, the real-time rendering of diamonds on electronic product terminals has always been a challenging topic in the field of computer graphics.
[0003] The reason why diamonds have a fascinating and dazzling luster lies in their unique internal optical properties. Diamonds have a very high refractive index, usually around 2.417 (for 589.3 nm sodium yellow light), which causes light to be strongly refracted when it enters the diamond. Moreover, the refractive index of a diamond is not a fixed value, but varies with the wavelength of light, a phenomenon known as dispersion. Different wavelengths of light have different angles of refraction in the diamond, causing the light to separate when it exits, forming the colored light we see, which is the fire of the diamond.
[0004] In addition, there are numerous facets and edges inside a diamond. A standard round brilliant cut diamond usually has 57-58 facets, which are carefully designed to allow light to be reflected and refracted multiple times inside the diamond. After entering the diamond, the light is first refracted on the crown facets, then reflected on the pavilion facets, and then back to the crown and out. In this process, the light also interacts with other facets inside the diamond, further increasing the complexity of the light propagation path.
[0005] The complex optical properties of diamonds make the rendering process require a large amount of light calculation and geometric operation. In order to simulate the multiple propagation of light inside the diamond, a large number of light rays need to be tracked and calculated for each light ray. When rendering a frame of diamond image, tens of thousands or even more light rays may need to be tracked, and each light ray needs to be tested for intersection with the geometric model of the diamond and perform complex optical calculations. This not only consumes a large amount of computing resources, but also causes the rendering time to be too long, which cannot meet the requirements of real-time rendering.
[0006] Therefore, to realize real-time rendering of diamonds on electronic product terminals, two key technical difficulties must be overcome: one is how to accurately simulate the refraction of light inside the diamond to render a diamond effect close to reality; the second is that real-time rendering needs to meet certain performance requirements to ensure that there is no lag or delay in the rendering process, thereby providing users with a smooth visual experience. However, the existing technology has insurmountable obstacles in these two aspects, resulting in the inability to achieve ideal real-time rendering of diamonds on electronic product terminals. SUMMARY
[0007] In view of the problems in the prior art, the present application provides a real-time rendering method for diamonds, comprising:
[0008] Step S1, pre-rendering the diamond to obtain collision point information between light and each triangular facet in the model mesh corresponding to the diamond when the light is incident on the diamond from different directions and saving the information;
[0009] Step S2, during real-time rendering, calculating the indirect light color formed by the reflection of light on the surface of the diamond according to an illumination model, and tracing the light refracted into the interior of the diamond, and calling the pre-stored collision point information to calculate the internal refraction color formed by the light refracted into the interior of the diamond during the light tracing process;
[0010] Step S3, mixing the indirect light color and the internal refraction color to obtain the real-time rendering result of the diamond.
[0011] Preferably, in step S1, a stereo camera is used to render the diamond from different directions to obtain the collision point information and save it to an environment cube map;
[0012] The collision point information includes the normal vector of the collision point and the length from the collision point to the center point of the model mesh of the diamond, wherein the normal vector is stored in the rgb channel of the environment cube map, and the length is stored in the a channel of the environment cube map.
[0013] Preferably, in step S2, the illumination model is a micro-surface model.
[0014] Preferably, in step S2, the process of calculating the internal refraction color includes:
[0015] Step S21, taking the incident point of the light on the diamond as the current light starting point;
[0016] Step S22, obtaining a collision point of the light ray propagating from the current light ray start point along the current light ray direction with the inside of the diamond by collision detection, and obtaining the normal vector of the collision point from the environment cube map;
[0017] Step S23, judging whether total reflection occurs at the collision point according to the current light ray direction, the normal vector and the relative refractive index of the diamond:
[0018] If yes, turning to step S24;
[0019] If no, calculating the refraction color contribution of the refraction direction, and then turning to step S24;
[0020] Step S24, accumulating the number of light ray bounces and calculating the current accumulated light ray attenuation coefficient, and judging whether the accumulated number of light ray bounces reaches a first threshold or the current accumulated light ray attenuation coefficient is lower than a second threshold:
[0021] If no, updating the current light ray direction as the reflection direction, and updating the collision point as the current light ray start point, and then returning to step S22;
[0022] If yes, turning to step S25;
[0023] Step S25, judging whether the last light ray bounce is total reflection:
[0024] If yes, accumulating all the refraction color contributions and the reflection color contribution of the reflection direction at the last light ray bounce as the internal refraction color;
[0025] If no, accumulating all the refraction color contributions as the internal refraction color.
[0026] Preferably, step S22 comprises:
[0027] Step S221, detecting the intersection coordinates of the collision point of the light ray propagating from the current light ray start point along the current light ray direction with the inside of the diamond according to the world space coordinates of the current light ray start point and the current light ray direction;
[0028] Step S222, calculating the distance vector from the current light ray start point to the collision point and the direction vector of the collision point relative to the center point of the diamond according to the current light ray start point and the intersection coordinates;
[0029] Step S223, obtaining the normal vector of the collision point from the environment cube map according to the direction vector.
[0030] Preferably, step S23 comprises:
[0031] Step S231, calculating a refraction direction vector according to the current light ray direction, the normal vector and the relative refractive index of the diamond;
[0032] Step S231, calculating a square value of a module length of the refraction direction vector, and judging whether the square value of the module length is less than a third threshold value:
[0033] If yes, judging that total reflection occurs at the collision point;
[0034] If no, judging that total reflection does not occur at the collision point.
[0035] Preferably, the relative refractive index of the diamond is the absolute refractive index of the diamond divided by the refractive index of the external medium, and the calculation of the refraction color contribution of the refraction direction includes:
[0036] Step A1, calculating a refraction energy proportion of the refraction direction according to the refraction direction vector, the normal vector and the relative refractive index;
[0037] Step A2, calculating a cosine value of an included angle between the current light ray direction and the refraction direction, and judging whether the cosine value of the included angle is less than a transmittance threshold value and the transmittance threshold value is greater than zero:
[0038] If yes, correcting the refraction color according to a sampling result of a sampling environment map and the refraction energy proportion to obtain the refraction color contribution;
[0039] If no, perturbing the refraction direction to obtain a plurality of perturbed refraction directions, and respectively correcting the refraction color of the refraction direction and each of the perturbed refraction directions by using the environment map to obtain the refraction color contribution.
[0040] Preferably, the sampling environment map correction of the refraction color includes:
[0041] Offset processing the starting point of the current light ray according to the refraction direction and the cosine value of the included angle to obtain a sampling position;
[0042] Sampling the environment map at the sampling position to obtain a sampling result, and multiplying the sampling result by the refraction energy proportion, the current cumulative light ray attenuation coefficient, a color correction factor and an enhancement factor to obtain the refraction color contribution.
[0043] Preferably, the plurality of perturbed refraction directions includes a first perturbed refraction direction and a second perturbed refraction direction;
[0044] Wherein, the relative refractive index of the first perturbed refraction direction is obtained by dividing the absolute refractive index of the refraction direction by the refractive index of the external medium after adding a fourth threshold value;
[0045] The absolute refractive index of the refractive direction minus the fourth threshold value divided by the external medium refractive index obtains the relative refractive index of the second perturbed refractive direction.
[0046] The application further provides an electronic product terminal applying the real-time rendering method, and the electronic product terminal comprises:
[0047] a memory for storing collision point information between a light ray and each triangular facet in a model mesh corresponding to the diamond when the light ray is incident on the diamond from different directions through pre-rendering of the diamond;
[0048] a processor connected to the memory, and the processor comprises:
[0049] a ray tracing unit for calculating indirect light color formed by reflection of the light ray on the surface of the diamond according to a light model and calculating internal refraction color formed by refraction of the light ray into the diamond according to the pre-stored collision point information when real-time rendering is performed;
[0050] a color mixing unit connected to the ray tracing unit and configured to mix the indirect light color and the internal refraction color to obtain a real-time rendering result of the diamond.
[0051] The above technical solution has the following advantages or beneficial effects:
[0052] 1) By tracing the light ray refracted into the diamond, rendering the internal effect of the diamond, and mixing the external effect of the diamond formed by reflection of the light ray on the surface of the diamond, the real-time rendering result can present a diamond effect close to reality;
[0053] 2) By pre-rendering the diamond to obtain the collision point information between the light ray and each triangular facet in the model mesh corresponding to the diamond and storing the collision point information, the collision point information can be directly called in the ray tracing process, the calculation amount in real-time rendering is effectively reduced, the performance requirement of real-time rendering is met, the application can be run in most mobile terminal web pages on the market, is adapted to most electronic product terminals on the market, and can be smoothly run on mobile terminals with low performance. BRIEF DESCRIPTION OF DRAWINGS
[0054] Figure 1 In a preferred embodiment of the application, a flowchart of a real-time rendering method of a diamond is provided.
[0055] Figure 2 In a preferred embodiment of the application, a flowchart of a sub-process of step S2 is provided.
[0056] Figure 3 In a preferred embodiment of the application, a flowchart of a sub-process of step S22 is provided.
[0057] Figure 4 This is a schematic diagram of the sub-process of step S23 in a preferred embodiment of the present invention;
[0058] Figure 5 A flowchart illustrating the calculation of the refracted color contribution in the refraction direction is shown in a preferred embodiment of the present invention.
[0059] Figure 6 A schematic diagram of the process for correcting the refractive color by sampling environment mapping, which is a preferred embodiment of the present invention;
[0060] Figure 7 This is a schematic diagram of the structure of an electronic product terminal, which is a preferred embodiment of the present invention. Detailed Implementation
[0061] The present invention will now be described in detail with reference to the accompanying drawings and specific embodiments. The present invention is not limited to this embodiment; other embodiments that conform to the spirit of the present invention may also fall within the scope of the present invention.
[0062] In a preferred embodiment of the present invention, based on the above-mentioned problems existing in the prior art, a real-time rendering method for diamonds is provided, such as... Figure 1 As shown, it includes:
[0063] Step S1: Pre-render the diamond to obtain and save the collision point information between the light and each triangular facet in the model mesh corresponding to the diamond when the light enters the diamond from different directions.
[0064] Step S2: During real-time rendering, the indirect light color formed by the reflection of light on the diamond surface is calculated based on the lighting model, and the light refracted into the diamond is tracked. During the light tracing process, the pre-stored collision point information is called to calculate the internal refraction color formed by the light refracted into the diamond.
[0065] Step S3: Mix the indirect light color and the internal refraction color to obtain the real-time rendering result of the diamond.
[0066] Specifically, as is generally known, when light travels from a denser medium to a less dense medium, if the angle of incidence exceeds a certain critical angle, the refracted light will disappear, resulting only in reflection without refraction—a phenomenon known as total internal reflection. Diamonds themselves do not emit light; their dazzling appearance is due to their critical angle for total internal reflection being only approximately 24°. This means that within a diamond, light rays with an angle of incidence exceeding 24° are trapped inside, with very little escaping. Therefore, in this embodiment, ray tracing is used to cause multiple reflections of light within the diamond, rendering the internal effects of the diamond.
[0067] When ray tracing is performed, because the reflection of light inside the diamond needs to be simulated, that is, intersection with the model grid, the grid information needs to be queried in the shader in some way. The simplest way is to calculate the triangles recorded by the map. But it will cause all the faces to be traversed every time the ray intersection is performed, resulting in a very large amount of calculation, which seriously affects the performance of real-time rendering of the diamond. Based on this, in the embodiment, by pre-rendering the diamond, the ray intersection is preprocessed before the diamond rendering process starts, the collision point information between the rays and the corresponding triangular faces in the model grid of the diamond is obtained and saved, and subsequent intersection only needs to be called, which effectively reduces the calculation amount in real-time rendering, and further meets the performance requirements of real-time rendering, so that the application can run in most mobile web pages on the market, adapt to most electronic product terminals on the market, and low-performance mobile terminals can also run smoothly.
[0068] In a preferred embodiment of the application, in step S1, a stereo camera is used to render the diamond from different directions to obtain the collision point information and save it to the environment cubemap.
[0069] The collision point information includes the normal vector of the collision point and the length of the collision point to the center point of the model grid of the diamond, wherein the normal vector is stored in the rgb channel of the environment cubemap, and the length is stored in the a channel of the environment cubemap.
[0070] Specifically, in the embodiment, the collision point information is obtained by rendering the environment cubemap (i.e. the cubemap) using a stereo camera, and each pixel point in the environment cubemap represents the intersection point of the light and the triangular face. The environment cubemap is composed of 6 square texture surfaces, corresponding to the +X (right), -X (left), +Y (up), -Y (down), +Z (front), and -Z (back) directions of the cube, respectively. The texture of each face is an independent 2D image, and the pixel format is usually rgba, and the rgb channel and the a channel are used to store the normal vector and the length in the collision point information, respectively.
[0071] Further specifically, when pre-rendering, the diamond needs to be processed first to obtain the diamond bounding sphere, which is used to obtain the diamond center point and the bounding box radius, and then the stereo camera is used to render the environment map. The specific steps of rendering the environment map are as follows:
[0072] 1. Create a cube rendering target, that is, the model grid of the diamond;
[0073] 2. Initialize the cube camera, and render the model grid to the cube map to obtain the rendered cube map;
[0074] 3. Vertex shader processing:
[0075] 3.1 Transform the vertex normal of the model mesh from model space to world space and normalize it;
[0076] 3.2 Calculate the position of the vertex relative to the center of the model mesh in world space;
[0077] 3.3 Calculate the clip space vertex position, which is the final coordinate of the vertex after spatial transformation, used by the graphics rendering pipeline to determine the projection position of the vertex on the screen. The calculation formula of the clip space vertex position is as follows:
[0078] gl_Position = projectionMatrix * modelViewMatrix * offsetMatrixInv * vec4(position - offsetCenter, 1.);
[0079] gl_Position = projectionMatrix * modelViewMatrix * offsetMatrixInv * vec4(position - offsetCenter, 1.);
[0080] Where gl_Position is the clip space vertex position, and vec4(position - offsetCenter, 1.) represents the offset of the vertex position from the model space origin to offsetCenter and the conversion to homogeneous coordinates. Then apply offsetMatrixInv (the inverse matrix of the offset matrix) to correct the coordinate system, and then apply the model view matrix modelViewMatrix (i.e. model matrix modelMatrix * view matrix viewMatrix) to convert the vertex from model space to observation space (coordinate system under the camera perspective), and finally use the projection matrix projectionMatrix to convert the observation space coordinates to the clip space coordinates.
[0081] 4. Fragment shader processing:
[0082] 4.1 Get the vertex normal, vertex position and preset radius from the vertex shader, then normalize the vertex normal to ensure the correct direction;
[0083] 4.2 Map the normalized vertex normal from [-1, 1] to [0, 1] range;
[0084] 4.3 Encode the mapped vertex normal as the normal vector of the collision point as rgb, and the ratio of the position of the vertex relative to the center of the model mesh to the preset radius as the length of the collision point to the center point of the model mesh of the diamond. The value of the preset radius needs to be set reasonably according to the model size to avoid overflow of the length value in the [0, 1] range.
[0085] In real-time rendering, the rendering of the diamond mainly consists of two steps:
[0086] The first step is to render the diamond surface: calculate the color of the indirect light formed by the reflection of light on the diamond surface based on the lighting model.
[0087] The lighting model mentioned above is a micro-surface model (GGX model), used to simulate the reflection of a smooth surface and then render the diamond surface. The specific steps are as follows:
[0088] 1. Calculate the cosine of the angle between the camera's viewing angle and the normal vector.
[0089] 2. The polynomial coefficients r are calculated based on the fitting coefficients c0 and c1 of the roughness and the GGX model, where r = roughness * c0 + c1. The roughness is used to control the smoothness of the surface, and its value ranges from [0,1]. A value of 0 indicates a perfect mirror surface, and a value of 1 indicates complete diffuse reflection. The polynomial coefficients r (rx, ry, rz, rw) are a four-dimensional vector.
[0090] 3. The ambient light shading term a004 is calculated based on the cosine of the included angle and the components rx and ry of the polynomial coefficients to simulate the self-shading effect of the diamond micro-surface. The calculation formula is as follows:
[0091] a004=min(rx*rx,exp2(-9.28*dotNV))*r.x+ry;
[0092] Where dotNV is the cosine of the included angle.
[0093] 4. Calculate the ambient light contribution coefficient based on the components rz and rw of the ambient light occlusion term and polynomial coefficients. Then, combine the specular reflection color with the ambient light contribution to obtain the final specular reflection ambient light contribution (RGB color), which is the indirect light color. The calculation formula is as follows:
[0094] specularColor*AB.x+AB.y;
[0095] Where AB is the ambient light contribution coefficient vector, which is a two-dimensional component (AB.x, AB.y), and AB=vec2(-1.04,1.04)*a004+r.zw; specularColor is the specular reflection color of the diamond, which is related to the material's F0 or basic reflectivity.
[0096] Second step is the refraction of light inside the diamond: in the light tracing, the calculation of the light ray that intersects only its own model grid after reflection or refraction, exits the diamond surface, and samples the environment map for color dispersion: different wavelengths of light have different refractive indices after passing through the diamond, so different dispersion intensities are assigned to RGB three colors of light, and a certain offset is performed according to the dispersion intensity after each reflection. Light absorption: refracted light propagates inside the transparent object, and after passing through a certain thickness, the medium absorbs part of the light energy, so the medium will show color tendency, so the light transmission ratio is calculated by the propagation distance after each reflection, and the light color and medium color are mixed.
[0097] Further specifically, in step S2, as shown in Figure 2 , the process of calculating the internal refraction color includes:
[0098] Step S21, taking the incident point of the light on the diamond as the current light starting point;
[0099] Step S22, obtaining the collision point of the light propagating from the current light starting point in the current light direction inside the diamond through collision detection, and obtaining the normal vector of the collision point from the environment cube map;
[0100] Step S23, judging whether total reflection occurs at the collision point according to the current light direction, the normal vector and the relative refractive index of the diamond:
[0101] If yes, turn to step S24;
[0102] If no, calculate the refraction color contribution of the refraction direction, and then turn to step S24;
[0103] Step S24, accumulate the number of light bounces and calculate the current cumulative light attenuation coefficient, and judge whether the accumulated number of light bounces reaches the first threshold or the current cumulative light attenuation coefficient is lower than the second threshold:
[0104] If no, update the current light direction to the reflection direction, update the collision point to the current light starting point, and then return to step S22;
[0105] If yes, turn to step S25;
[0106] Step S25, judging whether the last light bounce is total reflection:
[0107] If yes, accumulate all refraction color contributions and the reflection color contribution of the reflection direction at the last light bounce as the internal refraction color;
[0108] If no, accumulate all refraction color contributions as the internal refraction color.
[0109] Specifically, in the embodiment, the light inside the diamond needs to be simulated to pass through the surface of the diamond by refraction, propagate along the inside of the diamond, and collide with the surface of the diamond (from inside to outside), where the light needs to be refracted and reflected again. The reflected light is determined according to the original process until the set number of light reflections is reached. Finally, the results of each collision are superimposed in proportion, that is, the internal refraction color is obtained.
[0110] Further specifically, each refraction and reflection determination process includes, in sequence, light collision detection, normal vector processing, light attenuation calculation, coordinate space conversion, refraction direction calculation, and total reflection judgment.
[0111] Branch one: if total reflection occurs, the new direction is calculated, and the above refraction and reflection determination process is repeated. If it is the last bounce, the reflection color contribution of the reflection direction at the last light bounce is calculated and superimposed on the final color.
[0112] Branch two: if total reflection does not occur, that is, there is refraction, the refraction direction is calculated, and then the above refraction and reflection determination process is repeated, and the refraction color contribution is accumulated.
[0113] In the above process, the light collision detection is performed by step S22. In the preferred embodiment of the present application, as shown in Figure 3 , step S22 includes:
[0114] Step S221, according to the world space coordinates of the current light starting point and the current light direction, detecting the intersection coordinates of the collision point between the current light starting point and the current light direction.
[0115] Step S222, calculating the distance vector from the current light starting point to the collision point and the direction vector of the collision point relative to the center point of the diamond according to the current light starting point and the intersection coordinates.
[0116] Step S223, obtaining the normal vector of the collision point from the environment cube map according to the direction vector.
[0117] Specifically, in the embodiment, after obtaining the normal vector of the collision point from the environment cube map, a normal vector processing process is further included, which is specifically:
[0118] The normal vector obtained from the environment cube map is converted, that is, the normal vector in the range of [0, 1] is mapped to the range of [-1, 1], and then the normal vector is reversed and normalized.
[0119] In the above light attenuation calculation process, the light attenuation is absorption attenuation and is successive attenuation. The calculation formula of the current cumulative light attenuation coefficient attenuationFactor is as follows:
[0120] attenuationFactor* = exp(-r*(1.-color));
[0121] r = length(dist) / radius * absorptionFactor;
[0122] where dist represents a distance vector, radius represents a diamond radius, absorptionFactor represents an absorption factor, r represents an attenuation factor, based on which an overall attenuation factor, i.e. a current accumulated light ray attenuation factor attenuationFactor, is updated by using an exponential attenuation formula, color represents a material base RGB color.
[0123] The above coordinate space conversion procedure is to convert the collision point to a model space coordinate system, so as to facilitate subsequent texture sampling, and also includes updating the current light ray starting point as the current intersection point.
[0124] The above refraction direction calculation procedure and total reflection judgment procedure are executed by step S23, and in the preferred embodiment of the present application, as shown in Figure 4 step S23 includes:
[0125] Step S231, a refraction direction vector is calculated according to the current light ray direction, the normal vector and the relative refractive index of the diamond;
[0126] Step S232, the square value of the module length of the refraction direction vector is calculated, and it is judged whether the square value of the module length is less than a third threshold value:
[0127] If yes, it is judged that the collision point has total reflection;
[0128] If no, it is judged that the collision point has no total reflection.
[0129] Specifically, in the present embodiment, the relative refractive index of the diamond is the ratio of the refractive index of the diamond to the refractive index of the external environment. The third threshold value is preferably a very small value. When the square value of the module length of the refraction direction vector is less than the third threshold value, it means that the refraction direction vector is close to a zero vector, indicating that the incident angle is greater than the critical angle, the light ray is not successfully refracted, the refracted light ray does not exist, the refraction direction is invalid, i.e. the collision point has total reflection, and here the refraction direction vector is essentially a reflection direction vector. By detecting whether the refraction direction is a zero vector, the problem of directly comparing floating point precision is avoided.
[0130] If total reflection occurs, the reflection direction and the reflection energy proportion are calculated, and the current accumulated light attenuation coefficient is multiplied by the reflection energy proportion to update the attenuation coefficient, and then the bounce number is accumulated once. Wherein, the reflection direction is calculated according to the incident direction and the normal direction, and the incident direction and the normal direction need to be aligned in the same coordinate space (world space or model space), otherwise the matrix transformation is needed to align.
[0131] When the square value of the modulus of the refraction direction vector is less than the third threshold value, it indicates that the refraction direction is valid, that is, the collision point is not totally reflected, at this time, as shown in Figure 5 The refraction color contribution of the refraction direction is calculated, including:
[0132] Step A1, the refraction energy proportion of the refraction direction is calculated according to the refraction direction vector, the normal vector and the relative refractive index;
[0133] Step A2, the cosine value of the included angle between the current light direction and the refraction direction is calculated, and it is judged whether the cosine value of the included angle is less than the transmittance threshold value and the transmittance threshold value is greater than zero:
[0134] If yes, the refraction color is corrected according to the sampling result of the sampling environment map and the refraction energy proportion to obtain the refraction color contribution;
[0135] If no, the refraction direction is disturbed to obtain a plurality of disturbed refraction directions, and the refraction direction and each disturbed refraction direction is respectively corrected by the environment map to obtain the refraction color contribution.
[0136] Wherein, the refraction energy proportion is equal to 1 minus the reflection energy proportion, then the refraction direction is transformed to the model space, and the transmittance threshold value is calculated, and then it is judged whether the projection condition is met based on the transmittance threshold value, if the projection condition is met, the environment map is sampled and the color is accumulated, otherwise, a plurality of disturbed light paths are generated and sampled, finally the reflection direction and the attenuation coefficient are updated for the next bounce. If the bounce number reaches the first threshold value or the current accumulated light attenuation coefficient is lower than the second threshold value, the light ray tracing is stopped, otherwise the light ray tracing is continued. Wherein, the transmittance threshold value is the cosine value of the included angle between the current light direction and the refraction direction.
[0137] Further, in the preferred embodiment of the present application, as shown in Figure 6 The sampling environment map corrects the refraction color, including:
[0138] Step B1, offset processing is performed on the current light starting point according to the refraction direction and the included angle cosine value to obtain a sampling position;
[0139] Step B2, the environment map is sampled at the sampling position to obtain a sampling result, and the sampling result is multiplied by the refraction energy proportion, the current accumulated light attenuation coefficient, the color correction factor and the enhancement factor to obtain the refraction color contribution.
[0140] Wherein, the sampling position is the coordinate value of the current light ray starting point plus 0.5*the normalized vector of the refraction direction in the model space*the cosine value of the included angle, and the current light ray starting point is offset to simulate the transmission effect.
[0141] In the preferred embodiment of the present application, the plurality of perturbed refraction directions includes a first perturbed refraction direction and a second perturbed refraction direction.
[0142] Wherein, the absolute refractive index of the refraction direction plus the fourth threshold value is divided by the external medium refractive index to obtain the relative refractive index of the first perturbed refraction direction.
[0143] The absolute refractive index of the refraction direction minus the fourth threshold value is divided by the external medium refractive index to obtain the relative refractive index of the second perturbed refraction direction.
[0144] Wherein, the external medium is usually air, and the corresponding external medium refractive index is 1. By generating three light paths to enhance the details and dispersion effect of environment map sampling, and then enhance the visual effect, the three light paths are:
[0145] dir0: original refraction direction (world space);
[0146] First perturbed refraction direction dir1: direction after increasing refractive index (simulate dispersion effect);
[0147] Second perturbed refraction direction dir2: direction after reducing refractive index (simulate dispersion effect).
[0148] Also includes transforming the first perturbed refraction direction dir1 and the second perturbed refraction direction dir2 to the model space for environment map sampling.
[0149] Then, the dir1 (R channel), dir0 (G channel), and dir2 (B channel) are respectively sampled in the channel to capture the difference in refraction path of different wavelengths (colors), simulate the dispersion effect, and multiply the sampling result by the refraction energy proportion, the current cumulative light attenuation coefficient, the color correction factor, and the enhancement factor to obtain the refraction color contribution. The refraction energy proportion and the current cumulative light attenuation coefficient ensure that the energy proportion of reflection and refraction is reasonable, avoiding energy explosion or loss.
[0150] Finally, it is also necessary to update the reflection direction and the current cumulative light attenuation coefficient for the next bounce.
[0151] As a preferred embodiment, the idea of obtaining the collision point information by using the environment map is to first obtain a rough collision point range by the intersection of the ray and the bounding sphere of the diamond, and then obtain another collision point with the plane by the intersection of the ray and the plane again. The following process is similar to the above, as long as the bounding sphere collides once and the plane collides twice, the range is gradually reduced until the collision point of the ray and the surface of the diamond is obtained.
[0152] The specific steps are as follows:
[0153] 1. Calculate the first collision of the ray and the base sphere
[0154] According to the current starting point coordinates and the direction vector of the ray, the first intersection point coordinates of the ray and the sphere, i.e. the first collision point coordinates, are calculated.
[0155] 2. Calculate the unit direction vector from the sphere center to the first collision point
[0156] According to the first collision point coordinates and the preset sphere center offset, the unit vector from the sphere center to the collision point is calculated.
[0157] 3. Obtain the diamond surface initial intersection point data
[0158] According to the unit vector, the diamond surface data in the environment cube map is queried to obtain the normal vector and the distance parameter (length).
[0159] 4. Calculate the diamond surface initial actual intersection point
[0160] According to the length and the diamond model radius, the actual distance value and the diamond surface initial intersection point coordinates are obtained.
[0161] 5. Obtain the diamond surface initial normal vector
[0162] According to the diamond surface data processing, the diamond surface initial normal vector is obtained.
[0163] 6. Calculate the second collision of the ray and the diamond surface
[0164] According to the original ray starting point, the original ray direction, the diamond surface initial intersection point coordinates output by step 4 and the diamond surface initial normal vector output by step 5, the intersection point coordinates of the second collision of the ray and the diamond surface are calculated.
[0165] 7. Calculate the unit direction vector after the second collision
[0166] According to the intersection point coordinates output by step 6, the new unit direction vector is calculated.
[0167] 8、Get the second intersection point of the diamond surface
[0168] According to the new unit direction vector output in step 7, the diamond surface data in the environment cube map is queried to obtain the normal vector and distance parameter (length) of the second collision.
[0169] 9、Calculate the second actual intersection point of the diamond surface
[0170] According to the distance parameter of the second collision, the second actual distance value and the second intersection point coordinate of the diamond surface are calculated.
[0171] 10、Get the final surface normal vector
[0172] According to the normal vector and distance parameter (length) of the second collision output in step 8, the final surface normal vector is obtained.
[0173] 11、Calculate the final collision of the ray and the diamond surface
[0174] According to the original ray starting point, the original ray direction, the second surface intersection point output in step 9, and the final normal vector output in step 10, the linePlaneIntersect function is called to calculate the final effective collision point.
[0175] The application also provides an electronic product terminal applying the real-time rendering method, as shown in the figure, the electronic product terminal comprises: Figure 7
[0176] The memory 1 is used to save the collision point information between the rays and the corresponding triangular patches in the model mesh of the diamond when the rays are shot into the diamond from different directions by pre-rendering the diamond;
[0177] The processor 2 is connected with the memory 1, and the processor 2 comprises:
[0178] The ray tracing unit 21 is used to calculate the indirect light color formed by the reflection of the rays on the diamond surface according to the illumination model when performing real-time rendering, and calculate the internal refraction color formed by the refraction of the rays into the diamond interior by calling the pre-stored collision point information;
[0179] The color mixing unit 22 is connected with the ray tracing unit 21, and is used to mix the indirect light color and the internal refraction color to obtain the real-time rendering result of the diamond.
[0180] The above only describes the preferred embodiments of the application, and does not limit the implementation and protection scope of the application. For those skilled in the art, it should be realized that any equivalent replacement and obvious changes made according to the content of the specification and drawings should be included in the protection scope of the application.
Claims
1. A real-time rendering method for diamonds, characterized in that, include: Step S1: Pre-render the diamond to obtain and save the collision point information between the light rays and each triangular facet in the model mesh corresponding to the diamond when the light rays enter the diamond from different directions. Step S2: During real-time rendering, the indirect light color formed by the reflection of light on the diamond surface is calculated based on the lighting model, and the light refracted into the diamond is tracked. During the light tracing process, the pre-stored collision point information is called to calculate the internal refraction color formed by the light refracted into the diamond. Step S3: Mix the indirect light color and the internal refractive color to obtain the real-time rendering result of the diamond.
2. The real-time rendering method according to claim 1, characterized in that, In step S1, a stereo camera is used to render the diamond from different directions to obtain the collision point information and save it to the environment cube map. The collision point information includes the normal vector of the collision point and the length from the collision point to the center point of the model mesh of the diamond, wherein the normal vector is stored in the RGB channel of the environment cube map and the length is stored in the A channel of the environment cube map.
3. The real-time rendering method according to claim 1, characterized in that, In step S2, the illumination model is a micro-surface model.
4. The real-time rendering method according to claim 2, characterized in that, In step S2, the process of calculating the internal refractive color includes: Step S21: Take the point of incidence of the light on the diamond as the current starting point of the light; Step S22: Obtain the collision point between the light ray and the interior of the diamond after the light ray propagates from the current light ray origin along the current light ray direction through collision detection, and obtain the normal vector of the collision point from the environment cube map; Step S23: Determine whether total internal reflection occurs at the collision point based on the current ray direction, the normal vector, and the relative refractive index of the diamond. If so, proceed to step S24; If not, calculate the refracted color contribution in the refraction direction, and then proceed to step S24; Step S24: Accumulate the number of light bounces and calculate the current accumulated light attenuation coefficient, and determine whether the accumulated number of light bounces reaches a first threshold or the current accumulated light attenuation coefficient is lower than a second threshold. If not, update the current ray direction to the reflection direction, update the collision point to the current ray origin, and then return to step S22; If so, proceed to step S25; Step S25, determine whether the last light bounce was a total internal reflection: If so, then the internal refractive color is obtained by summing all the refractive color contributions and the reflection color contribution in the reflection direction at the last ray bounce; If not, then all the said refractive color contributions are summed as the said internal refractive color.
5. The real-time rendering method according to claim 4, characterized in that, Step S22 includes: Step S221: Detect the coordinates of the intersection point between the light source and the interior of the diamond after the light propagates from the current light source along the current light direction, based on the world space coordinates of the current light source and the current light direction. Step S222: Calculate the distance vector from the current ray starting point to the collision point and the direction vector of the collision point relative to the center point of the diamond based on the coordinates of the current ray starting point and the intersection point. Step S223: Obtain the normal vector of the collision point from the environment cube map based on the direction vector.
6. The real-time rendering method according to claim 4, characterized in that, Step S23 includes: Step S231: Calculate the refraction direction vector based on the current light direction, the normal vector, and the relative refractive index of the diamond; Step S232: Calculate the squared value of the magnitude of the refraction direction vector, and determine whether the squared value of the magnitude is less than a third threshold. If so, then it is determined that total internal reflection occurred at the collision point; If not, then it is determined that total internal reflection did not occur at the collision point.
7. The real-time rendering method according to claim 2, characterized in that, The relative refractive index of the diamond is the absolute refractive index of the diamond divided by the refractive index of the external medium, and the contribution of the refractive color in the calculation of the refraction direction includes: Step A1: Calculate the proportion of refracted energy in the refraction direction based on the refraction direction vector, the normal vector, and the relative refractive index; Step A2: Calculate the cosine of the angle between the current ray direction and the refraction direction, and determine whether the cosine of the angle is less than the transmittance threshold and whether the transmittance threshold is greater than zero. If so, the refractive color is corrected based on the sampling results of the sampling environment map and the refractive energy ratio to obtain the refractive color contribution; If not, the refraction direction is perturbed to obtain multiple perturbed refraction directions, and the refraction color is corrected by applying an environment map to the refraction direction and each of the perturbed refraction directions to obtain the refraction color contribution.
8. The real-time rendering method according to claim 7, characterized in that, The sampling environment texture correction for refraction color includes: The current ray starting point is offset according to the refraction direction and the cosine value of the included angle to obtain the sampling position; The sampling result is obtained by sampling the environment map at the sampling location, and the sampling result is multiplied by the refractive energy ratio, the current cumulative light attenuation coefficient, the color correction factor and the enhancement factor to obtain the refractive color contribution.
9. The real-time rendering method according to claim 7, characterized in that, Multiple perturbation refraction directions include a first perturbation refraction direction and a second perturbation refraction direction; The relative refractive index of the first perturbation refractive direction is obtained by adding the absolute refractive index of the refractive direction to the fourth threshold and dividing it by the refractive index of the external medium. The relative refractive index of the second perturbation refractive direction is obtained by subtracting the fourth threshold from the absolute refractive index of the refractive direction and dividing by the refractive index of the external medium.
10. An electronic product terminal, characterized in that, The electronic product terminal, employing the real-time rendering method as described in any one of claims 1-9, comprises: The memory is used to store the collision point information between the light rays obtained by pre-rendering the diamond and each triangular facet in the model mesh corresponding to the diamond when the light rays are incident on the diamond from different directions. A processor, connected to the memory, the processor comprising: The ray tracing unit is used to calculate the indirect light color formed by the reflection of light on the diamond surface according to the lighting model during real-time rendering, and to trace the light rays refracted into the diamond. During the ray tracing process, the pre-stored collision point information is called to calculate the internal refraction color formed by the light rays refracted into the diamond. The color mixing unit, connected to the ray tracing unit, is used to mix the indirect light color and the internal refraction color to obtain the real-time rendering result of the diamond.