internal static void UpdateModelMeshDescriptions(ChiselModel model)
        {
            if (!ChiselModelGeneratedObjects.IsValid(model.generated))
            {
                if (model.generated != null)
                {
                    model.generated.Destroy();
                }
                model.generated = ChiselModelGeneratedObjects.Create(model);
            }

            var tree = model.Node;

            if (!tree.Valid)
            {
                return;
            }

            var meshTypes        = ChiselMeshQueryManager.GetMeshQuery(model);
            var meshDescriptions = tree.GetMeshDescriptions(meshTypes, model.VertexChannelMask);

            // Check if the tree creates *any* meshes
            if (meshDescriptions == null || meshDescriptions.Length == 0)
            {
                //componentGenerator.RemoveAllGeneratedComponents(model);
                PostUpdateModel?.Invoke(model);
                return;
            }

            // Sort all meshDescriptions so that meshes that can be merged are next to each other
            Array.Sort(meshDescriptions, kMeshDescriptionSorterDelegate);

            model.generated.Update(model, meshDescriptions);
        }
        public static bool IsValid(ChiselModelGeneratedObjects satelliteObjects)
        {
            if (satelliteObjects == null)
            {
                return(false);
            }

            if (!satelliteObjects.generatedDataContainer ||
                !satelliteObjects.colliderContainer ||
                satelliteObjects.colliders == null ||   // must be an array, even if 0 length
                satelliteObjects.renderables == null ||
                satelliteObjects.renderables.Length != 8 ||
                satelliteObjects.meshRenderers == null ||
                satelliteObjects.meshRenderers.Length != 5)
            {
                return(false);
            }

            // These queries are valid, and should never be null
            if (satelliteObjects.renderables[1] == null ||
                satelliteObjects.renderables[2] == null ||
                satelliteObjects.renderables[3] == null ||
                satelliteObjects.renderables[5] == null ||
                satelliteObjects.renderables[7] == null)
            {
                return(false);
            }


            // These queries are invalid, and should always be null
            satelliteObjects.renderables[0] = null;
            satelliteObjects.renderables[4] = null;
            satelliteObjects.renderables[6] = null;


            for (int i = 0; i < satelliteObjects.renderables.Length; i++)
            {
                if (satelliteObjects.renderables[i] == null)
                {
                    continue;
                }
                if (!ChiselRenderObjects.IsValid(satelliteObjects.renderables[i]))
                {
                    return(false);
                }
            }

            for (int i = 0; i < satelliteObjects.colliders.Length; i++)
            {
                if (!ChiselColliderObjects.IsValid(satelliteObjects.colliders[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #3
0
        public override void OnInitialize()
        {
            if (generated != null &&
                !generated.generatedDataContainer)
            {
                generated.Destroy();
            }

            if (generated == null)
            {
                generated = ChiselModelGeneratedObjects.Create(this);
            }

            if (colliderSettings == null)
            {
                colliderSettings = new ChiselGeneratedColliderSettings();
                colliderSettings.Reset();
            }

            if (renderSettings == null)
            {
                renderSettings = new ChiselGeneratedRenderSettings();
                renderSettings.Reset();
            }

#if UNITY_EDITOR
            if (uvGenerationSettings == null)
            {
                uvGenerationSettings = new SerializableUnwrapParam();
                UnityEditor.UnwrapParam defaults;
                UnityEditor.UnwrapParam.SetDefaults(out defaults);
                uvGenerationSettings.angleError       = defaults.angleError;
                uvGenerationSettings.areaError        = defaults.areaError;
                uvGenerationSettings.hardAngle        = defaults.hardAngle;
                uvGenerationSettings.packMarginPixels = defaults.packMargin * 256;
            }
#else
            if (generated != null && generated.meshRenderers != null)
            {
                foreach (var renderable in generated.renderables)
                {
                    renderable.meshRenderer.forceRenderingOff = true;
                    renderable.meshRenderer.enabled           = renderable.sharedMesh.vertexCount == 0;
                }
            }
#endif

            initialized = true;
        }
        public void Rebuild(ChiselModel model)
        {
            if (!model.IsInitialized)
            {
                model.OnInitialize();
            }

            if (!ChiselModelGeneratedObjects.IsValid(model.generated))
            {
                if (model.generated != null)
                {
                    model.generated.Destroy();
                }
                model.generated = ChiselModelGeneratedObjects.Create(model);
            }

            UpdateModelFlags(model);
        }
        public static ChiselModelGeneratedObjects Create(ChiselModel model)
        {
            // Make sure there's not a dangling container out there from a previous version
            var existingContainer = model.FindChildByName(kGeneratedContainerName);

            ChiselObjectUtility.SafeDestroy(existingContainer, ignoreHierarchyEvents: true);

            var modelState         = GameObjectState.Create(model);
            var parent             = model.transform;
            var container          = ChiselObjectUtility.CreateGameObject(kGeneratedContainerName, parent, modelState);
            var containerTransform = container.transform;
            var colliderContainer  = ChiselObjectUtility.CreateGameObject(kGeneratedMeshColliderName, containerTransform, modelState);

            Debug.Assert((int)LayerUsageFlags.Renderable == 1);
            Debug.Assert((int)LayerUsageFlags.CastShadows == 2);
            Debug.Assert((int)LayerUsageFlags.ReceiveShadows == 4);
            Debug.Assert((int)LayerUsageFlags.RenderReceiveCastShadows == (1 | 2 | 4));

            var renderables = new ChiselRenderObjects[]
            {
                null,//ChiselRenderObjects Create(kGeneratedMeshRendererNames[0], containerTransform, modelState, LayerUsageFlags.None),
                ChiselRenderObjects.Create(kGeneratedMeshRendererNames[1], containerTransform, modelState, LayerUsageFlags.Renderable),
                ChiselRenderObjects.Create(kGeneratedMeshRendererNames[2], containerTransform, modelState, LayerUsageFlags.CastShadows),
                ChiselRenderObjects.Create(kGeneratedMeshRendererNames[3], containerTransform, modelState, LayerUsageFlags.Renderable | LayerUsageFlags.CastShadows),
                null, //ChiselRenderObjects.Create(kGeneratedMeshRendererNames[4], containerTransform, modelState,                                                     LayerUsageFlags.ReceiveShadows),
                ChiselRenderObjects.Create(kGeneratedMeshRendererNames[5], containerTransform, modelState, LayerUsageFlags.Renderable | LayerUsageFlags.ReceiveShadows),
                null, //ChiselRenderObjects.Create(kGeneratedMeshRendererNames[6], containerTransform, modelState,                       LayerUsageFlags.CastShadows | LayerUsageFlags.ReceiveShadows),
                ChiselRenderObjects.Create(kGeneratedMeshRendererNames[7], containerTransform, modelState, LayerUsageFlags.Renderable | LayerUsageFlags.CastShadows | LayerUsageFlags.ReceiveShadows),
            };

            // These queries are invalid, and should always be null
            Debug.Assert(renderables[0] == null);
            Debug.Assert(renderables[4] == null);
            Debug.Assert(renderables[6] == null);

            // These queries are valid, and should never be null
            Debug.Assert(renderables[1] != null);
            Debug.Assert(renderables[2] != null);
            Debug.Assert(renderables[3] != null);
            Debug.Assert(renderables[5] != null);
            Debug.Assert(renderables[7] != null);


            var meshRenderers = new MeshRenderer[]
            {
                renderables[1].meshRenderer,
                renderables[2].meshRenderer,
                renderables[3].meshRenderer,
                renderables[5].meshRenderer,
                renderables[7].meshRenderer
            };

            var result = new ChiselModelGeneratedObjects
            {
                generatedDataContainer = container,
                colliderContainer      = colliderContainer,
                colliders     = new ChiselColliderObjects[0],
                renderables   = renderables,
                meshRenderers = meshRenderers
            };

            Debug.Assert(IsValid(result));

            return(result);
        }