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