Separate method using VRM form format classes

This commit is contained in:
ousttrue
2019-02-28 19:03:36 +09:00
parent 2812ee356e
commit 24332fa3a0
7 changed files with 193 additions and 166 deletions

View File

@@ -204,7 +204,7 @@ namespace VRM
// materials
foreach (var m in exporter.Materials)
{
gltf.extensions.VRM.materialProperties.Add(glTF_VRM_Material.CreateFromMaterial(m, exporter.TextureManager.Textures));
gltf.extensions.VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, exporter.TextureManager.Textures));
}
}
}

View File

@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UniGLTF;
using UniGLTF.ShaderPropExporter;
using UnityEngine;
@@ -96,5 +99,115 @@ namespace VRM
return material;
}
#region CreateFromMaterial
private static readonly string[] VRMExtensionShaders = new string[]
{
"VRM/UnlitTransparentZWrite",
"VRM/MToon"
};
static readonly string[] TAGS = new string[]{
"RenderType",
// "Queue",
};
public static glTF_VRM_Material CreateFromMaterial(Material m, List<Texture> textures)
{
var material = new glTF_VRM_Material
{
name = m.name,
shader = m.shader.name,
renderQueue = m.renderQueue,
};
if (!VRMExtensionShaders.Contains(m.shader.name))
{
material.shader = glTF_VRM_Material.VRM_USE_GLTFSHADER;
return material;
}
var prop = PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
if (prop == null)
{
Debug.LogWarningFormat("Fail to export shader: {0}", m.shader.name);
}
else
{
foreach (var keyword in m.shaderKeywords)
{
material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword));
}
// get properties
//material.SetProp(prop);
foreach (var kv in prop.Properties)
{
switch (kv.ShaderPropertyType)
{
case ShaderPropertyType.Color:
{
var value = m.GetColor(kv.Key).ToArray();
material.vectorProperties.Add(kv.Key, value);
}
break;
case ShaderPropertyType.Range:
case ShaderPropertyType.Float:
{
var value = m.GetFloat(kv.Key);
material.floatProperties.Add(kv.Key, value);
}
break;
case ShaderPropertyType.TexEnv:
{
var texture = m.GetTexture(kv.Key);
if (texture != null)
{
var value = textures.IndexOf(texture);
if (value == -1)
{
Debug.LogFormat("not found {0}", texture.name);
}
else
{
material.textureProperties.Add(kv.Key, value);
}
}
// offset & scaling
var offset = m.GetTextureOffset(kv.Key);
var scaling = m.GetTextureScale(kv.Key);
material.vectorProperties.Add(kv.Key,
new float[] { offset.x, offset.y, scaling.x, scaling.y });
}
break;
case ShaderPropertyType.Vector:
{
var value = m.GetVector(kv.Key).ToArray();
material.vectorProperties.Add(kv.Key, value);
}
break;
default:
throw new NotImplementedException();
}
}
}
foreach (var tag in TAGS)
{
var value = m.GetTag(tag, false);
if (!String.IsNullOrEmpty(value))
{
material.tagMap.Add(tag, value);
}
}
return material;
}
#endregion
}
}

View File

@@ -0,0 +1,64 @@
using System.Linq;
using System.Collections.Generic;
using UniGLTF;
using UnityEngine;
namespace VRM
{
public static class glTF_VRMExtensions
{
public static glTF_VRM_BlendShapeBind Cerate(Transform root, List<Mesh> meshes, BlendShapeBinding binding)
{
var transform = UniGLTF.UnityExtensions.GetFromPath(root.transform, binding.RelativePath);
var renderer = transform.GetComponent<SkinnedMeshRenderer>();
var mesh = renderer.sharedMesh;
var meshIndex = meshes.IndexOf(mesh);
return new glTF_VRM_BlendShapeBind
{
mesh = meshIndex,
index = binding.Index,
weight = binding.Weight,
};
}
public static void Add(this glTF_VRM_BlendShapeMaster master,
BlendShapeClip clip, Transform transform, List<Mesh> meshes)
{
var list = new List<glTF_VRM_BlendShapeBind>();
if (clip.Values != null)
{
list.AddRange(clip.Values.Select(y => Cerate(transform, meshes.ToList(), y)));
}
var materialList = new List<glTF_VRM_MaterialValueBind>();
if (clip.MaterialValues != null)
{
materialList.AddRange(clip.MaterialValues.Select(y => new glTF_VRM_MaterialValueBind
{
materialName = y.MaterialName,
propertyName = y.ValueName,
targetValue = y.TargetValue.ToArray(),
}));
}
var group = new glTF_VRM_BlendShapeGroup
{
name = clip.BlendShapeName,
presetName = clip.Preset.ToString().ToLower(),
isBinary = clip.IsBinary,
binds = list,
materialValues = materialList,
};
master.blendShapeGroups.Add(group);
}
public static void Apply(this glTF_VRM_DegreeMap map, CurveMapper mapper)
{
map.curve = mapper.Curve.keys.SelectMany(x => new float[] { x.time, x.value, x.inTangent, x.outTangent }).ToArray();
map.xRange = mapper.CurveXRangeDegree;
map.yRange = mapper.CurveYRangeDegree;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5fbd5248b4a422e47be4510ca85c9ade
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,10 +1,9 @@
using System;
using System.Linq;
using System.Collections.Generic;
using UniGLTF;
using UnityEngine;
using UniJSON;
namespace VRM
{
[Serializable]
@@ -42,21 +41,6 @@ namespace VRM
f.KeyValue(() => index);
f.KeyValue(() => weight);
}
public static glTF_VRM_BlendShapeBind Cerate(Transform root, List<Mesh> meshes, BlendShapeBinding binding)
{
var transform = UniGLTF.UnityExtensions.GetFromPath(root.transform, binding.RelativePath);
var renderer = transform.GetComponent<SkinnedMeshRenderer>();
var mesh = renderer.sharedMesh;
var meshIndex = meshes.IndexOf(mesh);
return new glTF_VRM_BlendShapeBind
{
mesh = meshIndex,
index = binding.Index,
weight = binding.Weight,
};
}
}
public enum BlendShapePreset
@@ -143,35 +127,6 @@ namespace VRM
{
public List<glTF_VRM_BlendShapeGroup> blendShapeGroups = new List<glTF_VRM_BlendShapeGroup>();
public void Add(BlendShapeClip clip, Transform transform, List<Mesh> meshes)
{
var list = new List<glTF_VRM_BlendShapeBind>();
if (clip.Values != null)
{
list.AddRange(clip.Values.Select(y => glTF_VRM_BlendShapeBind.Cerate(transform, meshes.ToList(), y)));
}
var materialList = new List<glTF_VRM_MaterialValueBind>();
if (clip.MaterialValues != null)
{
materialList.AddRange(clip.MaterialValues.Select(y => new glTF_VRM_MaterialValueBind
{
materialName = y.MaterialName,
propertyName = y.ValueName,
targetValue = y.TargetValue.ToArray(),
}));
}
var group = new glTF_VRM_BlendShapeGroup
{
name = clip.BlendShapeName,
presetName = clip.Preset.ToString().ToLower(),
isBinary = clip.IsBinary,
binds = list,
materialValues = materialList,
};
blendShapeGroups.Add(group);
}
protected override void SerializeMembers(GLTFJsonFormatter f)
{

View File

@@ -1,10 +1,10 @@
using System;
using System.Linq;
using System.Collections.Generic;
using UniGLTF;
using UnityEngine;
using UniJSON;
namespace VRM
{
[Serializable]
@@ -29,13 +29,6 @@ namespace VRM
f.KeyValue(() => xRange);
f.KeyValue(() => yRange);
}
public void Apply(CurveMapper mapper)
{
curve = mapper.Curve.keys.SelectMany(x => new float[] { x.time, x.value, x.inTangent, x.outTangent }).ToArray();
xRange = mapper.CurveXRangeDegree;
yRange = mapper.CurveYRangeDegree;
}
}
public enum FirstPersonFlag

View File

@@ -2,9 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using UniGLTF;
using UnityEngine;
using UniJSON;
using UniGLTF.ShaderPropExporter;
namespace VRM
{
@@ -22,18 +21,7 @@ namespace VRM
public Dictionary<string, bool> keywordMap = new Dictionary<string, bool>();
public Dictionary<string, string> tagMap = new Dictionary<string, string>();
static readonly string[] TAGS = new string[]{
"RenderType",
// "Queue",
};
private static readonly string[] VRMExtensionShaders = new string[]
{
"VRM/UnlitTransparentZWrite",
"VRM/MToon"
};
private static readonly string VRM_USE_GLTFSHADER = "VRM_USE_GLTFSHADER";
public static readonly string VRM_USE_GLTFSHADER = "VRM_USE_GLTFSHADER";
protected override void SerializeMembers(GLTFJsonFormatter f)
{
@@ -116,102 +104,5 @@ namespace VRM
}
return materials;
}
public static glTF_VRM_Material CreateFromMaterial(Material m, List<Texture> textures)
{
var material = new glTF_VRM_Material
{
name = m.name,
shader = m.shader.name,
renderQueue = m.renderQueue,
};
if (!VRMExtensionShaders.Contains(m.shader.name))
{
material.shader = VRM_USE_GLTFSHADER;
return material;
}
var prop = PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
if (prop == null)
{
Debug.LogWarningFormat("Fail to export shader: {0}", m.shader.name);
}
else
{
foreach (var keyword in m.shaderKeywords)
{
material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword));
}
// get properties
//material.SetProp(prop);
foreach (var kv in prop.Properties)
{
switch (kv.ShaderPropertyType)
{
case ShaderPropertyType.Color:
{
var value = m.GetColor(kv.Key).ToArray();
material.vectorProperties.Add(kv.Key, value);
}
break;
case ShaderPropertyType.Range:
case ShaderPropertyType.Float:
{
var value = m.GetFloat(kv.Key);
material.floatProperties.Add(kv.Key, value);
}
break;
case ShaderPropertyType.TexEnv:
{
var texture = m.GetTexture(kv.Key);
if (texture != null)
{
var value = textures.IndexOf(texture);
if (value == -1)
{
Debug.LogFormat("not found {0}", texture.name);
}
else
{
material.textureProperties.Add(kv.Key, value);
}
}
// offset & scaling
var offset = m.GetTextureOffset(kv.Key);
var scaling = m.GetTextureScale(kv.Key);
material.vectorProperties.Add(kv.Key,
new float[] { offset.x, offset.y, scaling.x, scaling.y });
}
break;
case ShaderPropertyType.Vector:
{
var value = m.GetVector(kv.Key).ToArray();
material.vectorProperties.Add(kv.Key, value);
}
break;
default:
throw new NotImplementedException();
}
}
}
foreach (var tag in TAGS)
{
var value = m.GetTag(tag, false);
if (!String.IsNullOrEmpty(value))
{
material.tagMap.Add(tag, value);
}
}
return material;
}
}
}