Пример #1
0
        /// <summary>
        /// Init Each Element of Collection According to StiLib Configuration, Custom Vision Stimulus Type and Parameters Object
        /// </summary>
        /// <param name="count"></param>
        /// <param name="vstype"></param>
        /// <param name="gd"></param>
        /// <param name="slconfig"></param>
        /// <param name="service"></param>
        /// <param name="path"></param>
        /// <param name="vspara"></param>
        public virtual void Init(int count, VSType vstype, GraphicsDevice gd, AssemblySettings slconfig, IServiceProvider service, string path, object vspara)
        {
            vsType temp = null;

            try
            {
                switch (vstype)
                {
                case VSType.Bar:
                    temp = new Bar(gd, slconfig, (BarPara)vspara) as vsType;
                    break;

                case VSType.Grating:
                    temp = new Grating(gd, slconfig, service, path, (GratingPara)vspara) as vsType;
                    break;

                case VSType.Image:
                    temp = new Image(gd, slconfig, service, path, (ImagePara)vspara) as vsType;
                    break;

                case VSType.Primitive:
                    temp = new Primitive(gd, slconfig, (PrimitivePara)vspara) as vsType;
                    break;

                case VSType.SLModel:
                    temp = new SLModel(gd, slconfig, service, path, (ModelPara)vspara) as vsType;
                    break;

                case VSType.SLQuad:
                    temp = new SLQuad(gd, slconfig, service, path, (Quad)vspara) as vsType;
                    break;

                case VSType.SLVideo:
                    temp = new SLVideo(gd, slconfig, service, path, (VideoPara)vspara) as vsType;
                    break;

                case VSType.Text:
                    temp = new Text(gd, slconfig, service, path, (TextPara)vspara) as vsType;
                    break;
                }

                Init(count, gd, temp);
            }
            catch (Exception e)
            {
                SLConstant.ShowException(e);
            }
        }
Пример #2
0
        /// <summary>
        /// Optimized Batch Draw
        /// </summary>
        /// <param name="gd"></param>
        public virtual void BatchDraw(GraphicsDevice gd)
        {
            switch (Collection[0].VSType)
            {
            case VSType.Bar:
            case VSType.Primitive:
                PrimitiveType ptype  = Collection[0].BasePara.primitivetype;
                int           vexn   = Collection[0].VertexArray.Length;
                int           pcount = VisionStimulus.CheckPrimitiveCount(ptype, Collection[0].IndexArray.Length, Collection[0].IndexArray.Length);

                gd.VertexDeclaration = Collection[0].vertexDeclaration;
                gd.Vertices[0].SetSource(Collection[0].vertexBuffer, 0, VertexPositionColor.SizeInBytes);
                gd.Indices = Collection[0].indexBuffer;
                gd.RenderState.CullMode = CullMode.None;

                // Begin Draw
                Collection[0].basicEffect.Begin();
                Collection[0].basicEffect.CurrentTechnique.Passes[0].Begin();
                for (int i = 0; i < Collection.Count; i++)
                {
                    if (Collection[i].Visible)
                    {
                        Collection[0].basicEffect.World = Collection[i].Ori3DMatrix * Collection[i].WorldMatrix;
                        Collection[0].basicEffect.CommitChanges();
                        gd.DrawIndexedPrimitives(ptype, 0, 0, vexn, 0, pcount);
                    }
                }
                Collection[0].basicEffect.CurrentTechnique.Passes[0].End();
                Collection[0].basicEffect.End();
                break;

            case VSType.Grating:
                ptype  = Collection[0].BasePara.primitivetype;
                vexn   = Collection[0].VertexArray.Length;
                pcount = VisionStimulus.CheckPrimitiveCount(ptype, Collection[0].IndexArray.Length, Collection[0].IndexArray.Length);

                gd.VertexDeclaration = Collection[0].vertexDeclaration;
                gd.Vertices[0].SetSource(Collection[0].vertexBuffer, 0, VertexPositionColor.SizeInBytes);
                gd.Indices = Collection[0].indexBuffer;
                gd.RenderState.CullMode = CullMode.None;

                Grating grating = Collection[0] as Grating;
                // Begin Draw
                grating.gratingeffect.Begin();
                grating.gratingeffect.CurrentTechnique.Passes[0].Begin();
                for (int i = 0; i < Collection.Count; i++)
                {
                    if (Collection[i].Visible)
                    {
                        grating.eWorld.SetValue(Collection[i].Ori3DMatrix * Collection[i].WorldMatrix);
                        grating.gratingeffect.CommitChanges();
                        gd.DrawIndexedPrimitives(ptype, 0, 0, vexn, 0, pcount);
                    }
                }
                grating.gratingeffect.CurrentTechnique.Passes[0].End();
                grating.gratingeffect.End();
                break;

            case VSType.Image:
                Vector3 Center;
                float   unitFactor = Collection[0].unitFactor;
                Image   image      = Collection[0] as Image;
                // Begin Draw
                image.spriteBatch.Begin();
                for (int i = 0; i < Collection.Count; i++)
                {
                    if (Collection[i].Visible)
                    {
                        Center = Collection[i].Center;
                        image.spriteBatch.Draw(image.texture, new Vector2(Center.X * unitFactor + gd.Viewport.Width / 2 - image.texture.Width / 2, gd.Viewport.Height / 2 - Center.Y * unitFactor - image.texture.Height / 2), Collection[i].BasePara.color);
                    }
                }
                image.spriteBatch.End();
                break;

            case VSType.SLModel:
                SLModel model = Collection[0] as SLModel;
                foreach (ModelMesh mesh in model.Model.Meshes)
                {
                    for (int i = 0; i < Collection.Count; i++)
                    {
                        if (Collection[i].Visible)
                        {
                            foreach (BasicEffect effect in mesh.Effects)
                            {
                                effect.EnableDefaultLighting();
                                effect.GraphicsDevice.RenderState.DepthBufferEnable = true;

                                effect.World      = Collection[i].Ori3DMatrix * model.BoneTransforms[mesh.ParentBone.Index] * Collection[i].WorldMatrix;
                                effect.View       = model.ViewMatrix;
                                effect.Projection = model.ProjectionMatrix;
                            }
                            mesh.Draw();
                        }
                    }
                }
                break;

            case VSType.SLQuad:
                SLQuad quad = Collection[0] as SLQuad;
                ptype  = Collection[0].BasePara.primitivetype;
                vexn   = quad.Para.vertices.Length;
                pcount = VisionStimulus.CheckPrimitiveCount(ptype, Collection[0].IndexArray.Length, Collection[0].IndexArray.Length);

                gd.VertexDeclaration = quad.vertexDeclaration;
                gd.Vertices[0].SetSource(quad.vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
                gd.Indices = quad.indexBuffer;
                gd.RenderState.CullMode         = CullMode.None;
                gd.RenderState.AlphaBlendEnable = true;
                gd.RenderState.AlphaTestEnable  = true;
                gd.RenderState.AlphaFunction    = CompareFunction.Greater;

                // Begin Draw
                quad.basicEffect.Begin();
                quad.basicEffect.CurrentTechnique.Passes[0].Begin();
                for (int i = 0; i < Collection.Count; i++)
                {
                    if (Collection[i].Visible)
                    {
                        quad.basicEffect.World = Collection[i].Ori3DMatrix * Collection[i].WorldMatrix;
                        quad.basicEffect.CommitChanges();
                        gd.DrawIndexedPrimitives(ptype, 0, 0, vexn, 0, pcount);
                    }
                }
                quad.basicEffect.CurrentTechnique.Passes[0].End();
                quad.basicEffect.End();
                break;

            case VSType.SLVideo:
                SLVideo video = Collection[0] as SLVideo;
                unitFactor = Collection[0].unitFactor;
                // Begin Draw
                video.spriteBatch.Begin();
                for (int i = 0; i < Collection.Count; i++)
                {
                    if (Collection[i].Visible)
                    {
                        Center = Collection[i].Center;
                        if (video.MediaState == MediaState.Stopped)
                        {
                            video.Play();
                        }
                        if (video.MediaState != MediaState.Stopped)
                        {
                            video.texture = video.Player.GetTexture();
                        }
                        if (video.texture != null)
                        {
                            video.spriteBatch.Draw(video.texture, new Vector2(Center.X * unitFactor + gd.Viewport.Width / 2 - video.texture.Width / 2, gd.Viewport.Height / 2 - Center.Y * unitFactor - video.texture.Height / 2), Collection[i].BasePara.color);
                        }
                    }
                }
                video.spriteBatch.End();
                break;

            case VSType.Text:
                Text    text = Collection[0] as Text;
                Vector2 size = text.spriteFont.MeasureString(SLConstant.Help);
                unitFactor = Collection[0].unitFactor;
                // Begin Draw
                text.spriteBatch.Begin();
                for (int i = 0; i < Collection.Count; i++)
                {
                    if (Collection[i].Visible)
                    {
                        Center = Collection[i].Center;
                        text.spriteBatch.DrawString(text.spriteFont, SLConstant.Help, new Vector2(Center.X * unitFactor + gd.Viewport.Width / 2 - size.X / 2, gd.Viewport.Height / 2 - Center.Y * unitFactor - size.Y / 2), Collection[i].BasePara.color);
                    }
                }
                text.spriteBatch.End();
                break;
            }
        }