原生webgl未知球形世界效果
代码语言:html
所属分类:动画
代码描述:原生webgl未知球形世界效果
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> body { background-color: #000; margin: 0; overflow: hidden; background-repeat: no-repeat; } canvas { position : absolute; } </style> </head> <body translate="no"> <script> "use strict"; let canv, gl; let dimension; let animState; let midx, midy; let widthHandle, heightHandle; let lookAtHandle; let lookFromHandle; let fluidHandle; const mrandom = Math.random; const mfloor = Math.floor; const mround = Math.round; const mceil = Math.ceil; const mabs = Math.abs; const mmin = Math.min; const mmax = Math.max; const mPI = Math.PI; const mPIS2 = Math.PI / 2; const m2PI = Math.PI * 2; const msin = Math.sin; const mcos = Math.cos; const matan2 = Math.atan2; const mhypot = Math.hypot; const msqrt = Math.sqrt; //----------------------------------------------------------------------------- // miscellaneous functions //----------------------------------------------------------------------------- //************** Shader sources ************** let vertexSource = ` attribute vec2 position; void main() { gl_Position = vec4(position, 0.0, 1.0); } `; let fragmentSource = ` // "RayMarching starting point" // by Martijn Steinrucken aka BigWings/CountFrolic - 2020 // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. // // You can use this shader as a template for ray marching shaders // https://www.shadertoy.com/view/WtGXDD precision mediump float; uniform float width; uniform float height; uniform vec3 lookAt; uniform vec3 lookFrom; uniform float fluid; vec3 iResolution; #define MAX_STEPS 100 #define MAX_DIST 100. #define SURF_DIST .001 float GetDist(vec3 p) { float d1 = fluid * (sin (p.x)+2.9 + sin (p.y ) + sin (p.z)); return d1; } float RayMarch(vec3 rorigin, vec3 rdirection) { float dO=0.; for(int i=0; i<MAX_STEPS; i++) { vec3 p = rorigin + rdirection*dO; float dS = GetDist(p); dO += dS; if(dO>MAX_DIST || abs(dS)<SURF_DIST) break; } return dO; } vec3 GetNormal(vec3 p) { float d = GetDist(p); vec2 e = vec2(.001, 0); vec3 n = d - vec3( GetDist(p-e.xyy), GetDist(p-e.yxy), GetDist(p-e.yyx)); return normalize(n); } vec3 GetRayDir(vec2 uv, vec3 p, vec3 l, float z) { // z for zoom ??? vec3 f = normalize(l-p), r = normalize(cross(vec3(0,1,0), f)), u = cross(f,r), c = f*z, i = c + uv.x*r + uv.y*u, d = normalize(i); return d; } mat2 Rot (float angle) { return mat2(cos(angle),-sin(angle), sin(angle),cos(angle)); } void mainImage( out vec4 fragCo.........完整代码请登录后点击上方下载按钮下载查看
网友评论0