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