Skip to content

Commit

Permalink
NormalMapのテクスチャ修正/モデルスケール変更時コライダーのスケールも変更するように
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-akira committed Sep 19, 2018
1 parent c8f82de commit 7507e8a
Show file tree
Hide file tree
Showing 15 changed files with 486 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Assets/ExternalPlugins/CorrectNormalMapImport.meta

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

272 changes: 272 additions & 0 deletions Assets/ExternalPlugins/CorrectNormalMapImport/ChangeTextureType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
using System.Collections.Generic;

namespace Yashinut.VRoid
{
/// <summary>
/// TextureTypeを変換するときに使用する
/// </summary>
public static class ChangeTextureType
{
//DefaultからNormalMapへ変換する際に利用
//数式が導き出せなかったので、0~255の範囲で対応する値を取得するDictionaryを定義
public static readonly Dictionary<int, int> DefaultToNormalMap = new Dictionary<int, int>()
{
{0, 0},
{1, 13},
{2, 22},
{3, 28},
{4, 34},
{5, 38},
{6, 42},
{7, 46},
{8, 50},
{9, 53},
{10, 56},
{11, 59},
{12, 61},
{13, 64},
{14, 66},
{15, 69},
{16, 71},
{17, 73},
{18, 75},
{19, 77},
{20, 79},
{21, 81},
{22, 83},
{23, 85},
{24, 86},
{25, 88},
{26, 90},
{27, 91},
{28, 93},
{29, 95},
{30, 96},
{31, 98},
{32, 99},
{33, 101},
{34, 102},
{35, 104},
{36, 105},
{37, 106},
{38, 108},
{39, 109},
{40, 110},
{41, 112},
{42, 113},
{43, 114},
{44, 115},
{45, 117},
{46, 118},
{47, 119},
{48, 120},
{49, 121},
{50, 122},
{51, 124},
{52, 125},
{53, 126},
{54, 127},
{55, 128},
{56, 129},
{57, 130},
{58, 131},
{59, 132},
{60, 133},
{61, 134},
{62, 135},
{63, 136},
{64, 137},
{65, 138},
{66, 139},
{67, 140},
{68, 141},
{69, 142},
{70, 143},
{71, 144},
{72, 145},
{73, 146},
{74, 147},
{75, 148},
{76, 148},
{77, 149},
{78, 150},
{79, 151},
{80, 152},
{81, 153},
{82, 154},
{83, 155},
{84, 155},
{85, 156},
{86, 157},
{87, 158},
{88, 159},
{89, 160},
{90, 160},
{91, 161},
{92, 162},
{93, 163},
{94, 163},
{95, 164},
{96, 165},
{97, 166},
{98, 167},
{99, 167},
{100, 168},
{101, 169},
{102, 170},
{103, 170},
{104, 171},
{105, 172},
{106, 173},
{107, 173},
{108, 174},
{109, 175},
{110, 175},
{111, 176},
{112, 177},
{113, 178},
{114, 178},
{115, 179},
{116, 180},
{117, 180},
{118, 181},
{119, 182},
{120, 182},
{121, 183},
{122, 184},
{123, 184},
{124, 185},
{125, 186},
{126, 186},
{127, 187},
{128, 188},
{129, 189},
{130, 189},
{131, 190},
{132, 190},
{133, 191},
{134, 192},
{135, 192},
{136, 193},
{137, 194},
{138, 194},
{139, 195},
{140, 196},
{141, 196},
{142, 197},
{143, 197},
{144, 198},
{145, 199},
{146, 199},
{147, 200},
{148, 200},
{149, 201},
{150, 202},
{151, 202},
{152, 203},
{153, 203},
{154, 204},
{155, 205},
{156, 205},
{157, 206},
{158, 206},
{159, 207},
{160, 208},
{161, 208},
{162, 209},
{163, 209},
{164, 210},
{165, 210},
{166, 211},
{167, 211},
{168, 212},
{169, 213},
{170, 213},
{171, 214},
{172, 214},
{173, 215},
{174, 215},
{175, 216},
{176, 217},
{177, 217},
{178, 218},
{179, 218},
{180, 219},
{181, 219},
{182, 220},
{183, 220},
{184, 221},
{185, 221},
{186, 222},
{187, 222},
{188, 223},
{189, 223},
{190, 224},
{191, 224},
{192, 225},
{193, 226},
{194, 226},
{195, 227},
{196, 227},
{197, 228},
{198, 228},
{199, 229},
{200, 229},
{201, 230},
{202, 230},
{203, 231},
{204, 231},
{205, 232},
{206, 232},
{207, 233},
{208, 233},
{209, 234},
{210, 234},
{211, 235},
{212, 235},
{213, 236},
{214, 236},
{215, 236},
{216, 237},
{217, 237},
{218, 238},
{219, 238},
{220, 239},
{221, 239},
{222, 240},
{223, 240},
{224, 241},
{225, 241},
{226, 242},
{227, 242},
{228, 243},
{229, 243},
{230, 244},
{231, 244},
{232, 245},
{233, 245},
{234, 245},
{235, 246},
{236, 246},
{237, 247},
{238, 247},
{239, 248},
{240, 248},
{241, 249},
{242, 249},
{243, 250},
{244, 250},
{245, 251},
{246, 251},
{247, 252},
{248, 252},
{249, 252},
{250, 253},
{251, 253},
{252, 254},
{253, 254},
{254, 255},
{255, 255},
};
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Collections.Generic;
using UnityEngine;

namespace Yashinut.VRoid
{
/// <summary>
/// VRoid出力のVRMをランタイムで読み込んだときに、NormalMapを修正するクラス
/// </summary>
public class CorrectNormalMapImport
{

/// <summary>
/// VRoid用VRMのNormalMapを修正する
/// </summary>
/// <param name="vrmForVRoid"></param>
public static void CorrectNormalMap(GameObject vrmForVRoid)
{
var skinnedMeshRenderers = vrmForVRoid.GetComponentsInChildren<SkinnedMeshRenderer>();
var materials = new List<Material>();

//vrmデータ内で使用されているマテリアルを全て取得する
foreach (var skinnedMesh in skinnedMeshRenderers)
{
materials.AddRange(skinnedMesh.materials);
}

var normalTextures = new List<Texture>();

//全てのマテリアルからNormalMapに設定されているTextureを取得する。
foreach (var material in materials)
{
// VRM/MToonシェーダーのNormalMapを取得。
var tex = material.GetTexture("_BumpMap");
if (tex == null) continue;
var defaultNormalMapTexture = ToTexture2D(tex);

Object.Destroy(tex);
//修正したNormalMapを取得
var correctedNormalMapTexture = CorrectNormalMap(defaultNormalMapTexture);
Object.Destroy(defaultNormalMapTexture);
// 修正したNormalMapを設定
material.SetTexture("_BumpMap", correctedNormalMapTexture);
}
}

/// <summary>
/// TextureをTexture2Dへ変換
/// </summary>
/// <param name="texture"></param>
/// <returns></returns>
private static Texture2D ToTexture2D(Texture texture)
{
var resultTexture = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
var currentRenderTexture = RenderTexture.active;
var renderTexture = new RenderTexture(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
resultTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
resultTexture.Apply();
RenderTexture.active = currentRenderTexture;

return resultTexture;
}

/// <summary>
/// TextureTypeがNormalMapのときと同等になるように修正
/// </summary>
/// <param name="defaultNormalTexture"></param>
/// <returns></returns>
private static Texture2D CorrectNormalMap(Texture2D defaultNormalTexture)
{
var pixels = defaultNormalTexture.GetPixels();

for (var i = 0; i < pixels.Length; i++)
{
//各ピクセルごとにNormalMap用の修正を行う。
var x = 1f;
var y = ChangeTextureType.DefaultToNormalMap[(int)(pixels[i].g * 255f)] / 255f;
var z = ChangeTextureType.DefaultToNormalMap[(int)(pixels[i].g * 255f)] / 255f;
var w = pixels[i].a;

pixels[i] = new Color(x, y, z, w);
}

var resultTexture = new Texture2D(defaultNormalTexture.width, defaultNormalTexture.height, TextureFormat.RGBA32, false);
resultTexture.SetPixels(pixels);
resultTexture.Apply();
return resultTexture;
}
}
}

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

Loading

0 comments on commit 7507e8a

Please sign in to comment.