public static void RemoveFromShadowCasterGroup(ShadowCaster2D shadowCaster, ShadowCasterGroup2D shadowCasterGroup) { if (shadowCasterGroup != null) { shadowCasterGroup.UnregisterShadowCaster2D(shadowCaster); } }
public void UnregisterShadowCaster2D(ShadowCaster2D shadowCaster2D) { if (m_ShadowCasters != null) { m_ShadowCasters.Remove(shadowCaster2D); } }
/// <summary> /// Replaces the hash key of the shadow caster, which produces an internal data rebuild. /// </summary> /// <remarks> /// A change in the shape of the shadow caster will not block the light, it has to be rebuilt using this function. /// </remarks> /// <param name="shadowCaster">The object to modify.</param> /// <param name="hash">The new hash key to store. It must be different from the previous key to produce the rebuild. You can use a random number.</param> public static void SetPathHash(this UnityEngine.Rendering.Universal.ShadowCaster2D shadowCaster, int hash) { FieldInfo hashField = typeof(UnityEngine.Rendering.Universal.ShadowCaster2D).GetField("m_ShapePathHash", BindingFlags.NonPublic | BindingFlags.Instance); hashField.SetValue(shadowCaster, hash); }
/// <summary> /// Replaces the path that defines the shape of the shadow caster. /// </summary> /// <remarks> /// Calling this method will change the shape but not the mesh of the shadow caster. Call SetPathHash afterwards. /// </remarks> /// <param name="shadowCaster">The object to modify.</param> /// <param name="path">The new path to define the shape of the shadow caster.</param> public static void SetPath(this UnityEngine.Rendering.Universal.ShadowCaster2D shadowCaster, Vector3[] path) { FieldInfo shapeField = typeof(UnityEngine.Rendering.Universal.ShadowCaster2D).GetField("m_ShapePath", BindingFlags.NonPublic | BindingFlags.Instance); shapeField.SetValue(shadowCaster, path); }
public static void SetShadowProjectionGlobals(CommandBuffer cmdBuffer, ShadowCaster2D shadowCaster) { Vector3 shadowCasterScale = shadowCaster.transform.lossyScale; Matrix4x4 shadowMatrix = Matrix4x4.TRS(shadowCaster.transform.position, shadowCaster.transform.rotation, Vector3.one); cmdBuffer.SetGlobalVector(k_ShadowModelScaleID, new Vector3(shadowCasterScale.x, shadowCasterScale.y, shadowCasterScale.z)); cmdBuffer.SetGlobalMatrix(k_ShadowModelMatrixID, shadowMatrix); cmdBuffer.SetGlobalMatrix(k_ShadowModelInvMatrixID, shadowMatrix.inverse); }
public void RegisterShadowCaster2D(ShadowCaster2D shadowCaster2D) { if (m_ShadowCasters == null) { m_ShadowCasters = new List <ShadowCaster2D>(); } m_ShadowCasters.Add(shadowCaster2D); }
public static bool AddToShadowCasterGroup(ShadowCaster2D shadowCaster, ref ShadowCasterGroup2D shadowCasterGroup) { ShadowCasterGroup2D newShadowCasterGroup = FindTopMostCompositeShadowCaster(shadowCaster) as ShadowCasterGroup2D; if (newShadowCasterGroup == null) { newShadowCasterGroup = shadowCaster.GetComponent <ShadowCaster2D>(); } if (newShadowCasterGroup != null && shadowCasterGroup != newShadowCasterGroup) { newShadowCasterGroup.RegisterShadowCaster2D(shadowCaster); shadowCasterGroup = newShadowCasterGroup; return(true); } return(false); }
static CompositeShadowCaster2D FindTopMostCompositeShadowCaster(ShadowCaster2D shadowCaster) { CompositeShadowCaster2D retGroup = null; Transform transformToCheck = shadowCaster.transform.parent; while (transformToCheck != null) { CompositeShadowCaster2D currentGroup; if (transformToCheck.TryGetComponent <CompositeShadowCaster2D>(out currentGroup)) { retGroup = currentGroup; } transformToCheck = transformToCheck.parent; } return(retGroup); }
/// <summary> /// Given a Composite Collider 2D, it replaces existing Shadow Caster 2Ds (children) with new Shadow Caster 2D objects whose /// shapes coincide with the paths of the collider. /// </summary> /// <remarks> /// It is recommended that the object that contains the collider component has a Composite Shadow Caster 2D too. /// It is recommended to call this method in editor only. /// </remarks> /// <param name="collider">The collider which will be the parent of the new shadow casters.</param> public static void GenerateTilemapShadowCasters(CompositeCollider2D collider) { // First, it destroys the existing shadow casters UnityEngine.Rendering.Universal.ShadowCaster2D[] existingShadowCasters = collider.GetComponentsInChildren <UnityEngine.Rendering.Universal.ShadowCaster2D>(); for (int i = 0; i < existingShadowCasters.Length; ++i) { GameObject.DestroyImmediate(existingShadowCasters[i].gameObject); } // Then it creates the new shadow casters, based on the paths of the composite collider int pathCount = collider.pathCount; List <Vector2> pointsInPath = new List <Vector2>(); List <Vector3> pointsInPath3D = new List <Vector3>(); for (int i = 0; i < pathCount; ++i) { collider.GetPath(i, pointsInPath); GameObject newShadowCaster = new GameObject("ShadowCaster2D"); newShadowCaster.isStatic = true; newShadowCaster.transform.SetParent(collider.transform, false); for (int j = 0; j < pointsInPath.Count; ++j) { pointsInPath3D.Add(pointsInPath[j]); } UnityEngine.Rendering.Universal.ShadowCaster2D component = newShadowCaster.AddComponent <UnityEngine.Rendering.Universal.ShadowCaster2D>(); component.SetPath(pointsInPath3D.ToArray()); component.SetPathHash(Random.Range(int.MinValue, int.MaxValue)); // The hashing function GetShapePathHash could be copied from the LightUtility class pointsInPath.Clear(); pointsInPath3D.Clear(); } UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty(); }
public static void SetShadowProjectionGlobals(CommandBuffer cmdBuffer, ShadowCaster2D shadowCaster) { cmdBuffer.SetGlobalVector(k_ShadowModelScaleID, shadowCaster.m_CachedLossyScale); cmdBuffer.SetGlobalMatrix(k_ShadowModelMatrixID, shadowCaster.m_CachedShadowMatrix); cmdBuffer.SetGlobalMatrix(k_ShadowModelInvMatrixID, shadowCaster.m_CachedInverseShadowMatrix); }