Skip to content

Commit 27c6463

Browse files
committed
Working procedural asset load flow
1 parent 3323b8a commit 27c6463

15 files changed

+786
-213
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,5 @@ unity/Assets/Editor/Instant Screenshot/Documentation - Instant screenshot.pdf.me
128128
*.log
129129
tmp/
130130

131+
objaverse/
132+
unity/Assets/Resources/ai2thor-objaverse

scripts/objaverse_expand.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import os
2+
import glob
3+
import argparse
4+
import compress_pickle
5+
import json
6+
7+
def normalize_texture_path(obj_dir, texture_path):
8+
return os.path.join(obj_dir, os.path.basename(texture_path))
9+
10+
def expand_objaverse(directory, ids):
11+
id_set = {}
12+
if ids != "":
13+
id_set = {id for id in ids.split(",")}
14+
print(f"selected ids: {id_set}")
15+
for obj_file in glob.glob(os.path.join(directory, "*", "*.pkl.gz")):
16+
obj_dir = os.path.dirname(obj_file)
17+
obj_id = os.path.basename(obj_dir)
18+
if not id_set or obj_id in id_set:
19+
obj = compress_pickle.load(obj_file)
20+
obj["albedoTexturePath"] = normalize_texture_path(obj_dir, obj["albedoTexturePath"])
21+
obj["normalTexturePath"] = normalize_texture_path(obj_dir, obj["normalTexturePath"])
22+
obj["emissionTexturePath"] = normalize_texture_path(obj_dir, obj["emissionTexturePath"])
23+
out_obj = os.path.join(obj_dir, f"{obj_id}.json")
24+
print(out_obj)
25+
with open(out_obj, "w") as f:
26+
json.dump(obj, f)
27+
28+
if __name__ == "__main__":
29+
# 0a0a8274693445a6b533dce7f97f747c
30+
parser = argparse.ArgumentParser()
31+
parser.add_argument('directory')
32+
parser.add_argument(
33+
"--ids",
34+
help="Comma separated list of ids to convert",
35+
type=str,
36+
default="",
37+
)
38+
args = parser.parse_args()
39+
expand_objaverse(args.directory, args.ids)

unity/Assets/Scripts/BaseFPSAgentController.cs

Lines changed: 20 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ public virtual ObjectMetadata[] generateObjectMetadata(SimObjPhysics[] simObject
20532053

20542054
for (int k = 0; k < numObj; k++) {
20552055
SimObjPhysics simObj = simObjects[k];
2056-
ObjectMetadata meta = ObjectMetadataFromSimObjPhysics(
2056+
ObjectMetadata meta = SimObjPhysics.ObjectMetadataFromSimObjPhysics(
20572057
simObj,
20582058
visibleSimObjsHash.Contains(simObj),
20592059
interactableSimObjsHash.Contains(simObj)
@@ -6677,215 +6677,28 @@ public void CreateObjectPrefab(
66776677
Vector3[]? visibilityPoints = null,
66786678
ObjectAnnotations annotations = null,
66796679
bool receptacleCandidate = false,
6680-
float yRotOffset = 0f
6680+
float yRotOffset = 0f,
6681+
bool serializable = false
66816682
) {
6682-
// create a new game object
6683-
GameObject go = new GameObject();
6684-
6685-
// create a new mesh
6686-
GameObject meshObj = new GameObject("mesh");
6687-
meshObj.transform.parent = go.transform;
6688-
Mesh mesh = new Mesh();
6689-
if (vertices.Length >= 65535) {
6690-
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
6691-
}
6692-
mesh.vertices = vertices;
6693-
mesh.triangles = triangles;
6694-
mesh.normals = normals;
6695-
if (uvs != null) {
6696-
mesh.uv = uvs;
6697-
}
6698-
6699-
// add the mesh to the object
6700-
meshObj.AddComponent<MeshRenderer>();
6701-
MeshFilter meshFilter = meshObj.AddComponent<MeshFilter>();
6702-
meshFilter.mesh = mesh;
6703-
6704-
// add the mesh colliders
6705-
GameObject triggerCollidersObj = new GameObject("TriggerColliders");
6706-
triggerCollidersObj.layer = LayerMask.NameToLayer("SimObjVisible");
6707-
triggerCollidersObj.transform.parent = go.transform;
6708-
6709-
GameObject meshCollidersObj = new GameObject("Colliders");
6710-
meshCollidersObj.layer = LayerMask.NameToLayer("SimObjVisible");
6711-
meshCollidersObj.transform.parent = go.transform;
6712-
List<Collider> meshColliders = new List<Collider>();
6713-
if (colliders != null && colliders.Length > 0) {
6714-
int i = 0;
6715-
foreach (var collider in colliders) {
6716-
// create a mesh of the collider
6717-
Mesh colliderMesh = new Mesh();
6718-
colliderMesh.vertices = collider.vertices;
6719-
colliderMesh.triangles = collider.triangles;
6720-
6721-
// add the mesh collider
6722-
GameObject meshColliderObj = new GameObject($"collider_{i}");
6723-
meshColliderObj.layer = LayerMask.NameToLayer("SimObjVisible");
6724-
meshColliderObj.transform.parent = meshCollidersObj.transform;
6725-
MeshCollider meshCollider = meshColliderObj.AddComponent<MeshCollider>();
6726-
meshCollider.sharedMesh = colliderMesh;
6727-
meshCollider.convex = true;
6728-
meshColliders.Add(meshCollider);
6729-
6730-
// add the trigger collider
6731-
GameObject triggerColliderObj = new GameObject($"trigger_{i}");
6732-
triggerColliderObj.layer = LayerMask.NameToLayer("SimObjVisible");
6733-
triggerColliderObj.transform.parent = triggerCollidersObj.transform;
6734-
MeshCollider triggerCollider = triggerColliderObj.AddComponent<MeshCollider>();
6735-
triggerCollider.sharedMesh = colliderMesh;
6736-
triggerCollider.convex = true;
6737-
triggerCollider.isTrigger = true;
6738-
6739-
i++;
6740-
}
6741-
}
6742-
6743-
// add the visibility points
6744-
GameObject visPoints = new GameObject("VisibilityPoints");
6745-
visPoints.transform.parent = go.transform;
6746-
Transform[] visPointTransforms = new Transform[visibilityPoints.Length];
6747-
for (int i = 0; i < visibilityPoints.Length; i++) {
6748-
GameObject visPoint = new GameObject($"visPoint_{i}");
6749-
visPoint.transform.parent = visPoints.transform;
6750-
visPoint.transform.localPosition = visibilityPoints[i];
6751-
visPointTransforms[i] = visPoint.transform;
6752-
visPoint.layer = LayerMask.NameToLayer("SimObjVisible");
6753-
}
6754-
6755-
// Rotate the object, this requires reassigning things to a new game object
6756-
go.transform.Rotate(Vector3.up, yRotOffset);
6757-
GameObject newGo = new GameObject();
6758-
6759-
foreach (Transform t in go.GetComponentsInChildren<Transform>()) {
6760-
if (t.parent == go.transform) {
6761-
Debug.Log($"Moving transform of {t.gameObject.name}");
6762-
t.parent = newGo.transform;
6763-
}
6764-
}
6765-
go.SetActive(false);
6766-
go = newGo;
6767-
6768-
go.name = name;
6769-
go.layer = LayerMask.NameToLayer("SimObjVisible");
6770-
go.tag = "SimObjPhysics";
6771-
6772-
Material mat = null;
6773-
// load image from disk
6774-
if (albedoTexturePath != null) {
6775-
// textures aren't saved as part of the prefab, so we load them from disk
6776-
RuntimePrefab runtimePrefab = go.AddComponent<RuntimePrefab>();
6777-
runtimePrefab.localTexturePath = albedoTexturePath;
6778-
6779-
byte[] imageBytes = File.ReadAllBytes(albedoTexturePath);
6780-
// Is this size right?
6781-
Texture2D tex = new Texture2D(2, 2);
6782-
tex.LoadImage(imageBytes);
6783-
6784-
// create a new material
6785-
mat = new Material(Shader.Find("Standard"));
6786-
mat.mainTexture = tex;
6787-
6788-
// assign the material to the game object
6789-
meshObj.GetComponent<Renderer>().material = mat;
6790-
runtimePrefab.sharedMaterial = mat;
6791-
} else {
6792-
// create a new material
6793-
mat = new Material(Shader.Find("Standard"));
6794-
meshObj.GetComponent<Renderer>().material = mat;
6795-
}
6796-
6797-
mat.SetFloat("_Glossiness", 0f);
6798-
6799-
if (normalTexturePath != null) {
6800-
mat.EnableKeyword("_NORMALMAP");
6801-
byte[] imageBytes = File.ReadAllBytes(normalTexturePath);
6802-
Texture2D tex = new Texture2D(2, 2);
6803-
tex.LoadImage(imageBytes);
6804-
mat.SetTexture("_BumpMap", tex);
6805-
}
6806-
6807-
if (emissionTexturePath != null) {
6808-
mat.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
6809-
mat.EnableKeyword("_EMISSION");
6810-
byte[] imageBytes = File.ReadAllBytes(emissionTexturePath);
6811-
Texture2D tex = new Texture2D(2, 2);
6812-
tex.LoadImage(imageBytes);
6813-
mat.SetTexture("_EmissionMap", tex);
6814-
mat.SetColor("_EmissionColor", Color.white);
6815-
}
6816-
6817-
// have the mesh refer to the mesh at meshPath
6818-
meshObj.GetComponent<MeshFilter>().sharedMesh = mesh;
6819-
6820-
// add the rigidbody
6821-
Rigidbody rb = go.AddComponent<Rigidbody>();
6822-
if (physicalProperties != null) {
6823-
rb.mass = physicalProperties.mass;
6824-
rb.drag = physicalProperties.drag;
6825-
rb.angularDrag = physicalProperties.angularDrag;
6826-
rb.useGravity = physicalProperties.useGravity;
6827-
rb.isKinematic = physicalProperties.isKinematic;
6828-
}
6829-
6830-
// add the SimObjPhysics component
6831-
SimObjPhysics sop = go.AddComponent<SimObjPhysics>();
6832-
sop.VisibilityPoints = visPointTransforms;
6833-
sop.MyColliders = meshColliders.ToArray();
6834-
sop.assetID = name;
6835-
sop.objectID = name;
6836-
6837-
// add the annotations of the object
6838-
if (annotations == null) {
6839-
annotations = new ObjectAnnotations();
6840-
}
6841-
sop.PrimaryProperty = (SimObjPrimaryProperty)Enum.Parse(
6842-
typeof(SimObjPrimaryProperty), annotations.primaryProperty
6683+
var assetData = ProceduralTools.CreateAsset(
6684+
vertices,
6685+
normals,
6686+
name,
6687+
triangles,
6688+
uvs,
6689+
albedoTexturePath ,
6690+
normalTexturePath ,
6691+
emissionTexturePath,
6692+
colliders ,
6693+
physicalProperties,
6694+
visibilityPoints ,
6695+
annotations ,
6696+
receptacleCandidate ,
6697+
yRotOffset ,
6698+
serializable
68436699
);
6844-
sop.Type = (SimObjType)Enum.Parse(typeof(SimObjType), annotations.objectType);
6845-
if (annotations.secondaryProperties == null) {
6846-
annotations.secondaryProperties = new string[0];
6847-
}
6848-
sop.SecondaryProperties = annotations.secondaryProperties.Select(
6849-
p => (SimObjSecondaryProperty)Enum.Parse(typeof(SimObjSecondaryProperty), p)
6850-
).ToArray();
6851-
sop.syncBoundingBoxes(forceCacheReset: true, forceCreateObjectOrientedBoundingBox: true);
68526700

6853-
if (receptacleCandidate) {
6854-
BaseFPSAgentController.TryToAddReceptacleTriggerBox(sop: sop);
6855-
GameObject receptacleTriggerBoxes = go.transform.Find("ReceptacleTriggerBoxes").gameObject;
6856-
if (receptacleTriggerBoxes.transform.childCount > 0) {
6857-
sop.SecondaryProperties = new SimObjSecondaryProperty[] { SimObjSecondaryProperty.Receptacle };
6858-
}
6859-
}
6860-
6861-
// Add the asset to the procedural asset database
6862-
var assetDb = GameObject.FindObjectOfType<ProceduralAssetDatabase>();
6863-
if (assetDb != null) {
6864-
assetDb.addAsset(go, procedural: true);
6865-
}
6866-
6867-
// Compute the object metadata
6868-
// IMPORTANT: this must happen before the object is added to the prefabParent object below,
6869-
// otherwise the object's bounding box will be incorrect! This has something to do with how
6870-
// our bounding box computation code works where it ignores some unactive components of an object
6871-
var assetMeta = getAssetMetadata(sop.gameObject);
6872-
var objectMeta = ObjectMetadataFromSimObjPhysics(sop, true, true);
6873-
6874-
// get child object on assetDb's game object that's called "Prefabs"
6875-
// and add the prefab to that
6876-
var prefabParentTransform = assetDb.transform.Find("Prefabs");
6877-
if (prefabParentTransform == null) {
6878-
var prefabParent = new GameObject("Prefabs");
6879-
prefabParent.transform.parent = assetDb.transform;
6880-
prefabParent.SetActive(false);
6881-
prefabParentTransform = prefabParent.transform;
6882-
}
6883-
go.transform.parent = prefabParentTransform;
6884-
6885-
actionFinished(success: true, actionReturn: new Dictionary<string, object>{
6886-
{"assetMetadata", assetMeta},
6887-
{"objectMetadata", objectMeta}
6888-
});
6701+
actionFinished(success: true, actionReturn: assetData);
68896702
}
68906703

68916704
public void CreateHouse(ProceduralHouse house) {

unity/Assets/Scripts/DebugInputField.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4441,6 +4441,7 @@ IEnumerator executeBatch(JArray jActions) {
44414441
JObject obj = JObject.Parse(jsonStr);
44424442

44434443
obj["action"] = "CreateObjectPrefab";
4444+
obj["serializable"] = true;
44444445
CurrentActiveController().ProcessControlCommand(new DynamicServerAction(obj));
44454446

44464447
break;

unity/Assets/Scripts/DraggablePoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class DraggablePoint : PropertyAttribute {}
88

99

1010
#if UNITY_EDITOR
11-
[CustomEditor(typeof(MonoBehaviour), true)]
11+
// [CustomEditor(typeof(MonoBehaviour), true)]
1212
public class DraggablePointDrawer : Editor {
1313

1414
readonly GUIStyle style = new GUIStyle();

unity/Assets/Scripts/ProceduralAssetDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ProceduralAssetDatabase : MonoBehaviour {
1717

1818
public bool dontDestroyOnLoad = true;
1919

20-
void Awake() {
20+
public void Awake() {
2121
if (Instance != null) {
2222
Destroy(gameObject);
2323
return;

0 commit comments

Comments
 (0)