| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Rendering; |
| | 10 | | using Decentraland.Sdk.Ecs6; |
| | 11 | | using System; |
| | 12 | | using Object = UnityEngine.Object; |
| | 13 | |
|
| | 14 | | namespace DCL.Components |
| | 15 | | { |
| | 16 | | public class BasicMaterial : BaseDisposable |
| | 17 | | { |
| | 18 | | [System.Serializable] |
| | 19 | | public class Model : BaseModel |
| | 20 | | { |
| | 21 | | public string texture; |
| | 22 | |
|
| | 23 | | // value that defines if a pixel is visible or invisible (no transparency gradients) |
| | 24 | | [Range(0f, 1f)] |
| 173 | 25 | | public float alphaTest = 0.5f; |
| | 26 | |
|
| 173 | 27 | | public bool castShadows = true; |
| | 28 | |
|
| | 29 | | public override BaseModel GetDataFromJSON(string json) => |
| 64 | 30 | | Utils.SafeFromJson<Model>(json); |
| | 31 | |
|
| | 32 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 33 | | { |
| 0 | 34 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.BasicMaterial) |
| 0 | 35 | | return Utils.SafeUnimplemented<BasicMaterial, Model>(expected: ComponentBodyPayload.PayloadOneofCase |
| | 36 | |
|
| 0 | 37 | | var pb = new Model(); |
| 0 | 38 | | if (pbModel.BasicMaterial.HasTexture) pb.texture = pbModel.BasicMaterial.Texture; |
| 0 | 39 | | if (pbModel.BasicMaterial.HasAlphaTest) pb.alphaTest = pbModel.BasicMaterial.AlphaTest; |
| 0 | 40 | | if (pbModel.BasicMaterial.HasCastShadows) pb.castShadows = pbModel.BasicMaterial.CastShadows; |
| | 41 | |
|
| 0 | 42 | | return pb; |
| | 43 | | } |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public Material material; |
| | 47 | |
|
| | 48 | | private DCLTexture dclTexture = null; |
| | 49 | |
|
| 1 | 50 | | private static readonly int _BaseMap = Shader.PropertyToID("_BaseMap"); |
| 1 | 51 | | private static readonly int _AlphaClip = Shader.PropertyToID("_AlphaClip"); |
| 1 | 52 | | private static readonly int _Cutoff = Shader.PropertyToID("_Cutoff"); |
| 1 | 53 | | private static readonly int _ZWrite = Shader.PropertyToID("_ZWrite"); |
| | 54 | |
|
| 45 | 55 | | private readonly DCLTexture.Fetcher dclTextureFetcher = new DCLTexture.Fetcher(); |
| | 56 | | private bool isDisposed; |
| | 57 | |
|
| 45 | 58 | | public BasicMaterial() |
| | 59 | | { |
| 45 | 60 | | material = new Material(Utils.EnsureResourcesMaterial("Materials/BasicShapeMaterial")); |
| | 61 | |
|
| 45 | 62 | | OnAttach += OnMaterialAttached; |
| 45 | 63 | | OnDetach += OnMaterialDetached; |
| 45 | 64 | | model = new Model(); |
| 45 | 65 | | } |
| | 66 | |
|
| 4 | 67 | | new public Model GetModel() { return (Model) model; } |
| | 68 | |
|
| 0 | 69 | | public override int GetClassId() { return (int) CLASS_ID.BASIC_MATERIAL; } |
| | 70 | |
|
| | 71 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 72 | | { |
| 44 | 73 | | if (attachedEntities.Contains(entity)) |
| 0 | 74 | | return; |
| | 75 | |
|
| 44 | 76 | | scene.componentsManagerLegacy.RemoveSharedComponent(entity, (typeof(PBRMaterial))); |
| 44 | 77 | | base.AttachTo(entity, overridenAttachedType); |
| 44 | 78 | | } |
| | 79 | |
|
| | 80 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 81 | | { |
| 44 | 82 | | base.DetachFrom(entity, overridenAttachedType); |
| 44 | 83 | | } |
| | 84 | |
|
| | 85 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 86 | | { |
| 64 | 87 | | if (material == null) |
| | 88 | | { |
| 0 | 89 | | yield break; // We escape ApplyChanges called in the parent's constructor |
| | 90 | | } |
| | 91 | |
|
| | 92 | | #if UNITY_EDITOR |
| 64 | 93 | | material.name = "BasicMaterial_" + id; |
| | 94 | | #endif |
| | 95 | |
|
| 64 | 96 | | Model model = (Model) newModel; |
| | 97 | |
|
| 64 | 98 | | if (!string.IsNullOrEmpty(model.texture)) |
| | 99 | | { |
| 50 | 100 | | if (dclTexture == null || dclTexture.id != model.texture) |
| | 101 | | { |
| 50 | 102 | | yield return dclTextureFetcher.Fetch( |
| | 103 | | scene, |
| | 104 | | model.texture, |
| | 105 | | texture => |
| | 106 | | { |
| 48 | 107 | | if (isDisposed) |
| 0 | 108 | | return false; |
| | 109 | |
|
| 48 | 110 | | if (dclTexture != null) |
| | 111 | | { |
| 15 | 112 | | dclTexture.DetachFrom(this); |
| | 113 | | } |
| | 114 | |
|
| 48 | 115 | | material.SetTexture(_BaseMap, texture.texture); |
| 48 | 116 | | dclTexture = texture; |
| 48 | 117 | | dclTexture.AttachTo(this); |
| 48 | 118 | | return true; |
| | 119 | | }).ToCoroutine(e => |
| | 120 | | { |
| 2 | 121 | | if (e is not OperationCanceledException) |
| 0 | 122 | | throw e; |
| 2 | 123 | | }); |
| | 124 | | // using `ToCoroutine()` since using Task directly arise some component lifecycle issues |
| | 125 | | } |
| | 126 | | } |
| | 127 | | else |
| | 128 | | { |
| 14 | 129 | | material.mainTexture = null; |
| | 130 | |
|
| 14 | 131 | | if ( dclTexture != null ) |
| | 132 | | { |
| 0 | 133 | | dclTexture.DetachFrom(this); |
| 0 | 134 | | dclTexture = null; |
| | 135 | | } |
| | 136 | | } |
| | 137 | |
|
| 64 | 138 | | material.EnableKeyword("_ALPHATEST_ON"); |
| 62 | 139 | | material.SetInt(_ZWrite, 1); |
| 62 | 140 | | material.SetFloat(_AlphaClip, 1); |
| 62 | 141 | | material.SetFloat(_Cutoff, model.alphaTest); |
| 62 | 142 | | material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.AlphaTest; |
| | 143 | |
|
| 216 | 144 | | foreach (IDCLEntity entity in attachedEntities) |
| | 145 | | { |
| 46 | 146 | | InitMaterial(entity); |
| | 147 | | } |
| 62 | 148 | | } |
| | 149 | |
|
| | 150 | | void OnMaterialAttached(IDCLEntity entity) |
| | 151 | | { |
| 44 | 152 | | entity.OnShapeUpdated -= OnShapeUpdated; |
| 44 | 153 | | entity.OnShapeUpdated += OnShapeUpdated; |
| | 154 | |
|
| 44 | 155 | | if (entity.meshRootGameObject != null) |
| | 156 | | { |
| 20 | 157 | | var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>(); |
| | 158 | |
|
| 20 | 159 | | if (meshRenderer != null) |
| 20 | 160 | | InitMaterial(entity); |
| | 161 | | } |
| 44 | 162 | | } |
| | 163 | |
|
| | 164 | | void InitMaterial(IDCLEntity entity) |
| | 165 | | { |
| 107 | 166 | | var meshGameObject = entity.meshRootGameObject; |
| | 167 | |
|
| 107 | 168 | | if (meshGameObject == null) |
| 20 | 169 | | return; |
| | 170 | |
|
| 87 | 171 | | var meshRenderer = meshGameObject.GetComponent<MeshRenderer>(); |
| | 172 | |
|
| 87 | 173 | | if (meshRenderer == null) |
| 0 | 174 | | return; |
| | 175 | |
|
| 87 | 176 | | Model model = (Model) this.model; |
| | 177 | |
|
| 87 | 178 | | meshRenderer.shadowCastingMode = model.castShadows ? ShadowCastingMode.On : ShadowCastingMode.Off; |
| | 179 | |
|
| 87 | 180 | | if (meshRenderer.sharedMaterial == material) |
| 52 | 181 | | return; |
| | 182 | |
|
| 35 | 183 | | SRPBatchingHelper.OptimizeMaterial(material); |
| | 184 | |
|
| 35 | 185 | | Material oldMaterial = meshRenderer.sharedMaterial; |
| 35 | 186 | | meshRenderer.sharedMaterial = material; |
| | 187 | |
|
| 35 | 188 | | DataStore.i.sceneWorldObjects.RemoveMaterial(scene.sceneData.sceneNumber, entity.entityId, oldMaterial); |
| 35 | 189 | | DataStore.i.sceneWorldObjects.AddMaterial(scene.sceneData.sceneNumber, entity.entityId, material); |
| 35 | 190 | | } |
| | 191 | |
|
| | 192 | | private void OnShapeUpdated(IDCLEntity entity) |
| | 193 | | { |
| 41 | 194 | | if (entity != null) |
| 41 | 195 | | InitMaterial(entity); |
| 41 | 196 | | } |
| | 197 | |
|
| | 198 | | void OnMaterialDetached(IDCLEntity entity) |
| | 199 | | { |
| 44 | 200 | | if (entity.meshRootGameObject == null) |
| 11 | 201 | | return; |
| | 202 | |
|
| 33 | 203 | | entity.OnShapeUpdated -= OnShapeUpdated; |
| | 204 | |
|
| 33 | 205 | | var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>(); |
| | 206 | |
|
| 33 | 207 | | if (meshRenderer && meshRenderer.sharedMaterial == material) |
| 31 | 208 | | meshRenderer.sharedMaterial = null; |
| | 209 | |
|
| 33 | 210 | | DataStore.i.sceneWorldObjects.RemoveMaterial(scene.sceneData.sceneNumber, entity.entityId, material); |
| 33 | 211 | | } |
| | 212 | |
|
| | 213 | | public override void Dispose() |
| | 214 | | { |
| 41 | 215 | | isDisposed = true; |
| | 216 | |
|
| 41 | 217 | | dclTexture?.DetachFrom(this); |
| | 218 | |
|
| 62 | 219 | | while (attachedEntities != null && attachedEntities.Count > 0 ) |
| | 220 | | { |
| 21 | 221 | | DetachFrom(attachedEntities.First()); |
| | 222 | | } |
| | 223 | |
|
| 41 | 224 | | Object.Destroy(material); |
| | 225 | |
|
| 41 | 226 | | dclTextureFetcher.Dispose(); |
| 41 | 227 | | base.Dispose(); |
| 41 | 228 | | } |
| | 229 | | } |
| | 230 | | } |