Skip to content

Commit 2aac7fb

Browse files
Fixes for textures and materials.
1 parent 329847f commit 2aac7fb

File tree

3 files changed

+60
-26
lines changed

3 files changed

+60
-26
lines changed

Assets/AFrameExporter/AFrameExporter.cs

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class AFrameExporter : ScriptableObject {
99
public string title = "Hello world!";
1010
public string libraryAddress = "https://aframe.io/releases/1.1.0/aframe.min.js";
1111
public bool enable_performance_statistics = false;
12+
public bool unique_assets = false;
1213
[HeaderAttribute("Sky")]
1314
public bool enable_sky = false;
1415
public bool sky_color_from_MainCamera_Background = true;
@@ -157,7 +158,7 @@ private string convertScene()
157158
string imageSrcUrl = "";
158159
if (mat)
159160
{
160-
imageSrcUrl = "src=\"" + outputTexture(mat).Replace("src: ", "") + "\" ";
161+
imageSrcUrl = "src=\"" + outputTexture(mat, meshIndex).Replace("src: ", "") + "\" ";
161162
}
162163

163164
//Cubeの場合
@@ -166,7 +167,7 @@ private string convertScene()
166167
Vector3 scale = obj.transform.lossyScale;
167168

168169
//string append_str = indent + "<a-entity geometry=\"primitive: box; width: " + scale.x + "; height: " + scale.y + "; depth: " + scale.z + "\" " + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj) + "></a-entity>\n";
169-
string append_str = indent + "<a-box " + imageSrcUrl + "geometry=\"primitive: box; width: " + scale.x + "; height: " + scale.y + "; depth: " + scale.z + "\" " + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj) + "></a-box>\n";
170+
string append_str = indent + "<a-box " + imageSrcUrl + "geometry=\"primitive: box; width: " + scale.x + "; height: " + scale.y + "; depth: " + scale.z + "\" " + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj, meshIndex) + "></a-box>\n";
170171
ret_str += append_str;
171172
}
172173
//Sphereの場合
@@ -180,7 +181,7 @@ private string convertScene()
180181
radius = (scale.x + scale.y + scale.z) * 0.333333333f * 0.5f;
181182
}
182183
//string append_str = indent + "<a-entity geometry=\"primitive: sphere; radius: " + radius + "\" " + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj) + "></a-entity>\n";
183-
string append_str = indent + "<a-sphere " + imageSrcUrl + "geometry=\"primitive: sphere; radius: " + radius + "\" " + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj) + "></a-sphere>\n";
184+
string append_str = indent + "<a-sphere " + imageSrcUrl + "geometry=\"primitive: sphere; radius: " + radius + "\" " + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj, meshIndex) + "></a-sphere>\n";
184185
ret_str += append_str;
185186
}
186187
//Cylinderの場合(Unityのスケール1,1,1のシリンダーは半径0.5で高さ2) TODO:パックマンみたいに欠けたシリンダー対応 独自コンポーネントくっつけて対応予定
@@ -192,7 +193,7 @@ private string convertScene()
192193
radius = (scale.x + scale.z) * 0.5f * 0.5f;
193194
float height = scale.y * 2f;
194195
//string append_str = indent + "<a-entity geometry=\"primitive: cylinder; radius: " + radius + "\" height:" + height + "; " + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj) + "></a-entity>\n";
195-
string append_str = indent + "<a-cylinder " + imageSrcUrl + "geometry=\"primitive: cylinder; radius: " + radius + "\" height:" + height + "; " + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj) + "></a-cylinder>\n";
196+
string append_str = indent + "<a-cylinder " + imageSrcUrl + "geometry=\"primitive: cylinder; radius: " + radius + "\" height:" + height + "; " + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj, meshIndex) + "></a-cylinder>\n";
196197
ret_str += append_str;
197198
}
198199
//Planeの場合(Unityのスケール1,1,1のプレーンは幅10高さ10)x90回転でa-frameと揃う
@@ -204,7 +205,7 @@ private string convertScene()
204205
float height = obj.transform.lossyScale.z * 10f;
205206

206207
//string append_str = indent + "<a-entity geometry=\"primitive: plane; width:" + width + "; height:" + height + "\" " + outputRotation(eulerAngles) + outputPosition(obj) + outputMaterial(obj) + "></a-entity>\n";
207-
string append_str = indent + "<a-plane " + imageSrcUrl + "geometry=\"primitive: plane; width:" + width + "; height:" + height + "\" " + outputRotation(eulerAngles) + outputPosition(obj) + outputMaterial(obj) + "></a-plane>\n";
208+
string append_str = indent + "<a-plane " + imageSrcUrl + "geometry=\"primitive: plane; width:" + width + "; height:" + height + "\" " + outputRotation(eulerAngles) + outputPosition(obj) + outputMaterial(obj, meshIndex, false) + "></a-plane>\n";
208209
ret_str += append_str;
209210
}
210211
//TODO:videoの場合
@@ -217,7 +218,7 @@ private string convertScene()
217218
//Modelの場合
218219
else
219220
{
220-
string objFileName = meshFilter.sharedMesh.name.Replace(":", "_") + meshIndex++;
221+
string objFileName = meshFilter.sharedMesh.name.Replace(":", "_") + (unique_assets ? meshIndex.ToString() : "");
221222
string new_path = export_path + "/models/" + objFileName + ".obj";
222223
//obj無ければ作成
223224
if (!File.Exists(Application.dataPath + "/AFrameExporter/export/models/" + objFileName + ".obj"))
@@ -231,14 +232,14 @@ private string convertScene()
231232

232233
//マテリアルからテクスチャを取り出す
233234
//string append_str = indent + "<a-entity loader=\"src: url(models/" + objFileName + ".obj); format: obj\" " + outputScale(obj) + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj) + "></a-entity>\n";
234-
string append_str = indent + "<a-obj-model src=\"url(models/" + objFileName + ".obj)\" loader=\"src: url(models/" + objFileName + ".obj); format: obj\" " + outputScale(obj) + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj) + "></a-obj-model>\n";
235+
string append_str = indent + "<a-obj-model src=\"url(models/" + objFileName + ".obj)\" loader=\"src: url(models/" + objFileName + ".obj); format: obj\" " + outputScale(obj) + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj, meshIndex) + "></a-obj-model>\n";
235236
ret_str += append_str;
236237
}
237238
}
238239
//skinnedMeshModelの場合
239240
else if (skinnedMeshRenderer && skinnedMeshRenderer.sharedMesh)
240241
{
241-
string objFileName = skinnedMeshRenderer.sharedMesh.name.Replace(":", "_") + meshIndex++;
242+
string objFileName = skinnedMeshRenderer.sharedMesh.name.Replace(":", "_") + (unique_assets ? meshIndex.ToString() : "");
242243
string new_path = export_path + "/models/" + objFileName + ".obj";
243244
//obj無ければ作成
244245
if (!File.Exists(Application.dataPath + "/AFrameExporter/export/models/" + objFileName + ".obj"))
@@ -252,7 +253,7 @@ private string convertScene()
252253

253254
//マテリアルからテクスチャを取り出す
254255
//string append_str = indent + "<a-entity loader=\"src: url(models/" + objFileName + ".obj); format: obj\" " + outputScale(obj) + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj) + "></a-entity>\n";
255-
string append_str = indent + "<a-obj-model src=\"url(models/" + objFileName + ".obj)\" loader=\"src: url(models/" + objFileName + ".obj); format: obj\" " + outputScale(obj) + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj) + "></a-obj-model>\n";
256+
string append_str = indent + "<a-obj-model src=\"url(models/" + objFileName + ".obj)\" loader=\"src: url(models/" + objFileName + ".obj); format: obj\" " + outputScale(obj) + outputRotation(obj) + outputPosition(obj) + outputMaterial(obj, meshIndex) + "></a-obj-model>\n";
256257
ret_str += append_str;
257258
}
258259
//imageの場合 UnityはQuad シングルスプライトのみ対応
@@ -341,6 +342,8 @@ private string convertScene()
341342
//TODO:Hemisphereの場合
342343
}
343344
}
345+
346+
meshIndex++;
344347
}
345348

346349
//Cameraが無い場合はデフォルト設定
@@ -414,7 +417,7 @@ private string outputScale(Vector3 scale)
414417
return "scale=\"" + scale.x + " " + scale.y + " " + scale.z + "\" ";
415418
}
416419

417-
private string outputMaterial(GameObject obj)
420+
private string outputMaterial(GameObject obj, int index, bool isTransparent = true)
418421
{
419422
string ret_str = "";
420423

@@ -434,7 +437,7 @@ private string outputMaterial(GameObject obj)
434437
ret_str += "shader: standard; ";
435438

436439
//テクスチャ
437-
ret_str += outputTexture(mat);
440+
ret_str += outputTexture(mat, index);
438441

439442
//リピート(xを使う)
440443
ret_str += "repeat: " + mat.mainTextureScale.x + "; ";
@@ -449,7 +452,7 @@ private string outputMaterial(GameObject obj)
449452
ret_str += "roughness: " + (1f - mat.GetFloat("_Glossiness")) + "; ";
450453

451454
//透過有効(_Modeが3ならRendering Modeはtransparent)
452-
ret_str += "transparent: " + (mat.GetFloat("_Mode") == 3 ? "true" : "false") + "; ";
455+
ret_str += "transparent: " + isTransparent.ToString().ToLower() + "; "; // (mat.GetFloat("_Mode") == 3 ? "true" : "false") + "; ";
453456

454457
//透明度
455458
ret_str += "opacity: " + mat.color.a + "; ";
@@ -484,7 +487,7 @@ private string outputMaterial(GameObject obj)
484487
ret_str += "shader: flat; ";
485488

486489
//テクスチャ
487-
ret_str += outputTexture(mat);
490+
ret_str += outputTexture(mat, index);
488491

489492
//リピート(xを使う)
490493
ret_str += "repeat: " + mat.mainTextureScale.x + "; ";
@@ -503,7 +506,7 @@ private string outputMaterial(GameObject obj)
503506
ret_str += "shader: flat; ";
504507

505508
//テクスチャ
506-
ret_str += outputTexture(mat);
509+
ret_str += outputTexture(mat, index);
507510

508511
//リピート(xを使う)
509512
ret_str += "repeat: " + mat.mainTextureScale.x + "; ";
@@ -525,7 +528,7 @@ private string outputMaterial(GameObject obj)
525528
ret_str += "shader: flat; ";
526529

527530
//テクスチャ
528-
ret_str += outputTexture(mat);
531+
ret_str += outputTexture(mat, index);
529532

530533
//リピート(xを使う)
531534
ret_str += "repeat: " + mat.mainTextureScale.x + "; ";
@@ -557,7 +560,7 @@ private string outputMaterial(GameObject obj)
557560
ret_str += "shader: standard; ";
558561

559562
//テクスチャ
560-
ret_str += outputTexture(mat);
563+
ret_str += outputTexture(mat, index);
561564

562565
//リピート(xを使う)
563566
ret_str += "repeat: " + mat.mainTextureScale.x + "; ";
@@ -568,6 +571,8 @@ private string outputMaterial(GameObject obj)
568571
ret_str += "color: #" + ColorToHex(mat.color) + "; ";
569572
}
570573

574+
ret_str += "transparent: " + isTransparent.ToString().ToLower() + "; ";
575+
571576
//メタルネス
572577
//ret_str += "metalness: " + mat.GetFloat("_Metallic") + "; ";
573578

@@ -590,24 +595,43 @@ private string outputMaterial(GameObject obj)
590595
return ret_str;
591596
}
592597

593-
594-
595-
private string outputTexture(Material mat)
598+
private string outputTexture(Material mat, int index)
596599
{
597600
//テクスチャ
598601
Texture tex = mat.GetTexture("_MainTex");
599602
if (tex)
600603
{
601-
string texture_path = AssetDatabase.GetAssetPath(tex);
602-
string new_path = export_path + "/images/" + Path.GetFileName(texture_path);
604+
string base_path = export_path + "/images/";
605+
606+
// Assets/Rocks/Textures/image.tif
607+
string source_path = AssetDatabase.GetAssetPath(tex);
608+
// Assets/AFrameExporter/export/images/image.tif
609+
string destination_path = base_path + Path.GetFileName(source_path);
610+
// Assets/AFrameExporter/export/images/image0.tif
611+
string unique_destination_path = base_path + Path.GetFileNameWithoutExtension(source_path) + (unique_assets ? index.ToString() : "") + Path.GetExtension(source_path);
612+
// Assets/AFrameExporter/export/images/image0.png
613+
string png_destination_path = Path.ChangeExtension(Path.GetFileName(unique_destination_path), ".png");
614+
string file_path = unique_destination_path;
615+
603616
//テクスチャ無ければコピー
604-
if (AssetDatabase.AssetPathToGUID(new_path) == "")
617+
if (source_path.ToLower().EndsWith(".tif"))
605618
{
606-
AssetDatabase.CopyAsset(texture_path, new_path);
619+
file_path = png_destination_path;
620+
621+
if (!File.Exists(png_destination_path))
622+
{
623+
EditorUtility.DisplayProgressBar("Converting TIF to PNG", source_path + " => " + Path.GetFileName(file_path), 0);
624+
System.Drawing.Bitmap.FromFile(source_path).Save(file_path, System.Drawing.Imaging.ImageFormat.Png);
625+
}
626+
}
627+
else if (!File.Exists(unique_destination_path))
628+
{
629+
AssetDatabase.CopyAsset(source_path, file_path);
607630
}
608631

609-
return "src: url(images/" + Path.ChangeExtension(Path.GetFileName(texture_path), ".png") + "); ";
632+
return "src: url(images/" + Path.GetFileName(file_path) + "); ";
610633
}
634+
611635
return "";
612636
}
613637

@@ -625,7 +649,15 @@ private string outputPosition(GameObject obj)
625649
//A-Frameを実行する
626650
public void RunAFrame()
627651
{
628-
System.Diagnostics.Process.Start(Application.dataPath + "/AFrameExporter/export/" + export_filename);
652+
//System.Diagnostics.Process.Start(Application.dataPath + "/AFrameExporter/export/" + export_filename);
653+
var p = new System.Diagnostics.Process();
654+
p.StartInfo.FileName = "python3";
655+
p.StartInfo.Arguments = "-m http.server";
656+
p.StartInfo.WorkingDirectory = Application.dataPath + "/" + export_path;
657+
p.StartInfo.RedirectStandardOutput = true;
658+
p.StartInfo.UseShellExecute = false;
659+
p.StartInfo.CreateNoWindow = false;
660+
p.Start();
629661
}
630662

631663
//エクスポートしたA-Frameをクリア
473 KB
Binary file not shown.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ Compatible with A-Frame version [1.1.0](https://aframe.io/docs/1.1.0/introductio
1313
6. Click Export button on inspector.<br><br>
1414
7. Find the `index.html` file in your project Assets folder under `Assets/AFrameExporter/export/index.html`
1515

16-
*Note, you may need to copy `.png` images into the folder `Assets/AFrameExporter/export/images` for textures used by your scene.*
16+
*Note 1, you may need to copy `.png` images into the folder `Assets/AFrameExporter/export/images` for textures used by your scene.*
17+
18+
*Note 2, some models render better using gltf format, instead of obj. You can convert your models using [gltf-exporer](https://github.com/Plattar/gltf-exporter) from within Unity and save to your `/export/models` directory. Finally, edit your exported index.html to load the gltf by changing the `<a-obj-model>` tag to `<a-gltf-model src="models/your_model.gltf"></a-gltf-model>`*
1719

1820
## Using the Sample
1921
1. Clone This Repository in your PC.<br><br>

0 commit comments

Comments
 (0)