[1.17.1]Minecraft资源包-着色器实例

最近学了一些着色器的使用,扒教程时由于没有编程相关的基础,看GLSL的一些东西确实费劲。
现在的水平也只是会改改数据而已,做出自己想要的效果还不能实现。
另外虽然这是资源包的部分,但是我写这个毕竟是为了数据包的效果服务,所以tag还是commands。

效果演示

B站视频演示:

https://www.bilibili.com/video/BV1bb4y1r7Q1

演示1

大致原理

利用全局量GameTime来实现动画效果。
GameTime是一个从0增加到1200并循环的量,配合三角函数就能实现-1到1的循环变化,
然后在相关核心着色器当中引入GameTime来实现rgb变色。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#minecraft/shaders/core/rendertype_lines.json
{
"blend": {
"func": "add",
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "rendertype_lines",
"fragment": "rendertype_lines",
"attributes": [
"Position",
"Color",
"Normal"
],
"samplers": [
],
"uniforms": [
{ "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] },
{ "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] },
{ "name": "ColorModulator", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] },
{ "name": "LineWidth", "type": "float", "count": 1, "values": [ 1.0 ] },
{ "name": "ScreenSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] },
{ "name": "FogStart", "type": "float", "count": 1, "values": [ 0.0 ] },
{ "name": "FogEnd", "type": "float", "count": 1, "values": [ 1.0 ] },
{ "name": "FogColor", "type": "float", "count": 4, "values": [ 0.0, 0.0, 0.0, 0.0 ] },
{ "name": "GameTime", "type": "float", "count": 1, "values": [0.0] }
]
}

能够看到在uniforms最后引入了GameTime。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#minecraft/shaders/core/rendertype_lines.fsh

#version 150

#moj_import <fog.glsl>

uniform vec4 ColorModulator;
uniform float FogStart;
uniform float FogEnd;
uniform vec4 FogColor;
uniform float GameTime;

in float vertexDistance;
in vec4 vertexColor;

out vec4 fragColor;

void main() {
vec4 color = vertexColor * ColorModulator;
vec4 line_color = vec4(sin(GameTime * 2000), cos(GameTime * 2000), tan(GameTime * 2000), 0.0) + color;

fragColor = linear_fog(line_color, vertexDistance, FogStart, FogEnd, FogColor);
}

在相应的片段着色器(rendertype_lines,用以修改实体轮廓和方块边缘轮廓)中引入GameTime,然后引入一个四元向量line_color,用GameTime和三角函数做出循环变量,然后和原来的color相加。
其实应该可以直接改color的,但是我试了一下效果不是很好(?),不知道为什么。
同时我这个三角函数也是瞎套的,变色范围没有黄色,但是效果看起来还是不错的XD。
还有另一个挖方块变色的原理是一样的,修改片段着色器(rendertype_crumbling)来实现的。

鸣谢

  • 所有的着色器教程
  • qlkyyds
  • vsc的glsl语法高亮

resource

链接: https://pan.baidu.com/s/1gppn4DX5NKTH11Ke1XTL_Q
提取码: a35q

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2020-2021 Argon_gas

请我喝杯咖啡吧~