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