Exemplo n.º 1
0
        /// <summary>
        /// Update component
        /// </summary>
        /// <param name="dT">spend time [sec]</param>
        public override void Update(double dT)
        {
            var layout  = m_layoutC.Transform;
            var drawSys = DrawSystem.GetInstance();

            m_lastDrawModel = m_drawFunc(drawSys.GetDrawContext(), layout, m_lastDrawModel);
        }
Exemplo n.º 2
0
        public void Draw(IDrawContext context)
        {
            var    drawSys = DrawSystem.GetInstance();
            Matrix layout  = m_initParam.Layout;

            foreach (char c in m_text)
            {
                var   tex    = DrawSystem.TextureData.Null();
                float offset = 0.0f;
                switch (c)
                {
                case '.':
                    tex    = m_initParam.Dot;
                    offset = -0.22f;
                    break;

                default:
                    if ('0' <= c && c <= '9')
                    {
                        int num = (int)c - (int)'0';
                        tex    = m_initParam.Numbers[num];
                        offset = -0.3f;
                    }
                    break;
                }

                Debug.Assert(tex.Resource != null, "invalid character");
                context.DrawModel(layout * m_worldTrans, Color4.White, m_plane.NodeList[0].Mesh, StandardMaterial.Create(tex), DrawSystem.RenderMode.Transparency, null);
                layout *= Matrix.Translation(offset, 0, 0);
            }
        }
Exemplo n.º 3
0
        public void UpdateFrustum()
        {
            var drawSys   = DrawSystem.GetInstance();
            var cameraSys = CameraSystem.GetInstance();

            // frustum culling use always the ingame camera. this means that you can look down culling in editor mode
            var camera = cameraSys.IngameCamera;

            m_frustumMatrix = camera.GetCameraData().GetViewMatrix() * drawSys.GetDrawContext().GetHeadMatrix() * drawSys.ComputeProjectionTransform();
        }
Exemplo n.º 4
0
        public static DrawModel CreateFloor(float geometryScale, float uvScale, Vector4 offset)
        {
            var   drawSys = DrawSystem.GetInstance();
            var   d3d     = drawSys.D3D;
            float gs      = geometryScale;
            float us      = uvScale;

            var vertices = new _VertexCommon[]
            {
                new _VertexCommon()
                {
                    Position = new Vector4(-gs, 0, gs, 1), Texcoord = new Vector2(0, 0), Normal = Vector3.UnitY
                },
                new _VertexCommon()
                {
                    Position = new Vector4(gs, 0, gs, 1), Texcoord = new Vector2(us, 0), Normal = Vector3.UnitY
                },
                new _VertexCommon()
                {
                    Position = new Vector4(gs, 0, -gs, 1), Texcoord = new Vector2(us, us), Normal = Vector3.UnitY
                },
                new _VertexCommon()
                {
                    Position = new Vector4(-gs, 0, gs, 1), Texcoord = new Vector2(0, 0), Normal = Vector3.UnitY
                },
                new _VertexCommon()
                {
                    Position = new Vector4(gs, 0, -gs, 1), Texcoord = new Vector2(us, us), Normal = Vector3.UnitY
                },
                new _VertexCommon()
                {
                    Position = new Vector4(-gs, 0, -gs, 1), Texcoord = new Vector2(0, us), Normal = Vector3.UnitY
                },
            };

            Aabb aabb = Aabb.Invalid();

            for (int i = 0; i < vertices.Length; ++i)
            {
                vertices[i].Position  += offset;
                vertices[i].Position.W = 1;
                aabb.ExtendByPoint(MathUtil.ToVector3(vertices[i].Position));
            }

            var model = new DrawModel("");
            var mesh  = DrawUtil.CreateMeshData <_VertexCommon>(d3d, PrimitiveTopology.TriangleList, vertices);

            model.m_nodeList.Add(new Node()
            {
                Mesh = mesh, Material = DebugMaterial.Create(), IsDebug = false, HasBone = false
            });
            model.m_bb = aabb;

            return(model);
        }
Exemplo n.º 5
0
        private void _OnLoad(object sender, EventArgs e)
        {
#if DEBUG
            m_debugDialog = new DebugDialog();
            m_debugDialog.Show();
            var debugMenu = m_debugDialog.GetSystemMenuItem();
            DrawSystem.GetInstance().CreateDebugMenu(debugMenu);
            GameSystem.GetInstance().CreateDebugMenu(debugMenu);
            CameraSystem.GetInstance().CreateDebugMenu(debugMenu);
#endif // DEBUG
        }
Exemplo n.º 6
0
        public void DrawDebugModel(Matrix masterTrans)
        {
            var drawSys = DrawSystem.GetInstance();
            var context = drawSys.GetDrawContext();

            foreach (var node in m_nodeArray)
            {
                if (node.DbgDrawModel == null)
                {
                    // create model in first draw time
                    var model = DrawModel.CreateBone(node.Bone.Name, node.Bone.Length, Color.Yellow, Vector4.Zero);
                    //var model = DrawModel.CreateGizmo(node.Bone.Name, 1.0f);
                    node.DbgDrawModel = model;
                }

                var mesh           = node.DbgDrawModel.NodeList[0].Mesh;
                var worldTransform = GetModelTransform(node.Index) * masterTrans;
                var material       = node.DbgDrawModel.NodeList[0].Material;
                context.DrawDebugModel(worldTransform, mesh, DrawSystem.RenderMode.Transparency);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Update component
        /// </summary>
        /// <param name="dT">spend time [sec]</param>
        public override void Update(double dT)
        {
            var cameraSys = CameraSystem.GetInstance();
            var drawSys   = DrawSystem.GetInstance();

            // Calc camera
            var viewHeadMat = cameraSys.GetCameraData().GetViewMatrix() * drawSys.GetDrawContext().GetHeadMatrix();

            viewHeadMat.Invert();
            var moveDirection = new Vector3(viewHeadMat.Backward.X, 0, viewHeadMat.Backward.Z);

            moveDirection.Normalize();

            var gameSys = GameSystem.GetInstance();

            switch (gameSys.Config.InputDevice)
            {
            case GameConfig.UserInputDevices.MouseKeyboard:
            {
                IMouseKeyboardInputSource src = InputSystem.GetInstance().MouseKeyboard;

                var v = Vector3.Zero;
                if (src.TestKeyState(Keys.W))
                {
                    v.Z += PositionFactor;
                }
                if (src.TestKeyState(Keys.A))
                {
                    v.X -= PositionFactor;
                }
                if (src.TestKeyState(Keys.D))
                {
                    v.X += PositionFactor;
                }
                if (src.TestKeyState(Keys.S))
                {
                    v.Z -= PositionFactor;
                }

                if (v.X != 0 || v.Z != 0)
                {
                    float angleY = (float)Math.Atan2(moveDirection.X, moveDirection.Z);
                    v = Vector3.Transform(v, Matrix3x3.RotationY(angleY));
                    m_behaviorC.RequestMove(v);
                    m_behaviorC.RequestTurn(v);
                }
            }
            break;

            case GameConfig.UserInputDevices.Pad:
            {
                IPadInputSource src = InputSystem.GetInstance().Pad;

                float angleY    = (float)Math.Atan2(moveDirection.X, moveDirection.Z);
                var   thumbDir  = src.LeftThumbInput.Direction;
                float magnitude = src.LeftThumbInput.NormalizedMagnitude;
                if (magnitude >= MinimumPadMagnitude)
                {
                    var v = new Vector3(thumbDir.X, 0, thumbDir.Y);
                    v = Vector3.Transform(v, Matrix3x3.RotationY(angleY));
                    v = v * magnitude;
                    m_behaviorC.RequestMove(v);
                    m_behaviorC.RequestTurn(v);
                }
            }
            break;

            default:
                Debug.Fail("unsupported input device type : " + gameSys.Config.InputDevice);
                break;
            }
        }
Exemplo n.º 8
0
        public static DrawModel CreateFrustum(String uid, Matrix viewProjectionMatrix, Color4 color)
        {
            var drawSys = DrawSystem.GetInstance();
            var d3d     = drawSys.D3D;

            viewProjectionMatrix.Invert();

            var points = new Vector4[8];

            points[0] = Vector3.Transform(new Vector3(-1, -1, 0), viewProjectionMatrix);
            points[1] = Vector3.Transform(new Vector3(1, -1, 0), viewProjectionMatrix);
            points[2] = Vector3.Transform(new Vector3(-1, 1, 0), viewProjectionMatrix);
            points[3] = Vector3.Transform(new Vector3(1, 1, 0), viewProjectionMatrix);
            points[4] = Vector3.Transform(new Vector3(-1, -1, 1), viewProjectionMatrix);
            points[5] = Vector3.Transform(new Vector3(1, -1, 1), viewProjectionMatrix);
            points[6] = Vector3.Transform(new Vector3(-1, 1, 1), viewProjectionMatrix);
            points[7] = Vector3.Transform(new Vector3(1, 1, 1), viewProjectionMatrix);

            var vertices = new _VertexDebug[]
            {
                new _VertexDebug()
                {
                    Position = points[0]
                },
                new _VertexDebug()
                {
                    Position = points[1]
                },
                new _VertexDebug()
                {
                    Position = points[1]
                },
                new _VertexDebug()
                {
                    Position = points[3]
                },
                new _VertexDebug()
                {
                    Position = points[3]
                },
                new _VertexDebug()
                {
                    Position = points[2]
                },
                new _VertexDebug()
                {
                    Position = points[2]
                },
                new _VertexDebug()
                {
                    Position = points[0]
                },

                new _VertexDebug()
                {
                    Position = points[4]
                },
                new _VertexDebug()
                {
                    Position = points[5]
                },
                new _VertexDebug()
                {
                    Position = points[5]
                },
                new _VertexDebug()
                {
                    Position = points[7]
                },
                new _VertexDebug()
                {
                    Position = points[7]
                },
                new _VertexDebug()
                {
                    Position = points[6]
                },
                new _VertexDebug()
                {
                    Position = points[6]
                },
                new _VertexDebug()
                {
                    Position = points[4]
                },

                new _VertexDebug()
                {
                    Position = points[0]
                },
                new _VertexDebug()
                {
                    Position = points[4]
                },
                new _VertexDebug()
                {
                    Position = points[1]
                },
                new _VertexDebug()
                {
                    Position = points[5]
                },
                new _VertexDebug()
                {
                    Position = points[2]
                },
                new _VertexDebug()
                {
                    Position = points[6]
                },
                new _VertexDebug()
                {
                    Position = points[3]
                },
                new _VertexDebug()
                {
                    Position = points[7]
                },
            };

            Aabb aabb = Aabb.Invalid();

            for (int i = 0; i < vertices.Length; ++i)
            {
                vertices[i].Position.X /= vertices[i].Position.W;
                vertices[i].Position.Y /= vertices[i].Position.W;
                vertices[i].Position.Z /= vertices[i].Position.W;
                vertices[i].Position.W  = 1;
                vertices[i].Color       = color;
                aabb.ExtendByPoint(MathUtil.ToVector3(vertices[i].Position));
            }

            var model = new DrawModel(uid);

            model.m_nodeList.Add(new Node()
            {
                Mesh     = DrawUtil.CreateMeshData <_VertexDebug>(d3d, PrimitiveTopology.LineList, vertices),
                Material = DebugMaterial.Create(),
                IsDebug  = true,
                HasBone  = false,
            });
            model.m_bb = aabb;

            return(model);
        }
Exemplo n.º 9
0
        public static DrawModel CreateBox(String uid, Aabb box, Color4 color)
        {
            var drawSys = DrawSystem.GetInstance();
            var d3d     = drawSys.D3D;

            Vector3 min      = box.Min;
            Vector3 max      = box.Max;
            var     vertices = new _VertexDebug[]
            {
                new _VertexDebug()
                {
                    Position = new Vector4(min.X, min.Y, min.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, min.Y, min.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, min.Y, min.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, min.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, min.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(min.X, min.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(min.X, min.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(min.X, min.Y, min.Z, 1)
                },

                new _VertexDebug()
                {
                    Position = new Vector4(min.X, min.Y, min.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(min.X, max.Y, min.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, min.Y, min.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, max.Y, min.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, min.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, max.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(min.X, min.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(min.X, max.Y, max.Z, 1)
                },

                new _VertexDebug()
                {
                    Position = new Vector4(min.X, max.Y, min.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, max.Y, min.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, max.Y, min.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, max.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(max.X, max.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(min.X, max.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(min.X, max.Y, max.Z, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(min.X, max.Y, min.Z, 1)
                },
            };

            Aabb aabb = Aabb.Invalid();

            for (int i = 0; i < vertices.Length; ++i)
            {
                vertices[i].Position.W = 1;
                vertices[i].Color      = color;
                aabb.ExtendByPoint(MathUtil.ToVector3(vertices[i].Position));
            }

            var model = new DrawModel(uid);

            model.m_nodeList.Add(new Node()
            {
                Mesh     = DrawUtil.CreateMeshData <_VertexDebug>(d3d, PrimitiveTopology.LineList, vertices),
                Material = DebugMaterial.Create(),
                IsDebug  = true,
                HasBone  = false,
            });
            model.m_bb = aabb;

            return(model);
        }
Exemplo n.º 10
0
        public static DrawModel CreateBone(String uid, float length, Color4 color, Vector4 offset)
        {
            var   drawSys = DrawSystem.GetInstance();
            var   d3d     = drawSys.D3D;
            float w       = 0.1f * length;
            float L       = length;

            var vertices = new _VertexDebug[]
            {
                // bottom pyramid
                new _VertexDebug()
                {
                    Position = new Vector4(0, 0, 0, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(-w, -w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(0, 0, 0, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(-w, w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(0, 0, 0, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(w, -w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(0, 0, 0, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(w, w, w, 1)
                },

                // middle plane
                new _VertexDebug()
                {
                    Position = new Vector4(w, w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(-w, w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(-w, w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(-w, -w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(-w, -w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(w, -w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(w, -w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(w, w, w, 1)
                },

                // top pyramid
                new _VertexDebug()
                {
                    Position = new Vector4(0, 0, L, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(-w, -w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(0, 0, L, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(w, -w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(0, 0, L, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(-w, w, w, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(0, 0, L, 1)
                },
                new _VertexDebug()
                {
                    Position = new Vector4(w, w, w, 1)
                },
            };

            Aabb aabb = Aabb.Invalid();

            for (int i = 0; i < vertices.Length; ++i)
            {
                vertices[i].Position  += offset;
                vertices[i].Position.W = 1;
                vertices[i].Color      = color;
                aabb.ExtendByPoint(MathUtil.ToVector3(vertices[i].Position));
            }

            var model = new DrawModel(uid);

            model.m_nodeList.Add(new Node()
            {
                Mesh     = DrawUtil.CreateMeshData <_VertexDebug>(d3d, PrimitiveTopology.LineList, vertices),
                Material = DebugMaterial.Create(),
                IsDebug  = true,
                HasBone  = false,
            });
            model.m_bb = aabb;

            return(model);
        }
Exemplo n.º 11
0
        public static DrawModel FromScene(String uid, BlenderScene scene, string fileSearchPath)
        {
            var drawSys        = DrawSystem.GetInstance();
            var drawRepository = drawSys.ResourceRepository;
            var d3d            = drawSys.D3D;

            var  model   = new DrawModel(uid);
            bool hasBone = BlenderUtil.GetLengthOf(scene.NodeList[0].BoneArray) > 0;// boneArray is set to the first node
            var  aabb    = Aabb.Invalid();

            foreach (var n in scene.NodeList)
            {
                if (n.MaterialData.Type == MaterialBase.MaterialTypes.Marker)
                {
                    // marker material is used as 'Marker'
                    continue;
                }

                if (n.Vertics.Count() == 0)
                {
                    // empty vertex list
                    continue;
                }

                // Build a vertex buffer(s)
                var vertices1 = n.Vertics
                                .Select(v => new _VertexCommon()
                {
                    Position = v.Position,
                    Normal   = v.Normal,
                    Texcoord = v.Texcoord
                }).ToArray();

                var vertices2 = n.Vertics
                                .Select(v => v.Tangent).ToArray();

                // update aabb
                foreach (var v in vertices1)
                {
                    aabb.ExtendByPoint(MathUtil.ToVector3(v.Position));
                }

                var node = new Node();
                node.Material = n.MaterialData;
                node.IsDebug  = false;
                node.HasBone  = hasBone;
                if (node.HasBone)
                {
                    // if model has bone, we create a bone vertex info
                    var vertices3 = n.Vertics
                                    .Select(v =>
                    {
                        Debug.Assert(BlenderUtil.GetLengthOf(v.BoneIndices) == BlenderUtil.GetLengthOf(v.BoneWeights), "both of bone index and bone weight must be matched");
                        //Debug.Assert(BlenderUtil.GetLengthOf(v.BoneWeights) <= _VertexBoneWeight.MAX_COUNT, "length of bone weight is over :" + BlenderUtil.GetLengthOf(v.BoneWeights));
                        Debug.Assert(BlenderUtil.GetLengthOf(v.BoneWeights) != 0, "no bone entry");
                        var tmp = new _VertexBoneWeight()
                        {
                            Index0  = BlenderUtil.GetLengthOf(v.BoneIndices) > 0 ? v.BoneIndices[0] : 0,
                            Weight0 = BlenderUtil.GetLengthOf(v.BoneWeights) > 0 ? v.BoneWeights[0] : 0.0f,
                            Index1  = BlenderUtil.GetLengthOf(v.BoneIndices) > 1 ? v.BoneIndices[1] : 0,
                            Weight1 = BlenderUtil.GetLengthOf(v.BoneWeights) > 1 ? v.BoneWeights[1] : 0.0f,
                            Index2  = BlenderUtil.GetLengthOf(v.BoneIndices) > 2 ? v.BoneIndices[2] : 0,
                            Weight2 = BlenderUtil.GetLengthOf(v.BoneWeights) > 2 ? v.BoneWeights[2] : 0.0f,
                            Index3  = BlenderUtil.GetLengthOf(v.BoneIndices) > 3 ? v.BoneIndices[3] : 0,
                            Weight3 = BlenderUtil.GetLengthOf(v.BoneWeights) > 3 ? v.BoneWeights[3] : 0.0f,
                        };
                        float sumWeight = tmp.Weight0 + tmp.Weight1 + tmp.Weight2 + tmp.Weight3;
                        tmp.Weight0    /= sumWeight;
                        tmp.Weight1    /= sumWeight;
                        tmp.Weight2    /= sumWeight;
                        tmp.Weight3    /= sumWeight;
                        return(tmp);
                    }).ToArray();

                    node.Mesh = DrawUtil.CreateMeshData(d3d, PrimitiveTopology.TriangleList, vertices1, vertices2, vertices3);
                }
                else
                {
                    node.Mesh = DrawUtil.CreateMeshData(d3d, PrimitiveTopology.TriangleList, vertices1, vertices2);
                }

                // add dispoable

/*
 * foreach (var buf in node.Mesh.Buffers)
 * {
 *  model._AddDisposable(buf.Buffer);
 * }
 */

                // create skeleton
                if (model.m_boneArray == null && n.BoneArray != null)
                {
                    model.m_boneArray = (DrawSystem.BoneData[])n.BoneArray.Clone();
                }

                // load new texture
                foreach (var texInfo in n.TextureInfos.Values)
                {
                    if (!drawRepository.Contains(texInfo.Name))
                    {
                        var tex = TextureView.FromFile(texInfo.Name, drawSys.D3D, Path.Combine(fileSearchPath, texInfo.Name));
                        drawRepository.AddResource(tex);
                    }
                }

                // copy textures from cache
                foreach (DrawSystem.TextureTypes textureType in Enum.GetValues(typeof(DrawSystem.TextureTypes)))
                {
                    if (n.TextureInfos.ContainsKey(textureType))
                    {
                        node.Material.SetTextureData(
                            textureType,
                            new DrawSystem.TextureData
                        {
                            Resource = drawRepository.FindResource <TextureView>(n.TextureInfos[textureType].Name),
                            UvScale  = n.TextureInfos[textureType].UvScale,
                        });
                    }
                }

                model.m_nodeList.Add(node);
            }

            model.m_bb = aabb;
            return(model);
        }
Exemplo n.º 12
0
        /// <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);
                }
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Update component
        /// </summary>
        /// <param name="dT">spend time [sec]</param>
        public override void Update(double dT)
        {
            var mapSys = MapSystem.GetInstance();

            m_coroutine.Update(dT);

            if (!m_coroutine.HasCompleted())
            {
                // wait for end of character operation
                return;
            }

            var cameraSys = CameraSystem.GetInstance();
            var drawSys   = DrawSystem.GetInstance();

            var gameSys = GameSystem.GetInstance();

            switch (gameSys.Config.InputDevice)
            {
            case GameConfig.UserInputDevices.MouseKeyboard:
            {
                IMouseKeyboardInputSource src = InputSystem.GetInstance().MouseKeyboard;

                var v = Vector3.Zero;
                if (src.TestKeyState(Keys.W))
                {
                    v.Z += PositionFactor;
                }
                if (src.TestKeyState(Keys.A))
                {
                    v.X -= PositionFactor;
                }
                if (src.TestKeyState(Keys.D))
                {
                    v.X += PositionFactor;
                }
                if (src.TestKeyState(Keys.S))
                {
                    v.Z -= PositionFactor;
                }

                if (v.X != 0 || v.Z != 0)
                {
                    // @todo
                }
            }
            break;

            case GameConfig.UserInputDevices.Pad:
            {
                IPadInputSource src = InputSystem.GetInstance().Pad;

                // update move
                {
                    _InputType inputType = _InputType.None;

                    var   thumbDir  = src.LeftThumbInput.Direction;
                    float magnitude = src.LeftThumbInput.NormalizedMagnitude;
                    if (magnitude >= MinimumPadMagnitude)
                    {
                        // use pad stick
                        if (thumbDir.Y <= -0.851)
                        {
                            inputType = _InputType.Backward;
                        }
                        else if (thumbDir.Y >= 0.851)
                        {
                            inputType = _InputType.Forward;
                        }
                        else
                        {
                            if (thumbDir.X < 0)
                            {
                                inputType = _InputType.Left;
                            }
                            else
                            {
                                inputType = _InputType.Right;
                            }
                        }
                    }
                    else
                    {
                        // use pad cross key
                        if (src.TestButtonState(InputSystem.PadButtons.Down))
                        {
                            inputType = _InputType.Backward;
                        }
                        else if (src.TestButtonState(InputSystem.PadButtons.Up))
                        {
                            inputType = _InputType.Forward;
                        }
                        else if (src.TestButtonState(InputSystem.PadButtons.Left))
                        {
                            inputType = _InputType.Left;
                        }
                        else if (src.TestButtonState(InputSystem.PadButtons.Right))
                        {
                            inputType = _InputType.Right;
                        }
                    }

#if true
                    switch (inputType)
                    {
                    case _InputType.Forward:
                    {
                        var         nextDir      = MapLocation.GetForwardDirection(m_currentLocation.Direction);
                        MapLocation?nextLocation = mapSys.Walk(m_currentLocation, nextDir);
                        if (nextLocation.HasValue)
                        {
                            m_currentLocation = nextLocation.Value;
                            var pose = mapSys.GetPose(m_currentLocation);
                            m_coroutine.Start(new _MoveToTask(this, pose.TranslationVector));                                                            // no turn
                        }
                    }
                    break;

                    case _InputType.Backward:
                    {
                        var nextDir = MapLocation.GetBackwardDirection(m_currentLocation.Direction);
                        m_currentLocation.Direction = nextDir;
                        var pose = mapSys.GetPose(m_currentLocation);
                        m_coroutine.Start(new _TurnToTask(this, pose.Forward));
                    }
                    break;

                    case _InputType.Left:
                    {
                        var nextDir = MapLocation.GetLeftDirection(m_currentLocation.Direction);
                        m_currentLocation.Direction = nextDir;
                        var pose = mapSys.GetPose(m_currentLocation);
                        m_coroutine.Start(new _TurnToTask(this, pose.Forward));
                    }
                    break;

                    case _InputType.Right:
                    {
                        var nextDir = MapLocation.GetRightDirection(m_currentLocation.Direction);
                        m_currentLocation.Direction = nextDir;
                        var pose = mapSys.GetPose(m_currentLocation);
                        m_coroutine.Start(new _TurnToTask(this, pose.Forward));
                    }
                    break;
                    }
#else
                    switch (inputType)
                    {
                    case _InputType.Forward:
                    {
                        var         nextDir      = MapLocation.GetForwardDirection(m_currentLocation.Direction);
                        MapLocation?nextLocation = mapSys.Walk(m_currentLocation, nextDir);
                        if (nextLocation.HasValue)
                        {
                            m_currentLocation = nextLocation.Value;
                            var pose = mapSys.GetPose(m_currentLocation);
                            m_coroutine.Start(new _MoveToTask(this, pose.TranslationVector));                                                            // no turn
                        }
                    }
                    break;

                    case _InputType.Backward:
                    {
                        var         nextDir      = MapLocation.GetBackwardDirection(m_currentLocation.Direction);
                        MapLocation?nextLocation = mapSys.Walk(m_currentLocation, nextDir);
                        if (nextLocation.HasValue)
                        {
                            m_currentLocation = nextLocation.Value;
                            var pose = mapSys.GetPose(m_currentLocation);
                            m_coroutine.Start(Coroutine.Join(new _TurnToTask(this, pose.Forward), new _MoveToTask(this, pose.TranslationVector)));
                        }
                    }
                    break;

                    case _InputType.Left:
                    {
                        var         nextDir      = MapLocation.GetLeftDirection(m_currentLocation.Direction);
                        MapLocation?nextLocation = mapSys.Walk(m_currentLocation, nextDir);
                        if (nextLocation.HasValue)
                        {
                            m_currentLocation = nextLocation.Value;
                            var pose = mapSys.GetPose(m_currentLocation);
                            m_coroutine.Start(Coroutine.Join(new _TurnToTask(this, pose.Forward), new _MoveToTask(this, pose.TranslationVector)));
                        }
                    }
                    break;

                    case _InputType.Right:
                    {
                        var         nextDir      = MapLocation.GetRightDirection(m_currentLocation.Direction);
                        MapLocation?nextLocation = mapSys.Walk(m_currentLocation, nextDir);
                        if (nextLocation.HasValue)
                        {
                            m_currentLocation = nextLocation.Value;
                            var pose = mapSys.GetPose(m_currentLocation);
                            m_coroutine.Start(Coroutine.Join(new _TurnToTask(this, pose.Forward), new _MoveToTask(this, pose.TranslationVector)));
                        }
                    }
                    break;
                    }
#endif
                }
            }
            break;

            default:
                Debug.Fail("unsupported input device type : " + gameSys.Config.InputDevice);
                break;
            }
        }
Exemplo n.º 14
0
        public void RenderFrame()
        {
            double dt = m_fps.GetDeltaTime();

            var drawSys    = DrawSystem.GetInstance();
            var cameraSys  = CameraSystem.GetInstance();
            var inputSys   = InputSystem.GetInstance();
            var entitySys  = EntitySystem.GetInstance();
            var mapSys     = MapSystem.GetInstance();
            var cullingSys = CullingSystem.GetInstance();

            // update fps
            {
                double avgDT = m_fps.GetAverageDeltaTime();
                string text  = String.Format("FPS:{0:f2}, DeltaTime:{1:f2}ms", 1.0 / avgDT, avgDT * 1000.0f);
                m_numberEntity.SetNumber(1.0f / (float)avgDT);
            }

            if (m_multiThreadCount > 1)
            {
                Task.WaitAll(m_taskList.ToArray());
                var tmpTaskResult = new List <CommandList>(m_taskResultList);

                inputSys.Update(dt);
                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Input, dt);
                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Behavior, dt);
                cameraSys.Update(dt);
                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.PostBehavior, dt);

                DrawSystem.WorldData worldData;
                worldData.AmbientColor     = new Color3(0.4f, 0.45f, 0.55f);
                worldData.FogColor         = new Color3(0.3f, 0.5f, 0.8f);
                worldData.NearClip         = 0.01f;
                worldData.FarClip          = 100.0f;
                worldData.DirectionalLight = new DrawSystem.DirectionalLightData()
                {
                    Direction = new Vector3(0.3f, -0.2f, 0.4f),
                    Color     = new Color3(0.6f, 0.6f, 0.5f),
                };
                worldData.Camera = cameraSys.GetCameraData();

                drawSys.BeginScene(worldData);
                var context = drawSys.GetDrawContext();


                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Posing, dt);
                mapSys.Update(dt, context);
                cullingSys.UpdateFrustum();
                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.PreDraw, dt);
                drawSys.GetDrawBuffer().Process(drawSys.GetDrawContext());
                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Draw, dt);

                // start command list generation for the next frame
                m_taskList.Clear();
                m_taskResultList.Clear();
                m_taskResultList.AddRange(Enumerable.Repeat <CommandList>(null, m_multiThreadCount));
                m_accTime += dt;
                for (int threadIndex = 0; threadIndex < m_multiThreadCount; ++threadIndex)
                {
                    int resultIndex = threadIndex;

                    var subThreadContext = drawSys.GetSubThreadContext(threadIndex);
                    m_taskList.Add(Task.Run(() =>
                    {
                        // todo : do sub-thread task
                        m_taskResultList[resultIndex] = subThreadContext.FinishCommandList();
                    }));
                }

                foreach (var result in tmpTaskResult)
                {
                    context.ExecuteCommandList(result);
                }

                m_numberEntity.SetPose(ChrSystem.GetInstance().Player.FindComponent <LayoutComponent>().Transform);
                m_numberEntity.Draw(context);
                drawSys.EndScene();
            }
            else
            {
                // not supported
            }

            m_fps.EndFrame();
            m_fps.BeginFrame();
        }
Exemplo n.º 15
0
        public Scene(Device device, SwapChain swapChain, Panel renderTarget, HmdDevice hmd, bool bStereoRendering, int multiThreadCount)
        {
            var drawSys = DrawSystem.GetInstance();
            var mapSys  = MapSystem.GetInstance();

            // load textures
            var textures = new List <TextureView>(new[]
            {
                TextureView.FromFile("dot", drawSys.D3D, "Image/dot.png"),
                TextureView.FromFile("floor", drawSys.D3D, "Image/floor.jpg"),
            });
            var numTextures = new DrawSystem.TextureData[10];

            for (int i = 0; i < 10; ++i)
            {
                var name = String.Format("number_{0}", i);
                numTextures[i] = new DrawSystem.TextureData
                {
                    Resource = TextureView.FromFile(name, drawSys.D3D, String.Format("Image/{0}.png", name)),
                    UvScale  = Vector2.One,
                };
            }
            textures.AddRange(numTextures.Select(item => item.Resource));
            foreach (var tex in textures)
            {
                drawSys.ResourceRepository.AddResource(tex);
            }

            // create number entity
            m_fps          = new FpsCounter();
            m_numberEntity = new NumberEntity(new NumberEntity.InitParam()
            {
                Dot = new DrawSystem.TextureData
                {
                    Resource = drawSys.ResourceRepository.FindResource <TextureView>("dot"),
                    UvScale  = Vector2.One,
                },
                Numbers = numTextures,
                Layout  = Matrix.RotationYawPitchRoll(1.0f, -1.5f, MathUtil.PI) * Matrix.Translation(-1.5f, 2.5f, -4.5f)
            });

            // create map
            mapSys.LoadResource();
            mapSys.CreateMap("Level/l9000.tmx");

            // create player
            m_player = new PlayerEntity();
            ChrSystem.GetInstance().Player = m_player;

            m_multiThreadCount = multiThreadCount;
            m_taskList         = new List <Task>(m_multiThreadCount);
            m_taskResultList   = new List <CommandList>(m_multiThreadCount);

            // other settings
#if DEBUG
            CameraSystem.GetInstance().ActivateCamera(CameraSystem.FollowEntityCameraName);
            //CameraSystem.GetInstance().ActivateCamera(CameraSystem.FixedCameraName);
            //CameraSystem.GetInstance().ActivateCamera(CameraSystem.FreeCameraName);
#else
            CameraSystem.GetInstance().ActivateCamera(CameraSystem.FollowEntityCameraName);
#endif // DEBUG
        }