public DuMixNodePlane Convert3DPlane(DuMixCMPTPlane item) { var dumixNode = new DuMixNodePlane(); dumixNode.name = item.gameObject.name; // convert transform var dPosition = CoordinateUtil.Convert3DPosition(item.gameObject.transform.localPosition); dumixNode.position = string.Format("{0},{1},{2}", dPosition.x, dPosition.y, dPosition.z); var dEuler = CoordinateUtil.Convert3DEuler(item.gameObject.transform.localEulerAngles); dumixNode.rotation = string.Format("{0},{1},{2}", dEuler.x, dEuler.y, dEuler.z); var dScale = item.gameObject.transform.localScale * 10; dumixNode.scale = string.Format("{0},{1},{2}", dScale.x, dScale.y, dScale.z); dumixNode.material.common.textureList[0].textureName = "res/" + item.resPath; // copy image var srcPath = DuMixGlobalStringDefs.ResourcePath() + item.resPath; var desPath = DuMixGlobalStringDefs.BuildPath() + "ar/res/" + item.resPath; if (File.Exists(desPath)) { File.Delete(desPath); } var desDirPath = Path.GetDirectoryName(desPath); Debug.Log(desDirPath); if (!Directory.Exists(desDirPath)) { Directory.CreateDirectory(desDirPath); } FileUtil.CopyFileOrDirectory(srcPath, desPath); return(dumixNode); }
// 转换UI坐标系下的平面节点 public DuMixNodeUIPlane ConvertUIPlane(DuMixCMPTUIObject item) { var canvasSize = new Vector2(720, 1280); // TODO: Handle Rotation var nodeName = item.gameObject.name; var position = CoordinateUtil.ConvertUIPosition(item.gameObject.transform.localPosition, canvasSize); var scale = CoordinateUtil.ConvertUIScale(item.gameObject.transform.localScale, canvasSize); // json part var duMixNode = new DuMixNodeUIPlane(); duMixNode.name = nodeName; duMixNode.material.common.textureList[0].textureName = "res/" + item.resPath; duMixNode.hudDisplay.marginTop = position.y; duMixNode.hudDisplay.marginLeft = position.x; duMixNode.hudDisplay.screenRatio.screenWidthRatio = scale.x; duMixNode.hudDisplay.screenRatio.screenHeightRatio = scale.y; // resource part var srcPath = DuMixGlobalStringDefs.ResourcePath() + item.resPath; var desPath = DuMixGlobalStringDefs.BuildPath() + "ar/res/" + item.resPath; if (File.Exists(desPath)) { File.Delete(desPath); } var desDirPath = Path.GetDirectoryName(desPath); Debug.Log(desDirPath); if (!Directory.Exists(desDirPath)) // create directory { Directory.CreateDirectory(desDirPath); } FileUtil.CopyFileOrDirectory(srcPath, desPath); return(duMixNode); }
// 转换3D坐标系下的模型节点 public DuMixNodeGLTF Convert3DModel(DuMixCMPTModel item) { var duMixNode = new DuMixNodeGLTF(); // transform var dPosition = CoordinateUtil.Convert3DPosition(item.transform.localPosition); var dEuler = CoordinateUtil.Convert3DEuler(item.transform.localEulerAngles); var dScale = item.transform.localScale; duMixNode.position = string.Format("{0},{1},{2}", dPosition.x, dPosition.y, dPosition.z); duMixNode.rotation = string.Format("{0},{1},{2}", dEuler.x, dEuler.y, dEuler.z); duMixNode.scale = string.Format("{0},{1},{2}", dScale.x, dScale.y, dScale.z); // base duMixNode.name = item.name; // meshFileName var resPath = item.resPath; var fbxName = Path.GetFileName(resPath).Split('.')[0]; Debug.Log(fbxName); var fbxPath = DuMixGlobalStringDefs.ResourcePath() + resPath; var gltfGenPath = DuMixGlobalStringDefs.BuildPath() + "ar/res/gltf/" + fbxName; if (File.Exists(gltfGenPath)) { Debug.Log("destination folder path exits, would remove it"); File.Delete(gltfGenPath); } var binPath = DuMixGlobalStringDefs.ToolPath() + "FBX2glTF"; var argument = string.Format("-i {0} -o {1}", fbxPath, gltfGenPath); var proc = new System.Diagnostics.ProcessStartInfo(); proc.FileName = binPath; proc.Arguments = argument; var process = System.Diagnostics.Process.Start(proc); process.WaitForExit(); process.Close(); Debug.Log("FBX-" + fbxName + ": Converted"); var gltfPath = "res/gltf/" + fbxName + "_out/" + fbxName + ".gltf"; duMixNode.meshFileName = gltfPath; return(duMixNode); }
public DuMixNodeVideo Convert3DVideo(DuMixCMPT3DVideo item) { var dumixNode = new DuMixNodeVideo(); dumixNode.name = item.gameObject.name; // convert transform var dPosition = CoordinateUtil.Convert3DPosition(item.gameObject.transform.localPosition); dumixNode.position = string.Format("{0},{1},{2}", dPosition.x, dPosition.y, dPosition.z); var dEuler = CoordinateUtil.Convert3DEuler(item.gameObject.transform.localEulerAngles); dumixNode.rotation = string.Format("{0},{1},{2}", dEuler.x, dEuler.y, dEuler.z); var dScale = item.gameObject.transform.localScale * 10; dumixNode.scale = string.Format("{0},{1},{2}", dScale.x, dScale.y, dScale.z); dumixNode.material.common.uvUnwrapedTextureName = "res/" + item.resPath; dumixNode.material.android.uvUnwrapedTextureName = "res/" + item.resPath; if (item.videoType == DuMixCMPT3DVideo.VideoType.commonVideo) { dumixNode.material.common.defaultShaderName = "ordinaryVideoShader"; dumixNode.material.android.defaultShaderName = "androidOrdinaryVideoShader"; } // resource part var srcPath = DuMixGlobalStringDefs.ResourcePath() + item.resPath; var desPath = DuMixGlobalStringDefs.BuildPath() + "ar/res/" + item.resPath; if (File.Exists(desPath)) { File.Delete(desPath); } var desDirPath = Path.GetDirectoryName(desPath); Debug.Log(desDirPath); if (!Directory.Exists(desDirPath)) { Directory.CreateDirectory(desDirPath); } FileUtil.CopyFileOrDirectory(srcPath, desPath); return(dumixNode); }
// 转换UI坐标系下的视频节点 public DuMixNodeUIVideo ConvertUIVideo(DuMixCMPTUIVideo item) { var canvasSize = new Vector2(720, 1280); // TODO: Handle Rotation var nodeName = item.gameObject.name; var position = CoordinateUtil.ConvertUIPosition(item.gameObject.transform.localPosition, canvasSize); var scale = CoordinateUtil.ConvertUIScale(item.gameObject.transform.localScale, canvasSize); // json part var duMixNode = new DuMixNodeUIVideo(); duMixNode.name = nodeName; duMixNode.material.common.uvUnwrapedTextureName = "res/" + item.resPath; duMixNode.material.android.uvUnwrapedTextureName = "res/" + item.resPath; if (item.videoType == DuMixCMPTUIVideo.VideoType.commonVideo) { duMixNode.material.common.defaultShaderName = "ordinaryVideoShader"; duMixNode.material.android.defaultShaderName = "androidOrdinaryVideoShader"; } duMixNode.hudDisplay.marginTop = position.y; duMixNode.hudDisplay.marginLeft = position.x; duMixNode.hudDisplay.screenRatio.screenWidthRatio = scale.x; duMixNode.hudDisplay.screenRatio.screenHeightRatio = scale.y; duMixNode.rotation = "90,0,0"; //// resource part var srcPath = DuMixGlobalStringDefs.ResourcePath() + item.resPath; var desPath = DuMixGlobalStringDefs.BuildPath() + "ar/res/" + item.resPath; if (File.Exists(desPath)) { File.Delete(desPath); } var desDirPath = Path.GetDirectoryName(desPath); Debug.Log(desDirPath); if (!Directory.Exists(desDirPath)) { // create directory Directory.CreateDirectory(desDirPath); } FileUtil.CopyFileOrDirectory(srcPath, desPath); return(duMixNode); }
public void Run() { Debug.Log("Unit2DuMix 开始"); // Check AR Type var rootScene = Object.FindObjectOfType <DuMixCMPTScene>(); if (rootScene == null) { Debug.LogError("找不到有效的Scene"); return; } if (rootScene.GetType() == typeof(DuMixCMPTSlamScene)) { Debug.Log("AR type: SLAM"); this.caseType = DuMixCaseType.SLAM; } else if (rootScene.GetType() == typeof(DuMixCMPTTrackScene)) { Debug.Log("AR type: TRACK"); this.caseType = DuMixCaseType.TRACK2D; } else if (rootScene.GetType() == typeof(DuMixCMPTImuScene)) { Debug.Log("AR type: IMU"); this.caseType = DuMixCaseType.IMU; } // setup build path var templatePath = DuMixGlobalStringDefs.TemplatePath() + "ar"; var buildPath = DuMixGlobalStringDefs.BuildPath() + "ar"; if (!Directory.Exists(DuMixGlobalStringDefs.BuildPath())) { Directory.CreateDirectory(DuMixGlobalStringDefs.BuildPath()); } if (!Directory.Exists(templatePath)) { Debug.LogError("DuMixARCase/Templates/ar 文件夹不存在"); } if (Directory.Exists(buildPath)) { FileUtil.DeleteFileOrDirectory(buildPath); } FileUtil.CopyFileOrDirectory(templatePath, buildPath); // -- 输出 ar/res/main.json DuMixNodeJSONFrame frame = new DuMixNodeJSONFrame(); DuMixNodeCamera mainCamera = new DuMixNodeCamera(); frame.nodeList.Add(mainCamera); frame = this.FillSceneGraph(frame, this.caseType); string json = JsonConvert.SerializeObject(frame); var mainJsonPath = DuMixGlobalStringDefs.BuildPath() + "ar/res/main.json"; if (File.Exists(mainJsonPath)) { File.Delete(mainJsonPath); } FileStream fileStream = new FileStream(mainJsonPath, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter streamWriter = new StreamWriter(fileStream); streamWriter.WriteLine(json); streamWriter.Close(); streamWriter.Dispose(); fileStream.Close(); fileStream.Dispose(); // -- 输出 ar/targets.json DuMixCaseConfig config = new DuMixCaseConfig(); var targetJsonPath = DuMixGlobalStringDefs.BuildPath() + "ar/targets.json"; if (File.Exists(targetJsonPath)) { File.Delete(targetJsonPath); } fileStream = new FileStream(targetJsonPath, FileMode.OpenOrCreate, FileAccess.Write); streamWriter = new StreamWriter(fileStream); // TODO if (this.caseType == DuMixCaseType.TRACK2D) { // Update target values } json = JsonConvert.SerializeObject(config); streamWriter.WriteLine(json); streamWriter.Close(); streamWriter.Dispose(); fileStream.Close(); fileStream.Dispose(); // -- 输出 ar/default.lua var templateLuaPath = DuMixGlobalStringDefs.TemplatePath() + "default_lua/"; switch (this.caseType) { case DuMixCaseType.IMU: templateLuaPath += "default_imu.lua"; break; case DuMixCaseType.SLAM: templateLuaPath += "default_slam.lua"; break; case DuMixCaseType.TRACK2D: templateLuaPath += "default_track.lua"; break; } var desLuaPath = DuMixGlobalStringDefs.BuildPath() + "ar/default.lua"; if (File.Exists(desLuaPath)) { File.Delete(desLuaPath); } FileUtil.CopyFileOrDirectory(templateLuaPath, desLuaPath); // Copy Audio Resources var audioPlayers = Object.FindObjectsOfType <DuMixCMPTAudio>(); foreach (var audio in audioPlayers) { var src = DuMixGlobalStringDefs.ResourcePath() + audio.resPath; var dest = DuMixGlobalStringDefs.BuildPath() + "ar/res/" + audio.resPath; var desDirPath = Path.GetDirectoryName(dest); if (!Directory.Exists(desDirPath)) { // create directory Directory.CreateDirectory(desDirPath); } FileUtil.CopyFileOrDirectory(src, dest); } // User Scripts var luaFiles = Directory.GetFiles(DuMixGlobalStringDefs.LuaFilePath()); foreach (var luaFile in luaFiles) { var fileName = Path.GetFileName(luaFile); var desPath = DuMixGlobalStringDefs.BuildPath() + "ar/" + fileName; FileUtil.CopyFileOrDirectory(luaFile, desPath); } var luaFileDirs = Directory.GetDirectories(DuMixGlobalStringDefs.LuaFilePath()); foreach (var dir in luaFileDirs) { var dirName = Path.GetFileName(dir); var desPath = DuMixGlobalStringDefs.BuildPath() + "ar/" + dirName; FileUtil.CopyFileOrDirectory(dir, desPath); } // Write to ar/unity.lua var luaBuilder = new LuaConstructor(); luaBuilder.Reset(); var evtObjests = Object.FindObjectsOfType <DuMixCMPTEvent>(); foreach (var evt in evtObjests) { var newLine = evt.TransferToDuMixScript(); luaBuilder.AddScript(newLine); } luaBuilder.End(); var unityLuaPath = DuMixGlobalStringDefs.BuildPath() + "ar/unity.lua"; fileStream = new FileStream(unityLuaPath, FileMode.OpenOrCreate, FileAccess.Write); streamWriter = new StreamWriter(fileStream); streamWriter.WriteLine(luaBuilder.lua); streamWriter.Close(); streamWriter.Dispose(); fileStream.Close(); fileStream.Dispose(); // Send Case Type Message var setCaseTypeMsg = new DuMixRDMessageSetCaseType(); switch (this.caseType) { case DuMixCaseType.IMU: setCaseTypeMsg.caseType = "imu"; break; case DuMixCaseType.SLAM: setCaseTypeMsg.caseType = "slam"; break; case DuMixCaseType.TRACK2D: setCaseTypeMsg.caseType = "track"; break; } var msgJson = JsonConvert.SerializeObject(setCaseTypeMsg); Debug.Log("Unity2DuMix 完成"); // Zip ar string zipPath = DuMixGlobalStringDefs.BuildPath() + "ar.zip"; DuMixZipUtility.Zip(buildPath, zipPath, delegate() { //DuMixARNetwork.Instance.sendString(msgJson); DuMixARNetwork.Instance.sendFile(zipPath); }); }