three打造一个魔幻水晶体内魔法气体动画效果代码
代码语言:html
所属分类:三维
代码描述:three打造一个魔幻水晶体内魔法气体动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> * { margin: 0; padding: 0; box-sizing: border-box; } #world { position: relative; width: 100%; height: 100vh; } </style> </head> <body translate="no" > <div id="world"></div> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.r118.js"></script> <script type="module"> import { EffectComposer } from "https://unpkg.com/three@0.120.0/examples/jsm/postprocessing/EffectComposer.js"; import { RenderPass } from "https://unpkg.com/three@0.120.0/examples/jsm/postprocessing/RenderPass.js"; import { UnrealBloomPass } from "https://unpkg.com/three@0.120.0/examples/jsm/postprocessing/UnrealBloomPass.js"; import { OrbitControls } from "https://unpkg.com/three@0.120.0/examples/jsm/controls/OrbitControls"; var options = { exposure: 2.8, bloomStrength: 1.9, bloomThreshold: 0, bloomRadius: 0.55, color0: [74, 30, 0], color1: [201, 158, 72] }; // var gui = new dat.GUI(); // var bloom = gui.addFolder("Bloom"); // // bloom.add(options, "exposure", 0.0, 5.0).name("exposure").listen(); // bloom.add(options, "bloomStrength", 0.0, 5.0).name("bloomStrength").listen(); // // bloom.add(options, "bloomThreshold", 0.0, 1.0).name("bloomThreshold").listen(); // bloom.add(options, "bloomRadius", 0.1, 2.0).name("bloomRadius").listen(); // bloom.open(); // var color = gui.addFolder("Colors"); // color.addColor(options, "color0").name("Border"); // color.addColor(options, "color1").name("Base"); // color.open(); const vert = ` varying vec3 vNormal; varying vec3 camPos; varying vec3 vPosition; varying vec2 vUv; varying vec3 eyeVector; attribute vec3 center; varying vec3 vCenter; void main() { vNormal = normal; vCenter = center; camPos = cameraPosition; vPosition = position; vUv= uv; vec4 worldPosition = modelViewMatrix * vec4( position, 1.0); eyeVector = normalize(worldPosition.xyz - cameraPosition); gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); } `; const frag = ` #ifdef GL_ES precision lowp float; #endif #define NUM_OCTAVES 5 uniform vec4 resolution; uniform vec3 color1; uniform vec3 color0; uniform float utime; uniform sampler2D colorRamp; uniform sampler2D noiseTex; varying vec3 camPos; varying vec3 vNormal; varying vec3 vPosition; varying vec2 vUv; varying vec3 eyeVector; varying vec3 vCenter; float rand(vec2 n) { return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453); } float noise(vec2 p){ vec2 ip = floor(p); vec2 u = fract(p); u = u*u*(3.0-2.0*u); float res = mix( mix(rand(ip),rand(ip+vec2(1.0,0.0)),u.x), mix(rand(ip+vec2(0.0,1.0)),rand(ip+vec2(1.0,1.0)),u.x),u.y); return res*res; } float fbm(vec2 x) { float v = 0.0; float a = 0.5; vec2 shift = vec2(100); // Rotate to reduce axial bias mat2 rot = mat2(cos(0.5), sin(0.5), -sin(0.5), cos(0.50)); for (int i = 0; i < NUM_OCTAVES; ++i) { v += a * noise(x); x = rot * x * 2.0 + shift; a *= 0.5; } return v; } vec3 rgbcol(float r, float g, float b) { return vec3(r/255.0,g/255.0,b/255.0); } float setOpacity(float r, float g, float b) { float tone = (r + g + b) / 3.0; float alpha = 1.0; if(tone<0.99) { alpha = 0.0; } return alpha; } float Fresnel(vec3 eyeVector, vec3 worldNormal) { return pow( 1.0 + dot( eyeVector, worldNormal), 3.0 ); } float getTone(vec3 color){ float tone = (color.r + color.g + color.b) / 3.0; return tone; } float edgeFactorTri() { vec3 d = fwidth(vCenter.xyz); vec3 a3 = smoothstep(vec3(0.0), d * 1.5, vCenter.xyz); return min(min(a3.x, a3.y), a3.z); } void main() { //this is for plane geometry vec2 olduv = gl_FragCoord.xy/resolution.xy ; vec2 uv = vUv ; vec2 newUv = uv + vec2( -utime*0.004, -utime*0.004); vec2 newUv2 = uv + vec2( utime*0.004, utime*0.004); float scale = 5.; vec2 originaluv = uv*5.; vec2 p = newUv*scale; vec2 p2 = newUv2*scale; float noiseoriginal = fbm( originaluv + fbm( originaluv ) )*.8; float noise = fbm( p + fbm( p ))*.8; float noise2 = fbm( p2 + fbm( p2 ))*.8; vec4 animatedNoise = vec4(noise) + vec4(noise2); vec4 totaloffset = animatedNoise + vec4(noiseoriginal); vec3 refracted = refract(eyeVector, vNormal, 1.0/2.42); vec2 p3 = vec2(uv.x +totaloffset.x, uv.y + totaloffset.y)*scale; float noise3 = fbm( p3 + fbm( p3 ))+.8; vec4 finalColor = vec4(noise3) * animatedNoise ; float tone = getTone(finalColor.rgb); vec3 rumpedCol = texture2D(colorRamp,vec2(tone, 0.)).rgb; gl_FragColor = vec4(rumpedCol,1.)*0.5; float f = Fresnel(eyeVector, vNormal); vec3 newCam = vec3(camPos.x,camPos.y,7.); vec3 viewDirectionW = normalize(camPos - vPosition); float fresnelTerm = dot(viewDirectionW, vNormal); fresnelTerm = clamp( .5 - fresnelTerm, 0., 1.) ; gl_FragColor += fresnelTerm*0.6; // gl_FragColor = dot(vNormal,vec3(0,0,0)); vec2 uv2 = uv; vec3 pixeltex = texture2D(noiseTex,mod(uv*5.,1.)).rgb; float iTime = utime*0.004; // camPos.x*0.04; uv.y += iTime / 10.0; uv.x -= (sin(iTime/10.0)/2.0); uv2.x += iTime / 14.0; uv2.x += (sin(iTime/10.0)/9.0); float result = 0.0; result += texture2D(noiseTex, mod(uv*4.,1.) * 0.6 + vec2(iTime*-0.003)).r; result *= texture2D(noiseTex, mod(uv2*4.,1.) * 0.9 + vec2(iTime*+0.002)).b; result = pow(result, 15.0); gl_FragC.........完整代码请登录后点击上方下载按钮下载查看
网友评论0