webgl实现三维不锈钢镜面反射效果代码

代码语言:html

所属分类:三维

代码描述:webgl实现三维不锈钢镜面反射效果代码

代码标签: webgl 三维 不锈钢 镜面 反射

下面为部分代码预览,完整代码请点击下载或在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 >

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 float time;
uniform int pointerCount;
uniform vec2 touch;

#define T time
#define S smoothstep
#define PHI (.5+.5*sqrt(5.))
#define PI  3.14159
#define TAU 6.28318
#define RO   .78539
#define mouse (touch/resolution)
#define rot(a) mat2(cos(a),-sin(a),sin(a),cos(a))

float softmix(vec3 a, vec3 b) {
    float c=dot(a,b)*.5+.5;

    return c*c;
}

vec3 light(vec3 p) {
    vec3 col=
    + softmix(p, normalize(vec3(0,5,0)))
        * vec3(1,3,4)
    + softmix(p, normalize(vec3(0,-5,0)))
        * vec3(3,1,1)/3.
    + softmix(p, normalize(vec3(0,0,-5)))
        * vec3(3,0,3)/3.
    + vec3(0.125);

    return col*.25;
}

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 rnd(vec2 p) {
  return fract(
    sin(
      dot(
        p,
        vec2(12.9898, 78.233)
      )
    )*43758.5453123
  );
}

float noise(vec2 p) {
  float dist=1.;
  vec2 i=floor(p), f= fract(p);

  for(float x=-1.; x<=1.; x++)
  for(float y=-1.; y<=1.; y++) {
      float d=distance(rnd(i+vec2(x,y))+vec2(x,y),f);
      dist=smin(dist,S(.0,1.,d),.1);
  }

  return dist;
}

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 dode(vec3 p, float s) {
  float d=0.;
  d=max(d,abs(dot(p,normalize(vec3(0,PHI,1)))));
  d=max(d,abs(dot(p,normalize(vec3(0,-PHI,1)))));
  d=max(d,abs(dot(p,normalize(vec3(1,0,PHI)))));
  d=max(d,abs(dot(p,normalize(vec3(-1,0,PHI)))));
  d=max(d,abs(dot(p,normalize(vec3(PHI,1,0)))));
  d=max(d,abs(dot(p,normalize(vec3(-PHI,1,0)))));

  return d-s;
}


float mat=.0;
float map(vec3 p) {
  vec3 q=p;
  q.xy*=rot(RO);
  
  float d=5e5,
  room=-dode(p,6.),
  bx=box(
      q+noise((p-vec3(50.,40.,0)).xy*2.)*125e-4
      ,vec3(2,2,5e-3),
      5e-3
    );

  d=min(d,room);
  d=min(d,bx);

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

  return d;
}

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

  return normalize(n);
}

float tween(float a) {
  return a*a*a*a*a;
}

void cam(inout vec3 p) {
  if(pointerCount > 0) {
    p.yz*=rot(-mouse.y*PI+PI*.5);
    p.xz*=rot(PI-mouse.x*TAU);
  } else {
    p.yz *= rot(tween(min(.0,-cos(T*.8)*.9)));
    p.xz *= rot(tween(-cos(T*.4))*PI);
  }
}

void main(.........完整代码请登录后点击上方下载按钮下载查看

网友评论0