설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShadowCaster2D.cs 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Scripting.APIUpdating;
  4. using UnityEngine.U2D;
  5. using Unity.Collections;
  6. #if UNITY_EDITOR
  7. using System.Linq;
  8. using UnityEditor;
  9. using UnityEditor.Rendering.Universal;
  10. using UnityEditor.EditorTools;
  11. #endif
  12. namespace UnityEngine.Rendering.Universal
  13. {
  14. /// <summary>
  15. /// Class <c>ShadowCaster2D</c> contains properties used for shadow casting
  16. /// </summary>
  17. [CoreRPHelpURL("2DShadows", "com.unity.render-pipelines.universal")]
  18. [ExecuteInEditMode]
  19. [DisallowMultipleComponent]
  20. [AddComponentMenu("Rendering/2D/Shadow Caster 2D")]
  21. [MovedFrom(false, "UnityEngine.Experimental.Rendering.Universal", "com.unity.render-pipelines.universal")]
  22. public class ShadowCaster2D : ShadowCasterGroup2D, ISerializationCallbackReceiver
  23. {
  24. internal enum ComponentVersions
  25. {
  26. Version_Unserialized = 0,
  27. Version_1 = 1,
  28. Version_2 = 2,
  29. Version_3 = 3,
  30. Version_4 = 4,
  31. Version_5 = 5
  32. }
  33. const ComponentVersions k_CurrentComponentVersion = ComponentVersions.Version_5;
  34. [SerializeField] ComponentVersions m_ComponentVersion = ComponentVersions.Version_Unserialized;
  35. internal enum ShadowCastingSources
  36. {
  37. None,
  38. ShapeEditor,
  39. ShapeProvider
  40. }
  41. /// <summary>
  42. /// Options for what type of shadows are cast.
  43. /// </summary>
  44. public enum ShadowCastingOptions
  45. {
  46. /// <summary>
  47. /// Renders a shadows only for the sprite.
  48. /// </summary>
  49. SelfShadow,
  50. /// <summary>
  51. /// Renders a shadows only a cast shadow.
  52. /// </summary>
  53. CastShadow,
  54. /// <summary>
  55. /// Renders both a shadows for the sprite and a cast shadow.
  56. /// </summary>
  57. CastAndSelfShadow,
  58. /// <summary>
  59. /// Renders a sprite without shadow casting correctly on top of other shadow casting sprites
  60. /// </summary>
  61. NoShadow
  62. }
  63. internal enum EdgeProcessing
  64. {
  65. None = ShadowMesh2D.EdgeProcessing.None,
  66. Clipping = ShadowMesh2D.EdgeProcessing.Clipping,
  67. }
  68. [SerializeField] bool m_HasRenderer = false;
  69. [SerializeField] bool m_UseRendererSilhouette = true;
  70. [SerializeField] bool m_CastsShadows = true;
  71. [SerializeField] bool m_SelfShadows = false;
  72. [Range(0, 1)]
  73. [SerializeField] float m_AlphaCutoff = 0.1f;
  74. [SerializeField] int[] m_ApplyToSortingLayers = null;
  75. [SerializeField] Vector3[] m_ShapePath = null;
  76. [SerializeField] int m_ShapePathHash = 0;
  77. [SerializeField] int m_InstanceId;
  78. [SerializeField] Component m_ShadowShape2DComponent;
  79. [SerializeReference] ShadowShape2DProvider m_ShadowShape2DProvider;
  80. [SerializeField] ShadowCastingSources m_ShadowCastingSource = (ShadowCastingSources)(-1);
  81. [SerializeField] internal ShadowMesh2D m_ShadowMesh;
  82. [SerializeField] ShadowCastingOptions m_CastingOption = ShadowCastingOptions.CastShadow;
  83. [SerializeField] internal float m_PreviousTrimEdge = 0;
  84. [SerializeField] internal int m_PreviousEdgeProcessing;
  85. [SerializeField] internal int m_PreviousShadowCastingSource;
  86. [SerializeField] internal Component m_PreviousShadowShape2DSource = null;
  87. internal ShadowCasterGroup2D m_ShadowCasterGroup = null;
  88. internal ShadowCasterGroup2D m_PreviousShadowCasterGroup = null;
  89. internal bool m_ForceShadowMeshRebuild;
  90. internal EdgeProcessing edgeProcessing
  91. {
  92. get { return (EdgeProcessing)m_ShadowMesh.edgeProcessing; }
  93. set { m_ShadowMesh.edgeProcessing = (ShadowMesh2D.EdgeProcessing)value; }
  94. }
  95. /// <summary>
  96. /// The mesh to draw with.
  97. /// </summary>
  98. public Mesh mesh => m_ShadowMesh.mesh;
  99. /// <summary>
  100. /// The bounding sphere for the shadow caster
  101. /// </summary>
  102. public BoundingSphere boundingSphere => m_ShadowMesh.boundingSphere;
  103. /// <summary>
  104. /// The amount the shadow's edge is trimed
  105. /// </summary>
  106. public float trimEdge
  107. {
  108. get { return m_ShadowMesh.trimEdge; } set { m_ShadowMesh.trimEdge = value; }
  109. }
  110. /// <summary>
  111. /// The sets the renderer's shadow cutoff
  112. /// </summary>
  113. public float alphaCutoff
  114. {
  115. get { return m_AlphaCutoff; }
  116. set { m_AlphaCutoff = value; }
  117. }
  118. /// <summary>
  119. /// The path for the shape.
  120. /// </summary>
  121. public Vector3[] shapePath => m_ShapePath;
  122. internal int shapePathHash { get { return m_ShapePathHash; } set { m_ShapePathHash = value; } }
  123. internal ShadowCastingSources shadowCastingSource { get { return m_ShadowCastingSource; } set { m_ShadowCastingSource = value; } }
  124. // Make this public if possible...
  125. internal Component shadowShape2DComponent { get { return m_ShadowShape2DComponent; } set { m_ShadowShape2DComponent = value; } }
  126. internal ShadowShape2DProvider shadowShape2DProvider { get { return m_ShadowShape2DProvider; } set { m_ShadowShape2DProvider = value; } }
  127. int m_PreviousShadowGroup = 0;
  128. bool m_PreviousCastsShadows = true;
  129. int m_PreviousPathHash = 0;
  130. int m_SpriteMaterialCount;
  131. internal Vector3 m_CachedPosition;
  132. internal Vector3 m_CachedLossyScale;
  133. internal Quaternion m_CachedRotation;
  134. internal Matrix4x4 m_CachedShadowMatrix;
  135. internal Matrix4x4 m_CachedInverseShadowMatrix;
  136. internal Matrix4x4 m_CachedLocalToWorldMatrix;
  137. internal int spriteMaterialCount => m_SpriteMaterialCount;
  138. internal override void CacheValues()
  139. {
  140. m_CachedPosition = transform.position;
  141. m_CachedLossyScale = transform.lossyScale;
  142. m_CachedRotation = transform.rotation;
  143. bool flipX, flipY;
  144. m_ShadowMesh.GetFlip(out flipX, out flipY);
  145. Vector3 scale = new Vector3(flipX ? -1 : 1, flipY ? -1 : 1, 1);
  146. m_CachedShadowMatrix = Matrix4x4.TRS(m_CachedPosition, m_CachedRotation, scale);
  147. m_CachedInverseShadowMatrix = m_CachedShadowMatrix.inverse;
  148. m_CachedLocalToWorldMatrix = transform.localToWorldMatrix;
  149. }
  150. /// <summary>
  151. /// Sets the type of shadow cast.
  152. /// </summary>
  153. public ShadowCastingOptions castingOption
  154. {
  155. set { m_CastingOption = value; }
  156. get { return m_CastingOption; }
  157. }
  158. /// <summary>
  159. /// If selfShadows is true, useRendererSilhoutte specifies that the renderer's sihouette should be considered part of the shadow. If selfShadows is false, useRendererSilhoutte specifies that the renderer's sihouette should be excluded from the shadow
  160. /// </summary>
  161. [Obsolete("useRendererSilhoutte is deprecated. Use rendererSilhoutte instead")]
  162. public bool useRendererSilhouette
  163. {
  164. set { m_UseRendererSilhouette = value; }
  165. get { return m_UseRendererSilhouette && m_HasRenderer; }
  166. }
  167. /// <summary>
  168. /// If true, the shadow casting shape is included as part of the shadow. If false, the shadow casting shape is excluded from the shadow.
  169. /// </summary>
  170. public bool selfShadows
  171. {
  172. set
  173. {
  174. if (value)
  175. {
  176. if (castingOption == ShadowCastingOptions.CastShadow)
  177. castingOption = ShadowCastingOptions.CastAndSelfShadow;
  178. else if (castingOption == ShadowCastingOptions.NoShadow)
  179. castingOption = ShadowCastingOptions.SelfShadow;
  180. }
  181. else
  182. {
  183. if (castingOption == ShadowCastingOptions.CastAndSelfShadow )
  184. castingOption = ShadowCastingOptions.CastShadow;
  185. else if(castingOption == ShadowCastingOptions.SelfShadow)
  186. castingOption = ShadowCastingOptions.NoShadow;
  187. }
  188. }
  189. get { return castingOption == ShadowCastingOptions.CastAndSelfShadow || castingOption == ShadowCastingOptions.SelfShadow; }
  190. }
  191. /// <summary>
  192. /// Specifies if shadows will be cast.
  193. /// </summary>
  194. ///
  195. public bool castsShadows
  196. {
  197. set
  198. {
  199. if(value)
  200. {
  201. if (castingOption == ShadowCastingOptions.SelfShadow)
  202. castingOption = ShadowCastingOptions.CastAndSelfShadow;
  203. else if (castingOption == ShadowCastingOptions.NoShadow)
  204. castingOption = ShadowCastingOptions.CastShadow;
  205. }
  206. else
  207. {
  208. if (castingOption == ShadowCastingOptions.CastAndSelfShadow)
  209. castingOption = ShadowCastingOptions.SelfShadow;
  210. else if (castingOption == ShadowCastingOptions.CastShadow)
  211. castingOption = ShadowCastingOptions.NoShadow;
  212. }
  213. }
  214. get { return castingOption == ShadowCastingOptions.CastShadow || castingOption == ShadowCastingOptions.CastAndSelfShadow; }
  215. }
  216. static int[] SetDefaultSortingLayers()
  217. {
  218. int layerCount = SortingLayer.layers.Length;
  219. int[] allLayers = new int[layerCount];
  220. for (int layerIndex = 0; layerIndex < layerCount; layerIndex++)
  221. {
  222. allLayers[layerIndex] = SortingLayer.layers[layerIndex].id;
  223. }
  224. return allLayers;
  225. }
  226. internal bool IsLit(Light2D light)
  227. {
  228. // Oddly adding and subtracting vectors is expensive here because of the new structures created...
  229. Vector3 deltaPos;
  230. deltaPos.x = light.m_CachedPosition.x - boundingSphere.position.x;
  231. deltaPos.y = light.m_CachedPosition.y - boundingSphere.position.y;
  232. deltaPos.z = light.m_CachedPosition.z - boundingSphere.position.z;
  233. float distanceSq = Vector3.SqrMagnitude(deltaPos);
  234. float radiiLength = light.boundingSphere.radius + boundingSphere.radius;
  235. return distanceSq <= (radiiLength * radiiLength);
  236. }
  237. internal bool IsShadowedLayer(int layer)
  238. {
  239. return m_ApplyToSortingLayers != null ? Array.IndexOf(m_ApplyToSortingLayers, layer) >= 0 : false;
  240. }
  241. void SetShadowShape(ShadowMesh2D shadowMesh)
  242. {
  243. m_ForceShadowMeshRebuild = false;
  244. if (m_ShadowCastingSource == ShadowCastingSources.ShapeEditor)
  245. {
  246. NativeArray<Vector3> nativePath = new NativeArray<Vector3>(m_ShapePath, Allocator.Temp);
  247. NativeArray<int> nativeIndices = new NativeArray<int>(2 * m_ShapePath.Length, Allocator.Temp);
  248. int lastIndex = m_ShapePath.Length - 1;
  249. for (int i = 0; i < m_ShapePath.Length; i++)
  250. {
  251. int startingIndex = i << 1;
  252. nativeIndices[startingIndex] = lastIndex;
  253. nativeIndices[startingIndex + 1] = i;
  254. lastIndex = i;
  255. }
  256. shadowMesh.SetShapeWithLines(nativePath, nativeIndices, false);
  257. nativePath.Dispose();
  258. nativeIndices.Dispose();
  259. }
  260. if (m_ShadowCastingSource == ShadowCastingSources.ShapeProvider)
  261. {
  262. ShapeProviderUtility.PersistantDataCreated(m_ShadowShape2DProvider, m_ShadowShape2DComponent, shadowMesh);
  263. }
  264. }
  265. private void Awake()
  266. {
  267. if (m_ShadowCastingSource < 0)
  268. {
  269. #if UNITY_EDITOR
  270. ShapeProviderUtility.TryGetDefaultShadowShapeProviderSource(gameObject, out var component, out var provider);
  271. if (component != null && provider != null && (shapePath == null || shapePath.Length == 0))
  272. {
  273. m_ShadowShape2DComponent = component;
  274. m_ShadowShape2DProvider = provider;
  275. m_ShadowCastingSource = ShadowCastingSources.ShapeProvider;
  276. }
  277. else
  278. {
  279. m_ShadowCastingSource = ShadowCastingSources.ShapeEditor;
  280. }
  281. #else
  282. m_ShadowCastingSource = ShadowCastingSources.ShapeEditor;
  283. #endif
  284. }
  285. Vector3 inverseScale = Vector3.zero;
  286. Vector3 relOffset = transform.position;
  287. if (transform.lossyScale.x != 0 && transform.lossyScale.y != 0)
  288. {
  289. inverseScale = new Vector3(1 / transform.lossyScale.x, 1 / transform.lossyScale.y);
  290. relOffset = new Vector3(inverseScale.x * -transform.position.x, inverseScale.y * -transform.position.y);
  291. }
  292. if (m_ApplyToSortingLayers == null)
  293. m_ApplyToSortingLayers = SetDefaultSortingLayers();
  294. Bounds bounds = new Bounds(transform.position, Vector3.one);
  295. Renderer renderer = GetComponent<Renderer>();
  296. if (renderer != null)
  297. {
  298. bounds = renderer.bounds;
  299. m_SpriteMaterialCount = renderer.sharedMaterials.Length;
  300. }
  301. if (m_ShapePath == null || m_ShapePath.Length == 0)
  302. {
  303. m_ShapePath = new Vector3[]
  304. {
  305. relOffset + new Vector3(inverseScale.x * bounds.min.x, inverseScale.y * bounds.min.y),
  306. relOffset + new Vector3(inverseScale.x * bounds.min.x, inverseScale.y * bounds.max.y),
  307. relOffset + new Vector3(inverseScale.x * bounds.max.x, inverseScale.y * bounds.max.y),
  308. relOffset + new Vector3(inverseScale.x * bounds.max.x, inverseScale.y * bounds.min.y),
  309. };
  310. }
  311. if (m_ShadowMesh == null)
  312. {
  313. ShadowMesh2D newShadowMesh = new ShadowMesh2D();
  314. SetShadowShape(newShadowMesh);
  315. m_ShadowMesh = newShadowMesh;
  316. }
  317. #if UNITY_EDITOR
  318. // This step is required in case of copy/pasting an object with a shadow caster.
  319. else
  320. {
  321. ShadowMesh2D newShadowMesh = new ShadowMesh2D();
  322. newShadowMesh.CopyFrom(m_ShadowMesh);
  323. m_ShadowMesh = newShadowMesh;
  324. }
  325. #endif
  326. #if USING_PHYSICS2D_MODULE
  327. else
  328. {
  329. Collider2D collider = GetComponent<Collider2D>();
  330. if (collider != null)
  331. bounds = collider.bounds;
  332. }
  333. #endif
  334. }
  335. /// <summary>
  336. /// This function is called when the object becomes enabled and active.
  337. /// </summary>
  338. protected void OnEnable()
  339. {
  340. if (m_ShadowShape2DProvider != null)
  341. m_ShadowShape2DProvider.Enabled(m_ShadowShape2DComponent);
  342. m_ShadowCasterGroup = null;
  343. #if UNITY_EDITOR
  344. SortingLayer.onLayerAdded += OnSortingLayerAdded;
  345. SortingLayer.onLayerRemoved += OnSortingLayerRemoved;
  346. #endif
  347. }
  348. /// <summary>
  349. /// This function is called when the behaviour becomes disabled.
  350. /// </summary>
  351. protected void OnDisable()
  352. {
  353. ShadowCasterGroup2DManager.RemoveFromShadowCasterGroup(this, m_ShadowCasterGroup);
  354. if (m_ShadowShape2DProvider != null)
  355. m_ShadowShape2DProvider.Disabled(m_ShadowShape2DComponent);
  356. #if UNITY_EDITOR
  357. SortingLayer.onLayerAdded -= OnSortingLayerAdded;
  358. SortingLayer.onLayerRemoved -= OnSortingLayerRemoved;
  359. #endif
  360. }
  361. /// <summary>
  362. /// Update is called every frame, if the MonoBehaviour is enabled.
  363. /// </summary>
  364. public void Update()
  365. {
  366. Renderer renderer;
  367. m_HasRenderer = TryGetComponent<Renderer>(out renderer);
  368. bool rebuildMesh = LightUtility.CheckForChange((int)m_ShadowCastingSource, ref m_PreviousShadowCastingSource);
  369. rebuildMesh |= LightUtility.CheckForChange((int)edgeProcessing, ref m_PreviousEdgeProcessing);
  370. rebuildMesh |= edgeProcessing != EdgeProcessing.None && LightUtility.CheckForChange(trimEdge, ref m_PreviousTrimEdge);
  371. rebuildMesh |= m_ForceShadowMeshRebuild;
  372. if (m_ShadowCastingSource == ShadowCastingSources.ShapeEditor)
  373. {
  374. rebuildMesh |= LightUtility.CheckForChange(m_ShapePathHash, ref m_PreviousPathHash);
  375. if (rebuildMesh)
  376. {
  377. SetShadowShape(m_ShadowMesh);
  378. }
  379. }
  380. else
  381. {
  382. if ((rebuildMesh || LightUtility.CheckForChange(m_ShadowShape2DComponent, ref m_PreviousShadowShape2DSource)) && m_ShadowShape2DComponent != null)
  383. {
  384. SetShadowShape(m_ShadowMesh);
  385. }
  386. }
  387. m_PreviousShadowCasterGroup = m_ShadowCasterGroup;
  388. bool addedToNewGroup = ShadowCasterGroup2DManager.AddToShadowCasterGroup(this, ref m_ShadowCasterGroup, ref m_Priority);
  389. if (addedToNewGroup && m_ShadowCasterGroup != null)
  390. {
  391. if (m_PreviousShadowCasterGroup == this)
  392. ShadowCasterGroup2DManager.RemoveGroup(this);
  393. ShadowCasterGroup2DManager.RemoveFromShadowCasterGroup(this, m_PreviousShadowCasterGroup);
  394. if (m_ShadowCasterGroup == this)
  395. ShadowCasterGroup2DManager.AddGroup(this);
  396. }
  397. if (LightUtility.CheckForChange(m_ShadowGroup, ref m_PreviousShadowGroup))
  398. {
  399. ShadowCasterGroup2DManager.RemoveGroup(this);
  400. ShadowCasterGroup2DManager.AddGroup(this);
  401. }
  402. if (LightUtility.CheckForChange(m_CastsShadows, ref m_PreviousCastsShadows))
  403. {
  404. ShadowCasterGroup2DManager.AddGroup(this);
  405. }
  406. if(m_ShadowMesh != null)
  407. m_ShadowMesh.UpdateBoundingSphere(transform);
  408. }
  409. #if UNITY_EDITOR
  410. internal void DrawPreviewOutline(Transform t, float trimionDistance)
  411. {
  412. Vector3[] vertices = mesh.vertices;
  413. int[] triangles = mesh.triangles;
  414. Vector4[] tangents = mesh.tangents;
  415. Handles.color = Color.white;
  416. for (int i = 0; i < triangles.Length; i += 3)
  417. {
  418. int v0 = triangles[i];
  419. int v1 = triangles[i + 1];
  420. int v2 = triangles[i + 2];
  421. Vector3 pt0 = vertices[v0];
  422. Vector3 pt1 = vertices[v1];
  423. Vector3 pt2 = vertices[v2];
  424. Vector4 tan0 = tangents[v0];
  425. Vector4 tan1 = tangents[v1];
  426. Vector4 tan2 = tangents[v2];
  427. Vector3 trimPt0 = new Vector3(pt0.x + trimionDistance * tan0.x, pt0.y + trimionDistance * tan0.y, 0);
  428. Vector3 trimPt1 = new Vector3(pt1.x + trimionDistance * tan1.x, pt1.y + trimionDistance * tan1.y, 0);
  429. Vector3 trimPt2 = new Vector3(pt2.x + trimionDistance * tan2.x, pt2.y + trimionDistance * tan2.y, 0);
  430. bool flipX, flipY;
  431. m_ShadowMesh.GetFlip(out flipX, out flipY);
  432. Vector3 scale = new Vector3(t.lossyScale.x * (flipX ? -1 : 1), t.lossyScale.y * (flipY ? -1 : 1), 1);
  433. Matrix4x4 mat = Matrix4x4.TRS(t.position, t.rotation, scale);
  434. trimPt0 = mat.MultiplyPoint(trimPt0);
  435. trimPt1 = mat.MultiplyPoint(trimPt1);
  436. trimPt2 = mat.MultiplyPoint(trimPt2);
  437. if (pt0.z == 0 && pt1.z == 0)
  438. Handles.DrawAAPolyLine(4, new Vector3[] { trimPt0, trimPt1 });
  439. if (pt1.z == 0 && pt2.z == 0)
  440. Handles.DrawAAPolyLine(4, new Vector3[] { trimPt1, trimPt2 });
  441. if (pt2.z == 0 && pt0.z == 0)
  442. Handles.DrawAAPolyLine(4, new Vector3[] { trimPt2, trimPt0 });
  443. }
  444. }
  445. internal void DrawPreviewOutline()
  446. {
  447. if (m_ShadowMesh != null && mesh != null && m_ShadowCastingSource != ShadowCastingSources.None && enabled)
  448. {
  449. if (edgeProcessing == EdgeProcessing.None)
  450. DrawPreviewOutline(transform, trimEdge);
  451. else
  452. DrawPreviewOutline(transform, 0);
  453. }
  454. }
  455. void Reset()
  456. {
  457. ShadowCasterGroup2DManager.RemoveFromShadowCasterGroup(this, m_ShadowCasterGroup);
  458. m_ShadowCasterGroup = null;
  459. m_PreviousShadowCasterGroup = null;
  460. m_PreviousShadowCastingSource = -1;
  461. m_PreviousShadowShape2DSource = null;
  462. m_PreviousTrimEdge = 0;
  463. m_PreviousEdgeProcessing = -1;
  464. m_ForceShadowMeshRebuild = true;
  465. m_HasRenderer = false;
  466. m_UseRendererSilhouette = true;
  467. m_CastsShadows = true;
  468. m_SelfShadows = false;
  469. m_ApplyToSortingLayers = null;
  470. m_ShapePath = null;
  471. m_ShapePathHash = 0;
  472. m_ShadowShape2DComponent = null;
  473. m_ShadowShape2DProvider = null;
  474. m_ShadowCastingSource = (ShadowCastingSources)(-1);
  475. m_ShadowMesh = null;
  476. m_CastingOption = ShadowCastingOptions.CastShadow;
  477. ToolManager.RestorePreviousTool(); // This is needed in case you have the shape editor active
  478. Awake();
  479. OnEnable();
  480. }
  481. #endif
  482. #if UNITY_EDITOR
  483. private void OnSortingLayerAdded(SortingLayer layer)
  484. {
  485. m_ApplyToSortingLayers = m_ApplyToSortingLayers.Append(layer.id).ToArray();
  486. }
  487. private void OnSortingLayerRemoved(SortingLayer layer)
  488. {
  489. m_ApplyToSortingLayers = m_ApplyToSortingLayers.Where(x => x != layer.id && SortingLayer.IsValid(x)).ToArray();
  490. }
  491. #endif
  492. /// <inheritdoc/>
  493. public void OnBeforeSerialize()
  494. {
  495. m_ComponentVersion = k_CurrentComponentVersion;
  496. }
  497. /// <inheritdoc/>
  498. public void OnAfterDeserialize()
  499. {
  500. if (m_ComponentVersion < ComponentVersions.Version_2)
  501. {
  502. // ----------------------------------------------------
  503. // m_SelfShadows | m_CastsShadows | m_CastingOption
  504. // ----------------------------------------------------
  505. // 0 | 0 | DontCast
  506. // 0 | 1 | Renderer Only
  507. // 1 | 0 | Cast Only
  508. // 1 | 1 | CastAndSelfShadow
  509. // ----------------------------------------------------
  510. if (m_SelfShadows && m_CastsShadows)
  511. m_CastingOption = ShadowCastingOptions.CastAndSelfShadow;
  512. else if (m_SelfShadows)
  513. m_CastingOption = ShadowCastingOptions.SelfShadow;
  514. else if (m_CastsShadows)
  515. m_CastingOption = ShadowCastingOptions.CastShadow;
  516. else
  517. m_CastingOption = ShadowCastingOptions.NoShadow;
  518. }
  519. if(m_ComponentVersion < ComponentVersions.Version_3)
  520. {
  521. m_ShadowMesh = null;
  522. m_ForceShadowMeshRebuild = true;
  523. }
  524. }
  525. }
  526. }