| | 1 | | using DCL; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Assertions; |
| | 9 | | using UnityEngine.TestTools; |
| | 10 | |
|
| | 11 | | public class PBRMaterialShould : IntegrationTestSuite_Legacy |
| | 12 | | { |
| | 13 | | [UnitySetUp] |
| | 14 | | protected override IEnumerator SetUp() |
| | 15 | | { |
| 8 | 16 | | yield return base.SetUp(); |
| 8 | 17 | | Environment.i.world.sceneBoundsChecker.Stop(); |
| 8 | 18 | | } |
| | 19 | |
|
| | 20 | | [UnityTest] |
| | 21 | | public IEnumerator NotDestroySharedTextureWhenDisposed() |
| | 22 | | { |
| 1 | 23 | | DCLTexture texture = |
| | 24 | | TestHelpers.CreateDCLTexture(scene, TestAssetsUtils.GetPath() + "/Images/atlas.png"); |
| | 25 | |
|
| 1 | 26 | | yield return texture.routine; |
| | 27 | |
|
| 1 | 28 | | PBRMaterial mat = TestHelpers.CreateEntityWithPBRMaterial(scene, |
| | 29 | | new PBRMaterial.Model |
| | 30 | | { |
| | 31 | | albedoTexture = texture.id, |
| | 32 | | metallic = 0, |
| | 33 | | roughness = 1, |
| | 34 | | }, |
| | 35 | | out IDCLEntity entity1); |
| | 36 | |
|
| 1 | 37 | | yield return mat.routine; |
| | 38 | |
|
| 1 | 39 | | PBRMaterial mat2 = TestHelpers.CreateEntityWithPBRMaterial(scene, |
| | 40 | | new PBRMaterial.Model |
| | 41 | | { |
| | 42 | | albedoTexture = texture.id, |
| | 43 | | metallic = 0, |
| | 44 | | roughness = 1, |
| | 45 | | }, |
| | 46 | | out IDCLEntity entity2); |
| | 47 | |
|
| 1 | 48 | | yield return mat2.routine; |
| | 49 | |
|
| 1 | 50 | | TestHelpers.SharedComponentDispose(mat); |
| 1 | 51 | | Assert.IsTrue(texture.texture != null, "Texture should persist because is used by the other material!!"); |
| 1 | 52 | | } |
| | 53 | |
|
| | 54 | | [UnityTest] |
| | 55 | | public IEnumerator BeCreatedProperly() |
| | 56 | | { |
| 1 | 57 | | DCLTexture texture = |
| | 58 | | TestHelpers.CreateDCLTexture(scene, TestAssetsUtils.GetPath() + "/Images/atlas.png"); |
| | 59 | |
|
| 1 | 60 | | yield return texture.routine; |
| | 61 | |
|
| 1 | 62 | | PBRMaterial matPBR = TestHelpers.CreateEntityWithPBRMaterial(scene, |
| | 63 | | new PBRMaterial.Model |
| | 64 | | { |
| | 65 | | albedoTexture = texture.id, |
| | 66 | | metallic = 0, |
| | 67 | | roughness = 1, |
| | 68 | | }, |
| | 69 | | out IDCLEntity entity); |
| | 70 | |
|
| 1 | 71 | | yield return matPBR.routine; |
| | 72 | |
|
| 1 | 73 | | Assert.IsTrue(entity.meshRootGameObject != null, |
| | 74 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 75 | |
|
| 1 | 76 | | var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>(); |
| 1 | 77 | | Assert.IsTrue(meshRenderer != null, "MeshRenderer must exist"); |
| | 78 | |
|
| 1 | 79 | | var assignedMaterial = meshRenderer.sharedMaterial; |
| 1 | 80 | | Assert.IsTrue(meshRenderer != null, "MeshRenderer.sharedMaterial must be the same as assignedMaterial"); |
| 1 | 81 | | Assert.AreEqual(assignedMaterial, matPBR.material, "Assigned material"); |
| | 82 | |
|
| 1 | 83 | | var loadedTexture = meshRenderer.sharedMaterial.GetTexture("_BaseMap"); |
| 1 | 84 | | Assert.IsTrue(loadedTexture != null, "Texture must be loaded"); |
| 1 | 85 | | Assert.AreEqual(texture.texture, loadedTexture, "Texture data must be correct"); |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | [UnityTest] |
| | 89 | | public IEnumerator BeUpdatedProperly() |
| | 90 | | { |
| 1 | 91 | | string entityId = "1"; |
| 1 | 92 | | string materialID = "a-material"; |
| | 93 | |
|
| | 94 | | // Instantiate entity with default PBR Material |
| 1 | 95 | | TestHelpers.InstantiateEntityWithMaterial(scene, entityId, Vector3.zero, |
| | 96 | | new PBRMaterial.Model(), materialID); |
| | 97 | |
|
| 1 | 98 | | var materialComponent = scene.disposableComponents[materialID] as DCL.Components.PBRMaterial; |
| | 99 | |
|
| 1 | 100 | | yield return materialComponent.routine; |
| | 101 | |
|
| 1 | 102 | | Assert.IsTrue(materialComponent is DCL.Components.PBRMaterial, "material is PBRMaterial"); |
| | 103 | |
|
| | 104 | | // Check if material initialized correctly |
| | 105 | | { |
| 1 | 106 | | Assert.IsTrue(scene.entities[entityId].meshRootGameObject != null, |
| | 107 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 108 | |
|
| 1 | 109 | | var meshRenderer = scene.entities[entityId].meshRootGameObject.GetComponent<MeshRenderer>(); |
| | 110 | |
|
| 1 | 111 | | Assert.IsTrue(meshRenderer != null, "MeshRenderer must exist"); |
| | 112 | |
|
| 1 | 113 | | var assignedMaterial = meshRenderer.sharedMaterial; |
| 1 | 114 | | Assert.IsTrue(meshRenderer != null, "MeshRenderer.sharedMaterial must be the same as assignedMaterial"); |
| | 115 | |
|
| 1 | 116 | | Assert.AreEqual(assignedMaterial, materialComponent.material, "Assigned material"); |
| | 117 | | } |
| | 118 | |
|
| | 119 | | // Check default properties |
| | 120 | | { |
| | 121 | | // Texture |
| 1 | 122 | | Assert.IsTrue(materialComponent.material.GetTexture("_BaseMap") == null); |
| | 123 | |
|
| | 124 | | // Colors |
| 1 | 125 | | Assert.AreEqual("FFFFFF", ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_BaseColor"))); |
| 1 | 126 | | Assert.AreEqual("000000", |
| | 127 | | ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_EmissionColor"))); |
| 1 | 128 | | Assert.AreEqual("FFFFFF", |
| | 129 | | ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_SpecColor"))); |
| | 130 | |
|
| | 131 | | // Other properties |
| 1 | 132 | | Assert.AreApproximatelyEqual(0.5f, materialComponent.material.GetFloat("_Metallic")); |
| 1 | 133 | | Assert.AreApproximatelyEqual(0.5f, materialComponent.material.GetFloat("_Smoothness")); |
| 1 | 134 | | Assert.AreApproximatelyEqual(1.0f, materialComponent.material.GetFloat("_EnvironmentReflections")); |
| 1 | 135 | | Assert.AreApproximatelyEqual(1.0f, materialComponent.material.GetFloat("_SpecularHighlights")); |
| 1 | 136 | | Assert.AreApproximatelyEqual(.0f, materialComponent.material.GetFloat("_AlphaClip")); |
| | 137 | | } |
| | 138 | |
|
| | 139 | | // Update material |
| 1 | 140 | | DCLTexture texture = |
| | 141 | | TestHelpers.CreateDCLTexture(scene, TestAssetsUtils.GetPath() + "/Images/atlas.png"); |
| | 142 | |
|
| 1 | 143 | | yield return texture.routine; |
| | 144 | |
|
| | 145 | | Color color1, color2, color3; |
| | 146 | |
|
| 1 | 147 | | ColorUtility.TryParseHtmlString("#99deff", out color1); |
| 1 | 148 | | ColorUtility.TryParseHtmlString("#42f4aa", out color2); |
| 1 | 149 | | ColorUtility.TryParseHtmlString("#601121", out color3); |
| | 150 | |
|
| 1 | 151 | | scene.SharedComponentUpdate(materialID, JsonUtility.ToJson(new DCL.Components.PBRMaterial.Model |
| | 152 | | { |
| | 153 | | albedoTexture = texture.id, |
| | 154 | | albedoColor = color1, |
| | 155 | | emissiveColor = color2, |
| | 156 | | emissiveIntensity = 1, |
| | 157 | | reflectivityColor = color3, |
| | 158 | | metallic = 0.37f, |
| | 159 | | roughness = 0.9f, |
| | 160 | | microSurface = 0.4f, |
| | 161 | | specularIntensity = 2f, |
| | 162 | | transparencyMode = 2, |
| | 163 | | })); |
| | 164 | |
|
| 1 | 165 | | yield return materialComponent.routine; |
| | 166 | |
|
| | 167 | | // Check updated properties |
| | 168 | | { |
| | 169 | | // Texture |
| 1 | 170 | | Assert.IsTrue(materialComponent.material.GetTexture("_BaseMap") != null, "texture is null!"); |
| | 171 | |
|
| | 172 | | // Colors |
| 1 | 173 | | Assert.AreEqual("99DEFF", ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_BaseColor"))); |
| 1 | 174 | | Assert.AreEqual("42F4AA", |
| | 175 | | ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_EmissionColor"))); |
| 1 | 176 | | Assert.AreEqual("601121", |
| | 177 | | ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_SpecColor"))); |
| | 178 | |
|
| | 179 | | // Other properties |
| 1 | 180 | | Assert.AreApproximatelyEqual(0.37f, materialComponent.material.GetFloat("_Metallic")); |
| 1 | 181 | | Assert.AreApproximatelyEqual(0.1f, materialComponent.material.GetFloat("_Smoothness")); |
| 1 | 182 | | Assert.AreApproximatelyEqual(0.4f, materialComponent.material.GetFloat("_EnvironmentReflections")); |
| 1 | 183 | | Assert.AreApproximatelyEqual(2.0f, materialComponent.material.GetFloat("_SpecularHighlights")); |
| 1 | 184 | | Assert.AreApproximatelyEqual(.0f, materialComponent.material.GetFloat("_AlphaClip")); |
| 1 | 185 | | Assert.AreEqual((int) UnityEngine.Rendering.BlendMode.SrcAlpha, |
| | 186 | | materialComponent.material.GetInt("_SrcBlend")); |
| 1 | 187 | | Assert.AreEqual((int) UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha, |
| | 188 | | materialComponent.material.GetInt("_DstBlend")); |
| 1 | 189 | | Assert.AreEqual(0, materialComponent.material.GetInt("_ZWrite")); |
| | 190 | | } |
| 1 | 191 | | } |
| | 192 | |
|
| | 193 | | [UnityTest] |
| | 194 | | public IEnumerator BeSharedProperly() |
| | 195 | | { |
| | 196 | | // Create first entity with material |
| 1 | 197 | | string firstEntityID = "1"; |
| 1 | 198 | | string firstMaterialID = "a-material"; |
| | 199 | |
|
| 1 | 200 | | TestHelpers.InstantiateEntityWithMaterial(scene, firstEntityID, Vector3.zero, |
| | 201 | | new DCL.Components.PBRMaterial.Model |
| | 202 | | { |
| | 203 | | metallic = 0.3f, |
| | 204 | | }, firstMaterialID); |
| | 205 | |
|
| 1 | 206 | | Assert.IsTrue(scene.entities[firstEntityID].meshRootGameObject != null, |
| | 207 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 208 | |
|
| | 209 | | // Create second entity with material |
| 1 | 210 | | string secondEntityID = "2"; |
| 1 | 211 | | string secondMaterialID = "b-material"; |
| | 212 | |
|
| 1 | 213 | | TestHelpers.InstantiateEntityWithMaterial(scene, secondEntityID, Vector3.zero, |
| | 214 | | new DCL.Components.PBRMaterial.Model |
| | 215 | | { |
| | 216 | | metallic = 0.66f, |
| | 217 | | }, secondMaterialID); |
| | 218 | |
|
| 1 | 219 | | Assert.IsTrue(scene.entities[secondEntityID].meshRootGameObject != null, |
| | 220 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 221 | |
|
| | 222 | | // Create third entity and assign 1st material |
| 1 | 223 | | string thirdEntityID = "3"; |
| | 224 | |
|
| 1 | 225 | | TestHelpers.InstantiateEntityWithShape(scene, thirdEntityID, DCL.Models.CLASS_ID.BOX_SHAPE, Vector3.zero); |
| 1 | 226 | | scene.SharedComponentAttach( |
| | 227 | | thirdEntityID, |
| | 228 | | firstMaterialID |
| | 229 | | ); |
| | 230 | |
|
| 1 | 231 | | Assert.IsTrue(scene.entities[thirdEntityID].meshRootGameObject != null, |
| | 232 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 233 | |
|
| | 234 | | // Check renderers material references |
| 1 | 235 | | var firstRenderer = scene.entities[firstEntityID].meshRootGameObject.GetComponent<MeshRenderer>(); |
| 1 | 236 | | var secondRenderer = scene.entities[secondEntityID].meshRootGameObject.GetComponent<MeshRenderer>(); |
| 1 | 237 | | var thirdRenderer = scene.entities[thirdEntityID].meshRootGameObject.GetComponent<MeshRenderer>(); |
| 1 | 238 | | Assert.IsTrue(firstRenderer.sharedMaterial != secondRenderer.sharedMaterial, |
| | 239 | | "1st and 2nd entities should have different materials"); |
| 1 | 240 | | Assert.IsTrue(firstRenderer.sharedMaterial == thirdRenderer.sharedMaterial, |
| | 241 | | "1st and 3rd entities should have the same material"); |
| | 242 | |
|
| 1 | 243 | | yield break; |
| | 244 | | } |
| | 245 | |
|
| | 246 | | [UnityTest] |
| | 247 | | public IEnumerator AffectDifferentEntitiesCorrectly() |
| | 248 | | { |
| | 249 | | // Create first entity with material |
| 1 | 250 | | PBRMaterial material1 = TestHelpers.CreateEntityWithPBRMaterial(scene, |
| | 251 | | new PBRMaterial.Model |
| | 252 | | { |
| | 253 | | metallic = 0.3f, |
| | 254 | | }, out IDCLEntity entity1); |
| | 255 | |
|
| 1 | 256 | | Assert.IsTrue(entity1.meshRootGameObject != null, |
| | 257 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 258 | |
|
| | 259 | | // Create second entity with material |
| 1 | 260 | | PBRMaterial material2 = TestHelpers.CreateEntityWithPBRMaterial(scene, |
| | 261 | | new PBRMaterial.Model |
| | 262 | | { |
| | 263 | | metallic = 0.66f, |
| | 264 | | }, out IDCLEntity entity2); |
| | 265 | |
|
| 1 | 266 | | Assert.IsTrue(entity2.meshRootGameObject != null, |
| | 267 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 268 | |
|
| | 269 | | // Create third entity and assign 1st material |
| 1 | 270 | | var boxShape = TestHelpers.CreateEntityWithBoxShape(scene, Vector3.zero); |
| 1 | 271 | | var entity3 = boxShape.attachedEntities.First(); |
| | 272 | |
|
| 1 | 273 | | scene.SharedComponentAttach( |
| | 274 | | entity3.entityId, |
| | 275 | | material1.id |
| | 276 | | ); |
| | 277 | |
|
| 1 | 278 | | Assert.IsTrue(entity3.meshRootGameObject != null, |
| | 279 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 280 | |
|
| | 281 | | // Check renderers material references |
| 1 | 282 | | var firstRenderer = entity1.meshRootGameObject.GetComponent<MeshRenderer>(); |
| 1 | 283 | | var secondRenderer = entity2.meshRootGameObject.GetComponent<MeshRenderer>(); |
| 1 | 284 | | var thirdRenderer = entity3.meshRootGameObject.GetComponent<MeshRenderer>(); |
| | 285 | |
|
| 1 | 286 | | Assert.IsTrue(firstRenderer.sharedMaterial != secondRenderer.sharedMaterial, |
| | 287 | | "1st and 2nd entities should have different materials"); |
| 1 | 288 | | Assert.IsTrue(firstRenderer.sharedMaterial == thirdRenderer.sharedMaterial, |
| | 289 | | "1st and 3rd entities should have the same material"); |
| | 290 | |
|
| | 291 | | // Check material properties before updating them |
| 1 | 292 | | Assert.AreApproximatelyEqual(0.3f, firstRenderer.sharedMaterial.GetFloat("_Metallic")); |
| 1 | 293 | | Assert.AreApproximatelyEqual(0.66f, secondRenderer.sharedMaterial.GetFloat("_Metallic")); |
| | 294 | |
|
| | 295 | | // Update material properties |
| 1 | 296 | | scene.SharedComponentUpdate(material1.id, JsonUtility.ToJson(new PBRMaterial.Model |
| | 297 | | { |
| | 298 | | metallic = 0.95f |
| | 299 | | })); |
| | 300 | |
|
| 1 | 301 | | Assert.IsTrue(firstRenderer.sharedMaterial != null); |
| | 302 | |
|
| 1 | 303 | | yield return material1.routine; |
| | 304 | |
|
| 1 | 305 | | Assert.IsTrue(firstRenderer.sharedMaterial != null); |
| | 306 | | // Check material properties after updating them |
| 1 | 307 | | Assert.AreApproximatelyEqual(0.95f, firstRenderer.sharedMaterial.GetFloat("_Metallic")); |
| 1 | 308 | | Assert.AreApproximatelyEqual(0.66f, secondRenderer.sharedMaterial.GetFloat("_Metallic")); |
| 1 | 309 | | } |
| | 310 | |
|
| | 311 | | [UnityTest] |
| | 312 | | public IEnumerator WorkCorrectlyWhenAttachedBeforeShape() |
| | 313 | | { |
| 1 | 314 | | IDCLEntity entity = TestHelpers.CreateSceneEntity(scene); |
| | 315 | |
|
| 1 | 316 | | DCLTexture dclTexture = TestHelpers.CreateDCLTexture( |
| | 317 | | scene, |
| | 318 | | TestAssetsUtils.GetPath() + "/Images/atlas.png", |
| | 319 | | DCLTexture.BabylonWrapMode.CLAMP, |
| | 320 | | FilterMode.Bilinear); |
| | 321 | |
|
| 1 | 322 | | yield return dclTexture.routine; |
| | 323 | |
|
| 1 | 324 | | PBRMaterial mat = TestHelpers.SharedComponentCreate<PBRMaterial, PBRMaterial.Model>(scene, |
| | 325 | | CLASS_ID.PBR_MATERIAL, |
| | 326 | | new PBRMaterial.Model |
| | 327 | | { |
| | 328 | | albedoTexture = dclTexture.id, |
| | 329 | | metallic = 0, |
| | 330 | | roughness = 1, |
| | 331 | | } |
| | 332 | | ); |
| | 333 | |
|
| 1 | 334 | | yield return mat.routine; |
| | 335 | |
|
| 1 | 336 | | TestHelpers.SharedComponentAttach(mat, entity); |
| | 337 | |
|
| 1 | 338 | | SphereShape shape = TestHelpers.SharedComponentCreate<SphereShape, SphereShape.Model>(scene, |
| | 339 | | CLASS_ID.SPHERE_SHAPE, |
| | 340 | | new SphereShape.Model { }); |
| | 341 | |
|
| 1 | 342 | | yield return shape.routine; |
| | 343 | |
|
| 1 | 344 | | TestHelpers.SharedComponentAttach(shape, entity); |
| | 345 | |
|
| 1 | 346 | | Assert.IsTrue(entity.meshRootGameObject != null); |
| 1 | 347 | | Assert.IsTrue(entity.meshRootGameObject.GetComponent<MeshRenderer>() != null); |
| 1 | 348 | | Assert.AreEqual(entity.meshRootGameObject.GetComponent<MeshRenderer>().sharedMaterial, mat.material); |
| 1 | 349 | | } |
| | 350 | |
|
| | 351 | | [UnityTest] |
| | 352 | | public IEnumerator DefaultMissingValuesPropertyOnUpdate() |
| | 353 | | { |
| | 354 | | Color color1; |
| 1 | 355 | | ColorUtility.TryParseHtmlString("#808080", out color1); |
| | 356 | |
|
| | 357 | | // 1. Create component with non-default configs |
| 1 | 358 | | PBRMaterial PBRMaterialComponent = TestHelpers.SharedComponentCreate<PBRMaterial, PBRMaterial.Model>(scene, |
| | 359 | | CLASS_ID.PBR_MATERIAL, |
| | 360 | | new PBRMaterial.Model |
| | 361 | | { |
| | 362 | | albedoColor = color1, |
| | 363 | | metallic = 0.3f, |
| | 364 | | directIntensity = 0.1f, |
| | 365 | | specularIntensity = 3f |
| | 366 | | }); |
| | 367 | |
|
| 1 | 368 | | yield return PBRMaterialComponent.routine; |
| | 369 | |
|
| | 370 | | // 2. Check configured values |
| 1 | 371 | | Assert.AreEqual(color1, PBRMaterialComponent.GetModel().albedoColor); |
| 1 | 372 | | Assert.AreEqual(0.3f, PBRMaterialComponent.GetModel().metallic); |
| 1 | 373 | | Assert.AreEqual(0.1f, PBRMaterialComponent.GetModel().directIntensity); |
| 1 | 374 | | Assert.AreEqual(3f, PBRMaterialComponent.GetModel().specularIntensity); |
| | 375 | |
|
| | 376 | | // 3. Update component with missing values |
| 1 | 377 | | scene.SharedComponentUpdate(PBRMaterialComponent.id, JsonUtility.ToJson(new PBRMaterial.Model { })); |
| | 378 | |
|
| 1 | 379 | | yield return PBRMaterialComponent.routine; |
| | 380 | |
|
| | 381 | | // 4. Check defaulted values |
| 1 | 382 | | Assert.AreEqual(Color.white, PBRMaterialComponent.GetModel().albedoColor); |
| 1 | 383 | | Assert.AreEqual(0.5f, PBRMaterialComponent.GetModel().metallic); |
| 1 | 384 | | Assert.AreEqual(1, PBRMaterialComponent.GetModel().directIntensity); |
| 1 | 385 | | Assert.AreEqual(1f, PBRMaterialComponent.GetModel().specularIntensity); |
| 1 | 386 | | } |
| | 387 | |
|
| | 388 | | [UnityTest] |
| | 389 | | public IEnumerator GetReplacedWhenAnotherMaterialIsAttached() |
| | 390 | | { |
| 1 | 391 | | yield return TestHelpers.TestAttachedSharedComponentOfSameTypeIsReplaced<PBRMaterial.Model, PBRMaterial>( |
| | 392 | | scene, CLASS_ID.PBR_MATERIAL); |
| 1 | 393 | | } |
| | 394 | | } |