wtc-math+wtc-gl实现篮球高尔夫网球重力下落碰撞动画效果代码

代码语言:html

所属分类:动画

代码描述:wtc-math+wtc-gl实现篮球高尔夫网球重力下落碰撞动画效果代码,篮球定在空中挡住高尔夫与网球的下落。

代码标签: wtc-math wtc-gl 篮球 高尔夫 网球 重力 下落 碰撞 动画

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

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

<head>
  <meta charset="UTF-8">
  


  
  
<style>
body {
  background: linear-gradient(#001122, #001122, #334455);
  margin:0;
  overflow:hidden;
}
canvas {
  max-width: 100vw;
}
</style>

</head>

<body>
  <script type="x-shader/vertex" id="vert">#version 300 es
  in vec3 position;
  in vec3 normal;
  in vec2 uv;
  in float index;

  in vec2 offset;
  in vec2 scale;
  in float type;
  
  out vec2 vUv;
  out float vType;
  
  uniform vec2 u_resolution;
  
  vec2 getScreenSpace(vec2 p) {
    vec2 uv = (p - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);

    return uv;
  }
  
  void main() {
    vec2 vuv = getScreenSpace(position.xy);
    float a = scale.y;
    float s = sin(a);
    float c = cos(a);
    vUv = uv;
    vType = mod(type, 3.);
    // gl_Position = vec4(position.xy * scale.x + offset, 0, 0);
    vec2 pos = position.xy * mat2(c,-s,s,c);
    gl_Position = vec4(pos/u_resolution * scale.x + offset/u_resolution, .5, 1);
  }
</script>
<script type="x-shader/fragment" id="frag">#version 300 es
  precision highp float;
  
  in vec2 vUv;
  in float vType;
  
  uniform sampler2D s_ball1;
  uniform sampler2D s_ball2;
  uniform sampler2D s_ball3;
  
  out vec4 col;
  
  void main() {
    col = vec4(smoothstep(0.01, 0., length(vUv-.5) - .495));
    if(vType == 0.) col = texture(s_ball1, vUv);
    else if(vType == 1.) col = texture(s_ball2, vUv);
    else if(vType == 2.) col = texture(s_ball3, vUv);
    
    col *= col.a;
    // col = vec4(vUv, 1., 1.);
  }
</script>
  
      <script type="module">
import { Renderer, Drawable, Texture, Program, Plane, Geometry, GeometryAttribute, Mesh, Uniform } from "//repo.bfw.wiki/bfwrepo/js/module/wtc-gl.1.0.0-beta.49.js";
import { Vec2 } from "//repo.bfw.wiki/bfwrepo/js/module/wtc-math.1.0.17.js";

console.clear();

// Initialize global properties
const p = new Vec2(); // window size
const g = new Vec2(0, -1); // gravity
const num = 1000; // The number of instances

// Create the renderer and add it to the document
const r = new Renderer({ dpr: 2, premultipliedAlpha: true, alpha: true });
const gl = r.gl;
document.body.appendChild(gl.canvas);

// Create the uniforms array
const uniforms = {
  'u_resolution': new Uniform({ name: 'resolution', value: r.dimensions.array }) };


// Handle window resize
const resize = e => {
  p.reset(window.innerWidth, window.innerHeight);
  r.dimensions = new Vec2(window.innerWidth, window.innerHeight);
  uniforms.u_resolution.value = p.array;
};
window.addEventListener('resize', resize);
resize();

// Create the scene, this is the thing that we render
const scene = new Drawable(gl);

// Initialize the program (the shaders are in the HTML view)
const vertex = window.vert.innerText;
const fragment = window.frag.innerText;
const program = new Program(gl, {
  vertex, fragment, uniforms, transparent: true, depthTest: false });


// Initialize the various object properties
const offset = new Float32Array(num * 2); // The offset on screen, basically the position
const sr = new Float32Array(num * 2); // Scale and rotation vec2
const type = new Float32Array(num); // The object type - gets coerced in the fragment shader
const vels = []; // Velocity tracking - updated in the runloop
const spin = []; // spin tracking - updated in the runloop
const b = new Vec2(0, 0); // The position of the main barrier
offset.set(b.array, 0);
sr.set([5, 1], 0);
type.set([0], 0);
// Loop thgrough everything and update the properties
for (let i = 1; i < num; i++) {
  const os = // Offset
  new Vec2(Math.random(), Math.random()) // Random vector
  .multiply(p.scaleNew(2).addScalar(100)) // Multiply by the window size
  .subtract(p.subtractScalarNew(100)); // center
  offset.set(os.array, i * 2); // Set the offset property
  const s = .2 + Math.random(); // Random scale
  sr.set([s, 0], i * 2); // Set the scale and rotation
  vels[i] = new Vec2(0, 0); // Set the initial velocity to zero
  spin[i] = 0; // Set the initial spin to zero
  type.set([i], i); // Type is just instance index
}

// Set up the geometry attributes. Note the instanced prop.........完整代码请登录后点击上方下载按钮下载查看

网友评论0