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