Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

как в этом коде точки THREE.Points заменить на THREE.CircleGeometry? вот код: // import…

  1. User

    как в этом коде точки THREE.Points заменить на THREE.CircleGeometry?

    вот код: // import * as THREE from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';

    class Stage { constructor() { this.renderParam = { clearColor: 0x000000, // фон width: window.innerWidth, height: window.innerHeight }; this.cameraParam = { fov: 20, near: 0.1, far: 100, lookAt: new THREE.Vector3(0, 0, 0), x: 0, y: 0, z: 8 };

        this.scene = null;
        this.camera = null;
        this.renderer = null;
        this.isInitialized = false;
    
    }
    
    init() {
        this.setScene();
        this.setRender();
        this.setCamera();
        this.isInitialized = true;
    }
    
    setScene() {
        this.scene = new THREE.Scene();
        //this.scene.add(new THREE.GridHelper(1000, 100));
        //this.scene.add(new THREE.AxesHelper(100));
    }
    
    setRender() {
        this.renderer = new THREE.WebGLRenderer();
        this.renderer.setPixelRatio(window.devicePixelRatio);
        this.renderer.setClearColor(new THREE.Color(this.renderParam.clearColor));
        this.renderer.setSize(this.renderParam.width, this.renderParam.height);
        const wrapper = document.querySelector("#webgl");
        wrapper.appendChild(this.renderer.domElement);
    
    
    }
    
    setCamera() {
        if (!this.isInitialized) {
            this.camera = new THREE.PerspectiveCamera(
                0,
                0,
                this.cameraParam.near,
                this.cameraParam.far
            );
    
            this.camera.position.set(
                this.cameraParam.x,
                this.cameraParam.y,
                this.cameraParam.z
            );
            this.camera.lookAt(this.cameraParam.lookAt);
                
            // Control.
            const controls         = new OrbitControls(this.camera, this.renderer.domElement);
            controls.enableZoom    = false;
            controls.enableDamping = true;
            controls.dampingFactor = 0.05;
        }
    
        const windowWidth = window.innerWidth;
        const windowHeight = window.innerHeight;
        this.camera.aspect = windowWidth / windowHeight;
        this.camera.fov = this.cameraParam.fov;
    
        this.camera.updateProjectionMatrix();
        this.renderer.setSize(windowWidth, windowHeight);
        
    }
    
    render() {
        this.renderer.render(this.scene, this.camera);
    }
    
    onResize() {
        this.setCamera();
    }
    
    onRaf() {
        this.render();
    }
    

    }

    const loader = new GLTFLoader(); const url = 'GRAF_3_26.glb'; loader.load(url, function (gltf) { const model = gltf.scene;

    model.traverse((child) => {
        if (child.isMesh) {
            const geometryLogo = child.geometry;
            // console.log(geometryLogo);
    
            class Mesh {
                constructor(stage) {
                    this.rotationPower = 0.0000; // скорость вращения
                    this.stage = stage;
                }
                
                init() {
                    this.setMesh();
                    this.setScroll();
                }
                // Создает массив точек, лежащих на поверхности
                getGeometryPosition(geometry) {
                    const numParticles = 100000; // кол-во точек
                    const material = new THREE.MeshBasicMaterial();
                    const mesh = new THREE.Mesh(geometry, material);
                    const sampler = new THREE.MeshSurfaceSampler(mesh).build();
                    const particlesPosition = new Float32Array(numParticles * 3);
                    
                    for (let i = 0; i < numParticles; i++) {
                        const newPosition = new THREE.Vector3();
                        const normal = new THREE.Vector3();
                        sampler.sample(newPosition, normal);
                        particlesPosition.set([newPosition.x, newPosition.y, newPosition.z],i * 3);
                    }
                    return particlesPosition;
                }
        
                setMesh() {
                    // const positions = geometryLogo.getAttribute('position').array;
                    const geometry = new THREE.BufferGeometry();
                    const Logo = this.getGeometryPosition(geometryLogo.toNonIndexed());
                    console.log(geometryLogo);
                    const firstPos = this.getGeometryPosition(new THREE.SphereBufferGeometry(1, 32, 32).toNonIndexed());
                    // const secPos = this.getGeometryPosition(new THREE.TorusBufferGeometry(0.7, 0.3, 32, 32).toNonIndexed());
                    // const thirdPos = this.getGeometryPosition(new THREE.TorusKnotBufferGeometry(0.6, 0.25, 300, 20, 6, 10).toNonIndexed());
                    // const forthPos = this.getGeometryPosition(new THREE.CylinderBufferGeometry(1, 1, 1, 32, 32).toNonIndexed());
                    // const fivePos = this.getGeometryPosition(new THREE.IcosahedronBufferGeometry(1.1, 0).toNonIndexed());
                    // console.log(geometry);
                    const texture2 = new THREE.TextureLoader().load('Rectangle 56.png');
                    const texture = new THREE.TextureLoader().load('gradient-3.png');
                    let uniforms = {
                        mouseCoord: {
                            type: 'v3',
                            value: new THREE.Vector3()
                        },
                        mouseCoord: {
                            type: 'v3',
                            value: new THREE.Vector3()
                        },
                        tex: {
                            type: 't',
                            value: texture2
                        },
                        opacity: {
                            type: 'f',
                            value: 0.5
                        },
                        res: {
                            type: 'v2',
                            value: new THREE.Vector2(window.innerWidth, window.innerHeight)
                        },
                        u_sec1: { type: "f", value: 0.0 },
                        u_sec2: { type: "f", value: 0.0 }
                    };
                    const material = new THREE.RawShaderMaterial({
                        vertexShader: ``,
                        fragmentShader: ``,
                        uniforms: uniforms,
                        transparent: true,
                        blending: THREE.AdditiveBlending,
                    });
                    uniforms.mouseCoord.value.z = 50;
                    let time = 0;
                    function animate() {
                        requestAnimationFrame(animate);
                        time += 0.006;
                        uniforms.mouseCoord.value.x = Math.sin(time) / 2 + 0.5;
                        console.log('x =' + uniforms.mouseCoord.value.x);
                        uniforms.mouseCoord.value.y = Math.cos(time) / 2 + 0.5;
                        console.log('y =' + uniforms.mouseCoord.value.y);
                    }
                    animate();
                    geometry.setAttribute("position", new THREE.BufferAttribute(firstPos, 3));
                    // geometry.setAttribute("position", new THREE.BufferAttribute(Logo, 3));
                    geometry.setAttribute("secPosition", new THREE.BufferAttribute(Logo, 3));
                    geometry.setAttribute("thirdPosition", new THREE.BufferAttribute(Logo, 3));
                    // geometry.setAttribute("forthPosition", new THREE.BufferAttribute(Logo, 3));
                    // geometry.setAttribute("fivePosition",new THREE.BufferAttribute(Logo, 3));
                    
                    this.mesh = new THREE.Points(geometry, material);
                    
                    this.group = new THREE.Group();
                    this.group.add(this.mesh);
                    this.group.rotation.x = Math.PI / 2.0;
                    this.stage.scene.add(this.group);
                }
        
                setScroll() {
                    gsap.timeline({
                        defaults: {},
                        scrollTrigger: {
                            trigger: "body",
                            start: "top top",
                            end: "bottom bottom",
                            scrub: 2
                        }
                    }).to(this.mesh.rotation, {
                        x: Math.PI * 2,
                        y: Math.PI * 2,
                        z: Math.PI * 2
                    });
                    
                    /*Тут я изменил gsap.to на gsap.fromTo обозначив значения до и после*/
                    // gsap.to(this.mesh.material.uniforms.u_sec1, {
                    //         value: 1.0,
                    //         scrollTrigger: {
                    //                 trigger: ".s-1",
                    //                 start: "bottom bottom",
                    //                 end: "bottom top",
                    //                 scrub: .2
                    //             }
                    //         });
                            gsap.fromTo(this.mesh.material.uniforms.u_sec1, {
                                value: 5.0, // размер до
                            },{
                                value: 1.0, // размер после
                                scrollTrigger: {
                                    trigger: ".s-1",
                                    start: "bottom bottom",
                                    end: "bottom top",
                                    scrub: .2
                                }
                            });
                            gsap.to(this.mesh.material.uniforms.u_sec2, {
                                value: 1.0,
                                scrollTrigger: {
                                    trigger: ".s-2",
                            start: "bottom bottom",
                            end: "bottom top",
                            scrub: .2
                        }
                    });
                }
        
                render() {
                    // this.group.rotation.x += this.rotationPower;
                    // this.group.rotation.y += this.rotationPower;
                }
                
                
                
                onResize() {
                    //
                }
                
                onRaf() {
                    this.render();
                }
            }
    
            const init = () => {
                const stage = new Stage();
                stage.init();
                
                const mesh = new Mesh(stage);
                mesh.init();
                
                window.addEventListener("resize", () => {
                    stage.onResize();
                    mesh.onResize();
                });
                
                const raf = () => {
                    window.requestAnimationFrame(() => {
                        stage.onRaf();
                        mesh.onRaf();
                        raf();
                    });
                };
                raf();
                
            };
            init();
        }
    });
    

    });

  2. Assistant

    Чтобы заменить точки на геометрию круга в этом коде, сначала нужно создать экземпляр геометрии, затем создать экземпляр материала и передать его в конструктор THREE.Points. Вот как это может выглядеть:

    1. Создайте экземпляр геометрии круга:
    const circleGeometry = new THREE.CircleGeometry(1, 32);
    
    1. Создайте экземпляр материала:
    const material = new THREE.PointsMaterial({
      color: 0xffffff,
      size: 0.02
    });
    

    Обратите внимание, что свойство size задает размер точек в геометрии.

    1. Используйте геометрию и материал для создания экземпляра THREE.Points:
    this.mesh = new THREE.Points(circleGeometry, material);
    

    Полный код для замены точек на геометрию круга может выглядеть следующим образом:

    // Создайте экземпляр геометрии круга
    const circleGeometry = new THREE.CircleGeometry(1, 32);
    
    class Mesh {
      constructor(stage) {
        //...
      }
    
      setMesh() {
        //...
    
        // Создайте экземпляр материала
        const material = new THREE.PointsMaterial({
          color: 0xffffff,
          size: 0.02
        });
    
        // Используйте геометрию и материал для создания экземпляра THREE.Points
        this.mesh = new THREE.Points(circleGeometry, material);
    
        //...
      }
    
      //...
    }
    

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