Shader入门教程(图文)
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
简单几何图形区间(带通)1. float Band(float v, float start, float end) { 2. float up = step(start, v); 3. float down = 1.0 - step(end, v); 4. return up * down; 5. } 矩形1. float Rect(vec2 uv, float l, float b, float r, float t) { 2. float x = Band(uv.x, l, r); 3. float y = Band(uv.y, b, t); 4. return x * y; 5. } 圆形1. float Circle(vec2 uv, vec2 o, float r, float blur) { 2. return smoothstep(r, r-blur, distance(uv, o)); 3. } 混合叠加加减法例子:ET脸1. float ETFace(vec2 uv, vec2 o) { 2. float c = Circle(uv, vec2(.0, .0), 0.5, 0.01); 3. c -= Circle(uv, vec2(-.2, -.2), 0.2, 0.01); 4. c -= Circle(uv, vec2(.2, -.2), 0.2, 0.01); 5. return c; 6. } 坐标空间处理1. // (0, 1)区间映射到(-1, 1) 2. uv = uv * 2.0 - 1.0; 1. float Remap01(float a, float b, float t) { 2. return (t-a) / (b-a); 3. } 4. 5. 6. float Remap(float a, float b, float c, float d, float t) { 7. return Remap01(a, b, t) * (d-c) + c; 8. } 长宽适配1. void main() { 2. vec2 uv = gl_FragCoord.xy/u_resolution.xy; 3. uv = uv * 2.0 - 1.0; // 位移到以中间为原点 4. uv.x *= u_resolution.x/u_resolution.y; // 将x的自变量区间拉长 5. 6. float mask = Rect(uv, -0.5, -0.5, 0.5, 0.5); 7. vec3 color = vec3(mask); 8. gl_FragColor = vec4(color,1.0); 9. } 计算角度1. #define PI 3.141592653589793 2. 3. 4. void main() { 5. vec2 uv = gl_FragCoord.xy/u_resolution.xy; 6. uv = uv * 2.0 - 1.0; 7. uv.x *= u_resolution.x/u_resolution.y; 8. 9. float angle = atan(uv.y, uv.x); 10. angle = Remap(-PI, PI, 0., 1.0, angle); 11. vec3 color = vec3(angle); 12. gl_FragColor = vec4(color,1.0); 13.} 旋转1. #define PI 3.141592653589793 2. 3. 4. mat2 Rotate2d(float angle){ 5. return mat2(cos(angle), -sin(angle), 6. sin(angle), cos(angle)); 7. } 8. 9. 10.void main() { 11. vec2 uv = gl_FragCoord.xy/u_resolution.xy; 12. uv = uv * 2.0 - 1.0; 13. uv.x *= u_resolution.x/u_resolution.y; 14. 15. uv = Rotate2d(PI / 6.) * uv; 16. float mask = Rect(uv, -0.5, -0.5, 0.5, 0.5); 17. vec3 color = vec3(mask); 18. gl_FragColor = vec4(color,1.0); 19.} 网格化1. void main() { 2. // ... 3. uv = fract(uv * 5.); 4. float mask = Circle(uv, vec2(0.5), 0.5, 0.01); 5. // ... 6. } 噪音(随机化hash)1. float Noise1(vec2 p) { 2. return fract(sin( 3. dot(p, vec2(12.9898,78.233)) 4. ) * 43758.5453123); 5. } 6. 7. 8. float Noise2(vec2 p) { 9. p = frac(p * vec2(123.34, 345.45)); 10. p += dot(p, p + 34.345); 11. return frac(p.x * p.y); 12.} 常用链接该文章在 2024/4/1 12:46:01 编辑过 |
相关文章
正在查询... |