|
|

楼主 |
发表于 2025-5-6 19:48
|
显示全部楼层
帖子代码(threejs 特效源于 threejs 的一个公开示例):
- <style>
- #tz { --state: running; margin: 30px 0; left: calc(50% - 81px); transform: translateX(-50%); width: clamp(600px, 90vw, 1400px); min-height: 80vh; aspect-ratio: 16/9; background: #666 url('https://638183.freep.cn/638183/t24/webp3/scene.webp') no-repeat center/cover; box-shadow: 2px 2px 10px rgba(0,0,0,.65); z-index: 1; position: relative; }
- #player { position: absolute; left: 3%; top: 2%; z-index: 9; clip-path: circle(45%); transition: filter .7s; cursor: pointer; animation: rot 2s infinite linear var(--state); }
- #player:hover { filter: hue-rotate(120deg); }
- #btnFs { left: 4.5%; top: 22%; width: 5%; color: #eee; text-align: center; }
- #btnFs:hover { color: red; }
- @keyframes rot { to { transform: rotate(360deg); } }
- </style>
- <div id="tz">
- <audio id="aud" src="https://music.163.com/song/media/outer/url?id=304962" autoplay loop></audio>
- <img id="player" src="https://638183.freep.cn/638183/small/202501.png" width="10%" title="播放/暂停" />
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "https://esm.sh/three@0.176.0?target=es2022",
- "three/addons/": "https://esm.sh/three@0.176.0/addons/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js';
- import { FS } from 'https://638183.freep.cn/638183/web/ku/fscreen.js';
- let camera, scene, renderer;
- let physics, position;
- let boxes, spheres;
- init();
- FS(tz, player);
- async function init() {
- physics = await RapierPhysics();
- position = new THREE.Vector3();
- camera = new THREE.PerspectiveCamera(50, tz.offsetWidth / tz.offsetHeight, 0.1, 100);
- camera.position.set(- 1, 1.5, 5);
- camera.lookAt(0, 0.5, 0);
- scene = new THREE.Scene();
- const hemiLight = new THREE.HemisphereLight();
- scene.add(hemiLight);
- const dirLight = new THREE.DirectionalLight(0xffffff, 3);
- dirLight.position.set(5, 5, 5);
- dirLight.castShadow = true;
- dirLight.shadow.camera.zoom = 2;
- scene.add(dirLight);
- const shadowPlane = new THREE.Mesh(
- new THREE.PlaneGeometry(10, 10),
- new THREE.ShadowMaterial({
- color: 0x444444
- }),
- );
- shadowPlane.rotation.x = - Math.PI / 2;
- shadowPlane.receiveShadow = true;
- scene.add(shadowPlane);
- const floorCollider = new THREE.Mesh(
- new THREE.BoxGeometry(10, 5, 10),
- new THREE.MeshBasicMaterial({ color: 0x666666 })
- );
- floorCollider.position.y = - 2.5;
- floorCollider.userData.physics = { mass: 0 };
- floorCollider.visible = false;
- scene.add(floorCollider);
- const material = new THREE.MeshLambertMaterial();
- const matrix = new THREE.Matrix4();
- const color = new THREE.Color();
- const geometryBox = new THREE.BoxGeometry(0.075, 0.075, 0.075);
- boxes = new THREE.InstancedMesh(geometryBox, material, 400);
- boxes.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
- boxes.castShadow = true;
- boxes.receiveShadow = true;
- boxes.userData.physics = { mass: 1 };
- scene.add(boxes);
- for (let i = 0; i < boxes.count; i ++) {
- matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5);
- boxes.setMatrixAt(i, matrix);
- boxes.setColorAt(i, color.setHex(0xffffff * Math.random()));
- }
- const geometrySphere = new THREE.IcosahedronGeometry(0.05, 4);
- spheres = new THREE.InstancedMesh(geometrySphere, material, 400);
- spheres.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
- spheres.castShadow = true;
- spheres.receiveShadow = true;
- spheres.userData.physics = { mass: 1 };
- scene.add(spheres);
- for (let i = 0; i < spheres.count; i ++) {
- matrix.setPosition(Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5);
- spheres.setMatrixAt(i, matrix);
- spheres.setColorAt(i, color.setHex(0xffffff * Math.random()));
- }
- physics.addScene(scene);
- renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
- renderer.setPixelRatio(window.devicePixelRatio);
- renderer.setSize(tz.offsetWidth, tz.offsetHeight);
- renderer.setAnimationLoop(animate);
- renderer.shadowMap.enabled = true;
- tz.appendChild(renderer.domElement);
- const controls = new OrbitControls(camera, renderer.domElement);
- controls.target.y = 0.5;
- controls.update();
- setInterval(() => {
- let index = Math.floor(Math.random() * boxes.count);
- position.set(0, Math.random() + 1, 0);
- physics.setMeshPosition(boxes, position, index);
- index = Math.floor(Math.random() * spheres.count);
- position.set(0, Math.random() + 1, 0);
- physics.setMeshPosition(spheres, position, index);
- }, 1000 / 60);
- window.addEventListener('resize', onWindowResize);
- }
- function onWindowResize() {
- camera.aspect = tz.offsetWidth / tz.offsetHeight;
- camera.updateProjectionMatrix();
- renderer.setSize(tz.offsetWidth, tz.offsetHeight);
- }
- function animate() {
- if (aud.paused) return;
- renderer.render(scene, camera);
- }
- </script>
复制代码
|
评分
-
| 参与人数 3 | 威望 +130 |
金钱 +260 |
经验 +130 |
收起
理由
|
小辣椒
| + 50 |
+ 100 |
+ 50 |
赞一个! |
红影
| + 50 |
+ 100 |
+ 50 |
赞一个! |
花飞飞
| + 30 |
+ 60 |
+ 30 |
很给力! |
查看全部评分
|