public IEnumerator CreateBrushMaterialWithPhysicMaterial_ChangePhysicMaterial_ManagerOnlyKnowsNewMaterial() { using (var newBrushMaterial = new ChiselBrushMaterial()) { var newPhysicsMaterial1 = new PhysicMaterial(); var newPhysicsMaterial2 = new PhysicMaterial(); newBrushMaterial.PhysicsMaterial = newPhysicsMaterial1; yield return(null); var foundPhysicsMaterial = ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial2.GetInstanceID(), false); Assert.AreNotEqual(newPhysicsMaterial1, newPhysicsMaterial2); Assert.AreEqual(newPhysicsMaterial1, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial1.GetInstanceID(), false)); Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial1.GetInstanceID())); LogAssert.Expect(LogType.Error, new Regex("Could not find")); Assert.IsNull(foundPhysicsMaterial); newBrushMaterial.PhysicsMaterial = newPhysicsMaterial2; yield return(null); ChiselBrushMaterialManager.Update(); LogAssert.Expect(LogType.Error, new Regex("Could not find")); Assert.IsNull(ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial1.GetInstanceID(), false)); Assert.AreEqual(newPhysicsMaterial2, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial2.GetInstanceID(), false)); Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial2.GetInstanceID())); UnityEngine.Object.DestroyImmediate(newPhysicsMaterial1); UnityEngine.Object.DestroyImmediate(newPhysicsMaterial2); } }
public IEnumerator RemoveAndAddPhysicsMaterialToAnotherBrushMaterial_MaterialStillRegistered() { using (ChiselBrushMaterial newBrushMaterial1 = new ChiselBrushMaterial(), newBrushMaterial2 = new ChiselBrushMaterial()) { var newPhysicsMaterial1 = new PhysicMaterial(); var newPhysicsMaterial2 = new PhysicMaterial(); newBrushMaterial1.PhysicsMaterial = newPhysicsMaterial1; newBrushMaterial2.PhysicsMaterial = newPhysicsMaterial2; yield return(null); ChiselBrushMaterialManager.Update(); Assert.AreEqual(newPhysicsMaterial1, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial1.GetInstanceID(), false)); Assert.AreEqual(newPhysicsMaterial2, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial2.GetInstanceID(), false)); Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial1.GetInstanceID())); Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial2.GetInstanceID())); newBrushMaterial1.PhysicsMaterial = newPhysicsMaterial2; newBrushMaterial2.PhysicsMaterial = newPhysicsMaterial1; yield return(null); ChiselBrushMaterialManager.Update(); Assert.AreEqual(newPhysicsMaterial1, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial1.GetInstanceID(), false)); Assert.AreEqual(newPhysicsMaterial2, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial2.GetInstanceID(), false)); Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial1.GetInstanceID())); Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial2.GetInstanceID())); UnityEngine.Object.DestroyImmediate(newPhysicsMaterial1); UnityEngine.Object.DestroyImmediate(newPhysicsMaterial2); } }
public IEnumerator CreateBrushMaterialWithPhysicMaterial_ManagerKnowsMaterial() { using (var newBrushMaterial = new ChiselBrushMaterial()) { var newPhysicsMaterial = new PhysicMaterial(); newBrushMaterial.PhysicsMaterial = newPhysicsMaterial; yield return(null); ChiselBrushMaterialManager.Update(); Assert.True(ChiselBrushMaterialManager.IsRegistered(newBrushMaterial)); Assert.AreEqual(newPhysicsMaterial, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial.GetInstanceID(), false)); Assert.AreEqual(1, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial.GetInstanceID())); UnityEngine.Object.DestroyImmediate(newPhysicsMaterial); } }
public IEnumerator CreateTwoBrushMaterialsWithSamePhysicsMaterial_RefCountIsTwo() { using (ChiselBrushMaterial newBrushMaterial1 = new ChiselBrushMaterial(), newBrushMaterial2 = new ChiselBrushMaterial()) { var newPhysicsMaterial = new PhysicMaterial(); newBrushMaterial1.PhysicsMaterial = newPhysicsMaterial; newBrushMaterial2.PhysicsMaterial = newPhysicsMaterial; yield return(null); ChiselBrushMaterialManager.Update(); Assert.AreEqual(newPhysicsMaterial, ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial.GetInstanceID(), false)); Assert.AreEqual(2, ChiselBrushMaterialManager.GetPhysicsMaterialRefCountByInstanceID(newPhysicsMaterial.GetInstanceID())); UnityEngine.Object.DestroyImmediate(newPhysicsMaterial); } }
public IEnumerator CreateBrushMaterialWithPhysicMaterial_DestroyBrushMaterial_ManagerDoesNotKnowMaterial() { using (var newBrushMaterial = new ChiselBrushMaterial()) { var newPhysicsMaterial = new PhysicMaterial(); newBrushMaterial.PhysicsMaterial = newPhysicsMaterial; newBrushMaterial.Dispose(); yield return(null); ChiselBrushMaterialManager.Update(); LogAssert.Expect(LogType.Error, new Regex("Could not find")); Assert.IsNull(ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newPhysicsMaterial.GetInstanceID(), false)); UnityEngine.Object.DestroyImmediate(newPhysicsMaterial); } }
public IEnumerator CreateBrushMaterialWithRenderMaterial_RetrievePhysicsMaterialWithRenderMaterialInstanceID_ReturnsNull() { using (var newBrushMaterial = new ChiselBrushMaterial()) { var newRenderMaterial = new Material(Shader.Find("Specular")); newBrushMaterial.RenderMaterial = newRenderMaterial; yield return(null); ChiselBrushMaterialManager.Update(); Assert.True(ChiselBrushMaterialManager.IsRegistered(newBrushMaterial)); LogAssert.Expect(LogType.Error, new Regex("Trying to use Material with")); Assert.IsNull(ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(newRenderMaterial.GetInstanceID(), false)); UnityEngine.Object.DestroyImmediate(newRenderMaterial); } }
public static ChiselColliderObjects Create(GameObject container, int surfaceParameter) { var physicsMaterial = ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(surfaceParameter); var sharedMesh = new Mesh { name = ChiselGeneratedObjects.kGeneratedMeshColliderName }; var meshCollider = container.AddComponent <MeshCollider>(); var colliderObjects = new ChiselColliderObjects { surfaceParameter = surfaceParameter, meshCollider = meshCollider, physicsMaterial = physicsMaterial, sharedMesh = sharedMesh }; colliderObjects.Initialize(); return(colliderObjects); }
internal void BuildComponents(ChiselModel model, ModelState modelState, ChiselGeneratedModelMesh generatedMesh) { Material renderMaterial = null; PhysicMaterial physicsMaterial = null; if (generatedMesh.meshDescription.surfaceParameter != 0) { var type = generatedMesh.meshDescription.meshQuery.LayerParameterIndex; var parameter = generatedMesh.meshDescription.surfaceParameter; switch (type) { case LayerParameterIndex.LayerParameter1: renderMaterial = ChiselBrushMaterialManager.GetRenderMaterialByInstanceID(parameter); break; case LayerParameterIndex.LayerParameter2: physicsMaterial = ChiselBrushMaterialManager.GetPhysicsMaterialByInstanceID(parameter); break; } } else { var type = generatedMesh.meshDescription.meshQuery.LayerParameterIndex; switch (type) { case LayerParameterIndex.LayerParameter1: renderMaterial = CSGMaterialManager.DefaultMaterial; break; case LayerParameterIndex.LayerParameter2: physicsMaterial = CSGMaterialManager.DefaultPhysicsMaterial; break; } } if (renderMaterial != null) { generatedMesh.renderComponents = null; List <ChiselRenderComponents> components; if (modelState.existingRenderComponents.TryGetValue(renderMaterial, out components)) { while (components.Count > 0) { var curComponents = components[0]; components.RemoveAt(0); if (components.Count == 0) { modelState.existingRenderComponents.Remove(renderMaterial); model.generatedComponents.Remove(curComponents.transform); } if (curComponents.meshRenderer && curComponents.meshFilter) { generatedMesh.renderComponents = curComponents; break; } } } var forceUpdate = UpdateOrCreateRenderComponents(model, modelState, generatedMesh.meshDescription, ref generatedMesh.renderComponents); if (generatedMesh.renderComponents.meshRenderer.sharedMaterial != renderMaterial) { generatedMesh.renderComponents.meshRenderer.sharedMaterial = renderMaterial; } if (generatedMesh.renderComponents.meshFilter.sharedMesh != generatedMesh.sharedMesh) { generatedMesh.renderComponents.meshFilter.sharedMesh = generatedMesh.sharedMesh; } if (generatedMesh.needsUpdate || forceUpdate) { #if UNITY_EDITOR if ((modelState.staticFlags & UnityEditor.StaticEditorFlags.LightmapStatic) == UnityEditor.StaticEditorFlags.LightmapStatic) { generatedMesh.renderComponents.meshRenderer.realtimeLightmapIndex = -1; generatedMesh.renderComponents.meshRenderer.lightmapIndex = -1; generatedMesh.renderComponents.uvLightmapUpdateTime = Time.realtimeSinceStartup; haveUVsToUpdate = true; } #endif } } else if (physicsMaterial != null) { generatedMesh.colliderComponents = null; List <ChiselColliderComponents> components; if (modelState.existingMeshColliders.TryGetValue(physicsMaterial, out components)) { while (components.Count > 0) { var curComponents = components[0]; components.RemoveAt(0); if (components.Count == 0) { modelState.existingMeshColliders.Remove(physicsMaterial); model.generatedComponents.Remove(curComponents.transform); } if (curComponents.meshCollider) { generatedMesh.colliderComponents = curComponents; break; } } } UpdateOrCreateColliderComponents(model, modelState, generatedMesh.meshDescription, ref generatedMesh.colliderComponents); if (generatedMesh.colliderComponents.meshCollider.sharedMesh != generatedMesh.sharedMesh) { generatedMesh.colliderComponents.meshCollider.sharedMesh = generatedMesh.sharedMesh; } if (generatedMesh.colliderComponents.meshCollider.sharedMaterial != physicsMaterial) { generatedMesh.colliderComponents.meshCollider.sharedMaterial = physicsMaterial; } } generatedMesh.needsUpdate = false; }