| | 1 | | using Cysharp.Threading.Tasks; |
| | 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 | | using DCL.Shaders; |
| | 10 | | using Decentraland.Sdk.Ecs6; |
| | 11 | | using MainScripts.DCL.Components; |
| | 12 | | using System; |
| | 13 | | using Object = UnityEngine.Object; |
| | 14 | |
|
| | 15 | | namespace DCL.Components |
| | 16 | | { |
| | 17 | | public class PBRMaterial : BaseDisposable |
| | 18 | | { |
| | 19 | | [System.Serializable] |
| | 20 | | public class Model : BaseModel |
| | 21 | | { |
| | 22 | | [Range(0f, 1f)] |
| 89 | 23 | | public float alphaTest = 0.5f; |
| | 24 | |
|
| 89 | 25 | | public Color albedoColor = Color.white; |
| | 26 | | public string albedoTexture; |
| 89 | 27 | | public float metallic = 0.5f; |
| 89 | 28 | | public float roughness = 0.5f; |
| 89 | 29 | | public float microSurface = 1f; // Glossiness |
| 89 | 30 | | public float specularIntensity = 1f; |
| | 31 | |
|
| | 32 | | public string alphaTexture; |
| | 33 | | public string emissiveTexture; |
| 89 | 34 | | public Color emissiveColor = Color.black; |
| 89 | 35 | | public float emissiveIntensity = 2f; |
| 89 | 36 | | public Color reflectivityColor = Color.white; |
| 89 | 37 | | public float directIntensity = 1f; |
| | 38 | | public string bumpTexture; |
| 89 | 39 | | public bool castShadows = true; |
| | 40 | |
|
| | 41 | | [Range(0, 4)] |
| 89 | 42 | | public int transparencyMode = 4; // 0: OPAQUE; 1: ALPHATEST; 2: ALPHBLEND; 3: ALPHATESTANDBLEND; 4: AUTO (En |
| | 43 | |
|
| 31 | 44 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 45 | |
|
| | 46 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 47 | | { |
| 0 | 48 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.Material) |
| 0 | 49 | | return Utils.SafeUnimplemented<PBRMaterial, Model>(expected: ComponentBodyPayload.PayloadOneofCase.M |
| | 50 | |
|
| 0 | 51 | | var pb = new Model(); |
| 0 | 52 | | if (pbModel.Material.HasMetallic) pb.metallic = pbModel.Material.Metallic; |
| 0 | 53 | | if (pbModel.Material.HasRoughness) pb.roughness = pbModel.Material.Roughness; |
| 0 | 54 | | if (pbModel.Material.HasAlphaTest) pb.alphaTest = pbModel.Material.AlphaTest; |
| 0 | 55 | | if (pbModel.Material.HasDirectIntensity) pb.directIntensity = pbModel.Material.DirectIntensity; |
| 0 | 56 | | if (pbModel.Material.HasMicroSurface) pb.microSurface = pbModel.Material.MicroSurface; |
| 0 | 57 | | if (pbModel.Material.HasSpecularIntensity) pb.specularIntensity = pbModel.Material.SpecularIntensity; |
| 0 | 58 | | if (pbModel.Material.HasAlbedoTexture) pb.albedoTexture = pbModel.Material.AlbedoTexture; |
| 0 | 59 | | if (pbModel.Material.HasAlphaTexture) pb.alphaTexture = pbModel.Material.AlphaTexture; |
| 0 | 60 | | if (pbModel.Material.HasBumpTexture) pb.bumpTexture = pbModel.Material.BumpTexture; |
| 0 | 61 | | if (pbModel.Material.HasCastShadows) pb.castShadows = pbModel.Material.CastShadows; |
| 0 | 62 | | if (pbModel.Material.HasEmissiveIntensity) pb.emissiveIntensity = pbModel.Material.EmissiveIntensity; |
| 0 | 63 | | if (pbModel.Material.HasEmissiveTexture) pb.emissiveTexture = pbModel.Material.EmissiveTexture; |
| 0 | 64 | | if (pbModel.Material.HasTransparencyMode) pb.transparencyMode = (int)pbModel.Material.TransparencyMode; |
| 0 | 65 | | if (pbModel.Material.AlbedoColor != null) pb.albedoColor = pbModel.Material.AlbedoColor.AsUnityColor(); |
| 0 | 66 | | if (pbModel.Material.EmissiveColor != null) pb.emissiveColor = pbModel.Material.EmissiveColor.AsUnityCol |
| 0 | 67 | | if (pbModel.Material.ReflectivityColor != null) pb.reflectivityColor = pbModel.Material.ReflectivityColo |
| | 68 | |
|
| 0 | 69 | | return pb; |
| | 70 | | } |
| | 71 | | } |
| | 72 | |
|
| | 73 | | enum TransparencyMode |
| | 74 | | { |
| | 75 | | OPAQUE, |
| | 76 | | ALPHA_TEST, |
| | 77 | | ALPHA_BLEND, |
| | 78 | | ALPHA_TEST_AND_BLEND, |
| | 79 | | AUTO |
| | 80 | | } |
| | 81 | |
|
| | 82 | | private enum TextureType |
| | 83 | | { |
| | 84 | | Albedo, |
| | 85 | | Alpha, |
| | 86 | | Emissive, |
| | 87 | | Bump |
| | 88 | | } |
| | 89 | |
|
| 958 | 90 | | public Material material { get; private set; } |
| | 91 | | private string currentMaterialResourcesFilename; |
| | 92 | |
|
| | 93 | | const string MATERIAL_RESOURCES_PATH = "Materials/"; |
| | 94 | | const string PBR_MATERIAL_NAME = "ShapeMaterial"; |
| | 95 | |
|
| 27 | 96 | | private readonly DCLTexture[] textures = new DCLTexture[4]; |
| | 97 | |
|
| 27 | 98 | | private readonly DCLTexture.Fetcher[] dclTextureFetcher = new DCLTexture.Fetcher[] |
| | 99 | | { |
| | 100 | | new DCLTexture.Fetcher(), |
| | 101 | | new DCLTexture.Fetcher(), |
| | 102 | | new DCLTexture.Fetcher(), |
| | 103 | | new DCLTexture.Fetcher() |
| | 104 | | }; |
| | 105 | |
|
| 27 | 106 | | public PBRMaterial() |
| | 107 | | { |
| 27 | 108 | | model = new Model(); |
| | 109 | |
|
| 27 | 110 | | LoadMaterial(PBR_MATERIAL_NAME); |
| | 111 | |
|
| 27 | 112 | | OnAttach += OnMaterialAttached; |
| 27 | 113 | | OnDetach += OnMaterialDetached; |
| 27 | 114 | | } |
| | 115 | |
|
| 8 | 116 | | new public Model GetModel() { return (Model) model; } |
| | 117 | |
|
| 0 | 118 | | public override int GetClassId() { return (int) CLASS_ID.PBR_MATERIAL; } |
| | 119 | |
|
| | 120 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 121 | | { |
| 27 | 122 | | if (attachedEntities.Contains(entity)) |
| | 123 | | { |
| 0 | 124 | | return; |
| | 125 | | } |
| | 126 | |
|
| 27 | 127 | | scene.componentsManagerLegacy.RemoveSharedComponent(entity, (typeof(BasicMaterial))); |
| 27 | 128 | | base.AttachTo(entity); |
| 27 | 129 | | } |
| | 130 | |
|
| | 131 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 132 | | { |
| 31 | 133 | | Model model = (Model) newModel; |
| | 134 | |
|
| 31 | 135 | | LoadMaterial(PBR_MATERIAL_NAME); |
| | 136 | |
|
| 31 | 137 | | material.SetColor(ShaderUtils.BaseColor, model.albedoColor); |
| | 138 | |
|
| 31 | 139 | | if (model.emissiveColor != Color.clear && model.emissiveColor != Color.black) |
| | 140 | | { |
| 4 | 141 | | material.EnableKeyword("_EMISSION"); |
| | 142 | | } |
| | 143 | |
|
| | 144 | | // METALLIC/SPECULAR CONFIGURATIONS |
| 31 | 145 | | material.SetColor(ShaderUtils.EmissionColor, model.emissiveColor * model.emissiveIntensity); |
| 31 | 146 | | material.SetColor(ShaderUtils.SpecColor, model.reflectivityColor); |
| | 147 | |
|
| 31 | 148 | | material.SetFloat(ShaderUtils.Metallic, model.metallic); |
| 31 | 149 | | material.SetFloat(ShaderUtils.Smoothness, 1 - model.roughness); |
| 31 | 150 | | material.SetFloat(ShaderUtils.EnvironmentReflections, model.microSurface); |
| 31 | 151 | | material.SetFloat(ShaderUtils.SpecularHighlights, model.specularIntensity * model.directIntensity); |
| | 152 | |
|
| | 153 | |
|
| | 154 | | // FETCH AND LOAD EMISSIVE TEXTURE |
| 31 | 155 | | var fetchEmission = FetchTexture(ShaderUtils.EmissionMap, model.emissiveTexture, (int)TextureType.Emissive); |
| | 156 | |
|
| 31 | 157 | | SetupTransparencyMode(); |
| | 158 | |
|
| | 159 | | // FETCH AND LOAD TEXTURES |
| 31 | 160 | | var fetchBaseMap = FetchTexture(ShaderUtils.BaseMap, model.albedoTexture, (int)TextureType.Albedo); |
| 31 | 161 | | var fetchAlpha = FetchTexture(ShaderUtils.AlphaTexture, model.alphaTexture, (int)TextureType.Alpha); |
| 31 | 162 | | var fetchBump = FetchTexture(ShaderUtils.BumpMap, model.bumpTexture, (int)TextureType.Bump); |
| | 163 | |
|
| 31 | 164 | | yield return UniTask.WhenAll(fetchEmission, fetchBaseMap, fetchAlpha, fetchBump).ToCoroutine(e => |
| | 165 | | { |
| 0 | 166 | | if (e is not OperationCanceledException) |
| 0 | 167 | | throw e; |
| 0 | 168 | | }); |
| | 169 | |
|
| 118 | 170 | | foreach (IDCLEntity entity in attachedEntities) |
| 28 | 171 | | InitMaterial(entity); |
| 31 | 172 | | } |
| | 173 | |
|
| | 174 | | private void SetupTransparencyMode() |
| | 175 | | { |
| 31 | 176 | | Model model = (Model) this.model; |
| | 177 | |
|
| | 178 | | // Reset shader keywords |
| 31 | 179 | | material.DisableKeyword("_ALPHATEST_ON"); // Cut Out Transparency |
| 31 | 180 | | material.DisableKeyword("_ALPHABLEND_ON"); // Fade Transparency |
| 31 | 181 | | material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); // Transparent |
| | 182 | |
|
| 31 | 183 | | TransparencyMode transparencyMode = (TransparencyMode) model.transparencyMode; |
| | 184 | |
|
| 31 | 185 | | if (transparencyMode == TransparencyMode.AUTO) |
| | 186 | | { |
| 23 | 187 | | if (!string.IsNullOrEmpty(model.alphaTexture) || model.albedoColor.a < 1f) //AlphaBlend |
| | 188 | | { |
| 5 | 189 | | transparencyMode = TransparencyMode.ALPHA_BLEND; |
| | 190 | | } |
| | 191 | | else // Opaque |
| | 192 | | { |
| 18 | 193 | | transparencyMode = TransparencyMode.OPAQUE; |
| | 194 | | } |
| | 195 | | } |
| | 196 | |
|
| | 197 | | switch (transparencyMode) |
| | 198 | | { |
| | 199 | | case TransparencyMode.OPAQUE: |
| 18 | 200 | | material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Geometry; |
| 18 | 201 | | material.SetFloat(ShaderUtils.AlphaClip, 0); |
| 18 | 202 | | break; |
| | 203 | | case TransparencyMode.ALPHA_TEST: // ALPHATEST |
| 0 | 204 | | material.EnableKeyword("_ALPHATEST_ON"); |
| | 205 | |
|
| 0 | 206 | | material.SetInt(ShaderUtils.SrcBlend, (int) UnityEngine.Rendering.BlendMode.One); |
| 0 | 207 | | material.SetInt(ShaderUtils.DstBlend, (int) UnityEngine.Rendering.BlendMode.Zero); |
| 0 | 208 | | material.SetInt(ShaderUtils.ZWrite, 1); |
| 0 | 209 | | material.SetFloat(ShaderUtils.AlphaClip, 1); |
| 0 | 210 | | material.SetFloat(ShaderUtils.Cutoff, model.alphaTest); |
| 0 | 211 | | material.SetInt("_Surface", 0); |
| 0 | 212 | | material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.AlphaTest; |
| 0 | 213 | | break; |
| | 214 | | case TransparencyMode.ALPHA_BLEND: // ALPHABLEND |
| 13 | 215 | | material.EnableKeyword("_ALPHABLEND_ON"); |
| | 216 | |
|
| 13 | 217 | | material.SetInt(ShaderUtils.SrcBlend, (int) UnityEngine.Rendering.BlendMode.SrcAlpha); |
| 13 | 218 | | material.SetInt(ShaderUtils.DstBlend, (int) UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); |
| 13 | 219 | | material.SetInt(ShaderUtils.ZWrite, 0); |
| 13 | 220 | | material.SetFloat(ShaderUtils.AlphaClip, 0); |
| 13 | 221 | | material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Transparent; |
| 13 | 222 | | material.SetInt("_Surface", 1); |
| 13 | 223 | | break; |
| | 224 | | case TransparencyMode.ALPHA_TEST_AND_BLEND: |
| 0 | 225 | | material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); |
| | 226 | |
|
| 0 | 227 | | material.SetInt(ShaderUtils.SrcBlend, (int) UnityEngine.Rendering.BlendMode.One); |
| 0 | 228 | | material.SetInt(ShaderUtils.DstBlend, (int) UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); |
| 0 | 229 | | material.SetInt(ShaderUtils.ZWrite, 0); |
| 0 | 230 | | material.SetFloat(ShaderUtils.AlphaClip, 1); |
| 0 | 231 | | material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Transparent; |
| 0 | 232 | | material.SetInt("_Surface", 1); |
| | 233 | | break; |
| | 234 | | } |
| 0 | 235 | | } |
| | 236 | |
|
| | 237 | | private void LoadMaterial(string resourcesFilename) |
| | 238 | | { |
| 58 | 239 | | if (material == null || currentMaterialResourcesFilename != resourcesFilename) |
| | 240 | | { |
| 27 | 241 | | if (material != null) |
| 0 | 242 | | Object.Destroy(material); |
| | 243 | |
|
| 27 | 244 | | material = new Material(Utils.EnsureResourcesMaterial(MATERIAL_RESOURCES_PATH + resourcesFilename)); |
| | 245 | | #if UNITY_EDITOR |
| 27 | 246 | | material.name = "PBRMaterial_" + id; |
| | 247 | | #endif |
| 27 | 248 | | currentMaterialResourcesFilename = resourcesFilename; |
| | 249 | | } |
| 58 | 250 | | } |
| | 251 | |
|
| | 252 | | void OnMaterialAttached(IDCLEntity entity) |
| | 253 | | { |
| 27 | 254 | | entity.OnShapeUpdated -= OnShapeUpdated; |
| 27 | 255 | | entity.OnShapeUpdated += OnShapeUpdated; |
| | 256 | |
|
| 27 | 257 | | if (entity.meshRootGameObject != null) |
| | 258 | | { |
| 22 | 259 | | var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>(); |
| | 260 | |
|
| 22 | 261 | | if (meshRenderer != null) |
| 22 | 262 | | InitMaterial(entity); |
| | 263 | | } |
| 27 | 264 | | } |
| | 265 | |
|
| | 266 | | void InitMaterial(IDCLEntity entity) |
| | 267 | | { |
| 84 | 268 | | var meshGameObject = entity.meshRootGameObject; |
| | 269 | |
|
| 84 | 270 | | if (meshGameObject == null) |
| 14 | 271 | | return; |
| | 272 | |
|
| 70 | 273 | | var meshRenderer = meshGameObject.GetComponent<MeshRenderer>(); |
| | 274 | |
|
| 70 | 275 | | if (meshRenderer == null) |
| 0 | 276 | | return; |
| | 277 | |
|
| 70 | 278 | | Model model = (Model) this.model; |
| | 279 | |
|
| 70 | 280 | | meshRenderer.shadowCastingMode = model.castShadows ? ShadowCastingMode.On : ShadowCastingMode.Off; |
| | 281 | |
|
| 70 | 282 | | if (meshRenderer.sharedMaterial == material) |
| 47 | 283 | | return; |
| | 284 | |
|
| 23 | 285 | | Material oldMaterial = meshRenderer.sharedMaterial; |
| 23 | 286 | | meshRenderer.sharedMaterial = material; |
| 23 | 287 | | SRPBatchingHelper.OptimizeMaterial(material); |
| | 288 | |
|
| 23 | 289 | | DataStore.i.sceneWorldObjects.RemoveMaterial(scene.sceneData.sceneNumber, entity.entityId, oldMaterial); |
| 23 | 290 | | DataStore.i.sceneWorldObjects.AddMaterial(scene.sceneData.sceneNumber, entity.entityId, material); |
| 23 | 291 | | } |
| | 292 | |
|
| | 293 | | private void OnShapeUpdated(IDCLEntity entity) |
| | 294 | | { |
| 34 | 295 | | if (entity != null) |
| 34 | 296 | | InitMaterial(entity); |
| 34 | 297 | | } |
| | 298 | |
|
| | 299 | | private void OnMaterialDetached(IDCLEntity entity) |
| | 300 | | { |
| 20 | 301 | | if (entity.meshRootGameObject != null) |
| | 302 | | { |
| 5 | 303 | | entity.OnShapeUpdated -= OnShapeUpdated; |
| | 304 | |
|
| 5 | 305 | | var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>(); |
| | 306 | |
|
| 5 | 307 | | if (meshRenderer && meshRenderer.sharedMaterial == material) |
| 5 | 308 | | meshRenderer.sharedMaterial = null; |
| | 309 | | } |
| 20 | 310 | | DataStore.i.sceneWorldObjects.RemoveMaterial(scene.sceneData.sceneNumber, entity.entityId, material); |
| 20 | 311 | | } |
| | 312 | |
|
| | 313 | | private UniTask FetchTexture(int materialPropertyId, string textureComponentId, int textureType) |
| | 314 | | { |
| 124 | 315 | | if (!string.IsNullOrEmpty(textureComponentId)) |
| | 316 | | { |
| 39 | 317 | | if (!AreSameTextureComponent(textureType, textureComponentId)) |
| | 318 | | { |
| 39 | 319 | | return dclTextureFetcher[textureType] |
| | 320 | | .Fetch(scene, textureComponentId, |
| | 321 | | fetchedDCLTexture => |
| | 322 | | { |
| 39 | 323 | | if (material == null) |
| 0 | 324 | | return false; |
| | 325 | |
|
| 39 | 326 | | material.SetTexture(materialPropertyId, fetchedDCLTexture.texture); |
| 39 | 327 | | SwitchTextureComponent(textureType, fetchedDCLTexture); |
| 39 | 328 | | return true; |
| | 329 | | }); |
| | 330 | | } |
| | 331 | | } |
| | 332 | | else |
| | 333 | | { |
| 85 | 334 | | material.SetTexture(materialPropertyId, null); |
| 85 | 335 | | textures[textureType]?.DetachFrom(this); |
| 85 | 336 | | textures[textureType] = null; |
| | 337 | | } |
| | 338 | |
|
| 85 | 339 | | return new UniTask(); |
| | 340 | | } |
| | 341 | |
|
| | 342 | | bool AreSameTextureComponent(int textureType, string textureId) |
| | 343 | | { |
| 39 | 344 | | DCLTexture dclTexture = textures[textureType]; |
| 39 | 345 | | if (dclTexture == null) |
| 35 | 346 | | return false; |
| 4 | 347 | | return dclTexture.id == textureId; |
| | 348 | | } |
| | 349 | |
|
| | 350 | | void SwitchTextureComponent(int textureType, DCLTexture newTexture) |
| | 351 | | { |
| 39 | 352 | | DCLTexture dclTexture = textures[textureType]; |
| 39 | 353 | | dclTexture?.DetachFrom(this); |
| 39 | 354 | | textures[textureType] = newTexture; |
| 39 | 355 | | newTexture.AttachTo(this); |
| 39 | 356 | | } |
| | 357 | |
|
| | 358 | | public override void Dispose() |
| | 359 | | { |
| 110 | 360 | | for (int i = 0; i < dclTextureFetcher.Length; i++) |
| | 361 | | { |
| 44 | 362 | | dclTextureFetcher[i].Dispose(); |
| | 363 | | } |
| | 364 | |
|
| 110 | 365 | | for (int i = 0; i < textures.Length; i++) |
| | 366 | | { |
| 44 | 367 | | textures[i]?.DetachFrom(this); |
| 44 | 368 | | textures[i] = null; |
| | 369 | | } |
| | 370 | |
|
| 11 | 371 | | if (material != null) |
| | 372 | | { |
| | 373 | | // we make sure we detach this material from every entity |
| | 374 | | // before disposing it |
| 13 | 375 | | while ( attachedEntities.Count > 0 ) |
| | 376 | | { |
| 5 | 377 | | DetachFrom(attachedEntities.First()); |
| | 378 | | } |
| 8 | 379 | | Utils.SafeDestroy(material); |
| | 380 | | } |
| | 381 | |
|
| 11 | 382 | | base.Dispose(); |
| 11 | 383 | | } |
| | 384 | | } |
| | 385 | | } |