Commit 76a11b1d authored by BRUNEAU Julien's avatar BRUNEAU Julien
Browse files

+ Add wall and pillars loading feature

parent 1afde5bb
fileFormatVersion: 2
guid: 42390f8e53a82ec43b071178ef245c55
TextureImporter:
fileIDToRecycleName: {}
externalObjects: {}
serializedVersion: 7
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -100
wrapU: 2
wrapV: 2
wrapW: 2
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- serializedVersion: 2
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: Android
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: WebGL
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID: 8955e68877c90c347a4a186db39f9713
vertices: []
indices:
edges: []
weights: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 8b98398441c2b0a498faa12d0dd8f8cf
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:
......@@ -38,6 +38,7 @@ public class CustomEditor_LoadEnv : Editor
if (GUILayout.Button("Create Template Config Files"))
{
ConfigReader.CreateTemplate();
ObstaclesReader.CreateTemplate();
}
}
}
......
......@@ -8,6 +8,7 @@ using UnityEngine.UI;
public class MenuManager : MonoBehaviour {
string configPath;
ObstaclesReader obstReader;
Camera cam;
InputField inputConfigPath;
......@@ -20,7 +21,7 @@ public class MenuManager : MonoBehaviour {
cam = Camera.main;
menu=GameObject.FindGameObjectWithTag("Menu");
obstReader = new ObstaclesReader();
string dataPath = defaultScenarioPath();
......@@ -94,7 +95,10 @@ public class MenuManager : MonoBehaviour {
if (configPath == null || configFilesMenu.value == 0)
return;
ConfigReader.loadConfig(configPath);
ConfigReader.LoadConfig(configPath);
obstReader.clear();
obstReader.LoadObstacles(ConfigReader.obstaclesFile);
obstReader.createObstacles();
LoadEnv env =gameObject.GetComponent<LoadEnv>();
env.loadScenario(ConfigReader.trajectoriesDir);
......@@ -108,7 +112,7 @@ public class MenuManager : MonoBehaviour {
public void saveConfig()
{
ConfigReader.CreateXML(configPath);
ConfigReader.SaveConfig(configPath);
}
public void exit()
......
......@@ -50,21 +50,22 @@ public static class ConfigReader
data = new ConfigData();
}
static public void loadConfig(string path)
static public void LoadConfig(string path)
{
configFileName = path;
if (File.Exists(path))
{
string _info = LoadXML();
if (_info.ToString() != "")
{
data = (ConfigData)DeserializeObject(_info);
}
data= XMLLoader.LoadXML<ConfigData>(path);
}
}
public static void SaveConfig(string path)
{
XMLLoader.CreateXML<ConfigData>(path, data);
}
/// <summary>
/// Create a template config with all possible parameters
/// </summary>
......@@ -74,107 +75,22 @@ public static class ConfigReader
int lastIndex = pathPlayer.LastIndexOf('/');
string dataPath = pathPlayer.Remove(lastIndex, pathPlayer.Length - lastIndex);
data = new ConfigData();
data.colorList = new List<ConfigAgentColor>();
data.colorList.Add(new ConfigAgentColor(0, 1, 191, 1, 0, 0));
data.colorList.Add(new ConfigAgentColor(192, 1, 383, 0, 1, 0));
CreateXML(dataPath + @" / ConfigTemplate.xml");
}
#region XMLserialization
/// <summary>
/// Create the file config.XML
/// </summary>
static public void CreateXML()
{
CreateXML(configFileName);
}
/// <summary>
/// Create a config file
/// </summary>
/// <param name="filename">The name of the config</param>
static public void CreateXML(string filename)
{
StreamWriter writer;
FileInfo t = new FileInfo(filename);
if (!t.Exists)
{
writer = t.CreateText();
}
else
{
t.Delete();
writer = t.CreateText();
}
writer.Write(SerializeObject(data));
writer.Close();
}
/// <summary>
/// Read a file
/// </summary>
/// <returns>The complete string of the file</returns>
static string LoadXML()
{
StreamReader r = File.OpenText(configFileName);
string _info = r.ReadToEnd();
r.Close();
return _info;
}
/// <summary>
/// Serialize an object into an XML text
/// </summary>
/// <param name="pObject">Objct to serialze</param>
/// <returns>XML string representing the inout object</returns>
static string SerializeObject(object pObject)
{
string XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(ConfigData));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xmlTextWriter.Formatting= Formatting.Indented;
xs.Serialize(xmlTextWriter, pObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}
/// <summary>
/// Parse and XML file to deserialize a c# object
/// </summary>
/// <param name="pXmlizedString">XML string to parse</param>
/// <returns>C# object deserialzed from the XML string</returns>
static object DeserializeObject(string pXmlizedString)
{
XmlSerializer xs = new XmlSerializer(typeof(ConfigData));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
}
static string UTF8ByteArrayToString(byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
ConfigData template = new ConfigData();
template.colorList = new List<ConfigAgentColor>();
template.colorList.Add(new ConfigAgentColor(0, 1, 191, 1, 0, 0));
template.colorList.Add(new ConfigAgentColor(192, 1, 383, 0, 1, 0));
XMLLoader.CreateXML<ConfigData>(dataPath + @" / ConfigTemplate.xml", template);
}
static byte[] StringToUTF8ByteArray(string pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
#endregion
#region get_set
static public string trajectoriesDir
{
get { return data.env_filesPath; }
}
static public string obstaclesFile
{
get { return data.env_obstFile; }
}
static public Vector3 camPosition
{
get { return data.cam.position.vect; }
......@@ -242,6 +158,7 @@ public class ConfigData
// Evironnement config
public string env_filesPath;
public string env_obstFile;
// Camera config
public ConfigCam cam;
......@@ -258,6 +175,7 @@ public class ConfigData
{
// Evironnement config
env_filesPath = ".\\TrajExample\\ExampleTwoColor\\";
env_obstFile = "";
// Camera config
cam = new ConfigCam();
......
/* Crowd Simulator Engine
** Copyright (C) 2018 - Inria Rennes - Rainbow - Julien Pettre
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** Authors: Julien Bruneau
**
** Contact: crowd_group@inria.fr
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.Xml.Serialization;
public class ObstaclesReader {
XMLObstacles obst;
GameObject defaultWall;
GameObject defaultPillar;
List<GameObject> obstList;
public ObstaclesReader()
{
defaultPillar = null;
defaultWall = null;
obst = new XMLObstacles();
obstList = new List<GameObject>();
GameObject[] obstGO = GameObject.FindGameObjectsWithTag("Obst");
foreach (GameObject g in obstGO)
{
if (g.name == "Wall")
defaultWall = g;
else if (g.name == "Pillar")
defaultPillar = g;
g.SetActive(false);
}
}
public void clear()
{
foreach (GameObject g in obstList)
GameObject.Destroy(g);
obstList.Clear();
}
public void LoadObstacles(string file)
{
if (file != "")
obst = XMLLoader.LoadXML<XMLObstacles>(file);
}
public void createObstacles()
{
foreach (XMLRect g in obst.rectangles)
createRectangle(g);
foreach (XMLCylinder g in obst.cylinders)
createCylinder(g);
}
private void createRectangle(XMLRect infos)
{
float height = 1.8f;
GameObject cube = GameObject.Instantiate(defaultWall);
cube.transform.position = ((Vector3)infos.a + (Vector3)infos.b) / 2 + new Vector3(0, height / 2, 0);
Vector3 dir = ((Vector3)infos.b - (Vector3)infos.a);
//cylinder.transform.rotation.SetLookRotation(dir);
cube.transform.LookAt((Vector3)infos.b + dir);
cube.transform.localScale = new Vector3(infos.w, height, dir.magnitude);
cube.SetActive(true);
obstList.Add(cube);
}
private void createCylinder(XMLCylinder infos)
{
float height = 1.8f;
GameObject cylinder = GameObject.Instantiate(defaultPillar);
cylinder.transform.position = infos.c + new Vector3(0, height / 2, 0);
cylinder.transform.localScale = new Vector3(infos.r, height / 2, infos.r);
cylinder.SetActive(true);
obstList.Add(cylinder);
}
public static void CreateTemplate()
{
string pathPlayer = Application.dataPath;
int lastIndex = pathPlayer.LastIndexOf('/');
string dataPath = pathPlayer.Remove(lastIndex, pathPlayer.Length - lastIndex);
XMLObstacles template = new XMLObstacles();
template.rectangles.Add(new XMLRect(new XMLVect(0, 0), new XMLVect(5, 1), 1));
template.rectangles.Add(new XMLRect(new XMLVect(0, -2.2f), new XMLVect(5, -2.2f), 1));
template.cylinders.Add(new XMLCylinder(new XMLVect(-2, -1), 1.5f));
template.cylinders.Add(new XMLCylinder(new XMLVect(6, 0), .5f));
XMLLoader.CreateXML<XMLObstacles>(dataPath + @" / ObstaclesTemplate.xml", template);
}
}
public class XMLObstacles
{
[XmlArray("Rectangles")]
[XmlArrayItem("Rectangle")]
public List<XMLRect> rectangles;
[XmlArray("Cylinders")]
[XmlArrayItem("Cylinder")]
public List<XMLCylinder> cylinders;
public XMLObstacles()
{
rectangles = new List<XMLRect>();
cylinders = new List<XMLCylinder>();
}
}
public class XMLRect
{
[XmlElement("A")]
public XMLVect a;
[XmlElement("B")]
public XMLVect b;
[XmlAttribute("Width")]
public float w;
public XMLRect()
{
a = null;
b = null;
w = 0;
}
public XMLRect(XMLVect side1, XMLVect side2, float width)
{
a = side1;
b = side2;
w = width;
}
}
public class XMLCylinder
{
[XmlElement("Center")]
public XMLVect c;
[XmlAttribute("Radius")]
public float r;
public XMLCylinder()
{
c = null;
r = 0;
}
public XMLCylinder(XMLVect center, float radius)
{
c = center;
r = radius;
}
}
public class XMLVect
{
[XmlAttribute("X")]
public float x;
[XmlAttribute("Y")]
public float y;
public XMLVect()
{
x = 0;
y = 0;
}
public XMLVect(float X, float Y)
{
x = X;
y = Y;
}
public static implicit operator Vector3(XMLVect o)
{
return o == null ? new Vector3(0,0,0) : new Vector3(-o.x,0,o.y);
}
public static implicit operator XMLVect(Vector3 o)
{
return o == null ? null : new XMLVect(-o.x,o.z);
}
}
fileFormatVersion: 2
guid: bd8fbba0620ecd8408a44b756ac47562
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
/* Crowd Simulator Engine
** Copyright (C) 2018 - Inria Rennes - Rainbow - Julien Pettre
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** Authors: Julien Bruneau
**
** Contact: crowd_group@inria.fr
*/
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
/// <summary>
/// Tools to serialize/deserialize data to/from XML files
/// </summary>
public static class XMLLoader {
// -----------------------------------------
// DATA SERIALIZATION FOR XML SAVING/LOADING
#region dataSerialization
public static void CreateXML<ObjType>(string fileName, object pObject)
{
StreamWriter writer;
FileInfo t = new FileInfo(fileName);
if (!t.Exists)
{
writer = t.CreateText();
}
else
{
t.Delete();
writer = t.CreateText();
}