three模拟真实的暴风雪下雪动画效果代码

代码语言:html

所属分类:动画

代码描述:three模拟真实的暴风雪下雪动画效果代码

代码标签: three 模拟 真实 暴风雪 下雪 动画

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

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

  
  
<style>
* {
	margin: 0;
	padding: 0;
}

html,
body {
	overflow: hidden;
	background-color: black;
}

canvas {
	position: fixed;
	top: 0;
	left: 0;
	outline: none;
}

.image {
	position: absolute;
	display: flex;
	width: 100%;
	height: 100%;
	top: 0;
	left: 0;
	z-index: -1;
	filter: brightness(0.5) blur(1px);
}

img {
	object-fit: cover;
	object-position: 50% 50%;
	width: 100%;
	height: auto;
}
</style>

  
</head>

<body translate="no">
  <div class="image">
	<img src="//repo.bfw.wiki/bfwrepo/image/65d420c223aa5.png" />
</div>
<canvas></canvas>
  <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.128.js"></script>
      <script type="module">

class SnowAnimation {
    constructor(canvasSelector) {
        // Initialize canvas
        const canvas = document.querySelector(canvasSelector);
        if (!canvas)
            throw new Error("Canvas element not found");
        this.canvas = canvas;
        // Initialize properties
        this.geometry = null;
        this.material = null;
        this.points = null;
        this.previousTime = 0;
        this.clock = new THREE.Clock();
        // Setup snow parameters
        this.snow = {
            count: document.documentElement.clientWidth * 20,
            randomness: 0.5,
            randomnessPower: 3,
            sizeMin: 1.0,
            sizeMax: 4.0,
            opacityMin: 0.1,
            opacityMax: 0.4,
            gravity: 25.0
        };
        // Setup wind
        this.wind = {
            current: 0,
            force: 0.1,
            target: 0.1,
            min: 0.1,
            max: 0.2,
            easing: 0.005
        };
        // Initialize Three.js components
        this.scene = new THREE.Scene();
        this.scene.background = null;
        const { width, height } = this.getViewportSize();
        this.camera = new THREE.PerspectiveCamera(50, width / height, 0.5, 100);
        this.camera.position.set(-0.25, 0, -2);
        this.scene.add(this.camera);
        this.renderer = new THREE.WebGLRenderer({
            canvas: this.canvas,
            alpha: true
        });
        this.renderer.setSize(width, height);
        this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
        // Setup event listeners
        window.addEventListener("resize", this.handleResize.bind(this));
        // Initialize snow
        this.generateSnow();
    }
    getViewportSize() {
        return {
            width: window.innerWidth,
            height: window.innerHeight
        };
    }
    handleResize() {
        const { width, height } = this.getViewportSize();
        this.camera.aspect = width / height;
        this.camera.updateProjectionMatrix();
        this.renderer.setSize(width, height);
        this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
    }
    generateSnow() {
        var _a, _b;
        if (this.points !== null) {
            (_a = this.geometry) === null || _a === void 0 ? void 0 : _a.dispose();
            (_b = this.material) === null || _b === void 0 ? void 0 : _b.dispose();
            this.scene.remove(this.points);
        }
        this.geometry = new THREE.BufferGeometry();
        const positions = new Float32Array(this.snow.count * 3);
        const scales = new Float32Array(this.snow.count);
        const randomness = new Float32Array(this.snow.count * 3);
        const speeds = new Float32Array(this.snow.count * 3);
        const rotations = new Float32Array(this.snow.count * 3);
        const opacities = new Float32Array(this.snow.count);
        for (let i = 0; i < this.snow.count; i++) {
            const i3 = i * 2;
            // Set positions
            positions[i3] = (Math.random() - 0.5) * 12;
            positions[i3 + 1] = (Math.random() - 0.5) * 12;
            positions[i3 + 2] = (Math.random() - 0.5) * 12;
            // Calculate randomness
            const randomX = Math.pow(Math.random(), this.snow.randomnessPower) *
                (Math.random() < 0.5 ? 1 : -1) *
                this.snow.randomness;
            const randomY = Math.pow(Math.random(), this.snow.randomnessPower) *
                (Math.random() < 0.5 ? 1 : -1) *
                this.snow.randomness;
            const randomZ = Math.pow(Math.random(), this.snow.randomnessPower) *
                (Math.random() < 0.5 ? 1 : -1) *
                this.snow.randomness;
            randomness[i3] = randomX;
            randomness[i3 + 1] = randomY;
            randomness[i3 + 2] = randomZ;
            // Set opacities
            opacities[i] =
                Math.random() * (this.snow.opacityMax - this.snow.opacityMin) +
                    this.snow.opacityMin;
            // Set scales
            scales[i] =
                Math.random() * (this.snow.size.........完整代码请登录后点击上方下载按钮下载查看

网友评论0