как изменить этот код, чтобы размер точек немного колебался от точки к точке? import *…
- User
как изменить этот код, чтобы размер точек немного колебался от точки к точке? import * as THREE from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'; import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer'; import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass'; import { BloomPass } from 'three/examples/jsm/postprocessing/BloomPass';
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 controls = new OrbitControls(camera, renderer.domElement); controls.enableZoom = false; controls.enableDamping = true; controls.dampingFactor = 0.05;
// добавляем композитор для использования эффектов const composer = new EffectComposer(renderer);
// добавляем RenderPass const renderPass = new RenderPass(scene, camera); composer.addPass(renderPass);
// добавляем BloomPass const bloomPass = new BloomPass(1, 25, 4, 256); composer.addPass(bloomPass);
const loader = new GLTFLoader(); const url = 'GRAF_3_13.glb';
loader.load(url, function (gltf) { const model = gltf.scene;
model.traverse((child) => { if (child.isMesh) { const geometry = child.geometry;
const texture = new THREE.TextureLoader().load('Ellipse 2.png'); const materialPoint = new THREE.PointsMaterial({ size: 0.03, sizeAttenuation: true, map: texture, alphaTest: 0.01, transparent: true, color: new THREE.Color(1.2, 1.2, 1.2) // увеличиваем яркость на 20% }); const geometryPoint = new THREE.BufferGeometry(); const positions = geometry.getAttribute('position').array; const randomPosition = []; for (let i = 0; i < positions.length; i += 3) { randomPosition.push(10.0 * Math.random() - 5.0, 10.0 * Math.random() - 5.0, 10.0 * Math.random() - 5.0); } const randomPositionCopy = [...randomPosition]; let increment = 0.002; geometryPoint.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); const points = new THREE.Points(geometryPoint, materialPoint); geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(randomPosition), 3)); function updateArray(increment) { for (let j = 0; j < 75; j++) { for (let i = 0; i < randomPosition.length; i++) { if (randomPosition[i] < positions[i]) { randomPosition[i] = randomPosition[i] + increment; } else if (randomPosition[i] > positions[i]) { randomPosition[i] = randomPosition[i] - increment; } else if ((randomPosition[i] - positions[i]) < 0.002) { randomPosition[i] = positions[i]; } } } geometryPoint.setAttribute('position', new THREE.BufferAttribute(new Float32Array(randomPosition), 3)); } function reverseUpdateArray(increment) { for (let j = 0; j < 75; j++) { for (let i = 0; i < randomPosition.length; i++) { if (randomPosition[i] < randomPositionCopy[i]) { randomPosition[i] = randomPosition[i] + increment; } else if (randomPosition[i] > randomPositionCopy[i]) { randomPosition[i] = randomPosition[i] - increment; } else if ((randomPosition[i] - randomPositionCopy[i]) < 0.002) { randomPosition[i] = randomPositionCopy[i]; } } } geometryPoint.setAttribute('position', new THREE.BufferAttribute(new Float32Array(randomPosition), 3)); } let time = 0; let sinPosition; let sinPosition2; let cosPosition; let randSignArray = []; let sign; let ratio = 1.0; for (let i = 0; i < randomPosition.length; i++) { sign = Math.random() < 0.5 ? -1.0 : 1.0; randSignArray[i] = sign; } animateVerticles(); function animateVerticles() { requestAnimationFrame(animateVerticles); time += 0.1; sinPosition = Math.sin(time) * 0.1; cosPosition = Math.cos(time * 0.1) * 0.1; sinPosition2 = Math.sin(time / Math.PI) * 0.1; for (let i = 0; i < randomPosition.length; i++) { randomPosition[i] += (sinPosition * cosPosition * sinPosition2) * randSignArray[i] * ratio; } geometryPoint.setAttribute('position', new THREE.BufferAttribute(new Float32Array(randomPosition), 3)); } window.addEventListener('wheel', function (event) { if (event.deltaY > 0) { updateArray(increment); if (ratio > 0.0) { ratio -= 0.01; } } else { reverseUpdateArray(increment); ratio += 0.01; } console.log(ratio); }); // устанавливаем needsUpdate для текстуры, чтобы она обновлялась каждый кадр materialPoint.map.needsUpdate = true; scene.add(points); points.rotateX(Math.PI / 2.0); } - Assistant
Для изменения размера точек можно использовать свойство "size" в Three.js, которое можно изменять для каждой точки. Также можно добавить случайную вариацию размера для каждой точки, используя функцию Math.random(). Вот пример изменения размера точек в коде:
// задаем начальный размер let startSize = 0.03;
// добавляем случайную вариацию размера const sizeVariation = 0.01;
const materialPoint = new THREE.PointsMaterial({ size: startSize, sizeAttenuation: true, map: texture, alphaTest: 0.01, transparent: true, color: new THREE.Color(1.2, 1.2, 1.2) });
const geometryPoint = new THREE.BufferGeometry();
- User
весь код
- Assistant