HLSL DirectX9:是否有getTime()函数或类似函数?

Mik*_*ike 3 c++ directx time function hlsl

我目前正在开发一个使用C++和DirectX9的项目,我正在研究创建一个随着时间的推移颜色不同的光源.

我知道C++有一个timeGetTime()函数,但想知道是否有人知道HLSL中的一个函数允许我这样做?

问候.麦克风.

Jus*_*cle 6

在HLSL中使用着色器常量(请参阅此简介).以下是使用timeInSeconds修改纹理坐标的示例HLSL代码:

// HLSL
float4x4 view_proj_matrix; 
float4x4 texture_matrix0; 
// My time in seconds, passed in by CPU program
float    timeInSeconds;

struct VS_OUTPUT 
{ 
   float4 Pos     : POSITION; 
   float3 Pshade  : TEXCOORD0; 
}; 


VS_OUTPUT main (float4 vPosition : POSITION) 
{ 
   VS_OUTPUT Out = (VS_OUTPUT) 0;  

   // Transform position to clip space 
   Out.Pos = mul (view_proj_matrix, vPosition); 

   // Transform Pshade 
   Out.Pshade = mul (texture_matrix0, vPosition);

   // Transform according to time
   Out.Pshade = MyFunctionOfTime( Out.Pshade, timeInSeconds );

   return Out; 
} 
Run Code Online (Sandbox Code Playgroud)

然后在你的渲染(CPU)代码中,在你应该调用的效果上调用Begin()之前:

// C++
myLightSourceTime = GetTime(); // Or system equivalent here:
m_pEffect->SetFloat ("timeInSeconds ", &myLightSourceTime); 
Run Code Online (Sandbox Code Playgroud)

如果您不理解着色器常量的概念,请快速阅读PDF.您可以将任何HLSL数据类型用作常量(例如bool,float,float4,float4x4和friends).