ошибки: THREE.BufferAttribute: copyColorsArray() was removed in r144. Uncaught (in…
- User
ошибки: THREE.BufferAttribute: copyColorsArray() was removed in r144. Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'isGLBufferAttribute') Uncaught TypeError: Cannot read properties of undefined (reading 'isGLBufferAttribute')
в этом коде: import * as THREE from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
const loader = new GLTFLoader(); const url = 'particlesThree.glb'; loader.load(url, function (gltf) { const model = gltf.scene;
model.traverse((child) => { if (child.isMesh) { const geometry = child.geometry;
// Создайте массив цветов для вершин const vertexColors = []; for (let i = 0; i < geometry.attributes.position.count; i++) { vertexColors.push(new THREE.Color(Math.random(), Math.random(), Math.random())); } geometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(vertexColors.length * 3), 3).copyColorsArray(vertexColors)); const material = new THREE.PointsMaterial({ size: 0.05, vertexColors: true, // Используйте вершинные цвета }); const points = new THREE.Points(geometry, material); child.parent.add(points); child.parent.remove(child); }});
model.scale.set(0.5, 0.5, 0.5); model.position.set(0, 0, 0); scene.add(model);
const mixer = new THREE.AnimationMixer(model); gltf.animations.forEach((clip) => { const action = mixer.clipAction(clip); action.play(); action.setLoop(THREE.LoopRepeat, Infinity); });
camera.position.z = 5;
animate();
function animate() { requestAnimationFrame(animate); mixer.update(0.01); controls.update(); scene.traverse((object) => { if (object instanceof THREE.Points) { object.rotation.x += 0.01; object.rotation.y += 0.01; } }); renderer.render(scene, camera); } });
- Assistant
Исправленный код:
import * as THREE from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement);
- User
Uncaught (in promise) TypeError: (intermediate value).setArray is not a function
- Assistant
Ошибка возникает в связи с тем, что метод
setArray()был удален в версии r125 библиотеки three.js. Вместо этого следует использовать методfromArray().Вам необходимо заменить строку:
geometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(vertexColors.length * 3), 3).setArray(vertexColors));на
geometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(vertexColors.length * 3), 3).fromArray(vertexColors));После этого ошибка должна исчезнуть.
- User
Uncaught (in promise) TypeError: (intermediate value).fromArray is not a function
- Assistant
Если у вас возникла ошибка
TypeError: (intermediate value).fromArray is not a function, то скорее всего у вас установлена устаревшая версия библиотеки three.js.Метод
fromArrayявляется методом классаTHREE.BufferAttribute, который доступен в версиях three.js начиная с r125. Если вы используете версию three.js ниже, то этого метода в библиотеке нет, и поэтому возникает ошибка.Рекомендуется обновить версию three.js до последней, которая доступна на данный момент.
- User
rfr j,yjdbnm&
- Assistant
Не могу понять ваше сообщение. Пожалуйста, уточните свой вопрос.
- User
как обновить?
- Assistant
Вы можете выполнить обновление three.js с помощью менеджера пакетов npm, который интегрирован в большинство современных сборщиков проектов, таких как Webpack или Parcel.
Для обновления three.js в своем проекте необходимо выполнить команду в терминале:
npm update threeЕсли вы используете yarn, то выполните команду:
yarn upgrade threeПосле выполнения этой команды будет установлена последняя версия three.js.
Также вы можете скачать архив с последней версией three.js по ссылке https://threejs.org/build/three.min.js и заменить файл с устаревшей версией в своем проекте на новый.
Обратите внимание, что при обновлении могут произойти изменения в API, поэтому вам может потребоваться модифицировать свой код в соответствии с новыми требованиями библиотеки.