예제 #1
0
        /// <summary>
        /// Helper for loading and initializing the particle effect.
        /// </summary>
        private void LoadParticleEffect(ContentManager contentLoader, string techniqueName)
        {
            Effect genericEffect = contentLoader.Load <Effect>("shaders\\Particles");

            // If we have several particle systems, the content manager will return
            // a single shared effect instance to them all. But we want to preconfigure
            // the effect with parameters that are specific to this particular
            // particle system. By cloning the effect, we prevent one particle system
            // from stomping over the parameter settings of another.

            mEffect = genericEffect.Clone();
            mEffect.CurrentTechnique = mEffect.Techniques[techniqueName];
            EffectRegistry.Add(mEffect, RenderOptions.NoStandardParams | RenderOptions.ParticleParams);

            EffectParameterCollection parameters = mEffect.Parameters;

            // Set the values of parameters that do not change.
            parameters["Duration"].SetValue((float)mSettings.Duration.TotalSeconds);
            parameters["DurationRandomness"].SetValue(mSettings.DurationRandomness);
            parameters["Gravity"].SetValue(mSettings.Gravity);
            parameters["EndVelocity"].SetValue(mSettings.EndVelocity);
            parameters["MinColor"].SetValue(mSettings.MinColor.ToVector4());
            parameters["MaxColor"].SetValue(mSettings.MaxColor.ToVector4());
            parameters["RotateSpeed"].SetValue(new Vector2(mSettings.MinRotateSpeed, mSettings.MaxRotateSpeed));
            parameters["StartSize"].SetValue(new Vector2(mSettings.MinStartSize, mSettings.MaxStartSize));
            parameters["EndSize"].SetValue(new Vector2(mSettings.MinEndSize, mSettings.MaxEndSize));
            Texture2D texture = contentLoader.Load <Texture2D>(mSettings.TextureName);

            parameters["Texture"].SetValue(texture);
        }
예제 #2
0
        public override void Initialize(ContentManager contentLoader, ComponentManifest manifest)
        {
            TerrainAsset = contentLoader.Load <Terrain>((string)(manifest.Properties[ManifestKeys.TERRAIN]));
            Heights      = TerrainAsset.GetGeometry();

            EffectRegistry.Add(TerrainAsset.Effect, (RenderOptions)(TerrainAsset.Tag));

            mDefaultMaterial = new EffectApplication(TerrainAsset.Effect, RenderStatePresets.Default);
            mDefaultMaterial.AddParamSetter(new CommonParamSetter());
            mDefaultMaterial.AddParamSetter(new ShadowParamSetter());
            mDefaultMaterial.AddParamSetter(new HDRLightParamSetter());
            mDefaultMaterial.AddParamSetter(new FogParamSetter());
            mDepthOnlyMaterial = new EffectApplication(EffectRegistry.DepthOnlyFx, RenderStatePresets.Default);
            mDepthOnlyMaterial.AddParamSetter(new WorldViewProjParamSetter());

            base.Initialize(contentLoader, manifest);
        }
예제 #3
0
        public override void Initialize(ContentManager contentLoader, ComponentManifest manifest)
        {
            Effect genericEffect = contentLoader.Load <Effect>("shaders\\Billboard");

            // If we have several of these objects, the content manager will return
            // a single shared effect instance to them all. But we want to preconfigure
            // the effect with parameters that are specific to this particular
            // object. By cloning the effect, we prevent one
            // from stomping over the parameter settings of another.

            Effect = genericEffect.Clone();
            Effect.CurrentTechnique = Effect.Techniques["Billboard"];
            EffectRegistry.Add(Effect, RenderOptions.BillboardParams);

            EffectParameterCollection parameters = Effect.Parameters;

            // Set the values of parameters that do not change.
            parameters["WindAmount"].SetValue(0.0f);
            parameters["BillboardWidth"].SetValue(2.0f);
            parameters["BillboardHeight"].SetValue(2.0f);
            mTexture = contentLoader.Load <Texture>((string)(manifest.Properties[ManifestKeys.TEXTURE]));
            parameters["Texture"].SetValue(mTexture);
            parameters["gBright"].SetValue(0.17f);
            parameters["gContrast"].SetValue(0.9f);

            mDepthOnlyEffect = Effect.Clone();
            mDepthOnlyEffect.CurrentTechnique = mDepthOnlyEffect.Techniques["DepthOnlyBillboard"];
            EffectRegistry.Add(mDepthOnlyEffect, RenderOptions.BillboardParams);

            mGeometry = new MeshPart();

            BillboardVertex[] vertices = new BillboardVertex[4];

            vertices[0].Position = Vector3.Zero;
            vertices[1].Position = Vector3.Zero;
            vertices[2].Position = Vector3.Zero;
            vertices[3].Position = Vector3.Zero;

            vertices[0].Normal = Vector3.Up;
            vertices[1].Normal = Vector3.Up;
            vertices[2].Normal = Vector3.Up;
            vertices[3].Normal = Vector3.Up;

            vertices[0].TexCoord = Vector2.Zero;
            vertices[1].TexCoord = Vector2.UnitX;
            vertices[2].TexCoord = Vector2.One;
            vertices[3].TexCoord = Vector2.UnitY;

            float randValue = 0.5f;

            if (manifest.Properties.ContainsKey(ManifestKeys.IS_RANDOMIZED) &&
                (bool)(manifest.Properties[ManifestKeys.IS_RANDOMIZED]))
            {
                randValue = (float)(GameResources.ActorManager.Random.Next(-524288, 524288)) / (float)524288;
            }

            vertices[0].Random = randValue;
            vertices[1].Random = randValue;
            vertices[2].Random = randValue;
            vertices[3].Random = randValue;

            mGeometry.VertexBuffer = new VertexBuffer(
                SharedResources.Game.GraphicsDevice,
                BillboardVertex.VertexDeclaration,
                4,
                BufferUsage.None);

            mGeometry.VertexBuffer.SetData(vertices);

            // Create and populate the index buffer.
            ushort[] indices = new ushort[] { 0, 1, 2, 0, 2, 3 };

            mGeometry.IndexBuffer = new IndexBuffer(
                SharedResources.Game.GraphicsDevice,
                typeof(ushort),
                indices.Length,
                BufferUsage.None);

            mGeometry.IndexBuffer.SetData(indices);

            mGeometry.NumVertices    = 4;
            mGeometry.PrimitiveCount = 2;
            mGeometry.StartIndex     = 0;
            mGeometry.VertexOffset   = 0;

            GameResources.ActorManager.PreAnimationUpdateStep += PreAnimationUpdateHandler;

            base.Initialize(contentLoader, manifest);
        }
예제 #4
0
        protected override void BuildSphereAndGeometryNodes(ComponentManifest manifest, SceneNode parent)
        {
            Vector4 specColor = (Vector4)(manifest.Properties[ManifestKeys.MAT_SPEC_COLOR]);

            Effect.Parameters[EffectRegistry.MATERIAL_SPECULAR_COLOR_PARAM_NAME].SetValue(specColor);
            Effect.Parameters[EffectRegistry.TEXTURE_PARAM_NAME].SetValue(mTexture);

            float brightness = 1.0f;

            if (manifest.Properties.ContainsKey(ManifestKeys.BRIGHTNESS))
            {
                brightness = (float)(manifest.Properties[ManifestKeys.BRIGHTNESS]);
            }
            Effect.Parameters[EffectRegistry.BRIGHTNESS_PARAM_NAME].SetValue(brightness);

            float contrast = 1.0f;

            if (manifest.Properties.ContainsKey(ManifestKeys.CONTRAST))
            {
                contrast = (float)(manifest.Properties[ManifestKeys.CONTRAST]);
            }
            Effect.Parameters[EffectRegistry.CONTRAST_PARAM_NAME].SetValue(contrast);

            // First, add the effect's params to the EffectRegistry so the ParamSetter can retrieve them.
            EffectRegistry.Add(Effect, RenderOptions.RequiresHDRLighting | RenderOptions.RequiresShadowMap);

            // Create the default material
            EffectApplication defaultMaterial = new EffectApplication(Effect, RenderStatePresets.Default);

            defaultMaterial.AddParamSetter(new CommonParamSetter());
            defaultMaterial.AddParamSetter(new HDRLightParamSetter());
            defaultMaterial.AddParamSetter(new FogParamSetter());
            defaultMaterial.AddParamSetter(new ShadowParamSetter());

            RawGeometryNode            geometry  = null;
            ExplicitBoundingSphereNode meshBound = null;

            switch ((Shape)(manifest.Properties[ManifestKeys.SHAPE]))
            {
            case Shape.Box:
                geometry  = new RawGeometryNode(GeneratedGeometry.Box, defaultMaterial);
                meshBound = new ExplicitBoundingSphereNode(GeneratedGeometry.BoxBound);
                break;

            case Shape.Sphere:
                geometry  = new RawGeometryNode(GeneratedGeometry.Sphere, defaultMaterial);
                meshBound = new ExplicitBoundingSphereNode(GeneratedGeometry.SphereBound);
                break;
            }

            parent.AddChild(meshBound);
            meshBound.AddChild(geometry);

            // Create the ShadowMap material
            // Default to CastsShadow = true
            if (!manifest.Properties.ContainsKey(ManifestKeys.CASTS_SHADOW) || (bool)(manifest.Properties[ManifestKeys.CASTS_SHADOW]))
            {
                EffectApplication depthOnlyMaterial;
                depthOnlyMaterial = new EffectApplication(EffectRegistry.DepthOnlyFx, RenderStatePresets.Default);
                depthOnlyMaterial.AddParamSetter(new WorldViewProjParamSetter());
                geometry.AddMaterial(TraversalContext.MaterialFlags.ShadowMap, depthOnlyMaterial);
            }
            else
            {
                geometry.AddMaterial(TraversalContext.MaterialFlags.ShadowMap, null);
            }

            // If the GeometryNode requires additional materials, they would need to be added here.
        }
예제 #5
0
        protected override void BuildSphereAndGeometryNodes(ComponentManifest manifest, SceneNode parent)
        {
            foreach (ModelMesh mm in VisualModel.Meshes)
            {
                ExplicitBoundingSphereNode meshBound = new ExplicitBoundingSphereNode(mm.BoundingSphere);
                parent.AddChild(meshBound);

                foreach (ModelMeshPart mmp in mm.MeshParts)
                {
                    MaterialInfo mi = mmp.Tag as MaterialInfo;
                    if (mi == null)
                    {
                        throw new AssetFormatException("The VisualModel's ModelMeshParts do not contain the MaterialInfo in the Tag property.");
                    }

                    // First, add the effect's params to the EffectRegistry so the ParamSetter can retrieve them.
                    EffectRegistry.Add(mmp.Effect, mi.HandlingFlags);

                    // Create the default material
                    EffectApplication defaultMaterial = new EffectApplication(mmp.Effect, mi.RenderState);
                    defaultMaterial.AddParamSetter(new CommonParamSetter());
                    defaultMaterial.AddParamSetter(new FogParamSetter());

                    if ((mi.HandlingFlags & RenderOptions.RequiresHDRLighting) > 0)
                    {
                        defaultMaterial.AddParamSetter(new HDRLightParamSetter());
                    }

                    if ((mi.HandlingFlags & RenderOptions.RequiresSkeletalPose) > 0)
                    {
                        defaultMaterial.AddParamSetter(new SkinParamSetter());
                    }

                    if ((mi.HandlingFlags & RenderOptions.RequiresShadowMap) > 0)
                    {
                        defaultMaterial.AddParamSetter(new ShadowParamSetter());
                    }

                    if ((mi.HandlingFlags & RenderOptions.RequiresFringeMap) > 0)
                    {
                        defaultMaterial.AddParamSetter(new FringeMapParamSetter());
                    }

                    ModelGeometryNode geometry = new ModelGeometryNode(mmp, defaultMaterial);
                    meshBound.AddChild(geometry);

                    // Create the ShadowMap material
                    // Default to CastsShadow = true
                    if (!manifest.Properties.ContainsKey(ManifestKeys.CASTS_SHADOW) || (bool)(manifest.Properties[ManifestKeys.CASTS_SHADOW]))
                    {
                        EffectApplication depthOnlyMaterial;
                        if ((mi.HandlingFlags & RenderOptions.RequiresSkeletalPose) > 0)
                        {
                            depthOnlyMaterial = new EffectApplication(EffectRegistry.DepthOnlySkinFx, RenderStatePresets.Default);
                            depthOnlyMaterial.AddParamSetter(new SkinParamSetter());
                        }
                        else
                        {
                            depthOnlyMaterial = new EffectApplication(EffectRegistry.DepthOnlyFx, RenderStatePresets.Default);
                        }

                        depthOnlyMaterial.AddParamSetter(new WorldViewProjParamSetter());

                        geometry.AddMaterial(TraversalContext.MaterialFlags.ShadowMap, depthOnlyMaterial);
                    }
                    else
                    {
                        geometry.AddMaterial(TraversalContext.MaterialFlags.ShadowMap, null);
                    }

                    // If the GeometryNode requires additional materials, they would need to be added here.
                }
            }
        }