简体   繁体   English

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

[英]HLSL DirectX9: Is there a getTime() function or similar?

I'm currently working on a project using C++ and DirectX9 and I'm looking into creating a light source which varies in colour as time goes on. 我目前正在开发一个使用C ++和DirectX9的项目,我正在研究创建一个随着时间的推移颜色不同的光源。

I know C++ has a timeGetTime() function, but was wondering if anyone knows of a function in HLSL that will allow me to do this? 我知道C ++有一个timeGetTime()函数,但是想知道是否有人知道HLSL中的一个函数允许我这样做?

Regards. 问候。 Mike. 麦克风。

Use a shader constant in HLSL (see this introduction ). 在HLSL中使用着色器常量(请参阅此简介 )。 Here is example HLSL code that uses timeInSeconds to modify the texture coordinate: 以下是使用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; 
} 

And then in your rendering (CPU) code before you call Begin() on the effect you should call: 然后在你的渲染(CPU)代码中,在你应该调用的效果上调用Begin()之前:

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

If you don't understand the concept of shader constants, have a quick read of the PDF. 如果您不理解着色器常量的概念,请快速阅读PDF。 You can use any HLSL data type as a constant (eg bool, float, float4, float4x4 and friends). 您可以将任何HLSL数据类型用作常量(例如bool,float,float4,float4x4和friends)。

I am not familiar with HLSL, but I am with GLSL. 我不熟悉HLSL,但我和GLSL在一起。

Shaders have no concept of 'time' or 'frames'. 着色器没有“时间”或“帧”的概念。 Vertex shader "understands" vertices to render, and pixel shader "understands" textures to render. 顶点着色器“理解”要渲染的顶点,并且像素着色器“理解”要渲染的纹理。

Your only option is to pass a variable to the shader program, in GLSL it is called a 'uniform', but I am not sure about HLSL. 你唯一的选择是将变量传递给着色器程序,在GLSL中它被称为“统一”,但我不确定HLSL。

I'm looking into creating a light source which varies in colour as time goes on. 我正在研究创造一种随着时间的推移颜色不同的光源。

There is no need to pass anything with that, though. 但是,没有必要通过任何东西。 You can directly set the light source's color (at least, you can in OpenGL). 您可以直接设置光源的颜色(至少可以在OpenGL中设置)。 Simply change the light color on the rendering scene and the shader should pick it up from the built-in uniforms. 只需更改渲染场景中的灯光颜色,着色器就应该从内置制服中拾取它。

Nope. 不。 Shaders are essentially "one-way". 着色器基本上是“单向的”。 The CPU can affect what's happening on the GPU (specify which shader program to run, upload textures and constants and such), but the GPU can not access anything on the CPU side of the fence. CPU可以影响GPU上发生的事情(指定运行哪个着色器程序,上传纹理和常量等),但GPU无法访问围栏CPU侧的任何内容。 If the GPU (and your shader) needs a piece of data, it must be set by the CPU as a constant or written as a texture (or as part of the vertex data) 如果GPU(和着色器)需要一个数据,它必须由CPU设置为常量或写为纹理(或作为顶点数据的一部分)

如果您使用HLSL为Unity编写着色器, _Time秒为单位的时间变量将公开为_Time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM