three打造一个三维沙球动画效果
代码语言:html
所属分类:三维
代码描述:three打造一个三维沙球动画效果
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> canvas { width: 100%; height: 100vh; display: block; position: fixed; top: 0; left: 0; cursor: -webkit-grab; cursor: grab; } body { display: grid; } a { padding: 5vmin; position: relative; } </style> </head> <body translate="no"> <script type="module"> import * as $ from '//unpkg.com/three@0.118.3/build/three.module.js' import { OrbitControls } from '//unpkg.com/three@0.118.3/examples/jsm/controls/OrbitControls.js' // ---- // Boot // ---- const renderer = new $.WebGLRenderer({ preserveDrawingBuffer: true }); document.body.prepend(renderer.domElement); const renderTarget = new $.WebGLRenderTarget(1024, 1024/*, option */); window.addEventListener('resize', () => { const { clientWidth, clientHeight } = renderer.domElement; renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(clientWidth, clientHeight, false); scene0.camera.aspect = clientWidth / clientHeight; scene0.camera.updateProjectionMatrix(); }); // ------ // Scene1 // ------ function initScene1() { const scene = new $.Scene(); const camera = new $.OrthographicCamera(-5, 5, 5, -5, 0, 1); const meshes = []; const vertices = []; const N_MESH = 32; const V_MAX = 5 * 2; const SIDE = 5; const BS = SIDE * 2; const bbox = new $.Box2(new $.Vector2(-BS, 0), new $.Vector2(BS, SIDE)); function makeMesh() { const geom = new $.CircleBufferGeometry(SIDE / 10, 5); const color = new $.Color().setHSL(Math.random() * 0.3, .1, .1); const mat = new $.MeshBasicMaterial({ color, blending: $.SubtractiveBlending }); const mesh = new $.Mesh(geom, mat); mesh.position.set(0, 0, -1); return mesh; } function initMeshes() { for (let i = 0, I = N_MESH; i < I; ++i) { const mesh = makeMesh(i, I); meshes.push(mesh); scene.add(mesh); } } function initVertices() { for (let j = 0, J = N_MESH * 3; j < J; ++j) { const position = randomPosition2(SIDE); const velocity = randomVelocity2(V_MAX); vertices[j] = { position, velocity }; } } function randomPosition2(side) { const x = $.MathUtils.randFloat(-side, side); const y = $.MathUtils.randFloat(-side, side); return new $.Vector2(x, y); } function randomVelocity2(vMax) { const x = $.MathUtils.randFloatSpread(vMax * 2); const y = $.MathUtils.randFloatSpread(vMax * 2); return new $.Vector2(x, y); } function updatePosition({ position, velocity }, dt) { position.add(velocity.clone().multiplyScalar(dt)); if (!bbox.containsPoint(position)) { velocity.multiplyScalar(-1); bbox.clampPoint(position.clone(), position); } } function initBackdrop() { const mat = new $.SpriteMaterial(.........完整代码请登录后点击上方下载按钮下载查看
网友评论0