none
XNA 4.0 WaterShader Error RRS feed

  • Frage

  • Hallo Zusammen

    Ich haben das DemoProject: http://digitalerr0r.wordpress.com/2012/03/04/xna-4-0-shader-programming-6simple-ocean/ heruntergeladen und wollte die Wasseroberfläche in mein eigenes Spiel integrieren. Ich habe OceanShader.fx,das ocean.X Model und alle Texturen kopiert, und den Draw Code und den InitializeShader Code übernommen:

            protected void DrawOcean(GameTime gameTime)
            {
                ModelMesh mesh = theOceanMesh.Meshes[0];
                ModelMeshPart meshPart = mesh.MeshParts[0];
    
                // Set parameters
                projectionOceanParameter.SetValue(GameScreen.camera.projection);
                viewOceanParameter.SetValue(GameScreen.camera.view);
                worldOceanParameter.SetValue(Matrix.CreateRotationY(0)); //Matrix.CreateScale(50.0f) * Matrix.CreateRotationX(MathHelper.ToRadians(270)) * Matrix.CreateTranslation(0, -60, 0);
                ambientIntensityOceanParameter.SetValue(0.4f);
                ambientColorOceanParameter.SetValue(new Vector4(0.5f));
                diffuseColorOceanParameter.SetValue(Color.White.ToVector4());
                diffuseIntensityOceanParameter.SetValue(0.2f);
                specularColorOceanParameter.SetValue(Color.White.ToVector4());
                eyePosOceanParameter.SetValue(new Vector3(0));
                colorMapTextureOceanParameter.SetValue(diffuseOceanTexture);
                normalMapTextureOceanParameter.SetValue(normalOceanTexture);
                totalTimeOceanParameter.SetValue(totalTime);
    
                Vector3 lightDirection = new Vector3(1.0f, 0.0f, -1.0f);
    
                //ensure the light direction is normalized, or
                //the shader will give some weird results
                lightDirection.Normalize();
                lightDirectionOceanParameter.SetValue(lightDirection);
    
                //set the vertex source to the mesh's vertex buffer
                Basic.gDevice.SetVertexBuffer(meshPart.VertexBuffer, meshPart.VertexOffset);
    
                //set the current index buffer to the sample mesh's index buffer
                Basic.gDevice.Indices = meshPart.IndexBuffer;
    
                Basic.gDevice.BlendState = BlendState.AlphaBlend;
                Basic.gDevice.DepthStencilState = DepthStencilState.DepthRead;
    
                oceanEffect.CurrentTechnique = oceanEffect.Techniques["Technique1"];
    
                for (int i = 0; i < oceanEffect.CurrentTechnique.Passes.Count; i++)
                {
                    //EffectPass.Apply will update the device to
                    //begin using the state information defined in the current pass
                    oceanEffect.CurrentTechnique.Passes[i].Apply();
    
                    //theMesh contains all of the information required to draw
                    //the current mesh
                    Basic.gDevice.DrawIndexedPrimitives(
                        PrimitiveType.TriangleList, 0, 0,
                        meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount);
                }
            }

    trotzdem gibt es mir bei folgender Textstelle:

                    Basic.gDevice.DrawIndexedPrimitives(
                        PrimitiveType.TriangleList, 0, 0,
                        meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount);

    folgenden Fehler:

    InvalidOperationException wurde nicht behandelt.

    The current vertex declaration does not include all the elements required by the current vertex shader. Tangent0 is missing.

    In einem anderen Forum wurde das Problem schon mal angesprochen und danach auf den Shader als Problem hingewisen. Aber da ich den Shader eins zu eins aus dem Demoproject, in welchem es diesen Error nicht gibt, übernahm, weiss ich nicht was das Problem ist.

    Shader:

    // XNA 4.0 Shader Programming #4 - Normal Mapping
    
    // Matrix
    float4x4 World;
    float4x4 View;
    float4x4 Projection;
    
    // Light related
    float4 AmbientColor;
    float AmbientIntensity;
    
    float3 LightDirection;
    float4 DiffuseColor;
    float DiffuseIntensity;
    
    float4 SpecularColor;
    float3 EyePosition;
    
    float TotalTime;
    
    texture2D ColorMap;
    sampler2D ColorMapSampler = sampler_state
    {
    	Texture = <ColorMap>;
    	MinFilter = linear;
    	MagFilter = linear;
    	MipFilter = linear;
    };
    
    texture2D NormalMap;
    sampler2D NormalMapSampler = sampler_state
    {
    	Texture = <NormalMap>;
    	MinFilter = linear;
    	MagFilter = linear;
    	MipFilter = linear;
    };
    
    // The input for the VertexShader
    struct VertexShaderInput
    {
        float4 Position : POSITION0;
    	float2 TexCoord : TEXCOORD0;
    	float3 Normal : NORMAL0;
    	float3 Binormal : BINORMAL0;
    	float3 Tangent : TANGENT0;
    };
    
    // The output from the vertex shader, used for later processing
    struct VertexShaderOutput
    {
        float4 Position : POSITION0;
    	float2 TexCoord : TEXCOORD0;
    	float3 View : TEXCOORD1;
    	float3x3 WorldToTangentSpace : TEXCOORD2;
    };
    
    // The VertexShader.
    VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
    {
        VertexShaderOutput output;
    	input.Position.z += sin((TotalTime*16)+(input.Position.y/1))/16;
    
        float4 worldPosition = mul(input.Position, World);
        float4 viewPosition = mul(worldPosition, View);
        output.Position = mul(viewPosition, Projection);
    	output.TexCoord = input.TexCoord;
    
    	output.WorldToTangentSpace[0] = mul(normalize(input.Tangent), World);
    	output.WorldToTangentSpace[1] = mul(normalize(input.Binormal), World);
    	output.WorldToTangentSpace[2] = mul(normalize(input.Normal), World);
    	
    	output.View = normalize(float4(EyePosition,1.0) - worldPosition);
    
        return output;
    }
    
    // The Pixel Shader
    float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
    {
    	input.TexCoord.x = input.TexCoord.x*20.0f + sin(TotalTime*3+10)/256;
    	input.TexCoord.y = input.TexCoord.y*20.0f;
    
    	float4 color = tex2D(ColorMapSampler, input.TexCoord);
    
    	input.TexCoord.y += (sin(TotalTime*3+10)/256)+(TotalTime/16);
    	float3 normalMap = 2.0 *(tex2D(NormalMapSampler, input.TexCoord)) - 1.0;
    
    	input.TexCoord.y -= ((sin(TotalTime*3+10)/256)+(TotalTime/16))*2;
        float3 normalMap2 =(2 * (tex2D(NormalMapSampler, input.TexCoord))) - 1.0; 
    
    	normalMap = (normalMap + normalMap2) / 2;
    
    	normalMap = normalize(mul(normalMap, input.WorldToTangentSpace));
    	float4 normal = float4(normalMap,1.0);
    
    	float4 diffuse = saturate(dot(-LightDirection,normal));
    	float4 reflect = normalize(2*diffuse*normal-float4(LightDirection,1.0));
    	float4 specular = pow(saturate(dot(reflect,input.View)),28);
    
        float4 finalColor =  color * AmbientIntensity + 
    			color * DiffuseIntensity * diffuse + 
    			specular*250;
    
    	finalColor.a = 0.3f;
    	return finalColor;
    }
    
    // Our Techinique
    technique Technique1
    {
        pass Pass1
        {
            VertexShader = compile vs_2_0 VertexShaderFunction();
            PixelShader = compile ps_2_0 PixelShaderFunction();
        }
    }
    
    Danke im Vorraus für die Hilfe :)

    Samstag, 22. November 2014 10:20

Antworten

Alle Antworten