wtc-gl打造一个三维管道正方体效果代码

代码语言:html

所属分类:三维

代码描述:wtc-gl打造一个三维管道正方体效果代码

代码标签: 三维 管道 正方体 效果

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

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

<head>

  <meta charset="UTF-8">

  
<style>
body {
  background: #333;
  color: #fff;
  font-family: sans-serif;
}
body,
html {
  margin: 0;
  overflow: hidden;
  padding: 0;
}
canvas {
  width:100%;
  height:100%;
  }
</style>



</head>

<body>
  <script type="text/fragment" id="vertShader">
  attribute vec3 position;
  attribute vec3 normal;
  attribute vec2 uv;
  uniform mat4 u_viewMatrix;
  uniform mat4 u_modelMatrix;
  uniform mat4 u_modelViewMatrix;
  uniform mat4 u_projectionMatrix;
  uniform mat3 u_normalMatrix;
  varying vec3 vNormal;
  varying vec3 vPosition;
  varying vec3 vWorldPosition;
  varying vec2 vUV;
  
  void main() {
    vNormal = normalize(u_normalMatrix * normal);
    vUV = uv;
    vec4 camPos = u_projectionMatrix * u_modelViewMatrix * vec4(position, 1.0);
    
    gl_Position = camPos;
    
    vWorldPosition = (u_modelMatrix * vec4(0,0,0, 1.0)).xyz;
    vPosition = (u_modelMatrix * vec4(position, 1.0)).xyz;
  }
</script>
<script type="text/fragment" id="fragShader">
  precision highp float;
  varying vec3 vNormal;
  varying vec2 vUV;
  varying vec4 camPos;
  varying vec3 vPosition;
  varying vec3 vWorldPosition;
  uniform vec3 u_cameraPosition;
  
  uniform mat4 u_matrix;
  
  /* Raymarching constants */
  /* --------------------- */
  const float MAX_TRACE_DISTANCE = 5.;             // max trace distance
  const float INTERSECTION_PRECISION = 0.001;       // precision of the intersection
  const int NUM_OF_TRACE_STEPS = 64;               // max number of trace steps
  const float STEP_MULTIPLIER = .5;
  
  /* Structures */
  /* ---------- */
  struct Camera {
    vec3 ro;
    vec3 rd;
    float FOV;
  };
  struct Surface {
    float len;
    vec3 position;
    vec3 colour;
    float id;
    float steps;
    float AO;
  };
  struct Model {
    float dist;
    vec3 colour;
    float id;
  };
  
  Camera getCamera(in vec3 ro, in vec3 rd, in float FOV) {
    return Camera(
      ro,
      rd,
      FOV
    );
  }
  float sdBoxFrame( vec3 p, vec3 b, float e ) {
    p = abs(p  )-b;
    vec3 q = abs(p+e)-e;
    return min(min(
        length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0),
        length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)),
        length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0));
  }
  float sdRoundBox( vec3 p, vec3 b, float r ) {
    vec3 q = abs(p) - b;
    return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) - r;
  }
  
  Model map( vec3 p ){
    vec3 pos = (vec4(p-vWorldPosition, 1.) * u_matrix).xyz; // Transforming the cube position with the supplied transformation array
    
    pos = mod(pos - .5, 1.) - .5;
//     vec3 apos = abs(pos);
//     float st = max(max(apos.x, apos.y), apos.z);
//     // float st = 
//     vec3 pos2 = mod(pos * 2.4 * step(st, .45), 1.) - .5;
    
//     float field = min(
//         sdBoxFrame(pos, vec3(.4), .05) - .01,
//         sdRoundBox(pos, vec3(.1), .01)
//         );
    
//     // pos = mod(pos * 2., 1.) - .5;
    
//     field = min(field, sdBoxFrame(pos2, vec3(.4), .05));
    
    vec3 p3 = mod(pos * 2., 1.) - .5;
    float field = min( sdBoxFrame(pos, vec3(.3), .01), sdBoxFrame(p3, vec3(.4), .02));
    
    return Model(field, vec3(.5), 0.);
    
    // return Model(sdRoundBox(pos, vec3(.4), .05), vec3(.5), 0.);
    // return Model(distance(p,vWorldPosition)-.4, vec3(.5), 0.);
  }
  
  vec3 shade(Surface surface, vec3 nor, vec3 ref, Camera cam) {
    
    vec3 col = surface.colour;
    vec3 pos = surface.position;
    
    vec3 I = normalize(pos - cam.ro);
    // reflection *= 0.;
    // col = reflection;
    // lighitng        
    float occ = 1./surface.AO * 2.;
    vec3  lig = normalize( vec3(-0.3, 0.8, 0.6) );
    float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 );
    float dif = clamp( dot( nor, lig ), 0.0, 1.0 );
    float bac = clamp( dot( nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);
    // float dom = smoothstep( -0.1, 0.1, ref.y );
    float fre = pow( clamp(1.0+dot(nor,cam.rd),0.0,1.0), 2.0 );
    float spe = pow(clamp( dot( ref, lig ), 0.0, 1.0 ),4.0);

    // dif *= softshadow( pos, lig, 0.02, 2.5 );
    //dom *= softshadow( pos, ref, 0.02, 2.5 );

    vec3 lin = vec3(0.0);
    lin += .60*dif*vec3(.95,0.80,0.60);
    lin += .60*.........完整代码请登录后点击上方下载按钮下载查看

网友评论0