three实现圆润立方体旋转动画效果代码
代码语言:html
所属分类:三维
代码描述:three实现圆润立方体旋转动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel='stylesheet' href='https://cdn.jsdelivr.net/gh/alphardex/aqua.css/dist/aqua.min.css'> <style> body { display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; } :root { --black-color-1: #12112a; } .bg-black-1 { background: var(--black-color-1); } </style> </head> <body> <div class="relative w-screen h-screen"> <div class="twisted-shape w-full h-full bg-black-1"></div> </div> <script type="module"> import * as THREE from "https://cdn.skypack.dev/three@0.133.1"; import { ParametricGeometry } from "https://cdn.skypack.dev/three@0.133.1/examples/jsm/geometries/ParametricGeometry"; import { OrbitControls } from "https://cdn.skypack.dev/three@0.124.0/examples/jsm/controls/OrbitControls"; const calcAspect = (el) => el.clientWidth / el.clientHeight; const getNormalizedMousePos = (e) => { return { x: (e.clientX / window.innerWidth) * 2 - 1, y: -(e.clientY / window.innerHeight) * 2 + 1 }; }; // https://arxiv.org/pdf/1604.02174.pdf const sphube = (u1, v1, target) => { const s = 0.6; const r = 1; const theta = 2 * u1 * Math.PI; const phi = v1 * 2 * Math.PI; const u = Math.cos(theta) * Math.cos(phi); const v = Math.cos(theta) * Math.sin(phi); const w = Math.sin(theta); const z = (r * u) / Math.sqrt(1 - s * v ** 2 - s * w ** 2); const x = (r * v) / Math.sqrt(1 - s * u ** 2 - s * w ** 2); const y = (r * w) / Math.sqrt(1 - s * Math.cos(theta) ** 2); target.set(x, y, z); }; const twistedShapeVertexShader = ` #define GLSLIFY 1 mat2 rotation2d(float angle) { float s = sin(angle); float c = cos(angle); return mat2( c, -s, s, c ); } mat4 rotation3d(vec3 axis, float angle) { axis = normalize(axis); float s = sin(angle); float c = cos(angle); float oc = 1.0 - c; return mat4( oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0, oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0, 0.0, 0.0, 0.0, 1.0 ); } vec2 rotate(vec2 v, float angle) { return rotation2d(angle) * v; } vec3 rotate(vec3 v, vec3 axis, float angle) { return (rotation3d(axis, angle) * vec4(v, 1.0)).xyz; } float invert(float n){ return 1.-n; } vec3 invert(vec3 n){ return 1.-n; } // https://github.com/glslify/glsl-easings float qinticInOutAbs(float t){ return t<.5 ?+16.*pow(t,5.) :-.5*abs(pow(2.*t-2.,5.))+1.; } const float PI = 3.14159265359; // https://tympanus.net/codrops/2019/10/29/real-time-multiside-refraction-in-three-steps/ vec3 getEyeVector(mat4 modelMat,vec3 pos,vec3 camPos){ vec4 worldPosition=modelMat*vec4(pos,1.); vec3 eyeVector=normalize(worldPosition.xyz-camPos); return eyeVector; } uniform vec3 uAxis; uniform float uTime; uniform float uVelocity; uniform float uDistortion; varying vec3 vNormal; varying vec3 vEyeVector; void main(){ vec3 newPos=position; float offset=2.*dot(uAxis,position); float sDistortion=.01*uDistortion; float oDistortion=sDistortion*offset; float displacement=uVelocity*uTime; float progress=clamp((fract(displacement)-oDistortion)/invert(sDistortion),0.,1.); progress=qinticInOutAbs(progress)*PI; newPos=rotate(newPos,uAxis,progress); gl_Position=projectionMatrix*modelViewMatrix*vec4(newPos,1.); vec3 rotatedNormal=rotate(normal,uAxis,progress); vNormal=rotatedNormal; vec3 rotatedPos=rotate(position,uAxis,progress); vEyeVector=getEyeVector(modelMatrix,rotatedPos,cameraPosition); } `; const twistedShapeFragmentShader = ` #define GLSLIFY 1 // https://www.shadertoy.com/view/4scSW4 float fresnel(float bias,float scale,float power,vec3 I,vec3 N) { return bias+scale*pow(1.+dot(I,N),power); } uniform vec3 uColor; varying vec3 vNormal; varying vec3 vEyeVector; void main(){ float F=fresnel(0.,.6,2.,vEyeVector,vNormal); vec3 color=uColor+F; gl_FragColor=vec4(color,1.); } `; class Base { constructor(sel, debug = false) { this.debug = debug; this.container = document.querySelector(sel); this.perspectiveCameraParams = { fov: 75, near: 0.1, far: 100 }; this.orthographicCameraParams = { zoom: 2, near: -100, far: 1000 }; this.cameraPosition = new THREE.Vector3(0, 3, 10); this.lookAtPosition = new THREE.Vector3(0, 0, 0); this.rendererParams = { outputEncoding: THREE.LinearEncoding, config: { alpha: true, antialias: true } }; this.mousePos = new THREE.Vector2(0, 0); this.mouseSpeed = 0; } // 初始化 init() { this.createScene(); this.createPerspectiveCamera(); this.createRenderer(); this.createMesh({}); this.createLight(); this.createOrbitControls(); this.addListeners(); this.setLoop(); } // 创建场景 createScene() { const scene = new THREE.Scene(); if (this.debug) { scene.add(new THREE.AxesHelper()); const stats = Stats(); this.container.appendChild(stats.dom); this.stats = stats; } this.scene = scene; } // 创建透视相机 createPerspectiveCamera() { const { perspectiveCameraParams, cameraPosition, lookAtPosition } = this; const { fov, near, far } = perspectiveCameraParams; const aspect = calcAspect(this.container); const camera = new THREE.PerspectiveCamera(fov, aspect, near, far); camera.position.copy(cameraPosition); camera.lookAt(lookAtPosition); this.camera = camera; } // 创建正交相机 createOrthographicCamera() { const { orthographicCameraParams, cameraPosition, lookAtPosition } = this; const { left, right, top, bottom, near, far } = orthographicCameraParams; const camera =.........完整代码请登录后点击上方下载按钮下载查看
网友评论0