简体   繁体   English

三色片段着色器

[英]Tricolor Fragment Shader

I'm developing an animation using Three.js which simply involves an Icosahedron Buffer Geometry and a Shader Material applied together into a mesh.我正在使用 Three.js 开发一个 animation,它只涉及一个二十面体缓冲几何和一个着色器材质一起应用到一个网格中。 The goal is to get the object to resemble this:目标是让 object 类似于以下内容:

Vision for final product:最终产品的愿景:

图像

I'm new to GLSL so getting to this point hasn't been easy.我是 GLSL 的新手,所以要做到这一点并不容易。 I borrowed some code from the shaders used in this codepen.我从这个 codepen 中使用的着色器中借用了一些代码。 The issue with this shader is that the colors rendered are merely distinct outputs of Red, Green, and Blue, whereas I want three specific colors each with their own unique hex value.这个着色器的问题是,渲染的 colors 仅仅是红色、绿色和蓝色的不同输出,而我想要三个特定的 colors,每个都有自己独特的十六进制值。

Here is a snippet of the fragment shader code that I have tried:这是我尝试过的片段着色器代码片段:

uniform int u_color1;
uniform int u_color2;
uniform int u_color3;

void main() {
  float r1 = float(u_color1 / 256 / 256);
  float g1 = float(u_color1 / 256 - int(r1 * 256.0));
  float b1 = float(u_color1 - int(r1 * 256.0 * 256.0) - int(g1 * 256.0));

  float r2 = float(u_color2 / 256 / 256);
  float g2 = float(u_color2 / 256 - int(r1 * 256.0));
  float b2 = float(u_color2 - int(r2 * 256.0 * 256.0) - int(g2 * 256.0));

  float r3 = float(u_color3 / 256 / 256);
  float g3 = float(u_color3 / 256 - int(r3 * 256.0));
  float b3 = float(u_color3 - int(r3 * 256.0 * 256.0) - int(g3 * 256.0));

  vec3 color1 = vec3((r1/255.0) , (g1/255.0) , (b1/255.0) );
  vec3 color2 = vec3((r2/255.0) , (g2/255.0) , (b2/255.0) );
  vec3 color3 = vec3((r3/255.0) , (g3/255.0) , (b3/255.0) );

  vec4 outColor = vec4(r1 /256.0, g1/256.0, b1/256.0, 1.0);
  gl_FragColor = outColor;
}

Any input is appreciated.任何输入表示赞赏。 Thanks!谢谢!

Skip the integral color unforms跳过整体颜色展开

uniform int u_color1;
uniform int u_color2;
uniform int u_color3;

Use the data type vec3 for the color:使用数据类型vec3作为颜色:

uniform vec3 u_color1;
uniform vec3 u_color2;
uniform vec3 u_color3;

void main()
{
    gl_FragColor = vec4(u_color1.rgb, 1.0);
}

The components of each color can be accessed by Swizzling . Swizzling可以访问每种颜色的组件。 The read green and blue component can be get as floating value in range [0.0, 1.0] by u_color1.r , u_color1.g , u_color1.b .读取的绿色和蓝色分量可以通过u_color1.ru_color1.gu_color1.b获得 [0.0, 1.0] 范围内的浮点值。

A uniform of type vec3 c an be directly set by a THREE.Color object. vec3 c 类型的制服可以直接由THREE.Color设置。 See Uniform .制服 eg:例如:

uniforms: {
    u_color1: { value: new THREE.Color( 0xff0000 ) },
    u_color2: { value: new THREE.Color( 0x00ff00 ) },
    u_color3: { value: new THREE.Color( 0x0000ff ) }
}

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

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