javascript - 是否可以随着时间的推移更改 widthSegments 参数(来自 SphereGeometry)?

标签 javascript three.js

使用 Three.JS 我有以下球体几何形状:

        var radius = 1 ;
        var widthSegments = 12;
        var heightSegments = 12;

        var geometry = new THREE.SphereGeometry(radius, widthSegments, heightSegments);
        const material = new THREE.PointsMaterial({
            color: 'white',
            size: 0.01, 
        });

我希望它的属性 widthSegments 作为时间的函数进行更改。所以我尝试创建以下函数:

  var ChangeWidth = function (){
        var time = Date.now() * 0.0005;
        widthSegments =  Math.sin(time * 0.7) * 30;
    }

然后将其添加到我的更新函数

   function update() 
        {
            requestAnimationFrame( update );
            renderer.render( scene, camera );
            animate();    
            ChangeWidth();
        }

但是,注意实际上已经改变了。是否可以更改 widthSegments 以及变量 geometry

编辑

用于删除几何图形并使用新的 widthSegments 值重新创建几何图形的新函数

function update() {
            scene.add(points);
            requestAnimationFrame( update );
            renderer.render( scene, camera );
            }
        update();

        while (widthSegments < 60){

        // I begin the loop by destroying the older geometry
        var DestroyGeometry = function() {
            scene.remove(points);
            points.geometry.dispose();
        }

        DestroyGeometry();    

        // Then, I create a new geometry
        const newGeometry = new THREE.SphereBufferGeometry( radius, widthSegments, heightSegments );
        points = new THREE.Points( newGeometry, material );

        // Making sure the widthSegments is going to be increase
        widthSegments += 0.5; 

        // I end creating a new function to render the NewGeometry
        var Recreate = function() {
            scene.add(points);
            requestAnimationFrame( update );
            renderer.render( scene, camera );
        }

        Recreate();
        }

但这显然行不通。 DestroyGeometry()Recreate() 函数根本没有任何效果。我想知道是否可以将它们放在 while 循环中。

最佳答案

Is it possible to change widthSegments and consequently the variable geometry?

不,所有几何生成器的参数仅在构造几何时才会评估。如果您想随着时间的推移更改参数,则必须删除旧的几何图形并创建新的几何图形。像这样的东西:

scene.remove( points );
points.geometry.dispose(); // free internal buffers

const newGeometry = new THREE.SphereBufferGeometry( radius, widthSegments, heightSegments );
points = new THREE.Points( newGeometry, material ); // create new point cloud with new geometry and existing material
scene.add( points );

关于javascript - 是否可以随着时间的推移更改 widthSegments 参数(来自 SphereGeometry)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61283917/

相关文章:

div 的 JavaScript 淡入淡出效果

javascript - 如何使用 JavaScript 突出显示并保存 HTML 中的文本?

javascript - 相机在 three.js 中找不到对象

webgl - 如何在 Three.js 中使用和混合多个纹理与自定义值?

user-interface - 如何删除 dat.GUI 元素?

c# - 在 WebBrowser 中调用一个脚本,并等待它完成运行(同步)

javascript - 使用 Azure VM Javascript SDK 从 RunCommand 获取 StdOut

Javascript 在手机上缩小

three.js - 如何将等距柱状投影环境贴图添加到 A-Frame 中的 gltf 模型?

javascript - 导入 LOCAL .obj 以使用 React 使用 Three.js OBJLoader 加载