320 lines
9.7 KiB
C#
320 lines
9.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
//public class TerrainDump : MonoBehaviour
|
|
//{
|
|
// byte[] mapIndexBuffer;
|
|
// // Start is called before the first frame update
|
|
// void Start()
|
|
// {
|
|
|
|
// }
|
|
|
|
// // Update is called once per frame
|
|
// void Update()
|
|
// {
|
|
// if (Input.GetKeyDown(KeyCode.D))
|
|
// {
|
|
// dumpHeightData();
|
|
// }
|
|
// if (Input.GetKeyDown(KeyCode.M))
|
|
// {
|
|
// dumpModels();
|
|
// }
|
|
// if (Input.GetKeyDown(KeyCode.R))
|
|
// {
|
|
// readModels();
|
|
// }
|
|
// if (Input.GetKeyDown(KeyCode.H))
|
|
// {
|
|
// generateRaisedGroundMapping(); // set true bool if want blue boxes for debugging
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// public void dumpHeightData()
|
|
// {
|
|
// Texture2D texture = new Texture2D(1025, 1025, TextureFormat.RGB24, false);
|
|
// for (int i = 0; i < 1025; i++)
|
|
// {
|
|
// for (int j = 0; j < 1025; j++)
|
|
// {
|
|
// Ray r = new Ray(new Vector3(j, 256, i), new Vector3(0, -1, 0));
|
|
// RaycastHit hit;
|
|
// float height;
|
|
// if (Physics.Raycast(r, out hit, 256, LayerMask.GetMask("Floor")))
|
|
// {
|
|
// //Debug.Log("Raycast hit! " + hit.point.y);
|
|
// height = hit.point.y;
|
|
// } else
|
|
// {
|
|
// height = 0.0f;
|
|
// }
|
|
// texture.SetPixel(j, i, new Color(height / 256, height / 256, height / 256));
|
|
// }
|
|
// }
|
|
|
|
// texture.Apply();
|
|
// byte[] fileData = texture.EncodeToPNG();
|
|
|
|
// System.IO.File.WriteAllBytes("hmtest.png", fileData);
|
|
// }
|
|
|
|
|
|
// public void readModels()
|
|
// {
|
|
// TextAsset ta = Resources.Load("test") as TextAsset;
|
|
// Packet pkt = new global::Packet(ta.bytes);
|
|
|
|
// int totalModels = pkt.ReadInt();
|
|
// for (int i = 0; i < totalModels; i++)
|
|
// {
|
|
// string name = pkt.readLongString();
|
|
// int materialCount = pkt.ReadInt();
|
|
// string[] materials = new string[materialCount];
|
|
// for (int j = 0; j < materialCount; j++)
|
|
// {
|
|
// materials[j] = pkt.readLongString();
|
|
// }
|
|
// float x = pkt.readFloat2();
|
|
// float y = pkt.readFloat2();
|
|
// float z = pkt.readFloat2();
|
|
// float heightFromTerrain = pkt.readFloat2();
|
|
// float rx = pkt.readFloat2();
|
|
// float ry = pkt.readFloat2();
|
|
// float rz = pkt.readFloat2();
|
|
// float rw = pkt.readFloat2();
|
|
// float sx = pkt.readFloat2();
|
|
// float sy = pkt.readFloat2();
|
|
// float sz = pkt.readFloat2();
|
|
// int mask = pkt.ReadInt();
|
|
// Debug.Log(name);
|
|
// GameObject resource = Resources.Load("AssetPacks/PolygonFantasyKingdom/Models/" + name) as GameObject;
|
|
// if (resource != null) {
|
|
// GameObject g = GameObject.Instantiate(resource) as GameObject;
|
|
// //MeshRenderer meshRenderer = g.AddComponent<MeshRenderer>();
|
|
// //MeshFilter meshFilter = g.AddComponent<MeshFilter>();
|
|
// //meshFilter.sharedMesh = Resources.Load("AssetPacks/PolygonFantasyKingdom/Models/" + name) as Mesh;
|
|
|
|
// g.name = name;
|
|
// if (g.GetComponent<MeshRenderer>() == null)
|
|
// {
|
|
// g.AddComponent<MeshRenderer>();
|
|
// }
|
|
// Material m = Resources.Load("AssetPacks/PolygonFantasyKingdom/Materials/" + materials[0]) as Material;
|
|
// g.GetComponent<MeshRenderer>().material = m;
|
|
// //g.GetComponent<MeshRenderer>().materials = new Material[materialCount];
|
|
// for (int j = 0; j < g.GetComponent<MeshRenderer>().materials.Length; j++)
|
|
// {
|
|
// Debug.Log("AssetPacks/PolygonFantasyKingdom/Materials/" + materials[j]);
|
|
// if (j < materials.Length)
|
|
// g.GetComponent<MeshRenderer>().materials[j] = Resources.Load("AssetPacks/PolygonFantasyKingdom/Materials/" + materials[j]) as Material;
|
|
// }
|
|
|
|
|
|
// g.transform.position = new Vector3(x, y, z);
|
|
// g.transform.rotation = new Quaternion(rx, ry, rz, rw);
|
|
// g.transform.localScale = new Vector3(sx, sy, sz);
|
|
|
|
// WorldObject t = g.AddComponent<WorldObject>();
|
|
// t.setmask(mask);
|
|
|
|
// Transform[] children = g.GetComponentsInChildren<Transform>();
|
|
// for (int j = 0; j < children.Length; j++)
|
|
// {
|
|
|
|
// if (children[j].GetComponent<Renderer>() != null)
|
|
// {
|
|
|
|
// children[j].gameObject.GetComponent<Renderer>().material = m;
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// public void dumpModels()
|
|
// {
|
|
// WorldObject[] ts = gameObject.GetComponentsInChildren<WorldObject>();
|
|
// //bytes required for map index
|
|
// int bytesRequired = 4; //num scene objects
|
|
// int modelCount = 0;
|
|
|
|
// for (int i = 0; i < ts.Length; i++)
|
|
// {
|
|
// int br = bytesRequiredForModel(ts[i].gameObject);
|
|
// if (br > 0)
|
|
// {
|
|
// bytesRequired += br;
|
|
// modelCount++;
|
|
// }
|
|
|
|
// }
|
|
|
|
// //PacketWriter p = new PacketWriter(bytesRequired);
|
|
// //Debug.Log(ts.Length);
|
|
// //p.writeInt(modelCount);
|
|
// //for (int i = 0; i < ts.Length; i++)
|
|
// //{
|
|
// // GameObject g = ts[i].gameObject;
|
|
// // dumpModel(g, p);
|
|
// //}
|
|
|
|
// //p.writeFile("test");
|
|
// }
|
|
|
|
// public int bytesRequiredForModel(GameObject g)
|
|
// {
|
|
// if (g.GetComponent<MeshFilter>() == null)
|
|
// {
|
|
// return 0;
|
|
// }
|
|
// int bytes = 0;
|
|
// string modelNameInstance = g.GetComponent<MeshFilter>().mesh.name;
|
|
// string model = modelNameInstance.Substring(0, modelNameInstance.Length - " Instance".Length);
|
|
// Debug.Log(model);
|
|
|
|
// int materialCount = g.GetComponent<MeshRenderer>().materials.Length;
|
|
// string[] materials = new string[materialCount];
|
|
// for (int i = 0; i < materialCount; i++)
|
|
// {
|
|
// string materialInstance = g.GetComponent<MeshRenderer>().materials[i].name;
|
|
// materials[i] = materialInstance.Substring(0, materialInstance.Length - " (Instance)".Length);
|
|
// }
|
|
// Vector3 position = g.transform.position;
|
|
// Quaternion rotation = g.transform.rotation;
|
|
// Vector3 scale = g.transform.lossyScale;
|
|
// Debug.Log(position);
|
|
// Debug.Log(rotation);
|
|
// Debug.Log(scale);
|
|
// int mask = g.GetComponent<WorldObject>().getMask();
|
|
// bytes += model.Length + 4; //prefix string length int
|
|
// bytes +=4; //material count
|
|
// for (int i = 0; i < materials.Length;i++)
|
|
// {
|
|
// bytes += 4 + materials[i].Length;
|
|
// }
|
|
// bytes += 4 * 3;//pos
|
|
// bytes += 4; //y height from terrain at x
|
|
// bytes += 4 * 4;//rot
|
|
// bytes += 4 * 3;//scale
|
|
// bytes += 4; //mask
|
|
// return bytes;
|
|
// }
|
|
|
|
// //public void dumpModel(GameObject g, PacketWriter p)
|
|
// //{
|
|
// // if (g.GetComponent<MeshFilter>() == null)
|
|
// // {
|
|
// // return;
|
|
// // }
|
|
// // string modelNameInstance = g.GetComponent<MeshFilter>().mesh.name;
|
|
// // string model = modelNameInstance.Substring(0, modelNameInstance.Length - " Instance".Length);
|
|
// // Debug.Log(model);
|
|
// // int materialCount = g.GetComponent<MeshRenderer>().materials.Length;
|
|
// // string[] materials = new string[materialCount];
|
|
// // for (int i = 0; i < materialCount; i++)
|
|
// // {
|
|
// // string materialInstance = g.GetComponent<MeshRenderer>().materials[i].name;
|
|
// // materials[i] = materialInstance.Substring(0, materialInstance.Length - " (Instance)".Length);
|
|
// // }
|
|
|
|
// // Vector3 position = g.transform.position;
|
|
// // Quaternion rotation = g.transform.rotation;
|
|
// // Vector3 scale = g.transform.lossyScale;
|
|
// // Debug.Log(position);
|
|
// // Debug.Log(rotation);
|
|
// // Debug.Log(scale);
|
|
// // int mask = g.GetComponent<ObjectType>().getMask();
|
|
|
|
// // p.writeString(model);
|
|
// // p.writeInt(materialCount);
|
|
// // for (int i = 0; i < materialCount; i++)
|
|
// // {
|
|
// // p.writeString(materials[i]);
|
|
// // }
|
|
// // p.writeFloat(position.x);
|
|
// // p.writeFloat(position.y);
|
|
// // p.writeFloat(position.z);
|
|
// // //y height from terrain
|
|
// // float terrainHeight = GameObject.Find("Terrain").GetComponent<Terrain>().getLowestVertexHeightOfTile((int)position.x, (int)position.z);
|
|
// // p.writeFloat(terrainHeight);
|
|
// // p.writeFloat(rotation.x);
|
|
// // p.writeFloat(rotation.y);
|
|
// // p.writeFloat(rotation.z);
|
|
// // p.writeFloat(rotation.w);
|
|
// // p.writeFloat(scale.x);
|
|
// // p.writeFloat(scale.y);
|
|
// // p.writeFloat(scale.z);
|
|
// // p.writeInt(mask);
|
|
|
|
// //}
|
|
|
|
// // Block out tiles 1x1 units that players cannot access.
|
|
// private void generateRaisedGroundMapping(bool withDebug = false)
|
|
// {
|
|
// Texture2D texture = new Texture2D(1025, 1025, TextureFormat.RGB24, false);
|
|
// float height;
|
|
// List<GameObject> objectsHit = new List<GameObject>();
|
|
|
|
// // set everything black - cannot do on the below as it uses raycastall and will reset pixels to black when rays hit underneath
|
|
// for (int i = 0; i < 1025; i++)
|
|
// {
|
|
// for (int j = 0; j < 1025; j++)
|
|
// {
|
|
// texture.SetPixel(j, i, new Color(0, 0, 0));
|
|
// }
|
|
// }
|
|
|
|
// // Iterate through all squares on the grid
|
|
// for (int i = 0; i < 1025; i++)
|
|
// {
|
|
// for (int j = 0; j < 1025; j++)
|
|
// {
|
|
|
|
// // if it hits a raised bit of ground, for instance a bridge - include the height on this.
|
|
// RaycastHit[] hits = Physics.RaycastAll(new Vector3(j, 256, i), new Vector3(0, -1, 0), 1025, 0|1|2|3|4|5|6|7|8|9|10|11|12|13, QueryTriggerInteraction.UseGlobal);
|
|
// objectsHit.Clear();
|
|
|
|
// foreach (RaycastHit h in hits)
|
|
// {
|
|
// objectsHit.Add(h.transform.gameObject);
|
|
|
|
// try
|
|
// {
|
|
// //if (h.transform.GetComponent<WorldObject>().isRaisedWalkablePlatform == true)
|
|
// //{
|
|
// // height = h.point.y;
|
|
// // texture.SetPixel(j, i, new Color(0, 0, height / 256));
|
|
// //
|
|
// // if (withDebug)
|
|
// // {
|
|
// // // create blue squares for top down view of blocks
|
|
// // GameObject walkMarker = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
|
// // walkMarker.AddComponent<WorldObject>(); // set this so we dont get exceptions as we are not ray casting against a layer
|
|
// // walkMarker.GetComponent<Renderer>().material.color = new Color(0, 0, height / 256);
|
|
// // walkMarker.transform.position = new Vector3(j, height, i);
|
|
// // }
|
|
// //}
|
|
// } catch(Exception e)
|
|
// {
|
|
// Debug.LogError(e);
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// texture.Apply();
|
|
// byte[] fileData = texture.EncodeToPNG();
|
|
|
|
// System.IO.File.WriteAllBytes("walkableRaisedGround.png", fileData);
|
|
// }
|
|
// }
|
|
|
|
//} // end class
|