javascript - Three.js 中的插值曲面

标签 javascript plot three.js surface

大家好,

我有一个关于 Three.js 中的曲面的问题: 我有一堆 Vec3 点,想要通过它们插值一个曲面。在搜索时,我偶然发现了贝塞尔曲线( three.js bezier - 仅作为线条),看起来更像我正在搜索: three.js Nurbs 。我尝试重建代码,但文档很糟糕(像 this 这样的页面),并且我没有通过重建代码了解一切是如何工作的...

所以问题是: 有没有简单的方法可以从我的计算点中获得形状? (如果没有插值,我仍然会很高兴)。

谢谢大家!

垫子

编辑:我想要实现的是表面图。我偶然发现http://acko.net/blog/making-mathbox/但它对于我的需求来说太大了......

最佳答案

经过一番尝试和错误,我找到了一个解决方案:添加一个平面,然后变换单个顶点。

// need to setup 'step', 'xStart', 'xEnd', 'yStart', 'yEnd'

// calc the variables
var width = Math.abs(-xStart+xEnd),
    height = Math.abs(-yStart+yEnd);

var stepsX = width*step, stepsY = height*step;

var posX = (xStart+xEnd)/2;
var posZ = (yStart+yEnd)/2;

// add a plane and morph it to a function
var geometry = new THREE.PlaneGeometry( width, height, stepsX - 1, stepsY - 1 );
geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );

var size = stepsX * (stepsY), 
    data = new Float32Array( size );

var count = 0, scope = {};

mesh = new THREE.Mesh( geometry, new THREE.MeshNormalMaterial( { 
    side : THREE.DoubleSide,
    transparent: true,
    shading: THREE.SmoothShading,
    opacity : _opacity }));


mesh.updateMatrixWorld();

// calc y value for every vertice
for ( var i = 0; i < size; i ++ ) {
    // calculate the current values
    // http://stackoverflow.com/questions/11495089/how-to-get-the-absolute-position-of-a-vertex-in-three-js
    var vector = mesh.geometry.vertices[i].clone();
    vector.applyMatrix4( 
        mesh.matrixWorld
        );
    // set them into the scope
    scope.x = vector.x + posX;
    scope.y = vector.z + posZ;
    // calculate point and write it in a temp array
    data[i] = math.eval(term, scope);
}

// push the new vertice data
for ( var i = 0, l = geometry.vertices.length; i < l; i ++ ) {
    geometry.vertices[ i ].y = data[ i ];
}

// update the new normals
geometry.computeFaceNormals();
geometry.computeVertexNormals();

// add to scene
scene.add( mesh );

唯一的问题是它不适用于像 tan(x) 这样的非静态函数。此片段使用 math.js计算该术语。

问候垫

关于javascript - Three.js 中的插值曲面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24662398/

相关文章:

python - python中的plot()错误消息: No module named 'plotting'

three.js - 为什么我必须标准化坐标?

javascript - 使用 Threejs 计算 Delta 四元数

javascript - Three.js 动画中的时间

javascript - 将数组的元素转换为对象/Javascript

javascript - Firefox 插件引用错误

Matlab - 将 Y 轴移动到原点?

python - matplotlib 中的直方图,x 轴上的时间

javascript - Promise.prototype.then() 行为

javascript - 是否可以将 domparser 元素更改为字符串?