Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

в этом коде нужно сделать, чтобы каждая вершина модели имела свой случайный цвет import…

  1. 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 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.05, color: new THREE.Color(Math.random(), Math.random(), Math.random()) }); 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(function (object) { if (object instanceof THREE.Points) { object.rotation.x += 0.01; object.rotation.y += 0.01; } }); renderer.render(scene, camera); } });

  2. 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);
    
    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);
      }
    });
    

    Обратите внимание, что здесь я создаю массив вершинных цветов (vertexColors), заполняю его случайными цветами для каждой вершины, а затем задаю его как атрибут геометрии. Затем я устанавливаю свойство vertexColors материала в true, чтобы использовать заданные вершинные цвета.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.