The following tutorial is original from the technical hut of turtles and donkeys.ArticleSource, and keep this paragraph, do not infringe. Shader learning experience 2: insert snippets of FX function, and texture: 2.1 adds texture for our shader
Step 1:In ourEmpty EffectMedium:
Float4x4 worldviewproj: worldviewprojection;
Right-click the following line,Select"Insert snippets"
Select"Texture and sampler"
InNameDouble-click,ChangeDiffuse,So all"Name"It is automatically changed"Diffuse"Now
Step 2: AddProgramSegment,MakeCodeChange
Float4x4 worldviewproj: worldviewprojection;
Texture diffuse <
String resourcename = ""; // optional default file name
String uiname = "diffuse texture ";
String resourcetype = "2D ";
>;
Sampler2d diffusesampler = sampler_state {
Texture = <diffuse>;
Minfilter = Linear;
Magfilter = Linear;
Mipfilter = Linear;
Addressu = wrap;
Addressv = wrap;
};
Struct vs_output
{
Float4 pos: position;
Float2 TEX: texcoord0;
};
Vs_output mainvs (float4 pos: Position, float2 TEX: texcoord0 ){
Vs_output vsout;
Vsout. Pos = MUL (Pos, worldviewproj );
Vsout. Tex = Tex;
Return vsout;
}
Float4 mainps (float2 TEX: texcoord0): Color {
Return tex2d (diffusesampler, Tex );
}
Technique technique0 {
Pass P0 {
Vertexshader = compile vs_3_0 mainvs ();
Pixelshader = compile ps_3_0 mainps ();
}
}
Step 2:Select ourEmpty EffectFile,In the upper-right cornerPropertyTableParameterIn"Diffuse texture"Select a texture file you like.
Step 3:PressCTRL + F7,CompileShader,ThenShaderDrag to the object we want to display
2.2 explain the meaning of the Code
Texture diffuse <
String resourcename = ""; // optional default file name
String uiname = "diffuse texture ";
String resourcetype = "2D ";
>;
Many people may<... >The content in,In factDirectXIs of little use,The main function is to makeFX composerCan identify our parameters,In addition, the parameter panel displays,Easy to modify
Return tex2d (diffusesampler, Tex );
AccordingDiffusesampleSamplezer,PairTexInterpolation of corresponding texture coordinates,If you do not understand this Code clearly,Please search for this by yourselfHLSLRelated books.The number of references starting with Chapter 1 may be helpful to you
2.3 Running Effect