1 Star 0 Fork 1

mrwh/threejs-master-learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
webgl_loader_texture_lottie_20230913_gongziting.html 4.77 KB
一键复制 编辑 原始数据 按行查看 历史
wuhui_ylz 提交于 1年前 . feat: 初始化项目
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - lottie loader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<span class="colorPicker"><input id="body-color" type="color" value="#ffffff"></input><br/>Body</span>
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - lottie<br/></br>
<input id="scrubber" type="range" value="0" style="width: 300px">
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
import { LottieLoader } from 'three/addons/loaders/LottieLoader.js';
let renderer, scene, camera;
let mesh;
init();
animate();
function init() {
// 创建透视相机
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
camera.position.z = 10;
scene = new THREE.Scene();
scene.background = new THREE.Color( '#ffffff' );
const bodyColorInput = document.getElementById('body-color');
bodyColorInput.addEventListener( 'input', function () {
scene.background = new THREE.Color( this.value );
} );
// 创建动画片段
const loader = new LottieLoader();
console.log('loader: ', loader);
// 动画质量
loader.setQuality( 2 );
loader.load( 'textures/lottie/24017-lottie-logo-animation.json', function ( texture ) {
console.log('texture: ', texture);
// 动画控制器
setupControls( texture.animation );
// texture = new THREE.TextureLoader().load( 'textures/uv_grid_directx.jpg' );
// texture.colorSpace = THREE.SRGBColorSpace;
// 创建圆角缓冲立方体
// 长、宽、高、每面分几段、半径
const geometry = new RoundedBoxGeometry( 4, 4, 4, 4, 0.4 );
// 创建材质
const material = new THREE.MeshStandardMaterial({
roughness: 0.1,
map: texture
} );
// 创建网格
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
} );
// 创建渲染器
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// 创建场景
const environment = new RoomEnvironment( renderer );
// 辐射环境贴图
const pmremGenerator = new THREE.PMREMGenerator( renderer);
// scene场景 sigma模糊半径 near近平面值 far远平面值
scene.environment = pmremGenerator.fromScene( environment,0.1,1,100 ).texture;
// 监听窗口变化
window.addEventListener( 'resize', onWindowResize );
}
function setupControls( animation ) {
// Lottie animation API
// https://airbnb.io/lottie/#/web
// There are a few undocumented properties:
// console.log( animation );
const scrubber = document.getElementById( 'scrubber' );
scrubber.max = animation.totalFrames;
// 鼠标按下事件
scrubber.addEventListener( 'pointerdown', function () {
animation.pause();
} );
// 鼠标松开事件
scrubber.addEventListener( 'pointerup', function () {
animation.play();
} );
// 监听手写笔进度
scrubber.addEventListener( 'input', function () {
animation.goToAndStop( parseFloat( scrubber.value ), true );
} );
// enterFrame 浏览器发布时的播放器事件 1/帧频
/* 举例: Flash的舞台的帧频stage.FrameRate设置为25帧/秒,即每帧运行时间是1000ms/25帧,即1帧占40毫秒。Event.EnterFrame的出发间隔为40ms,如果Event.EnterFrame的执行函数花费时间为25ms,那么Event.EnterFrame将以间隔40ms的恒定频率循环执行函数。 */
animation.addEventListener( 'enterFrame', function () {
scrubber.value = animation.currentFrame;
} );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
requestAnimationFrame( animate );
if ( mesh ) {
mesh.rotation.y += 0.001;
}
renderer.render( scene, camera );
}
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mrwh/threejs-master-learn.git
git@gitee.com:mrwh/threejs-master-learn.git
mrwh
threejs-master-learn
threejs-master-learn
main

搜索帮助