pmx export support
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: dc65b4eda2291b741939ee543b48572e
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!114 &11400000
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 0}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 0}
|
||||||
|
m_Name: TestData
|
||||||
|
m_EditorClassIdentifier: Assembly-CSharp-Editor::TestData
|
||||||
|
testName: name
|
||||||
|
level: 1
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 444be3d6353840348baf31cc846545e7
|
||||||
|
NativeFormatImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
mainObjectFileID: 11400000
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,14 +1,293 @@
|
|||||||
using System.Collections;
|
using LibMMD.Material;
|
||||||
|
using LibMMD.Model;
|
||||||
|
using LibMMD.Reader;
|
||||||
|
using LibMMD.Writer;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.Serialization;
|
using System.IO;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using static LibMMD.Model.SkinningOperator;
|
||||||
//using UnityGLTF;
|
//using UnityGLTF;
|
||||||
|
|
||||||
|
|
||||||
public class ModelExporter
|
public class ModelExporter
|
||||||
{
|
{
|
||||||
public static void exportModel(string path,GameObject gameobj)
|
static string modelInfo = $"Generated by UmaViewer {Application.version}, Copyright Cygames \nLink : https://github.com/katboi01/UmaViewer";
|
||||||
|
|
||||||
|
public static void ExportModel(UmaContainer container, string path)
|
||||||
{
|
{
|
||||||
//Todo Export PmX
|
container.SetDynamicBoneEnable(false);
|
||||||
|
container.UmaFaceAnimator?.Rebind();
|
||||||
|
container.UmaAnimator?.Rebind();
|
||||||
|
container.EnableEyeTracking = false;
|
||||||
|
container.FaceDrivenKeyTarget?.FacialResetAll();
|
||||||
|
|
||||||
|
var textures = TextureExporter.ExportAllTexture(Path.GetDirectoryName(path), container.gameObject);
|
||||||
|
var model = ReadPMXModel(container.CharaEntry, container, textures);
|
||||||
|
|
||||||
|
FileStream fileStream = new FileStream(path, FileMode.Create);
|
||||||
|
BinaryWriter writer = new BinaryWriter(fileStream);
|
||||||
|
var config = new ModelConfig() { GlobalToonPath = "Toon" };
|
||||||
|
PMXWriter.Write(writer, model, config);
|
||||||
|
|
||||||
|
writer.Close();
|
||||||
|
fileStream.Close();
|
||||||
|
|
||||||
|
fileStream = new FileStream(path, FileMode.Open);
|
||||||
|
BinaryReader reader = new BinaryReader(fileStream);
|
||||||
|
|
||||||
|
new PMXReader().Read(reader, config);
|
||||||
|
|
||||||
|
reader.Close();
|
||||||
|
fileStream.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RawMMDModel ReadPMXModel(CharaEntry entry, UmaContainer container, string[] textures)
|
||||||
|
{
|
||||||
|
RawMMDModel model = new RawMMDModel();
|
||||||
|
model.Name = entry.Name;
|
||||||
|
model.NameEn = entry.GetName();
|
||||||
|
model.Description = model.DescriptionEn = $"{entry.GetName()}\n{modelInfo}";
|
||||||
|
|
||||||
|
//Rean Bones
|
||||||
|
var rootBone = container.transform.Find("Position");
|
||||||
|
List<Transform> bones = new List<Transform>(rootBone.GetComponentsInChildren<Transform>());
|
||||||
|
bones.RemoveAll(o => o.name.Contains("Col_"));
|
||||||
|
|
||||||
|
//Read vertices And triangles
|
||||||
|
List<Renderer> renderers = new List<Renderer>(container.GetComponentsInChildren<Renderer>());
|
||||||
|
List<int> triangles = new List<int>();
|
||||||
|
model.Vertices = ReadVerticesAndTriangles(renderers, bones, ref triangles);
|
||||||
|
model.TriangleIndexes = triangles.ToArray();
|
||||||
|
Debug.LogError($"{model.Vertices.Length} {model.TriangleIndexes.Length}");
|
||||||
|
//Read Texture reference
|
||||||
|
foreach (var texture in textures)
|
||||||
|
{
|
||||||
|
model.TextureList.Add(new MMDTexture(texture));
|
||||||
|
}
|
||||||
|
|
||||||
|
model.Parts = ReadPartMaterials(renderers, model);
|
||||||
|
model.Bones = ReadBones(bones);
|
||||||
|
model.Morphs = new Morph[0];
|
||||||
|
model.Rigidbodies = new MMDRigidBody[0];
|
||||||
|
model.Joints = new MMDJoint[0];
|
||||||
|
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Bone[] ReadBones(List<Transform> bonelist)
|
||||||
|
{
|
||||||
|
List<Bone> pmxbones = new List<Bone>();
|
||||||
|
foreach(var bone in bonelist)
|
||||||
|
{
|
||||||
|
Bone pmxbone = new Bone();
|
||||||
|
pmxbone.Name = pmxbone.NameEn = bone.name;
|
||||||
|
pmxbone.Position = bone.position;
|
||||||
|
pmxbone.ParentIndex = bonelist.IndexOf(bone.parent);
|
||||||
|
pmxbone.TransformLevel = 0;
|
||||||
|
pmxbone.Visible = true;
|
||||||
|
pmxbone.Movable = true;
|
||||||
|
pmxbone.Rotatable = true;
|
||||||
|
pmxbone.Controllable = true;
|
||||||
|
pmxbone.ChildBoneVal = new Bone.ChildBone()
|
||||||
|
{
|
||||||
|
ChildUseId = true,
|
||||||
|
Index = (bone.childCount > 0 ? bonelist.IndexOf(bone.GetChild(0)) : -1)
|
||||||
|
};
|
||||||
|
pmxbones.Add(pmxbone);
|
||||||
|
}
|
||||||
|
return pmxbones.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Part[] ReadPartMaterials(List<Renderer> renderers, RawMMDModel model)
|
||||||
|
{
|
||||||
|
List<Part> parts = new List<Part>();
|
||||||
|
int baseShift = 0;
|
||||||
|
foreach (Renderer renderer in renderers)
|
||||||
|
{
|
||||||
|
Mesh mesh;
|
||||||
|
if (renderer is MeshRenderer mr)
|
||||||
|
{
|
||||||
|
var meshfilter = mr.GetComponent<MeshFilter>();
|
||||||
|
mesh = meshfilter.mesh;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mesh = ((SkinnedMeshRenderer)renderer).sharedMesh;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var material in renderer.sharedMaterials)
|
||||||
|
{
|
||||||
|
var part = new Part();
|
||||||
|
var mat = new MMDMaterial();
|
||||||
|
part.Material = mat;
|
||||||
|
mat.Name = mat.NameEn = material.name;
|
||||||
|
mat.DiffuseColor = Color.white;
|
||||||
|
mat.SpecularColor = Color.clear;
|
||||||
|
mat.AmbientColor = Color.white * 0.5f;
|
||||||
|
mat.Shiness = 5;
|
||||||
|
mat.CastSelfShadow = true;
|
||||||
|
mat.DrawGroundShadow = true;
|
||||||
|
mat.DrawSelfShadow = true;
|
||||||
|
mat.EdgeColor = Color.black;
|
||||||
|
mat.EdgeSize = 0.4f;
|
||||||
|
var tex = model.TextureList.Find(t => t.TexturePath.Contains($"/{material.mainTexture.name}.png"));
|
||||||
|
mat.Texture = (tex ?? model.TextureList[0]);
|
||||||
|
mat.MetaInfo = "";
|
||||||
|
if (mesh.subMeshCount > 0)
|
||||||
|
{
|
||||||
|
var index = new List<Material>(renderer.sharedMaterials).IndexOf(material);
|
||||||
|
part.BaseShift = baseShift + (int)mesh.GetIndexStart(index);
|
||||||
|
part.TriangleIndexNum = (int)mesh.GetIndexCount(index);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
part.BaseShift = baseShift;
|
||||||
|
part.TriangleIndexNum = mesh.triangles.Length;
|
||||||
|
}
|
||||||
|
parts.Add(part);
|
||||||
|
}
|
||||||
|
baseShift += mesh.triangles.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return parts.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Vertex[] ReadVerticesAndTriangles(List<Renderer> renderers, List<Transform> bones, ref List<int> triangleList)
|
||||||
|
{
|
||||||
|
List<Vertex> verticesList = new List<Vertex>();
|
||||||
|
|
||||||
|
foreach (Renderer renderer in renderers)
|
||||||
|
{
|
||||||
|
if (renderer is MeshRenderer mr)
|
||||||
|
{
|
||||||
|
var meshfilter = mr.GetComponent<MeshFilter>();
|
||||||
|
//Cache this data to avoid copying arrays.
|
||||||
|
var mesh = meshfilter.mesh;
|
||||||
|
var vertices = mesh.vertices;
|
||||||
|
var normals = mesh.normals;
|
||||||
|
var uv = mesh.uv;
|
||||||
|
var uv1 = mesh.uv2;
|
||||||
|
var uv2 = mesh.uv3;
|
||||||
|
var colors = mesh.colors;
|
||||||
|
var triangles = mesh.triangles;
|
||||||
|
|
||||||
|
for (int i = 0; i < vertices.Length; i++)
|
||||||
|
{
|
||||||
|
Vertex vertex = new Vertex();
|
||||||
|
vertex.Coordinate = renderer.transform.TransformPoint(vertices[i]);
|
||||||
|
vertex.Normal = normals[i];
|
||||||
|
vertex.UvCoordinate = uv[i];
|
||||||
|
|
||||||
|
vertex.ExtraUvCoordinate = new Vector4[3]
|
||||||
|
{
|
||||||
|
uv1.Length > 0 ? uv1[i] : Vector2.zero,
|
||||||
|
uv2.Length > 0 ? uv2[i] : Vector2.zero,
|
||||||
|
colors.Length > 0 ? colors[i] : Color.clear
|
||||||
|
};
|
||||||
|
|
||||||
|
vertex.SkinningOperator = new SkinningOperator() { Type = SkinningType.SkinningBdef1 };
|
||||||
|
vertex.SkinningOperator.Param = new Bdef1() { BoneId = bones.IndexOf(renderer.transform) };
|
||||||
|
vertex.EdgeScale = 1;
|
||||||
|
verticesList.Add(vertex);
|
||||||
|
}
|
||||||
|
|
||||||
|
int vertexOffset = verticesList.Count;
|
||||||
|
foreach (var triangle in mesh.triangles)
|
||||||
|
{
|
||||||
|
triangleList.Add(triangle + vertexOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (renderer is SkinnedMeshRenderer smr)
|
||||||
|
{
|
||||||
|
var mesh = smr.sharedMesh;
|
||||||
|
var vertices = mesh.vertices;
|
||||||
|
var normals = mesh.normals;
|
||||||
|
var uv = mesh.uv;
|
||||||
|
var uv1 = mesh.uv2;
|
||||||
|
var uv2 = mesh.uv3;
|
||||||
|
var colors = mesh.colors;
|
||||||
|
var weights = mesh.boneWeights;
|
||||||
|
var boneCounts = mesh.GetBonesPerVertex();
|
||||||
|
var skinbone = smr.bones;
|
||||||
|
|
||||||
|
for (int i = 0; i < vertices.Length; i++)
|
||||||
|
{
|
||||||
|
Vertex vertex = new Vertex();
|
||||||
|
vertex.Coordinate = smr.transform.TransformPoint(vertices[i]);
|
||||||
|
vertex.Normal = normals[i];
|
||||||
|
vertex.UvCoordinate = uv[i];
|
||||||
|
|
||||||
|
vertex.ExtraUvCoordinate = new Vector4[3]
|
||||||
|
{
|
||||||
|
uv1.Length > 0 ? uv1[i] : Vector2.zero,
|
||||||
|
uv2.Length > 0 ? uv2[i] : Vector2.zero,
|
||||||
|
colors.Length > 0 ? colors[i] : Color.clear
|
||||||
|
};
|
||||||
|
|
||||||
|
var boneWeight = weights[i];
|
||||||
|
var boneCount = boneCounts[i];
|
||||||
|
|
||||||
|
switch (boneCount)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
vertex.SkinningOperator = new SkinningOperator() { Type = SkinningType.SkinningBdef1 };
|
||||||
|
vertex.SkinningOperator.Param = new Bdef1() { BoneId = GetBoneIndex(bones,renderer.transform) };
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
case 1:
|
||||||
|
vertex.SkinningOperator = new SkinningOperator() { Type = SkinningType.SkinningBdef1 };
|
||||||
|
vertex.SkinningOperator.Param = new Bdef1() { BoneId = GetBoneIndex(bones, skinbone[boneWeight.boneIndex0]) };
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
vertex.SkinningOperator = new SkinningOperator() { Type = SkinningType.SkinningBdef2 };
|
||||||
|
vertex.SkinningOperator.Param = new Bdef2() {
|
||||||
|
BoneId = new int []{
|
||||||
|
GetBoneIndex(bones, skinbone[boneWeight.boneIndex0]),
|
||||||
|
GetBoneIndex(bones, skinbone[boneWeight.boneIndex1]),
|
||||||
|
},
|
||||||
|
BoneWeight = boneWeight.weight0
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
case 4:
|
||||||
|
vertex.SkinningOperator = new SkinningOperator() { Type = SkinningType.SkinningBdef4 };
|
||||||
|
vertex.SkinningOperator.Param = new Bdef4()
|
||||||
|
{
|
||||||
|
BoneId = new int[]{
|
||||||
|
GetBoneIndex(bones, skinbone[boneWeight.boneIndex0]),
|
||||||
|
GetBoneIndex(bones, skinbone[boneWeight.boneIndex1]),
|
||||||
|
GetBoneIndex(bones, skinbone[boneWeight.boneIndex2]),
|
||||||
|
GetBoneIndex(bones, skinbone[boneWeight.boneIndex3]),
|
||||||
|
},
|
||||||
|
BoneWeight = new float[]
|
||||||
|
{
|
||||||
|
boneWeight.weight0,
|
||||||
|
boneWeight.weight1,
|
||||||
|
boneWeight.weight2,
|
||||||
|
boneWeight.weight3
|
||||||
|
}
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
vertex.EdgeScale = 1;
|
||||||
|
verticesList.Add(vertex);
|
||||||
|
|
||||||
|
int vertexOffset = verticesList.Count;
|
||||||
|
foreach (var triangle in mesh.triangles)
|
||||||
|
{
|
||||||
|
triangleList.Add(triangle + vertexOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return verticesList.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int GetBoneIndex(List<Transform> bones, Transform bone)
|
||||||
|
{
|
||||||
|
return bones.Contains(bone) ? bones.IndexOf(bone) : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,36 +27,37 @@ public class TextureExporter
|
|||||||
return readableText;
|
return readableText;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void exportAllTexture(string path, GameObject gameobj)
|
static public string[] ExportAllTexture(string path, GameObject gameobj)
|
||||||
{
|
{
|
||||||
string savePath = path + "/Texture2D/";
|
string savePath = path + "/Texture2D/";
|
||||||
|
List<string> textureNames = new List<string>();
|
||||||
if (!Directory.Exists(savePath))
|
if (!Directory.Exists(savePath))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(savePath);
|
Directory.CreateDirectory(savePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (SkinnedMeshRenderer skin_mesh in gameobj.transform.GetComponentsInChildren<SkinnedMeshRenderer>())
|
foreach (Renderer renderer in gameobj.transform.GetComponentsInChildren<Renderer>())
|
||||||
{
|
{
|
||||||
foreach (Material mat in skin_mesh.sharedMaterials)
|
foreach (Material mat in renderer.sharedMaterials)
|
||||||
{
|
{
|
||||||
//获得材质的所有属性名,如果改属性名对应的是贴图,提取该贴图
|
//获得材质的所有属性名,如果改属性名对应的是贴图,提取该贴图
|
||||||
Shader mat_shader = mat.shader;
|
Shader mat_shader = mat.shader;
|
||||||
int p_num = mat_shader.GetPropertyCount();
|
int p_num = mat_shader.GetPropertyCount();
|
||||||
Debug.Log(p_num);
|
|
||||||
for (int i = 0; i < p_num; i++)
|
for (int i = 0; i < p_num; i++)
|
||||||
{
|
{
|
||||||
string p_name = mat_shader.GetPropertyName(i);
|
string p_name = mat_shader.GetPropertyName(i);
|
||||||
if (mat_shader.GetPropertyType(i) == UnityEngine.Rendering.ShaderPropertyType.Texture)
|
if (mat_shader.GetPropertyType(i) == UnityEngine.Rendering.ShaderPropertyType.Texture)
|
||||||
{
|
{
|
||||||
//Debug.Log("Hello!");
|
|
||||||
Debug.Log(p_name);
|
|
||||||
Texture2D mat_texture = (Texture2D)mat.GetTexture(p_name);
|
Texture2D mat_texture = (Texture2D)mat.GetTexture(p_name);
|
||||||
if (mat_texture)
|
if (mat_texture)
|
||||||
{
|
{
|
||||||
//Debug.Log("There is a Name!");
|
|
||||||
string tex_name = mat_texture.name;
|
string tex_name = mat_texture.name;
|
||||||
string tex_path = savePath + tex_name + ".png";
|
string tex_path = savePath + tex_name + ".png";
|
||||||
|
var dpath = "Texture2D/" + tex_name + ".png";
|
||||||
|
if (!textureNames.Contains(dpath))
|
||||||
|
{
|
||||||
|
textureNames.Add(dpath);
|
||||||
|
}
|
||||||
if (!File.Exists(tex_path))
|
if (!File.Exists(tex_path))
|
||||||
{
|
{
|
||||||
mat_texture = duplicateTexture(mat_texture);
|
mat_texture = duplicateTexture(mat_texture);
|
||||||
@@ -68,42 +69,7 @@ public class TextureExporter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (MeshRenderer mr in gameobj.transform.GetComponentsInChildren<MeshRenderer>())
|
|
||||||
{
|
|
||||||
foreach (Material mat in mr.sharedMaterials)
|
|
||||||
{
|
|
||||||
//获得材质的所有属性名,如果改属性名对应的是贴图,提取该贴图
|
|
||||||
Shader mat_shader = mat.shader;
|
|
||||||
int p_num = mat_shader.GetPropertyCount();
|
|
||||||
Debug.Log(p_num);
|
|
||||||
for (int i = 0; i < p_num; i++)
|
|
||||||
{
|
|
||||||
//Debug.Log(mat_shader.GetPropertyName(i));
|
|
||||||
//Debug.Log(mat_shader.GetPropertyType(i));
|
|
||||||
string p_name = mat_shader.GetPropertyName(i);
|
|
||||||
if (mat_shader.GetPropertyType(i) == UnityEngine.Rendering.ShaderPropertyType.Texture)
|
|
||||||
{
|
|
||||||
//Debug.Log("Hello!");
|
|
||||||
Debug.Log(p_name);
|
|
||||||
Texture2D mat_texture = (Texture2D)mat.GetTexture(p_name);
|
|
||||||
if (mat_texture)
|
|
||||||
{
|
|
||||||
//Debug.Log("There is a Name!");
|
|
||||||
string tex_name = mat_texture.name;
|
|
||||||
string tex_path = savePath + tex_name + ".png";
|
|
||||||
if (!File.Exists(tex_path))
|
|
||||||
{
|
|
||||||
mat_texture = duplicateTexture(mat_texture);
|
|
||||||
byte[] bytes = mat_texture.EncodeToPNG();
|
|
||||||
File.WriteAllBytes(tex_path, bytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Debug.Log("提取成功");
|
Debug.Log("提取成功");
|
||||||
|
return textureNames.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ namespace LibMMD.Model
|
|||||||
public MMDRigidBody[] Rigidbodies { get; set; }
|
public MMDRigidBody[] Rigidbodies { get; set; }
|
||||||
public MMDJoint[] Joints { get; set; }
|
public MMDJoint[] Joints { get; set; }
|
||||||
|
|
||||||
public PmxConfig PmxConfig;
|
|
||||||
public List<MMDTexture> TextureList = new List<MMDTexture>();
|
public List<MMDTexture> TextureList = new List<MMDTexture>();
|
||||||
public List<PMXEntryItem> Entrys = new List<PMXEntryItem>();
|
public List<PMXEntryItem> Entrys = new List<PMXEntryItem>();
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ namespace LibMMD.Reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
var model = new RawMMDModel();
|
var model = new RawMMDModel();
|
||||||
var pmxConfig = model.PmxConfig = ReadPmxConfig(reader, model);
|
var pmxConfig = ReadPmxConfig(reader, model);
|
||||||
ReadModelNameAndDescription(reader, model, pmxConfig);
|
ReadModelNameAndDescription(reader, model, pmxConfig);
|
||||||
ReadVertices(reader, model, pmxConfig);
|
ReadVertices(reader, model, pmxConfig);
|
||||||
ReadTriangles(reader, model, pmxConfig);
|
ReadTriangles(reader, model, pmxConfig);
|
||||||
|
|||||||
@@ -508,7 +508,7 @@ namespace LibMMD.Unity3D
|
|||||||
switch (op.Type)
|
switch (op.Type)
|
||||||
{
|
{
|
||||||
case SkinningOperator.SkinningType.SkinningBdef1:
|
case SkinningOperator.SkinningType.SkinningBdef1:
|
||||||
var b def1 = (SkinningOperator.Bdef1)op.Param;
|
var bdef1 = (SkinningOperator.Bdef1)op.Param;
|
||||||
ret.boneIndex0 = bdef1.BoneId;
|
ret.boneIndex0 = bdef1.BoneId;
|
||||||
ret.weight0 = 1.0f;
|
ret.weight0 = 1.0f;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -37,17 +37,11 @@ namespace LibMMD.Util
|
|||||||
throw new MMDFileParseException("pmx string length is negative");
|
throw new MMDFileParseException("pmx string length is negative");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str.Length > length)
|
if(length > 0)
|
||||||
{
|
{
|
||||||
str = str.Substring(0, length);
|
var bytes = encoding.GetBytes(str);
|
||||||
|
writer.Write(bytes);
|
||||||
}
|
}
|
||||||
else if (str.Length < length)
|
|
||||||
{
|
|
||||||
str = str.PadRight(length, '\0');
|
|
||||||
}
|
|
||||||
|
|
||||||
var bytes = encoding.GetBytes(str);
|
|
||||||
writer.Write(bytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace LibMMD.Writer
|
|||||||
{
|
{
|
||||||
public class PMXWriter
|
public class PMXWriter
|
||||||
{
|
{
|
||||||
public void Write(BinaryWriter writer, RawMMDModel model, ModelConfig config)
|
public static void Write(BinaryWriter writer, RawMMDModel model, ModelConfig config)
|
||||||
{
|
{
|
||||||
var pmxHeader = new PmxMeta
|
var pmxHeader = new PmxMeta
|
||||||
{
|
{
|
||||||
@@ -22,18 +22,31 @@ namespace LibMMD.Writer
|
|||||||
FileFlagSize = 8
|
FileFlagSize = 8
|
||||||
};
|
};
|
||||||
|
|
||||||
|
PmxConfig PmxConfig = new PmxConfig()
|
||||||
|
{
|
||||||
|
Utf8Encoding = true,
|
||||||
|
Encoding = Encoding.UTF8,
|
||||||
|
ExtraUvNumber = 3, //include uv2 and color
|
||||||
|
VertexIndexSize = 2,
|
||||||
|
TextureIndexSize = 1,
|
||||||
|
MaterialIndexSize = 1,
|
||||||
|
BoneIndexSize = 2,
|
||||||
|
MorphIndexSize = 2,
|
||||||
|
RigidBodyIndexSize = 2
|
||||||
|
};
|
||||||
|
|
||||||
WriteMeta(writer, pmxHeader);
|
WriteMeta(writer, pmxHeader);
|
||||||
WritePmxConfig(writer, model.PmxConfig);
|
WritePmxConfig(writer, PmxConfig);
|
||||||
WriteModelNameAndDescription(writer, model, model.PmxConfig);
|
WriteModelNameAndDescription(writer, model, PmxConfig);
|
||||||
WriteVertices(writer, model, model.PmxConfig);
|
WriteVertices(writer, model, PmxConfig);
|
||||||
WriteTriangles(writer, model, model.PmxConfig);
|
WriteTriangles(writer, model, PmxConfig);
|
||||||
WriteTextureList(writer, model.TextureList, model.PmxConfig);
|
WriteTextureList(writer, model.TextureList, PmxConfig);
|
||||||
WriteParts(writer, config, model, model.PmxConfig);
|
WriteParts(writer, config, model, PmxConfig);
|
||||||
WriteBones(writer, model, model.PmxConfig);
|
WriteBones(writer, model, PmxConfig);
|
||||||
WriteMorphs(writer, model, model.PmxConfig);
|
WriteMorphs(writer, model, PmxConfig);
|
||||||
WriteEntries(writer, model.Entrys, model.PmxConfig);
|
WriteEntries(writer, model.Entrys, PmxConfig);
|
||||||
WriteRigidBodies(writer, model.Rigidbodies, model.PmxConfig);
|
WriteRigidBodies(writer, model.Rigidbodies, PmxConfig);
|
||||||
WriteJoints(writer, model, model.PmxConfig);
|
WriteJoints(writer, model, PmxConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteJoints(BinaryWriter writer, RawMMDModel model, PmxConfig pmxConfig)
|
private static void WriteJoints(BinaryWriter writer, RawMMDModel model, PmxConfig pmxConfig)
|
||||||
@@ -401,32 +414,11 @@ namespace LibMMD.Writer
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteParts(BinaryWriter writer, RawMMDModel model, ModelConfig config, PmxConfig pmxConfig, MMDTexture[] textureList)
|
|
||||||
{
|
|
||||||
var partNum = model.Parts.Length;
|
|
||||||
writer.Write(partNum);
|
|
||||||
var partBaseShift = 0;
|
|
||||||
for (var i = 0; i < partNum; i++)
|
|
||||||
{
|
|
||||||
var part = model.Parts[i];
|
|
||||||
var material = part.Material;
|
|
||||||
WriteMaterial(writer, material, config, pmxConfig.Encoding, pmxConfig.TextureIndexSize, new List<MMDTexture>(textureList));
|
|
||||||
var partTriangleIndexNum = part.TriangleIndexNum;
|
|
||||||
writer.Write(partTriangleIndexNum);
|
|
||||||
if (partTriangleIndexNum % 3 != 0)
|
|
||||||
{
|
|
||||||
throw new MMDFileParseException("part" + i + " triangle index count " + partTriangleIndexNum + " is not a multiple of 3");
|
|
||||||
}
|
|
||||||
part.BaseShift = partBaseShift;
|
|
||||||
partBaseShift += partTriangleIndexNum;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void WriteTextureList(BinaryWriter writer, List<MMDTexture> textureList, PmxConfig pmxConfig)
|
private static void WriteTextureList(BinaryWriter writer, List<MMDTexture> textureList, PmxConfig pmxConfig)
|
||||||
{
|
{
|
||||||
var textureNum = textureList.Count;
|
var textureNum = textureList.Count;
|
||||||
writer.Write(textureNum);
|
writer.Write(textureNum);
|
||||||
for (var i = 0; i < textureNum; ++i)
|
for (var i = 0; i < textureNum; i++)
|
||||||
{
|
{
|
||||||
var texturePathEncoding = pmxConfig.Utf8Encoding ? Encoding.UTF8 : Encoding.Unicode;
|
var texturePathEncoding = pmxConfig.Utf8Encoding ? Encoding.UTF8 : Encoding.Unicode;
|
||||||
var texturePath = textureList[i].TexturePath;
|
var texturePath = textureList[i].TexturePath;
|
||||||
@@ -528,11 +520,6 @@ namespace LibMMD.Writer
|
|||||||
MMDReaderWriteUtil.WriteVector2(writer, uvCoordinate);
|
MMDReaderWriteUtil.WriteVector2(writer, uvCoordinate);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WritePmxConfig(object reader, object model)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void WriteMeta(BinaryWriter writer, PmxMeta meta)
|
private static void WriteMeta(BinaryWriter writer, PmxMeta meta)
|
||||||
{
|
{
|
||||||
MMDReaderWriteUtil.WriteStringFixedLength(writer, meta.Magic, 4, Encoding.ASCII);
|
MMDReaderWriteUtil.WriteStringFixedLength(writer, meta.Magic, 4, Encoding.ASCII);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ using Random = UnityEngine.Random;
|
|||||||
|
|
||||||
public class UmaContainer : MonoBehaviour
|
public class UmaContainer : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
public CharaEntry CharaEntry;
|
||||||
public DataRow CharaData;
|
public DataRow CharaData;
|
||||||
public GameObject Body;
|
public GameObject Body;
|
||||||
public GameObject Tail;
|
public GameObject Tail;
|
||||||
@@ -81,7 +82,7 @@ public class UmaContainer : MonoBehaviour
|
|||||||
|
|
||||||
[Header("Other")]
|
[Header("Other")]
|
||||||
public CharaShaderEffectData ShaderEffectData;
|
public CharaShaderEffectData ShaderEffectData;
|
||||||
private List<MaterialHelper> Materials = new List<MaterialHelper>();
|
public List<MaterialHelper> Materials = new List<MaterialHelper>();
|
||||||
|
|
||||||
|
|
||||||
public void Initialize(bool smile)
|
public void Initialize(bool smile)
|
||||||
@@ -490,7 +491,7 @@ public class UmaContainer : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class MaterialHelper
|
public class MaterialHelper
|
||||||
{
|
{
|
||||||
public Material Mat;
|
public Material Mat;
|
||||||
public UmaUIContainer Toggle;
|
public UmaUIContainer Toggle;
|
||||||
@@ -509,6 +510,7 @@ public class UmaContainer : MonoBehaviour
|
|||||||
//if material slot in list and toggle is off - assign invisible
|
//if material slot in list and toggle is off - assign invisible
|
||||||
mat[i] = rend.Value.Contains(i) ? (value? Mat : UmaViewerBuilder.Instance.TransMaterialCharas ): rend.Key.materials[i];
|
mat[i] = rend.Value.Contains(i) ? (value? Mat : UmaViewerBuilder.Instance.TransMaterialCharas ): rend.Key.materials[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
rend.Key.materials = mat;
|
rend.Key.materials = mat;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,10 +78,15 @@ public class FaceTypeData
|
|||||||
public class CharaEntry
|
public class CharaEntry
|
||||||
{
|
{
|
||||||
public string Name;
|
public string Name;
|
||||||
|
public string EnName;
|
||||||
public Sprite Icon;
|
public Sprite Icon;
|
||||||
public int Id;
|
public int Id;
|
||||||
public string ThemeColor;
|
public string ThemeColor;
|
||||||
public bool IsMob;
|
public bool IsMob;
|
||||||
|
public string GetName()
|
||||||
|
{
|
||||||
|
return string.IsNullOrEmpty(EnName) ? Name : EnName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
|
|
||||||
int id = chara.Id;
|
int id = chara.Id;
|
||||||
CurrentUMAContainer = new GameObject($"Chara_{id}_{costumeId}").AddComponent<UmaContainer>();
|
CurrentUMAContainer = new GameObject($"Chara_{id}_{costumeId}").AddComponent<UmaContainer>();
|
||||||
|
CurrentUMAContainer.CharaEntry = chara;
|
||||||
UnloadAllBundle();
|
UnloadAllBundle();
|
||||||
|
|
||||||
if (mini)
|
if (mini)
|
||||||
|
|||||||
@@ -88,7 +88,8 @@ public class UmaViewerMain : MonoBehaviour
|
|||||||
{
|
{
|
||||||
Characters.Add(new CharaEntry()
|
Characters.Add(new CharaEntry()
|
||||||
{
|
{
|
||||||
Name = enNames.ContainsKey(id) ? enNames[id] : item["charaname"].ToString(),
|
Name = item["charaname"].ToString(),
|
||||||
|
EnName = enNames.ContainsKey(id) ? enNames[id] : "",
|
||||||
Icon = UmaViewerBuilder.Instance.LoadCharaIcon(id.ToString()),
|
Icon = UmaViewerBuilder.Instance.LoadCharaIcon(id.ToString()),
|
||||||
Id = id,
|
Id = id,
|
||||||
ThemeColor = "#" + item["ui_nameplate_color_1"].ToString()
|
ThemeColor = "#" + item["ui_nameplate_color_1"].ToString()
|
||||||
|
|||||||
@@ -254,7 +254,7 @@ public class UmaViewerUI : MonoBehaviour
|
|||||||
var charaInstance = chara;
|
var charaInstance = chara;
|
||||||
|
|
||||||
var container3 = Instantiate(UmaContainerPrefab, CharactersList.content).GetComponent<UmaUIContainer>();
|
var container3 = Instantiate(UmaContainerPrefab, CharactersList.content).GetComponent<UmaUIContainer>();
|
||||||
container3.Name = container3.name = chara.Id + " " + chara.Name;
|
container3.Name = container3.name = chara.Id + " " + chara.GetName();
|
||||||
container3.Button.onClick.AddListener(() =>
|
container3.Button.onClick.AddListener(() =>
|
||||||
{
|
{
|
||||||
HighlightChildImage(CharactersList.content, container3);
|
HighlightChildImage(CharactersList.content, container3);
|
||||||
@@ -268,7 +268,7 @@ public class UmaViewerUI : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
var container4 = Instantiate(UmaContainerPrefab, AnimationSetList.content).GetComponent<UmaUIContainer>();
|
var container4 = Instantiate(UmaContainerPrefab, AnimationSetList.content).GetComponent<UmaUIContainer>();
|
||||||
container4.Name = container4.name = chara.Id + " " + chara.Name;
|
container4.Name = container4.name = chara.Id + " " + chara.GetName();
|
||||||
container4.Button.onClick.AddListener(() =>
|
container4.Button.onClick.AddListener(() =>
|
||||||
{
|
{
|
||||||
HighlightChildImage(AnimationSetList.content, container4);
|
HighlightChildImage(AnimationSetList.content, container4);
|
||||||
@@ -288,7 +288,7 @@ public class UmaViewerUI : MonoBehaviour
|
|||||||
var charaInstance = chara;
|
var charaInstance = chara;
|
||||||
var pageentry = new PageManager.Entry();
|
var pageentry = new PageManager.Entry();
|
||||||
|
|
||||||
pageentry.Name = chara.Name;
|
pageentry.Name = chara.GetName();
|
||||||
pageentry.OnClick = (container) =>
|
pageentry.OnClick = (container) =>
|
||||||
{
|
{
|
||||||
HighlightChildImage(MobCharactersList.content, container);
|
HighlightChildImage(MobCharactersList.content, container);
|
||||||
@@ -317,7 +317,7 @@ public class UmaViewerUI : MonoBehaviour
|
|||||||
{
|
{
|
||||||
var charaInstance = chara;
|
var charaInstance = chara;
|
||||||
var container2 = Instantiate(UmaContainerPrefab, MiniCharactersList.content).GetComponent<UmaUIContainer>();
|
var container2 = Instantiate(UmaContainerPrefab, MiniCharactersList.content).GetComponent<UmaUIContainer>();
|
||||||
container2.Name = container2.name = chara.Id + " " + chara.Name;
|
container2.Name = container2.name = chara.Id + " " + chara.GetName();
|
||||||
container2.Button.onClick.AddListener(() =>
|
container2.Button.onClick.AddListener(() =>
|
||||||
{
|
{
|
||||||
HighlightChildImage(MiniCharactersList.content, container2);
|
HighlightChildImage(MiniCharactersList.content, container2);
|
||||||
@@ -331,7 +331,7 @@ public class UmaViewerUI : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
var container3 = Instantiate(UmaContainerPrefab, MiniAnimationSetList.content).GetComponent<UmaUIContainer>();
|
var container3 = Instantiate(UmaContainerPrefab, MiniAnimationSetList.content).GetComponent<UmaUIContainer>();
|
||||||
container3.Name = container3.name = chara.Id + " " + chara.Name;
|
container3.Name = container3.name = chara.Id + " " + chara.GetName();
|
||||||
container3.Button.onClick.AddListener(() =>
|
container3.Button.onClick.AddListener(() =>
|
||||||
{
|
{
|
||||||
HighlightChildImage(MiniAnimationSetList.content, container3);
|
HighlightChildImage(MiniAnimationSetList.content, container3);
|
||||||
@@ -903,7 +903,7 @@ public class UmaViewerUI : MonoBehaviour
|
|||||||
string getCharaName(string id)
|
string getCharaName(string id)
|
||||||
{
|
{
|
||||||
var entry = Main.Characters.FirstOrDefault(a => a.Id.ToString().Equals(id));
|
var entry = Main.Characters.FirstOrDefault(a => a.Id.ToString().Equals(id));
|
||||||
return (entry == null) ? id.ToString() : entry.Name;
|
return (entry == null) ? id.ToString() : entry.GetName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetCostumeName(string costumeId, string defaultname)
|
public static string GetCostumeName(string costumeId, string defaultname)
|
||||||
@@ -1317,11 +1317,11 @@ public class UmaViewerUI : MonoBehaviour
|
|||||||
var container = Builder.CurrentUMAContainer;
|
var container = Builder.CurrentUMAContainer;
|
||||||
if (container)
|
if (container)
|
||||||
{
|
{
|
||||||
var path = StandaloneFileBrowser.SaveFilePanel("Save PMX File", Config.Instance.MainPath, container.gameObject.name, "pmx");
|
var entry = container.CharaEntry;
|
||||||
|
var path = StandaloneFileBrowser.SaveFilePanel("Save PMX File", Config.Instance.MainPath, $"{entry.Id}_{entry.GetName()}", "pmx");
|
||||||
if (!string.IsNullOrEmpty(path))
|
if (!string.IsNullOrEmpty(path))
|
||||||
{
|
{
|
||||||
ModelExporter.exportModel(path,container.gameObject);
|
ModelExporter.ExportModel(container, path);
|
||||||
TextureExporter.exportAllTexture(Path.GetDirectoryName(path),container.gameObject);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user