KopMap/Assets/Scripts/MonoKopTest.cs

77 lines
2.6 KiB
C#
Raw Normal View History

2025-04-03 02:30:16 +08:00
using System.IO;
using UnityEngine;
namespace MindPowerSdk
{
public class MonoKopTest : MonoBehaviour
{
public string mapPath = "";
public string mapName = "";
public short chunkSize = 64;
public MPMap map = null;
public Material terrainMaterial;
public Material terrainMaterial2;
void Start()
{
using FileStream fs = File.OpenRead(Path.Combine(mapPath, mapName));
BinaryReader reader = new BinaryReader(fs);
map = new MPMap();
map.Load(reader);
Debug.Log($"map size:({map.Width},{map.Height}) | section size:({map.SectionWidth},{map.SectionHeight})");
// GenTerrainMesh();
// TerrainGenerator.GenTerrain(map, chunkSize, terrainMaterial2, transform);
}
public void GenTerrainMesh()
{
// 自定义分块生成
int xCnt = Mathf.CeilToInt((float)map.Width / chunkSize);
int yCnt = Mathf.CeilToInt((float)map.Height / chunkSize);
for (short i = 0; i < yCnt; i++)
{
for (short j = 0; j < xCnt; j++)
{
short x = (short)(j * chunkSize);
short y = (short)(i * chunkSize);
Mesh mesh = map.GenMesh(x, y, chunkSize);
(Texture2D texNo, Texture2D maskNo) = map.GenTxtNoTexture(x, y, chunkSize);
GameObject go = new GameObject("m_" + i);
// go.isStatic = true;
go.transform.parent = transform;
// Vector3 pos = Vector3.forward * map.Height;
// go.transform.position = pos;
// 设置材质
MeshRenderer meshRenderer = go.AddComponent<MeshRenderer>();
var mat = meshRenderer.material = new Material(terrainMaterial);
mat.SetTexture("_TexNo", texNo);
mat.SetTexture("_MaskNo", maskNo);
MeshFilter meshFilter = go.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
#if UNITY_EDITOR
//AssetDatabase.CreateAsset(mesh, "Assets/Sandbox/map.mesh");
//(Texture2D t1, Texture2D t2) = map.GenTxtNoTexture(x, y, chunkSize);
//byte[] png = t1.EncodeToPNG();
//File.WriteAllBytes($"Assets/Sandbox/map_tex_no.png", png);
//png = t2.EncodeToPNG();
//File.WriteAllBytes($"Assets/Sandbox/map_alph_no.png", png);
#endif
}
}
}
}
}