three容器中灵气飘动流动效果

代码语言:html

所属分类:三维

代码描述:three容器中灵气飘动流动效果

代码标签: 灵气 飘动 流动 效果

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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<style>
canvas {
    display: block; width: 100%; height: 100vh;
    position: fixed; top: 0; left: 0;
}

body {
    display: grid;
}

a {
    position: relative;
    color: #eee;
    padding: 5vmin;
}
</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 = 100;
    const V_MAX = 5;
    const SIDE = 5;
    const BS = SIDE * 2;
    const bbox = new $.Box2(new $.Vector2(-BS, -BS), new $.Vector2(BS, SIDE));

    function makeMesh() {
        const geom = new $.CircleBufferGeometry(SIDE / 10, 10);
        const color = new $.Color().setHSL(Math.random() * 0.5 + 0.5, 1, .02);
        const mat = new $.MeshBasicMaterial({ color, blending: $.AdditiveBlending });
        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, 0);
        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.multiplyScal.........完整代码请登录后点击上方下载按钮下载查看

网友评论0