diff --git a/Assets/Scripts/Main.cs b/Assets/Scripts/Main.cs index 3a602883..0fdb2ee7 100644 --- a/Assets/Scripts/Main.cs +++ b/Assets/Scripts/Main.cs @@ -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); @@ -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(); diff --git a/Assets/Scripts/Tools/Mesh/ProceduralMesh.cs b/Assets/Scripts/Tools/Mesh/ProceduralMesh.cs index 4526c3a4..21755cba 100644 --- a/Assets/Scripts/Tools/Mesh/ProceduralMesh.cs +++ b/Assets/Scripts/Tools/Mesh/ProceduralMesh.cs @@ -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 @@ -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)); } } @@ -740,7 +742,7 @@ public static Mesh CreatePolylines(in SDF.Polylines polylines) var points = new List(); 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); } diff --git a/Assets/Scripts/Tools/SDF/Implement/Implement.Collision.cs b/Assets/Scripts/Tools/SDF/Implement/Implement.Collision.cs index 2017c0e6..c7a2c9b3 100644 --- a/Assets/Scripts/Tools/SDF/Implement/Implement.Collision.cs +++ b/Assets/Scripts/Tools/SDF/Implement/Implement.Collision.cs @@ -101,6 +101,7 @@ private static void RemoveRenderers(UE.MeshFilter[] meshFilters) var meshRenderer = meshFilter.GetComponent(); if (meshRenderer != null) { + UE.Debug.LogWarning($"{meshFilter.name} MeshRenderer should not exist"); UE.GameObject.Destroy(meshRenderer); } UE.GameObject.Destroy(meshFilter); diff --git a/Assets/Scripts/Tools/SDF/Implement/Implement.Geometry.cs b/Assets/Scripts/Tools/SDF/Implement/Implement.Geometry.cs index c6d47a5d..5c2d8e31 100644 --- a/Assets/Scripts/Tools/SDF/Implement/Implement.Geometry.cs +++ b/Assets/Scripts/Tools/SDF/Implement/Implement.Geometry.cs @@ -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) { @@ -105,10 +106,6 @@ public static void GenerateMeshObject(in SDF.ShapeType shape, in UE.GameObject t var meshFilter = createdObject.AddComponent(); meshFilter.sharedMesh = mesh; - - var meshRenderer = createdObject.AddComponent(); - meshRenderer.sharedMaterial = SDF2Unity.Material.Create(mesh.name); - meshRenderer.allowOcclusionWhenDynamic = true; } createdObject.transform.SetParent(targetParentObject.transform, false); diff --git a/Assets/Scripts/Tools/SDF/Implement/Implement.Material.Ogre.cs b/Assets/Scripts/Tools/SDF/Implement/Implement.Material.Ogre.cs new file mode 100644 index 00000000..ca35abf8 --- /dev/null +++ b/Assets/Scripts/Tools/SDF/Implement/Implement.Material.Ogre.cs @@ -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 uris, in string targetFileName) + { + var ext = Path.GetExtension(targetFileName); + + foreach (var currentDir in uris) + { + var subdirectoryEntries = Directory.GetDirectories(currentDir); + var subDirectories = new List(); + 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 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 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 textrueUnits, + UE.Material material, + in List 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 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; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Tools/SDF/Implement/Implement.Material.Ogre.cs.meta b/Assets/Scripts/Tools/SDF/Implement/Implement.Material.Ogre.cs.meta new file mode 100644 index 00000000..7664b00d --- /dev/null +++ b/Assets/Scripts/Tools/SDF/Implement/Implement.Material.Ogre.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c6eeee00d5f3338f5bb6dda50e025939 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Tools/SDF/Implement/Implement.Material.cs b/Assets/Scripts/Tools/SDF/Implement/Implement.Material.cs new file mode 100644 index 00000000..f6f124b9 --- /dev/null +++ b/Assets/Scripts/Tools/SDF/Implement/Implement.Material.cs @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2020 LG Electronics Inc. + * + * SPDX-License-Identifier: MIT + */ + +using System.Collections.Generic; +using System.Linq; +using System.IO; +using UE = UnityEngine; +using Debug = UnityEngine.Debug; + +namespace SDF +{ + namespace Implement + { + public partial class Material + { + public static void Apply(in SDF.Material sdfMaterial, UE.Renderer renderer) + { + foreach (var material in renderer.materials) + { + if (sdfMaterial.ambient != null) + { + UE.Debug.Log(material.name + ": ambient is not support. " + SDF2Unity.Color(sdfMaterial.ambient)); + } + + if (sdfMaterial.diffuse != null) + { + SDF2Unity.Material.SetBaseColor(material, SDF2Unity.Color(sdfMaterial.diffuse)); + } + + if (sdfMaterial.emissive != null) + { + SDF2Unity.Material.SetEmission(material, SDF2Unity.Color(sdfMaterial.emissive)); + } + + if (sdfMaterial.specular != null) + { + SDF2Unity.Material.SetSpecular(material, SDF2Unity.Color(sdfMaterial.specular)); + // UE.Debug.Log("ImportMaterial HasColorSpecular " + material.GetColor("_SpecColor")); + } + } + + // apply material script + if (sdfMaterial.script != null) + { + // Name of material from an installed script file. + // This will override the color element if the script exists. + var scriptAppliedMaterials = ApplyScript(sdfMaterial.script, renderer.materials); + renderer.materials = scriptAppliedMaterials; + + if (sdfMaterial.script.name.ToLower().Contains("tree")) + { + foreach (var material in renderer.materials) + { + SDF2Unity.Material.ConvertToSpeedTree(material); + } + } + } + } + + public static UE.Material ApplyScript(in SDF.Material.Script script, in UE.Material baseMasterial) + { + var materials = ApplyScript(script, new UE.Material[] { baseMasterial }); + return materials[0]; + } + + public static UE.Material[] ApplyScript(in SDF.Material.Script script, in UE.Material[] baseMaterials) + { + var targetMaterialName = script.name; + var targetMaterialFilepath = FindMaterialFilepathAndUpdateURIs(script.uri, out var texturesPath); + + var outputMaterials = baseMaterials; + + if (string.IsNullOrEmpty(targetMaterialFilepath) == false) + { + var ogreMaterial = OgreMaterial.Parse(targetMaterialFilepath, targetMaterialName); + if (ogreMaterial != null) + { + // UE.Debug.Log($"Found: '{ogreMaterial.name}' material, techniques: {ogreMaterial.techniques.Count}"); + outputMaterials = Ogre.ApplyMaterial(ogreMaterial, baseMaterials, texturesPath); + } + } + + return outputMaterials; + } + + private static string FindMaterialFilepathAndUpdateURIs(in List scriptUris, out List texturesPath) + { + texturesPath = new List(); + var targetMaterialFilepath = string.Empty; + + foreach (var uri in scriptUris) + { + // Debug.Log(uri); + if (uri.EndsWith(".material") && File.Exists(uri)) + { + targetMaterialFilepath = uri; + var targetDir = Directory.GetParent(uri).Parent.ToString(); + texturesPath.Add(targetDir); + } + // find *.material file in folder + else if (Directory.Exists(uri)) + { + var files = Directory.GetFiles(uri); + foreach (var file in files) + { + if (file.EndsWith(".material")) + { + targetMaterialFilepath = file; + // Console.Write(file); + } + } + + texturesPath.Add(uri); + } + } + + return targetMaterialFilepath; + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Tools/SDF/Implement/Implement.Material.cs.meta b/Assets/Scripts/Tools/SDF/Implement/Implement.Material.cs.meta new file mode 100644 index 00000000..2a988851 --- /dev/null +++ b/Assets/Scripts/Tools/SDF/Implement/Implement.Material.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 37c885daf92efdbeead9f3826575840b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Tools/SDF/Implement/Implement.Road.cs b/Assets/Scripts/Tools/SDF/Implement/Implement.Road.cs index eeb3e718..81d89ed5 100644 --- a/Assets/Scripts/Tools/SDF/Implement/Implement.Road.cs +++ b/Assets/Scripts/Tools/SDF/Implement/Implement.Road.cs @@ -44,7 +44,7 @@ public static UE.GameObject Generate(in SDF.World.Road road) var material = SDF2Unity.Material.Create(road.Name + "_Material"); - SDF.Implement.Visual.ApplyMaterial(road.material.script, material); + material = Material.ApplyScript(road.material.script, material); var roadGenerator = newRoadObject.AddComponent(); roadGenerator.SdfMaterial = road.material; diff --git a/Assets/Scripts/Tools/SDF/Implement/Implement.Visual.cs b/Assets/Scripts/Tools/SDF/Implement/Implement.Visual.cs index f2748154..588ab48e 100644 --- a/Assets/Scripts/Tools/SDF/Implement/Implement.Visual.cs +++ b/Assets/Scripts/Tools/SDF/Implement/Implement.Visual.cs @@ -7,7 +7,6 @@ using System.Collections.Generic; using System.Linq; using System.IO; -using System; using UE = UnityEngine; using Debug = UnityEngine.Debug; @@ -106,222 +105,6 @@ var optimizationTarget OptimizeMesh(optimizationTarget); } } - - private static string FindFile(in List uris, in string targetFileName) - { - var ext = Path.GetExtension(targetFileName); - - foreach (var currentDir in uris) - { - var subdirectoryEntries = Directory.GetDirectories(currentDir); - var subDirectories = new List(); - 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 ApplyOgreVertexColour(in OgreMaterial.Pass pass, UE.Material material) - { - if (pass.properties.ContainsKey("diffuse")) - { - var diffuse = pass.properties["diffuse"].Trim(); - SDF2Unity.Material.SetBaseColor(material, SDF2Unity.Color(diffuse)); - } - - if (pass.properties.ContainsKey("emissive")) - { - var emissive = pass.properties["emissive"].Trim(); - SDF2Unity.Material.SetEmission(material, SDF2Unity.Color(emissive)); - } - - if (pass.properties.ContainsKey("specular")) - { - var specular = pass.properties["specular"].Trim(); - - var tmp = specular.Split(' '); - 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)); - } - } - - private static void ApplyOgreTextureUnits(in OgreMaterial.Pass pass, UE.Material material, in List uris) - { - foreach (var textureunitEntry in pass.textureUnits) - { - var textureunit = textureunitEntry.Value; - - // UE.Debug.Log($" TextureUnit: {pass.textureUnits.IndexOf(textureunit)}"); - // 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; - } - - var texture = MeshLoader.GetTexture(textureFilePath); - if (texture != null) - { - if (textureUnitProps.ContainsKey("filtering")) - { - var textureFiltering = textureUnitProps["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 (textureUnitProps.ContainsKey("max_anisotropy")) - { - var textureAnisotropy = textureUnitProps["max_anisotropy"]; - texture.anisoLevel = Convert.ToInt32(textureAnisotropy); - } - - if (textureUnitProps.ContainsKey("scale")) - { - var scaleSet = textureUnitProps["scale"]; - var tileScale = SDF2Unity.Scale(scaleSet); - - // TODO: Check texture tile scaling - tileScale.x = 1 / tileScale.x; - tileScale.y = 1 / tileScale.y; - - material.SetTextureScale("_BaseMap", tileScale); - } - - material.SetTexture("_BaseMap", texture); - } - - return; - } - } - - private static void ApplyOgreMaterial(in OgreMaterial.Material ogreMaterial, UE.Material material, in List uris) - { - if (ogreMaterial.hasReceiveShadows) - { - material.SetFloat("_ReceiveShadows", ogreMaterial.receiveShadows ? 1f : 0); - } - - foreach (var techEntry in ogreMaterial.techniques) - { - var technique = techEntry.Value; - - foreach (var passEntry in technique.passes) - { - var pass = passEntry.Value; - - // UE.Debug.Log($"Technique: {technique.passes.IndexOf(pass)}"); - // foreach (var kvp in pass.properties) - // { - // UE.Debug.Log($" Pass: {kvp.Key}: {kvp.Value}"); - // } - - ApplyOgreVertexColour(pass, material); - - ApplyOgreTextureUnits(pass, material, uris); - } - } - } - - public static void ApplyMaterial(in SDF.Material.Script script, UE.Material targetMaterial) - { - var targetMaterialName = script.name; - var texturesPath = new List(); - var targetMaterialFilepath = string.Empty; - - foreach (var uri in script.uri) - { - // Debug.Log(uri); - if (uri.EndsWith(".material") && File.Exists(uri)) - { - targetMaterialFilepath = uri; - var targetDir = Directory.GetParent(uri).Parent.ToString(); - texturesPath.Add(targetDir); - } - // find *.material file in folder - else if (Directory.Exists(uri)) - { - var files = Directory.GetFiles(uri); - foreach (var file in files) - { - if (file.EndsWith(".material")) - { - targetMaterialFilepath = file; - // Console.Write(file); - } - } - - texturesPath.Add(uri); - } - } - - if (string.IsNullOrEmpty(targetMaterialFilepath) == false) - { - var ogreMaterial = OgreMaterial.Parse(targetMaterialFilepath, targetMaterialName); - if (ogreMaterial != null) - { - // UE.Debug.Log($"Found: '{ogreMaterial.name}' material, techniques: {ogreMaterial.techniques.Count}"); - ApplyOgreMaterial(ogreMaterial, targetMaterial, texturesPath); - } - } - } } } } \ No newline at end of file diff --git a/Assets/Scripts/Tools/SDF/Import/Import.Base.cs b/Assets/Scripts/Tools/SDF/Import/Import.Base.cs index a1f27ca9..661d53dc 100644 --- a/Assets/Scripts/Tools/SDF/Import/Import.Base.cs +++ b/Assets/Scripts/Tools/SDF/Import/Import.Base.cs @@ -25,11 +25,11 @@ private void ImportVisuals(IReadOnlyList items, in Object parentObject) ImportGeometry(item.GetGeometry(), createdObject); - ImportMaterial(item.GetMaterial(), createdObject); - ImportPlugins(item.GetPlugins(), createdObject); AfterImportVisual(item, createdObject); + + ImportMaterial(item.GetMaterial(), createdObject); } } @@ -128,7 +128,7 @@ protected void ImportLights(IReadOnlyList items) } } - public IEnumerator StartImport(World world) + public IEnumerator Start(World world) { // Console.WriteLine("Import Models({0})/Links/Joints", world.GetModels().Count); jointObjectList.Clear(); @@ -148,7 +148,7 @@ public IEnumerator StartImport(World world) ImportActors(world.GetActors()); } - public IEnumerator StartImport(Model model) + public IEnumerator Start(Model model) { jointObjectList.Clear(); var tempModels = new List() { model }; diff --git a/Assets/Scripts/Tools/SDF/Import/Import.Collision.cs b/Assets/Scripts/Tools/SDF/Import/Import.Collision.cs index 5de7025d..6671c73f 100644 --- a/Assets/Scripts/Tools/SDF/Import/Import.Collision.cs +++ b/Assets/Scripts/Tools/SDF/Import/Import.Collision.cs @@ -44,23 +44,26 @@ protected override void AfterImportCollision(in SDF.Collision collision, in Syst Implement.Collision.Make(geometryObject); var shape = collision.GetGeometry().GetShape(); - var shapeType = shape.GetType(); + if (shape != null) + { + var shapeType = shape.GetType(); - var existingMeshCollider = geometryObject.GetComponent(); + var existingMeshCollider = geometryObject.GetComponent(); - if (shapeType.Equals(typeof(Plane))) - { - collisionObject.layer = Implement.Collision.PlaneLayerIndex; - existingMeshCollider.convex = false; - } - else - { - if (EnhanceCollisionPerformance(shapeType, shape, existingMeshCollider)) + if (shapeType.Equals(typeof(Plane))) + { + collisionObject.layer = Implement.Collision.PlaneLayerIndex; + existingMeshCollider.convex = false; + } + else { - var meshColliders = geometryObject.GetComponentsInChildren(); - for (var index = 0; index < meshColliders.Length; index++) + if (EnhanceCollisionPerformance(shapeType, shape, existingMeshCollider)) { - UE.GameObject.Destroy(meshColliders[index]); + var meshColliders = geometryObject.GetComponentsInChildren(); + for (var index = 0; index < meshColliders.Length; index++) + { + UE.GameObject.Destroy(meshColliders[index]); + } } } } diff --git a/Assets/Scripts/Tools/SDF/Import/Import.Material.cs b/Assets/Scripts/Tools/SDF/Import/Import.Material.cs index 0fb234f4..14a3d03a 100644 --- a/Assets/Scripts/Tools/SDF/Import/Import.Material.cs +++ b/Assets/Scripts/Tools/SDF/Import/Import.Material.cs @@ -24,42 +24,7 @@ protected override void ImportMaterial(in SDF.Material sdfMaterial, in System.Ob var meshRenderers = targetObject.GetComponentsInChildren(true); foreach (var renderer in meshRenderers) { - foreach (var material in renderer.materials) - { - if (sdfMaterial.ambient != null) - { - UE.Debug.Log(material.name + ": ambient is not support. " + SDF2Unity.Color(sdfMaterial.ambient)); - } - - if (sdfMaterial.diffuse != null) - { - SDF2Unity.Material.SetBaseColor(material, SDF2Unity.Color(sdfMaterial.diffuse)); - } - - if (sdfMaterial.emissive != null) - { - SDF2Unity.Material.SetEmission(material, SDF2Unity.Color(sdfMaterial.emissive)); - } - - if (sdfMaterial.specular != null) - { - SDF2Unity.Material.SetSpecular(material, SDF2Unity.Color(sdfMaterial.specular)); - // UE.Debug.Log("ImportMaterial HasColorSpecular " + material.GetColor("_SpecColor")); - } - - // apply material script - if (sdfMaterial.script != null) - { - // Name of material from an installed script file. - // This will override the color element if the script exists. - Implement.Visual.ApplyMaterial(sdfMaterial.script, material); - - if (sdfMaterial.script.name.ToLower().Contains("tree")) - { - SDF2Unity.Material.ConvertToSpeedTree(material); - } - } - } + Implement.Material.Apply(sdfMaterial, renderer); // Turn off high-loading features in renderer as a performance tunig renderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off; diff --git a/Assets/Scripts/Tools/SDF/Import/Import.Visual.cs b/Assets/Scripts/Tools/SDF/Import/Import.Visual.cs index adefa746..e17031cf 100644 --- a/Assets/Scripts/Tools/SDF/Import/Import.Visual.cs +++ b/Assets/Scripts/Tools/SDF/Import/Import.Visual.cs @@ -52,12 +52,9 @@ protected override void AfterImportVisual(in SDF.Visual visual, in System.Object return; } - // remove all colliders - var colliders = visualObject.GetComponentsInChildren(); - foreach (var collider in colliders) - { - UE.GameObject.Destroy(collider); - } + RemoveColliders(visualObject); + + AddRenderes(visualObject); if (EnableOptimization) { @@ -80,6 +77,32 @@ protected override void AfterImportVisual(in SDF.Visual visual, in System.Object SceneVisibilityManager.instance.DisablePicking(visualObject, true); #endif } + + private void RemoveColliders(UE.GameObject targetObject) + { + var colliders = targetObject.GetComponentsInChildren(); + foreach (var collider in colliders) + { + UE.Debug.LogWarning($"{collider.name} Collider should not exit. There was collider"); + UE.GameObject.Destroy(collider); + } + } + + private void AddRenderes(UE.GameObject targetObject) + { + var meshFilters = targetObject.GetComponentsInChildren(); + foreach (var meshFilter in meshFilters) + { + var meshRenderer = meshFilter.gameObject.GetComponent(); + if (meshRenderer == null) + { + meshRenderer = meshFilter.gameObject.AddComponent(); + meshRenderer.materials = new UE.Material[] { SDF2Unity.Material.Create(meshFilter.name + "_material") }; + meshRenderer.allowOcclusionWhenDynamic = true; + meshRenderer.receiveShadows = true; + } + } + } } } } \ No newline at end of file diff --git a/Assets/Scripts/Tools/SDF/Parser/Color.cs b/Assets/Scripts/Tools/SDF/Parser/Color.cs index c72d3a1d..28559be7 100644 --- a/Assets/Scripts/Tools/SDF/Parser/Color.cs +++ b/Assets/Scripts/Tools/SDF/Parser/Color.cs @@ -22,7 +22,7 @@ public void FromString(string value) value = value.Trim(); - var tmp = value.Split(' '); + var tmp = value.Replace('\t', ' ').Split(' ', StringSplitOptions.RemoveEmptyEntries); if (tmp.Length < 3) return; diff --git a/Assets/Scripts/Tools/SDF/Parser/Entity.cs b/Assets/Scripts/Tools/SDF/Parser/Entity.cs index 6cb0e054..0cbfe7c1 100644 --- a/Assets/Scripts/Tools/SDF/Parser/Entity.cs +++ b/Assets/Scripts/Tools/SDF/Parser/Entity.cs @@ -229,7 +229,7 @@ private void ParsePose() // x y z roll pitch yaw value = value.Trim().Replace(" ", " ").Replace(" ", " ").Replace(" ", " "); - var poseStr = value.Split(' '); + var poseStr = value.Replace('\t', ' ').Split(' ', StringSplitOptions.RemoveEmptyEntries); try { diff --git a/Assets/Scripts/Tools/SDF/Parser/Pose.cs b/Assets/Scripts/Tools/SDF/Parser/Pose.cs index 1d6aec58..5b52c406 100644 --- a/Assets/Scripts/Tools/SDF/Parser/Pose.cs +++ b/Assets/Scripts/Tools/SDF/Parser/Pose.cs @@ -76,7 +76,7 @@ public void FromString(in string value) return; } - var tmp = value.Trim().Split(' '); + var tmp = value.Trim().Replace('\t', ' ').Split(' ', System.StringSplitOptions.RemoveEmptyEntries); if (tmp.Length == 6) { _pos.Set(tmp[0], tmp[1], tmp[2]); diff --git a/Assets/Scripts/Tools/SDF/Parser/Quaternion.cs b/Assets/Scripts/Tools/SDF/Parser/Quaternion.cs index afb3d623..d259cab2 100644 --- a/Assets/Scripts/Tools/SDF/Parser/Quaternion.cs +++ b/Assets/Scripts/Tools/SDF/Parser/Quaternion.cs @@ -257,7 +257,7 @@ public void FromString(in string value) return; } - var tmp = value.Trim().Split(' '); + var tmp = value.Trim().Replace('\t', ' ').Split(' ', StringSplitOptions.RemoveEmptyEntries); if (tmp.Length == 3) { diff --git a/Assets/Scripts/Tools/SDF/Parser/SensorTypes.cs b/Assets/Scripts/Tools/SDF/Parser/SensorTypes.cs index dceeb876..0938158c 100644 --- a/Assets/Scripts/Tools/SDF/Parser/SensorTypes.cs +++ b/Assets/Scripts/Tools/SDF/Parser/SensorTypes.cs @@ -87,7 +87,7 @@ public void ParsePose(in string poseString, in string relativeTo = "") if (!string.IsNullOrEmpty(poseString)) { // x y z roll pitch yaw - var poseInfo = poseString.Split(' '); + var poseInfo = poseString.Replace('\t', ' ').Split(' ', System.StringSplitOptions.RemoveEmptyEntries); pose.Pos.Set(poseInfo[0], poseInfo[1], poseInfo[2]); pose.Rot.Set(poseInfo[3], poseInfo[4], poseInfo[5]); diff --git a/Assets/Scripts/Tools/SDF/Parser/Vector.cs b/Assets/Scripts/Tools/SDF/Parser/Vector.cs index 0c2b5cf2..9296c955 100644 --- a/Assets/Scripts/Tools/SDF/Parser/Vector.cs +++ b/Assets/Scripts/Tools/SDF/Parser/Vector.cs @@ -92,7 +92,7 @@ public void FromString(in string value) return; } - var tmp = value.Trim().Split(' '); + var tmp = value.Trim().Replace('\t', ' ').Split(' ', StringSplitOptions.RemoveEmptyEntries); if (tmp.Length == 2) { Set(tmp[0], tmp[1]); @@ -173,7 +173,7 @@ public void Set(in string x, in string y, in string z) return; } - var tmp = value.Trim().Split(' '); + var tmp = value.Trim().Replace('\t', ' ').Split(' ', StringSplitOptions.RemoveEmptyEntries); if (tmp.Length == 3) { Set(tmp[0], tmp[1], tmp[2]); diff --git a/Assets/Scripts/Tools/SDF/Util/OgreMaterial.cs b/Assets/Scripts/Tools/SDF/Util/OgreMaterial.cs index fec2b3ee..e69f3143 100644 --- a/Assets/Scripts/Tools/SDF/Util/OgreMaterial.cs +++ b/Assets/Scripts/Tools/SDF/Util/OgreMaterial.cs @@ -112,7 +112,7 @@ public static Material Parse(string filePath, string targetMaterialName) } else if (key == "technique" && propertyLevel == PropertyLevel.MATERIAL) { - var techName = (parts.Length > 1) ? parts[1] : string.Empty; + var techName = (string.IsNullOrEmpty(value))? (key + material.techniques.Count) : value; material.techniques[techName] = new Technique(); targetTechName = techName; @@ -125,13 +125,14 @@ public static Material Parse(string filePath, string targetMaterialName) // Debug.Log(key + " => " + value); if (material.techniques.Count == 0) { - // Debug.Log("!! Missing technique"); + Debug.LogWarning("!! Missing technique"); break; } - var passName = (parts.Length > 1) ? parts[1] : string.Empty; - var targetTechnique = material.techniques[targetTechName]; + + var passName = (string.IsNullOrEmpty(value))? (key + targetTechnique.passes.Count) : value; + if (!targetTechnique.passes.ContainsKey(passName)) targetTechnique.passes[passName] = new Pass(); @@ -144,11 +145,11 @@ public static Material Parse(string filePath, string targetMaterialName) { if (material.techniques[targetTechName].passes.Count == 0) { - // Debug.Log("!! Missing pass"); + Debug.LogWarning("!! Missing pass"); break; } - var textureUnitName = (parts.Length > 1) ? parts[1] : string.Empty; + var textureUnitName = (parts.Length > 1) ? value : string.Empty; var targetPass = material.techniques[targetTechName].passes[targetPassName]; diff --git a/Assets/Scripts/Tools/SDF/Util/SDF2Unity.Material.cs b/Assets/Scripts/Tools/SDF/Util/SDF2Unity.Material.cs index aa33682f..3951d75a 100644 --- a/Assets/Scripts/Tools/SDF/Util/SDF2Unity.Material.cs +++ b/Assets/Scripts/Tools/SDF/Util/SDF2Unity.Material.cs @@ -48,7 +48,7 @@ public static UE.Material Create(in string materialName = "") return newMaterial; } - private static void SetTransparent(UE.Material target) + public static void SetTransparent(UE.Material target) { target.SetOverrideTag("RenderType", "Transparent"); target.SetFloat("_Surface", 1); // set to transparent @@ -62,7 +62,7 @@ private static void SetTransparent(UE.Material target) target.renderQueue = (int)RenderQueue.Transparent; } - private static void SetOpaque(UE.Material target) + public static void SetOpaque(UE.Material target) { target.SetOverrideTag("RenderType", "Opaque"); target.SetFloat("_Surface", 0); // set to opaque diff --git a/Assets/Scripts/Tools/SDF/Util/SDF2Unity.cs b/Assets/Scripts/Tools/SDF/Util/SDF2Unity.cs index 5e5e2088..f56c7e94 100644 --- a/Assets/Scripts/Tools/SDF/Util/SDF2Unity.cs +++ b/Assets/Scripts/Tools/SDF/Util/SDF2Unity.cs @@ -93,6 +93,16 @@ public static Vector3 Scale(in double x, in double y, in double z) return scaleVector; } + public static Vector2 Size(in SDF.Vector2 value) + { + return new Vector2((float)value.X, (float)value.Y); + } + + public static Vector2 Point(in SDF.Vector2 value) + { + return new Vector2((float)value.Y, (float)value.X); + } + public static Vector3 Normal(in SDF.Vector3 value) { return Position(value); diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 866bb0b0..35edbf39 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -139,7 +139,7 @@ PlayerSettings: loadStoreDebugModeEnabled: 0 visionOSBundleVersion: 1.0 tvOSBundleVersion: 1.0 - bundleVersion: 4.5.2 + bundleVersion: 4.5.3 preloadedAssets: [] metroInputSource: 0 wsaTransparentSwapchain: 0 diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index 9efb20c7..d971767b 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2022.3.27f1 -m_EditorVersionWithRevision: 2022.3.27f1 (73effa14754f) +m_EditorVersion: 2022.3.28f1 +m_EditorVersionWithRevision: 2022.3.28f1 (6bae5ce6b222)