【weblg threejs shader】2.色彩与线条还有简单的参数

前言

glsl语言通常由通道组成,每一个色彩通道由r,g,b,a颜色和透明度通道组成,取值范围1到0,
而着色位置由x,y和z三个坐标组成,这个取值比较随意。
由于空间表示过于抽象,我先介绍色彩通道,然后再介绍空间坐标。
首先请看上篇文章的色彩部分

const fragment = `
${ShaderChunk.logdepthbuf_pars_fragment}
precision mediump float;
varying vec2 vUv;
uniform float uTime;

void main() {
    gl_FragColor =vec4(0.0,1.0,1.0,.5);
  ${ShaderChunk.logdepthbuf_fragment}
}
    `;

此时每个通道的颜色都是固定的颜色,我们并没有拿到什么特殊的参数去改变它的颜色,所以他的颜色就是他本来的样子:
s

gl_FragColor =vec4(0.0,1.0,1.0,.5);

我们来尝试输入一些参数,让色彩变化起来 ,比如随着画布位置的改变调整它的颜色。这里我需要介绍一下我代码里面的vUv参数,他是由顶点着色器uv参数继承而来,

varying vec2 vUv;

在顶点函数中我们只要使用uv获取,并不需要额外声明,因为他是原生变量,反应的是着色器每个点的位置,从(0,0)到(1,1)的值,也就是画布最中心的坐标是(0.5,0.5)。
现在我们让红色r,黄色g,和蓝色b随着x,y的变动变化起来,只需要替换原来的数字就行了,然后透明通道alpha给1,表示透明度为0

const fragment = `
${ShaderChunk.logdepthbuf_pars_fragment}
precision mediump float;
varying vec2 vUv;
uniform float uTime;

void main() {
    vec2 uv = vUv;
    gl_FragColor =vec4(uv.x,uv.y,1.-uv.x*uv.y,1.);
  ${ShaderChunk.logdepthbuf_fragment}
}
    `;

normal
现在看起来是不是比刚才好看一点,现在我们再尝试使用time参数实时修改他的xy值,同事使用fract函数获取time的小数点后几位,保证变化值在0-1之间循环。

void main() {
    vec2 uv = vUv;
    gl_FragColor =vec4(uv.x+fract(uTime/10.),uv.y+fract(uTime/10.),1.-uv.x*uv.y,1.);
  ${ShaderChunk.logdepthbuf_fragment}
}

time

现在我想你已经为glsl的空间坐标,色彩等等工作方式有一定了解来,我们可以加入更多“规律”点的东西来调整画面,比如画一些函数:

y=x

没有比这个更简单的一元函数了。但为了表示他我们需要在主函数main外重新声明一个函数

	float plot(vec2 st, float pct){
  		return  smoothstep( pct-0.05, pct, st.y) -
          			smoothstep( pct, pct+0.05, st.y);
}

smoothstep函数可以返回他参数1和参数2之间对于参数3的差值,如果超过了这个范围参数1,返回0,超过了参数2则返回1,而这里plot返回的两个数相减和反转参数1和2,则是为了输出当前数与st.y±0.05之间的数。这样线条就有了一定的宽度,当然没有宽度的线条是看不到的。

	float plot(vec2 st, float pct){
  			return  smoothstep( pct-0.05, pct, st.y) -
          			smoothstep( pct, pct+0.05, st.y);
	}
void main() {
    vec2 uv = vUv;
    float y = uv.y;
    float x = uv.x;
    y=x;
    float pct = plot(uv,y);
    gl_FragColor =vec4(uv.x+fract(uTime/10.),uv.y+fract(uTime/10.),1.-uv.x*uv.y,pct);
  ${ShaderChunk.logdepthbuf_fragment}
}

只要将透明通道上的值替换为当前线段y的值这样我们就看到了一个y关于参数x的函数出现在屏幕上,
s

当然你看到了。线条两边是发虚的,这就是smoothstep的作用,如果还不明白可以观察下面两个截图,第一个是plot前半段的图像,第二个是后半段的图像,我们可以看到他们分别从
正向左到右y=x+.5衰减

反向右往左y=x-.5增强
他们的差就最终形成了一个中间深两边浅的线条
正向递减
反向递增

在你掌握了这些内容后,你已经可以随心所欲的创建自己的图形了,在下一章我会讨论更多关于函数和图形混合的信息,毕竟透明通道不是万能的。

  • 19
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Three.js is a JavaScript library used for creating 3D graphics in the browser. It provides a set of built-in shaders that can be used to create various visual effects like reflections, refractions, shadows, and more. Three.js shaders are written in GLSL (OpenGL Shading Language), a C-like language used for programming graphics processing units (GPUs). To create a custom shader in Three.js, you need to define a new shader material and pass in the GLSL code as a string. Here is an example of a simple custom shader that applies a color gradient based on the object's Y position: ``` var customShader = new THREE.ShaderMaterial({ uniforms: { color1: { value: new THREE.Color(0xff0000) }, color2: { value: new THREE.Color(0x0000ff) } }, vertexShader: ` varying vec3 vPosition; void main() { vPosition = position; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); } `, fragmentShader: ` uniform vec3 color1; uniform vec3 color2; varying vec3 vPosition; void main() { float t = (vPosition.y + 1.0) / 2.0; gl_FragColor = vec4(mix(color1, color2, t), 1.0); } ` }); var cube = new THREE.Mesh( new THREE.BoxGeometry(1, 1, 1), customShader ); scene.add(cube); ``` This shader defines two uniforms (color1 and color2) that represent the two colors of the gradient. In the vertex shader, we pass the vertex position to the fragment shader via a varying variable. In the fragment shader, we calculate the gradient value (t) based on the Y position of the vertex and use the mix() function to interpolate between the two colors. Finally, we set the output color using gl_FragColor.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸢_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值