“天路”所至处 天堑变通途——学习马老师帖《three.js几何体之多面缓冲几何体》
本帖最后由 杨帆 于 2025-5-22 16:35 编辑 <br /><br /><!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>“天路”所至处天堑变通途</title>
</head>
<body>
<style>
#papa { --state: running; margin: 30px 0; left: calc(50% - 81px); transform: translateX(-50%); width: clamp(600px, 90vw, 1400px); height: auto; aspect-ratio: 16/9; background: url('https://cccimg.com/view.php/4910b7926251bf254563aa390ca74c9e.jpg') no-repeat center/cover; display: grid; place-items: center; box-shadow: 2px 2px 10px rgba(0,0,0,.65); z-index: 1; position: relative; overflow: hidden;
}
#papa canvas { display: block; position: absolute; }
#player { position: absolute; top: 42.8%; width: 8%; cursor: pointer; opacity: 0.5; transition: width .75s; z-index: 9; animation: rot 10s linear infinite var(--state); }
#player:hover { opacity: .8; width: 12%;#mplayer img { filter: invert(.8); filter: drop-shadow(0 0 30px yellow);}
}
#btnFs { bottom: 20px; color: lightblue; border-color: lightblue !important; background: gray; }
#btnFs:hover { color: gold; }
@keyframes rot { to { transform: rotate(1turn); } }
#dt{
position:absolute;
width: 100%;
height: 40%;
top:60%;
left: 0%;
mix-blend-mode: screen;
pointer-events: none;
z-index: 1;
}
#dt img{width: 100%;
height: 100%;
}
#lrc {
--state: paused;
--motion: cover2;
--tt: 2s;
--bg: linear-gradient(180deg, #880000, #ff0000, #80ef03);
position: absolute;z-index: 5;
left: 50%;
transform: translate(-50%);
top: 85%;
font:normal 2.8em 华文隶书;
color: #055306;
white-space: pre;
-webkit-background-clip: text;
filter:drop-shadow(#FFFFFF 1px 0 0)drop-shadow(#FFFFFF 0 1px 0)drop-shadow(#FFFFFF -1px 0 0) drop-shadow(#FFFFFF 0 -1px0);
z-index:5;
}
#lrc::before {
position: absolute;
content: attr(data-lrc);
width: 20%;
height: 100%;
z-index:5;
color: transparent;
overflow: hidden;
white-space: pre;
background: var(--bg);
-webkit-background-clip: text;
animation: var(--motion) var(--tt) linear forwards;
animation-play-state: var(--state);
}
@keyframes cover1 { from { width: 0; } to { width: 100%; } }
@keyframes cover2 { from { width: 0; } to { width: 100%; } }
#vid1{ position: absolute; left: 0%;top: 0%;
width: 100%; height:100%; object-fit: cover; mix-blend-mode: screen;-webkit-mask: linear-gradient(red, transparent);pointer-events: none; }
#vid2{ position: absolute; left: 0%;top: 0%;
width: 100%; height:100%; object-fit: cover; mix-blend-mode: multiply;-webkit-mask: linear-gradient(to right top, red 0%, transparent 90%, transparent);pointer-events: none; }
#fullscreen { position: absolute; top:5%; left:88%;color:#ffffff; filter:drop-shadow( 1px 1px 1px #000000);font: normal 2.2em华文新魏; opacity: 0; cursor: pointer; z-index: 8;
}
#papa:hover#fullscreen{ opacity: .9; }
</style>
<div id="papa">
<span id="fullscreen">全屏观赏</span>
<div id="dt">
<img src="https://cccimg.com/view.php/7dedb9e000fd4291a27e6557f3b41c9a.gif" id="IMG" / ></div>
<audio id="aud" src="https://cccimg.com/view.php/e219abadc901f871555dad56f0c26fdc.mp3" autoplay loop></audio>
<img id="player" src="https://pic.imgdb.cn/item/65b3c398871b83018a80649f.png"alt="" title="播放/暂停" />
<videoid="vid1" src="https://cccimg.com/view.php/7f9f1d52f7c575953b6957576cd5d0b2.mp4" loop muted autoplay=""></video>
<videoid="vid2" src="https://img.tukuppt.com/video_show/2475824/00/01/72/5b492fae7af99.mp4" loop muted autoplay=""></video>
<div id="lrc" data-lrc="">“天路”所至处天堑变通途</div>
<canvas id='canv' width="1430" height="250" style="position: absolute; left:0px; bottom: 5px;"></canvas>
</div>
<script>
(function(){
let script = document.createElement('script');
script.src = 'https://unpkg.com/css-doodle@0.34.9/css-doodle.min.js';
document.head.appendChild(script);
let mState = () => aud.paused ? ( papa.style.setProperty('--state', 'paused')) : ( papa.style.setProperty('--state', 'running'));
aud.addEventListener('play', mState, false);
aud.addEventListener('pause', mState, false);
aud.addEventListener('seeked', () => calcKey());
player.onclick = () => aud.paused ? (aud.play(),vid1.play(),vid2.play()) : (aud.pause(),vid1.pause(),vid2.pause());
})();
(function() {
/*原始lrc歌词*/
let lrcStr = `
天路 (世界音乐版) - 韩红
词:屈塬
曲:印青
编曲:印青
清晨我站在青青的牧场
看到神鹰披着那霞光
像一片祥云飞过蓝天
为藏家儿女带来吉祥
黄昏我站在高高地山岗
看到铁路修到我家乡
一条条巨龙翻山越岭
为雪域高原送来安康
那是一条神奇的天路耶喂
把人间的温暖送到边疆
从此山不再高
路不再漫长
各族儿女欢聚一堂
黄昏我站在高高地山岗
看到铁路修到我家乡
一条条巨龙翻山越岭
为雪域高原送来安康
那是一条神奇的天路耶喂
带我们走进人间天堂
青稞酒酥油茶
会更加香甜
幸福的歌声传遍四方
幸福的歌声传遍四方
`;
/*变量 :mKey - 当前歌词索引;mFlag :调用关键帧动画索引;averAdd :平均值补偿*/
let mKey = 0, mFlag = true, averAdd = 0.3;
/*函数 :获取每句歌词用时,歌词用时若超过平均值则取平均值,最后一句歌词则取平均值*/
let lrcTime = (ar) => {
let tmpAr = [];
for(j = 0; j <ar.length - 1; j ++) {
if(j !== ar.length - 1) tmpAr = parseFloat((ar - ar).toFixed(1));
}
let aver = parseInt(tmpAr.reduce((a,b) => a + b) / (tmpAr.length - 1)) + averAdd;
tmpAr.push(aver);
tmpAr.forEach((item,key) => {
ar = item > aver ? aver : item;
});
return ar;
};
/*函数 :从原始lrc歌词获取信息并存入 n*3 数组*/
let getLrcAr = (text) => {
let lrcAr = [];
let calcRule = ;
for(x of text.split('\n')) {
let ar = [];
let re = /\d+[\.:]\d+([\.:]\d+)?/g;
let geci = x.replace(re,'');
if(geci) {
geci = geci.replace(/[\[\]\'\"\t,]s?/g,'');
let time = x.match(re);
if(time != null) {
for(y of time) {
let tmp = y.match(/\d+/g);
let sec = 0;
for(z in tmp) sec += tmp * calcRule;
ar = ;
lrcAr.push(ar);
}
}
}
}
lrcAr.sort((a,b)=> a - b);
return(lrcTime(lrcAr));
};
/*函数 :模拟显示同步歌词*/
let showLrc = (time) => {
let name = mFlag ? 'cover1' : 'cover2';
lrc.innerHTML = lrcAr;
lrc.dataset.lrc = lrcAr;
lrc.style.setProperty('--motion', name);
lrc.style.setProperty('--tt', time + 's');
lrc.style.setProperty('--state', 'running');
mKey += 1;
mFlag = !mFlag;
};
/*函数 :处理当前歌词索引 mKey*/
let calcKey = () => {
for (j = 0; j < lrcAr.length; j++) {
if (aud.currentTime <= lrcAr) {
mKey = j - 1;
break;
}
}
if (mKey < 0) mKey = 0;
if (mKey > lrcAr.length - 1) mKey = lrcAr.length - 1;
let time = lrcAr - (aud.currentTime - lrcAr);
showLrc(time);
};
/*格式化时间信息*/
let toMin = (val) => {
if (!val) return '00:00';
val = Math.floor(val);
let min = parseInt(val / 60),
sec = parseFloat(val % 60);
if (min < 10) min = '0' + min;
if (sec < 10) sec = '0' + sec;
return min + ':' + sec;
}
/*函数 :关键帧动画状态切换*/
let mState = () => aud.paused ? (lrc.style.setProperty('--state','paused'),mplayer.style.animationPlayState = 'paused') : (lrc.style.setProperty('--state','running'),mplayer.style.animationPlayState = 'running');
/*监听播放进度*/
aud.addEventListener('timeupdate', () => {
for (j = 0; j < lrcAr.length; j++) {
if (aud.currentTime >= lrcAr) {
cKey = j;
if (mKey === j) showLrc(lrcAr);
else continue;
}
}
});
aud.addEventListener('pause', () => mState());/*监听暂停事件*/
aud.addEventListener('play', () => mState());/*监听播放事件*/
aud.addEventListener('seeked', () => calcKey());/*监听查询事件*/
let lrcAr = getLrcAr(lrcStr); /*获得歌词数组*/
})();
var mState = () => {
player.style.setProperty('--state', aud.paused ? 'paused' : 'running');
IMG.style.setProperty('opacity', aud.paused ? '0' : '.9');
};
aud.oncanplay = aud.onplaying = aud.onpause = () => mState();
let fs = true;
fullscreen.onclick = () => {
fs ? (fullscreen.innerText = '退出全屏',papa.requestFullscreen()) : (fullscreen.innerText = '全屏欣赏', document.exitFullscreen());
fs = !fs;
};
</script >
<script type="module">
import * as THREE from 'https://638183.freep.cn/638183/web/ku/three.module.min.js';
console.log('Three.js loaded:', THREE);
var scene = new THREE.Scene;
var clock = new THREE.Clock();
var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 10);
var renderer = new THREE.WebGLRenderer({ antialias: true,alpha: true
});
renderer.setSize(papa.clientWidth, papa.clientHeight);
papa.appendChild(renderer.domElement);
// 顶点数组
const verticesOfCube = [
-1,-1,-1, 1,-1,-1, 1, 1,-1, -1, 1,-1,
-1,-1, 1, 1,-1, 1, 1, 1, 1, -1, 1, 1,
];
// 面索引数组
const indicesOfFaces = [
2,1,0, 0,3,2,
0,4,7, 7,3,0,
0,1,5, 5,4,0,
1,2,6, 6,5,1,
2,3,7, 7,6,2,
4,5,6, 6,7,4
];
// 创建多面缓冲几何体(图形骨架)
const geometry = new THREE.PolyhedronGeometry(verticesOfCube, indicesOfFaces, 2, 3);
// 定义材质 : 线框化法向量材质(图形血肉)
const material = new THREE.MeshNormalMaterial({ wireframe: true });
// 成图(图形躯体)
const mesh = new THREE.Mesh(geometry, material);
// 图形加入场景
scene.add(mesh);
const texture1 = new THREE.TextureLoader().load('https://642303.freep.cn/642303/za/2c963b7f7f0947f4b78450448f4a3776_tplv-obj_1200_1200.png');
const texture2 = new THREE.TextureLoader().load('https://642303.freep.cn/642303/za/2c963b7f7f0947f4b78450448f4a3776_tplv-obj_1200_1200.png');
texture1.colorSpace = THREE.SRGBColorSpace;
texture2.colorSpace = THREE.SRGBColorSpace;
const ball = new THREE.Mesh(
new THREE.SphereGeometry(),
new THREE.MeshBasicMaterial({
map: texture1,
transparent: true,
opacity: 0.5,
side: THREE.DoubleSide
})
);
const circle = new THREE.Mesh(
new THREE.CircleGeometry(0.8),
new THREE.MeshBasicMaterial({
map: texture2,
side: THREE.DoubleSide
})
);
const circle1 = circle.clone();
circle1.rotateY(Math.PI / 2);
ball.add(circle, circle1);
scene.add(ball);
// 动画 :xy轴旋转
const animate = () => {
requestAnimationFrame(animate);
const delta = clock.getDelta();
mesh.rotation.x -= delta / 5;
mesh.rotation.y -= delta / 5;
renderer.render(scene, camera);
};
papa.onclick = () => clock.running ? clock.stop() : clock.start();
window.onresize = () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
animate();
</script>
</body>
</html>
漂亮的变幻频谱,大气的制作。
欣赏杨帆好帖{:4_199:} 把黑黑老师的球体提升了,还在里面装了小播,还加了歌词。真的太厉害了!{:4_187:} 梦江南 发表于 2025-5-22 17:25
把黑黑老师的球体提升了,还在里面装了小播,还加了歌词。真的太厉害了!
我是一个小白,是跟黑黑老师等一点一滴学习的,全是东凑西拼代码,纯玩~ 红影 发表于 2025-5-22 15:01
漂亮的变幻频谱,大气的制作。
欣赏杨帆好帖
问好影子,谢谢观赏与鼓励{:4_187:} 杨帆 发表于 2025-5-22 18:47
问好影子,谢谢观赏与鼓励
问好杨帆,晚上好{:4_187:} 杨帆 发表于 2025-5-22 18:46
我是一个小白,是跟黑黑老师等一点一滴学习的,全是东凑西拼代码,纯玩~
早上好,你这个小白大有可为,东凑西拼代码也真的是不容易。为你点赞加油!{:4_187:} 梦江南 发表于 2025-5-23 08:14
早上好,你这个小白大有可为,东凑西拼代码也真的是不容易。为你点赞加油!
问好江南,谢谢观赏与鼓励{:4_204:}
页:
[1]