| | 1 | | using DCL.Shaders; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Pool; |
| | 6 | | using UnityEngine.Rendering; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public static class SliceByRenderState |
| | 11 | | { |
| 1 | 12 | | private static readonly Dictionary<Shader, bool> SHADERS_OPACITY = new (); |
| | 13 | |
|
| | 14 | | private readonly struct LayerKey : IEquatable<LayerKey> |
| | 15 | | { |
| | 16 | | public readonly bool IsOpaque; |
| | 17 | | public readonly CullMode CullMode; |
| | 18 | |
|
| | 19 | | public LayerKey(bool isOpaque, CullMode cullMode) |
| | 20 | | { |
| 118 | 21 | | IsOpaque = isOpaque; |
| 118 | 22 | | CullMode = cullMode; |
| 118 | 23 | | } |
| | 24 | |
|
| | 25 | | public bool Equals(LayerKey other) => |
| 82 | 26 | | IsOpaque == other.IsOpaque && CullMode == other.CullMode; |
| | 27 | |
|
| | 28 | | public override bool Equals(object obj) => |
| 0 | 29 | | obj is LayerKey other && Equals(other); |
| | 30 | |
|
| | 31 | | public override int GetHashCode() => |
| 153 | 32 | | HashCode.Combine(IsOpaque, (int)CullMode); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// <p> |
| | 37 | | /// This method takes a skinned mesh renderer list and turns it into a series of CombineLayer elements. |
| | 38 | | /// Each CombineLayer element represents a combining group, and the renderers are grouped using a set of criteri |
| | 39 | | /// </p> |
| | 40 | | /// <p> |
| | 41 | | /// For SliceByRenderState, the returned CombineLayer list will be grouped according to shared cull mode and |
| | 42 | | /// blend state. |
| | 43 | | /// </p> |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="renderers">List of renderers to slice.</param> |
| | 46 | | /// <returns>List of CombineLayer objects that can be used to produce a highly optimized combined mesh.</returns |
| | 47 | | internal static void Execute(IReadOnlyList<SkinnedMeshRenderer> renderers, List<CombineLayer> result, bool cullO |
| | 48 | | { |
| 17 | 49 | | var grouping = DictionaryPool<LayerKey, CombineLayer>.Get(); |
| | 50 | |
|
| | 51 | | // Group renderers on opaque and transparent materials |
| | 52 | | // Then, make subgroups to divide them between culling modes |
| 270 | 53 | | foreach (var meshRenderer in renderers) |
| | 54 | | { |
| 118 | 55 | | var opaque = IsOpaque(meshRenderer); |
| 118 | 56 | | var cullMode = cullOpaque && opaque ? GetCullModeWithoutCullOff(meshRenderer) : GetCullMode(meshRenderer |
| 118 | 57 | | var key = new LayerKey(opaque, cullMode); |
| | 58 | |
|
| 118 | 59 | | if (!grouping.TryGetValue(key, out var combineLayer)) |
| | 60 | | { |
| 36 | 61 | | grouping[key] = combineLayer = CombineLayer.Rent(cullMode, opaque); |
| 36 | 62 | | result.Add(combineLayer); |
| | 63 | | } |
| | 64 | |
|
| 118 | 65 | | combineLayer.AddRenderer(meshRenderer); |
| | 66 | | } |
| | 67 | |
|
| 17 | 68 | | DictionaryPool<LayerKey, CombineLayer>.Release(grouping); |
| | 69 | |
|
| | 70 | | /* |
| | 71 | | * The grouping outcome ends up like this: |
| | 72 | | * |
| | 73 | | * Opaque Transparent |
| | 74 | | * / | \ / | \ |
| | 75 | | * Back - Front - Off - Back - Front - Off -> rendererGroups |
| | 76 | | */ |
| 17 | 77 | | } |
| | 78 | |
|
| | 79 | | internal static CullMode GetCullMode(Renderer renderer) => |
| 118 | 80 | | GetCullMode(renderer.sharedMaterial); |
| | 81 | |
|
| | 82 | | internal static CullMode GetCullMode(Material material) |
| | 83 | | { |
| 124 | 84 | | if (material.HasProperty(ShaderUtils.Cull)) |
| | 85 | | { |
| 124 | 86 | | CullMode result = (CullMode)material.GetInt(ShaderUtils.Cull); |
| 124 | 87 | | return result; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | // GLTFast materials dont have culling, instead they have the "Double Sided" check toggled on "double" suffi |
| 0 | 91 | | if (material.shader.name.Contains("double")) |
| 0 | 92 | | return CullMode.Off; |
| | 93 | |
|
| 0 | 94 | | return CullMode.Back; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// |
| | 99 | | /// </summary> |
| | 100 | | /// <param name="renderer"></param> |
| | 101 | | /// <returns></returns> |
| | 102 | | internal static CullMode GetCullModeWithoutCullOff(Renderer renderer) |
| | 103 | | { |
| 3 | 104 | | CullMode result = GetCullMode(renderer.sharedMaterial); |
| | 105 | |
|
| 3 | 106 | | if (result == CullMode.Off) |
| 1 | 107 | | result = CullMode.Back; |
| | 108 | |
|
| 3 | 109 | | return result; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Determines if the given renderer is going to be enqueued at the opaque section of the rendering pipeline. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name="renderer">Renderer to be checked.</param> |
| | 116 | | /// <returns>True if its opaque</returns> |
| 118 | 117 | | internal static bool IsOpaque(Renderer renderer) => IsOpaque(renderer.sharedMaterial); |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Determines if the given renderer is going to be enqueued at the opaque section of the rendering pipeline. |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="material">Material to be checked</param> |
| | 123 | | /// <returns>True if its opaque</returns> |
| | 124 | | internal static bool IsOpaque(Material material) |
| | 125 | | { |
| 124 | 126 | | if (material == null) |
| 0 | 127 | | return true; |
| | 128 | |
|
| 124 | 129 | | var shader = material.shader; |
| | 130 | |
|
| 124 | 131 | | if (!SHADERS_OPACITY.TryGetValue(shader, out var isOpaqueShader)) |
| | 132 | | { |
| | 133 | | // NOTE(Kinerius): Since GLTFast materials doesn't have ZWrite property, we check if the shader name is |
| 1 | 134 | | bool hasOpaqueName = shader.name.Contains("opaque", StringComparison.OrdinalIgnoreCase); |
| 1 | 135 | | SHADERS_OPACITY[shader] = isOpaqueShader = hasOpaqueName; |
| | 136 | | } |
| | 137 | |
|
| 124 | 138 | | bool hasZWrite = material.HasProperty(ShaderUtils.ZWrite); |
| 124 | 139 | | isOpaqueShader = (!hasZWrite && !isOpaqueShader) || (hasZWrite && (int)material.GetFloat(ShaderUtils.ZWrite) |
| | 140 | |
|
| 124 | 141 | | return !isOpaqueShader; |
| | 142 | | } |
| | 143 | | } |
| | 144 | | } |