| | 1 | | using System.Collections; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Components; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Rendering; |
| | 8 | | using UnityEngine.TestTools; |
| | 9 | | using Assert = UnityEngine.Assertions.Assert; |
| | 10 | |
|
| | 11 | | public class BasicMaterialShould : IntegrationTestSuite_Legacy |
| | 12 | | { |
| | 13 | | [UnitySetUp] |
| | 14 | | protected override IEnumerator SetUp() |
| | 15 | | { |
| 9 | 16 | | yield return base.SetUp(); |
| 9 | 17 | | Environment.i.world.sceneBoundsChecker.Stop(); |
| 9 | 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 | | BasicMaterial mat = TestHelpers.CreateEntityWithBasicMaterial(scene, |
| | 29 | | new BasicMaterial.Model |
| | 30 | | { |
| | 31 | | texture = texture.id, |
| | 32 | | alphaTest = 1, |
| | 33 | | }, |
| | 34 | | out IDCLEntity entity1); |
| | 35 | |
|
| 1 | 36 | | yield return mat.routine; |
| | 37 | |
|
| 1 | 38 | | BasicMaterial mat2 = TestHelpers.CreateEntityWithBasicMaterial(scene, |
| | 39 | | new BasicMaterial.Model |
| | 40 | | { |
| | 41 | | texture = texture.id, |
| | 42 | | alphaTest = 1, |
| | 43 | | }, |
| | 44 | | out IDCLEntity entity2); |
| | 45 | |
|
| 1 | 46 | | yield return mat2.routine; |
| | 47 | |
|
| 1 | 48 | | TestHelpers.SharedComponentDispose(mat); |
| 1 | 49 | | Assert.IsTrue(texture.texture != null, "Texture should persist because is used by the other material!!"); |
| 1 | 50 | | } |
| | 51 | |
|
| | 52 | | [UnityTest] |
| | 53 | | public IEnumerator WorkCorrectlyWhenAttachedBeforeShape() |
| | 54 | | { |
| 1 | 55 | | IDCLEntity entity = TestHelpers.CreateSceneEntity(scene); |
| | 56 | |
|
| 1 | 57 | | DCLTexture dclTexture = TestHelpers.CreateDCLTexture( |
| | 58 | | scene, |
| | 59 | | TestAssetsUtils.GetPath() + "/Images/atlas.png", |
| | 60 | | DCLTexture.BabylonWrapMode.CLAMP, |
| | 61 | | FilterMode.Bilinear); |
| | 62 | |
|
| 1 | 63 | | yield return dclTexture.routine; |
| | 64 | |
|
| 1 | 65 | | BasicMaterial mat = TestHelpers.SharedComponentCreate<BasicMaterial, BasicMaterial.Model> |
| | 66 | | (scene, CLASS_ID.BASIC_MATERIAL, |
| | 67 | | new BasicMaterial.Model |
| | 68 | | { |
| | 69 | | texture = dclTexture.id, |
| | 70 | | alphaTest = 0.5f |
| | 71 | | }); |
| | 72 | |
|
| 1 | 73 | | yield return mat.routine; |
| | 74 | |
|
| 1 | 75 | | TestHelpers.SharedComponentAttach(mat, entity); |
| | 76 | |
|
| 1 | 77 | | SphereShape shape = TestHelpers.SharedComponentCreate<SphereShape, SphereShape.Model>(scene, |
| | 78 | | CLASS_ID.SPHERE_SHAPE, |
| | 79 | | new SphereShape.Model { }); |
| | 80 | |
|
| 1 | 81 | | TestHelpers.SharedComponentAttach(shape, entity); |
| | 82 | |
|
| 1 | 83 | | Assert.IsTrue(entity.meshRootGameObject != null); |
| 1 | 84 | | Assert.IsTrue(entity.meshRootGameObject.GetComponent<MeshRenderer>() != null); |
| 1 | 85 | | Assert.AreEqual(entity.meshRootGameObject.GetComponent<MeshRenderer>().sharedMaterial, mat.material); |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | [UnityTest] |
| | 89 | | public IEnumerator GetReplacedWhenAnotherMaterialIsAttached() |
| | 90 | | { |
| 1 | 91 | | yield return |
| | 92 | | TestHelpers.TestAttachedSharedComponentOfSameTypeIsReplaced<BasicMaterial.Model, BasicMaterial>(scene, |
| | 93 | | CLASS_ID.BASIC_MATERIAL); |
| 1 | 94 | | } |
| | 95 | |
|
| | 96 | | [UnityTest] |
| | 97 | | public IEnumerator BeDetachedCorrectly() |
| | 98 | | { |
| 1 | 99 | | string entityId = "1"; |
| 1 | 100 | | string materialID = "a-material"; |
| | 101 | |
|
| 1 | 102 | | TestHelpers.InstantiateEntityWithMaterial(scene, entityId, Vector3.zero, |
| | 103 | | new BasicMaterial.Model(), materialID); |
| | 104 | |
|
| 1 | 105 | | Assert.IsTrue(scene.entities[entityId].meshRootGameObject != null, |
| | 106 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 107 | |
|
| 1 | 108 | | var meshRenderer = scene.entities[entityId].meshRootGameObject.GetComponent<MeshRenderer>(); |
| 1 | 109 | | var materialComponent = scene.disposableComponents[materialID] as BasicMaterial; |
| | 110 | |
|
| 1 | 111 | | yield return materialComponent.routine; |
| | 112 | |
|
| | 113 | | // Check if material initialized correctly |
| | 114 | | { |
| 1 | 115 | | Assert.IsTrue(meshRenderer != null, "MeshRenderer must exist"); |
| | 116 | |
|
| 1 | 117 | | Assert.AreEqual(meshRenderer.sharedMaterial, materialComponent.material, "Assigned material"); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | // Remove material |
| 1 | 121 | | materialComponent.DetachFrom(scene.entities[entityId]); |
| | 122 | |
|
| | 123 | | // Check if material was removed correctly |
| 1 | 124 | | Assert.IsTrue(meshRenderer.sharedMaterial == null, |
| | 125 | | "Assigned material should be null as it has been removed"); |
| 1 | 126 | | } |
| | 127 | |
|
| | 128 | | [UnityTest] |
| | 129 | | public IEnumerator BeDetachedOnDispose() |
| | 130 | | { |
| 1 | 131 | | string firstEntityId = "1"; |
| 1 | 132 | | string secondEntityId = "2"; |
| 1 | 133 | | string materialID = "a-material"; |
| | 134 | |
|
| | 135 | | // Instantiate entity with material |
| 1 | 136 | | TestHelpers.InstantiateEntityWithMaterial(scene, firstEntityId, Vector3.zero, |
| | 137 | | new BasicMaterial.Model(), materialID); |
| | 138 | |
|
| 1 | 139 | | Assert.IsTrue(scene.entities[firstEntityId].meshRootGameObject != null, |
| | 140 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 141 | |
|
| | 142 | | // Create 2nd entity and attach same material to it |
| 1 | 143 | | TestHelpers.InstantiateEntityWithShape(scene, secondEntityId, CLASS_ID.BOX_SHAPE, Vector3.zero); |
| 1 | 144 | | scene.SharedComponentAttach( |
| | 145 | | secondEntityId, |
| | 146 | | materialID |
| | 147 | | ); |
| | 148 | |
|
| 1 | 149 | | Assert.IsTrue(scene.entities[secondEntityId].meshRootGameObject != null, |
| | 150 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 151 | |
|
| 1 | 152 | | var firstMeshRenderer = scene.entities[firstEntityId].meshRootGameObject.GetComponent<MeshRenderer>(); |
| 1 | 153 | | var secondMeshRenderer = scene.entities[secondEntityId].meshRootGameObject.GetComponent<MeshRenderer>(); |
| 1 | 154 | | var materialComponent = scene.disposableComponents[materialID] as DCL.Components.BasicMaterial; |
| | 155 | |
|
| 1 | 156 | | yield return materialComponent.routine; |
| | 157 | |
|
| | 158 | | // Check if material attached correctly |
| | 159 | | { |
| 1 | 160 | | Assert.IsTrue(firstMeshRenderer != null, "MeshRenderer must exist"); |
| 1 | 161 | | Assert.AreEqual(firstMeshRenderer.sharedMaterial, materialComponent.material, "Assigned material"); |
| | 162 | |
|
| 1 | 163 | | Assert.IsTrue(secondMeshRenderer != null, "MeshRenderer must exist"); |
| 1 | 164 | | Assert.AreEqual(secondMeshRenderer.sharedMaterial, materialComponent.material, "Assigned material"); |
| | 165 | | } |
| | 166 | |
|
| | 167 | | // Dispose material |
| 1 | 168 | | scene.SharedComponentDispose(materialID); |
| | 169 | |
|
| | 170 | | // Check if material detached correctly |
| 1 | 171 | | Assert.IsTrue(firstMeshRenderer.sharedMaterial == null, "MeshRenderer must exist"); |
| 1 | 172 | | Assert.IsTrue(secondMeshRenderer.sharedMaterial == null, "MeshRenderer must exist"); |
| 1 | 173 | | } |
| | 174 | |
|
| | 175 | | [UnityTest] |
| | 176 | | public IEnumerator EntityBasicMaterialUpdate() |
| | 177 | | { |
| 1 | 178 | | string entityId = "1"; |
| 1 | 179 | | string materialID = "a-material"; |
| | 180 | |
|
| 1 | 181 | | Assert.IsFalse(scene.disposableComponents.ContainsKey(materialID)); |
| | 182 | |
|
| | 183 | | // Instantiate entity with default material |
| 1 | 184 | | TestHelpers.InstantiateEntityWithMaterial(scene, entityId, new Vector3(8, 1, 8), |
| | 185 | | new BasicMaterial.Model(), materialID); |
| | 186 | |
|
| 1 | 187 | | var meshObject = scene.entities[entityId].meshRootGameObject; |
| 1 | 188 | | Assert.IsTrue(meshObject != null, |
| | 189 | | "Every entity with a shape should have the mandatory 'Mesh' object as a child"); |
| | 190 | |
|
| 1 | 191 | | var meshRenderer = meshObject.GetComponent<MeshRenderer>(); |
| 1 | 192 | | var materialComponent = scene.disposableComponents[materialID] as BasicMaterial; |
| | 193 | |
|
| 1 | 194 | | yield return materialComponent.routine; |
| | 195 | |
|
| | 196 | | // Check if material initialized correctly |
| | 197 | | { |
| 1 | 198 | | Assert.IsTrue(meshRenderer != null, "MeshRenderer must exist"); |
| | 199 | |
|
| 1 | 200 | | var assignedMaterial = meshRenderer.sharedMaterial; |
| 1 | 201 | | Assert.IsTrue(meshRenderer != null, "MeshRenderer.sharedMaterial must be the same as assignedMaterial"); |
| | 202 | |
|
| 1 | 203 | | Assert.AreEqual(assignedMaterial, materialComponent.material, "Assigned material"); |
| | 204 | | } |
| | 205 | |
|
| | 206 | | // Check default properties |
| | 207 | | { |
| 1 | 208 | | Assert.IsTrue(materialComponent.material.GetTexture("_BaseMap") == null); |
| 1 | 209 | | Assert.AreApproximatelyEqual(1.0f, materialComponent.material.GetFloat("_AlphaClip")); |
| | 210 | | } |
| | 211 | |
|
| 1 | 212 | | DCLTexture dclTexture = TestHelpers.CreateDCLTexture( |
| | 213 | | scene, |
| | 214 | | TestAssetsUtils.GetPath() + "/Images/atlas.png", |
| | 215 | | DCLTexture.BabylonWrapMode.MIRROR, |
| | 216 | | FilterMode.Bilinear); |
| | 217 | |
|
| | 218 | | // Update material |
| 1 | 219 | | scene.SharedComponentUpdate(materialID, JsonUtility.ToJson(new BasicMaterial.Model |
| | 220 | | { |
| | 221 | | texture = dclTexture.id, |
| | 222 | | alphaTest = 0.5f, |
| | 223 | | })); |
| | 224 | |
|
| 1 | 225 | | yield return materialComponent.routine; |
| | 226 | |
|
| | 227 | | // Check updated properties |
| | 228 | | { |
| 1 | 229 | | Texture mainTex = materialComponent.material.GetTexture("_BaseMap"); |
| 1 | 230 | | Assert.IsTrue(mainTex != null); |
| 1 | 231 | | Assert.AreApproximatelyEqual(0.5f, materialComponent.material.GetFloat("_Cutoff")); |
| 1 | 232 | | Assert.AreApproximatelyEqual(1.0f, materialComponent.material.GetFloat("_AlphaClip")); |
| 1 | 233 | | Assert.AreEqual(TextureWrapMode.Mirror, mainTex.wrapMode); |
| 1 | 234 | | Assert.AreEqual(FilterMode.Bilinear, mainTex.filterMode); |
| | 235 | | } |
| 1 | 236 | | } |
| | 237 | |
|
| | 238 | | [UnityTest] |
| | 239 | | public IEnumerator DefaultMissingValuesPropertyOnUpdate() |
| | 240 | | { |
| | 241 | | // 1. Create component with non-default configs |
| 1 | 242 | | BasicMaterial basicMaterialComponent = |
| | 243 | | TestHelpers.SharedComponentCreate<BasicMaterial, BasicMaterial.Model>(scene, CLASS_ID.BASIC_MATERIAL, |
| | 244 | | new BasicMaterial.Model |
| | 245 | | { |
| | 246 | | alphaTest = 1f |
| | 247 | | }); |
| | 248 | |
|
| 1 | 249 | | yield return basicMaterialComponent.routine; |
| | 250 | |
|
| | 251 | | // 2. Check configured values |
| 1 | 252 | | Assert.AreEqual(1f, basicMaterialComponent.GetModel().alphaTest); |
| | 253 | |
|
| | 254 | | // 3. Update component with missing values |
| | 255 | |
|
| 1 | 256 | | scene.SharedComponentUpdate(basicMaterialComponent.id, JsonUtility.ToJson(new BasicMaterial.Model { })); |
| | 257 | |
|
| 1 | 258 | | yield return basicMaterialComponent.routine; |
| | 259 | |
|
| | 260 | | // 4. Check defaulted values |
| 1 | 261 | | Assert.AreEqual(0.5f, basicMaterialComponent.GetModel().alphaTest); |
| 1 | 262 | | } |
| | 263 | |
|
| | 264 | | [UnityTest] |
| | 265 | | public IEnumerator ProcessCastShadowProperty_True() |
| | 266 | | { |
| 1 | 267 | | BasicMaterial basicMaterialComponent = TestHelpers.CreateEntityWithBasicMaterial(scene, new BasicMaterial.Model |
| | 268 | | { |
| | 269 | | alphaTest = 1f, |
| | 270 | | castShadows = true |
| | 271 | | }, out IDCLEntity entity); |
| 1 | 272 | | yield return basicMaterialComponent.routine; |
| | 273 | |
|
| 1 | 274 | | Assert.AreEqual(true, basicMaterialComponent.GetModel().castShadows); |
| 1 | 275 | | Assert.AreEqual(ShadowCastingMode.On, entity.meshRootGameObject.GetComponent<MeshRenderer>().shadowCastingMode); |
| 1 | 276 | | } |
| | 277 | |
|
| | 278 | | [UnityTest] |
| | 279 | | public IEnumerator ProcessCastShadowProperty_False() |
| | 280 | | { |
| 1 | 281 | | BasicMaterial basicMaterialComponent = TestHelpers.CreateEntityWithBasicMaterial(scene, new BasicMaterial.Model |
| | 282 | | { |
| | 283 | | alphaTest = 1f, |
| | 284 | | castShadows = false |
| | 285 | | }, out IDCLEntity entity); |
| 1 | 286 | | yield return basicMaterialComponent.routine; |
| | 287 | |
|
| 1 | 288 | | Assert.AreEqual(false, basicMaterialComponent.GetModel().castShadows); |
| 1 | 289 | | Assert.AreEqual(ShadowCastingMode.Off, entity.meshRootGameObject.GetComponent<MeshRenderer>().shadowCastingMode) |
| 1 | 290 | | } |
| | 291 | | } |