| | 1 | | using DCL.Helpers; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Rendering |
| | 5 | | { |
| | 6 | | public static class CullingControllerUtils |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Computes the rule used for toggling skinned meshes updateWhenOffscreen param. |
| | 10 | | /// Skinned meshes should be always updated if near the camera to avoid false culling positives on screen edges. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="settings">Any settings object to use thresholds for computing the rule.</param> |
| | 13 | | /// <param name="distanceToCamera">Mesh distance from camera used for computing the rule.</param> |
| | 14 | | /// <returns>True if mesh should be updated when offscreen, false if otherwise.</returns> |
| | 15 | | internal static bool TestSkinnedRendererOffscreenRule(CullingControllerSettings settings, float distanceToCamera |
| | 16 | | { |
| 8 | 17 | | bool finalValue = true; |
| | 18 | |
|
| 8 | 19 | | if (settings.enableAnimationCulling) |
| | 20 | | { |
| 2 | 21 | | if (distanceToCamera > settings.enableAnimationCullingDistance) |
| 1 | 22 | | finalValue = false; |
| | 23 | | } |
| | 24 | |
|
| 8 | 25 | | return finalValue; |
| | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Computes the rule used for toggling renderers visibility. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="profile">Profile used for size and distance thresholds needed for the rule.</param> |
| | 32 | | /// <param name="viewportSize">Diagonal viewport size of the renderer.</param> |
| | 33 | | /// <param name="distanceToCamera">Distance to camera of the renderer.</param> |
| | 34 | | /// <param name="boundsContainsCamera">Renderer bounds contains camera?</param> |
| | 35 | | /// <param name="isOpaque">Renderer is opaque?</param> |
| | 36 | | /// <param name="isEmissive">Renderer is emissive?</param> |
| | 37 | | /// <returns>True if renderer should be visible, false if otherwise.</returns> |
| | 38 | | internal static bool TestRendererVisibleRule(CullingControllerProfile profile, float viewportSize, float distanc |
| | 39 | | { |
| 28 | 40 | | bool shouldBeVisible = distanceToCamera < profile.visibleDistanceThreshold || boundsContainsCamera; |
| | 41 | |
|
| 28 | 42 | | if (isEmissive) |
| 22 | 43 | | shouldBeVisible |= viewportSize > profile.emissiveSizeThreshold; |
| | 44 | |
|
| 28 | 45 | | if (isOpaque) |
| 25 | 46 | | shouldBeVisible |= viewportSize > profile.opaqueSizeThreshold; |
| | 47 | |
|
| 28 | 48 | | return shouldBeVisible; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Computes the rule used for toggling renderer shadow casting. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="profile">Profile used for size and distance thresholds needed for the rule.</param> |
| | 55 | | /// <param name="viewportSize">Diagonal viewport size of the renderer.</param> |
| | 56 | | /// <param name="distanceToCamera">Distance from renderer to camera.</param> |
| | 57 | | /// <param name="shadowMapTexelSize">Shadow map bounding box size in texels.</param> |
| | 58 | | /// <returns>True if renderer should have shadow, false otherwise.</returns> |
| | 59 | | internal static bool TestRendererShadowRule(CullingControllerProfile profile, float viewportSize, float distance |
| | 60 | | { |
| 28 | 61 | | bool shouldHaveShadow = distanceToCamera < profile.shadowDistanceThreshold; |
| 28 | 62 | | shouldHaveShadow |= viewportSize > profile.shadowRendererSizeThreshold; |
| 28 | 63 | | shouldHaveShadow &= shadowMapTexelSize > profile.shadowMapProjectionSizeThreshold; |
| 28 | 64 | | return shouldHaveShadow; |
| | 65 | | } |
| | 66 | |
|
| 0 | 67 | | internal static bool TestAvatarShadowRule(CullingControllerProfile profile, float avatarDistance) { return avata |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Determines if the given renderer is going to be enqueued at the opaque section of the rendering pipeline. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="renderer">Renderer to be checked.</param> |
| | 73 | | /// <returns>True if its opaque</returns> |
| | 74 | | internal static bool IsOpaque(Renderer renderer) |
| | 75 | | { |
| 23 | 76 | | Material firstMat = renderer.sharedMaterials[0]; |
| | 77 | |
|
| 23 | 78 | | if (firstMat == null) |
| 5 | 79 | | return true; |
| | 80 | |
|
| 18 | 81 | | if (firstMat.HasProperty(ShaderUtils.ZWrite) && |
| | 82 | | (int) firstMat.GetFloat(ShaderUtils.ZWrite) == 0) |
| | 83 | | { |
| 0 | 84 | | return false; |
| | 85 | | } |
| | 86 | |
|
| 18 | 87 | | return true; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Determines if the given renderer has emissive material traits. |
| | 92 | | /// </summary> |
| | 93 | | /// <param name="renderer">Renderer to be checked.</param> |
| | 94 | | /// <returns>True if the renderer is emissive.</returns> |
| | 95 | | internal static bool IsEmissive(Renderer renderer) |
| | 96 | | { |
| 23 | 97 | | Material firstMat = renderer.sharedMaterials[0]; |
| | 98 | |
|
| 23 | 99 | | if (firstMat == null) |
| 5 | 100 | | return false; |
| | 101 | |
|
| 18 | 102 | | if (firstMat.HasProperty(ShaderUtils.EmissionMap) && firstMat.GetTexture(ShaderUtils.EmissionMap) != null) |
| 0 | 103 | | return true; |
| | 104 | |
|
| 18 | 105 | | if (firstMat.HasProperty(ShaderUtils.EmissionColor) && firstMat.GetColor(ShaderUtils.EmissionColor) != Color |
| 18 | 106 | | return true; |
| | 107 | |
|
| 0 | 108 | | return false; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// ComputeShadowMapTexelSize computes the shadow-map bounding box diagonal texel size |
| | 113 | | /// for the given bounds size. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name="boundsSize">Diagonal bounds size of the object</param> |
| | 116 | | /// <param name="shadowDistance">Shadow distance as set in the quality settings</param> |
| | 117 | | /// <param name="shadowMapRes">Shadow map resolution as set in the quality settings (128, 256, etc)</param> |
| | 118 | | /// <returns>The computed shadow map diagonal texel size for the object.</returns> |
| | 119 | | /// <remarks> |
| | 120 | | /// This is calculated by doing the following: |
| | 121 | | /// |
| | 122 | | /// - We get the boundsSize to a normalized viewport size. |
| | 123 | | /// - We multiply the resulting value by the shadow map resolution. |
| | 124 | | /// |
| | 125 | | /// To get the viewport size, we assume the shadow distance value is directly correlated by |
| | 126 | | /// the orthogonal projection size used for rendering the shadow map. |
| | 127 | | /// |
| | 128 | | /// We can use the bounds size and shadow distance to obtain the normalized shadow viewport |
| | 129 | | /// value because both are expressed in world units. |
| | 130 | | /// |
| | 131 | | /// After getting the normalized size, we scale it by the shadow map resolution to get the |
| | 132 | | /// diagonal texel size of the bounds shadow. |
| | 133 | | /// |
| | 134 | | /// This leaves us with: |
| | 135 | | /// <c>shadowTexelSize = boundsSize / shadow dist * shadow res</c> |
| | 136 | | /// |
| | 137 | | /// This is a lazy approximation and most likely will need some refinement in the future. |
| | 138 | | /// </remarks> |
| 0 | 139 | | internal static float ComputeShadowMapTexelSize(float boundsSize, float shadowDistance, float shadowMapRes) { re |
| | 140 | |
|
| | 141 | | public static void DrawBounds(Bounds b, Color color, float delay = 0) |
| | 142 | | { |
| | 143 | | // bottom |
| 16 | 144 | | var p1 = new Vector3(b.min.x, b.min.y, b.min.z); |
| 16 | 145 | | var p2 = new Vector3(b.max.x, b.min.y, b.min.z); |
| 16 | 146 | | var p3 = new Vector3(b.max.x, b.min.y, b.max.z); |
| 16 | 147 | | var p4 = new Vector3(b.min.x, b.min.y, b.max.z); |
| | 148 | |
|
| 16 | 149 | | Debug.DrawLine(p1, p2, color, delay); |
| 16 | 150 | | Debug.DrawLine(p2, p3, color, delay); |
| 16 | 151 | | Debug.DrawLine(p3, p4, color, delay); |
| 16 | 152 | | Debug.DrawLine(p4, p1, color, delay); |
| | 153 | |
|
| | 154 | | // top |
| 16 | 155 | | var p5 = new Vector3(b.min.x, b.max.y, b.min.z); |
| 16 | 156 | | var p6 = new Vector3(b.max.x, b.max.y, b.min.z); |
| 16 | 157 | | var p7 = new Vector3(b.max.x, b.max.y, b.max.z); |
| 16 | 158 | | var p8 = new Vector3(b.min.x, b.max.y, b.max.z); |
| | 159 | |
|
| 16 | 160 | | Debug.DrawLine(p5, p6, color, delay); |
| 16 | 161 | | Debug.DrawLine(p6, p7, color, delay); |
| 16 | 162 | | Debug.DrawLine(p7, p8, color, delay); |
| 16 | 163 | | Debug.DrawLine(p8, p5, color, delay); |
| | 164 | |
|
| | 165 | | // sides |
| 16 | 166 | | Debug.DrawLine(p1, p5, color, delay); |
| 16 | 167 | | Debug.DrawLine(p2, p6, color, delay); |
| 16 | 168 | | Debug.DrawLine(p3, p7, color, delay); |
| 16 | 169 | | Debug.DrawLine(p4, p8, color, delay); |
| 16 | 170 | | } |
| | 171 | | } |
| | 172 | | } |