Exemplo n.º 1
0
        internal static void UnloadData()
        {
            MyActorFactory.RemoveAll();
            // many-to-one relation, can live withput owners, deallocated separately
            // MyComponentFactory<MyInstancingComponent>.RemoveAll();

            //MyVoxelMesh.RemoveAll();
            //MyDynamicMesh.RemoveAll();


            MyRender11.Log.WriteLine("Unloading session data");

            MyScene.RenderablesDBVH.Clear();
            MyScene.GroupsDBVH.Clear();
            MyClipmapFactory.RemoveAll();

            MyInstancing.OnSessionEnd();
            MyMeshes.OnSessionEnd();
            MyLights.OnSessionEnd();

            MyMaterials1.OnSessionEnd();
            MyVoxelMaterials1.OnSessionEnd();
            MyMeshMaterials1.OnSessionEnd();

            MyTextures.OnSessionEnd();
            MyBigMeshTable.Table.OnSessionEnd();

            //MyAssetsLoader.ClearMeshes();
        }
Exemplo n.º 2
0
 internal static MyActor GetInstanceActor(MyRenderableComponent original)
 {
     if (m_instanceActor == null)
     {
         m_instanceActor = MyActorFactory.Create();
         m_instanceActor.AddComponent <MyInstanceLodComponent>(MyComponentFactory <MyInstanceLodComponent> .Create());
     }
     return(m_instanceActor);
 }
Exemplo n.º 3
0
 internal void Unload()
 {
     if (m_actor != null && !m_actor.IsDestroyed)
     {
         MyActorFactory.Destroy(m_actor);
     }
     m_actor = null;
     MyMeshes.RemoveVoxelCell(m_mesh);
 }
Exemplo n.º 4
0
 private static void DisposeInstanceActor()
 {
     if (m_instanceActor != null)
     {
         if (!m_instanceActor.IsDestroyed)
         {
             MyActorFactory.Destroy(m_instanceActor);
         }
         m_instanceActor = null;
     }
 }
Exemplo n.º 5
0
        internal MyClipmapCellProxy(MyCellCoord cellCoord, ref VRageMath.MatrixD worldMatrix, RenderFlags additionalFlags = 0)
        {
            m_worldMatrix = worldMatrix;

            m_actor = MyActorFactory.CreateSceneObject();
            m_actor.SetMatrix(ref worldMatrix);
            m_actor.AddComponent(MyComponentFactory <MyFoliageComponent> .Create());

            m_lod = cellCoord.Lod;

            Mesh = MyMeshes.CreateVoxelCell(cellCoord.CoordInLod, cellCoord.Lod);
            m_actor.GetRenderable().SetModel(Mesh);
            m_actor.GetRenderable().m_additionalFlags = MyProxiesFactory.GetRenderableProxyFlags(additionalFlags);
        }
Exemplo n.º 6
0
        internal MyClipmapCellProxy(MyCellCoord cellCoord, ref VRageMath.MatrixD worldMatrix)
        {
            m_worldMatrix = worldMatrix;

            m_actor = MyActorFactory.CreateSceneObject();
            //m_mesh = new MyVoxelMesh(cellCoord.CoordInLod, cellCoord.Lod, "");
            //m_actor.GetRenderable().SetModel(m_mesh);
            m_actor.SetMatrix(ref worldMatrix);
            m_actor.AddComponent(MyComponentFactory <MyFoliageComponent> .Create());

            m_lod = cellCoord.Lod;

            Mesh = MyMeshes.CreateVoxelCell(cellCoord.CoordInLod, cellCoord.Lod);
            m_actor.GetRenderable().SetModel(Mesh);

            m_discardingOn = false;
        }
Exemplo n.º 7
0
        internal static void UnloadData()
        {
            MyManagers.OnUnloadData();

            MyActorFactory.RemoveAll();
            // many-to-one relation, can live withput owners, deallocated separately
            // MyComponentFactory<MyInstancingComponent>.RemoveAll();

            //MyVoxelMesh.RemoveAll();
            //MyDynamicMesh.RemoveAll();


            MyRender11.Log.WriteLine("Unloading session data");

            // Remove leftover persistent debug draw messages
            m_debugDrawMessages.Clear();

            MyScene.DynamicRenderablesDBVH.Clear();
            if (MyScene.SeparateGeometry)
            {
                MyScene.StaticRenderablesDBVH.Clear();
            }
            MyScene.GroupsDBVH.Clear();
            MyScene.FoliageDBVH.Clear();
            MyClipmapFactory.RemoveAll();
            MyClipmap.UnloadCache();

            MyInstancing.OnSessionEnd();
            MyFoliageComponents.OnSessionEnd();
            MyMeshes.OnSessionEnd();
            MyLights.OnSessionEnd();

            MyMaterials1.OnSessionEnd();
            MyVoxelMaterials1.OnSessionEnd();
            MyMeshMaterials1.OnSessionEnd();
            MyScreenDecals.OnSessionEnd();

            MyBigMeshTable.Table.OnSessionEnd();

            MyPrimitivesRenderer.Unload();

            MyTransparentRendering.OnSessionEnd();

            //MyAssetsLoader.ClearMeshes();
        }
Exemplo n.º 8
0
        internal MyClipmapCellProxy(MyCellCoord cellCoord, ref MatrixD worldMatrix, Vector3D massiveCenter, float massiveRadius, RenderFlags additionalFlags = 0)
        {
            m_worldMatrix = worldMatrix;

            m_actor = MyActorFactory.CreateVoxelCell();
            m_actor.SetMatrix(ref worldMatrix);

            m_lod = cellCoord.Lod;

            MyVoxelRenderableComponent renderableComponent = m_actor.GetRenderable() as MyVoxelRenderableComponent;

            m_mesh = MyMeshes.CreateVoxelCell(cellCoord.CoordInLod, cellCoord.Lod);
            renderableComponent.SetVoxelLod(m_lod, ScaleGroup);
            renderableComponent.SetModel(m_mesh);
            renderableComponent.m_massiveCenter   = massiveCenter;
            renderableComponent.m_massiveRadius   = massiveRadius;
            renderableComponent.m_additionalFlags = MyProxiesFactory.GetRenderableProxyFlags(additionalFlags);
        }
Exemplo n.º 9
0
        internal static void DrawHierarchyDebug()
        {
            var worldToClip   = MyEnvironment.ViewProjection;
            var displayString = new StringBuilder();

            var batch = MyLinesRenderer.CreateBatch();

            if (MyRender11.Settings.DisplayIDs)
            {
                foreach (var actor in MyActorFactory.GetAll())
                {
                    var h = actor.GetGroupLeaf();
                    var r = actor.GetRenderable();

                    Vector3 position;
                    uint    ID;

                    if (r != null)
                    {
                        position = r.Owner.WorldMatrix.Translation;
                        ID       = r.Owner.ID;
                    }
                    else if (h != null)
                    {
                        position = h.Owner.WorldMatrix.Translation;
                        ID       = h.Owner.ID;
                    }
                    else
                    {
                        continue;
                    }

                    var clipPosition = Vector3.Transform(position, ref worldToClip);
                    clipPosition.X = clipPosition.X * 0.5f + 0.5f;
                    clipPosition.Y = clipPosition.Y * -0.5f + 0.5f;

                    if (clipPosition.Z > 0 && clipPosition.Z < 1)
                    {
                        displayString.AppendFormat("{0}", ID);
                        MySpritesRenderer.DrawText(new Vector2(clipPosition.X, clipPosition.Y) * MyRender11.ViewportResolution,
                                                   displayString, Color.DarkCyan, 0.5f);
                    }

                    displayString.Clear();
                }
            }

            if (MyRender11.Settings.DisplayAabbs)
            {
                foreach (var actor in MyActorFactory.GetAll())
                {
                    var h = actor.GetGroupRoot();
                    var r = actor.GetRenderable();

                    if (h != null)
                    {
                        var bb = BoundingBoxD.CreateInvalid();

                        foreach (var child in h.m_children)
                        {
                            if (child.IsVisible)
                            {
                                bb.Include(child.Aabb);
                            }
                        }

                        batch.AddBoundingBox((BoundingBox)bb, Color.Red);
                        MyPrimitivesRenderer.Draw6FacedConvexZ(bb.GetCorners().Select(x => (Vector3)x).ToArray(), Color.Red, 0.1f);
                    }
                    else if (r != null && actor.GetGroupLeaf() == null)
                    {
                        batch.AddBoundingBox((BoundingBox)r.Owner.Aabb, Color.Green);
                    }
                }
            }

            batch.Commit();
        }
        private static void ProcessMessageInternal(IMyRenderMessage message)
        {
            switch (message.MessageType)
            {
            case MyRenderMessageEnum.SetCameraViewMatrix:
            {
                var rMessage = (MyRenderMessageSetCameraViewMatrix)message;

                SetupCameraMatrices(rMessage);

                break;
            }

            case MyRenderMessageEnum.DrawScene:
            {
                var rMessage = (IMyRenderMessage)message;

                m_drawQueue.Enqueue(rMessage);

                m_messageTracker.Clear();

                break;
            }

            case MyRenderMessageEnum.RebuildCullingStructure:
            {
                break;
            }

                #region Profiler

            case MyRenderMessageEnum.RenderProfiler:
            {
                var profMessage = (MyRenderMessageRenderProfiler)message;

                MyRenderProfiler.HandleInput(profMessage.Command, profMessage.Index);

                break;
            }

                #endregion

                #region Characters

            case MyRenderMessageEnum.CreateRenderCharacter:
            {
                var rMessage = (MyRenderMessageCreateRenderCharacter)message;

                var    actor        = MyActorFactory.CreateCharacter();
                Matrix worldMatrixF = rMessage.WorldMatrix;
                //actor.GetRenderable().SetModel(MyAssetsLoader.GetModel(rMessage.Model));
                actor.GetRenderable().SetModel(MyMeshes.GetMeshId(X.TEXT(rMessage.Model)));
                actor.SetMatrix(ref worldMatrixF);

                if (rMessage.ColorMaskHSV.HasValue)
                {
                    var color = ColorFromMask(rMessage.ColorMaskHSV.Value);
                    actor.GetRenderable().SetKeyColor(new Vector4(color, 1));
                }

                actor.SetID(rMessage.ID);

                //var entity = MyComponents.CreateEntity(rMessage.ID);
                //MyComponents.CreateRenderable(
                //    entity,
                //    MyMeshes.GetMeshId(X.TEXT(rMessage.Model)),
                //    rMessage.ColorMaskHSV.HasValue ? rMessage.ColorMaskHSV.Value : Vector3.One);
                //MyComponents.SetMatrix(entity, ref rMessage.WorldMatrix);

                break;
            }

            case MyRenderMessageEnum.SetCharacterSkeleton:
            {
                var rMessage = (MyRenderMessageSetCharacterSkeleton)message;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.CharacterID);

                if (actor != null)
                {
                    actor.GetSkinning().SetSkeleton(rMessage.SkeletonBones, rMessage.SkeletonIndices);
                }

                //var entity = MyComponents.GetEntity(rMessage.CharacterID);
                //MyComponents.SetSkeleton(entity, rMessage.SkeletonBones, rMessage.SkeletonIndices);

                break;
            };

            case MyRenderMessageEnum.SetCharacterTransforms:
            {
                var rMessage = (MyRenderMessageSetCharacterTransforms)message;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.CharacterID);

                if (actor != null)
                {
                    actor.GetSkinning().SetAnimationBones(rMessage.RelativeBoneTransforms);
                }
                //var entity = MyComponents.GetEntity(rMessage.CharacterID);
                //MyComponents.SetAnimation(entity, rMessage.RelativeBoneTransforms);

                break;
            }

            case MyRenderMessageEnum.UpdateRenderEntity:
            {
                var rMessage = (MyRenderMessageUpdateRenderEntity)message;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.ID);

                if (actor != null && actor.GetRenderable() != null)
                {
                    if (rMessage.ColorMaskHSV.HasValue)
                    {
                        actor.GetRenderable().SetKeyColor(new Vector4(ColorFromMask(rMessage.ColorMaskHSV.Value), 1));
                    }
                    actor.GetRenderable().SetDithering(rMessage.Dithering);

                    if (rMessage.Dithering < 0)
                    {
                    }
                }

                break;
            }

            case MyRenderMessageEnum.ChangeModelMaterial:
            {
                var rMessage = (MyRenderMessageChangeModelMaterial)message;



                //var matId = MyMeshMaterialId.NULL;
                //if (rMessage.Material.ToLower().Contains("debug"))
                //{
                //    matId = MyMeshMaterials1.DebugMaterialId;
                //}
                //else
                //{
                //    matId = MyMeshMaterials1.GetMaterialId(rMessage.Material);
                //}

                //MyAssetsLoader.GetModel(rMessage.Model).SetMaterial_SLOW(MyMeshMaterials1.GetProxyId(matId));

                break;
            }

                #endregion

                #region Render objects

            case MyRenderMessageEnum.CreateRenderEntity:
            {
                var rMessage = (MyRenderMessageCreateRenderEntity)message;

                Matrix m = (Matrix)rMessage.WorldMatrix;

                var actor = MyActorFactory.CreateSceneObject();
                if (rMessage.Model != null)
                {
                    var model = MyAssetsLoader.ModelRemap.Get(rMessage.Model, rMessage.Model);

                    actor.GetRenderable().SetModel(MyMeshes.GetMeshId(X.TEXT(model)));
                    //if (MyDestructionMesh.ModelsDictionary.ContainsKey(model))
                    //{
                    //    //actor.GetRenderable().SetModel(MyDestructionMesh.ModelsDictionary.Get(model));
                    //    actor.GetRenderable().SetModel(MyMeshes.GetMeshId(X.TEXT(model)));
                    //}
                    //else
                    //{
                    //    //actor.GetRenderable().SetModel(MyAssetsLoader.GetModel(model));
                    //    actor.GetRenderable().SetModel(MyMeshes.GetMeshId(X.TEXT(model)));
                    //}
                }

                actor.SetID(rMessage.ID);
                actor.SetMatrix(ref m);

                break;
            }

            case MyRenderMessageEnum.CreateRenderVoxelDebris:
            {
                var rMessage = (MyRenderMessageCreateRenderVoxelDebris)message;

                Matrix m = (Matrix)rMessage.WorldMatrix;

                var actor = MyActorFactory.CreateSceneObject();
                if (rMessage.Model != null)
                {
                    actor.GetRenderable().SetModel(MyMeshes.GetMeshId(X.TEXT(rMessage.Model)));
                }

                actor.SetID(rMessage.ID);
                actor.SetMatrix(ref m);

                MyRenderableComponent.DebrisEntityVoxelMaterial[rMessage.ID] = rMessage.VoxelMaterialIndex;

                break;
            }

            case MyRenderMessageEnum.UpdateRenderObject:
            {
                var rMessage = (MyRenderMessageUpdateRenderObject)message;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.ID);

                if (actor != null)
                {
                    Matrix m = (Matrix)rMessage.WorldMatrix;
                    actor.SetMatrix(ref m);
                    if (rMessage.AABB.HasValue)
                    {
                        actor.SetAabb((BoundingBox)rMessage.AABB.Value);
                    }
                }

                var entity = MyComponents.GetEntity(rMessage.ID);
                if (entity != EntityId.NULL)
                {
                    MyComponents.SetMatrix(entity, ref rMessage.WorldMatrix);
                    if (rMessage.AABB.HasValue)
                    {
                        var aabb = rMessage.AABB.Value;
                        MyComponents.SetAabb(entity, ref aabb);
                    }
                }

                break;
            }

            case MyRenderMessageEnum.RemoveRenderObject:
            {
                var rMessage = (MyRenderMessageRemoveRenderObject)message;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.ID);

                if (actor != null)
                {
                    if (actor.GetRenderable() != null && actor.GetRenderable().GetModel().Info.Dynamic)
                    {
                        MyMeshes.RemoveMesh(actor.GetRenderable().GetModel());
                    }

                    actor.Destruct();
                }

                var instancing = MyInstancing.Get(rMessage.ID);
                if (instancing != InstancingId.NULL)
                {
                    MyInstancing.Remove(rMessage.ID, instancing);
                }

                var light = MyLights.Get(rMessage.ID);
                if (light != LightId.NULL)
                {
                    MyLights.Remove(rMessage.ID, light);
                }

                var clipmap = MyClipmapFactory.ClipmapByID.Get(rMessage.ID);
                if (clipmap != null)
                {
                    clipmap.RemoveFromUpdate();
                }

                break;
            }

            case MyRenderMessageEnum.UpdateRenderObjectVisibility:
            {
                var rMessage = (MyRenderMessageUpdateRenderObjectVisibility)message;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.ID);

                if (actor != null)
                {
                    actor.SetVisibility(rMessage.Visible);
                }

                break;
            }


            case MyRenderMessageEnum.CreateRenderInstanceBuffer:
            {
                var rMessage = (MyRenderMessageCreateRenderInstanceBuffer)message;

                //var instancing = MyComponentFactory<MyInstancingComponent>.Create();
                //instancing.SetID(rMessage.ID);
                //instancing.Init(rMessage.Type);
                //instancing.SetDebugName(rMessage.DebugName);

                MyInstancing.Create(rMessage.ID, rMessage.Type, rMessage.DebugName);

                break;
            }

            case MyRenderMessageEnum.UpdateRenderInstanceBuffer:
            {
                var rMessage = (MyRenderMessageUpdateRenderInstanceBuffer)message;

                //var instancing = MyIDTracker<MyInstancingComponent>.FindByID(rMessage.ID);
                //if(instancing != null)
                //{
                //    instancing.UpdateGeneric(rMessage.InstanceData, rMessage.Capacity);
                //}

                MyInstancing.UpdateGeneric(MyInstancing.Get(rMessage.ID), rMessage.InstanceData, rMessage.Capacity);

                rMessage.InstanceData.Clear();

                break;
            }

            case MyRenderMessageEnum.UpdateRenderCubeInstanceBuffer:
            {
                var rMessage = (MyRenderMessageUpdateRenderCubeInstanceBuffer)message;

                //var instancing = MyIDTracker<MyInstancingComponent>.FindByID(rMessage.ID);
                //if (instancing != null)
                //{
                //    instancing.UpdateCube(rMessage.InstanceData, rMessage.Capacity);
                //}
                MyInstancing.UpdateCube(MyInstancing.Get(rMessage.ID), rMessage.InstanceData, rMessage.Capacity);

                rMessage.InstanceData.Clear();

                break;
            }

            case MyRenderMessageEnum.SetInstanceBuffer:
            {
                var rMessage = (MyRenderMessageSetInstanceBuffer)message;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.ID);

                //var instancing = MyIDTracker<MyInstancingComponent>.FindByID(rMessage.InstanceBufferId);

                if (actor != null)
                {
                    //if (actor.GetComponent(MyActorComponentEnum.Instancing) != instancing)
                    //{
                    //    actor.AddComponent(instancing);
                    //}
                    //actor.SetLocalAabb(rMessage.LocalAabb);
                    //actor.GetRenderable().SetInstancingCounters(rMessage.InstanceCount, rMessage.InstanceStart);

                    actor.GetRenderable().SetInstancing(MyInstancing.Get(rMessage.InstanceBufferId));
                    actor.SetLocalAabb(rMessage.LocalAabb);
                    actor.GetRenderable().SetInstancingCounters(rMessage.InstanceCount, rMessage.InstanceStart);
                }

                break;
            }

            case MyRenderMessageEnum.CreateManualCullObject:
            {
                var rMessage = (MyRenderMessageCreateManualCullObject)message;

                var actor = MyActorFactory.CreateGroup();
                actor.SetID(rMessage.ID);
                Matrix m = (Matrix)rMessage.WorldMatrix;
                actor.SetMatrix(ref m);

                break;
            }

            case MyRenderMessageEnum.SetParentCullObject:
            {
                var rMessage = (MyRenderMessageSetParentCullObject)message;

                var child = MyIDTracker <MyActor> .FindByID(rMessage.ID);

                var parent = MyIDTracker <MyActor> .FindByID(rMessage.CullObjectID);

                if (child != null && parent != null && parent.GetGroupRoot() != null && child.GetGroupLeaf() == null)
                {
                    child.SetRelativeTransform(rMessage.ChildToParent);
                    parent.GetGroupRoot().Add(child);
                }

                break;
            }

            case MyRenderMessageEnum.CreateLineBasedObject:
            {
                var rMessage = (MyRenderMessageCreateLineBasedObject)message;

                var actor = MyActorFactory.CreateSceneObject();
                //actor.GetRenderable().SetModel(new MyDynamicMesh());

                actor.SetID(rMessage.ID);
                actor.SetMatrix(ref Matrix.Identity);

                MyMeshMaterials1.GetMaterialId("__ROPE_MATERIAL", null, "Textures/rope_cm.dds", "Textures/rope_ng.dds", "Textures/rope_add.dds", MyMesh.DEFAULT_MESH_TECHNIQUE);
                actor.GetRenderable().SetModel(MyMeshes.CreateRuntimeMesh(X.TEXT("LINE" + rMessage.ID), 1, true));

                break;
            }

            case MyRenderMessageEnum.UpdateLineBasedObject:
            {
                var rMessage = (MyRenderMessageUpdateLineBasedObject)message;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.ID);

                if (actor != null)
                {
                    //var mesh = actor.GetRenderable().GetMesh() as MyDynamicMesh;

                    MyVertexFormatPositionH4 []            stream0;
                    MyVertexFormatTexcoordNormalTangent [] stream1;

                    MyLineHelpers.GenerateVertexData(ref rMessage.WorldPointA, ref rMessage.WorldPointB,
                                                     out stream0, out stream1);

                    var indices  = MyLineHelpers.GenerateIndices(stream0.Length);
                    var sections = new MySectionInfo[]
                    {
                        new MySectionInfo {
                            TriCount = indices.Length / 3, IndexStart = 0, MaterialName = "__ROPE_MATERIAL"
                        }
                    };

                    MyMeshes.UpdateRuntimeMesh(MyMeshes.GetMeshId(X.TEXT("LINE" + rMessage.ID)),
                                               indices,
                                               stream0,
                                               stream1,
                                               sections,
                                               (BoundingBox)MyLineHelpers.GetBoundingBox(ref rMessage.WorldPointA, ref rMessage.WorldPointB));

                    //actor.SetAabb((BoundingBox)MyLineHelpers.GetBoundingBox(ref rMessage.WorldPointA, ref rMessage.WorldPointB));
                    actor.MarkRenderDirty();

                    var matrix = Matrix.CreateTranslation((Vector3)(rMessage.WorldPointA + rMessage.WorldPointB) * 0.5f);
                    actor.SetMatrix(ref matrix);
                }

                break;
            }

            case MyRenderMessageEnum.SetRenderEntityData:
            {
                var rMessage = (MyRenderMessageSetRenderEntityData)message;

                Debug.Assert(false, "MyRenderMessageSetRenderEntityData is deprecated!");

                break;
            }

            case MyRenderMessageEnum.AddRuntimeModel:
            {
                var rMessage = (MyRenderMessageAddRuntimeModel)message;

                //MyDestructionMesh mesh = MyDestructionMesh.ModelsDictionary.Get(rMessage.Name);
                //if (mesh == null)
                //{
                //mesh = new MyDestructionMesh(rMessage.Name);

                //ProfilerShort.Begin("LoadBuffers");
                //mesh.Fill(rMessage.ModelData.Indices, rMessage.ModelData.Positions, rMessage.ModelData.Normals, rMessage.ModelData.Tangents, rMessage.ModelData.TexCoords, rMessage.ModelData.Sections, rMessage.ModelData.AABB);
                //ProfilerShort.End();

                if (!MyMeshes.Exists(rMessage.Name))
                {
                    {
                        ushort[] indices = new ushort[rMessage.ModelData.Indices.Count];
                        for (int i = 0; i < rMessage.ModelData.Indices.Count; i++)
                        {
                            indices[i] = (ushort)rMessage.ModelData.Indices[i];
                        }
                        var verticesNum = rMessage.ModelData.Positions.Count;
                        MyVertexFormatPositionH4[]            stream0 = new MyVertexFormatPositionH4[verticesNum];
                        MyVertexFormatTexcoordNormalTangent[] stream1 = new MyVertexFormatTexcoordNormalTangent[verticesNum];
                        for (int i = 0; i < verticesNum; i++)
                        {
                            stream0[i] = new MyVertexFormatPositionH4(rMessage.ModelData.Positions[i]);
                            stream1[i] = new MyVertexFormatTexcoordNormalTangent(
                                rMessage.ModelData.TexCoords[i], rMessage.ModelData.Normals[i], rMessage.ModelData.Tangents[i]);
                        }
                        var id = MyMeshes.CreateRuntimeMesh(X.TEXT(rMessage.Name), rMessage.ModelData.Sections.Count, false);
                        MyMeshes.UpdateRuntimeMesh(id, indices, stream0, stream1, rMessage.ModelData.Sections.ToArray(), rMessage.ModelData.AABB);
                    }

                    if (rMessage.ReplacedModel != null)
                    {
                        //MyAssetsLoader.ModelRemap[rMessage.ReplacedModel] = rMessage.Name;
                        MyAssetsLoader.ModelRemap[rMessage.Name] = rMessage.ReplacedModel;
                    }

                    //if (MyAssetsLoader.LOG_MESH_STATISTICS)
                    //{
                    //    mesh.DebugWriteInfo();
                    //}
                }

                break;
            }

            case MyRenderMessageEnum.UpdateModelProperties:
            {
                var rMessage = (MyRenderMessageUpdateModelProperties)message;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.ID);

                if (actor != null)
                {
                    var key = new MyEntityMaterialKey {
                        LOD = rMessage.LOD, Material = X.TEXT(rMessage.MaterialName)
                    };

                    if (rMessage.Enabled.HasValue)
                    {
                        if (!MyScene.EntityDisabledMaterials.ContainsKey(rMessage.ID))
                        {
                            MyScene.EntityDisabledMaterials.Add(rMessage.ID, new HashSet <MyEntityMaterialKey>());
                        }

                        if (!rMessage.Enabled.Value)
                        {
                            MyScene.EntityDisabledMaterials[rMessage.ID].Add(key);
                        }
                        else
                        {
                            MyScene.EntityDisabledMaterials[rMessage.ID].Remove(key);
                        }
                    }

                    var r = actor.GetRenderable();

                    if ((rMessage.Emissivity.HasValue || rMessage.DiffuseColor.HasValue) && !r.ModelProperties.ContainsKey(key))
                    {
                        r.ModelProperties[key] = new MyModelProperties();
                    }

                    if (rMessage.Emissivity.HasValue)
                    {
                        r.ModelProperties[key].Emissivity = rMessage.Emissivity.Value;
                    }

                    if (rMessage.DiffuseColor.HasValue)
                    {
                        r.ModelProperties[key].ColorMul = rMessage.DiffuseColor.Value;
                    }

                    actor.MarkRenderDirty();
                }

                break;
            }

            case MyRenderMessageEnum.PreloadModel:
            {
                var rMessage = (MyRenderMessagePreloadModel)message;

                //MyAssetsLoader.GetModel(rMessage.Name);
                MyMeshes.GetMeshId(X.TEXT(rMessage.Name));

                break;
            }

            case MyRenderMessageEnum.ChangeMaterialTexture:
            {
                var rMessage = (MyRenderMessageChangeMaterialTexture)message;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.RenderObjectID);

                if (actor != null)
                {
                    var r   = actor.GetRenderable();
                    var key = new MyEntityMaterialKey {
                        LOD = 0, Material = X.TEXT(rMessage.MaterialName)
                    };

                    if (!r.ModelProperties.ContainsKey(key))
                    {
                        r.ModelProperties[key] = new MyModelProperties();
                    }
                    r.ModelProperties[key].TextureSwap = new MyMaterialTextureSwap {
                        TextureName = X.TEXT(rMessage.TextureName)
                    };

                    r.FreeCustomRenderTextures(key);

                    actor.MarkRenderDirty();
                }

                break;
            }

            case MyRenderMessageEnum.DrawTextToMaterial:
            {
                var rMessage = (MyRenderMessageDrawTextToMaterial)message;

                //rMessage.EntityId
                //rMessage.FontColor
                //rMessage.MaterialName
                //rMessage.Text;
                //rMessage.TextScale;

                var actor = MyIDTracker <MyActor> .FindByID(rMessage.RenderObjectID);

                if (actor != null)
                {
                    var r   = actor.GetRenderable();
                    var key = new MyEntityMaterialKey {
                        LOD = 0, Material = X.TEXT(rMessage.MaterialName)
                    };

                    if (!r.ModelProperties.ContainsKey(key))
                    {
                        r.ModelProperties[key] = new MyModelProperties();
                    }
                    else
                    {
                        r.ModelProperties[key].TextureSwap = null;
                    }

                    RwTexId handle = r.ModelProperties[key].CustomRenderedTexture;
                    if (handle == RwTexId.NULL && MyModelProperties.CustomTextures < MyModelProperties.MaxCustomTextures)
                    {
                        handle = MyRwTextures.CreateRenderTarget(rMessage.TextureResolution * rMessage.TextureAspectRatio, rMessage.TextureResolution, SharpDX.DXGI.Format.R8G8B8A8_UNorm_SRgb, true);
                        r.ModelProperties[key].CustomRenderedTexture = handle;
                        ++MyModelProperties.CustomTextures;
                    }

                    if (handle != RwTexId.NULL)
                    {
                        var clearColor = new SharpDX.Color4(rMessage.BackgroundColor.PackedValue);
                        clearColor.Alpha = 0;
                        MyRender11.ImmediateContext.ClearRenderTargetView(handle.Rtv, clearColor);

                        // my sprites renderer -> push state
                        MySpritesRenderer.PushState(new Vector2(rMessage.TextureResolution * rMessage.TextureAspectRatio, rMessage.TextureResolution));


                        MySpritesRenderer.DrawText(Vector2.Zero, new StringBuilder(rMessage.Text), rMessage.FontColor, rMessage.TextScale);
                        // render text with fonts to rt
                        // update texture of proxy
                        MySpritesRenderer.Draw(handle.Rtv, new MyViewport(rMessage.TextureResolution * rMessage.TextureAspectRatio, rMessage.TextureResolution));

                        // render to rt
                        // my sprites renderer -> pop state
                        MySpritesRenderer.PopState();


                        MyRender11.ImmediateContext.GenerateMips(handle.ShaderView);

                        actor.MarkRenderDirty();
                    }
                    else
                    {
                        MyRenderProxy.TextNotDrawnToTexture(rMessage.EntityId);
                    }
                }
                else
                {
                    MyRenderProxy.TextNotDrawnToTexture(rMessage.EntityId);
                }

                break;
            }

            case MyRenderMessageEnum.PreloadMaterials:
            {
                var rMessage = (MyRenderMessagePreloadMaterials)message;

                //MyAssetsLoader.GetMaterials(rMessage.Name);
                MyMeshes.GetMeshId(X.TEXT(rMessage.Name));

                break;
            }

                #endregion

                #region Voxels

            case MyRenderMessageEnum.CreateClipmap:
            {
                var rMessage = (MyRenderMessageCreateClipmap)message;

                var clipmap = new MyClipmapHandler(rMessage.ClipmapId, rMessage.ScaleGroup, rMessage.WorldMatrix, rMessage.SizeLod0);
                MyClipmapFactory.ClipmapByID[rMessage.ClipmapId] = clipmap;
                clipmap.Base.LoadContent();


                break;
            }

            case MyRenderMessageEnum.UpdateClipmapCell:
            {
                var rMessage = (MyRenderMessageUpdateClipmapCell)message;

                var clipmap = MyClipmapFactory.ClipmapByID.Get(rMessage.ClipmapId);
                if (clipmap != null)
                {
                    clipmap.Base.UpdateCell(rMessage);
                }

                rMessage.Batches.Clear();
                break;
            }

            case MyRenderMessageEnum.InvalidateClipmapRange:
            {
                var rMessage = (MyRenderMessageInvalidateClipmapRange)message;

                var clipmap = MyClipmapFactory.ClipmapByID.Get(rMessage.ClipmapId);
                if (clipmap != null)
                {
                    clipmap.Base.InvalidateRange(rMessage.MinCellLod0, rMessage.MaxCellLod0);
                }

                break;
            }

            case MyRenderMessageEnum.CreateRenderVoxelMaterials:
            {
                var rMessage = (MyRenderMessageCreateRenderVoxelMaterials)message;

                Debug.Assert(MyVoxelMaterials1.CheckIndices(rMessage.Materials));
                MyVoxelMaterials1.Set(rMessage.Materials);

                rMessage.Materials = null;

                break;
            }


                #endregion

                #region Lights

            case MyRenderMessageEnum.CreateRenderLight:
            {
                var rMessage = (MyRenderMessageCreateRenderLight)message;

                //MyLight.Create(rMessage.ID);

                MyLights.Create(rMessage.ID);

                break;
            }

            case MyRenderMessageEnum.UpdateRenderLight:
            {
                var rMessage = (MyRenderMessageUpdateRenderLight)message;


                var light = MyLights.Get(rMessage.ID);

                if (light != LightId.NULL)
                {
                    var lightInfo = new MyLightInfo
                    {
                        Position           = rMessage.Position,
                        PositionWithOffset = rMessage.Position + rMessage.Offset * rMessage.Range * rMessage.ReflectorDirection,
                        CastsShadows       = rMessage.CastShadows,
                        ShadowsDistance    = rMessage.ShadowDistance,
                        ParentGID          = rMessage.ParentID,
                        UsedInForward      = rMessage.UseInForwardRender
                    };

                    MyLights.UpdateEntity(light, ref lightInfo);

                    if ((rMessage.Type & LightTypeEnum.PointLight) > 0)
                    {
                        MyLights.UpdatePointlight(light, rMessage.LightOn, rMessage.Range, new Vector3(rMessage.Color.R, rMessage.Color.G, rMessage.Color.B) / 255.0f * rMessage.Intensity, rMessage.Falloff);
                    }
                    if ((rMessage.Type & LightTypeEnum.Hemisphere) > 0)
                    {
                        //rMessage.Color;
                        //rMessage.Falloff;
                        //rMessage.Intensity;
                        //rMessage.LightOn;
                        //rMessage.ReflectorDirection;
                        //rMessage.ReflectorUp;
                    }
                    if ((rMessage.Type & LightTypeEnum.Spotlight) > 0)
                    {
                        // because it's so in dx9...
                        float coneMaxAngleCos = 1 - rMessage.ReflectorConeMaxAngleCos;
                        coneMaxAngleCos = (float)Math.Min(Math.Max(coneMaxAngleCos, 0.01), 0.99f);
                        MyLights.UpdateSpotlight(light, rMessage.ReflectorOn,
                                                 rMessage.ReflectorDirection, rMessage.ReflectorRange, coneMaxAngleCos, rMessage.ReflectorUp,
                                                 new Vector3(rMessage.ReflectorColor.R, rMessage.ReflectorColor.G, rMessage.ReflectorColor.B) / 255.0f * rMessage.Intensity, rMessage.ReflectorFalloff,
                                                 MyTextures.GetTexture(rMessage.ReflectorTexture, MyTextureEnum.CUSTOM));
                    }

                    if (rMessage.GlareOn)
                    {
                        MyLights.UpdateGlare(light, new MyGlareDesc
                            {
                                Enabled     = rMessage.GlareOn,
                                Material    = X.TEXT(rMessage.GlareMaterial),
                                Intensity   = rMessage.GlareIntensity,
                                QuerySize   = rMessage.GlareQuerySize,
                                Type        = rMessage.GlareType,
                                Size        = rMessage.GlareSize,
                                MaxDistance = rMessage.GlareMaxDistance,
                                Color       = rMessage.Color,
                                Direction   = rMessage.ReflectorDirection,
                                Range       = rMessage.Range
                            });
                    }
                }

                break;
            }

            case MyRenderMessageEnum.SetLightShadowIgnore:
            {
                var rMessage = (MyRenderMessageSetLightShadowIgnore)message;

                var light = MyLights.Get(rMessage.ID);
                var actor = MyIDTracker <MyActor> .FindByID(rMessage.ID2);

                if (light != LightId.NULL && actor != null)
                {
                    if (!MyLights.IgnoredEntitites.ContainsKey(light))
                    {
                        MyLights.IgnoredEntitites[light] = new HashSet <uint>();
                    }
                    MyLights.IgnoredEntitites[light].Add(rMessage.ID2);
                }

                break;
            }


            case MyRenderMessageEnum.ClearLightShadowIgnore:
            {
                var rMessage = (MyRenderMessageClearLightShadowIgnore)message;

                var light = MyLights.Get(rMessage.ID);
                if (light != LightId.NULL)
                {
                    MyLights.IgnoredEntitites.Remove(light);
                }

                break;
            }

            case MyRenderMessageEnum.UpdateFogSettings:
            {
                var rMessage = (MyRenderMessageUpdateFogSettings)message;

                MyEnvironment.FogSettings = rMessage.Settings;

                break;
            }

            case MyRenderMessageEnum.UpdateRenderEnvironment:
            {
                var rMessage = (MyRenderMessageUpdateRenderEnvironment)message;

                MyEnvironment.DirectionalLightDir       = VRageMath.Vector3.Normalize(rMessage.SunDirection);
                MyEnvironment.DirectionalLightIntensity = rMessage.SunIntensity * rMessage.SunColor.ToVector3();
                MyEnvironment.DirectionalLightEnabled   = rMessage.SunLightOn;
                MyEnvironment.DayTime           = (float)(rMessage.DayTime - Math.Truncate(rMessage.DayTime));
                MyEnvironment.SunDistance       = rMessage.DistanceToSun;
                MyEnvironment.SunColor          = rMessage.SunColor;
                MyEnvironment.SunMaterial       = rMessage.SunMaterial;
                MyEnvironment.SunSizeMultiplier = rMessage.SunSizeMultiplier;

                var skybox = rMessage.BackgroundTexture;

                m_resetEyeAdaptation = m_resetEyeAdaptation || rMessage.ResetEyeAdaptation;

                break;
            }

            case MyRenderMessageEnum.UpdateEnvironmentMap:
            {
                break;
            }

            case MyRenderMessageEnum.UpdatePostprocessSettings:
            {
                var rMessage = (MyRenderMessageUpdatePostprocessSettings)message;

                m_postprocessSettings = rMessage.Settings;

                break;
            }

            case MyRenderMessageEnum.UpdateSSAOSettings:
            {
                var rMessage = (MyRenderMessageUpdateSSAOSettings)message;


                MySSAO.Params.MinRadius  = rMessage.MinRadius;
                MySSAO.Params.MaxRadius  = rMessage.MaxRadius;
                MySSAO.Params.RadiusGrow = rMessage.RadiusGrowZScale;

                MySSAO.Params.RadiusBias    = rMessage.Bias;
                MySSAO.Params.Falloff       = rMessage.Falloff;
                MySSAO.Params.Normalization = rMessage.NormValue;
                MySSAO.Params.Contrast      = rMessage.Contrast;

                break;
            }

                #endregion

                #region Sprites
            case MyRenderMessageEnum.DrawSprite:
            case MyRenderMessageEnum.DrawSpriteNormalized:
            case MyRenderMessageEnum.DrawSpriteAtlas:
            case MyRenderMessageEnum.SpriteScissorPush:
            case MyRenderMessageEnum.SpriteScissorPop:
            {
                m_drawQueue.Enqueue(message);
                break;
            }

                #endregion

                #region Fonts and text

            case MyRenderMessageEnum.CreateFont:
            {
                var createFontMessage = message as MyRenderMessageCreateFont;
                Debug.Assert(createFontMessage != null);

                var renderFont = new MyRenderFont(createFontMessage.FontPath);
                renderFont.LoadContent();
                AddFont(createFontMessage.FontId, renderFont, createFontMessage.IsDebugFont);

                break;
            }

            case MyRenderMessageEnum.DrawString:
            {
                m_drawQueue.Enqueue(message);
                break;
            }

                #endregion

                #region Textures

            case MyRenderMessageEnum.PreloadTextures:
            {
                var preloadMsg = message as MyRenderMessagePreloadTextures;

                //MyTextureManager.PreloadTextures(preloadMsg.InDirectory, preloadMsg.Recursive);
                //MyTextures.UnloadTexture(texMessage.Texture);

                break;
            }

            case MyRenderMessageEnum.UnloadTexture:
            {
                var texMessage = (MyRenderMessageUnloadTexture)message;

                //MyTextureManager.UnloadTexture(texMessage.Texture);
                MyTextures.UnloadTexture(texMessage.Texture);

                break;
            }

            case MyRenderMessageEnum.ReloadTextures:
            {
                var reloadMsg = (MyRenderMessageReloadTextures)message;

                MyVoxelMaterials1.InvalidateMaterials();
                MyMeshMaterials1.InvalidateMaterials();
                MyTextures.ReloadAssetTextures();

                //MyTextureManager.UnloadTextures();
                //MyMaterialProxyFactory.ReloadTextures();

                break;
            }

            case MyRenderMessageEnum.ReloadModels:
            {
                var reloadMsg = (MyRenderMessageReloadModels)message;

                //MyMaterials.Clear();
                MyAssetsLoader.ReloadMeshes();
                MyRenderableComponent.MarkAllDirty();

                break;
            }

                #endregion

            case MyRenderMessageEnum.TakeScreenshot:
            {
                var rMessage = (MyRenderMessageTakeScreenshot)message;

                m_screenshot = new MyScreenshot(rMessage.PathToSave, rMessage.SizeMultiplier, rMessage.IgnoreSprites);

                break;
            }

            case MyRenderMessageEnum.ReloadEffects:
            {
                m_reloadShaders = true;

                //MyShaderBundleFactory.ClearCache();
                //MyShaderMaterial.ClearCache();
                //MyShaderPass.ClearCache();

                MyShaders.Recompile();
                MyMaterialShaders.Recompile();

                MyRenderableComponent.MarkAllDirty();

                foreach (var f in MyComponentFactory <MyFoliageComponent> .GetAll())
                {
                    f.Dispose();
                }

                break;
            }

            case MyRenderMessageEnum.PlayVideo:
            {
                var rMessage = (MyRenderMessagePlayVideo)message;

                MyVideoFactory.Create(rMessage.ID, rMessage.VideoFile);
                var video = MyVideoFactory.Videos.Get(rMessage.ID);
                if (video != null)
                {
                    video.Volume = rMessage.Volume;
                }

                break;
            }

            case MyRenderMessageEnum.CloseVideo:
            {
                var rMessage = (MyRenderMessageCloseVideo)message;

                var video = MyVideoFactory.Videos.Get(rMessage.ID);
                if (video != null)
                {
                    video.Stop();
                    video.Dispose();
                    MyVideoFactory.Videos.Remove(rMessage.ID);
                }

                break;
            }

            case MyRenderMessageEnum.DrawVideo:
            {
                var rMessage = (MyRenderMessageDrawVideo)message;

                var video = MyVideoFactory.Videos.Get(rMessage.ID);
                if (video != null)
                {
                    video.Draw(rMessage.Rectangle, rMessage.Color, rMessage.FitMode);
                }

                break;
            }

            case MyRenderMessageEnum.UpdateVideo:
            {
                var rMessage = (MyRenderMessageUpdateVideo)message;

                var video = MyVideoFactory.Videos.Get(rMessage.ID);
                if (video != null)
                {
                    video.Update();
                }

                break;
            }

            case MyRenderMessageEnum.SetVideoVolume:
            {
                var rMessage = (MyRenderMessageSetVideoVolume)message;

                var video = MyVideoFactory.Videos.Get(rMessage.ID);
                if (video != null)
                {
                    video.Volume = rMessage.Volume;
                }

                break;
            }

            case MyRenderMessageEnum.VideoAdaptersRequest:
            {
                MyRenderProxy.SendVideoAdapters(GetAdaptersList());
                break;
            }

            case MyRenderMessageEnum.SwitchDeviceSettings:
            {
                MyRenderProxy.RenderThread.SwitchSettings((message as MyRenderMessageSwitchDeviceSettings).Settings);
                break;
            }

            case MyRenderMessageEnum.SwitchRenderSettings:
                break;         // Can be ignored as we're handling newer version of the message.

            case MyRenderMessageEnum.SwitchRenderSettings1:
            {
                UpdateRenderSettings((message as MyRenderMessageSwitchRenderSettings1).Settings);
                break;
            }

            case MyRenderMessageEnum.UnloadData:
            {
                MyRender11.UnloadData();
                break;
            }

            case MyRenderMessageEnum.CollectGarbage:
            {
                GC.Collect();
                break;
            }

                #region Debug draw

            case MyRenderMessageEnum.DebugDrawPoint:
            case MyRenderMessageEnum.DebugDrawLine3D:
            case MyRenderMessageEnum.DebugDrawLine2D:
            case MyRenderMessageEnum.DebugDrawSphere:
            case MyRenderMessageEnum.DebugDrawAABB:
            case MyRenderMessageEnum.DebugDrawAxis:
            case MyRenderMessageEnum.DebugDrawOBB:
            case MyRenderMessageEnum.DebugDrawCone:
            case MyRenderMessageEnum.DebugDrawTriangle:
            case MyRenderMessageEnum.DebugDrawCapsule:
            case MyRenderMessageEnum.DebugDrawText2D:
            case MyRenderMessageEnum.DebugDrawText3D:
            case MyRenderMessageEnum.DebugDrawModel:
            case MyRenderMessageEnum.DebugDrawTriangles:
            case MyRenderMessageEnum.DebugDrawPlane:
            case MyRenderMessageEnum.DebugDrawCylinder:
            {
                m_debugDrawMessages.Enqueue(message);
            }
            break;

            case MyRenderMessageEnum.DebugCrashRenderThread:
            {
                throw new InvalidOperationException("Forced exception");
            }
                #endregion
            }
        }