javascript - THREE.js 规范化

标签 javascript three.js

我觉得我已经非常接近解决我手头的问题,但我不知道如何继续。 我的目标是通过烘焙顶点颜色来渲染球体。对于每个顶点,我需要通过平均面法线来计算其法线,这两条线应该是这样做的。

sphere.geometry.computeFaceNormals();
sphere.geometry.computeVertexNormals();

然后,我需要标准化并将标准化值传递为 Hue、Sat、Lightness,即 x->hue、y->sat、z->light 使用...

myGeometry.color.setHSL();

我的问题是我不确定如何对其进行标准化并从中获取可用于 HSL 的 x,y,z 值。非常感谢任何正确方向的帮助!这是我正在使用的相关代码。

var sphereBaked =  new THREE.MeshPhongMaterial();
sphere = new THREE.Mesh(new THREE.SphereGeometry( 150, 32, 32),sphereBaked);

sphere.geometry.computeFaceNormals();
sphere.geometry.computeVertexNormals();

//sphereBaked.color.setHSL();

sphere.position.set(0,150,0);
scene.add(sphere);

最佳答案

我不确定这是否是您真正需要的,但我尝试过:

// you could try something like this:
for ( var i = 0; i < myGeometry.faces.length; i ++ ) {

    // get current normal and normalize
    var currentNormal = myGeometry.faces[i].normal.clone();
    currentNormal.normalize();

    // iterate the vertexColors
    // like in http://stackoverflow.com/questions/17874682/dynamically-change-vertex-color-in-three-js
    for ( var j = 0; j < myGeometry.faces[i].vertexColors.length; j ++ ) {
        myGeometry.faces[i].vertexColors[j].setHSL(currentNormal.x, currentNormal.y, currentNormal.z);
    }
}

// force color update
myGeometry.colorsNeedUpdate = true;

希望对你有帮助!

关于javascript - THREE.js 规范化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28643135/

相关文章:

javascript - BoxHelper 让 BoxGeometry 变得巨大

performance - 如何使用Three.js InstancedBufferGeometry和InstancedBufferAttribute?

javascript - 在 Three.js 中标准化网格

node.js - node.js 中的 httpServer 服务于 three.js

javascript - 使用 Appcelerator Titanium 将行添加到 sqlite 数据库时出错

javascript - 如何在node js中一起使用writeFile和readFile

php - ExtJS 网格表单与 MySQL 中的文档

javascript - 如何在 three.js 中按深度顺序渲染粒子?

javascript - 在按钮上设置透明背景在 IE9 中不起作用

javascript - 当服务器关闭/崩溃时,Node JS 自动重启所有永远的 JS 进程