【问题标题】:Three.js texture for mesh with custom geometry具有自定义几何形状的网格的 Three.js 纹理
【发布时间】:2013-02-06 05:24:44
【问题描述】:

我正在尝试创建房屋几何图形并将不同的纹理附加到几何图形的表面。我正在使用r55。问题是具有从纹理创建的材质的面不会出现。然而,具有简单颜色材料的面会出现。如果我将roofTexture 生成的材质替换为简单的颜色材质,使用该材质的面也会正确显示。

这是我的代码的相关部分:

var geom = new THREE.Geometry();

// Load the roof texture
var roofTexture = new THREE.ImageUtils.loadTexture('gfx/textures/roof.jpg');

// Let the roof texture repeat itself
roofTexture.wrapS = roofTexture.wrapT = THREE.RepeatWrapping;
roofTexture.repeat.set(10, 10);

// Materials
var materialArray = [];
materialArray.push(new THREE.MeshLambertMaterial({color: 0xD3E3F0 }));
materialArray.push(new THREE.MeshLambertMaterial({map: roofTexture}));

// Base edges
var edge0 = new THREE.Vector2(obj.ridgeLength/2, -obj.buildingDepth/2);
var edge1 = new THREE.Vector2(obj.ridgeLength/2, obj.buildingDepth/2);
var edge2 = new THREE.Vector2(-obj.ridgeLength/2, obj.buildingDepth/2);
var edge3 = new THREE.Vector2(-obj.ridgeLength/2, -obj.buildingDepth/2);

// Floor
geom.vertices.push(new THREE.Vector3(edge0.x, -1, edge0.y));
geom.vertices.push(new THREE.Vector3(edge1.x, -1, edge1.y));
geom.vertices.push(new THREE.Vector3(edge2.x, -1, edge2.y));
geom.vertices.push(new THREE.Vector3(edge3.x, -1, edge3.y));

// Eave
geom.vertices.push(new THREE.Vector3(edge0.x, obj.eaveHeight, edge0.y));
geom.vertices.push(new THREE.Vector3(edge1.x, obj.eaveHeight, edge1.y));
geom.vertices.push(new THREE.Vector3(edge2.x, obj.eaveHeight, edge2.y));
geom.vertices.push(new THREE.Vector3(edge3.x, obj.eaveHeight, edge3.y));

// Ridge
geom.vertices.push(new THREE.Vector3(obj.ridgeLength/2, obj.ridgeHeight, 0));
geom.vertices.push(new THREE.Vector3(-obj.ridgeLength/2, obj.ridgeHeight, 0));


// Ground
geom.faces.push( new THREE.Face4(0, 0, 0, 0) );

// Front
geom.faces.push( new THREE.Face4(0, 3, 7, 4) );

// Left side
geom.faces.push( new THREE.Face4(0, 4, 5, 1) );

// Back
geom.faces.push( new THREE.Face4(1, 5, 6, 2) );

// Right side
geom.faces.push( new THREE.Face4(2, 6, 7, 3) );

// Left triangle
geom.faces.push( new THREE.Face3(4, 8, 5));

// Right triangle
geom.faces.push( new THREE.Face3(6, 9, 7));

// Front roof
geom.faces.push( new THREE.Face4(7, 9, 8, 4) );

// Back roof
geom.faces.push( new THREE.Face4(5, 8, 9, 6) );

// Assign materials to the faces
geom.faces[0].materialIndex = 0;
geom.faces[1].materialIndex = 0;
geom.faces[2].materialIndex = 0;
geom.faces[3].materialIndex = 0;
geom.faces[4].materialIndex = 0;
geom.faces[5].materialIndex = 0;
geom.faces[6].materialIndex = 0;
geom.faces[7].materialIndex = 1;
geom.faces[8].materialIndex = 1;

geom.computeFaceNormals();

obj.house = new THREE.Mesh( geom, new THREE.MeshFaceMaterial(materialArray) );
obj.house.doubleSided = true;
obj.house.castShadow = true;
obj.sun.shadowDarkness = 1.0;
obj.scene.add(obj.house);

我做错了什么?

【问题讨论】:

  • 嗨,我也在用三个js创建一个车库。我必须在外壁和内壁上应用不同的纹理。该方法与您类似,创建顶点并将面添加到几何体,但一个纹理会应用于所有墙壁,如果我为材质提供数组它不起作用,甚至墙壁都不可见。你找到解决办法了吗?

标签: javascript three.js textures


【解决方案1】:

您的几何图形上缺少 UV 坐标。 UV 坐标从 0 到 1,因此由于您自己创建几何图形,因此您可以在右下角 (1.0, 0.0)、左上角 (0.0, 1.0) 和右上角 ( 1.0, 1.0)。您可以查看 PlaneGeometry.js 文件以了解如何分配 UV。

【讨论】:

  • 对于那些寻找语法的人来说:geom.faceVertexUvs[0].push([new THREE.Vector2(0, 0),new THREE.Vector2(0, 1),new THREE.Vector2(1, 1)]);。这将 uv 坐标 (0,0),(0,1),(1,1) 分配给第一个面的三个顶点。要将 uv 分配给下一张脸,请执行 geom.faceVertexUvs[0].push(...);.
猜你喜欢
  • 2011-11-02
  • 2017-03-28
  • 2019-08-23
  • 2012-11-02
  • 2013-10-12
  • 2016-10-31
  • 2012-11-02
  • 2016-11-16
  • 2021-11-28
相关资源
最近更新 更多