限制在使用Sin函数时,GLSL中的u_time不会低于0.5。

huangapple go评论51阅读模式
英文:

Restrict GLSL u_time from falling below 0.5 when using it with Sin

问题

以下是您提供的GLSL代码的翻译部分:

void main() {
	vec2 uv = gl_FragCoord.xy / u_resolution.xy * 10.0;
	
	vec3 color = vec3(0.0);
	
	float wave_width = sin(u_time); //这是我希望u_time受限制的地方
	uv.y -= 4.0;
	
	uv.y += (0.2 * sin(uv.x + u_time));
	wave_width *= abs(1.0 / (10.0 * uv.y));
	color += vec3(wave_width, 0.0 , 0.0);
	
	gl_FragColor = vec4(color, 1.0);
}

请注意,这段代码中没有实际翻译的部分,只是代码本身。如果您有其他问题或需要进一步帮助,请告诉我。

英文:

I am in the process of learning glsl and I am having trouble restricting the u_time from falling below 0.5. I'd like the range of it to be from 0.5-1.0 with sin. Here is my code below where I have a line moving by sin. The restriction of where I want the u_time is labeled. It is for a glow effect to the line.

void main() {
	vec2 uv = gl_FragCoord.xy / u_resolution.xy * 10.0;
	
	vec3 color = vec3(0.0);
	
	float wave_width = sin(u_time); //Here is where I want the u_time restricted
	uv.y -= 4.0;
	
	uv.y += (0.2 * sin(uv.x + u_time));
	wave_width *= abs(1.0 / (10.0 * uv.y));
	color += vec3(wave_width, 0.0 , 0.0);
	
	
	
	gl_FragColor = vec4(color, 1.0);
}

答案1

得分: 1

函数 sin 返回一个在范围 [-1, 1] 内的数。将这个数乘以 0.25 会得到一个在范围 [-0.25, 0.25] 内的数。如果您然后加上 0.75,您会得到一个在范围 [0.5, 1.0] 内的数:

float wave_width = sin(u_time) * 0.25 + 0.75;
英文:

The function sin returns a number in the range [-1, 1]. Scaling this number by 0.25 gives a number in the range [-0.25, 0.25]. If you then add 0.75, you get a number in the range [0.5, 1.0]:

float wave_width = sin(u_time) * 0.25 + 0.75;

huangapple
  • 本文由 发表于 2023年4月11日 14:11:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定