Skip to content

Commit

Permalink
Merge from 'develop' into 'main' for CLOiSim-4.5.3 (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunseok-yang authored May 11, 2024
2 parents c9632b0 + b439b96 commit 2bf7780
Show file tree
Hide file tree
Showing 25 changed files with 507 additions and 305 deletions.
4 changes: 2 additions & 2 deletions Assets/Scripts/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private IEnumerator LoadModel(string modelPath, string modelFileName)
// Debug.Log("Parsed: " + item.Key + ", " + item.Value.Item1 + ", " + item.Value.Item2);
model.Name = GetClonedModelName(model.Name);

yield return StartCoroutine(_sdfLoader.StartImport(model));
yield return StartCoroutine(_sdfLoader.Start(model));

var targetObject = _worldRoot.transform.Find(model.Name);

Expand Down Expand Up @@ -393,7 +393,7 @@ private IEnumerator LoadWorld()
_sdfLoader.SetRootLights(_lightsRoot);
_sdfLoader.SetRootRoads(_roadsRoot);

yield return _sdfLoader.StartImport(world);
yield return _sdfLoader.Start(world);

// for GUI
_followingList?.UpdateList();
Expand Down
8 changes: 5 additions & 3 deletions Assets/Scripts/Tools/Mesh/ProceduralMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,9 @@ public static Mesh CreateSphere(in float radius = 1f, int nbLong = 24, int nbLat
{
for (var u = 0; u < resX; u++)
{
uvs[u + v * resX] = new Vector2((float)u / (resX - 1), (float)v / (resZ - 1));
var U = (float)u / (resX - 1);
var V = (float)v / (resZ - 1);
uvs[u + v * resX] = new Vector2(V, U);
}
}
#endregion
Expand Down Expand Up @@ -726,7 +728,7 @@ public static Mesh CreatePolylines(in SDF.Polylines polylines)
{
foreach (var point in polylines[0].point)
{
pointsToTriangulate.Add(new Vector2((float)point.Y, (float)point.X));
pointsToTriangulate.Add(SDF2Unity.Point(point));
}
}

Expand All @@ -740,7 +742,7 @@ public static Mesh CreatePolylines(in SDF.Polylines polylines)
var points = new List<Vector2>();
foreach (var point in polylines[i].point)
{
points.Add(new Vector2((float)point.Y, (float)point.X));
points.Add(SDF2Unity.Point(point));
}
constrainedEdgePoints.Add(points);
}
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Tools/SDF/Implement/Implement.Collision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private static void RemoveRenderers(UE.MeshFilter[] meshFilters)
var meshRenderer = meshFilter.GetComponent<UE.MeshRenderer>();
if (meshRenderer != null)
{
UE.Debug.LogWarning($"{meshFilter.name} MeshRenderer should not exist");
UE.GameObject.Destroy(meshRenderer);
}
UE.GameObject.Destroy(meshFilter);
Expand Down
7 changes: 2 additions & 5 deletions Assets/Scripts/Tools/SDF/Implement/Implement.Geometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public static void GenerateMeshObject(in SDF.ShapeType shape, in UE.GameObject t
{
var plane = shape as SDF.Plane;
var normal = SDF2Unity.Normal(plane.normal);
mesh = ProceduralMesh.CreatePlane((float)plane.size.X, (float)plane.size.Y, normal);
var size = SDF2Unity.Size(plane.size);
mesh = ProceduralMesh.CreatePlane(size.x, size.y, normal);
}
else if (shape is SDF.Polylines)
{
Expand All @@ -105,10 +106,6 @@ public static void GenerateMeshObject(in SDF.ShapeType shape, in UE.GameObject t

var meshFilter = createdObject.AddComponent<UE.MeshFilter>();
meshFilter.sharedMesh = mesh;

var meshRenderer = createdObject.AddComponent<UE.MeshRenderer>();
meshRenderer.sharedMaterial = SDF2Unity.Material.Create(mesh.name);
meshRenderer.allowOcclusionWhenDynamic = true;
}

createdObject.transform.SetParent(targetParentObject.transform, false);
Expand Down
271 changes: 271 additions & 0 deletions Assets/Scripts/Tools/SDF/Implement/Implement.Material.Ogre.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
/*
* Copyright (c) 2020 LG Electronics Inc.
*
* SPDX-License-Identifier: MIT
*/

using System.Collections.Generic;
using System.IO;
using System;
using UE = UnityEngine;

namespace SDF
{
namespace Implement
{
public partial class Material
{
public static class Ogre
{
private static string FindFile(in List<string> uris, in string targetFileName)
{
var ext = Path.GetExtension(targetFileName);

foreach (var currentDir in uris)
{
var subdirectoryEntries = Directory.GetDirectories(currentDir);
var subDirectories = new List<string>();
subDirectories.AddRange(subdirectoryEntries);
subDirectories.Insert(0, currentDir);
foreach (var subdirectory in subDirectories)
{
var fileEntries = Directory.GetFiles(subdirectory, "*" + ext);
foreach (var fileName in fileEntries)
{
if (fileName.EndsWith(targetFileName))
return fileName;
}
}
}

return string.Empty;
}


private static void ApplyVertexColour(in Dictionary<string, string> passProperties, UE.Material material)
{
if (passProperties.ContainsKey("diffuse"))
{
var diffuse = passProperties["diffuse"].Trim();
SDF2Unity.Material.SetBaseColor(material, SDF2Unity.Color(diffuse));
}

if (passProperties.ContainsKey("emissive"))
{
var emissive = passProperties["emissive"].Trim();
SDF2Unity.Material.SetEmission(material, SDF2Unity.Color(emissive));
}

if (passProperties.ContainsKey("specular"))
{
var specular = passProperties["specular"].Trim();

var tmp = specular.Replace('\t', ' ').Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (tmp.Length == 5)
{
var shininess = Convert.ToSingle(tmp[4]);
// ObsoleteProperties in Simple lit
material.SetFloat("_Shininess", shininess);

specular = string.Join(" ", tmp, 0, 4);
}
else if (tmp.Length == 4)
{
var alpha = Convert.ToSingle(tmp[3]);
if (alpha > 1)
{
material.SetFloat("_Shininess", alpha);
var r = Convert.ToSingle(tmp[0]);
var g = Convert.ToSingle(tmp[1]);
var b = Convert.ToSingle(tmp[2]);
tmp[3] = Convert.ToString((r + g + b) / 3f);
}

specular = string.Join(" ", tmp, 0, 4);
}

SDF2Unity.Material.SetSpecular(material, SDF2Unity.Color(specular));
}

if (passProperties.ContainsKey("scene_blend"))
{
var sceneBlend = passProperties["scene_blend"].Trim();
if (sceneBlend == "alpha_blend")
{
SDF2Unity.Material.SetTransparent(material);
}
}
}

private static void ApplyTexture(
in string path,
in Dictionary<string, string> props,
UE.Material material)
{
var texture = MeshLoader.GetTexture(path);
if (texture != null)
{
if (props.ContainsKey("filtering"))
{
var textureFiltering = props["filtering"];
// to make upper in First character
switch (textureFiltering)
{
case "bilinear":
texture.filterMode = UE.FilterMode.Bilinear;
break;
case "trilinear":
case "anisotropic":
texture.filterMode = UE.FilterMode.Trilinear;
break;
case "none":
default:
texture.filterMode = UE.FilterMode.Point;
break;
}
}

if (props.ContainsKey("max_anisotropy"))
{
var textureAnisotropy = props["max_anisotropy"];
texture.anisoLevel = Convert.ToInt32(textureAnisotropy);
}

if (props.ContainsKey("scale"))
{
var scaleSet = props["scale"];
var tileScale = SDF2Unity.Scale(scaleSet);

// UE.Debug.Log(tileScale);

// TODO: Check texture tile scaling
tileScale.x = 1 / tileScale.x;
tileScale.y = 1 / tileScale.y;

material.SetTextureScale("_BaseMap", tileScale);
}

material.SetTexture("_BaseMap", texture);
}
else
{
UE.Debug.LogWarning($"Wrong texture path: {path}");
}
}

private static void ApplyTextureUnits(
in Dictionary<string, OgreMaterial.TextureUnit> textrueUnits,
UE.Material material,
in List<string> uris)
{
foreach (var textureUnitEntry in textrueUnits)
{
var textureunit = textureUnitEntry.Value;

// UE.Debug.Log($" TextureUnit: {textureunitEntry.Key}");
// foreach (var kvp in textureunit.properties)
// {
// UE.Debug.Log($" TextureUnit: {kvp.Key} -> {kvp.Value}");
// }
var textureUnitProps = textureunit.properties;

if (!textureUnitProps.ContainsKey("texture"))
{
continue;
}

var textureFileName = textureUnitProps["texture"];
var textureFilePath = FindFile(uris, textureFileName);

// UE.Debug.Log(textureFilePath + ", " + textureFileName);
if (string.IsNullOrEmpty(textureFilePath))
{
continue;
}

ApplyTexture(textureFilePath, textureUnitProps, material);
return;
}
}

private static UE.Material[] ResizeMaterials(in OgreMaterial.Material ogreMaterial, in UE.Material[] baseMaterials)
{
var requiredMaterialCount = 0;
foreach (var techEntry in ogreMaterial.techniques)
{
var technique = techEntry.Value;
requiredMaterialCount += technique.passes.Count;
}
// UE.Debug.LogWarning($"requiredMaterialCount: {requiredMaterialCount}");

// resizing materials
UE.Material[] materials = null;
if (baseMaterials.Length < requiredMaterialCount)
{
var prevMaterials = (UE.Material[])baseMaterials.Clone();
var lastMaterial = prevMaterials[prevMaterials.Length - 1];

materials = new UE.Material[requiredMaterialCount];
for (var i = 0; i < prevMaterials.Length; i++)
{
materials[i] = prevMaterials[i];
}
for (var i = prevMaterials.Length - 1; i < requiredMaterialCount; i++)
{
materials[i] = new UE.Material(lastMaterial);
}

// UE.Debug.Log("Resizing materials " + materials.Length);
}
else
{
materials = baseMaterials;
}

return materials;
}

public static UE.Material[] ApplyMaterial(
in OgreMaterial.Material ogreMaterial,
UE.Material[] baseMaterials,
in List<string> texturePathURIs)
{
var materials = ResizeMaterials(ogreMaterial, baseMaterials);

if (ogreMaterial.hasReceiveShadows)
{
foreach (var material in materials)
{
material.SetFloat("_ReceiveShadows", ogreMaterial.receiveShadows ? 1f : 0);
}
}

var materialIndex = 0;
foreach (var techEntry in ogreMaterial.techniques)
{
var technique = techEntry.Value;

foreach (var passEntry in technique.passes)
{
var pass = passEntry.Value;

// UE.Debug.Log($"Passes in Technique: {technique.passes.Count}");
// foreach (var kvp in pass.properties)
// {
// UE.Debug.Log($" Pass: {kvp.Key}: {kvp.Value}");
// }

var material = materials[materialIndex++];

ApplyVertexColour(pass.properties, material);

ApplyTextureUnits(pass.textureUnits, material, texturePathURIs);
}
}

return materials;
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Tools/SDF/Implement/Implement.Material.Ogre.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2bf7780

Please sign in to comment.