better system for texture terrain.

This commit is contained in:
Troispoils 2024-03-01 21:56:18 +01:00
parent 3aa6b5bc83
commit 443b3a319d
9 changed files with 120 additions and 69 deletions

View file

@ -2,6 +2,7 @@
using LandblockExtraction.AtlasMaker;
using LandblockExtraction.DatEngine;
using LandblockExtraction.Tools;
using System;
using System.Numerics;
namespace LandblockExtraction.LandBlockExtractor {
@ -14,13 +15,15 @@ namespace LandblockExtraction.LandBlockExtractor {
private readonly int BlockSize = 17;
private readonly int cellSize = 8;
private int[] indiceBase;
public LandBlockExtrator(PortalEngine portalEngine, CellEngine cellEngine) {
this.portalEngine = portalEngine;
this.cellEngine = cellEngine;
terrainAtlasManager = new TerrainAtlasManager(portalEngine);
terrainAtlasManager.ExtractTexture();
terrainAtlasManager.GenerateUV();
indiceBase = GenerateBasicIndices();
}
public BlockStruct? GetBlock(int landX, int landY) {
@ -37,17 +40,50 @@ namespace LandblockExtraction.LandBlockExtractor {
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.indices = indiceBase;
//blockStruct.verticesStruct.color[indice] = GenerateVertexColor(blockData.cellInfos[indice]);
//blockStruct.verticesStruct.farcolor[indice] = GenerateVertexFarColor(blockData.cellInfos[indice]);
blockStruct.verticesStruct.terraintype[indice] = GenerateVertexTerrainType(blockData.cellInfos[indice]);
blockStruct.indices = GenerateBasicIndices();
}
}
GenerateBasicNormalandRighTexture(blockStruct);
DoubleEdgeVertices(blockStruct);
return blockStruct;
}
private void GenerateBasicNormalandRighTexture(BlockStruct blockStruct) {
for (int i = 0; i < blockStruct.indices.Length; i += 6) {
int index1 = blockStruct.indices[i];
int index2 = blockStruct.indices[i + 1];
int index3 = blockStruct.indices[i + 2];
int index4 = blockStruct.indices[i + 5];
Vector3 vertex1 = blockStruct.verticesStruct.position[index1];
Vector3 vertex2 = blockStruct.verticesStruct.position[index2];
Vector3 vertex3 = blockStruct.verticesStruct.position[index3];
Vector3 vertex4 = blockStruct.verticesStruct.position[index4];
var normal = MathOperations.CalculateQuadNormal(vertex1, vertex2, vertex3, vertex4);
var sub1 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index1].X].DetermineSubTerrain(1 - normal.Y);
var sub2 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index2].X].DetermineSubTerrain(1 - normal.Y);
var sub3 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index3].X].DetermineSubTerrain(1 - normal.Y);
var sub4 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index4].X].DetermineSubTerrain(1 - normal.Y);
InitColorFarColorTerrain(index1, sub1, blockStruct);
InitColorFarColorTerrain(index2, sub2, blockStruct);
InitColorFarColorTerrain(index3, sub3, blockStruct);
InitColorFarColorTerrain(index4, sub4, blockStruct);
}
}
private void InitColorFarColorTerrain(int index, SubTerrain terrain, BlockStruct blockStruct) {
blockStruct.verticesStruct.terraintype[index].X = terrain.terrainIndex;
blockStruct.verticesStruct.color[index] = terrain.Color;
blockStruct.verticesStruct.farcolor[index] = terrain.farColor;
}
private int[] GenerateBasicIndices() {
List<int> indices = new List<int>();
@ -123,7 +159,6 @@ namespace LandblockExtraction.LandBlockExtractor {
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;
@ -136,43 +171,32 @@ namespace LandblockExtraction.LandBlockExtractor {
var two = blockStruct.indices[i + 1];
var three = blockStruct.indices[i + 2];
var foor = blockStruct.indices[i + 5];
Vector4 terrainType = new Vector4(blockStruct.verticesStruct.terraintype[one].X,
blockStruct.verticesStruct.terraintype[two].X,
blockStruct.verticesStruct.terraintype[three].X,
blockStruct.verticesStruct.terraintype[foor].X);
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);
newTerrainTypes.Add(terrainType);
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);
newTerrainTypes.Add(terrainType);
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);
newTerrainTypes.Add(terrainType);
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);
newTerrainTypes.Add(terrainType);
newTexCoord.Add(new(0, 0));
newTexCoord.Add(new(0, 1));
@ -198,7 +222,6 @@ namespace LandblockExtraction.LandBlockExtractor {
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);
}
@ -206,12 +229,12 @@ namespace LandblockExtraction.LandBlockExtractor {
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);
indices.Add(i); //A
indices.Add(i + 1); //B
indices.Add(i + 2); //C
indices.Add(i + 2); //C
indices.Add(i + 1); //B
indices.Add(i + 3); //D
}
return indices.ToArray();

View file

@ -8,7 +8,6 @@ namespace LandblockExtraction.LandBlockExtractor {
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];
@ -17,13 +16,12 @@ namespace LandblockExtraction.LandBlockExtractor {
farcolor = new Vector4[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 * 21]; // 3 pour position, 4 pour color, et 4 pour farcolor
for (int i = 0, vi = 0; i < length; i++, vi += 21) {
float[] vertices = new float[length * 20];
for (int i = 0, vi = 0; i < length; i++, vi += 20) {
vertices[vi] = position[i].X;
vertices[vi + 1] = position[i].Y;
vertices[vi + 2] = position[i].Z;
@ -44,7 +42,6 @@ namespace LandblockExtraction.LandBlockExtractor {
vertices[vi + 17] = terraintype[i].Y;
vertices[vi + 18] = terraintype[i].Z;
vertices[vi + 19] = terraintype[i].W;
vertices[vi + 20] = realtype[i];
}
return vertices;
}