WebGL纯色

时间:2015-01-28 08:51:31

标签: javascript coffeescript glsl webgl

https://www.dropbox.com/s/4zkhtdv4yaqhpxy/Screenshot%20from%202015-01-28%2010%3A42%3A02%201.png?dl=0 有人可以向我解释一下,我做错了什么?我希望立方体的每个面都有一个纯色。创建多维数据集的代码位于RunCube.coffee文件中,顶点和颜色在Cube.coffee文件中定义。我认为问题在于我不知道如何使用索引进行颜色处理。 这是github上的存储库https://github.com/trimpirim/shiny-soice

更新: 我有Cube的所有数据。

@vertices: [
  [ 6.89954888016507530,  0.39691390817415106, -4.02972512706645780],
  [-0.78006682662096161, -3.78853119791598660, -7.00275139558893490],
  [-5.79336942493284560,  3.47790796230961650, -4.28264251507835430],
  [ 1.88624628185319150,  7.66335306839975420, -1.30961624655587690],
  [ 0.78006682662096205,  3.78853119791598920,  7.00275139558893490],
  [ 5.79336942493284290, -3.47790796230961740,  4.28264251507835780],
  [-1.88624628185319150, -7.66335306839975150,  1.30961624655588270],
  [-6.89954888016507440, -0.39691390817415328,  4.02972512706646220]
];

@faces: [
  [0, 1, 2], [0, 2, 3],
  [0, 3, 4], [0, 4, 5],
  [0, 5, 6], [0, 6, 1]
  [2, 1, 6], [2, 6, 7],
  [2, 7, 4], [2, 4, 3],
  [4, 7, 6], [4, 6, 5]
];

@colors: [
  [1.0, 0.0, 0.0, 1.0],
  [1.0, 1.0, 0.0, 1.0],
  [0.0, 1.0, 0.0, 1.0],
  [1.0, 0.5, 0.5, 1.0],
  [1.0, 0.0, 1.0, 1.0],
  [0.0, 0.0, 1.0, 1.0],
]

有人能告诉我,正确的颜色数据应该如何?

1 个答案:

答案 0 :(得分:1)

修改

不是分隔的颜色索引。顶点位置和颜色(或您可以想到的任何其他属性)的指数都相同。

要获得6种纯色的立方体,您必须重复阵列的某些部分。

这是一种原型,顶点如何:

vertices: [
    {
        position: [x,y,z],
        color: [r, g, b, a]
    },
    {
        position: [x,y,z],
        color: [r, g, b, a]
    },
    ...
];

position: [0,0,0], color [1,0,0,1]的顶点与position: [0,0,0], color [0,1,0,1]的顶点不同。您希望立方体的一个角落是具有不同颜色的3个面的一部分。因此,一个角中必须有3个顶点具有相同的位置,但颜色不同。不幸的是,在这种情况下,位置无法共享。

所以你的定义应该是这样的:

var vertex_positions = [
    // see that front face and back face has 8 unique positions
    // front face
    [0, 0, 0],
    [1, 0, 0],
    [1, 1, 0],
    [0, 1, 0],
    // back face
    [0, 0, 1],
    [1, 0, 1],
    [1, 1, 1],
    [0, 1, 1],
    // see that bottom face and top face has 8 unique positions too,
    // but they repeated with different order from front and back
    // bottom face
    [0, 0, 0],
    [1, 0, 0],
    [1, 0, 1],
    [0, 0, 1],
    // top face
    [0, 1, 0],
    [1, 1, 0],
    [1, 1, 1],
    [0, 1, 1],
    // left and right face have 8 unique positions too, but again
    // repeated from front, back / bottom, top
    // left face
    [0, 0, 0],
    [0, 1, 0],
    [0, 1, 1],
    [0, 0, 1],
    // right face
    [1, 0, 0],
    [1, 1, 0],
    [1, 1, 1],
    [1, 0, 1]
];

颜色,与位置数量相同的元素:

var vertex_colors = [
    // front face
    [1.0, 0.0, 0.0, 1.0],
    [1.0, 0.0, 0.0, 1.0],
    [1.0, 0.0, 0.0, 1.0],
    [1.0, 0.0, 0.0, 1.0],
    // back face
    [1.0, 1.0, 0.0, 1.0],
    [1.0, 1.0, 0.0, 1.0],
    [1.0, 1.0, 0.0, 1.0],
    [1.0, 1.0, 0.0, 1.0],
    // bottom face
    [0.0, 1.0, 0.0, 1.0],
    [0.0, 1.0, 0.0, 1.0],
    [0.0, 1.0, 0.0, 1.0],
    [0.0, 1.0, 0.0, 1.0],
    // top face
    [1.0, 0.5, 0.5, 1.0],
    [1.0, 0.5, 0.5, 1.0],
    [1.0, 0.5, 0.5, 1.0],
    [1.0, 0.5, 0.5, 1.0],
    // left face
    [1.0, 0.0, 1.0, 1.0],
    [1.0, 0.0, 1.0, 1.0],
    [1.0, 0.0, 1.0, 1.0],
    [1.0, 0.0, 1.0, 1.0],
    // right face
    [0.0, 0.0, 1.0, 1.0],
    [0.0, 0.0, 1.0, 1.0],
    [0.0, 0.0, 1.0, 1.0],
    [0.0, 0.0, 1.0, 1.0],
];

现在指数:

var triangles = [
    // front face
    [0, 1, 2],
    [0, 2, 3],
    // back face
    [4, 5, 6],
    [4, 6, 7],
    // bottom face
    [8, 9, 10],
    [8, 10, 11],
    // top face
    [12, 13, 14],
    [12, 14, 15],
    // left face
    [16, 17, 18],
    [16, 18, 19],
    // right face
    [20, 21, 22],
    [20, 22, 23]
];

立方体由12个三角形组成。对于纯色面,我们需要4个独特的顶点用于2个三角形,因此我们需要24个不同的顶点定义。

这是格曼所说的最传统的方式。还有其他方法可以达到同样的效果,但他们的使用情况很少见。

PS:抱歉,我的指数可能不正确