/// <summary> /// Update component /// </summary> /// <param name="dT">spend time [sec]</param> public override void Update(double dT) { if (ModelContext.DrawModel == null) { return; } var drawSys = DrawSystem.GetInstance(); var cullingSys = CullingSystem.GetInstance(); var context = drawSys.GetDrawContext(); var dbg = DrawSystem.GetInstance().DebugCtrl; var layout = m_layoutC.Transform; CullingSystem.FrustumCullingResult result = cullingSys.CheckFrustumCulling(ModelContext.DrawModel.BoundingBox, layout); if (!result.IsVisible) { // out of view-volume } else { // Add command for draw mdoel foreach (var node in ModelContext.DrawModel.NodeList) { if (UpdateLine == GameEntityComponent.UpdateLines.PreDraw) { // use draw buffer drawSys.GetDrawBuffer().AppendStaticModel(layout, result.Z, ref node.Mesh, node.Material); } else if (UpdateLine == GameEntityComponent.UpdateLines.Draw) { Matrix[] boneMatrices = null; if (m_skeletonC != null) { // has skeleton boneMatrices = m_skeletonC.Skeleton.GetAllSkinningTransforms(); } MaterialBase material = node.Material; context.DrawModel(layout, Color4.White, node.Mesh, material, DrawSystem.RenderMode.Opaque, boneMatrices); } } } /* * // Add command for debug mdoel * if (dbg.IsEnableDrawTangentFrame && ModelContext.DebugModel != null) * { * foreach (var node in ModelContext.DebugModel.NodeList) * { * var command = DrawCommand.CreateDrawDebugCommand(node.Material, layout, node.Mesh); * drawSys.AddDrawCommand(command); * } * } */ /* * // Add command for draw shadow * if (ModelContext.EnableCastShadow) * { * foreach (var node in ModelContext.DrawModel.NodeList) * { * var command = DrawCommand.CreateDrawShadowCommand(layout, node.Mesh); * drawSys.AddDrawCommand(command); * } * * } */ // draw aabb if (dbg.IsEnableAabb) { if (m_dbgBoundingBoxModel == null) { // create model in first draw time var model = DrawModel.CreateBox("aabb", ModelContext.DrawModel.BoundingBox, Color.Pink); m_dbgBoundingBoxModel = model; } var mesh = m_dbgBoundingBoxModel.NodeList[0].Mesh; MaterialBase material = m_dbgBoundingBoxModel.NodeList[0].Material; context.DrawDebugModel(layout, mesh, DrawSystem.RenderMode.Transparency); } // draw bones for debug if (dbg.IsEnableDrawBone) { if (m_skeletonC != null) { m_skeletonC.Skeleton.DrawDebugModel(layout); } } }
virtual public void DrawModel(Matrix worldTrans, Color4 color, DrawSystem.MeshData mesh, MaterialBase material, DrawSystem.RenderMode renderMode, Matrix[] boneMatrices) { _SetModelParams(mesh, material, renderMode); // update vertex shader resouce var vdata = new _VertexShaderConst_Main() { // hlsl is column-major memory layout, so we must transpose matrix worldMat = Matrix.Transpose(worldTrans), }; m_context.UpdateSubresource(ref vdata, m_mainVtxConst); // update pixel shader resouce var pdata = new _PixelShaderConst_Main() { instanceColor = color }; m_context.UpdateSubresource(ref pdata, m_mainPixConst); // update a bone constant buffer if (boneMatrices != null) { // UpdateSubresouce supports only to upate entire of resource. // so, we must copy boneMatrices to m_tmpBoneMatrices. // It seems inefficient. we search for better solutions. Debug.Assert(boneMatrices.Length <= m_tmpBoneMatrices.Length); for (int i = 0; i < boneMatrices.Length; ++i) { m_tmpBoneMatrices[i] = boneMatrices[i]; m_tmpBoneMatrices[i].Transpose(); } m_context.UpdateSubresource <Matrix>(m_tmpBoneMatrices, m_boneVtxConst); } else { // low performance @todo for (int i = 0; i < m_tmpBoneMatrices.Length; ++i) { m_tmpBoneMatrices[i] = Matrix.Identity; } m_context.UpdateSubresource <Matrix>(m_tmpBoneMatrices, m_boneVtxConst); } // draw m_context.Draw(mesh.VertexCount, 0); m_drawCallCount++; }
public void AppendStaticModel(Matrix layout, float z, ref DrawSystem.MeshData mesh, MaterialBase material) { var key = new _StaticModelData() { Mesh = mesh, Material = material }; List <_InstanceData> instanceList = null; if (!m_staticModelBuffer.TryGetValue(key, out instanceList)) { int capacity = 128; instanceList = new List <_InstanceData>(capacity); m_staticModelBuffer.Add(key, instanceList); } instanceList.Add(new _InstanceData() { Layout = layout, Z = z }); }
private void _SetModelParams(DrawSystem.MeshData mesh, MaterialBase material, DrawSystem.RenderMode renderMode) { // update texture slot DrawSystem.TextureData tex; bool bModelPixConstChanged = false; for (int index = 0; index < m_lastTextureSlots.Count(); ++index) { int slotIndex = m_lastTextureSlots[index].SlotIndex; if (material == null) { tex = DrawSystem.TextureData.Null(); } else { material.GetTextureDataBySlotIndex(slotIndex, out tex); } // update resouce if (m_lastTextureSlots[index].Texture.Resource != tex.Resource) { // update resource if (tex.Resource != null) { m_context.PixelShader.SetShaderResource(slotIndex, tex.Resource.View); m_context.PixelShader.SetSampler(slotIndex, tex.Resource.SamplerState); } else { // set null texture m_context.PixelShader.SetShaderResource(slotIndex, null); m_context.PixelShader.SetSampler(slotIndex, null); } m_lastTextureSlots[index].Texture.Resource = tex.Resource; } // update texture uv scale if (!m_lastTextureSlots[index].Texture.UvScale.Equals(tex.UvScale)) { bModelPixConstChanged = true; m_lastTextureSlots[index].Texture.UvScale = tex.UvScale; } } if (bModelPixConstChanged) { var modelPixConst = new _PixelShaderConst_Model() { uvScale1 = m_lastTextureSlots[0].Texture.UvScale, uvScale2 = m_lastTextureSlots[1].Texture.UvScale, }; m_context.UpdateSubresource(ref modelPixConst, m_modelPixConst); } // update model vertex shader param bool isEnableSkinning = mesh.Buffers.Count() == 3; if (m_lastEnableSkinning == null || !m_lastEnableSkinning.Value.Equals(isEnableSkinning)) { var modelVtxConst = new _VertexShaderConst_Model() { isEnableSkinning = isEnableSkinning, }; m_context.UpdateSubresource(ref modelVtxConst, m_modelVtxConst); m_lastEnableSkinning = isEnableSkinning; } // update material MaterialBase.MaterialTypes materialType = material == null ? MaterialBase.MaterialTypes.Debug : material.Type; if (m_materialType == null || m_materialType != materialType) { switch (materialType) { case MaterialBase.MaterialTypes.Standard: { Effect effect = null; effect = m_initParam.Repository.FindResource <Effect>("Std"); m_context.InputAssembler.InputLayout = effect.Layout; m_context.VertexShader.Set(effect.VertexShader); m_context.PixelShader.Set(effect.PixelShader); } break; case MaterialBase.MaterialTypes.Minimap: { Effect effect = null; effect = m_initParam.Repository.FindResource <Effect>("Minimap"); m_context.InputAssembler.InputLayout = effect.Layout; m_context.VertexShader.Set(effect.VertexShader); m_context.PixelShader.Set(effect.PixelShader); // update material shader param var mtl = material as MinimapMaterial; m_context.PixelShader.SetConstantBuffer(3, m_modelMinimapPixConst); var tmpConst = new _PixelShaderConst_ModelMinimap(); tmpConst.width = mtl.GetMapWidth(); tmpConst.height = mtl.GetMapHeight(); int[] mapTable = mtl.GetMapTable(); unsafe { // copy map table Marshal.Copy(mapTable, 0, new IntPtr(tmpConst.map), tmpConst.width * tmpConst.height); } m_context.UpdateSubresource(ref tmpConst, m_modelMinimapPixConst); } break; case MaterialBase.MaterialTypes.Debug: { Effect effect = null; effect = m_initParam.Repository.FindResource <Effect>("Debug"); m_context.InputAssembler.InputLayout = effect.Layout; m_context.VertexShader.Set(effect.VertexShader); m_context.PixelShader.Set(effect.PixelShader); } break; case MaterialBase.MaterialTypes.Marker: Debug.Assert(false, "unsupported material"); break; } m_materialType = materialType; } // update render mode if (m_lastRenderMode == null || m_lastRenderMode != renderMode) { m_context.OutputMerger.BlendState = m_initParam.BlendStates[(int)renderMode]; m_context.OutputMerger.DepthStencilState = m_initParam.DepthStencilStates[(int)renderMode]; m_lastRenderMode = renderMode; } // update input assembler if (m_lastTopology == null || m_lastTopology != mesh.Topology) { m_context.InputAssembler.PrimitiveTopology = mesh.Topology; m_lastTopology = mesh.Topology; } if (m_lastVertexBuffers == null || m_lastVertexBuffers != mesh.Buffers) { m_context.InputAssembler.SetVertexBuffers(0, mesh.Buffers); m_lastVertexBuffers = mesh.Buffers; m_lastVertexCount = mesh.VertexCount; } }
virtual public void BeginDrawInstance(DrawSystem.MeshData mesh, MaterialBase material, DrawSystem.RenderMode renderMode) { Debug.Assert(material.IsEnableInstanceRendering(), "this material does not support instance rendering"); _SetModelParams(mesh, material, renderMode); }
public void BeginDrawInstance(DrawSystem.MeshData mesh, MaterialBase material, DrawSystem.RenderMode renderMode) { m_isContextDirty = true; m_context.BeginDrawInstance(mesh, material, renderMode); }
public void DrawModel(Matrix worldTrans, Color4 color, DrawSystem.MeshData mesh, MaterialBase material, DrawSystem.RenderMode renderMode, Matrix[] boneMatrices) { m_isContextDirty = true; m_context.DrawModel(worldTrans, color, mesh, material, renderMode, boneMatrices); }
private bool _LoadMaterial(BlendTypeRepository repository, BlendValueCapsule bMaterial, out Dictionary <DrawSystem.TextureTypes, TextureInfo> outTextureInfos, out MaterialBase outMaterial) { string mtlName = bMaterial.GetMember("id").GetMember("name").GetAllValueAsString(); Console.WriteLine(" found material : " + mtlName); var texInfos = new Dictionary <DrawSystem.TextureTypes, TextureInfo>(); string materialTypeName = Path.GetExtension(mtlName); switch (materialTypeName) { case "": case ".std": _LoadTextures(repository, bMaterial, ref texInfos, new DrawSystem.TextureTypes[] { DrawSystem.TextureTypes.Diffuse0, DrawSystem.TextureTypes.Bump0 }); outMaterial = new StandardMaterial(); outTextureInfos = texInfos; return(true); case ".map": _LoadTextures(repository, bMaterial, ref texInfos, new DrawSystem.TextureTypes[] { DrawSystem.TextureTypes.Diffuse0 }); outMaterial = new MinimapMaterial(); texInfos.Add(DrawSystem.TextureTypes.MinimapRoute, new TextureInfo { Name = "route.png", UvScale = new Vector2(1, 1) }); // add a special texture outTextureInfos = texInfos; return(true); case ".mark": { var prop = _FindCustomProperty(bMaterial.GetMember("id"), "id"); if (prop == null) { Debug.Fail("marker material must have id property"); break; } outMaterial = MarkerMaterial.Create(prop.Value); outTextureInfos = texInfos; } return(true); default: Debug.Fail("unknown material type : " + materialTypeName); break; } outMaterial = null; outTextureInfos = texInfos; return(false); }