Share via


효과 작성(Direct3D 9)

효과를 작성하려면 효과 구문을 이해하고 필요한 상태 정보를 생성해야 합니다. 셰이더 상태, 텍스처 및 샘플러 상태 및 애플리케이션에 필요한 모든 파이프라인 상태를 효과에 추가할 수 있습니다.

효과 구문은 효과 형식(Direct3D 9)에 자세히 설명되어 있습니다. 효과는 일반적으로 효과 파일(.fx)에 캡슐화되지만 애플리케이션에서 텍스트 문자열로 작성될 수도 있습니다.

효과 예제

효과에는 셰이더 상태, 텍스처 및 샘플러 상태, 파이프라인 상태의 세 가지 상태 유형이 포함됩니다. 다음은 BasicHLSL 샘플의 효과 예입니다.

// Global variables
float4 g_MaterialAmbientColor;      // Material's ambient color
float4 g_MaterialDiffuseColor;      // Material's diffuse color
int g_nNumLights;

float3 g_LightDir[3];               // Light's direction in world space
float4 g_LightDiffuse[3];           // Light's diffuse color
float4 g_LightAmbient;              // Light's ambient color

texture g_MeshTexture;              // Color texture for mesh

float    g_fTime;                   // App's time in seconds
float4x4 g_mWorld;                  // World matrix for object
float4x4 g_mWorldViewProjection;    // World * View * Projection matrix
// Texture samplers
sampler MeshTextureSampler = 
sampler_state
{
    Texture = <g_MeshTexture>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};
struct VS_OUTPUT
{
    float4 Position   : POSITION;   // vertex position 
    float2 TextureUV  : TEXCOORD0;  // vertex texture coords 
    float4 Diffuse    : COLOR0;     // vertex diffuse color
};
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0,
                         uniform int nNumLights,
                         uniform bool bTexture,
                         uniform bool bAnimate )
{
    VS_OUTPUT Output;
    float3 vNormalWorldSpace;
  
    float4 vAnimatedPos = vPos;
    
    // Animation the vertex based on time and the vertex's object space position
    if( bAnimate )
        vAnimatedPos += float4(vNormal, 0) * (sin(g_fTime+5.5)+0.5)*5;
    
    // Transform the position from object space to homogeneous projection space
    Output.Position = mul(vAnimatedPos, g_mWorldViewProjection);
    
    // Transform the normal from object space to world space    
    vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld));
    
    // Compute simple directional lighting equation
    float3 vTotalLightDiffuse = float3(0,0,0);
    for(int i=0; i < nNumLights; i++ )
        vTotalLightDiffuse += g_LightDiffuse[i] * max(0,dot(vNormalWorldSpace, g_LightDir[i]));
        
    Output.Diffuse.rgb = g_MaterialDiffuseColor * vTotalLightDiffuse + 
                         g_MaterialAmbientColor * g_LightAmbient;   
    Output.Diffuse.a = 1.0f; 
    
    // Just copy the texture coordinate through
    if( bTexture ) 
        Output.TextureUV = vTexCoord0; 
    else
        Output.TextureUV = 0; 
    
    return Output;    
}
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};
PS_OUTPUT RenderScenePS( VS_OUTPUT In,
                         uniform bool bTexture ) 
{ 
    PS_OUTPUT Output;

    // Lookup mesh texture and modulate it with diffuse
    if( bTexture )
        Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;
    else
        Output.RGBColor = In.Diffuse;

    return Output;
}
technique RenderSceneWithTexture1Light
{
    pass P0
    {          
        VertexShader = compile vs_1_1 RenderSceneVS( 1, true, true );
        PixelShader  = compile ps_1_1 RenderScenePS( true ); 
    }
}

이 효과에는 다음이 포함됩니다.

  • 꼭짓점 셰이더 및 픽셀 셰이더와 연결된 모든 상태인 셰이더 상태입니다. 이는 꼭짓점 및 픽셀 셰이더 함수, 필요한 전역 변수 및 입력 및 출력 데이터 구조에 의해 정의되며, 모두 여기에 나열되어 있습니다.

    struct VS_OUTPUT
    {  ...  };
    VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
                             ...
    {  ...  }
    
    struct PS_OUTPUT
    {  ...  };
    PS_OUTPUT RenderScenePS( VS_OUTPUT In,
                             uniform bool bTexture ) 
    {  ...  }
    
  • 텍스처 개체 및 샘플러 개체의 전역 변수인 텍스처 및 샘플러 상태:

    texture g_MeshTexture;              // Color texture for mesh
    
    sampler MeshTextureSampler = 
    sampler_state
    {
       ...
    };
    
  • 다른 효과 상태입니다. 이 예제에는 명시적으로 사용되는 다른 효과 상태가 없습니다. 있는 경우 적용된 패스 내의 기술이 될 것입니다.

    technique RenderSceneWithTexture1Light
    {
        pass P0
        {          
            // Any other effect state can be set here.
    
            VertexShader = compile vs_1_1 RenderSceneVS( 1, true, true );
            PixelShader  = compile ps_1_1 RenderScenePS( true ); 
        }
    }
    
    

효과에는 하나 이상의 기술 및 패스가 포함됩니다.

효과 렌더링 옵션은 기술과 패스를 추가하여 제어됩니다.

더 복잡한 렌더링 효과를 용이하게 하기 위해 추가 패스로 효과를 만들 수 있습니다. 기술은 임의의 패스 수를 지원합니다.

technique T0
{
    pass P0
    { ... }

    pass P1
    { ... }

    ...

    pass Pn
    { ... }
}

임의 수의 기술로 효과를 만들 수도 있습니다.

technique T0
{
    pass P0
    { ... }
}

technique T1
{
    pass P0
    { ... }

    pass P1
    { ... }
}

...

technique Tn
{
    pass P0
    { ... }
}

technique TVertexShaderOnly
{
    pass P0
    {
        VertexShader =
        asm
        {
         // assembly-language shader code goes here
         ...
        };
    }
}

기술과 패스는 임의의 이름을 지정할 수 있습니다.

효과