我使用 BufferGeometry 已经有一段时间了,我认为我对它非常熟悉。现在,我正在尝试创建一个简单的方形平面,但它什么也没做——据我所知,没有可见的平面,没有错误,也没有明显的问题。我在这里看到过其他类似的帖子,但没有一个解决方案成功。
当我检查场景时,网格就在那里,它有合适的材质,而且它的几何形状似乎设置正确。但我得到的只是一个黑色的视口。我必须错过一个痛苦的明显/简单的步骤,但现在它正在逃避我。我究竟做错了什么?
小提琴+代码:http: //jsfiddle.net/TheJim01/kafybhge/34/
// BufferGeometry Tester
var hostDiv, scene, renderer, camera, root, controls, light;
var WIDTH = 500;//window.innerWidth,
HEIGHT = 500;//window.innerHeight,
FOV = 35,
NEAR = 1,
FAR = 1000;
function createBufferGeometryMesh(){
var geo = new THREE.BufferGeometry();
var vertices =
[
-10., 10., 0., // 0 - top left
10., 10., 0., // 1 - top right
10., -10., 0., // 2 - bottom right
-10., -10., 0. // 3 - bottom left
],
normals =
[
0., 0., 1.,
0., 0., 1.,
0., 0., 1.,
0., 0., 1.
],
indices = [ 0, 1, 2, 0, 2, 3 ];
geo.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
geo.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
geo.addAttribute( 'index', new THREE.BufferAttribute( new Uint32Array( indices ), 1 ) );
var mat = new THREE.MeshPhongMaterial( {
color: 0xffffff,
ambient: 0xffffff,
specular: 0xffffff,
shininess: 50,
side: THREE.DoubleSide
} );
var msh = new THREE.Mesh(geo, mat);
return msh;
}
function init() {
hostDiv = document.createElement('div');
document.body.appendChild(hostDiv);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(WIDTH, HEIGHT);
hostDiv.appendChild(renderer.domElement);
camera = new THREE.PerspectiveCamera(FOV, WIDTH / HEIGHT, NEAR, FAR);
camera.position.z = 50;
controls = new THREE.TrackballControls(camera, renderer.domElement);
light = new THREE.PointLight(0xffffff, 1, 1000);
light.position.copy(camera.position);
scene = new THREE.Scene();
scene.add(camera);
scene.add(light);
var square = createBufferGeometryMesh();
scene.add(square);
animate();
}
function render() {
renderer.render(scene, camera);
}
function animate() {
light.position.copy(camera.position);
requestAnimationFrame(animate);
render();
controls.update();
}
init();
只是为了表明我对 BufferGeometry 并不陌生,这是我以前做过的事情,如果我将它插入到我的代码中代替createBufferGeometryMesh()
上面的内容,它就会起作用。我已经尝试定义像下面这样的缓冲区(甚至明确地),但它没有改变任何东西。
function colorCube(scale){
scale = scale || 1;
var geo = new THREE.BufferGeometry();
var positions = new Float32Array( 72 );
var normals = new Float32Array( 72 );
var colors = new Float32Array( 72 );
var indices = new Uint16Array( 36 );
var face = 0, idx = 0, vert = 0;
var x = 0, r = 0, y = 1, g = 1, z = 2, b = 2;
// front face (RED)
positions[vert + x] = 0.5; positions[vert + y] = 0.5; positions[vert + z] = 0.5;
normals[vert + x] = 0.; normals[vert + y] = 0.; normals[vert + z] = 1.;
colors[vert + r] = 1.; colors[vert + g] = 0.; colors[vert + b] = 0.;
vert += 3;
positions[vert + x] = -0.5; positions[vert + y] = 0.5; positions[vert + z] = 0.5;
normals[vert + x] = 0.; normals[vert + y] = 0.; normals[vert + z] = 1.;
colors[vert + r] = 1.; colors[vert + g] = 0.; colors[vert + b] = 0.;
vert += 3;
positions[vert + x] = -0.5; positions[vert + y] = -0.5; positions[vert + z] = 0.5;
normals[vert + x] = 0.; normals[vert + y] = 0.; normals[vert + z] = 1.;
colors[vert + r] = 1.; colors[vert + g] = 0.; colors[vert + b] = 0.;
vert += 3;
positions[vert + x] = 0.5; positions[vert + y] = -0.5; positions[vert + z] = 0.5;
normals[vert + x] = 0.; normals[vert + y] = 0.; normals[vert + z] = 1.;
colors[vert + r] = 1.; colors[vert + g] = 0.; colors[vert + b] = 0.;
vert += 3;
indices[idx + 0] = (face * 4) + 0; indices[idx + 1] = (face * 4) + 1; indices[idx + 2] = (face * 4) + 2;
indices[idx + 3] = (face * 4) + 0; indices[idx + 4] = (face * 4) + 2; indices[idx + 5] = (face * 4) + 3;
idx += 6;
++face;
// back face (BLUE)
positions[vert + x] = -0.5; positions[vert + y] = 0.5; positions[vert + z] = -0.5;
normals[vert + x] = 0.; normals[vert + y] = 0.; normals[vert + z] = -1.;
colors[vert + r] = 0.; colors[vert + g] = 0.; colors[vert + b] = 1.;
vert += 3;
positions[vert + x] = 0.5; positions[vert + y] = 0.5; positions[vert + z] = -0.5;
normals[vert + x] = 0.; normals[vert + y] = 0.; normals[vert + z] = -1.;
colors[vert + r] = 0.; colors[vert + g] = 0.; colors[vert + b] = 1.;
vert += 3;
positions[vert + x] = 0.5; positions[vert + y] = -0.5; positions[vert + z] = -0.5;
normals[vert + x] = 0.; normals[vert + y] = 0.; normals[vert + z] = -1.;
colors[vert + r] = 0.; colors[vert + g] = 0.; colors[vert + b] = 1.;
vert += 3;
positions[vert + x] = -0.5; positions[vert + y] = -0.5; positions[vert + z] = -0.5;
normals[vert + x] = 0.; normals[vert + y] = 0.; normals[vert + z] = -1.;
colors[vert + r] = 0.; colors[vert + g] = 0.; colors[vert + b] = 1.;
vert += 3;
indices[idx + 0] = (face * 4) + 0; indices[idx + 1] = (face * 4) + 1; indices[idx + 2] = (face * 4) + 2;
indices[idx + 3] = (face * 4) + 0; indices[idx + 4] = (face * 4) + 2; indices[idx + 5] = (face * 4) + 3;
idx += 6;
++face;
// right face (GREEN)
positions[vert + x] = 0.5; positions[vert + y] = 0.5; positions[vert + z] = -0.5;
normals[vert + x] = 1.; normals[vert + y] = 0.; normals[vert + z] = 0.;
colors[vert + r] = 0.; colors[vert + g] = 1.; colors[vert + b] = 0.;
vert += 3;
positions[vert + x] = 0.5; positions[vert + y] = 0.5; positions[vert + z] = 0.5;
normals[vert + x] = 1.; normals[vert + y] = 0.; normals[vert + z] = 0.;
colors[vert + r] = 0.; colors[vert + g] = 1.; colors[vert + b] = 0.;
vert += 3;
positions[vert + x] = 0.5; positions[vert + y] = -0.5; positions[vert + z] = 0.5;
normals[vert + x] = 1.; normals[vert + y] = 0.; normals[vert + z] = 0.;
colors[vert + r] = 0.; colors[vert + g] = 1.; colors[vert + b] = 0.;
vert += 3;
positions[vert + x] = 0.5; positions[vert + y] = -0.5; positions[vert + z] = -0.5;
normals[vert + x] = 1.; normals[vert + y] = 0.; normals[vert + z] = 0.;
colors[vert + r] = 0.; colors[vert + g] = 1.; colors[vert + b] = 0.;
vert += 3;
indices[idx + 0] = (face * 4) + 0; indices[idx + 1] = (face * 4) + 1; indices[idx + 2] = (face * 4) + 2;
indices[idx + 3] = (face * 4) + 0; indices[idx + 4] = (face * 4) + 2; indices[idx + 5] = (face * 4) + 3;
idx += 6;
++face;
// left face (MAGENTA)
positions[vert + x] = -0.5; positions[vert + y] = 0.5; positions[vert + z] = 0.5;
normals[vert + x] = -1.; normals[vert + y] = 0.; normals[vert + z] = 0.;
colors[vert + r] = 1.; colors[vert + g] = 0.; colors[vert + b] = 1.;
vert += 3;
positions[vert + x] = -0.5; positions[vert + y] = 0.5; positions[vert + z] = -0.5;
normals[vert + x] = -1.; normals[vert + y] = 0.; normals[vert + z] = 0.;
colors[vert + r] = 1.; colors[vert + g] = 0.; colors[vert + b] = 1.;
vert += 3;
positions[vert + x] = -0.5; positions[vert + y] = -0.5; positions[vert + z] = -0.5;
normals[vert + x] = -1.; normals[vert + y] = 0.; normals[vert + z] = 0.;
colors[vert + r] = 1.; colors[vert + g] = 0.; colors[vert + b] = 1.;
vert += 3;
positions[vert + x] = -0.5; positions[vert + y] = -0.5; positions[vert + z] = 0.5;
normals[vert + x] = -1.; normals[vert + y] = 0.; normals[vert + z] = 0.;
colors[vert + r] = 1.; colors[vert + g] = 0.; colors[vert + b] = 1.;
vert += 3;
indices[idx + 0] = (face * 4) + 0; indices[idx + 1] = (face * 4) + 1; indices[idx + 2] = (face * 4) + 2;
indices[idx + 3] = (face * 4) + 0; indices[idx + 4] = (face * 4) + 2; indices[idx + 5] = (face * 4) + 3;
idx += 6;
++face;
// top face (CYAN)
positions[vert + x] = 0.5; positions[vert + y] = 0.5; positions[vert + z] = -0.5;
normals[vert + x] = 0.; normals[vert + y] = 1.; normals[vert + z] = 0.;
colors[vert + r] = 0.; colors[vert + g] = 1.; colors[vert + b] = 1.;
vert += 3;
positions[vert + x] = -0.5; positions[vert + y] = 0.5; positions[vert + z] = -0.5;
normals[vert + x] = 0.; normals[vert + y] = 1.; normals[vert + z] = 0.;
colors[vert + r] = 0.; colors[vert + g] = 1.; colors[vert + b] = 1.;
vert += 3;
positions[vert + x] = -0.5; positions[vert + y] = 0.5; positions[vert + z] = 0.5;
normals[vert + x] = 0.; normals[vert + y] = 1.; normals[vert + z] = 0.;
colors[vert + r] = 0.; colors[vert + g] = 1.; colors[vert + b] = 1.;
vert += 3;
positions[vert + x] = 0.5; positions[vert + y] = 0.5; positions[vert + z] = 0.5;
normals[vert + x] = 0.; normals[vert + y] = 1.; normals[vert + z] = 0.;
colors[vert + r] = 0.; colors[vert + g] = 1.; colors[vert + b] = 1.;
vert += 3;
indices[idx + 0] = (face * 4) + 0; indices[idx + 1] = (face * 4) + 1; indices[idx + 2] = (face * 4) + 2;
indices[idx + 3] = (face * 4) + 0; indices[idx + 4] = (face * 4) + 2; indices[idx + 5] = (face * 4) + 3;
idx += 6;
++face;
// bottom face (YELLOW)
positions[vert + x] = 0.5; positions[vert + y] = -0.5; positions[vert + z] = 0.5;
normals[vert + x] = 0.; normals[vert + y] = -1.; normals[vert + z] = 0.;
colors[vert + r] = 1.; colors[vert + g] = 1.; colors[vert + b] = 0.;
vert += 3;
positions[vert + x] = -0.5; positions[vert + y] = -0.5; positions[vert + z] = 0.5;
normals[vert + x] = 0.; normals[vert + y] = -1.; normals[vert + z] = 0.;
colors[vert + r] = 1.; colors[vert + g] = 1.; colors[vert + b] = 0.;
vert += 3;
positions[vert + x] = -0.5; positions[vert + y] = -0.5; positions[vert + z] = -0.5;
normals[vert + x] = 0.; normals[vert + y] = -1.; normals[vert + z] = 0.;
colors[vert + r] = 1.; colors[vert + g] = 1.; colors[vert + b] = 0.;
vert += 3;
positions[vert + x] = 0.5; positions[vert + y] = -0.5; positions[vert + z] = -0.5;
normals[vert + x] = 0.; normals[vert + y] = -1.; normals[vert + z] = 0.;
colors[vert + r] = 1.; colors[vert + g] = 1.; colors[vert + b] = 0.;
vert += 3;
indices[idx + 0] = (face * 4) + 0; indices[idx + 1] = (face * 4) + 1; indices[idx + 2] = (face * 4) + 2;
indices[idx + 3] = (face * 4) + 0; indices[idx + 4] = (face * 4) + 2; indices[idx + 5] = (face * 4) + 3;
idx += 6;
++face;
geo.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
geo.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
geo.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
geo.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
var mat = new THREE.MeshPhongMaterial( {
color: 0xffffff,
ambient: 0xffffff,
specular: 0xffffff,
shininess: 50,
side: THREE.DoubleSide,
vertexColors: THREE.VertexColors
} );
var msh = new THREE.Mesh(geo, mat);
msh.scale.multiplyScalar(scale);
return msh;
}