Debug terrain editing

This commit is contained in:
Nahuel Rocchetti 2023-07-28 01:14:05 -03:00
parent f74645bb30
commit fb5a3361e9
16 changed files with 199 additions and 28 deletions

View file

@ -313,7 +313,6 @@ GameObject:
- component: {fileID: 779771443}
- component: {fileID: 779771442}
- component: {fileID: 779771441}
- component: {fileID: 779771440}
- component: {fileID: 779771444}
m_Layer: 0
m_Name: Water
@ -322,20 +321,6 @@ GameObject:
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!64 &779771440
MeshCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 779771439}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 4
m_Convex: 0
m_CookingOptions: 30
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &779771441
MeshRenderer:
m_ObjectHideFlags: 0
@ -424,7 +409,7 @@ GameObject:
- component: {fileID: 976550843}
- component: {fileID: 976550846}
- component: {fileID: 976550842}
m_Layer: 0
m_Layer: 3
m_Name: Terrain
m_TagString: Untagged
m_Icon: {fileID: 0}
@ -743,6 +728,7 @@ GameObject:
- component: {fileID: 1642340381}
- component: {fileID: 1642340384}
- component: {fileID: 1642340385}
- component: {fileID: 1642340386}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
@ -849,3 +835,19 @@ MonoBehaviour:
ReflectionTexture: {fileID: 0}
m_ClipPlaneOffset: 0.07
ReflectionSurface: {fileID: 779771439}
--- !u!114 &1642340386
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1642340380}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2eff8bcd6fb795946b0eb83411331f30, type: 3}
m_Name:
m_EditorClassIdentifier:
BrushSize: 40
BrushForce: 5
UpdateColliderRealtime: 1
UpdateNormalsRealtime: 1

View file

@ -12,11 +12,13 @@ namespace OpenTS2.Content.DBPF
public float SeaLevel { get; }
public string TerrainTypeName { get; }
public TerrainType TerrainType => TerrainManager.GetTerrainType(TerrainTypeName);
public FloatArray2D VertexHeights { get; }
public FloatArray2D VertexHeights => _vertexHeights;
private FloatArray2D _vertexHeights;
public NeighborhoodTerrainAsset(int width, int height, float seaLevel, string terrainType,
FloatArray2D vertexHeights) =>
(Width, Height, SeaLevel, TerrainTypeName, VertexHeights) =
(Width, Height, SeaLevel, TerrainTypeName, _vertexHeights) =
(width, height, seaLevel, terrainType, vertexHeights);
public Mesh MakeMesh()
@ -58,6 +60,21 @@ namespace OpenTS2.Content.DBPF
return terrainMesh;
}
public void FromMesh(Mesh terrainMesh)
{
var floatArr = new float[Width, Height];
var vertices = terrainMesh.vertices;
foreach(var vert in vertices)
{
var vertW = (int)(vert.x / 10f);
var vertH = (int)(vert.z / 10f);
if (vertW >= Width || vertH >= Height)
continue;
floatArr[vertW, vertH] = vert.y;
}
_vertexHeights = new FloatArray2D(floatArr);
}
public void ApplyToTerrain(Terrain terrain)
{
var heightMap = new float[VertexHeights.values.GetLength(1), VertexHeights.values.GetLength(0)];

View file

@ -0,0 +1,94 @@
using OpenTS2.Content;
using OpenTS2.Rendering;
using OpenTS2.Scenes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace OpenTS2.Diagnostic
{
public class DebugTerrainEditor : MonoBehaviour
{
public float BrushSize = 20f;
public float BrushForce = 5f;
public bool UpdateColliderRealtime = false;
public bool UpdateNormalsRealtime = false;
bool _usingTool = false;
int _terrainLayerMask = (1 << 3);
private void Start()
{
NeighborhoodTerrain.Instance.GetComponent<MeshFilter>().sharedMesh.MarkDynamic();
}
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.F1))
{
if (Input.GetKey(KeyCode.Mouse0))
{
_usingTool = true;
DoRaise(BrushForce);
return;
}
if (Input.GetKey(KeyCode.Mouse1))
{
_usingTool = true;
DoRaise(-BrushForce);
return;
}
}
if (_usingTool)
{
_usingTool = false;
var terrainInstance = NeighborhoodTerrain.Instance;
if (terrainInstance != null)
{
var meshFilter = terrainInstance.GetComponent<MeshFilter>();
var collider = terrainInstance.GetComponent<MeshCollider>();
if (meshFilter != null && collider != null)
{
if (!UpdateNormalsRealtime)
meshFilter.sharedMesh.RecalculateNormals();
if (!UpdateColliderRealtime)
collider.sharedMesh = meshFilter.sharedMesh;
var terrainAsset = NeighborhoodManager.CurrentNeighborhood.Terrain;
terrainAsset.FromMesh(meshFilter.sharedMesh);
//terrainAsset.Compressed = true;
//terrainAsset.Save();
}
}
}
}
void DoRaise(float force)
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hitInfo, 2000f, _terrainLayerMask))
{
var terrain = hitInfo.collider.GetComponent<NeighborhoodTerrain>();
if (terrain == null)
return;
var terrainMesh = terrain.GetComponent<MeshFilter>().sharedMesh;
var vertices = terrainMesh.vertices;
for(var i=0;i<vertices.Length;i++)
{
var dist = Vector3.Distance(vertices[i], hitInfo.point);
dist = (-Mathf.Min(0f, dist - BrushSize))/BrushSize;
dist *= dist;
vertices[i].y += force * dist;
}
terrainMesh.vertices = vertices;
LightmapManager.RenderShadowMap();
if (UpdateNormalsRealtime)
terrainMesh.RecalculateNormals();
if (UpdateColliderRealtime)
{
(hitInfo.collider as MeshCollider).sharedMesh = terrainMesh;
}
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2eff8bcd6fb795946b0eb83411331f30
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -4,7 +4,7 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace OpenTS2.Engine.Diagnostic
namespace OpenTS2.Diagnostic
{
[RequireComponent(typeof(Text))]
public class FPSCounter : MonoBehaviour

View file

@ -244,13 +244,13 @@ namespace OpenTS2.Files.Formats.DBPF
try
{
//begin main Compression Loop
while (index < data.Length - 3)
while (index < data.Length - 6)
{
#region get all Compression Candidates (list of offsets for all occurances of the current 3 bytes)
do
{
index++;
if (index >= data.Length - 2)
if (index >= data.Length - 5)
{
end = true;
break;
@ -298,7 +298,6 @@ namespace OpenTS2.Files.Formats.DBPF
else if ((offsetcopycount < 4) && (copyoffset > 0x400)) offsetcopycount = 0;
else if ((offsetcopycount < 5) && (copyoffset > 0x4000)) offsetcopycount = 0;
//this is offset-compressable? so do the compression
if (offsetcopycount > 0)
{

View file

@ -1,6 +1,8 @@
using System;
using System.IO;
using System.Text;
using OpenTS2.Common;
using OpenTS2.Common.Utils;
using OpenTS2.Content;
using OpenTS2.Content.DBPF;
using OpenTS2.Files.Formats.DBPF.Types;
@ -48,5 +50,25 @@ namespace OpenTS2.Files.Formats.DBPF
reader.Dispose();
return new NeighborhoodTerrainAsset(width, height, seaLevel, terrainType, vertexHeights);
}
public override byte[] Serialize(AbstractAsset asset)
{
var terrainAsset = asset as NeighborhoodTerrainAsset;
var stream = new MemoryStream(0);
var writer = new BinaryWriter(stream);
writer.Write(TypeIDs.NHOOD_TERRAIN);
writer.Write(4);
writer.Write(terrainAsset.Width);
writer.Write(terrainAsset.Height);
writer.Write(terrainAsset.SeaLevel);
var terrainType = terrainAsset.TerrainType.Name;
writer.Write(terrainType.Length);
writer.Write(Encoding.UTF8.GetBytes(terrainType));
writer.Write(terrainAsset.VertexHeights.Serialize());
var buffer = StreamUtils.GetBuffer(stream);
stream.Dispose();
writer.Dispose();
return buffer;
}
}
}

View file

@ -1,4 +1,7 @@
using System;
using System.IO;
using System.Text;
using OpenTS2.Common.Utils;
using OpenTS2.Files.Utils;
using UnityEngine;
@ -21,6 +24,28 @@ namespace OpenTS2.Files.Formats.DBPF.Types
return $"FloatArray2D({nameof(values)}: {values})";
}
public byte[] Serialize()
{
var width = values.GetLength(0);
var height = values.GetLength(1);
var stream = new MemoryStream(0);
var writer = new BinaryWriter(stream);
writer.Write(TypeId);
writer.Write(1);
writer.Write(TypeName.Length);
writer.Write(Encoding.UTF8.GetBytes(TypeName));
writer.Write(width);
writer.Write(height);
for (var i = 0; i < width * height; i++)
{
writer.Write(values[i % width, i / width]);
}
var buffer = StreamUtils.GetBuffer(stream);
stream.Dispose();
writer.Dispose();
return buffer;
}
public static FloatArray2D Deserialize(IoBuffer reader)
{
var id = reader.ReadUInt32();

View file

@ -23,8 +23,8 @@ namespace OpenTS2.Rendering
private static RenderTexture s_shoreMap;
private static Shader s_shoreMapShader = Shader.Find("OpenTS2/ShoreMask");
private static Material s_shoreMapMaterial = new Material(s_shoreMapShader);
private static int s_shadowMapResolution = 128;
private static int s_heightMapResolution = 128;
private static int s_shadowMapResolution = 256;
private static int s_heightMapResolution = 256;
private static int s_shoreResolution = 128;
/// <summary>

View file

@ -24,10 +24,12 @@ namespace OpenTS2.Scenes
private static ResourceKey s_shoreKey = new ResourceKey("neighborhood-terrain-moisture-9_txtr", 0x1C0532FA, TypeIDs.SCENEGRAPH_TXTR);
//private static ResourceKey TemperateWetKey = new ResourceKey(0xFF354609, 0x1A9C59CC, 0x1C0532FA, TypeIDs.SCENEGRAPH_TXTR);
private Material _terrainMaterial;
private MeshFilter _meshFilter;
// Start is called before the first frame update
void Awake()
{
Instance = this;
_meshFilter = GetComponent<MeshFilter>();
var contentProvider = ContentProvider.Get();
var meshRenderer = GetComponent<MeshRenderer>();
_terrainMaterial = meshRenderer.material;
@ -66,10 +68,9 @@ namespace OpenTS2.Scenes
{
var terrainAsset = NeighborhoodManager.CurrentNeighborhood.Terrain;
var terrainMesh = terrainAsset.MakeMesh();
var meshFilter = GetComponent<MeshFilter>();
var meshCollider = GetComponent<MeshCollider>();
var meshRenderer = GetComponent<MeshRenderer>();
meshFilter.sharedMesh = terrainMesh;
_meshFilter.sharedMesh = terrainMesh;
meshCollider.sharedMesh = terrainMesh;
InitializeVertexColors(terrainMesh);
var vars1 = GetVariationRectangles(terrainAsset.Width, terrainAsset.Height);

View file

@ -67,7 +67,7 @@ Shader "OpenTS2/HeightMapShadows"
{
// sample the texture
//fixed4 col = tex2D(_MainTex, i.uv);
float shade = getShade(i.uv, 0.01, 1.0);
float shade = getShade(i.uv, 0.002, 1.0);
fixed4 col = fixed4(shade, shade, shade, 1);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);

View file

@ -8,7 +8,7 @@ TagManager:
- Default
- TransparentFX
- Ignore Raycast
-
- Terrain
- Water
- UI
-