question: The collection of missing variants is causing the game package to fail to find matching variants, resulting in errors. How can this variant omission problem be solved?
Previous plan
1. Put all Material sphere created in batches within the scene, then collect:
2. After about 20 minutes, it was collected into a variant:
Problem
When there are many material orbs, the variations of material orbs collected multiple times in a row will be different each time.
Caused impact
Instability: It’s uncertain which variant will cause the problem each time it’s collected.
Difficult to locate: Difficult-to-locate problems are all concentrated in one variant, and the omitted variants are different each time they are collected.
The collection process took too long, which significantly impacted the art department’s ability to identify problems.
Analyze the reasons
Scene loading issues during collection: When there are many materials and the loading time is long, the next collection may proceed before all materials are fully loaded. This issue often results in some variants not being collected correctly.
Differences in scene and light source configurations may cause some shader variants to fail to be rendered.
Granularity issues in variant collection: If there is no strict control during variant collection, some variants may be missed or the results may be unstable due to inconsistent order.
New technical solutions
Static-based shader variant collection method
Solution advantages
- stability:
By utilizing an offline collection method, static collection analyzes the keyword configuration of material spheres without relying on real-time rendering scenes, thus avoiding instability during the collection process, ensuring the stability of each collection, and eliminating the problem of inconsistent collection variants. - Quick verification:
Since rendering the entire scene is unnecessary, the time spent collecting variants can be significantly reduced, while avoiding problems caused by material loading delays in the scene. Separating the time-consuming material acquisition process allows for rapid verification of variant collection while keeping the material spheres unchanged. The time-consuming parts often involve collecting material spheres for the entire project. - Quickly locate the problem:
By classifying variants by shader, when encountering variant issues, the problem can be quickly located based on the problematic shader. - Fine-grained refinement of collection methods:
The provided code adds finer-grained filtering and validity checks during the shader variant collection process, ensuring that only valid variants are collected and stored.
5. Automated Management:
Automatically create, update, and clean up variant files to ensure clean and efficient management of shader variant files.
Implementation steps
- Shader variants
First, check if the corresponding [item] already exists. If a file does not exist, a new file will be created. If a file exists and contains valid variants, it will be updated; if a file exists but does not contain valid variants, it will be deleted. This allows for automatic updates and deletions, avoiding the storage of unnecessary variant data. - Variation collection, filtering, and addition
By iterating through each material (Material) and its corresponding Shader (Shader), to obtain the shader variants related to the material.
use ShaderUtil.GetShaderVariantEntriesFilteredThe method retrieves eligible Shader variants (including all valid Shader keywords and Pass types).
For each variant (ShaderVariantIt will check if it already exists.ShaderVariantCollectionIf it does not exist, add the variant. - Shader Variant Collection file generation
Create a corresponding shader for each shader..shadervariantsThe file will be created and the variant data will be saved to the file. If the variant is empty or invalid, the existing variant file will be cleaned up. - Variant Manifest Generation
The code also supports generating manifest files for shader variants.JSONThe format records detailed information about the Shader variants (such as the number of variants, Shader name, Pass type, and keywords) and saves it as….jsonThe variant file’s contents are parsed into JSON format to generate a variant manifest file, facilitating subsequent runtime debugging and analysis. - Incremental collection
Support incremental collection (increment(Parameters) allow for the collection of variants based on existing data, or for the collection to be restarted after the data has been cleared.