学习three.js的第4天:three.js能动的狗


先放效果图
 

核心代码 

const gltfLoader = new GLTFLoader()
let clock = new THREE.Clock(); 
let mixer, animationClip, clipAction = null; 
gltfLoader.load("./Fox/fox.gltf", (gltf) => {
  gltf.scene.scale.set(0.025, 0.025, 0.025);
  gltf.scene.position.set(0.025, 0.025, -3.025);
  scene.add(gltf.scene);
  mixer = new THREE.AnimationMixer(gltf.scene);
  animationClip = gltf.animations[0]; 
  clipAction = mixer.clipAction(animationClip).play();
  animationClip = clipAction.getClip();
  const skinnedMesh = new THREE.SkeletonHelper(gltf.scene);
  scene.add(skinnedMesh);
});

总代码

import * as THREE from "three";
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";

const scene = new THREE.Scene();
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
const gridHelper = new THREE.GridHelper(10, 10);
scene.add(gridHelper);

const gltfLoader = new GLTFLoader()
let clock = new THREE.Clock();
let mixer, animationClip, clipAction = null;
gltfLoader.load("./Fox/glTf/fox.gltf", (gltf) => {
  gltf.scene.scale.set(0.025, 0.025, 0.025);
  gltf.scene.position.set(0.025, 0.025, -3.025);
  scene.add(gltf.scene);
  mixer = new THREE.AnimationMixer(gltf.scene);
  animationClip = gltf.animations[0];
  clipAction = mixer.clipAction(animationClip).play();
  animationClip = clipAction.getClip();
  const skinnedMesh = new THREE.SkeletonHelper(gltf.scene);
  scene.add(skinnedMesh);
});

const fov = 55;
const aspect = 2;
const near = 0.1;
const far = 100;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 20, 30);
camera.lookAt(0, 0, 0);
scene.add(camera);
{
  const planeSize = 10;
  const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  const planeMat = new THREE.MeshStandardMaterial({ color: 0x999999 });
  const mesh = new THREE.Mesh(planeGeo, planeMat);
  mesh.rotation.x = Math.PI * - .5;
  mesh.position.set(0, 0, 0)
  scene.add(mesh);
}
const color = 0xFFFFFF;
const intensity = 1;
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(0, 10, 2);
light.target.position.set(0, 0, -0);
scene.add(light);
scene.add(light.target);
scene.add(light);
const helper = new THREE.DirectionalLightHelper(light, 5);
scene.add(helper);

const sizes = {
  width: 1600,
  height: 800,
};
const canvas = document.querySelector("canvas.webgl");
const renderer = new THREE.WebGLRenderer({ canvas: canvas });
renderer.setSize(sizes.width, sizes.height);
renderer.render(scene, camera);
const controls = new OrbitControls(camera, canvas)
controls.update();

function animate() {
  var delta = clock.getDelta();
  requestAnimationFrame(animate);
  controls.update();
  const radius = 10;
  const angle = Date.now() * 0.0001;
  const x = Math.cos(angle) * radius;
  const z = Math.sin(angle) * radius;
  light.position.set(x, 10, z);
  helper.update();
  renderer.render(scene, camera);
  if (mixer && clipAction) {
    mixer.update(delta);
  }
}

animate();


整个demo的gitee仓库: 普通切图仔/three-demo
使用了github的模型库:GitHub - KhronosGroup/glTF-Sample-Models: glTF Sample Models
学习参考的文章:https://juejin.cn/post/6940079683740368933

  • 13
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提供的引用内容,报错"Uncaught TypeError: Cannot read properties of undefined (reading 'offsetX')"是在three.js的第37959行发生的。这个错误通常表示在访问一个未定义的属性时发生了错误。要解决这个问题,可以按照以下步骤进行排查: 1. 确保你已经正确引入了three.js库,并且版本是最新的。可以通过检查引入的脚本路径和版本号来确认。 2. 检查代码中是否存在拼写错误或语法错误。特别是在访问属性时,确保属性名的拼写是正确的,并且确保在访问属性之前已经定义了相应的对象。 3. 检查代码中是否存在异步加载的问题。如果在加载three.js库或其他依赖库时存在异步加载的情况,可能会导致在访问属性时出现未定义的错误。确保在访问属性之前,相关的依赖已经加载完成。 4. 如果以上步骤都没有解决问题,可以尝试在three.js的官方文档或社区中搜索类似的问题,看看是否有其他开发者遇到过类似的错误,并找到解决方法。 下面是一个示例代码,用于演示如何使用three.js创建一个简单的场景: ```javascript // 引入three.js库 import * as THREE from 'three'; // 创建场景 const scene = new THREE.Scene(); // 创建相机 const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.z = 5; // 创建渲染器 const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // 创建一个立方体 const geometry = new THREE.BoxGeometry();const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geometry, material); scene.add(cube); // 渲染场景 function animate() { requestAnimationFrame(animate); cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render(scene, camera); } animate(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值