gpt4 book ai didi

javascript - 使用 Three.js 和 PLY 文件的纹理

转载 作者:行者123 更新时间:2023-11-29 19:55:58 37 4
gpt4 key购买 nike

我现在可以正常加载 ascii ply 文件,但它有一个纹理,无论我如何配置,我似乎都无法加载它。

    cameraMain = new THREE.Camera(8, MainWidth / MainHeight, 1, 10000);
sceneMain = new THREE.Scene();
ambientLightMain = new THREE.AmbientLight(0x202020);
directionalLightMainRight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLightMainLeft = new THREE.DirectionalLight(0xffffff, 0.5);
pointLightMain = new THREE.PointLight(0xffffff, 0.3);

directionalLightMainRight.position.x = dlpx;
directionalLightMainRight.position.y = dlpy;
directionalLightMainRight.position.z = dlpz;
directionalLightMainRight.position.normalize();

directionalLightMainLeft.position.x = -dlpx;
directionalLightMainLeft.position.y = dlpy;
directionalLightMainLeft.position.z = dlpz;
directionalLightMainLeft.position.normalize();

pointLightMain.position.x = plpx;
pointLightMain.position.y = plpy;
pointLightMain.position.z = plpz;

sceneMain.addLight(ambientLightMain);
sceneMain.addLight(directionalLightMainRight);
sceneMain.addLight(directionalLightMainLeft);
sceneMain.addLight(pointLightMain);

rendererMain = new THREE.WebGLRenderer();
texture = THREE.ImageUtils.loadTexture('3D/'+ mainURL +'.jpg', {}, function() {
rendererMain.render(sceneMain);
});

rendererMain.domElement.style.backgroundColor = backgroundColor;
rendererMain.setSize(MainWidth, MainHeight);
rendererMain.domElement.addEventListener('DOMMouseScroll', onRendererMainScroll, false);
rendererMain.domElement.addEventListener('mousewheel', onRendererMainScroll, false);
rendererMain.domElement.addEventListener('dblclick', onRendererMainDblClick, false);
rendererMain.domElement.addEventListener('mousedown', onRendererMainMouseDown, false);

$("#viewerMain").append(rendererMain.domElement);

window.addEventListener('mousemove', onMouseMove, false);
window.addEventListener('mouseup', onMouseUp, false);

那段代码初始化了模型。其中一些不需要用于提问目的,它有用于缩放、旋转等的代码。我知道还有另一行,我已将其注释掉,但出于某种原因它不会在此处显示,所以我将单独进行。

material = new THREE.MeshBasicMaterial({map: texture});

该行在 loadTexture 代码之后。

剩下的部分用loadPly函数加载

        var geometryMain = new THREE.Geometry();
for (i in event.data.content[0])
geometryMain.vertices.push(new THREE.Vertex(new THREE.Vector3(event.data.content[0][i][0], event.data.content[0][i][1], event.data.content[0][i][2])));
for (i in event.data.content[1])
geometryMain.faces.push(new THREE.Face3(event.data.content[1][i][0], event.data.content[1][i][1], event.data.content[1][i][2]));
geometryMain.computeCentroids();
geometryMain.computeFaceNormals();
mainModel = new THREE.Mesh(geometryMain, new THREE.MeshLambertMaterial({color:0xffffff, shading:THREE.FlatShading}));
sceneMain.addObject(mainModel);
mainModel.overdraw = true;
mainModel.doubleSided = true;

我尝试调整 THREE.mesh() 部分以包含 Material ,但它破坏了一切。我尝试过将 map:material 添加到 MeshLambertMaterial,同样不行。任何人有任何见解?抱歉,如果这太复杂了,但我还远远不够了解这一点以提高效率。

如果我添加

map:THREE.ImageUtils.loadTexture('3D/'+ mainURL +'.jpg')

到 THREE.Mesh() 行,我得到

.WebGLRenderingContext: GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 2

最佳答案

为了在对象上显示纹理,需要纹理坐标。您似乎只向几何体添加顶点和面。查看THREE.PLYLoader 来源,它似乎不支持加载纹理坐标。 (虽然我不确定你是否使用那个加载器,因为它应该已经返回一个准备好的 Geometry 实例,而不是要求你手动构造数组)

关于javascript - 使用 Three.js 和 PLY 文件的纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16109774/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com