Esempio n. 1
0
        public bool ModifyItemModel(int itemInstanceId, int localModelId, bool updateSector, bool sync)
        {
            MyEnvironmentItemData data;

            if (!m_itemsData.TryGetValue(itemInstanceId, out data))
            {
                Debug.Assert(false, "Item instance not found.");
                return(false);
            }

            int modelId = MyEnvironmentItems.GetModelId(data.SubtypeId, localModelId);

            if (data.Enabled && data.ModelId != modelId)
            {
                Matrix matrix = data.Transform.TransformMatrix;

                var     sectorId  = MyEnvironmentSector.GetSectorId(matrix.Translation - CellsOffset, Definition.SectorSize);
                MyModel modelData = MyModels.GetModelOnlyData(MyModel.GetById(modelId));
                var     sector    = Sectors[sectorId];

                Matrix invOffset = Matrix.Invert(sector.SectorMatrix);
                matrix = matrix * invOffset;

                sector.DisableInstance(data.SectorInstanceId, data.ModelId);
                int newSectorInstanceId = sector.AddInstance(data.SubtypeId, modelId, itemInstanceId, ref matrix, modelData.BoundingBox, m_instanceFlags, m_definition.MaxViewDistance);

                data.ModelId                = modelId;
                data.SectorInstanceId       = newSectorInstanceId;
                m_itemsData[itemInstanceId] = data;

                if (updateSector)
                {
                    sector.UpdateRenderInstanceData();
                    sector.UpdateRenderEntitiesData(WorldMatrix);
                }

                if (ItemModified != null)
                {
                    ItemModified(this,
                                 new ItemInfo()
                    {
                        LocalId   = data.Id,
                        SubtypeId = data.SubtypeId,
                        Transform = data.Transform
                    });
                }

                if (sync)
                {
                    MySyncEnvironmentItems.SendModifyModelMessage(EntityId, itemInstanceId, localModelId);
                }
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Adds environment item to internal collections. Creates render and physics data.
        /// </summary>
        /// <returns>True if successfully added, otherwise false.</returns>
        private bool AddItem(
            MyEnvironmentItemDefinition itemDefinition,
            ref MatrixD worldMatrix,
            ref BoundingBoxD aabbWorld,
            HkStaticCompoundShape sectorRootShape,
            Dictionary <MyStringHash, HkShape> subtypeIdToShape,
            int localModelId = MAIN_MODEL_LOCAL_ID)
        {
            if (!MyFakes.ENABLE_ENVIRONMENT_ITEMS)
            {
                return(true);
            }

            Debug.Assert(m_definition.ContainsItemDefinition(itemDefinition),
                         String.Format("Environment item with definition '{0}' not found in class '{1}'", itemDefinition.Id, m_definition.Id));
            if (!m_definition.ContainsItemDefinition(itemDefinition))
            {
                return(false);
            }

            if (itemDefinition.Model == null)
            {
                return(false);
            }

            //MyDefinitionId defId = new MyDefinitionId(envItemObjectBuilderType, subtypeId.ToString());
            int    modelId   = MyEnvironmentItems.GetModelId(itemDefinition.Id.SubtypeId, localModelId);
            string modelName = MyModel.GetById(modelId);

            MyModel model = MyModels.GetModelOnlyData(modelName);

            if (model == null)
            {
                //Debug.Fail(String.Format("Environment item model of '{0}' not found, skipping the item...", itemDefinition.Id));
                return(false);
            }

            CheckModelConsistency(itemDefinition);

            int localId = worldMatrix.Translation.GetHashCode();

            MyEnvironmentItemData data = new MyEnvironmentItemData()
            {
                Id               = localId,
                SubtypeId        = itemDefinition.Id.SubtypeId,
                Transform        = new MyTransformD(ref worldMatrix),
                Enabled          = true,
                SectorInstanceId = -1,
                ModelId          = modelId,
            };

            //Preload split planes
            //VRageRender.MyRenderProxy.PreloadMaterials(model.AssetName);

            aabbWorld.Include(model.BoundingBox.Transform(worldMatrix));

            MatrixD transform = data.Transform.TransformMatrix;

            Vector3I            sectorId = MyEnvironmentSector.GetSectorId(transform.Translation - CellsOffset, m_definition.SectorSize);
            MyEnvironmentSector sector;

            if (!m_sectors.TryGetValue(sectorId, out sector))
            {
                sector = new MyEnvironmentSector(sectorId, sectorId * m_definition.SectorSize + CellsOffset);
                m_sectors.Add(sectorId, sector);
            }

            // Adds instance of the given model. Local matrix specified might be changed internally in renderer.

            MatrixD sectorOffset = MatrixD.CreateTranslation(-sectorId * m_definition.SectorSize - CellsOffset);
            Matrix  transformL   = (Matrix)(transform * sectorOffset);

            data.SectorInstanceId = sector.AddInstance(itemDefinition.Id.SubtypeId, data.ModelId, localId, ref transformL, model.BoundingBox, m_instanceFlags, m_definition.MaxViewDistance);

            int physicsShapeInstanceId;

            if (AddPhysicsShape(data.SubtypeId, model, ref transform, sectorRootShape, subtypeIdToShape, out physicsShapeInstanceId))
            {
                // Map to data index - note that itemData is added after this to its list!
                m_physicsShapeInstanceIdToLocalId[physicsShapeInstanceId] = localId;
                m_localIdToPhysicsShapeInstanceId[localId] = physicsShapeInstanceId;
            }

            data.Transform = new MyTransformD(transform);

            if (m_itemsData.ContainsKey(localId))
            {
                //Debug.Fail("More items on same place! " + transform.Translation.ToString());
            }
            else
            {
                m_itemsData.Add(localId, data);
            }

            if (ItemAdded != null)
            {
                ItemAdded(this,
                          new ItemInfo()
                {
                    LocalId   = localId,
                    SubtypeId = data.SubtypeId,
                    Transform = data.Transform,
                });
            }

            return(true);
        }