Shader入門教程(圖文)
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
簡單幾何圖形區(qū)間(帶通)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)區(qū)間映射到(-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的自變量區(qū)間拉長 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.} 旋轉(zhuǎn)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.} 網(wǎng)格化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 編輯過 |
相關文章
正在查詢... |