webgl实现canvas三维立方体落入沙堆里效果代码
代码语言:html
所属分类:三维
代码描述:webgl实现canvas三维立方体落入沙堆里效果代码
代码标签: webgl canvas 三维 立方体 落入 沙堆
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> * { box-sizing: border-box; } html, body { 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; } </style> </head> <body > <canvas id="canvas"></canvas> <script > /********* * made by Matthias Hurrle (@atzedent) */ /** @type {HTMLCanvasElement} */ const canvas = window.canvas; const gl = canvas.getContext("webgl2"); const dpr = Math.max(.5, .25 * window.devicePixelRatio); /** @type {Map<string,PointerEvent>} */ const touches = new Map(); const vertexSource = `#version 300 es #ifdef GL_FRAGMENT_PRECISION_HIGH precision highp float; #else precision mediump float; #endif in vec2 position; void main(void) { gl_Position = vec4(position, 0., 1.); } `; const fragmentSource = `#version 300 es /********* * made by Matthias Hurrle (@atzedent) */ #ifdef GL_FRAGMENT_PRECISION_HIGH precision highp float; #else precision mediump float; #endif uniform float time; uniform vec2 resolution; uniform vec2 touch; uniform int pointerCount; out vec4 fragColor; #define T (mod(.3*time, 100.)) #define S smoothstep #define mouse (touch/resolution) #define rot(a) mat2(cos(a),-sin(a),sin(a),cos(a)) #define rep(p,n) (mod(p,n)-.5*n) #define sunoff vec3(1,2,3) // 45 degrees #define INCLANG .78539 float rnd(vec2 p) { return fract(sin(dot(p, vec2(12.233, 78.599))) * 45019.422); } float noise(vec2 p) { p *= 3.; vec2 f = fract(p), i = floor(p); float a = rnd(i), b = rnd(i + vec2(1, 0)), c = rnd(i + vec2(0, 1)), d = rnd(i + vec2(1, 1)); vec2 u = f * f * (3. - 2. * f); return mix(a, b, u.x) + (c - a) * u.y * (1. - u.x) + (d - b) * u.y * u.x; } float smin(float a, float b, float k) { float h = clamp(.5 + .5 * (b - a) / k, .0, 1.); return mix(b, a, h) - k * h * (1. - h); } float disc(vec3 p, vec2 s, float r) { vec2 e = vec2(abs(length(p.xz)), abs(p.y)) - s; return length(max(e, .0)) + min(.0, max(e.x, e.y)) - r; } float box(vec3 p, vec3 s, float r) { p = abs(p) - s; return length(max(p, .0)) + min(.0, max(max(p.x, p.y), p.z)) - r; } float mspo(vec3 p, vec3 s, float r, float l) { const float k = .25; float d = box(p, s, r), res = d, f = 1., i = .0; for(; i < l; i++) { vec3 a = mod(p * f, 2.) - 1., r = abs(1. - 3. * abs(a)); f *= 3.; float da = max(r.x, r.y), db = max(r.y, r.z), dc = max(r.z, r.x), c = (smin(da, smin(db, dc, k), k) - 1.) / f; if(c > d) { d = c, res = c; } } return res; } float rubble(vec3 p, float n) { vec3 id = floor(p / n); p.xz = rep(p.xz, n); return disc(p - vec3(0, -.75, 0) + rnd(id.xz) * 2., vec2(.25, .5), .0); } float mat = .0; float map(vec3 p) { vec3 q = p; q.yz *= rot(INCLANG); q.xz *= rot(INCLANG); float d = 9e5, rbl = rubble(p, 4.), spo = mspo(q - vec3(.25, -.125, .25), vec3(1), .05, 3.), flr = p.y + (1. + -cos(p.z) * .25) * rnd(vec2(2.23, 8.59)); d = min(d, rbl + sin((p.x + p.x) * 1.45) * .25); d = smin(d, flr, .45); d = smin((d + noise(p.xz * .25) * .235) * .5, spo, .025); if(d == spo) mat = 1.; else mat = .0; return d; } vec3 norm(vec3 p) { vec2 e = vec2(1e-3, 0); return normalize(map(p) - vec3(map(p - e.xyy), map(p - e.yxy), map(p - e.yyx))); } float getao(vec3 p, vec3 n, float dist) { return clamp(map(p + n * dist) / dist, .0, 1.); } float getsss(vec3 p, vec3 rd, float dist, float k) { float ddist = dist * k; return clamp(map(p + rd * dist) / dist, .0, 1.) + clamp(map(p + rd * ddist) / ddist, .0, 1.); } float getshadow(vec3 ro, vec3 rd) { const float steps = 10., k = 16.; float shade = 1.; for(float i = 1e-3; i < steps;) { float d = map(ro + rd * i); if(d < 1e-3) { shade = .0; break; } shade = min(shade, k * d / i); i += d; } return shade; } void cam(inout v.........完整代码请登录后点击上方下载按钮下载查看
网友评论0