Add Normal in vertex. And add light in shader.
This commit is contained in:
parent
8908ef1332
commit
0e96615b32
16 changed files with 221 additions and 85 deletions
|
|
@ -1,12 +1,6 @@
|
|||
using AC2RE.Definitions;
|
||||
using ImageMagick;
|
||||
using LandblockExtraction.DatEngine;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LandblockExtraction.AtlasMaker;
|
||||
public class AtlasBuilder : IDisposable {
|
||||
|
|
@ -21,7 +15,7 @@ public class AtlasBuilder : IDisposable {
|
|||
|
||||
public bool AddTexture(int index, DataId matId) {
|
||||
var img = texturesImage.GetImage(matId);
|
||||
if(img == null) return false;
|
||||
if (img == null) return false;
|
||||
textures.Add(index, img);
|
||||
return true;
|
||||
}
|
||||
|
|
@ -41,7 +35,7 @@ public class AtlasBuilder : IDisposable {
|
|||
int x = (index % count) * (int)TEXTURESIZE;
|
||||
int y = (index / count) * (int)TEXTURESIZE;
|
||||
atlas.Composite(kvp.Value, x, y);
|
||||
|
||||
|
||||
index++;
|
||||
if (index >= count * count) break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,7 @@
|
|||
using AC2RE.Definitions;
|
||||
using ImageMagick;
|
||||
using LandblockExtraction.DatEngine;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LandblockExtraction.AtlasMaker;
|
||||
public class TerrainAtlasManager {
|
||||
|
|
@ -15,10 +9,14 @@ public class TerrainAtlasManager {
|
|||
|
||||
public Dictionary<int, Vector2> textureCoord;
|
||||
public Dictionary<int, MagickImage> terrainTexture;
|
||||
public Dictionary<int, Terrain> terrains;
|
||||
public TerrainAtlasManager(PortalEngine portalEngine) {
|
||||
this.portalEngine = portalEngine;
|
||||
textureCoord = new Dictionary<int, Vector2>();
|
||||
terrainTexture = new Dictionary<int, MagickImage>();
|
||||
terrains = new Dictionary<int, Terrain>();
|
||||
|
||||
testGenerateList();
|
||||
}
|
||||
public void ExtractTexture() {
|
||||
using (var atlasBuilder = new AtlasBuilder(portalEngine)) {
|
||||
|
|
@ -26,22 +24,46 @@ public class TerrainAtlasManager {
|
|||
foreach (var surface in portalEngine.cSurfaceDesc.surfaces) {
|
||||
if (surface.surfIndex == terrain.surfaceInfo) {
|
||||
atlasBuilder.AddTexture((int)terrain.terrainIndex, surface.terrainMaterials.First().baseMaterials.First().materialDid);
|
||||
//terrains.Add((int)terrain.terrainIndex, GenerateSubTerrain((int)terrain.terrainIndex, surface.terrainMaterials));
|
||||
}
|
||||
}
|
||||
textureCoord.Add((int)terrain.terrainIndex, Vector2.Zero);
|
||||
}
|
||||
|
||||
atlasBuilder.GenerateAtlas();
|
||||
foreach(var img in atlasBuilder.textures) {
|
||||
foreach (var img in atlasBuilder.textures) {
|
||||
terrainTexture.Add(img.Key, img.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*private Terrain GenerateSubTerrain(int index, List<CSurfaceDesc.MaterialGroup> materialGroups) {
|
||||
Terrain tmpTerrain = new Terrain(index);
|
||||
foreach(var material in materialGroups) {
|
||||
int indexTer = GetIndexBySurface(material.s)
|
||||
SubTerrain subTerrain = new(ma)
|
||||
}
|
||||
}*/
|
||||
private void testGenerateList() {
|
||||
foreach(var surface in portalEngine.cSurfaceDesc.surfaces) {
|
||||
foreach(var terrain in portalEngine.cTerrainDesc.terrains) {
|
||||
if(surface.surfIndex == terrain.surfaceInfo) {
|
||||
Console.WriteLine($"[{terrain.terrainIndex}:{terrain.terrainName}]\t- Surface: {surface.surfIndex:X8} | Base: {surface.terrainMaterials.First().baseMaterials.First().materialDid}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private int? GetIndexBySurface(uint surfaceIndex) {
|
||||
foreach(var terrain in portalEngine.cTerrainDesc.terrains) {
|
||||
if (terrain.surfaceInfo == surfaceIndex)
|
||||
return (int)terrain.terrainIndex;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public void GenerateUV() {
|
||||
int count = (int)Math.Ceiling(Math.Sqrt(textureCoord.Count));
|
||||
int atlasSize = (int)64 * count;
|
||||
int index = 0;
|
||||
foreach(var terrain in textureCoord) {
|
||||
foreach (var terrain in textureCoord) {
|
||||
int x = (index % count) * (int)64;
|
||||
int y = (index / count) * (int)64;
|
||||
textureCoord[index] = new Vector2(x, y);
|
||||
|
|
|
|||
15
LandblockExtraction/AtlasMaker/TerrainInfo/SubTerrain.cs
Normal file
15
LandblockExtraction/AtlasMaker/TerrainInfo/SubTerrain.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
namespace LandblockExtraction.AtlasMaker;
|
||||
|
||||
public class SubTerrain {
|
||||
public int terrainIndex { get; set; }
|
||||
public float minPitch { get; set; }
|
||||
public float maxPitch { get; set; }
|
||||
public SubTerrain(int terrainIndex, float minPitch, float maxPitch) {
|
||||
this.terrainIndex = terrainIndex;
|
||||
this.minPitch = minPitch;
|
||||
this.maxPitch = maxPitch;
|
||||
}
|
||||
public bool MatchesPitch(float pitch) {
|
||||
return pitch >= minPitch && pitch <= maxPitch;
|
||||
}
|
||||
}
|
||||
22
LandblockExtraction/AtlasMaker/TerrainInfo/Terrain.cs
Normal file
22
LandblockExtraction/AtlasMaker/TerrainInfo/Terrain.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
namespace LandblockExtraction.AtlasMaker;
|
||||
public class Terrain {
|
||||
public int terrainIndex { get; set; }
|
||||
public List<SubTerrain> subTerrains { get; set; }
|
||||
|
||||
public Terrain(int index) {
|
||||
terrainIndex = index;
|
||||
subTerrains = new List<SubTerrain>();
|
||||
}
|
||||
public void AddSubTerrain(SubTerrain sousTerrain) {
|
||||
subTerrains.Add(sousTerrain);
|
||||
}
|
||||
|
||||
public int DetermineSubTerrain(float pitch) {
|
||||
foreach (var terrain in subTerrains) {
|
||||
if (terrain.MatchesPitch(pitch)) {
|
||||
return terrain.terrainIndex;
|
||||
}
|
||||
}
|
||||
return terrainIndex;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,11 +2,6 @@
|
|||
using ImageMagick;
|
||||
using LandblockExtraction.DatEngine;
|
||||
using LandblockExtraction.Tools;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LandblockExtraction.AtlasMaker;
|
||||
public class TexturesImage {
|
||||
|
|
@ -19,10 +14,10 @@ public class TexturesImage {
|
|||
}
|
||||
|
||||
public MagickImage? GetImage(DataId matId) {
|
||||
if(!portalEngine.datReader.contains(matId)) return null;
|
||||
using(var data = portalEngine.datReader.getFileReader(matId)) {
|
||||
if (!portalEngine.datReader.contains(matId)) return null;
|
||||
using (var data = portalEngine.datReader.getFileReader(matId)) {
|
||||
var materialLayer = new RenderMaterial(data);
|
||||
if(materialLayer == null) return null;
|
||||
if (materialLayer == null) return null;
|
||||
|
||||
return ImageInMaterial(materialLayer);
|
||||
}
|
||||
|
|
@ -30,10 +25,10 @@ public class TexturesImage {
|
|||
|
||||
private MagickImage? ImageInMaterial(RenderMaterial materialLayer) {
|
||||
var vdescId = materialLayer.layers.First().stages.First().textureID;
|
||||
if(!portalEngine.datReader.contains(new(vdescId))) return null;
|
||||
using(var data = portalEngine.datReader.getFileReader(new(vdescId))) {
|
||||
if (!portalEngine.datReader.contains(new(vdescId))) return null;
|
||||
using (var data = portalEngine.datReader.getFileReader(new(vdescId))) {
|
||||
var texture = new RenderTexture(data);
|
||||
if(texture == null) return null;
|
||||
if (texture == null) return null;
|
||||
return TextureInRender(texture);
|
||||
}
|
||||
|
||||
|
|
@ -41,13 +36,13 @@ public class TexturesImage {
|
|||
|
||||
private MagickImage? TextureInRender(RenderTexture texture) {
|
||||
MagickImage magickImage;
|
||||
foreach(var img in texture.levelSurfaceDids) {
|
||||
if(!portalEngine.datReader.contains(img)) continue;
|
||||
using(var data = portalEngine.datReader.getFileReader(img)) {
|
||||
foreach (var img in texture.levelSurfaceDids) {
|
||||
if (!portalEngine.datReader.contains(img)) continue;
|
||||
using (var data = portalEngine.datReader.getFileReader(img)) {
|
||||
var image = new RenderSurface(data);
|
||||
if(image.width != 64) continue;
|
||||
if (image.width != 64) continue;
|
||||
var dataImg = DDSHeader.Generate(image);
|
||||
using(MagickImage realImg = new MagickImage(dataImg)) {
|
||||
using (MagickImage realImg = new MagickImage(dataImg)) {
|
||||
magickImage = new(realImg);
|
||||
}
|
||||
return magickImage;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,4 @@
|
|||
using AC2RE.Definitions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LandblockExtraction.DatEngine;
|
||||
public class DatEngine {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
using AC2RE.Definitions;
|
||||
|
||||
namespace LandblockExtraction.DatEngine {
|
||||
namespace LandblockExtraction.DatEngine {
|
||||
public class TextureEngine : DatEngine {
|
||||
public TextureEngine() : base(@"X:\DatFiles\highres.dat") {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
using LandblockExtraction.AtlasMaker;
|
||||
using LandblockExtraction.DatEngine;
|
||||
using LandblockExtraction.Tools;
|
||||
using System.Dynamic;
|
||||
using System.Numerics;
|
||||
|
||||
namespace LandblockExtraction.LandBlockExtractor {
|
||||
|
|
@ -25,11 +24,11 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
}
|
||||
|
||||
public BlockStruct? GetBlock(int landX, int landY) {
|
||||
CellId landBlockId = new CellId((byte)landX, (byte)landY, 0xFF, 0xFF);
|
||||
var landBlock = cellEngine.GetLandBlockData(landBlockId.id);
|
||||
if (landBlock == null) return null;
|
||||
CellId landBlockId = new CellId((byte)landX, (byte)landY, 0xFF, 0xFF);
|
||||
var landBlock = cellEngine.GetLandBlockData(landBlockId.id);
|
||||
if (landBlock == null) return null;
|
||||
|
||||
return GenerateBlockStructByData(landBlock, landX, landY);
|
||||
return GenerateBlockStructByData(landBlock, landX, landY);
|
||||
}
|
||||
|
||||
public BlockStruct GenerateBlockStructByData(CLandBlockData blockData, int landX, int landY) {
|
||||
|
|
@ -120,6 +119,7 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
//TEST
|
||||
public void DoubleEdgeVertices(BlockStruct blockStruct) {
|
||||
List<Vector3> newPositions = new List<Vector3>();
|
||||
List<Vector3> newNormals = new List<Vector3>();
|
||||
List<Vector4> newColors = new List<Vector4>();
|
||||
List<Vector4> newFarColors = new List<Vector4>();
|
||||
List<Vector2> newTexCoord = new List<Vector2>();
|
||||
|
|
@ -131,7 +131,7 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
|
||||
// Doubler les sommets sur le bord supérieur et inférieur
|
||||
|
||||
for(int i = 0; i < originalIndicesCount; i = i + 6) {
|
||||
for (int i = 0; i < originalIndicesCount; i = i + 6) {
|
||||
|
||||
var one = blockStruct.indices[i + 0];
|
||||
var two = blockStruct.indices[i + 1];
|
||||
|
|
@ -179,11 +179,22 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
newTexCoord.Add(new(0, 1));
|
||||
newTexCoord.Add(new(1, 0));
|
||||
newTexCoord.Add(new(1, 1));
|
||||
|
||||
var normal = MathOperations.CalculateQuadNormal(blockStruct.verticesStruct.position[one],
|
||||
blockStruct.verticesStruct.position[two],
|
||||
blockStruct.verticesStruct.position[three],
|
||||
blockStruct.verticesStruct.position[foor]);
|
||||
|
||||
newNormals.Add(normal);
|
||||
newNormals.Add(normal);
|
||||
newNormals.Add(normal);
|
||||
newNormals.Add(normal);
|
||||
}
|
||||
|
||||
// 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.normal = newNormals.ToArray();
|
||||
blockStruct.verticesStruct.color = newColors.ToArray();
|
||||
blockStruct.verticesStruct.farcolor = newFarColors.ToArray();
|
||||
blockStruct.verticesStruct.texturecoord = newTexCoord.ToArray();
|
||||
|
|
@ -195,7 +206,7 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
|
||||
private int[] GenerateNewsIndices(int count) {
|
||||
List<int> indices = new List<int>();
|
||||
for(int i = 0; i < count; i = i + 4) {
|
||||
for (int i = 0; i < count; i = i + 4) {
|
||||
indices.Add(i);
|
||||
indices.Add(i + 1);
|
||||
indices.Add(i + 2);
|
||||
|
|
|
|||
|
|
@ -1,8 +1,4 @@
|
|||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
namespace LandblockExtraction.LandBlockExtractor {
|
||||
namespace LandblockExtraction.LandBlockExtractor {
|
||||
public class BlockStruct {
|
||||
public readonly static int BlockSize = 17;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace LandblockExtraction.LandBlockExtractor {
|
||||
public class VerticesStruct {
|
||||
public Vector3[] position { get; set; }
|
||||
public Vector3[] normal { get; set; }
|
||||
public Vector4[] color { get; set; }
|
||||
public Vector4[] farcolor { get; set; }
|
||||
public Vector2[] texturecoord { get; set; }
|
||||
|
|
@ -11,6 +12,7 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
|
||||
public VerticesStruct(int blockSize) {
|
||||
position = new Vector3[blockSize * blockSize];
|
||||
normal = new Vector3[blockSize * blockSize];
|
||||
color = new Vector4[blockSize * blockSize];
|
||||
farcolor = new Vector4[blockSize * blockSize];
|
||||
texturecoord = new Vector2[blockSize * blockSize];
|
||||
|
|
@ -20,26 +22,29 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
|
||||
public float[] Vertices() {
|
||||
int length = position.Length;
|
||||
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) {
|
||||
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) {
|
||||
vertices[vi] = position[i].X;
|
||||
vertices[vi + 1] = position[i].Y;
|
||||
vertices[vi + 2] = position[i].Z;
|
||||
vertices[vi + 3] = color[i].X;
|
||||
vertices[vi + 4] = color[i].Y;
|
||||
vertices[vi + 5] = color[i].Z;
|
||||
vertices[vi + 6] = color[i].W;
|
||||
vertices[vi + 7] = farcolor[i].X;
|
||||
vertices[vi + 8] = farcolor[i].Y;
|
||||
vertices[vi + 9] = farcolor[i].Z;
|
||||
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];
|
||||
vertices[vi + 3] = normal[i].X;
|
||||
vertices[vi + 4] = normal[i].Y;
|
||||
vertices[vi + 5] = normal[i].Z;
|
||||
vertices[vi + 6] = color[i].X;
|
||||
vertices[vi + 7] = color[i].Y;
|
||||
vertices[vi + 8] = color[i].Z;
|
||||
vertices[vi + 9] = color[i].W;
|
||||
vertices[vi + 10] = farcolor[i].X;
|
||||
vertices[vi + 11] = farcolor[i].Y;
|
||||
vertices[vi + 12] = farcolor[i].Z;
|
||||
vertices[vi + 13] = farcolor[i].W;
|
||||
vertices[vi + 14] = texturecoord[i].X;
|
||||
vertices[vi + 15] = texturecoord[i].Y;
|
||||
vertices[vi + 16] = terraintype[i].X;
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,5 +24,22 @@ namespace LandblockExtraction.Tools {
|
|||
public static Vector4 RGBAColorToVector4(RGBAColor color) {
|
||||
return new Vector4(color.r, color.g, color.b, color.a);
|
||||
}
|
||||
// Calcule la normale d'un triangle à partir de trois points
|
||||
public static Vector3 CalculateTriangleNormal(Vector3 A, Vector3 B, Vector3 C) {
|
||||
Vector3 AB = B - A;
|
||||
Vector3 AC = C - A;
|
||||
Vector3 normal = Vector3.Cross(AB, AC);
|
||||
normal = Vector3.Normalize(normal);
|
||||
return normal;
|
||||
}
|
||||
|
||||
// Calcule la normale moyenne d'un quadrilatère en utilisant deux de ses triangles
|
||||
public static Vector3 CalculateQuadNormal(Vector3 A, Vector3 B, Vector3 C, Vector3 D) {
|
||||
Vector3 normal1 = CalculateTriangleNormal(A, B, C);
|
||||
Vector3 normal2 = CalculateTriangleNormal(C, B, D);
|
||||
|
||||
Vector3 averageNormal = (normal1 + normal2) / 2;
|
||||
return Vector3.Normalize(averageNormal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue