@@ -2053,7 +2053,7 @@ public virtual ObjectMetadata[] generateObjectMetadata(SimObjPhysics[] simObject
2053
2053
2054
2054
for ( int k = 0 ; k < numObj ; k ++ ) {
2055
2055
SimObjPhysics simObj = simObjects [ k ] ;
2056
- ObjectMetadata meta = ObjectMetadataFromSimObjPhysics (
2056
+ ObjectMetadata meta = SimObjPhysics . ObjectMetadataFromSimObjPhysics (
2057
2057
simObj ,
2058
2058
visibleSimObjsHash . Contains ( simObj ) ,
2059
2059
interactableSimObjsHash . Contains ( simObj )
@@ -6677,215 +6677,28 @@ public void CreateObjectPrefab(
6677
6677
Vector3 [ ] ? visibilityPoints = null ,
6678
6678
ObjectAnnotations annotations = null ,
6679
6679
bool receptacleCandidate = false ,
6680
- float yRotOffset = 0f
6680
+ float yRotOffset = 0f ,
6681
+ bool serializable = false
6681
6682
) {
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
6843
6699
) ;
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 ) ;
6852
6700
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 ) ;
6889
6702
}
6890
6703
6891
6704
public void CreateHouse ( ProceduralHouse house ) {
0 commit comments