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