gl-matrix实现三维螺旋球体阴影变化旋转动画效果代码

代码语言:html

所属分类:三维

代码描述:gl-matrix实现三维螺旋球体阴影变化旋转动画效果代码

代码标签: gl-matrix 三维 螺旋 球体 阴影 变化 旋转 动画

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

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">

<style>
    body, html {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #001;
}
canvas {
  width: 100%;
  height: 100%;
  display: block;
}
#controls {
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 100;
  display: flex;
  align-items: center;
}
.btn {
  background-color: rgba(0, 0, 0, 0.4);
  border: none;
  color: rgba(255, 255, 255, 0.4);
  padding: 10px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 2px 2px;
  cursor: pointer;
  border-radius: 5px;
  transition: background-color 0.3s;
}
.btn:hover {
  background-color: rgba(255, 255, 255, 0.2);
  color: rgba(255, 255, 0, 1);
}
#fullscreenBtn {
  font-size: 20px;
}
</style>
</head>
<body>

<canvas id="glCanvas"></canvas>

<div id="controls">
  <button id="fullscreenBtn" class="btn" title="Toggle Fullscreen">⤢</button>
</div>

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gl-matrix-min.js"></script>

  <script  >

const canvas = document.getElementById('glCanvas');
const gl = canvas.getContext('webgl2');
if (!gl) {
    console.error('WebGL 2 not supported');
    document.body.innerHTML = 'WebGL 2 is not supported in your browser.';
}

const vertexShaderSource = `#version 300 es
in vec4 aPosition;
void main() {
    gl_Position = aPosition;
}`;

const fragmentShaderSource = `#version 300 es
precision highp float;

uniform vec3 iResolution;
uniform float iTime;
uniform vec4 iMouse;
out vec4 fragColor;

/*--- BEGIN OF SHADERTOY ---*/

// 20240927

#define Shadow 1     // 0 or 1
#define rot(t) mat2(cos(t), sin(t), -sin(t), cos(t))
#define time (iTime*2.)
int obj;
float dist;

float smin(float d1, float d2, float k)
{
        float h =clamp(.5 + .5 *(d2-d1)/k,0.,1.);
        return mix(d2, d1, h)-k*h*(1.-h);
}

float smax(float d1, float d2, float k)
{
        return smin(d1,d2,-k);
}

float gyr(vec3 p){
      obj=0;
      float r=4.25;
      float d1=length(p)-r;
      vec3 q=p ;
      float d2= dot(cos(q.yzx*4.),sin(q*4.))/4.-.0;
      //float k = 48.;//floor(iTime);  // 4 12 20 28 36 48
      //float d6= dot(cos(p*k),sin(p.zxy*k))/k;    
      
// NEW Iteration to follow mouse y-pos
      float k = 36.;//floor(iTime);  // 4 12 20 28 36 48
      if(iMouse.x>iResolution.x*.25)k=20.; // NEW
      if(iMouse.x>iResolution.x*.5)k=12.;  // NEW
      if(iMouse.x>iResolution.x*.75)k=4.;  // NEW
                  
      float d6= dot(cos(p*k),sin(p.yzx*k))/k;       
      
      float d5 = length(max(abs(vec2(d2,d1)),0.))-.03;            
      
      dist = .5* smin(d5, length(smax(0.,smax(d1,smax(abs(d2),abs(d6)-.01,.03),.03),.01)-.001), .01);// 202409270516
            
      float flr = (100.+r)-length(p-vec3(0,100,0));
      if(flr<dist)obj=1;
      dist = min(dist,flr);
      return dist;
}


float map(vec3 p)
{
        float t = time; // 2.54
        
        //p.yz *=rot(t*.4);
        p.xz *=rot(t*.05);
        //p.xy *=rot(t*.7);
        return gyr(p);        
}


float calcSoftshadow( in vec3 ro, in vec3 rd  )
{// https://www.shadertoy.com/view/lsKcDD - iq
	float res = 1.0;
    float t = .001;
    float ph = 1e10; // big, such that y = 0 on the first iteration
    int technique=1;
    float tmax=5., w=.2;
    int j=0;
    for( int i=0; i<32; i++ )
    {   j++;
		float h = map( ro + rd*t );
        
        float dm = max(dist,.001);
        
        if( technique==0 )
        {
        	res = min( res, dm/(w*t) );
        }
        // improved technique
        else
        {
            // use this if you are getting artifact on the first iteration, or unroll the
            // first iteration out of the loop
            //float y = (i==0) ? 0.0 : h*h/(2.0*ph); 

            float y = dm*dm/(2.0*ph);
            float d = sqrt(dm*dm-y*y);
            res = min( res, d/(w*max(0.0,t-y)) );
            ph = dm;
        }
       
        t += h;//min(max(h,0.001),.5);
        
        if( res<0.0001 || t>tmax ) break;
        
    }
    //if(j>32)return 0.;
    res = clamp( res, 0.0, 1.0 );
    return res*res*(3.0-2.0*res);
}


void mainImage(out vec4 O, vec2 v)
{        
        O = vec4(.5);
        vec4 clr=vec4(1);
        vec2 R = iResolution.xy,
             u = 1. * (v+v+.1 - R) / R.y,       
             m = 1. * (iMouse.xy*2. - R) / R.y; 
        vec3 o = vec3(0, 0, -9),                
             r = normalize(vec3(u+vec2(0,-.0), 1.5)),        
             e = vec3(0, 1e.........完整代码请登录后点击上方下载按钮下载查看

网友评论0