[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; public class MyGame : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D texture; Effect effect; public MyGame() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void LoadContent() { texture = Content.Load("henohenomoheji"); effect = Content.Load ("2DEffect"); spriteBatch = new SpriteBatch(GraphicsDevice); } protected override void UnloadContent() { spriteBatch.Dispose(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin( SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None ); effect.Begin(); effect.CurrentTechnique.Passes[0].Begin(); spriteBatch.Draw(texture, new Vector2(), Color.White); effect.CurrentTechnique.Passes[0].End(); effect.End(); spriteBatch.End(); } static void Main() { using (MyGame game = new MyGame()) { game.Run(); } } }
sampler TextureSampler : register(s0); float4 PixelShaderFunction(float2 textureCoordinate : TEXCOORD) : COLOR0 { float2 texOffset = {sin(textureCoordinate.x * 30) / 20 , 0}; return tex2D( TextureSampler, textureCoordinate + texOffset ); } technique Technique1 { pass Pass1 { PixelShader = compile ps_2_0 PixelShaderFunction(); } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; public class MyGame : Game { GraphicsDeviceManager graphics; Texture2D inputTexture; RenderTarget2D result; Effect additionCalculator; VertexDeclaration vertexDeclaration; //GPUから計算結果を受け取るバッファ float[] resultBuffer = new float[2]; VertexPositionTexture[] vertices = { new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2()), new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)), new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)), new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)) }; public MyGame() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void LoadContent() { inputTexture = new Texture2D(GraphicsDevice, 2, 1, 0, TextureUsage.None, SurfaceFormat.Single); inputTexture.SetData<float>(new float[] { 1, 1 }); result = new RenderTarget2D(GraphicsDevice, 2, 1, 0, SurfaceFormat.Single); additionCalculator = Content.Load<Effect>("AdditionCalculator"); GraphicsDevice.VertexDeclaration = new VertexDeclaration( GraphicsDevice, VertexPositionTexture.VertexElements ); vertexDeclaration = GraphicsDevice.VertexDeclaration; } protected override void UnloadContent() { inputTexture.Dispose(); result.Dispose(); vertexDeclaration.Dispose(); } protected override void Update(GameTime gameTime) { Window.Title = resultBuffer[1].ToString(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.SetRenderTarget(0, result); additionCalculator.Parameters["InputTexture"].SetValue(inputTexture); additionCalculator.Parameters["InputWidth"].SetValue(2); additionCalculator.Begin(); additionCalculator.CurrentTechnique.Passes[0].Begin(); GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>( PrimitiveType.TriangleList, vertices, 0, 2 ); additionCalculator.CurrentTechnique.Passes[0].End(); additionCalculator.End(); //GetDataを呼ぶには、レンダーターゲットはいったん //デバイスから外されなければいけません。 GraphicsDevice.SetRenderTarget(0, null); result.GetTexture().GetData<float>(resultBuffer); } static void Main() { using (MyGame game = new MyGame()) { game.Run(); } } }
float InputWidth; texture InputTexture; sampler InputSampler = sampler_state { Texture = (InputTexture); }; struct VertexPositionTexture { float4 Position : POSITION; float2 TextureCoordinate : TEXCOORD; }; VertexPositionTexture VertexShaderFunction(VertexPositionTexture input) { return input; } float4 PixelShaderFunction(VertexPositionTexture input) : COLOR0 { return tex2D(InputSampler, input.TextureCoordinate + float2(-1/InputWidth, 0)) + tex2D(InputSampler, input.TextureCoordinate); } technique Technique1 { pass Pass1 { VertexShader = compile vs_1_1 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }
ContentManager Content { get; }
protected virtual void LoadContent();
protected virtual void UnloadContent();
public GraphicsDevice GraphicsDevice { get; }
float4x4 World, View, Projection; float Shatter; texture Texture; sampler TextureSampler = sampler_state { Texture = (Texture); }; struct VertexPositionTexture { float4 Position : POSITION; float2 TextureCoordinate : TEXCOORD; }; VertexPositionTexture ShatterVertexShader( float4 position:POSITION, float4 normal:NORMAL, float2 textureCoordinate :TEXCOORD ) { VertexPositionTexture output; position.xyz += normal.xyz * Shatter; float4x4 transform = mul(World, mul(View, Projection)); output.Position = mul(position, transform); output.TextureCoordinate = textureCoordinate; return output; } float4 ShatterPixelShader(float2 textureCoordinate : TEXCOORD) : COLOR { return tex2D(TextureSampler, textureCoordinate); } technique ShatterEffect { pass { VertexShader = compile vs_2_0 ShatterVertexShader(); PixelShader = compile ps_2_0 ShatterPixelShader(); } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; class MyGame : Game { GraphicsDeviceManager graphics; ContentManager content; //Graphics Device Objects Effect effect; Model model; public MyGame() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { effect = content.Load<Effect>("ShatterEffect"); model = loadModel("Ship", effect); } } Model loadModel(string modelName, Effect effect) { Model result = content.Load<Model>(modelName); foreach (ModelMesh mesh in result.Meshes) { foreach (ModelMeshPart part in mesh.MeshParts) { effect.Parameters["Texture"].SetValue( ((BasicEffect)part.Effect).Texture ); part.Effect = effect.Clone(graphics.GraphicsDevice); } } return result; } protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent) { content.Unload(); } } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); foreach (ModelMesh mesh in model.Meshes) { foreach (Effect effect in mesh.Effects) { setEffectParameters( effect, gameTime.TotalGameTime.TotalSeconds ); } mesh.Draw(); } } private void setEffectParameters(Effect effect, double totalSeconds) { Matrix world = Matrix.CreateRotationY( (float)totalSeconds * 2 ); Matrix view = Matrix.CreateLookAt( new Vector3(0, 0, 3000), new Vector3(), Vector3.UnitY ); Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(90), (float)graphics.GraphicsDevice.Viewport.Width / graphics.GraphicsDevice.Viewport.Height, 1, 10000 ); effect.Parameters["Shatter"].SetValue( (float)(1 + System.Math.Sin(totalSeconds)) * 100 ); effect.Parameters["World"].SetValue(world); effect.Parameters["View"].SetValue(view); effect.Parameters["Projection"].SetValue(projection); } }