代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
button {
position: absolute;
bottom: 0;
right: 0;
}
</style>
</head>
<body>
<button>继续/暂停</button>
<script src="./three.js/three.js"></script>
<script src="./three.js/OrbitControls.js"></script>
<script src="./three.js/stats.js"></script>
<script src="./three.js/dat.gui.js"></script>
<script>
var scene = new THREE.Scene();
var ball = new THREE.SphereGeometry(20);
var ballMaterial = new THREE.MeshLambertMaterial({
color: "#000",
wireframe: true,
});
var ballMesh = new THREE.Mesh(ball, ballMaterial);
var rectangle = new THREE.BoxGeometry(50, 10, 8);
var rectangleMaterial = new THREE.MeshLambertMaterial({
color: "#00f",
wireframe: true,
});
var rectangleMesh = new THREE.Mesh(rectangle, rectangleMaterial);
var group = new THREE.Group();
rectangleMesh.name = "Box"; //网格模型1命名
ballMesh.name = "Sphere";
scene.add(group);
group.add(ballMesh);
group.add(rectangleMesh);
var posTrack = new THREE.KeyframeTrack(
"Box.position",
[0, 10],
[0, 0, 0, 300, 0, 0]
);
var colorTrack = new THREE.KeyframeTrack(
"Box.material.color",
[10, 20],
[1, 0, 0, 1, 1, 0]
);
var scaleTrack = new THREE.KeyframeTrack(
"Sphere.scale",
[0, 20],
[1, 1, 1, 3, 3, 3]
);
var clip = new THREE.AnimationClip("default", 20, [
posTrack,
colorTrack,
scaleTrack,
]);
var mixer = new THREE.AnimationMixer(group);
var AnimationAction = mixer.clipAction(clip);
AnimationAction.timeScale = 5;
AnimationAction.play();
var ambientLight = new THREE.AmbientLight("#fff");
scene.add(ambientLight);
var width = window.innerWidth;
var height = window.innerHeight;
var camera = new THREE.OrthographicCamera(
width / -2,
width / 2,
height / 2,
height / -2,
0.1,
1000
);
camera.position.set(200, 200, 200);
camera.lookAt(scene.position);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
renderer.setClearColor("#333");
document.body.appendChild(renderer.domElement);
var axes = new THREE.AxesHelper(500);
scene.add(axes);
var orbitControls = new THREE.OrbitControls(camera, renderer.domElement);
var stats = new Stats();
stats.domElement.style.position = "absolute";
stats.domElement.style.left = "0px";
stats.domElement.style.top = "0px";
document.body.appendChild(stats.domElement);
var datGUI = new dat.GUI();
var Options = function () {
this.rotationSpeed = 0.01;
};
var options = new Options();
datGUI.add(options, "rotationSpeed", 0, 0.5);
//创建一个时钟对象
var clock = new THREE.Clock();
function render() {
// stats.begin();
renderer.render(scene, camera);
rectangleMesh.rotation.x += options.rotationSpeed;
ballMesh.rotation.y += options.rotationSpeed;
requestAnimationFrame(render);
// stats.end();
mixer.update(clock.getDelta());
}
render();
window.onresize = function () {
renderer.setSize(window.innerWidth, window.innerHeight);
// 重置相机投影的相关参数
camera.left = window.innerWidth / -2;
camera.right = window.innerWidth / 2;
camera.top = window.innerHeight / 2;
camera.bottom = window.innerHeight / -2;
// 渲染器执行render方法的时候会读取相机对象的投影矩阵属性projectionMatrix
// 但是不会每渲染一帧,就通过相机的属性计算投影矩阵(节约计算资源)
// 如果相机的一些属性发生了变化,需要执行updateProjectionMatrix ()方法更新相机的投影矩阵
camera.updateProjectionMatrix();
};
var btn = document.querySelector("button");
btn.addEventListener("click", () => {
if (AnimationAction.paused) {
AnimationAction.paused = false;
} else {
AnimationAction.paused = true;
}
});
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。