Shader in XNA programmieren



  • Ich möchte eine Maske erstellen damit nur einige Stellen des Hintergrunds sichtbar sind. Dazu möchte ich zwei Texturen kombinieren. Die Hintergrundtextur und eine schwarze Textur die aber an bestimmten Stellen weiß oder grau ist. Jedoch funktioniert der Code leider nicht. Könnt ihr mir helfen? Ist mein erster Shader.
    So sollte das Resultat aussehen: http://www.xnamag.de/articles/images/img_034_010.png

    float4x4 World; 
    float4x4 View; 
    float4x4 Projection; 
    
    sampler firstSampler; 
    sampler maskSampler : register(s1); 
    GraphicsDevice.Textures[1] = texture; 
    
       float4 PixelShaderFunction(float2 inCoord: TEXCOORD0) : COLOR 
    { 
    
       float4 color = tex2D(firstSampler, inCoord); 
       color.rgba *= tex2D(maskSampler, inCoord).r; 
    
       return color; 
    } 
    
    technique Technique1 
    { 
        pass Pass1 
        { 
    
            PixelShader = compile ps_2_0 PixelShaderFunction(); 
        } 
    }
    

  • Mod

    klar koennen wir die helfen, erste hilfe: fehler genau beschreiben damit man dir helfen kann.



  • *edit*
    Der Code funktioniert jetzt richtig.

    Code .fx Datei:

    float4x4 World;
    float4x4 View;
    float4x4 Projection;
    
    sampler firstSampler : register(s0);
    sampler maskSampler : register(s1);
    
       float4 PixelShaderFunction(float2 inCoord: TEXCOORD0) : COLOR
    {
    
    	float4 color = tex2D(firstSampler, inCoord);
    	color.rgba *= tex2D(maskSampler, inCoord).r;
    
    	return color;
    }
    
    technique Technique1
    {
        pass Pass1
        {
    
            PixelShader = compile ps_2_0 PixelShaderFunction();
        }
    }
    

    Restlicher Code:

    namespace WindowsGame6
    {
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            Texture2D backgr;
            Effect Maske;
            Texture2D licht;
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
            }
    
            protected override void Initialize()
            {
                graphics.PreferredBackBufferWidth = 1280;
                graphics.PreferredBackBufferHeight = 720;
                graphics.IsFullScreen = false;
                graphics.ApplyChanges();
    
                base.Initialize();
            }
    
            protected override void LoadContent()
            {
                spriteBatch = new SpriteBatch(GraphicsDevice);
                backgr = Content.Load<Texture2D>("newtestx");
                Maske = Content.Load<Effect>("Effect1");
                licht = Content.Load<Texture2D>("light");
                GraphicsDevice.Textures[0] = backgr;
                GraphicsDevice.Textures[1] = licht;
    
            }
    
            protected override void UnloadContent()
            {
    
            }
    
            protected override void Update(GameTime gameTime)
            {
    
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();
    
                base.Update(gameTime);
            }
    
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque,
              null, null, null, Maske);
    
                spriteBatch.Draw(backgr, new Rectangle(0, 0, backgr.Width, backgr.Height), Color.White);
    
                spriteBatch.End();
    
                base.Draw(gameTime);
            }
        }
    }
    

Anmelden zum Antworten