This project is read-only.

CustomMaterialPart for Trilinear filtering

Jun 5, 2013 at 6:03 AM
I was trying to write a custom material part to use trilinear filtering but I'm having trouble sampling the texture. I define the texture in the Material group and then try to access it from the shader. Am I doing anything incorrectly?
<MaterialGroup Texture="{ContentReference ../Textures/Terrain/grass}" xmlns="http://schemas.microsoft.com/nine/2011/xaml" >
    <CustomMaterialPart xml:space="preserve" >
                    <![CDATA[
                        texture2D Texture : TEXTURE;
                        float4 DiffuseColor : DIFFUSE = 1;
                        sampler BasicSampler { Texture = (Texture); };
        
                        void PixelShader(
                             in float4 positionWorld,
                             in float3 normal:NORMAL0,
                             out float4 diffuse:COLOR0
                        )
                        {
                            diffuse = DiffuseColor;    

                            float tighten = 0; //0.4679f;
                            float mXY = abs(normal.z) - tighten;
                            float mXZ = abs(normal.y) - tighten;
                            float mYZ = abs(normal.x) - tighten;

                            float total = mXY + mXZ + mYZ;
                            mXY /= total;
                            mXZ /= total;
                            mYZ /= total;

                            float2 xySamplePos = ((abs(positionWorld.xy) % 8) * 32) / 256;
                            float2 xzSamplePos = ((abs(positionWorld.xz) % 8) * 32) / 256;
                            float2 yzSamplePos = ((abs(positionWorld.yz) % 8) * 32) / 256;

                            float4 cXY = tex2D(BasicSampler,xySamplePos);
                            float4 cXZ = tex2D(BasicSampler,xzSamplePos);
                            float4 cYZ = tex2D(BasicSampler,yzSamplePos);

                            //Add trilinear samples together
                            float4 tex = cXY*mXY + cXZ*mXZ + cYZ*mYZ;

                            //{$AO}tex.rgb = lerp(tex.rgb, OverlayColor, tex.a);
                            //specular = tex.a;

                            //{$TE}{$AO}diffuse.rgb *= tex.rgb;
                            //{$TE}{$AS}diffuse.rgb *= tex.rgb;
                            diffuse.rgb = tex.rgb * 100;
                            //{$TE}{$AA}diffuse *= tex;
                        }
                    ]]>
    </CustomMaterialPart>
    <!--<ShadowMapMaterialPart />-->
    <DirectionalLightMaterialPart />
</MaterialGroup>
Jun 6, 2013 at 1:48 AM
Think I figured it out, not getting the world position in, but the texture works now.
<CustomMaterialPart xml:space="preserve" >
                    <![CDATA[
                        texture Diffuse : DIFFUSE;
                        sampler2D DiffuseSampler = sampler_state
                        {
                            Texture = <Diffuse>;
                        };
        
                        void PixelShader(
                             float4 positionWorld,
                             float3 normal:NORMAL0,
                             out float4 diffuse:COLOR0
                        )
                        {
                            diffuse = 1;    

                            float tighten = 0; //0.4679f;
                            float mXY = abs(normal.z) - tighten;
                            float mXZ = abs(normal.y) - tighten;
                            float mYZ = abs(normal.x) - tighten;

                            float total = mXY + mXZ + mYZ;
                            mXY /= total;
                            mXZ /= total;
                            mYZ /= total;

                            float2 xySamplePos = ((abs(positionWorld.xy) % 8) * 32) / 256;
                            float2 xzSamplePos = ((abs(positionWorld.xz) % 8) * 32) / 256;
                            float2 yzSamplePos = ((abs(positionWorld.yz) % 8) * 32) / 256;

                            float4 cXY = tex2D(DiffuseSampler,xySamplePos);
                            float4 cXZ = tex2D(DiffuseSampler,xzSamplePos);
                            float4 cYZ = tex2D(DiffuseSampler,yzSamplePos);

                            //Add trilinear samples together
                            float4 tex = cXY*mXY + cXZ*mXZ + cYZ*mYZ;

                            //{$AO}tex.rgb = lerp(tex.rgb, OverlayColor, tex.a);
                            //specular = tex.a;

                            //{$TE}{$AO}diffuse.rgb *= tex.rgb;
                            //{$TE}{$AS}diffuse.rgb *= tex.rgb;
                            diffuse = tex;
                            //{$TE}{$AA}diffuse *= tex;
                        }
                    ]]>
    </CustomMaterialPart>