webgl实现三维水晶金色方块旋转效果代码

代码语言:html

所属分类:三维

代码描述:webgl实现三维水晶金色方块旋转效果代码

代码标签: 水晶 金色 方块 旋转 效果

下面为部分代码预览,完整代码请点击下载或在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 = 10.;             // 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 = 1.;
  
  /* 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;
    return Model(
      min(
        sdBoxFrame(pos, vec3(.4), .001) - .02,
        sdRoundBox(pos, vec3(.1), .05)
        ), vec3(.5), 0.);
    // return Model(sdRoundBox(pos, vec3(.4), .05), vec3(.5), 0.);
    // return Model(distance(p,vWorldPosition)-.4, vec3(.5), 0.);
  }
  
  Surface calcIntersection( in Camera cam ){
    float h =  IN.........完整代码请登录后点击上方下载按钮下载查看

网友评论0