A method, system and storage medium for generating lightning effects based on audio in Unity
Patent Information
- Application Number
- CN202311649468.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-01
- Publication Date
- 2026-09-29
- Estimated Expiration
- 2043-12-01
AI Technical Summary
[0005]本发明的主要目的在于提供了一种Unity下基于音频生成闪电效果的方法、系统及存储介质,旨在解决现有的Unity下闪电效果生成方法操作不够简便的技术问题
[0017](1)与现有技术相比,本发明通过对音频数据进行采样并转换为闪电效果的曲线信息,使得曲线能够更好地表现音频的特征,将转换后得到的曲线信息应用在闪电效果上,可以使得闪电效果贴合音频整体表现,更加逼真;并且,通过将曲线信息对应的初始的配置数据进行调整,以得到至少一种不同闪电效果的新曲线信息以及对应的配置数据并进行存储,后续直接调用对应的配置数据,就能实现不同的闪电效果,若需要实现更多不同的闪电效果,用户只需要对配置数据进行调整,不仅操作简便,而且降低了后续的维护成本和开发成本;
Smart Images

Figure CN117788691B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method, system, and storage medium for generating lightning effects based on audio in Unity. Background Technology
[0002] Unity is a real-time 3D interactive content creation and operation platform. Creators across industries, including game development, art, architecture, automotive design, and film, use Unity to bring their ideas to life. The Unity platform provides a complete suite of software solutions for creating, operating, and monetizing any real-time interactive 2D and 3D content.
[0003] Currently, in the creation of 3D battle scenes in Unity, weather effects (such as lightning effects) are often added to enhance the combat experience. Although Unity has weather system plugins that allow users to create the desired environmental effects, users need to learn how to use these plugins before attempting to generate lightning effects, which requires a significant investment of time and effort.
[0004] Therefore, the existing methods for generating lightning effects in Unity are not simple enough to operate. Summary of the Invention
[0005] The main objective of this invention is to provide a method, system, and storage medium for generating lightning effects based on audio in Unity, aiming to solve the technical problem that existing methods for generating lightning effects in Unity are not simple enough to operate.
[0006] To achieve the above objectives, this invention provides a method for generating lightning effects based on audio in Unity, comprising the following steps: acquiring audio data of a lightning effect; sampling the audio data and converting it into curve information of the lightning effect according to a preset sampling frequency and sampling threshold; adjusting the initial configuration data corresponding to the curve information to obtain new curve information of at least one different lightning effect and corresponding configuration data, and storing them; the configuration data includes at least: scene and character light colors, scene and character light intensities, corresponding audio, duration, and curve index; listening to whether the weather in the current scene is rainy during the frame update of the script, and if so, randomly calling the configuration data of at least one new curve information to generate a new lightning effect.
[0007] Optionally, the preset sampling frequency is 400-1000Hz, and the preset sampling threshold is 0.002-0.01Hz.
[0008] Optionally, based on a preset sampling frequency and sampling threshold, the audio data is sampled and converted into lightning effect curve information, including at least the following steps: constructing an audio data array of length N, where N = D * C, D is the audio sample data, and C is the audio channel; calling the AudioClip.GetData function in Unity to fill the audio data array with audio information; calculating the number of samples F based on the audio sample data, audio channels, and preset sampling frequency; where, f is the preset sampling frequency; the audio data is cyclically sampled according to the preset loop range and preset increment step size. In each loop, the key of the curve information is obtained based on the current loop value Z and the number of samples F, and the value of the curve information is obtained based on the current loop value Z and the preset sampling frequency; the curve value corresponding to each loop sampling is obtained based on the key and value of the curve information; where... value = Z * f; Normalize the curve values that need to be recorded, and record the corresponding curve values after normalization.
[0009] Optionally, before normalizing the curve values that need to be recorded, the following steps are also included: calculating the absolute value according to the first preset formula, the first preset formula being: absolute value = |curve value corresponding to the current cycle sampling – curve value corresponding to the previous cycle sampling|; determining whether the curve value needs to be recorded according to the preset sampling threshold. If the absolute value is greater than the preset sampling threshold, the curve value needs to be recorded; if the absolute value is less than or equal to the preset sampling threshold, the curve value does not need to be recorded.
[0010] Optionally, the preset loop range is [0, F], and the preset increment step size is 1.
[0011] Optionally, in the configuration data, the scene and character light colors and the scene and character light intensities are the performance values when the lightning effect is strongest, that is, the maximum values of the scene and character light colors and the maximum values of the scene and character light intensities; the curve index is specifically: the index of the curve information corresponding to different lightning effects in the same array under the same scene.
[0012] Optionally, when generating a new lightning effect, the running time of the lightning effect is recorded; based on the running time and duration, the curve information is sampled to obtain the current light intensity value; the first light intensity value is calculated according to the second preset formula, and the first light color value is calculated according to the third preset formula. The second preset formula is: first light intensity value = adjusted scene and character light intensity * current light intensity value; the third preset formula is: first light color value = adjusted scene and character light color * current light intensity value; based on the first light intensity value, the maximum adjusted character light intensity value, the first light color value, and the maximum adjusted scene and character light color value, the lights are changed to generate a new lightning effect.
[0013] Optionally, the lightning effect ends when its duration is greater than or equal to its running time.
[0014] Corresponding to the method for generating lightning effects based on audio in Unity, this invention provides a system for generating lightning effects based on audio in Unity, comprising: an audio data acquisition module for acquiring audio data of a lightning effect; a conversion module for sampling the audio data and converting it into curve information of a lightning effect according to a preset sampling frequency and sampling threshold; a storage module for adjusting the initial configuration data corresponding to the curve information to obtain new curve information of at least one different lightning effect and corresponding configuration data, and storing them; the configuration data includes at least: scene and character light colors, scene and character light intensities, corresponding audio, duration, and curve index; and a monitoring module for monitoring whether the weather in the current scene is rainy during the frame update of the script, and if so, randomly calling the configuration data of at least one new curve information to generate a new lightning effect.
[0015] Furthermore, to achieve the above objectives, the present invention also provides a computer-readable storage medium storing a Unity program for generating lightning effects based on audio, wherein when the Unity program for generating lightning effects based on audio is executed by a processor, it implements the steps of the Unity method for generating lightning effects based on audio as described above.
[0016] The beneficial effects of this invention are:
[0017] (1) Compared with the prior art, the present invention samples the audio data and converts it into curve information of the lightning effect, so that the curve can better represent the characteristics of the audio. Applying the converted curve information to the lightning effect can make the lightning effect fit the overall audio performance and be more realistic. Furthermore, by adjusting the initial configuration data corresponding to the curve information, at least one new curve information of different lightning effects and the corresponding configuration data can be obtained and stored. In the future, the corresponding configuration data can be directly called to realize different lightning effects. If more different lightning effects are needed, the user only needs to adjust the configuration data. This not only makes the operation simple, but also reduces the subsequent maintenance and development costs.
[0018] (2) Compared with the prior art, the present invention can reduce key frames by determining whether the curve value needs to be recorded according to the preset sampling threshold.
[0019] (3) Compared with the prior art, the present invention generates a new lightning effect by changing the light according to the first light intensity value, the maximum value of the adjusted character light intensity, the first light color value and the maximum value of the adjusted scene and character light colors, making the lightning effect generation method in Unity simpler and more efficient. Attached Figure Description
[0020] The accompanying drawings, which are included to provide a further understanding of the invention and form part of this invention, illustrate exemplary embodiments of the invention and are used to explain the invention, but do not constitute an undue limitation of the invention. In the drawings:
[0021] Figure 1 This is a simplified flowchart of an embodiment of the method for generating lightning effects based on audio in Unity according to the present invention;
[0022] Figure 2 These are two example curve diagrams illustrating the method for generating lightning effects based on audio in Unity according to this invention.
[0023] Figure 3 This is a framework diagram of an embodiment of the system for generating lightning effects based on audio in Unity according to the present invention. Detailed Implementation
[0024] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0025] like Figure 1 As shown, the present invention provides a method for generating lightning effects based on audio in Unity, comprising the following steps: acquiring audio data of a lightning effect; sampling the audio data and converting it into curve information of the lightning effect according to a preset sampling frequency and sampling threshold; adjusting the initial configuration data corresponding to the curve information to obtain at least one new curve information of different lightning effects and corresponding configuration data, and storing them; the configuration data includes at least: scene and character light colors, scene and character light intensities, corresponding audio, duration, and curve index; in the frame update of the script, listening to whether the weather in the current scene is rainy, if so, randomly calling the configuration data of at least one new curve information to generate a new lightning effect.
[0026] Understandably, if multiple new lightning effects are required, the configuration data for at least two new curves can be randomly invoked.
[0027] Preferably, in this embodiment, the configuration data is stored in a table for easy user management and adjustment. Furthermore, it is preferable to use the Update function in Unity to monitor whether the weather in the current scene is rainy during the script's frame update.
[0028] This invention samples audio data and converts it into a lightning effect curve, allowing the curve to better represent the characteristics of the audio. The louder the thunder in the audio data, the greater the fluctuation of the converted curve (see reference). Figure 2 -A curve example diagram), this curve can simulate the effect of violent lightning and large changes in light intensity; if the thunder in the audio data is a booming sound, the converted curve will resemble a wavy line (see reference). Figure 2-B curve example diagram), this curve can simulate the effect of lightning flashing in bursts; applying the converted curve information to the lightning effect can make the lightning effect fit the overall audio performance, making it more realistic; furthermore, by adjusting the initial configuration data corresponding to the curve information, at least one new curve information and corresponding configuration data for different lightning effects can be obtained and stored. Subsequently, the corresponding configuration data can be directly called to achieve different lightning effects. If more different lightning effects are needed, users only need to adjust the configuration data, reducing subsequent maintenance and development costs.
[0029] Preferably, the preset sampling frequency is 400-1000Hz, and the preset sampling threshold is 0.002-0.01Hz. A higher preset sampling frequency results in a curve that more closely approximates the audio spectrum. The preset sampling threshold can filter out some minor fluctuations.
[0030] In this embodiment, the audio data is sampled and converted into lightning effect curve information according to a preset sampling frequency and sampling threshold, including at least the following steps: constructing an audio data array of length N, where N = D * C, D is the audio sample data, and C is the audio channel; calling the AudioClip.GetData function in Unity to fill the audio data array with audio information; calculating the number of samples F according to the audio sample data, audio channels, and preset sampling frequency; wherein, f is the preset sampling frequency; the audio data is cyclically sampled according to the preset loop range and preset increment step size. In each loop, the key of the curve information is obtained based on the current loop value Z and the number of samples F, and the value of the curve information is obtained based on the current loop value Z and the preset sampling frequency; the curve value corresponding to each loop sampling is obtained based on the key and value of the curve information; where... value = Z * f; Normalize the curve values that need to be recorded, and record the corresponding curve values after normalization.
[0031] Preferably, the curve values that need to be recorded are normalized, that is, the normalized curve value = (curve value * 0.5) + 0.5.
[0032] In this embodiment, the AudioClip.GetData function in Unity is called to fill the audio data array with audio information. Specifically, after the audio data is imported into Unity, Unity will generate an AudioClip resource, which stores the audio information (i.e., the audio spectrum value). The corresponding audio information can be obtained through the AudioClip.GetData interface.
[0033] In this embodiment, before normalizing the curve values that need to be recorded, the following steps are also included: calculating the absolute value according to a first preset formula, the first preset formula being: absolute value = |curve value corresponding to the current cycle sampling – curve value corresponding to the previous cycle sampling|; determining whether the curve value needs to be recorded according to a preset sampling threshold, if the absolute value is greater than the preset sampling threshold, the curve value needs to be recorded, if the absolute value is less than or equal to the preset sampling threshold, the curve value does not need to be recorded.
[0034] This invention reduces keyframes by determining whether curve values need to be recorded based on a preset sampling threshold.
[0035] Preferably, the preset loop range is [0, F], and the preset increment step size is 1.
[0036] In this embodiment, the scene and character light colors and scene and character light intensities in the configuration data are the performance values when the lightning effect is strongest, that is, the maximum values of scene and character light colors and the maximum values of scene and character light intensities; the curve index is specifically: the index of the curve information corresponding to different lightning effects in the same array under the same scene.
[0037] In this embodiment, when generating a new lightning effect, the running time of the lightning effect is recorded; based on the running time and duration, the curve information is sampled to obtain the current light intensity value; the first light intensity value is calculated according to a second preset formula, and the first light color value is calculated according to a third preset formula. The second preset formula is: first light intensity value = adjusted scene and character light intensity * current light intensity value; the third preset formula is: first light color value = adjusted scene and character light color * current light intensity value; based on the first light intensity value, the maximum adjusted character light intensity value, the first light color value, and the maximum adjusted scene and character light color value, the light is changed to generate a new lightning effect.
[0038] This invention generates a new lightning effect by changing the light according to the first light intensity value, the adjusted maximum value of the character light intensity, the first light color value, and the adjusted maximum values of the scene and character light colors. This makes the lightning effect generation method in Unity simpler and more efficient.
[0039] In this embodiment, the lightning effect ends when the running time of the lightning effect is greater than or equal to the duration.
[0040] like Figure 3As shown, the present invention also provides a system for generating lightning effects based on audio in Unity, comprising: an audio data acquisition module 10 for acquiring audio data of a lightning effect; a conversion module 20 for sampling the audio data and converting it into curve information of a lightning effect according to a preset sampling frequency and sampling threshold; a storage module 30 for adjusting the initial configuration data corresponding to the curve information to obtain new curve information of at least one different lightning effect and corresponding configuration data and storing them; the configuration data includes at least: scene and character light colors, scene and character light intensities, corresponding audio, duration, and curve index; and a monitoring module 40 for monitoring whether the weather in the current scene is rainy during the frame update of the script, and if so, randomly calling the configuration data of at least one new curve information to generate a new lightning effect.
[0041] This invention also provides a computer-readable storage medium, which may be a computer-readable storage medium included in the memory described in the above embodiments; or it may be a standalone computer-readable storage medium not assembled into a device. The computer-readable storage medium stores at least one instruction, which is loaded and executed by a processor to implement... Figure 1 The method shown is for generating lightning effects based on audio in Unity. The computer-readable storage medium can be a read-only memory, a disk, or an optical disk, etc.
[0042] It should be noted that the various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the device embodiments, equipment embodiments, and storage medium embodiments, since they are basically similar to the method embodiments, the descriptions are relatively simple, and relevant parts can be referred to the descriptions of the method embodiments.
[0043] Furthermore, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0044] The foregoing description illustrates and describes preferred embodiments of the present invention. It should be understood that the present invention is not limited to the forms disclosed herein and should not be construed as excluding other embodiments. It can be used in various other combinations, modifications, and environments, and can be altered within the scope of the inventive concept by means of the foregoing teachings or techniques or knowledge in related fields. Any modifications and variations made by those skilled in the art that do not depart from the spirit and scope of the present invention should be within the protection scope of the appended claims.
Claims
1. A method for generating lightning effects based on audio in Unity, characterized in that, Includes the following steps: Get audio data with a lightning effect; Based on the preset sampling frequency and sampling threshold, the audio data is sampled and converted into curve information with a lightning effect; The initial configuration data corresponding to the curve information is adjusted to obtain new curve information and corresponding configuration data for at least one different lightning effect, and then stored. The configuration data includes at least: scene and character light colors, scene and character light intensities, corresponding audio, duration, and curve index. During the frame update of the script, listen for whether the weather in the current scene is rainy. If so, randomly call the configuration data of at least one new curve information to generate a new lightning effect. When a new lightning effect is generated, the runtime of the lightning effect is recorded; Based on the running time and duration, the curve information is sampled to obtain the light intensity value at the current time; The first light intensity value is calculated according to the second preset formula, and the first light color value is calculated according to the third preset formula. The second preset formula is as follows: First light intensity value = Adjusted scene and character light intensity The current light intensity value, the third preset formula is as follows: First light color value = adjusted scene and character light colors Current light intensity value; Based on the first light intensity value, the adjusted scene and character light intensity, the first light color value, and the adjusted scene and character light colors, the light is changed to generate a new lightning effect; In the configuration data, the scene and character light colors and the scene and character light intensities are the performance values when the lightning effect is at its strongest, that is, the maximum values of the scene and character light colors and the maximum values of the scene and character light intensities; The curve index is specifically the index of the curve information corresponding to different lightning effects in the same scene within the same array.
2. The method for generating lightning effects based on audio in Unity according to claim 1, characterized in that: The preset sampling frequency is 400-1000Hz, and the preset sampling threshold is 0.002-0.01Hz.
3. The method for generating lightning effects based on audio in Unity according to claim 1, characterized in that: Based on a preset sampling frequency and sampling threshold, the audio data is sampled and converted into lightning effect curve information, including at least the following steps: Construct an audio data array of length N, where N=D C and D represent audio sampling data, and C represents the audio channel. Call the AudioClip.GetData function in Unity to populate the audio data array with audio information; Based on the audio sampling data, audio channels, and preset sampling frequency, calculate the number of samples F; where... f is the preset sampling frequency; Based on the preset loop range and preset increment step size, the audio data is sampled cyclically. In each loop, the key of the curve information is obtained based on the current loop value Z and the number of samplings F, and the value of the curve information is obtained based on the current loop value Z and the preset sampling frequency. The curve value corresponding to each cyclic sampling is obtained based on the key and value of the curve information; where... , ; Normalize the curve values that need to be recorded, and record the corresponding curve values after normalization.
4. The method for generating lightning effects based on audio in Unity according to claim 3, characterized in that: Before normalizing the curve values that need to be recorded, the following steps are also included: The absolute value is calculated according to the first preset formula, which is: Absolute value = |Curve value corresponding to the current cycle sampling – Curve value corresponding to the previous cycle sampling|. Based on the preset sampling threshold, it is determined whether the curve value needs to be recorded. If the absolute value is greater than the preset sampling threshold, the curve value needs to be recorded. If the absolute value is less than or equal to the preset sampling threshold, the curve value does not need to be recorded.
5. The method for generating lightning effects based on audio in Unity according to claim 3, characterized in that: The preset loop range is [0, F], and the preset increment step size is 1.
6. The method for generating lightning effects based on audio in Unity according to claim 1, characterized in that: The lightning effect ends when its duration is greater than or equal to its total duration.
7. A system for generating lightning effects based on audio in Unity, characterized in that, include: The audio data acquisition module is used to acquire audio data with a lightning effect. The conversion module is used to sample audio data and convert it into lightning effect curve information according to the preset sampling frequency and sampling threshold. The storage module is used to adjust the initial configuration data corresponding to the curve information to obtain new curve information and corresponding configuration data for at least one different lightning effect, and store them. The configuration data includes at least: scene and character light colors, scene and character light intensities, corresponding audio, duration, and curve index. In the configuration data, the scene and character light colors and scene and character light intensities are the performance values when the lightning effect is strongest, that is, the maximum values of scene and character light colors and scene and character light intensities. The curve index is specifically the index of the curve information corresponding to different lightning effects in the same array under the same scene. The monitoring module listens for rainy weather in the current scene during script frame updates. If rainy, it randomly calls the configuration data of at least one new curve information to generate a new lightning effect. When generating the new lightning effect, it records the running time. Based on the running time and duration, it samples the curve information to obtain the current light intensity value. It calculates the first light intensity value according to a second preset formula and the first light color value according to a third preset formula. The second preset formula is: First light intensity value = Adjusted scene and character light intensity. The current light intensity value, the third preset formula is as follows: First light color value = adjusted scene and character light colors The current light intensity value; based on the first light intensity value, the adjusted scene and character light intensity, the first light color value, and the adjusted scene and character light colors, the light is changed to generate a new lightning effect.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a program for generating lightning effects based on audio in Unity. When the program for generating lightning effects based on audio in Unity is executed by a processor, it implements the steps of the method for generating lightning effects based on audio in Unity as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Game special effect generation method and device thereof, storage medium and electronic equipment
CN111249727A
Vibration generation method and device based on audio signal, equipment and storage medium
CN114299991A