Canvas three.js:如何让多个材质为一个网格工作
我对three.js有一个大问题: 我想要一个简单的立方体,每个面上有不同的颜色。我试过这样做:Canvas three.js:如何让多个材质为一个网格工作,canvas,three.js,webgl,javascript,Canvas,Three.js,Webgl,Javascript,我对three.js有一个大问题: 我想要一个简单的立方体,每个面上有不同的颜色。我试过这样做: // set the scene size var WIDTH = jQuery('#showcase').width() - 20, HEIGHT = jQuery('#showcase').height(); // set some camera attributes var VIEW_ANGLE = 45, ASPECT = WIDTH / HEIGHT, NEAR
// set the scene size
var WIDTH = jQuery('#showcase').width() - 20, HEIGHT = jQuery('#showcase').height();
// set some camera attributes
var VIEW_ANGLE = 45, ASPECT = WIDTH / HEIGHT, NEAR = 0.1, FAR = 10000000;
this.container = jQuery('#showcase');
this.renderer = new THREE.WebGLRenderer();
this.camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
this.scene = new THREE.Scene();
this.scene.add(this.camera);
// camera start position
this.camera.position.z = this.model.radius;
this.camera.position.y = this.model.cameraY;
this.camera.position.x = 0;
this.camera.lookAt(this.scene.position);
this.renderer.setSize(WIDTH, HEIGHT);
this.container.append(this.renderer.domElement);
//Multiple Colors
var materials = [new THREE.MeshBasicMaterial({
color : 0xFF0000
}), new THREE.MeshBasicMaterial({
color : 0x00FF00
}), new THREE.MeshBasicMaterial({
color : 0x0000FF
}), new THREE.MeshBasicMaterial({
color : 0xAA0000
}), new THREE.MeshBasicMaterial({
color : 0x00AA00
}), new THREE.MeshBasicMaterial({
color : 0x0000AA
})];
this.geometry = new THREE.CubeGeometry(100, 100, 100, materials);
this.mesh = new THREE.Mesh(this.geometry, new THREE.MeshFaceMaterial());
this.scene.add(this.mesh);
// create a point light
this.pointLight = new THREE.PointLight(0xFFFFFF);
this.pointLight.position = this.scene.position;
this.scene.add(this.pointLight);
this.renderer.render(this.scene, this.camera);
但我得到了这个错误信息:
“未捕获的TypeError:无法从three.js中的第19152行读取未定义的属性'map'
对我来说,这似乎是webgl渲染器的问题。在我在这里和其他地方找到的大多数主题中,它们都使用画布渲染器。但我想继续使用webgl渲染器
有人知道这个问题吗?
非常感谢:-)试试这个
this.geometry = new THREE.CubeGeometry(100, 100, 100);
this.mesh = new THREE.Mesh(this.geometry, new THREE.MeshFaceMaterial(materials));
也许可以尝试使用数组 对于three.js 49(如果它与57一起使用,则不必考虑),我将此代码用于具有不同材质的skybox:
var axes = new THREE.AxisHelper();
scene.add( axes );
var materialArray = [];
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/himmel.jpg' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/himmel.jpg' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/himmel.jpg' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/himmel.jpg' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/himmel.jpg' ) }));
materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/himmel.jpg' ) }));
var skyboxGeom = new THREE.CubeGeometry( 5000, 5000, 5000, 1, 1, 1, materialArray );
var skybox = new THREE.Mesh( skyboxGeom, new THREE.MeshFaceMaterial() );
skybox.flipSided = true;
scene.add( skybox );
也可以尝试使用顶点颜色
var geom = new THREE.BoxGeometry(20, 20, 20);
var faceIndices = ['a', 'b', 'c'];
var vertexIndex, point;
mat = new THREE.MeshBasicMaterial({
vertexColors: THREE.VertexColors
});
mesh = new THREE.Mesh(geom, mat);
scene.add(mesh);
geom.faces.forEach(function (face) { // loop through faces
for (var i = 0; i < 3; i++) {
vertexIndex = face[ faceIndices];
point = geom.vertices[vertexIndex];
color = new THREE.Color(
point.x + 0.5,
point.y + 0.5,
point.z + 0.5
);
face.vertexColors[ i ] = [![the output will be as in the attached picture][1]][1]color;
}
});
var geom=新的三箱几何结构(20,20,20);
var FaceIndex=['a','b','c'];
var vertexIndex,点;
mat=新的三网格基本材料({
顶点颜色:三。顶点颜色
});
网格=新的三个网格(geom、mat);
场景。添加(网格);
forEach(函数(面){//通过面循环
对于(变量i=0;i<3;i++){
顶点索引=面[面索引];
点=几何顶点[顶点索引];
颜色=新的三种颜色(
点x+0.5,
点y+0.5,
点z+0.5
);
face.vertexColors[i]=[![输出将如所附图片中所示][1][1]颜色;
}
});
你可以在找到答案,但它是如何将材质分布到面上的?这篇文章提供了有关材质到面映射的更多信息: