/// <summary>
        /// Constructor for LightShader, which renders both directional lights
        /// and point lights.
        /// </summary>
        /// <param name="profile"></param>
        /// <param name="content"></param>

        public LightShader(RenderProfile profile, ContentManager content)
            : base(profile, content)
        {
            // Lighting render target
            lightRT = profile.AddRenderTarget(
                (int)(backBufferWidth * bufferScaling),
                (int)(backBufferHeight * bufferScaling),
                SurfaceFormat.HdrBlendable, DepthFormat.None);

            halfPixel.X = 0.5f / (float)(backBufferWidth * bufferScaling);
            halfPixel.Y = 0.5f / (float)(backBufferHeight * bufferScaling);

            outputTargets = new RenderTarget2D[] { lightRT };

            // Configure camera for directional light
            lightCamera = new Camera();
            lightCamera.farPlaneDistance = 1000f;
            lightCamera.Initialize(shadowMapSize, shadowMapSize);

            lightViewProj = new Matrix[numCascades];

            // Load the shader effects
            directionalLightEffect = content.Load <Effect>("directionalLight");
            pointLightEffect       = content.Load <Effect>("pointLight");

            // Set constant parameters
            directionalLightEffect.Parameters["halfPixel"].SetValue(halfPixel);
            pointLightEffect.Parameters["halfPixel"].SetValue(halfPixel);

            // Load the point light model
            sphereModel = content.Load <XnaModel>("sphere");
        }
        public SwapBaseTextureComponent(XnaModel model, params Texture[] textures)
        {
            xnaModel = model;
            if (textures == null)
            {
                throw new ArgumentNullException("you must pass at least 2 textures");
            }

            _textures = textures.ToList();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Attempt to find the location of the model to be loaded.
        /// </summary>
        private XnaModel FindModel(String directory, String modelPath)
        {
            XnaModel model = null;
            String   path  = "Models\\" + modelPath;

            if (Directory.Exists(content.RootDirectory + "\\Models\\" + directory))
            {
                path = "Models\\" + directory + "\\" + modelPath;
            }

            model = content.Load <XnaModel>(path);
            return(model);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create a Model and set up mesh data.
        /// </summary>

        public Model(XnaModel model)
        {
            // Set up model lists
            textures          = new List <Texture2D>();
            normalMapTextures = new List <Texture2D>();
            materials         = new List <Material>();

            modelMeshes        = new List <ModelMesh>();
            meshInstanceGroups = new Dictionary <string, MeshInstanceGroup>();

            // Bounding box data
            boxVertices = new VertexPositionColor[BoundingBox.CornerCount];

            // Set up model mesh and texture data
            SetModelData(model);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Add model from a specified directory and filename, setting
        /// the same key name as the file for the model
        /// </summary>

        private Model AddModel(String directory, String modelPath, Model model = null)
        {
            if (model != null)
            {
                XnaModel sourceModel = FindModel(directory, modelPath);
                sceneModels.Add(modelPath, model);
                sceneModels[modelPath].SetModelData(sourceModel);
            }
            else
            {
                sceneModels.Add(modelPath, new Model(FindModel(directory, modelPath)));
            }

            // Add all the meshes to the resource pool
            //foreach (ModelMesh mesh in sceneModels[modelPath].modelMeshes)
            //	sceneMeshes.Add(modelPath + "." + mesh.Name, mesh);

            return(sceneModels[modelPath]);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Set up model mesh and texture data.
        /// </summary>
        /// <param name="model"></param>

        public void SetModelData(XnaModel model)
        {
            // Set world transformation matrix
            scale          = (scale == 0) ? 1f : scale;
            worldTransform = Matrix.CreateScale(scale) *
                             Matrix.CreateFromYawPitchRoll(rotation.Y, rotation.X, rotation.Z) *
                             Matrix.CreateTranslation(translation);

            // Add model matrices
            boneMatrices = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(boneMatrices);
            int index = 0;

            // Animation data
            modelTag        = model.Tag;
            animationPlayer = null;

            // Extract textures and create bounding boxes
            foreach (ModelMesh mesh in model.Meshes)
            {
                string meshName = mesh.Name;

                if (mesh.Name == null || mesh.Name == "(null)")
                {
                    meshName = "DefaultName_" + index++;
                }

                // Add to modelMesh list
                modelMeshes.Add(mesh);

                // Add mesh Instance Group
                meshInstanceGroups.Add(meshName, new MeshInstanceGroup());

                // Build bounding volumes
                Matrix         meshTransform  = boneMatrices[mesh.ParentBone.Index];
                BoundingSphere boundingSphere = new BoundingSphere();
                meshInstanceGroups[meshName].boundingBox = BuildBoundingBox(mesh, ref boundingSphere);

                // Add instance data
                meshInstanceGroups[meshName].instances.Add(new MeshInstance(boundingSphere));
                meshInstanceGroups[meshName].visibleInstances.Add(new MeshInstance(boundingSphere));
                meshInstanceGroups[meshName].tempTransforms[0] = new Matrix();
                lastInstance = 0;

                // Extract textures from each mesh
                foreach (ModelMeshPart meshPart in mesh.MeshParts)
                {
                    Material meshMaterial = new Material();

                    Texture2D texture   = meshPart.Effect.Parameters["Texture"].GetValueTexture2D();
                    Texture2D normalMap = meshPart.Effect.Parameters["NormalMap"].GetValueTexture2D();

                    if (texture != null)
                    {
                        meshMaterial.textures.Add("Texture", texture);
                    }
                    meshMaterial.textures.Add("NormalMap", normalMap);

                    materials.Add(meshMaterial);
                }
            }
        }