コード例 #1
0
        /// <summary>
        /// Load the DXS model. If the model is a single mesh, Mesh will
        /// be returned. Otherwise Node will be returned.
        /// </summary>
        /// <param name="resource">Dxs resource</param>
        /// <param name="parameters">Model loader parameters, must be of type ModelLoaderParameters otherwise it's ignored.</param>
        /// <returns>The model</returns>
        public override Spatial Load(IResource resource, LoaderParameters parameters)
        {
            _parser.Reset();

            ModelLoaderParameters modelParams = parameters as ModelLoaderParameters;

            if (modelParams == null)
            {
                modelParams = _defaultParams;
            }

            DxsNode dxsModel;

            using (Stream stream = resource.OpenStream()) {
                dxsModel = _parser.Parse(stream, resource.Name);
            }

            Node root = new Node(dxsModel.Name);

            foreach (DxsNode node in dxsModel.Children)
            {
                ParseDxsTree(node, root, modelParams);
            }

            if (root.Count == 1 && root[0] is Mesh)
            {
                Spatial s = root[0];
                s.RemoveFromParent();
                return(s);
            }

            return(root);
        }
コード例 #2
0
 //Per-model import cleanup
 private void Cleanup()
 {
     m_materials    = null;
     m_meshData     = null;
     m_meshMatInfo  = null;
     m_loaderParams = null;
     m_importer.DetachLogStreams();
     m_importer.RemoveConfig(NormalSmoothingAngleConfig.NormalSmoothingAngleConfigName);
 }
コード例 #3
0
ファイル: AssimpLoaders.cs プロジェクト: chuz/tesla-engine
        /// <summary>
        /// Loads the specified resource.
        /// </summary>
        /// <param name="resource">The resource to load</param>
        /// <param name="parameters">Loader parameters</param>
        /// <returns>The loaded resource</returns>
        public override Spatial Load(IResource resource, LoaderParameters parameters)
        {
            ModelLoaderParameters modelParams = parameters as ModelLoaderParameters;

            if (modelParams == null)
            {
                modelParams = new ModelLoaderParameters();
            }

            return(m_translator.Load(resource, base.Extension, modelParams));
        }
コード例 #4
0
        protected override void LoadContent()
        {
            ModelLoaderParameters mlp = new ModelLoaderParameters();

            mlp.SwapWindingOrder = true;
            mlp.NormalGeneration = NormalGeneration.Face;

            mlp.PreferLitMaterials   = true;
            mlp.GenerateTangentBasis = false;
            ship = ContentManager.Load <Spatial>("Models\\station.dxs", mlp);

            ship.AddController(new Tesla.Scene.Extension.RotateController(new Vector3(0, 1, 0), 5));
            ship.SetRenderState(SamplerState.AnisotropicWrap);
            engineMat = ContentManager.Load <Material>("BasicTexture.tem");

            Material mat = new Material();

            mat.LoadEffect(ContentManager.Load <Effect>("Shaders//TextureGlowEffect.fx"));
            mat.SetActiveTechnique("TextureGlow");
            mat.SetParameter("DiffuseMap", ContentManager.Load <Texture2D>("Textures//station_diff.dds"));
            mat.SetParameter("EmissiveMap", ContentManager.Load <Texture2D>("Textures//station_glow.dds"));
            mat.SetParameter("MatEmissive", new Vector3(.5f, .5f, .5f));
            mat.SetEngineParameter("WVP", Tesla.Core.EngineValue.WorldViewProjection);
            mat.SetEngineParameter("World", Tesla.Core.EngineValue.WorldMatrix);
            mat.SetEngineParameter("WorldIT", Tesla.Core.EngineValue.WorldInverseTranspose);
            mat.SetEngineParameter("EyePos", Tesla.Core.EngineValue.CameraPosition);

            ship.SetMaterial(mat);

            engineMat.SetParameter("DiffuseMap", ContentManager.Load <Texture2D>("Textures//station_glow.dds"));

            width  = Renderer.CurrentCamera.Viewport.Width;
            height = Renderer.CurrentCamera.Viewport.Height;

            bool test = Renderer.Adapter.QueryRenderTargetFormat(SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 4);

            sceneRT    = new RenderTarget2D(width, height, false, Window.SwapChain.PresentationParameters.BackBufferFormat, Window.SwapChain.PresentationParameters.DepthStencilFormat, Window.SwapChain.PresentationParameters.MultiSampleCount, RenderTargetUsage.DiscardContents);
            rt1        = new RenderTarget2D(width, height, true, sceneRT.Format, DepthFormat.None);
            rt2        = new RenderTarget2D(width, height, true, sceneRT.Format, DepthFormat.None);
            rt3        = new RenderTarget2D(width, height, true, sceneRT.Format, DepthFormat.None);
            blurEffect = ContentManager.Load <Effect>("Shaders//GaussianBlur.fx");
            batch      = new SpriteBatch();

            RootNode.AddChild(ship);
            sky = new Box("Space", Vector3.Zero, 100, 100, 100);
            sky.SceneHints.RenderBucketType = RenderBucketType.Skip;
            sky.Material = ContentManager.Load <Material>("Materials//SpaceSkybox.tem");

            RootNode.SetModelBound(new Tesla.Bounding.BoundingBox());
        }
コード例 #5
0
ファイル: RayPicking.cs プロジェクト: chuz/tesla-engine
        protected override void LoadContent()
        {
            Window.Title = "Ray Picking Sample";
            //Load a model that we'll use to pick
            ModelLoaderParameters mlp = new ModelLoaderParameters();

            mlp.SwapWindingOrder   = true;
            mlp.NormalGeneration   = NormalGeneration.Crease;
            mlp.PreferLitMaterials = true;

            Spatial jeep = ContentManager.Load <Spatial>("Models//Jeep.dxs", mlp);

            //jeep.Translation = new Vector3(0, -50, 0);
            jeep.Rotation = Quaternion.FromAngleAxis(MathHelper.ToRadians(140), Vector3.UnitY);
            jeep.SetModelBound(new BoundingBox());
            RootNode.AddChild(jeep);

            //Adjust the default camera a tad
            Renderer.CurrentCamera.Position = new Vector3(0, 100, 100);
            Renderer.CurrentCamera.LookAt(jeep.Translation, Vector3.Up);
            Renderer.CurrentCamera.Update();

            //Set the mouse visible so we can see the cursor
            Window.IsMouseVisible = true;

            //Create our query
            query = new PickQuery();
            //We want to enable primitive picking to get triangle accurate results, when we do the intersection tests,
            //the utility will first do a board bounding volume test, and if that succeeds then a finer primitive test.
            query.EnablePrimitivePicking = true;

            //Setup out input - whenever we left click, we'll cast a ray into the scene
            InputLayer.RegisterTrigger(new InputTrigger(new InputCondition(delegate(GameTime time) {
                MouseState ms = Mouse.GetMouseState();
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    return(true);
                }
                return(false);
            }), new InputAction(delegate(GameTime time) {
                MouseState ms = Mouse.GetMouseState();
                query.Clear();

                //The camera class has easy-built in functionality to quickly create a pick ray where the origin
                //of the ray is on the near-plane of the view frustum
                PickingUtil.FindPick(RootNode, Renderer.CurrentCamera.CreatePickRay(ms.Position), query);
                CreatePicks(query);
            })));
        }
コード例 #6
0
 private void ParseDxsTree(DxsNode dxsNode, Node parent, ModelLoaderParameters modelParams)
 {
     if (dxsNode is DxsPrimitive)
     {
         ParseDxsPrimitive(dxsNode as DxsPrimitive, parent, modelParams);
     }
     else
     {
         Node child = new Node(dxsNode.Name);
         parent.AddChild(child);
         foreach (DxsNode node in dxsNode.Children)
         {
             ParseDxsTree(node, child, modelParams);
         }
     }
 }
コード例 #7
0
ファイル: NormalMapSample.cs プロジェクト: chuz/tesla-engine
        protected override void LoadContent()
        {
            ClearColor = Color.Black;
            MemoryStream   ms       = new MemoryStream();
            BinaryExporter exporter = new BinaryExporter();
            BinaryLoader   loader   = new BinaryLoader();


            Window.Title = "Normal Mapping Sample";
            ModelLoaderParameters mlp = new ModelLoaderParameters();

            mlp.SwapWindingOrder     = true;
            mlp.PreferLitMaterials   = false;
            mlp.GenerateTangentBasis = true;
            mlp.NormalGeneration     = NormalGeneration.Crease;


            Spatial model = ContentManager.Load <Spatial>("Models//starship.dxs", mlp);

            model.SetScale(.15f);
            //model.Material = ContentManager.Load<Material>("NormalMap.tem");
            // model.Material.SetParameter("DiffuseMap", ContentManager.Load<Texture2D>("Textures//rock_diff.dds"));
            //  model.Material.SetParameter("NormalMap", ContentManager.Load<Texture2D>("Textures//rock_norm.dds"));
            // model.Material.SetParameter("MatSpecular", Color.Gray.ToVector3());
            //   model.AddController(new RotateController(new Vector3(.5f, .5f, 0), 25f));
            //model.ComputeTangentBasis();
            // RootNode.AddChild(model);
            RootNode.SetModelBound(new Tesla.Bounding.BoundingBox());

            model = ContentManager.Load <Spatial>("Models//statue.tebo");
            Material mat = ContentManager.Load <Material>("NormalMap.tem").Clone();

            mat.SetParameter("DiffuseMap", ContentManager.Load <Texture2D>("Textures//statue_diff.dds"));
            mat.SetParameter("NormalMap", ContentManager.Load <Texture2D>("Textures//statue_norm.dds"));
            mat.SetParameter("MatSpecular", new Vector3(.3f, .3f, .3f));
            model.SetMaterial(mat);
            model.Rotation = Quaternion.FromAngleAxis(MathHelper.ToRadians(-90), Vector3.Up);
            model.SetModelBound(new Tesla.Bounding.BoundingBox());
            RootNode.AddChild(model);

            /*
             * model = ContentManager.Load<Spatial>("Models//statue.dxs", mlp);
             * model.Rotation = Quaternion.FromAngleAxis(MathHelper.ToRadians(-90), Vector3.Up);
             * FileStream fs = File.OpenWrite("C:\\Users\\Nicholas Woodfield\\Desktop\\D3D10\\statue2.tebo");
             * exporter.Save(model, fs);
             * fs.Close();*/
        }
コード例 #8
0
ファイル: DxsModelLoading.cs プロジェクト: chuz/tesla-engine
        protected override void LoadContent()
        {
            Window.Title = "DXS Model Loading Sample";
            Renderer.CurrentCamera.Position = new Vector3(-70, 90, 120);
            Renderer.CurrentCamera.LookAt(new Vector3(0, 0, 0), Vector3.Up);
            Renderer.CurrentCamera.Update();

            //When calling ContentManager.Load<T>(), an optional loader parameter can be passed that tells the content importer
            //how to process the content. Model loaders take ModelLoaderParameters and image loaders take ImageLoaderParameters. They
            //can be re-used.
            //
            //In this case, we have to tell the model loader that we want to swap the winding order of polygons so we do not get
            //incorrect rendering. Other options include the angle at which to rotate the model about the XYZ axes, how normals should
            //be generated (if any), if a tangent basis should be generated, where to find textures, or overload material creation by
            //supplying a material to load and use as a template.
            ModelLoaderParameters mlp = new ModelLoaderParameters();

            mlp.SwapWindingOrder   = true;
            mlp.PreferLitMaterials = false;

            //In BasicApp, we registered a DXSModelLoader to the content manager, if we didn't this will throw
            //an exception that we do not have a means to handle the requested content. Loaders are designated by
            //both the runtime type and the content file extension.
            //
            //Notice with loading this model, we use Spatial rather than Mesh, because a model may be in fact a
            //scene graph with multiple parts. We can either have a Node or Mesh returned. In this case,
            //we should have a mesh returned since the model is a single mesh in the file. If the model
            //used two different materials, it would have to be split up into two different meshes
            //and then attached to a Node that would be returned, for example.
            Spatial model = ContentManager.Load <Spatial>("Models//station.dxs", mlp);

            model.AddController(new RotateController(Vector3.Up, 5));
            model.SetModelBound(new BoundingBox());
            RootNode.AddChild(model);

            Box sky = new Box("Space", Vector3.Zero, 100, 100, 100);

            sky.SceneHints.RenderBucketType = RenderBucketType.PreBucket;
            sky.Material = ContentManager.Load <Material>("Materials//SpaceSkybox.tem");

            RootNode.AddChild(sky);
        }
コード例 #9
0
 private Texture LoadTexture(String file, ModelLoaderParameters modelParams)
 {
     if (file == null)
     {
         return(null);
     }
     try {
         if (modelParams.TexturePath != null)
         {
             return(ContentManager.Load <Texture>(Path.Combine(modelParams.TexturePath, file), modelParams.TextureParameters));
         }
         else
         {
             return(ContentManager.Load <Texture>(file, modelParams.TextureParameters));
         }
     } catch (Exception e) {
         Console.WriteLine(String.Format("Could not load {0}, loading default texture\n {1}", file, e.Message));
         return(ContentManager.Load <Texture>("notexture.tebo"));
     }
 }
コード例 #10
0
        public Spatial Load(IResource resource, String format, ModelLoaderParameters loaderParams)
        {
            if (!m_importer.IsFormatSupported(format))
            {
                throw new TeslaException("Format not supported " + resource.Name);
            }

            m_modelResource          = resource;
            m_loaderParams           = loaderParams;
            m_importer.Scale         = loaderParams.Scale;
            m_importer.XAxisRotation = loaderParams.XAxisRotation;
            m_importer.YAxisRotation = loaderParams.YAxisRotation;
            m_importer.ZAxisRotation = loaderParams.ZAxisRotation;

            Assimp.Scene scene = LoadFromAssimp(resource, format);

            if (scene == null)
            {
                throw new TeslaException("Could not load resource: " + resource.Name);
            }

            //TODO: Maybe return a not-found mesh rather than exception? May need to add that functionality to the content manager.

            ParseMeshData(scene);

            Spatial graph = ParseNodes(scene.RootNode);

            if (graph == null)
            {
                throw new TeslaException("Parse exception, no scene graph to load!" + resource.Name);
            }

            if (m_loaderParams.ImportLights)
            {
                ParseLights(scene, graph);
            }

            return(graph);
        }
コード例 #11
0
        protected override void LoadContent()
        {
            ClearColor = Color.Black;
            shadowMap  = new RenderTarget2D(1024, 1024, false, SurfaceFormat.Single, DepthFormat.Depth24Stencil8);
            blurRT     = new RenderTarget2D(1024, 1024, false, SurfaceFormat.Single, DepthFormat.None);
            blurEffect = ContentManager.Load <Effect>("Shaders//GaussianBlur.fx");

            ModelLoaderParameters mlp = new ModelLoaderParameters();

            mlp.NormalGeneration   = NormalGeneration.Smooth;
            mlp.SwapWindingOrder   = true;
            mlp.PreferLitMaterials = true;

            rs      = new RasterizerState();
            rs.Cull = CullMode.Front;
            rs.BindRenderState();

            // mesh = new Sphere("Sphere", 50, 50, 20);
            mesh = ContentManager.Load <Mesh>("Models//statue.tebo");
            mesh.ComputeTangentBasis();
            Material mat = ContentManager.Load <Material>("LitBasicTexture.tem").Clone();

            mat.SetParameter("DiffuseMap", ContentManager.Load <Texture2D>("Textures//statue_diff.dds"));
            mat.SetParameter("NormalMap", ContentManager.Load <Texture2D>("Textures//statue_norm.dds"));
            mat.SetParameter("MatSpecular", new Vector3(.3f, .3f, .3f));
            mesh.SetMaterial(mat);

            Quad q = new Quad("Floor", 800, 800);

            q.Rotation    = Quaternion.FromAngleAxis(MathHelper.ToRadians(-90), Vector3.UnitX);
            q.Translation = new Vector3(0, -70, 0);
            q.Material    = ContentManager.Load <Material>("Materials//LitBasicTextureShadow.tem").Clone();
            q.Material.SetParameter("DiffuseMap", ContentManager.Load <Texture2D>("Textures//statue_diff.dds"));
            receiverMat = q.Material;
            receiverMat.SetParameter("MatSpecular", new Vector3(.3f, .3f, .3f));
            // mesh.SetMaterial(receiverMat);

            InputLayer.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.Enter, false), new InputAction(delegate(GameTime time) {
                receive = !receive;
                if (receive)
                {
                    mesh.SetMaterial(receiverMat);
                }
                else
                {
                    mesh.SetMaterial(mat);
                }
            })));
            InputLayer.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.C, false), new InputAction(delegate(GameTime time) {
                cullFront = !cullFront;
            })));

            DataBuffer <Vector2> texCoords = q.MeshData.TextureCoordinates;

            texCoords.Position = 0;
            texCoords.Set(new Vector2(0, 0));
            texCoords.Set(new Vector2(4, 0));
            texCoords.Set(new Vector2(4, 4));
            texCoords.Set(new Vector2(0, 4));
            q.MeshData.UpdateVertexData <Vector2>(VertexSemantic.TextureCoordinate, texCoords);
            RootNode.AddChild(q);

            sl = new SpotLight(new Vector3(100, 200, 10), new Vector3(-100, -200, -10), 5, 15);
            RootNode.RemoveAllLights();
            RootNode.AddLight(sl);
            RootNode.AddChild(mesh);


            shadowMat = new Material("ShadowMat");
            shadowMat.LoadEffect(ContentManager.Load <Effect>("Shaders//ShadowMapEffect.fx"));
            shadowMat.SetEngineParameter("LightWVP", Tesla.Core.EngineValue.WorldViewProjection);
            lightCam = new Camera();

            batch             = new SpriteBatch();
            dss               = new DepthStencilState();
            dss.DepthFunction = ComparisonFunction.LessEqual;
            dss.BindRenderState();

            float texOffsets = 0.5f + (0.5f / (float)shadowMap.Width);

            texMat = new Matrix(0.5f, 0.0f, 0.0f, 0.0f,
                                0.0f, -0.5f, 0.0f, 0.0f,
                                0.0f, 0.0f, 1.0f, 0.0f,
                                texOffsets, texOffsets, 0.0f, 1.0f);
            rtnode             = new Node("Node");
            rtnode.Translation = sl.Position;
            Node parent = new Node("Parent");

            parent.AddChild(rtnode);
            RootNode.AddChild(parent);
            parent.AddController(new Tesla.Scene.Extension.RotateController(Vector3.UnitY, 25));
        }
コード例 #12
0
 /// <summary>
 /// Creates a new instance of DXSLoader.
 /// </summary>
 public DXSLoader() : base(".dxs")
 {
     _parser        = new DxsParser();
     _defaultParams = new ModelLoaderParameters();
 }
コード例 #13
0
        private void ParseDxsPrimitive(DxsPrimitive dxsPrim, Node parent, ModelLoaderParameters modelParams)
        {
            int k = 0;

            dxsPrim.CompileMeshes(modelParams.NormalGeneration, modelParams.CreaseAngle, modelParams.SwapWindingOrder);

            foreach (DxsMesh dxsMesh in dxsPrim.CompiledMeshes)
            {
                Mesh mesh;
                if (k != 0)
                {
                    mesh = new Mesh(dxsPrim.Name + k);
                }
                else
                {
                    mesh = new Mesh(dxsPrim.Name);
                }

                //We transform the vertices to be centered around the origin, rotation/scale can't be determined from DeleD so
                //only care about translation
                mesh.Translation = dxsMesh.Translation;

                MeshData md = mesh.MeshData;

                DxsMaterial material = dxsPrim.Materials[dxsMesh.MaterialID];

                int vertCount = dxsMesh.Vertices.Count;

                DataBuffer <Vector3> positions      = new DataBuffer <Vector3>(vertCount);
                DataBuffer <Vector2> texCoords      = null;
                DataBuffer <Vector2> lightMapCoords = null;
                DataBuffer <Color>   colors         = null;
                DataBuffer <Vector3> normals        = null;
                if (material.HasTexture)
                {
                    texCoords = new DataBuffer <Vector2>(vertCount);
                }
                if (material.HasLightMap)
                {
                    lightMapCoords = new DataBuffer <Vector2>(vertCount);
                }
                if (dxsPrim.VertexColorEnabled)
                {
                    colors = new DataBuffer <Color>(vertCount);
                }
                if (modelParams.NormalGeneration != NormalGeneration.None)
                {
                    normals = new DataBuffer <Vector3>(vertCount);
                }

                foreach (DxsVertex v in dxsMesh.Vertices)
                {
                    positions.Set(v.Position);
                    if (texCoords != null)
                    {
                        texCoords.Set(v.TextureCoordinate);
                    }
                    if (lightMapCoords != null)
                    {
                        lightMapCoords.Set(v.LightMapCoordinate);
                    }
                    if (colors != null)
                    {
                        colors.Set(v.VertexColor);
                    }
                    if (normals != null)
                    {
                        normals.Set(v.Normal);
                    }
                }

                md.Positions = positions;
                md.Indices   = dxsMesh.Indices;
                if (texCoords != null)
                {
                    md.TextureCoordinates = texCoords;
                }
                if (lightMapCoords != null)
                {
                    md.AddBuffer(VertexSemantic.TextureCoordinate, 1, lightMapCoords);
                }
                if (colors != null)
                {
                    md.Colors = colors;
                }
                if (normals != null)
                {
                    md.Normals = normals;
                }

                //Build the material
                mesh.Material = BuildMaterial(material, mesh, colors != null, normals != null, modelParams);

                md.Reconstruct();

                if (modelParams.GenerateTangentBasis)
                {
                    mesh.ComputeTangentBasis();
                }

                parent.AddChild(mesh);
            }
        }
コード例 #14
0
        private Material BuildMaterial(DxsMaterial material, Mesh mesh, bool hasVertexColors, bool hasNormals, ModelLoaderParameters modelParams)
        {
            if (modelParams.UserMaterialFile != null)
            {
                return(ContentManager.Load <Material>(modelParams.UserMaterialFile).Clone());
            }

            bool hasTexture   = material.HasTexture;
            bool hasLightMap  = material.HasLightMap;
            bool useNormalMap = (hasNormals == true) && (modelParams.GenerateTangentBasis == true) && (modelParams.PreferLitMaterials == true);

            Material mat;

            if (useNormalMap)
            {
                if (hasVertexColors)
                {
                    mat = ContentManager.Load <Material>("NormalMapVertColor.tem").Clone();
                }
                else
                {
                    mat = ContentManager.Load <Material>("NormalMap.tem").Clone();
                }

                int num = 0;
                foreach (DxsLayer layer in material.Layers)
                {
                    if (num == 0)
                    {
                        if (layer.MaterialType == MaterialType.Texture)
                        {
                            mat.SetParameter("DiffuseMap", LoadTexture(layer.TextureFile, modelParams));
                            mat.SetParameter("UseDiffuseMap", true);
                        }
                        else if (layer.MaterialType == MaterialType.Color)
                        {
                            mat.SetParameter("MatDiffuse", layer.Color.ToVector3());
                            mat.SetParameter("UseDiffuseMap", false);
                        }

                        //Set blendstate
                        switch (layer.Blend)
                        {
                        case BlendType.Modulate:
                            mat.SetParameter("Alpha", .5f);
                            mat.SetRenderState(BlendState.AdditiveBlend);
                            mesh.SceneHints.RenderBucketType = RenderBucketType.Transparent;
                            break;

                        case BlendType.Add:
                            mat.SetRenderState(BlendState.AdditiveBlend);
                            mesh.SceneHints.RenderBucketType = RenderBucketType.Transparent;
                            break;

                        case BlendType.Alphablend:
                            mat.SetRenderState(BlendState.AlphaBlendNonPremultiplied);
                            mesh.SceneHints.RenderBucketType = RenderBucketType.Transparent;
                            break;
                        }
                    }
                    else if (num == 1)
                    {
                        if (layer.MaterialType == MaterialType.Texture)
                        {
                            mat.SetParameter("NormalMap", LoadTexture(layer.TextureFile, modelParams));
                        }
                    }
                    else if (num == 2)
                    {
                        if (layer.MaterialType == MaterialType.Texture)
                        {
                            mat.SetParameter("SpecularMap", LoadTexture(layer.TextureFile, modelParams));
                            mat.SetParameter("UseSpecularMap", true);
                        }
                        else
                        {
                            mat.SetParameter("UseSpecularMap", false);
                        }
                        break;
                    }
                    num++;
                }

                return(mat);
            }

            if (modelParams.PreferLitMaterials && hasNormals)
            {
                if (material.HasTexture)
                {
                    if (hasVertexColors)
                    {
                        mat = ContentManager.Load <Material>("LitBasicVertColorTexture.tem").Clone();
                    }
                    else
                    {
                        mat = ContentManager.Load <Material>("LitBasicTexture.tem").Clone();
                    }
                }
                else
                {
                    if (hasVertexColors)
                    {
                        mat = ContentManager.Load <Material>("LitBasicVertColor.tem").Clone();
                    }
                    else
                    {
                        mat = ContentManager.Load <Material>("LitBasicColor.tem").Clone();
                    }
                }

                int num = 0;
                foreach (DxsLayer layer in material.Layers)
                {
                    if (num == 0)
                    {
                        if (layer.MaterialType == MaterialType.Texture)
                        {
                            mat.SetParameter("DiffuseMap", LoadTexture(layer.TextureFile, modelParams));
                        }
                        else if (layer.MaterialType == MaterialType.Color)
                        {
                            mat.SetParameter("MatDiffuse", layer.Color.ToVector3());
                        }

                        //Set blendstate
                        switch (layer.Blend)
                        {
                        case BlendType.Modulate:
                            mat.SetParameter("Alpha", .5f);
                            mat.SetRenderState(BlendState.AdditiveBlend);
                            mesh.SceneHints.RenderBucketType = RenderBucketType.Transparent;
                            break;

                        case BlendType.Add:
                            mat.SetRenderState(BlendState.AdditiveBlend);
                            mesh.SceneHints.RenderBucketType = RenderBucketType.Transparent;
                            break;

                        case BlendType.Alphablend:
                            mat.SetRenderState(BlendState.AlphaBlendNonPremultiplied);
                            mesh.SceneHints.RenderBucketType = RenderBucketType.Transparent;
                            break;
                        }
                    }
                    else if (num == 1)
                    {
                        if (layer.MaterialType == MaterialType.Texture)
                        {
                            mat.SetParameter("DiffuseMap", LoadTexture(layer.TextureFile, modelParams));
                        }
                        break;
                    }
                    num++;
                }
            }
            else
            {
                if (material.HasTexture)
                {
                    if (hasVertexColors)
                    {
                        mat = ContentManager.Load <Material>("BasicVertColorTexture.tem").Clone();
                    }
                    else
                    {
                        mat = ContentManager.Load <Material>("BasicTexture.tem").Clone();
                    }
                }
                else
                {
                    if (hasVertexColors)
                    {
                        mat = ContentManager.Load <Material>("BasicVertColor.tem").Clone();
                    }
                    else
                    {
                        mat = ContentManager.Load <Material>("BasicColor.tem").Clone();
                    }
                }

                int num = 0;
                foreach (DxsLayer layer in material.Layers)
                {
                    if (num == 0)
                    {
                        if (layer.MaterialType == MaterialType.Texture)
                        {
                            mat.SetParameter("DiffuseMap", LoadTexture(layer.TextureFile, modelParams));
                        }
                        else
                        {
                            if (layer.MaterialType == MaterialType.Color)
                            {
                                mat.SetParameter("DiffuseColor", layer.Color.ToVector4());
                            }
                        }

                        //Set blendstate
                        switch (layer.Blend)
                        {
                        case BlendType.Modulate:
                            if (layer.MaterialType == MaterialType.Color)
                            {
                                mat.SetParameter("DiffuseColor", new Color(layer.Color.ToVector3(), .5f).ToVector4());
                            }
                            mat.SetRenderState(BlendState.AdditiveBlend);
                            break;

                        case BlendType.Add:
                            mat.SetRenderState(BlendState.AdditiveBlend);
                            break;

                        case BlendType.Alphablend:
                            mat.SetRenderState(BlendState.AlphaBlendNonPremultiplied);
                            break;
                        }
                    }
                    else if (num == 1)
                    {
                        if (layer.MaterialType == MaterialType.Texture)
                        {
                            mat.SetParameter("DiffuseMap", LoadTexture(layer.TextureFile, modelParams));
                        }
                        break;
                    }
                    num++;
                }
            }

            return(mat);
        }
コード例 #15
0
        protected override void LoadContent()
        {
            Window.Title = "Input Handling Sample";
            rotateSpeed  = 2.5f;
            speed        = 105.0f;

            Renderer.CurrentCamera.Position = new Vector3(0, 800, 100);
            Renderer.CurrentCamera.LookAt(Vector3.Zero, Vector3.Up);
            Renderer.CurrentCamera.Update();

            //Load our fancy pants racecar
            ModelLoaderParameters mlp = new ModelLoaderParameters();

            mlp.SwapWindingOrder   = true;
            mlp.NormalGeneration   = NormalGeneration.Crease;
            mlp.PreferLitMaterials = true;

            Spatial car = ContentManager.Load <Spatial>("Models//Jeep.dxs", mlp);

            car.SetModelBound(new BoundingBox());
            RootNode.AddChild(car);

            //Create an input layer, this layer will hold all of our controls and input response to the arrow keys, moving the car
            input = new InputLayer();

            //Input layers can be disabled or enabled, so you're able to logically group your inputs together and treat them as one unit.
            //E.g. if you have a screen menu and want the mouse enabled, but for gameplay do not want the mouse enabled.
            //Rather than disabling the whole layer, we'll just disable the first 5 triggers - which are the
            //FPS controls set in BasicApp.
            InputLayer.EnableTrigger(0, false);
            InputLayer.EnableTrigger(1, false);
            InputLayer.EnableTrigger(2, false);
            InputLayer.EnableTrigger(3, false);
            InputLayer.EnableTrigger(4, false);

            //Since we disabled the basicApp's input alyer, lets make sure we can get out of the app
            input.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.Escape, false),
                                                   new InputAction(delegate(GameTime time) {
                Exit();
            })));

            input.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.A, true), new InputAction(
                                                       delegate(GameTime time) {
                car.Rotation *= Quaternion.FromAngleAxis(rotateSpeed * (float)time.ElapsedTimeInSeconds, Vector3.Up);
            }
                                                       )));
            input.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.D, true), new InputAction(
                                                       delegate(GameTime time) {
                car.Rotation *= Quaternion.FromAngleAxis(-rotateSpeed * (float)time.ElapsedTimeInSeconds, Vector3.Up);
            }
                                                       )));

            input.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.W, true), new InputAction(
                                                       delegate(GameTime time) {
                //Usually a good idea to use the world properties, since this may be attached to a node that is moving also
                car.Translation -= car.WorldTransform.GetRotationVector(2) * speed * (float)time.ElapsedTimeInSeconds;
            }
                                                       )));
            input.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.S, true), new InputAction(
                                                       delegate(GameTime time) {
                car.Translation += car.WorldTransform.GetRotationVector(2) * speed * (float)time.ElapsedTimeInSeconds;
            }
                                                       )));

            //Add another point light to the scene
            PointLight pl = new PointLight();

            pl.Attenuate = false;
            pl.Position  = new Vector3(0, 0, 50);
            car.AddLight(pl);
        }