ошибок не выводит, но отображается только белый экран import * as THREE from 'three';…
- User
ошибок не выводит, но отображается только белый экран 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 lerpSpeed = 0.01; // скорость интерполяции, значение от 0 до 1
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 material = new THREE.PointsMaterial({ size: 0.01, vertexColors: true // включаем использование цветов вершин }); const points = new THREE.Points(geometry, material);
// получаем массив вершин из объекта geometry const positions = geometry.getAttribute('position').array; // создаем массив цветов для вершин const colors = []; for (let i = 0; i < positions.length; i += 3) { colors.push(Math.random(), Math.random(), Math.random()); // создаем случайный цвет для каждой вершины } let midPosition = [...positions]; // задаем массив цветов в объекте geometry geometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3)); geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(colors), 3)); 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 = 3;
// let midPosition = positions; function animate() { requestAnimationFrame(animate); mixer.update(0.01); controls.update();
model.traverse((child) => { if (child instanceof THREE.Points) { const geometry = child.geometry; const oldPositions = geometry.getAttribute('position').array; const newPositions = new Float32Array(oldPositions.length); for (let i = 0; i < oldPositions.length; i++) { newPositions[i] = oldPositions[i] + (colors[i] - oldPositions[i]) * lerpSpeed; } geometry.setAttribute('position', new THREE.BufferAttribute(newPositions, 3)); } }); renderer.render(scene, camera);} });
- Assistant
Попробуйте заменить этот блок кода:
geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(colors), 3));на следующий:
geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), 3));Таким образом, позиции будут назначены вершинам, а не цветам. Полный фрагмент кода для этого изменения:
model.traverse((child) => { if (child.isMesh) { const geometry = child.geometry; const material = new THREE.PointsMaterial({ size: 0.01, vertexColors: true // включаем использование цветов вершин }); const points = new THREE.Points(geometry, material); // получаем массив вершин из объекта geometry const positions = geometry.getAttribute('position').array; // создаем массив цветов для вершин const colors = []; for (let i = 0; i < positions.length; i += 3) { colors.push(Math.random(), Math.random(), Math.random()); // создаем случайный цвет для каждой вершины } let midPosition = [...positions]; // задаем массив цветов в объекте geometry geometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3)); geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), 3)); child.parent.add(points); child.parent.remove(child); } });
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.