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