webgl实现canvas三维晶莹剔透反射折射动画效果代码

代码语言:html

所属分类:三维

代码描述:webgl实现canvas三维晶莹剔透反射折射动画效果代码

代码标签: webgl canvas 三维 晶莹剔透 反射 折射 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>

  
  
  
<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(1, .5 * 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

out vec4 fragColor;

uniform vec2 resolution;
uniform vec2 touch;
uniform float time;
uniform int pointerCount;

const vec3 lcol=vec3(1,.5,.3).rbg*.0+1.;

const vec3 gold = vec3(255, 233, 152) / 255.;
const vec3 bron = vec3(87, 55, 13) / 255.;
vec3 tint = mix(bron, gold, .5);

#define PI 3.141592
#define TAU 6.283836
#define ESN 1.570796

#define P pointerCount
#define T mod(time*2.,300.)
#define S smoothstep
#define mouse (touch/resolution)
#define rot(a) mat2(cos(a),-sin(a),sin(a),cos(a))
#define syl(p,s) (length(p)-s)
#define noise(p) (.5+.5*sin(p.x*1.5)*sin(p.y*1.5))

float rnd(vec2 p) {
  return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453123);
}

float fbm(vec2 p) {
  float f = .0;
  mat2 m = mat2(1.6, 1.2, -1.2, 1.6);

  f += .500000*noise(p); p *= m;
  f += .250000*noise(p); p *= m;
  f += .125000*noise(p); p *= m;
  f += .062500*noise(p); p *= m;
  f += .015625*noise(p);

  return f;
}

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 ftor(vec3 p, vec3 s, float r) {
  vec2 e = vec2(
    abs(length(p.xz)-s.x)-s.z,
    abs(p.y)-s.y
  );

  return length(max(e,.0))+
  min(.0, max(e.x, e.y))-r;
}

float dsc(vec3 p, vec2 s, float r) {
  vec2 e = vec2(
    abs(length(p.xz)-s.x),
    abs(p.y)
  ) - s;

  return length(max(e,.0))+
  min(.0, max(e.x, e.y))-r;
}

float mat = .0;
float map(vec3 p, float scene) {
  float n = 6.;

  p.xz = (p.xz-n*clamp(round(p.xz/n), -1.,1.));

  vec3 q = p;
  q.y += (scene<2.?2.:6.)*T+(scene < 2.?cos(T)+2.75:.0);
  q.y = mod(q.y, 5.)-2.5;

  float d = 5e5,
  core = abs(dsc(p, vec2(.4,.7),.005))-3e-3,
  shell = ftor(q, vec3(1.2,.75,.1),.005),
  obj = shell;

  d = min(d, min(obj, core));

  if (d == core) mat = 1.;
  else mat = .0;

  return d;
}

vec3 norm(vec3 p, float scene) {
  vec2 e = vec2(1e-3, 0);
  float d = map(p, scene);
  vec3 n = d-vec3(
    map(p-e.xyy, scene),
    map(p-e.yxy, scene),
    map(p-e.yyx, scene)
  );

  return normalize(n);
}

void cam(inout vec3 p, float scene) {
  if (P > 0) {
    p.yz *= rot(-mouse.y*PI+ESN);
    p.xz *= rot(PI-mouse.x*TAU);
  } else {
    vec3 q = p;
    p.yz *= rot(sin(T*.5)*.25+.75);
    p.xz *= rot(T*.25);

    if (scene == .0) {
      p.xy *= rot(sin(T*.5)*.25+.25);
    } else if (scene == 2.) {
      p = q;
      p.yz *= rot(TAU+ESN);
      p.xz *= rot(T*.05);
    }
  }
}

void main(void) {
  vec2 uv = (
    gl_FragCoord.xy-.5*resolution
  )/min(resolution.x, resolution.y);

  if (abs(uv.y) > .4 || abs(uv.x) > .9) {
    fragColor = vec4(vec3(0), 1);
    return;
  }

  // anim
  vec2 st = abs(uv);
  float
  num = 3.,
  t = .125*T,
  prog = t*.25,
  preanim = P>0?.0:floor(mod(prog, num));
  prog += max(st.x, st.y) * .08;

  float
  anim = mod(prog, num),
  scene = P>0?.0:floor(anim);

  vec3 col = vec3(0),
  ro = vec3(
    0,
    (P > 0?.0: (scene < 2.?scene*-2.:2.95)),
    (P > 0?-3.:exp(-cos(T*(scene<2.?.25:.5)))-(5.+13.*(scene < 2.?scene:.9)))*1.125
  ),
  rd = normalize(vec3(uv, 1));
  cam(ro, scene);
  cam(rd, scene);

  vec3 p = ro;

  const float steps = 400.,maxd = 40.;
  float dd = .0,
  ii = .0,
  at = .0,
  bnz = .0,
  side = 1.,e = 1.;

  for (float i = .0; i < steps; i++, ii = i) {
    float d = map(p, sc.........完整代码请登录后点击上方下载按钮下载查看

网友评论0