【发布时间】:2021-02-10 22:51:53
【问题描述】:
以下两个球体之间的区别 - 就其渐变颜色的应用方式而言,归结为一种说法:
sphereGeometry = sphereGeometry.toNonIndexed();
因为我真的很喜欢 .toNonIndexed() 为我们提供的更平滑的外观,所以我尝试将其应用于 THREE.js GIT 上可用的一些导入的“.glb”模型 - 但它不起作用。
例如,当我使用此处提供的马模型时会发生以下情况:https://github.com/mrdoob/three.js/blob/master/examples/models/gltf/Horse.glb
它基本上完全忽略了我的颜色,出于某种原因默认为红色和黑色。
但是当我注释掉 .toNonIndexed() 行时,它会给出我要求的颜色 - 除非你肯定看到三角形,这是我试图避免的外观:
这是我加载对象的代码:
function loadAny3DModel() {
loader.load("./Horse.glb", function(theHorse) {
console.log("===>'theHorse' has arrived!!!\n");
var horseScene = theHorse.scene;
horseMesh = horseScene.children[0];
var horseGeometry = horseMesh.geometry;
let horseMat = horseMesh.material;
var horseVertexPositionsArray = horseGeometry.attributes.position;
// Here's the command that isn't working:
// horseGeometry = horseGeometry.toNonIndexed();
// horseVertexPositionsArray = horseGeometry.attributes.position;
let theColor = new THREE.Color();
let colorsArray = [];
for(let i = 0; i < horseVertexPositionsArray.count; i++) {
let randC1 = "purple";
let randC2 = "white";
let chosenColor = i % 2 == 0 ? randC1 : randC2;
theColor.set(chosenColor);
colorsArray.push(theColor.r, theColor.g, theColor.b);
}
horseGeometry.setAttribute("color", new THREE.Float32BufferAttribute(colorsArray, 3));
horseMat.vertexColors = true;
render();
scene.add(horseScene);
}
}
我应该怎么做才能获得更平滑的渐变?
================================================ =======================
更新:
这是我尝试做的一个非常粗略的想法:将渐变扩展到整个模型,而不是形成模型的每个三角形。 (将这张图片与上一张进行比较。)
【问题讨论】:
-
你无法避开马 GLTF 上的三角形。这是一个非常低多边形的模型,所以你总是能够看到几何图形。也许您想将其导入到 Blender 之类的 3D 编辑器中并对其进行细分,使几何体变得更平滑和更高密度,然后您可以再次将其导出为 GLTF。
-
@Marquizzo 我明白你在说什么,但即使我要在 Blender 中更改模型的几何形状并使其不那么三角形,问题仍然存在,那就是当我导入模型到
THREE.js并在其几何上调用.toNonIndexed(),我所有的颜色都会完全消失。这就是我需要帮助的地方。想办法解决这个问题,这样它就不会发生。
标签: javascript three.js