1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace Underwater
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
Effect underwater;
Model room;
SpriteBatch sprite;
RenderTarget2D renderTarget;
Texture2D shaderTexture;
Matrix view;
Matrix projection;
Matrix []transform;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}
protected override void Initialize()
{
view = Matrix.CreateLookAt(new Vector3(-700, 200, 250), Vector3.Zero, Vector3.Up);
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 4.0f / 3.0f, 1, 10000);
sprite = new SpriteBatch(graphics.GraphicsDevice);
base.Initialize();
}
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
underwater = content.Load<Effect>("underwater");
room = content.Load<Model>("piece");
}
renderTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.PresentationParameters.BackBufferWidth, graphics.GraphicsDevice.PresentationParameters.BackBufferHeight, 1, graphics.GraphicsDevice.PresentationParameters.BackBufferFormat, graphics.GraphicsDevice.PresentationParameters.MultiSampleType, graphics.GraphicsDevice.PresentationParameters.MultiSampleQuality);
shaderTexture = new Texture2D(graphics.GraphicsDevice, renderTarget.Width, renderTarget.Height, 1, ResourceUsage.ResolveTarget, renderTarget.Format, ResourceManagementMode.Manual);
}
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent)
{
content.Unload();
}
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.SetRenderTarget(0, renderTarget);
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
transform = new Matrix[room.Bones.Count];
room.CopyAbsoluteBoneTransformsTo(transform);
foreach (ModelMesh mesh in room.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.View = view;
effect.Projection = projection;
effect.World = transform[mesh.ParentBone.Index];
}
mesh.Draw();
}
graphics.GraphicsDevice.ResolveRenderTarget(0);
shaderTexture = renderTarget.GetTexture();
graphics.GraphicsDevice.SetRenderTarget(0, null);
sprite.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None);
underwater.Begin();
underwater.CurrentTechnique.Passes[0].Begin();
sprite.Draw(shaderTexture, Vector2.Zero, Color.White);
sprite.End();
underwater.CurrentTechnique.Passes[0].End();
underwater.End();
base.Draw(gameTime);
}
}
} |
Partager