Success to blend 4 texture
Need improuve for angle.
This commit is contained in:
parent
b9a2b87fe9
commit
664eb5872a
8 changed files with 186 additions and 105 deletions
|
|
@ -21,7 +21,6 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
|
||||
terrainAtlasManager = new TerrainAtlasManager(portalEngine);
|
||||
terrainAtlasManager.ExtractTexture();
|
||||
terrainAtlasManager.GenerateAtlas();
|
||||
terrainAtlasManager.GenerateUV();
|
||||
}
|
||||
|
||||
|
|
@ -35,72 +34,48 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
|
||||
public BlockStruct GenerateBlockStructByData(CLandBlockData blockData, int landX, int landY) {
|
||||
BlockStruct blockStruct = new BlockStruct();
|
||||
for (int y = 0; y < BlockStruct.BlockSize; y++) {
|
||||
for (int x = 0; x < BlockStruct.BlockSize; x++) {
|
||||
var indice = y * BlockStruct.BlockSize + x;
|
||||
for (int y = 0; y < BlockSize; y++) {
|
||||
for (int x = 0; x < BlockSize; x++) {
|
||||
var indice = y * BlockSize + x;
|
||||
blockStruct.verticesStruct.position[indice] = GenerateVertexPosition(landX, landY, x, y, blockData.heights[indice]);
|
||||
blockStruct.verticesStruct.color[indice] = GenerateVertexColor(blockData.cellInfos[indice]);
|
||||
blockStruct.verticesStruct.farcolor[indice] = GenerateVertexFarColor(blockData.cellInfos[indice]);
|
||||
blockStruct.GenerateIndices();
|
||||
//blockStruct.GenerateUVByIndices();
|
||||
GenerateVertexTextureCoord(blockData, blockStruct);
|
||||
blockStruct.verticesStruct.terraintype[indice] = GenerateVertexTerrainType(blockData.cellInfos[indice]);
|
||||
blockStruct.indices = GenerateBasicIndices();
|
||||
}
|
||||
}
|
||||
|
||||
DoubleEdgeVertices(blockStruct);
|
||||
|
||||
return blockStruct;
|
||||
}
|
||||
|
||||
private void GenerateVertexTextureCoord(CLandBlockData blockData, BlockStruct blockStruct) {
|
||||
List<Vector3> vertices = new List<Vector3>(); // Liste pour stocker les positions des sommets dupliqués
|
||||
List<Vector4> colors = new List<Vector4>();
|
||||
List<Vector4> farcolors = new List<Vector4>();
|
||||
List<Vector2> uvs = new List<Vector2>(); // Liste pour stocker les coordonnées UV
|
||||
List<int> indices = new List<int>(); // Liste pour stocker les nouveaux indices
|
||||
private int[] GenerateBasicIndices() {
|
||||
List<int> indices = new List<int>();
|
||||
|
||||
for (int y = 0; y < blockStruct.indices.Length; y = y + 6) {
|
||||
// Créez 4 sommets
|
||||
vertices.Add(blockStruct.verticesStruct.position[blockStruct.indices[y]]);
|
||||
vertices.Add(blockStruct.verticesStruct.position[blockStruct.indices[y + 1]]);
|
||||
vertices.Add(blockStruct.verticesStruct.position[blockStruct.indices[y + 2]]);
|
||||
vertices.Add(blockStruct.verticesStruct.position[blockStruct.indices[y + 5]]);
|
||||
for (int y = 0; y < BlockSize - 1; y++) {
|
||||
for (int x = 0; x < BlockSize - 1; x++) {
|
||||
// Indices des sommets du premier triangle
|
||||
indices.Add(y * BlockSize + x);
|
||||
indices.Add((y + 1) * BlockSize + x);
|
||||
indices.Add(y * BlockSize + x + 1);
|
||||
|
||||
// Créez 4 color
|
||||
colors.Add(blockStruct.verticesStruct.color[blockStruct.indices[y]]);
|
||||
colors.Add(blockStruct.verticesStruct.color[blockStruct.indices[y + 1]]);
|
||||
colors.Add(blockStruct.verticesStruct.color[blockStruct.indices[y + 2]]);
|
||||
colors.Add(blockStruct.verticesStruct.color[blockStruct.indices[y + 5]]);
|
||||
|
||||
// Créez 4 farcolor
|
||||
farcolors.Add(blockStruct.verticesStruct.farcolor[blockStruct.indices[y]]);
|
||||
farcolors.Add(blockStruct.verticesStruct.farcolor[blockStruct.indices[y + 1]]);
|
||||
farcolors.Add(blockStruct.verticesStruct.farcolor[blockStruct.indices[y + 2]]);
|
||||
farcolors.Add(blockStruct.verticesStruct.farcolor[blockStruct.indices[y + 5]]);
|
||||
|
||||
// Assignez les coordonnées UV à chaque sommet
|
||||
uvs.Add(new Vector2(0, 0)); // Coin inférieur gauche
|
||||
uvs.Add(new Vector2(1, 0)); // Coin inférieur droit
|
||||
uvs.Add(new Vector2(0, 1)); // Coin supérieur gauche
|
||||
uvs.Add(new Vector2(1, 1)); // Coin supérieur droit
|
||||
|
||||
// Créez les indices pour les deux triangles du quad
|
||||
int baseIndex = (y / 6) * 4; // Calculez l'index de base pour ce quad
|
||||
indices.Add(baseIndex);
|
||||
indices.Add(baseIndex + 1);
|
||||
indices.Add(baseIndex + 2);
|
||||
|
||||
indices.Add(baseIndex + 2);
|
||||
indices.Add(baseIndex + 1);
|
||||
indices.Add(baseIndex + 3);
|
||||
// Indices des sommets du deuxième triangle
|
||||
indices.Add(y * BlockSize + x + 1);
|
||||
indices.Add((y + 1) * BlockSize + x);
|
||||
indices.Add((y + 1) * BlockSize + x + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Mettez à jour le BlockStruct avec les nouvelles listes de sommets, UVs, et indices
|
||||
Console.WriteLine(indices.Count);
|
||||
blockStruct.verticesStruct.position = vertices.ToArray();
|
||||
blockStruct.verticesStruct.color = colors.ToArray();
|
||||
blockStruct.verticesStruct.farcolor = farcolors.ToArray();
|
||||
blockStruct.verticesStruct.texturecoord = uvs.ToArray();
|
||||
blockStruct.indices = indices.ToArray();
|
||||
return indices.ToArray();
|
||||
}
|
||||
|
||||
private Vector4 GenerateVertexTerrainType(uint cellInfo) {
|
||||
var terrain = MathOperations.GetTerrainInCellInfo(cellInfo);
|
||||
|
||||
return new(terrain, 0f, 0f, 0f);
|
||||
}
|
||||
|
||||
private Vector3 GenerateVertexPosition(int landx, int landy, int x, int y, byte height) {
|
||||
int tmpx = (landx * BlockSize + y) * cellSize;
|
||||
int tmpy = (BlockSize * NumberLandBlocks * cellSize) - ((landy * BlockSize + x) * cellSize) - 1;
|
||||
|
|
@ -141,5 +116,95 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
|
||||
return Vector4.One;
|
||||
}
|
||||
|
||||
//TEST
|
||||
public void DoubleEdgeVertices(BlockStruct blockStruct) {
|
||||
List<Vector3> newPositions = new List<Vector3>();
|
||||
List<Vector4> newColors = new List<Vector4>();
|
||||
List<Vector4> newFarColors = new List<Vector4>();
|
||||
List<Vector2> newTexCoord = new List<Vector2>();
|
||||
List<Vector4> newTerrainTypes = new List<Vector4>();
|
||||
List<float> newRealTerrainType = new List<float>();
|
||||
|
||||
int originalVertexCount = blockStruct.verticesStruct.position.Length;
|
||||
int originalIndicesCount = blockStruct.indices.Length;
|
||||
|
||||
// Doubler les sommets sur le bord supérieur et inférieur
|
||||
|
||||
for(int i = 0; i < originalIndicesCount; i = i + 6) {
|
||||
|
||||
var one = blockStruct.indices[i + 0];
|
||||
var two = blockStruct.indices[i + 1];
|
||||
var three = blockStruct.indices[i + 2];
|
||||
var foor = blockStruct.indices[i + 5];
|
||||
|
||||
newPositions.Add(blockStruct.verticesStruct.position[one]);
|
||||
newColors.Add(blockStruct.verticesStruct.color[one]);
|
||||
newFarColors.Add(blockStruct.verticesStruct.farcolor[one]);
|
||||
newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X,
|
||||
blockStruct.verticesStruct.terraintype[two].X,
|
||||
blockStruct.verticesStruct.terraintype[three].X,
|
||||
blockStruct.verticesStruct.terraintype[foor].X));
|
||||
newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[one].X);
|
||||
|
||||
newPositions.Add(blockStruct.verticesStruct.position[two]);
|
||||
newColors.Add(blockStruct.verticesStruct.color[two]);
|
||||
newFarColors.Add(blockStruct.verticesStruct.farcolor[two]);
|
||||
newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X,
|
||||
blockStruct.verticesStruct.terraintype[two].X,
|
||||
blockStruct.verticesStruct.terraintype[three].X,
|
||||
blockStruct.verticesStruct.terraintype[foor].X));
|
||||
newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[two].X);
|
||||
|
||||
newPositions.Add(blockStruct.verticesStruct.position[three]);
|
||||
newColors.Add(blockStruct.verticesStruct.color[three]);
|
||||
newFarColors.Add(blockStruct.verticesStruct.farcolor[three]);
|
||||
newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X,
|
||||
blockStruct.verticesStruct.terraintype[two].X,
|
||||
blockStruct.verticesStruct.terraintype[three].X,
|
||||
blockStruct.verticesStruct.terraintype[foor].X));
|
||||
newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[three].X);
|
||||
|
||||
|
||||
newPositions.Add(blockStruct.verticesStruct.position[foor]);
|
||||
newColors.Add(blockStruct.verticesStruct.color[foor]);
|
||||
newFarColors.Add(blockStruct.verticesStruct.farcolor[foor]);
|
||||
newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X,
|
||||
blockStruct.verticesStruct.terraintype[two].X,
|
||||
blockStruct.verticesStruct.terraintype[three].X,
|
||||
blockStruct.verticesStruct.terraintype[foor].X));
|
||||
newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[foor].X);
|
||||
|
||||
newTexCoord.Add(new(0, 0));
|
||||
newTexCoord.Add(new(0, 1));
|
||||
newTexCoord.Add(new(1, 0));
|
||||
newTexCoord.Add(new(1, 1));
|
||||
}
|
||||
|
||||
// Ajouter les nouveaux sommets à la structure BlockStruct (étape 2)
|
||||
// Vous devez également mettre à jour les indices dans blockStruct.indices
|
||||
blockStruct.verticesStruct.position = newPositions.ToArray();
|
||||
blockStruct.verticesStruct.color = newColors.ToArray();
|
||||
blockStruct.verticesStruct.farcolor = newFarColors.ToArray();
|
||||
blockStruct.verticesStruct.texturecoord = newTexCoord.ToArray();
|
||||
blockStruct.verticesStruct.terraintype = newTerrainTypes.ToArray();
|
||||
blockStruct.verticesStruct.realtype = newRealTerrainType.ToArray();
|
||||
|
||||
blockStruct.indices = GenerateNewsIndices(newPositions.Count);
|
||||
}
|
||||
|
||||
private int[] GenerateNewsIndices(int count) {
|
||||
List<int> indices = new List<int>();
|
||||
for(int i = 0; i < count; i = i + 4) {
|
||||
indices.Add(i);
|
||||
indices.Add(i + 1);
|
||||
indices.Add(i + 2);
|
||||
indices.Add(i + 2);
|
||||
indices.Add(i + 1);
|
||||
indices.Add(i + 3);
|
||||
}
|
||||
|
||||
return indices.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,34 +30,5 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void GenerateUVByIndices() {
|
||||
// Calculer les UV en fonction de la position de chaque sommet
|
||||
for (int y = 0; y < BlockSize; y++) {
|
||||
for (int x = 0; x < BlockSize; x++) {
|
||||
// Indices des sommets du premier triangle
|
||||
//float u = ((float)x / (BlockSize - 1)) * 16;
|
||||
//float v = ((float)y / (BlockSize - 1)) * 16;
|
||||
// Définir la position de la texture dans l'atlas
|
||||
int textureIndexX = 1; // Deuxième colonne
|
||||
int textureIndexY = 0; // Première ligne
|
||||
int atlasSize = 4; // L'atlas contient 4x4 textures
|
||||
|
||||
// Calculer le décalage dans l'atlas pour la texture ciblée
|
||||
float offsetX = (float)textureIndexX / atlasSize;
|
||||
float offsetY = (float)textureIndexY / atlasSize;
|
||||
|
||||
// Calculer la taille d'une texture dans l'atlas
|
||||
float textureSize = 1.0f / atlasSize;
|
||||
|
||||
// Calculer les coordonnées UV pour la répétition
|
||||
float u = ((float)x / (BlockSize - 1)) * 16 * textureSize + offsetX;
|
||||
float v = ((float)y / (BlockSize - 1)) * 16 * textureSize + offsetY;
|
||||
|
||||
|
||||
verticesStruct.texturecoord[y * BlockSize + x] = new(u, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,18 +6,22 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
public Vector4[] color { get; set; }
|
||||
public Vector4[] farcolor { get; set; }
|
||||
public Vector2[] texturecoord { get; set; }
|
||||
public Vector4[] terraintype { get; set; }
|
||||
public float[] realtype { get; set; }
|
||||
|
||||
public VerticesStruct(int blockSize) {
|
||||
position = new Vector3[blockSize * blockSize];
|
||||
color = new Vector4[blockSize * blockSize];
|
||||
farcolor = new Vector4[blockSize * blockSize];
|
||||
texturecoord = new Vector2[blockSize * blockSize];
|
||||
texturecoord = new Vector2[blockSize * blockSize];
|
||||
terraintype = new Vector4[blockSize * blockSize];
|
||||
realtype = new float[blockSize * blockSize];
|
||||
}
|
||||
|
||||
public float[] Vertices() {
|
||||
int length = position.Length;
|
||||
float[] vertices = new float[length * 13]; // 3 pour position, 4 pour color, et 4 pour farcolor
|
||||
for (int i = 0, vi = 0; i < length; i++, vi += 13) {
|
||||
float[] vertices = new float[length * 18]; // 3 pour position, 4 pour color, et 4 pour farcolor
|
||||
for (int i = 0, vi = 0; i < length; i++, vi += 18) {
|
||||
vertices[vi] = position[i].X;
|
||||
vertices[vi + 1] = position[i].Y;
|
||||
vertices[vi + 2] = position[i].Z;
|
||||
|
|
@ -31,6 +35,11 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
vertices[vi + 10] = farcolor[i].W;
|
||||
vertices[vi + 11] = texturecoord[i].X;
|
||||
vertices[vi + 12] = texturecoord[i].Y;
|
||||
vertices[vi + 13] = terraintype[i].X;
|
||||
vertices[vi + 14] = terraintype[i].Y;
|
||||
vertices[vi + 15] = terraintype[i].Z;
|
||||
vertices[vi + 16] = terraintype[i].W;
|
||||
vertices[vi + 17] = realtype[i];
|
||||
}
|
||||
return vertices;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue