Continuing to think about the idea of parametric modeling and developing the previous idea: Parametric modeling from primitives (cube, sphere...) and animation
I decided to do it through a function. Where the vertex ID will be passed at the input. And at the output it should return the vertices, normals, UVS and the IDs of the next vertices. Thus, calling this function will build a model tree.
In essence, the model is built by arrows. From point to point. Three points form a triangle. This is a great idea!
Two hours later. Implementation of the cube function.
let box = function(vId, fId){ // vertexId, prevousId
const vert = [[0, -1, 0], [-1, -1, -1], [1, -1, -1], [1, -1, 1], [-1, -1, 1], [-1, 1, -1], [1, 1, -1], [1, 1, 1], [-1, 1, 1]];
const uvs = [[7, 7], [0, 0], [1, 0], [1, 1], [0, 1], [0, 0], [1, 0], [1, 1], [0, 1]];
if(vId == 0){
return { vert: [0, -1, 0], norm: [0, -1, 0], uv: [.5, .5], id: [1, 2, 3, 4] };
} else if(vId < 5){
return { vert: vert[vId], norm: [vert[vId][0] * 0.5773502691896258, vert[vId][1] * 0.5773502691896258, vert[vId][2] * 0.5773502691896258], uv: uvs[vId],
id: fId == 0 ? [vId != 4 ? (vId + 1) : 1] : [vId + 4] };
}else if(vId < 9){
return { vert: vert[vId], norm: [vert[vId][0] * 0.5773502691896258, vert[vId][1] * 0.5773502691896258, vert[vId][2] * 0.5773502691896258], uv: uvs[vId],
id: fId < 5 ? [vId != 8 ? vId + 1 : 5] :
Math.abs(vId - fId) == 1 ? [vId != 8 ? vId + 1 : 5] : 0
//id: fId < 5 ? [fId != 4 ? (fId + 1) : 1] : []
// id: fId < 5 ? [vId != 5 ? (vId - 1) : 8] : [9]
};
} else if(vId == 9){
return { vert: [0, 1, 0], norm: [0, 1, 0], uv: [.5, .5], id: [] };
} else if(vId == undefined){
return { verts: 10 };
}
}
This is impossible to use. It is extremely difficult to describe logically. A vector language is needed here. Helping to build connections between points.
Are there any ways to improve the current approach? So that it can be used.
I am thinking about the level method. When the function will return a list of points for the level. For example, a cube is two levels of 4 points.