Ivo*_*tão 13 opengl math directx glsl hlsl
我在HLSL中实现了这个问题中描述的螺旋GLSL着色器,但结果并不相同.我认为这是因为mod我已经fmod在HLSL中翻译了GLSL 中的函数.我怀疑只有在fmod函数输入中有负数时才会出现此问题.
我已经尝试mod通过调用我已经制作的函数替换调用来执行GLSL文档中描述的函数并且它可以工作:
mod返回xmodulo 的值y.计算方法如下x - y * floor(x/y).
我使用的工作代码fmod是:
float mod(float x, float y)
{
return x - y * floor(x/y)
}
Run Code Online (Sandbox Code Playgroud)
与GLSL相比mod,MSDN表示 HLSL fmod功能可以做到这一点:
计算浮点余数,使得
x = i * y + f其中i,整数f具有与其相同的符号x,并且绝对值f小于绝对值y.
我使用了HLSL到GLSL转换器,并将该fmod函数翻译为mod.但是,我不知道我是否可以假设mod转换为fmod.
mod和HLSL有fmod什么区别? fmod转换为伪代码实现?uniform float time;
uniform vec2 resolution;
uniform vec2 aspect;
void main( void ) {
vec2 position = -aspect.xy + 2.0 * gl_FragCoord.xy / resolution.xy * aspect.xy;
float angle = 0.0 ;
float radius = length(position) ;
if (position.x != 0.0 && position.y != 0.0){
angle = degrees(atan(position.y,position.x)) ;
}
float amod = mod(angle+30.0*time-120.0*log(radius), 30.0) ;
if (amod<15.0){
gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 );
} else{
gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );
}
}
Run Code Online (Sandbox Code Playgroud)
struct Psl_VertexShaderInput
{
float3 pos : POSITION;
};
struct Psl_VertexShaderOutput
{
float4 pos : POSITION;
};
struct Psl_PixelShaderOutput
{
float4 Output0 : COLOR0;
};
float3 psl_positionOffset;
float2 psl_dimension;
Psl_VertexShaderOutput Psl_VertexShaderFunction(Psl_VertexShaderInput psl_input)
{
Psl_VertexShaderOutput psl_output = (Psl_VertexShaderOutput)0;
psl_output.pos = float4(psl_input.pos + psl_positionOffset, 1);
return psl_output;
}
float time : TIME;
float2 resolution : DIMENSION;
Psl_PixelShaderOutput Psl_PixelShaderFunction(float2 pos : VPOS)
{
Psl_PixelShaderOutput psl_output = (Psl_PixelShaderOutput)0;
float2 aspect = float2(resolution.x / resolution.y, 1.0);
float2 position = -aspect.xy + 2.0 * pos.xy / resolution.xy * aspect.xy;
float angle = 0.0;
float radius = length(position);
if (position.x != 0.0 && position.y != 0.0)
{
angle = degrees(atan2(position.y, position.x));
}
float amod = fmod((angle + 30.0 * time - 120.0 * log(radius)), 30.0);
if (amod < 15.0)
{
psl_output.Output0 = float4(0.0, 0.0, 0.0, 1.0);
return psl_output;
}
else
{
psl_output.Output0 = float4(1.0, 1.0, 1.0, 1.0);
return psl_output;
}
}
technique Default
{
pass P0
{
VertexShader = compile vs_3_0 Psl_VertexShaderFunction();
PixelShader = compile ps_3_0 Psl_PixelShaderFunction();
}
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*odd 17
正如你所说,它们是不同的.GLSL mod将始终具有相同的符号y而不是x.否则它是相同的 - 一个值f,x = i*y + f其中where i是一个整数和|f| < |y|.如果你想尝试某种重复模式,那么GLSL mod通常就是你想要的.
相比之下,HLSL fmod相当于x - y * trunc(x/y).它们x/y是正面的,不同的x/y是负面的.