webgl+canvas实现三维可旋转空间圆环效果代码
代码语言:html
所属分类:三维
代码描述:webgl+canvas实现三维可旋转空间圆环效果代码
代码标签: webgl canvas 三维 旋转 空间 圆环
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * { box-sizing: border-box; } html, body { position: relative; margin: 0; min-height: 100vh; overflow: hidden; background: repeating-radial-gradient( circle at center, #444 0 10%, #111 10% 20% ); touch-action: none; } canvas { width: 100%; height: auto; object-fit: contain; } button { padding: 0.5rem; font-size: 1rem; background: #444; border: 2px solid; border-radius: 0.5rem; justify-self: end; } kbd { background-color: #eee; border-radius: 3px; border: 1px solid #b4b4b4; color: #333; display: inline-block; font-size: 0.85em; font-weight: 700; line-height: 1; padding: 2px 4px; white-space: nowrap; box-shadow: 0 1px 1px #0001, 0 2px 0 0 #fffb inset; } #dialog { user-select: none; position: fixed; inset: 0; padding: 1em 2em 3em 2em; border-radius: 4px; font-family: system-ui; line-height: 1.6; } .x { filter: grayscale(1); border: none; background: none; position: absolute; top: 0; right: 0.4rem; cursor: pointer; } .x:hover, .x:focus-visible { outline: none; box-shadow: 0 1px 1px 1px #1111; } </style> </head> <body > <canvas id="canvas"></canvas> <script > /********* * made by Matthias Hurrle (@atzedent) */ /** @type {HTMLCanvasElement} */ const canvas = window.canvas; const gl = canvas.getContext('webgl'); const dpr = window.devicePixelRatio; const vertexSource = ` #ifdef GL_FRAGMENT_PRECISION_HIGH precision highp float; #else precision mediump float; #endif attribute vec2 position; void main(void) { gl_Position = vec4(position, 0., 1.); } `; const fragmentSource = ` /********* * made by Matthias Hurrle (@atzedent) */ #ifdef GL_FRAGMENT_PRECISION_HIGH precision highp float; #else precision mediump float; #endif uniform vec2 resolution; uniform float time; uniform vec2 pointers[10]; #define MAX_STEPS 100 #define MAX_DIST 100. #define SURF_DIST .001 #define T time * .25 #define mouse pointers[0] mat2 Rot(float a) { float s = sin(a), c = cos(a); return mat2(c, -s, s, c); } float sdBoxFrame(vec3 p, vec3 b, float e, float r) { p = abs(p ) - b; vec3 q = abs(p+e) - e; return min(min( length( max(vec3(p.x, q.y, q.z), .0)) + min(max(p.x, max(q.y, q.z)), .0) - r, length( max(vec3(q.x, p.y, q.z), .0)) + min(max(q.x, max(p.y, q.z)), .0) - r), length( max(vec3(q.x, q.y, p.z), .0)) + min(max(q.x, max(q.y, p.z)), .0) - r); } float sdRoundBox(vec3 p, vec3 b, float r) { vec3 q = abs(p) - b; return length( max(q, .0)) + min(max(q.x, max(q.y, q.z)), .0) - r; } float sdOctahedron( vec3 p, float s) { p = abs(p); return (p.x + p.y + p.z - s) * .57735027; } float sdTorus(vec3 p, vec2 t) { vec2 q = vec2(length(p.xz) - t.x, p.y); return length(q) - t.y; } float sdSphere(vec3 p, float s) { return length(p) - s; } float opSmoothUnion(float d1, float d2, float k) { float h = clamp(.5 + .5 * (d2 - d1) / k, .0, 1.); return mix(d2, d1, h) - k * h * (1. - h); } float opSmoothSubtraction(float d1, float d2, float k) { float h = clamp(.5 - .5 * (d2 + d1) / .........完整代码请登录后点击上方下载按钮下载查看
网友评论0