internal static void SetTexture(this Effect effect, Texture2D texture) { if (effect is BasicEffect) { BasicEffect source = effect as BasicEffect; source.Texture = texture; source.TextureEnabled = texture != null; } else if (effect is SkinnedEffect) { SkinnedEffect source = effect as SkinnedEffect; source.Texture = texture; } else if (effect is EnvironmentMapEffect) { EnvironmentMapEffect source = effect as EnvironmentMapEffect; source.Texture = texture; } else if (effect is DualTextureEffect) { DualTextureEffect source = effect as DualTextureEffect; source.Texture = texture; } else if (effect is AlphaTestEffect) { AlphaTestEffect source = effect as AlphaTestEffect; source.Texture = texture; } }
internal static Texture2D GetTexture(this Effect sourceEffect) { Texture2D texture = null; if (sourceEffect is BasicEffect) { BasicEffect source = sourceEffect as BasicEffect; texture = source.Texture; } else if (sourceEffect is SkinnedEffect) { SkinnedEffect source = sourceEffect as SkinnedEffect; texture = source.Texture; } else if (sourceEffect is EnvironmentMapEffect) { EnvironmentMapEffect source = sourceEffect as EnvironmentMapEffect; texture = source.Texture; } else if (sourceEffect is DualTextureEffect) { DualTextureEffect source = sourceEffect as DualTextureEffect; texture = source.Texture; } else if (sourceEffect is AlphaTestEffect) { AlphaTestEffect source = sourceEffect as AlphaTestEffect; texture = source.Texture; } return(texture); }
public override void LoadContent() { base.LoadContent(); // To hacked for now if (!_environmentTextureLoaded && _environmentTextureNames != null) { // Detect the texture size if it doesn't specified if (_environmentTextureSize == 0) { Texture2D firstTexture = YnG.Content.Load <Texture2D>(_environmentTextureNames[0]); _environmentTextureSize = Math.Min(firstTexture.Width, firstTexture.Height); } // Create the environment texture _environmentTexture = new TextureCube(YnG.GraphicsDevice, _environmentTextureSize, _enableTextureMipmap, SurfaceFormat.Color); Texture2D texture = null; // Temp texture Color[] textureData; // Temp textureData array string[] tempTextureNames = new string[6]; // If the texture array has not a size of 6, we replace empty texture by the latest int nbTextures = _environmentTextureNames.Length; for (int i = 0; i < 6; i++) { if (i < nbTextures) // Texture { tempTextureNames[i] = _environmentTextureNames[i]; } else // Copied texture { tempTextureNames[i] = _environmentTextureNames[nbTextures - 1]; } // Load the texture and add it to the TextureCube texture = YnG.Content.Load <Texture2D>(tempTextureNames[i]); textureData = new Color[texture.Width * texture.Height]; texture.GetData <Color>(textureData); _environmentTexture.SetData <Color>((CubeMapFace)i, textureData); } // Update the texture names array _environmentTextureNames = tempTextureNames; _environmentTextureLoaded = true; // If the first texture is null we create a dummy texture with the same size of environment texture if (_texture == null) { _texture = YnGraphics.CreateTexture(Color.White, _environmentTextureSize, _environmentTextureSize); _textureLoaded = true; } } if (!_effectLoaded) { _effect = new EnvironmentMapEffect(YnG.GraphicsDevice); _effectLoaded = true; } }
public override void Update(BaseCamera camera, ref Matrix world) { if (!_effectLoaded) { return; } // Update matrices base.Update(camera, ref world); EnvironmentMapEffect environmentMapEffect = (EnvironmentMapEffect)_effect; // Texture environmentMapEffect.Texture = _texture; environmentMapEffect.EnvironmentMap = _environmentTexture; environmentMapEffect.EnvironmentMapAmount = _environmentAmount; environmentMapEffect.EnvironmentMapSpecular = _specularColor * _specularIntensity; environmentMapEffect.FresnelFactor = _fresnelFactor; // Fog UpdateFog(environmentMapEffect); // Lights if (UpdateLights(environmentMapEffect)) { environmentMapEffect.EmissiveColor = _emissiveColor; environmentMapEffect.DiffuseColor = _diffuseColor * _diffuseIntensity; environmentMapEffect.Alpha = _alphaColor; } }
/// <summary> /// Initializes this instance. /// </summary> /// <param name="ginfo"></param> /// <param name="factory"></param> /// <param name="obj"></param> public override void Initialize(GraphicInfo ginfo, GraphicFactory factory, IObject obj) { effect = factory.GetEnvironmentMapEffect(); if (enableLightining) { effect.EnableDefaultLighting(); } effect.EnvironmentMapAmount = amountDiffuse; base.Initialize(ginfo, factory, obj); }
public GenericEffect(Effect effectToAssimilate) : this() { /* * DefaultShaderType type = DetermineEffect( false, effectToAssimilate ); * if( type == DefaultShaderType.Basic ) * { * mBasicEffect = (BasicEffect)effectToAssimilate; * } * else if( type == DefaultShaderType.DualTexture ) * { * mDualTextureEffect = (DualTextureEffect)effectToAssimilate; * } * else if( type == DefaultShaderType.EnvironmentMapped ) * { * mEnvironmentMapEffect = (EnvironmentMapEffect)effectToAssimilate; * } * else if( type == DefaultShaderType.Skinned ) * { * mSkinnedEffect = (SkinnedEffect)effectToAssimilate; * } */ if (effectToAssimilate is BasicEffect) { mBasicEffect = effectToAssimilate as BasicEffect; } else if (effectToAssimilate is AlphaTestEffect) { mAlphaTestEffect = effectToAssimilate as AlphaTestEffect; } #if !MONOGAME else if (effectToAssimilate is DualTextureEffect) { mDualTextureEffect = effectToAssimilate as DualTextureEffect; } else if (effectToAssimilate is EnvironmentMapEffect) { mEnvironmentMapEffect = effectToAssimilate as EnvironmentMapEffect; } else if (effectToAssimilate is SkinnedEffect) { mSkinnedEffect = effectToAssimilate as SkinnedEffect; } #endif }
private void DrawModel(Model m, Matrix world, EnvironmentMapEffect be, ChaseCamera camera) { foreach (ModelMesh mm in m.Meshes) { foreach (ModelMeshPart mmp in mm.MeshParts) { be.View = camera.View; be.Projection = camera.Projection; be.World = world; ScreenManager.GraphicsDevice.SetVertexBuffer(mmp.VertexBuffer, mmp.VertexOffset); ScreenManager.GraphicsDevice.Indices = mmp.IndexBuffer; be.CurrentTechnique.Passes[0].Apply(); ScreenManager.GraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, mmp.NumVertices, mmp.StartIndex, mmp.PrimitiveCount); } } }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // Calculate the screen aspect ratio float aspectRatio = (float)GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height; // Create a projection matrix Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), aspectRatio, 0.1f, 1000.0f); // Calculate a view matrix (where we are looking from and to) Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 4), Vector3.Zero, Vector3.Up); // Create and initialize the effect _effect = new EnvironmentMapEffect(GraphicsDevice); _effect.Projection = projection; _effect.View = view; _effect.World = Matrix.Identity; _effect.EnableDefaultLighting(); base.Initialize(); }
public GenericEffect(DefaultShaderType type) : this() { if (type != DefaultShaderType.Determine) { if (type == DefaultShaderType.Basic) { mBasicEffect = new BasicEffect(FlatRedBallServices.GraphicsDevice); } else if (type == DefaultShaderType.AlphaTest) { mAlphaTestEffect = new AlphaTestEffect(FlatRedBallServices.GraphicsDevice); } else if (type == DefaultShaderType.DualTexture) { #if MONOGAME throw new NotImplementedException(); #else mDualTextureEffect = new DualTextureEffect(FlatRedBallServices.GraphicsDevice); #endif } else if (type == DefaultShaderType.EnvironmentMapped) { #if MONOGAME throw new NotImplementedException(); #else mEnvironmentMapEffect = new EnvironmentMapEffect(FlatRedBallServices.GraphicsDevice); #endif } else if (type == DefaultShaderType.Skinned) { #if MONOGAME throw new NotImplementedException(); #else mSkinnedEffect = new SkinnedEffect(FlatRedBallServices.GraphicsDevice); #endif } } }
/// <summary> /// Prepare a BasicEffect for rendering /// </summary> /// <param name="effect"></param> protected void PrepareEffect(EnvironmentMapEffect effect) { // Apply the texture if we have one and it differs from the active texture if (effect.Texture != ObjectTexture && ObjectTexture != null) { effect.Texture = ObjectTexture; } // Set the color and alpha effect.DiffuseColor = ObjectColor.ToVector3(); effect.EmissiveColor = EmissiveColor.ToVector3(); effect.Alpha = (float)ObjectColor.A / 255.0f; // Set other effect properties // Do we have a valid texture? if (effect.Texture != null) { // Yes, so apply the other environment map properties as normal effect.EnvironmentMapAmount = EnvironmentMapAmount; effect.EnvironmentMapSpecular = EnvironmentMapSpecular.ToVector3(); effect.FresnelFactor = FresnelFactor; } else { // No, so make the object completely reflective. // Any texture set by other objects will remain but will be entirely hidden by the environment map effect.EnvironmentMapAmount = 1.0f; effect.EnvironmentMapSpecular = Vector3.Zero; effect.FresnelFactor = 0.0f; } // Apply the transformation matrix effect.World = Transformation; // Now the effect is ready for the derived class to actually draw the object }
/// <summary> /// Load graphics content for the game. /// </summary> public override void Activate(bool instancePreserved) { if (!instancePreserved) { if (content == null) { content = new ContentManager(ScreenManager.Game.Services, "Content"); } topViewport = ScreenManager.GraphicsDevice.Viewport; bottomViewport = ScreenManager.GraphicsDevice.Viewport; if (ScreenManager.ScreenHorizontal == true) { topViewport.Height = topViewport.Height / 2; bottomViewport.Height = bottomViewport.Height / 2; bottomViewport.Y = topViewport.Height; camera.AspectRatio = (float)ScreenManager.GraphicsDevice.Viewport.Width / (ScreenManager.GraphicsDevice.Viewport.Height / 2); camera2.AspectRatio = (float)ScreenManager.GraphicsDevice.Viewport.Width / (ScreenManager.GraphicsDevice.Viewport.Height / 2); camera.FieldOfView = MathHelper.ToRadians(45); camera2.FieldOfView = MathHelper.ToRadians(45); } else { topViewport.Width = topViewport.Width / 2; bottomViewport.Width = bottomViewport.Width / 2; bottomViewport.X = topViewport.Width; camera.AspectRatio = (float)(ScreenManager.GraphicsDevice.Viewport.Width / 2) / ScreenManager.GraphicsDevice.Viewport.Height; camera2.AspectRatio = (float)(ScreenManager.GraphicsDevice.Viewport.Width / 2) / ScreenManager.GraphicsDevice.Viewport.Height; camera.FieldOfView = MathHelper.ToRadians(60); camera2.FieldOfView = MathHelper.ToRadians(60); } gameFont = content.Load <SpriteFont>("gamefont"); rockModel = content.Load <Model>("Rock"); shipModel = content.Load <Model>("SpaceShip1"); shipModel2 = content.Load <Model>("SpaceShip2"); groundModel = content.Load <Model>("Ground"); cubeModel = content.Load <Model>("cube"); bulletModel = content.Load <Model>("Cone"); skyBoxModel = content.Load <Model>("Space_SkyBox"); audioEngine = ScreenManager.AudioEngine; soundBank = ScreenManager.SoundBank; waveBank = ScreenManager.WaveBank; acSFX = audioEngine.GetCategory("SFX"); acMusic = audioEngine.GetCategory("Music"); // Environmental Map Effect for Player 1 envEffect1 = new EnvironmentMapEffect(ScreenManager.GraphicsDevice); envEffect1.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver4, ScreenManager.GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f); envEffect1.View = Matrix.CreateLookAt( new Vector3(2, 3, 32), Vector3.Zero, Vector3.Up); textureCube1 = new TextureCube(ScreenManager.GraphicsDevice, 256, false, SurfaceFormat.Color); Color[] facedata1 = new Color[256 * 256]; for (int i = 0; i < 6; i++) { envEffect1.Texture = content.Load <Texture2D>("skybox" + i.ToString()); envEffect1.Texture.GetData <Color>(facedata1); textureCube1.SetData <Color>((CubeMapFace)i, facedata1); } envEffect1.Texture = (shipModel.Meshes[0].Effects[0] as EnvironmentMapEffect).Texture; envEffect1.EnvironmentMap = textureCube1; envEffect1.EnableDefaultLighting(); envEffect1.EnvironmentMapAmount = 1.0f; envEffect1.FresnelFactor = 1.0f; envEffect1.EnvironmentMapSpecular = Vector3.Zero; // Environmental Map Effect for Player 2 envEffect2 = new EnvironmentMapEffect(ScreenManager.GraphicsDevice); envEffect2.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver4, ScreenManager.GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f); envEffect2.View = Matrix.CreateLookAt( new Vector3(2, 3, 32), Vector3.Zero, Vector3.Up); textureCube2 = new TextureCube(ScreenManager.GraphicsDevice, 256, false, SurfaceFormat.Color); Color[] facedata2 = new Color[256 * 256]; for (int i = 0; i < 6; i++) { envEffect2.Texture = content.Load <Texture2D>("skybox" + i.ToString()); envEffect2.Texture.GetData <Color>(facedata2); textureCube2.SetData <Color>((CubeMapFace)i, facedata2); } envEffect2.Texture = (shipModel2.Meshes[0].Effects[0] as EnvironmentMapEffect).Texture; envEffect2.EnvironmentMap = textureCube2; envEffect2.EnableDefaultLighting(); envEffect2.EnvironmentMapAmount = 1.0f; envEffect2.FresnelFactor = 1.0f; envEffect2.EnvironmentMapSpecular = Vector3.Zero; //audioEngine = ScreenManager.AudioEngine; //soundBank = ScreenManager.SoundBank; //waveBank = ScreenManager.WaveBank; if (ScreenManager.AudioEnabled == true) { acSFX.SetVolume(ScreenManager.SFXVolume); acMusic.SetVolume(ScreenManager.AudioVolume); } else { acSFX.SetVolume(0); acMusic.SetVolume(0); } FxCue = soundBank.GetCue("ShotFx"); //FxCue.Apply3D(shipListen1, shipEmit1); ship1Pos = new Vector3(10000, 350, 10000); ship2Pos = new Vector3(100, 350, 100); // Create shiplllllllllllllllllllllllllllll ship = new Ship(ScreenManager.GraphicsDevice, ship1Pos, soundBank); ship2 = new Ship(ScreenManager.GraphicsDevice, ship2Pos, soundBank); //ship2.Position = new Vector3(100, 100, 100); RandomRockSpawner(); UpdateCameraChaseTarget(ship, camera); UpdateCameraChaseTarget(ship2, camera2); camera.Reset(); camera2.Reset(); // A real game would probably have more content than this sample, so // it would take longer to load. We simulate that by delaying for a // while, giving you a chance to admire the beautiful loading screen. Thread.Sleep(1000); // once the load has finished, we use ResetElapsedTime to tell the game's // timing mechanism that we have just finished a very long frame, and that // it should not try to catch up. ScreenManager.Game.ResetElapsedTime(); } #if WINDOWS_PHONE if (Microsoft.Phone.Shell.PhoneApplicationService.Current.State.ContainsKey("PlayerPosition")) { playerPosition = (Vector2)Microsoft.Phone.Shell.PhoneApplicationService.Current.State["PlayerPosition"]; enemyPosition = (Vector2)Microsoft.Phone.Shell.PhoneApplicationService.Current.State["EnemyPosition"]; } #endif }
internal static void CopyMaterialsFrom(this Effect effect, Effect sourceEffect) { if (effect == null || sourceEffect == null) { return; } Vector3 DiffuseColor = Vector3.One; Vector3 SpecularColor = Vector3.Zero; Vector3 EmissiveColor = Vector3.Zero; float SpecularPower = 16; float Alpha = 1; Texture2D Texture = sourceEffect.GetTexture(); if (Texture != null) { effect.SetTexture(Texture); } // Extract from source if (sourceEffect is BasicEffect) { BasicEffect source = sourceEffect as BasicEffect; DiffuseColor = source.DiffuseColor; EmissiveColor = source.EmissiveColor; SpecularColor = source.SpecularColor; SpecularPower = source.SpecularPower; Alpha = source.Alpha; } else if (sourceEffect is SkinnedEffect) { SkinnedEffect source = sourceEffect as SkinnedEffect; DiffuseColor = source.DiffuseColor; EmissiveColor = source.EmissiveColor; SpecularColor = source.SpecularColor; SpecularPower = source.SpecularPower; Alpha = source.Alpha; } else if (sourceEffect is EnvironmentMapEffect) { EnvironmentMapEffect source = sourceEffect as EnvironmentMapEffect; DiffuseColor = source.DiffuseColor; EmissiveColor = source.EmissiveColor; Alpha = source.Alpha; } else if (sourceEffect is DualTextureEffect) { DualTextureEffect source = sourceEffect as DualTextureEffect; DiffuseColor = source.DiffuseColor; Alpha = source.Alpha; } else if (sourceEffect is AlphaTestEffect) { AlphaTestEffect source = sourceEffect as AlphaTestEffect; DiffuseColor = source.DiffuseColor; Alpha = source.Alpha; } // Apply to target if (effect is BasicEffect) { BasicEffect target = effect as BasicEffect; target.DiffuseColor = DiffuseColor; target.EmissiveColor = EmissiveColor; target.SpecularColor = SpecularColor; target.SpecularPower = SpecularPower; target.Alpha = Alpha; } else if (effect is SkinnedEffect) { SkinnedEffect target = effect as SkinnedEffect; target.DiffuseColor = DiffuseColor; target.EmissiveColor = EmissiveColor; target.SpecularColor = SpecularColor; target.SpecularPower = SpecularPower; target.Alpha = Alpha; } else if (effect is EnvironmentMapEffect) { EnvironmentMapEffect target = effect as EnvironmentMapEffect; target.DiffuseColor = DiffuseColor; target.EmissiveColor = EmissiveColor; target.Alpha = Alpha; } else if (effect is DualTextureEffect) { DualTextureEffect target = effect as DualTextureEffect; target.DiffuseColor = DiffuseColor; target.Alpha = Alpha; } else if (effect is AlphaTestEffect) { AlphaTestEffect target = effect as AlphaTestEffect; target.DiffuseColor = DiffuseColor; target.Alpha = Alpha; } }
public EnvironmentMapMaterial(GraphicsDevice graphics) { GraphicsDevice = graphics; effect = GraphicsResources <EnvironmentMapEffect> .GetInstance(graphics, typeof(EnvironmentMapMaterial)); }
private DefaultShaderType DetermineEffect(bool createNewEffect, Effect effectToTest) { #region Determine precendence between effects float skinningPrecedence = mPrecedenceTable["SkinnedEffect"]; float dualTexturePrecedence = mPrecedenceTable["DualTextureEffect"]; float environmentMappingPrecedence = mPrecedenceTable["EnvironmentMapEffect"]; float basicEffectPrecedence = mPrecedenceTable["BasicEffect"]; #endregion #region Determining the possible types of Effect #region Check for key information for determining possible effect types Texture2D tex = Texture2; Matrix[] transforms = BoneTransforms; #if !MONOGAME TextureCube cube = CubeMap; #endif if (tex == null) { dualTexturePrecedence = float.MinValue; } #if !MONOGAME if (cube == null) { environmentMappingPrecedence = float.MinValue; } #endif if (transforms == null) { skinningPrecedence = float.MinValue; } #endregion // Three possible outcomes. If its a lower precedence than any // of it's competitors, it will lose out. // If it's greater than the others, it will win, // If it's equal to one another, well in technical terms, we guess. // ... // WELCOME TO IF STATEMENT HELL #if !MONOGAME #region Skinning Shader determination... Don't look. Ever. //... I said don't look. Sigh if (skinningPrecedence >= environmentMappingPrecedence) { if (skinningPrecedence >= dualTexturePrecedence) { if (skinningPrecedence > basicEffectPrecedence) { if (createNewEffect) { // Create a skinning shader mSkinnedEffect = new SkinnedEffect(FlatRedBallServices.GraphicsDevice); SetSkinningEffectParameters(); } return(DefaultShaderType.Skinned); } } } #endregion #region Environment Mapping Determination if (environmentMappingPrecedence >= dualTexturePrecedence) { if (environmentMappingPrecedence >= basicEffectPrecedence) { if (createNewEffect) { // Create Environment map mEnvironmentMapEffect = new EnvironmentMapEffect(FlatRedBallServices.GraphicsDevice); SetEnvironmentMapEffectParameters(); } return(DefaultShaderType.EnvironmentMapped); } } #endregion #region Dual Texture determination if (dualTexturePrecedence >= basicEffectPrecedence) { // Create Dual Texture if (createNewEffect) { mDualTextureEffect = new DualTextureEffect(FlatRedBallServices.GraphicsDevice); SetDualTextureEffectParameters(); } return(DefaultShaderType.DualTexture); } #endregion #endif #region Basic Shader determination // Create a basic Shader if (createNewEffect) { mBasicEffect = new BasicEffect(FlatRedBallServices.GraphicsDevice); SetBasicEffectParameters(); } return(DefaultShaderType.Basic); #endregion #endregion }
/// <summary> /// Looks up shortcut references to our effect parameters. /// </summary> void CacheEffectParameters(EnvironmentMapEffect cloneSource) { textureParam = Parameters["Texture"]; environmentMapParam = Parameters["EnvironmentMap"]; environmentMapAmountParam = Parameters["EnvironmentMapAmount"]; environmentMapSpecularParam = Parameters["EnvironmentMapSpecular"]; fresnelFactorParam = Parameters["FresnelFactor"]; diffuseColorParam = Parameters["DiffuseColor"]; emissiveColorParam = Parameters["EmissiveColor"]; eyePositionParam = Parameters["EyePosition"]; fogColorParam = Parameters["FogColor"]; fogVectorParam = Parameters["FogVector"]; worldParam = Parameters["World"]; worldInverseTransposeParam = Parameters["WorldInverseTranspose"]; worldViewProjParam = Parameters["WorldViewProj"]; shaderIndexParam = Parameters["ShaderIndex"]; light0 = new DirectionalLight(Parameters["DirLight0Direction"], Parameters["DirLight0DiffuseColor"], null, (cloneSource != null) ? cloneSource.light0 : null); light1 = new DirectionalLight(Parameters["DirLight1Direction"], Parameters["DirLight1DiffuseColor"], null, (cloneSource != null) ? cloneSource.light1 : null); light2 = new DirectionalLight(Parameters["DirLight2Direction"], Parameters["DirLight2DiffuseColor"], null, (cloneSource != null) ? cloneSource.light2 : null); }
/// <summary> /// Creates a new EnvironmentMapEffect by cloning parameter settings from an existing instance. /// </summary> protected EnvironmentMapEffect(EnvironmentMapEffect cloneSource) : base(cloneSource) { CacheEffectParameters(cloneSource); fogEnabled = cloneSource.fogEnabled; fresnelEnabled = cloneSource.fresnelEnabled; specularEnabled = cloneSource.specularEnabled; world = cloneSource.world; view = cloneSource.view; projection = cloneSource.projection; diffuseColor = cloneSource.diffuseColor; emissiveColor = cloneSource.emissiveColor; ambientLightColor = cloneSource.ambientLightColor; alpha = cloneSource.alpha; fogStart = cloneSource.fogStart; fogEnd = cloneSource.fogEnd; }