//public  static readonly  Dictionary<GameObject, EDclNodeType> GameObjectToNodeTypeDict = new Dictionary<GameObject, EDclNodeType>();


        public static ResourceRecorder TraverseAllScene(StringBuilder exportStr, SceneStatistics statistics,
                                                        SceneWarningRecorder warningRecorder)
        {
            var rootGameObjects = new List <GameObject>();

            for (int i = 0; i < SceneManager.sceneCount; i++)
            {
                var roots = SceneManager.GetSceneAt(i).GetRootGameObjects();
                rootGameObjects.AddRange(roots);
            }

            _sceneMeta = Object.FindObjectOfType <DclSceneMeta>();

            _resourceRecorder = new ResourceRecorder();

            //GameObjectToNodeTypeDict.Clear();

            //====== Start Traversing ======
            foreach (var rootGO in rootGameObjects)
            {
                RecursivelyTraverseTransform(rootGO.transform, exportStr, _resourceRecorder, 4, statistics,
                                             warningRecorder);
            }

            //Append PlayAudio functions
            if (exportStr != null)
            {
                if (_resourceRecorder.audioSourceAddFunctions.Count > 0)
                {
                    exportStr.AppendLine();
                    exportStr.AppendLine(
                        @"export class AutoPlayUnityAudio implements ISystem {
  activate() {");
                    foreach (var functionName in _resourceRecorder.audioSourceAddFunctions)
                    {
                        exportStr.AppendIndent(indentUnit, 2).AppendFormat("{0}()\n", functionName);
                    }
                    exportStr.AppendLine(
                        @"  }
}
engine.addSystem(new AutoPlayUnityAudio())
");
                }
            }

            if (statistics != null)
            {
                statistics.textureCount = _resourceRecorder.primitiveTexturesToExport.Count +
                                          _resourceRecorder.gltfTextures.Count;
            }

            return(_resourceRecorder);
        }
예제 #2
0
        //public  static readonly  Dictionary<GameObject, EDclNodeType> GameObjectToNodeTypeDict = new Dictionary<GameObject, EDclNodeType>();


        public static ResourceRecorder TraverseAllScene(StringBuilder exportStr, SceneStatistics statistics, SceneWarningRecorder warningRecorder)
        {
            var rootGameObjects = new List <GameObject>();

            for (int i = 0; i < SceneManager.sceneCount; i++)
            {
                var roots = SceneManager.GetSceneAt(i).GetRootGameObjects();
                rootGameObjects.AddRange(roots);
            }

            _sceneMeta = Object.FindObjectOfType <DclSceneMeta>();

            resourceRecorder = new ResourceRecorder();

            //GameObjectToNodeTypeDict.Clear();

            //====== Start Traversing ======
            foreach (var rootGO in rootGameObjects)
            {
                RecursivelyTraverseTransform(rootGO.transform, exportStr, resourceRecorder, 4, statistics, warningRecorder);
            }

            //Append PlayAudio functions
            if (exportStr != null)
            {
                if (resourceRecorder.audioSourceAddFunctions.Count > 0)
                {
                    exportStr.AppendLine();
                    exportStr.AppendLine("Input.instance.subscribe(\"BUTTON_UP\", e => {");
                    foreach (var functionName in resourceRecorder.audioSourceAddFunctions)
                    {
                        exportStr.AppendIndent(indentUnit, 1).AppendFormat("{0}()\n", functionName);
                    }
                    exportStr.AppendLine("})\n");
                }
            }

            if (statistics != null)
            {
                statistics.textureCount = resourceRecorder.primitiveTexturesToExport.Count + resourceRecorder.gltfTextures.Count;
            }

            return(resourceRecorder);
        }
예제 #3
0
        public static void ProcessShape(Transform tra, string entityName, StringBuilder exportStr, ResourceRecorder resourceRecorder, SceneStatistics statistics)
        {
            var meshFilter = tra.GetComponent <MeshFilter>();

            if (!(meshFilter && tra.GetComponent <MeshRenderer>()))
            {
                return;
            }

            var dclObject = tra.GetComponent <DclObject>();

            string shapeName = null;

            if (dclObject)
            {
                switch (dclObject.dclPrimitiveType)
                {
                case DclPrimitiveType.box:
                    dclObject.dclNodeType = EDclNodeType.box;
                    shapeName             = "BoxShape";
                    break;

                case DclPrimitiveType.sphere:
                    dclObject.dclNodeType = EDclNodeType.sphere;
                    shapeName             = "SphereShape";
                    break;

                case DclPrimitiveType.plane:
                    dclObject.dclNodeType = EDclNodeType.plane;
                    shapeName             = "PlaneShape";
                    break;

                case DclPrimitiveType.cylinder:
                    dclObject.dclNodeType = EDclNodeType.cylinder;
                    shapeName             = "CylinderShape";
                    break;

                case DclPrimitiveType.cone:
                    dclObject.dclNodeType = EDclNodeType.cone;
                    shapeName             = "ConeShape";
                    break;
                }
            }

            if (shapeName != null)
            {
                //Primitive
                if (exportStr != null)
                {
                    exportStr.AppendFormat(SetShape, entityName, shapeName);
                }
            }
            else
            {
                //gltf - root
                dclObject.dclNodeType = EDclNodeType.gltf;

                if (exportStr != null)
                {
                    string gltfPath = string.Format("./unity_assets/{0}.gltf", GetIdentityName(tra.gameObject));
                    exportStr.AppendFormat(SetGLTFshape, entityName, gltfPath);
                }

                //export as a glTF model
                if (resourceRecorder != null)
                {
                    resourceRecorder.meshesToExport.Add(tra.gameObject);
                }
            }

            if (exportStr != null)
            {
                if (dclObject && dclObject.withCollision)
                {
                    exportStr.AppendFormat("{0}.getComponent({1}).withCollisions = true\n", entityName, shapeName);
                }

                if (dclObject && !dclObject.visible)
                {
                    exportStr.AppendFormat("{0}.getComponent(Shape).visible = false\n", entityName);
                }
            }
        }
예제 #4
0
        public static void RecursivelyTraverseTransform(Transform tra, StringBuilder exportStr,
                                                        ResourceRecorder resourceRecorder, int indentLevel, SceneStatistics statistics, SceneWarningRecorder warningRecorder)
        {
            if (!tra.gameObject.activeInHierarchy)
            {
                return;
            }
            if (tra.gameObject.GetComponent <DclSceneMeta>())
            {
                return;                                             //skip .dcl
            }
            var dclObject = tra.GetComponent <DclObject>() ?? tra.gameObject.AddComponent <DclObject>();

            dclObject.dclNodeType = EDclNodeType.entity;

            var entityName = GetIdentityName(tra.gameObject);

            if (statistics != null)
            {
                statistics.entityCount += 1;
            }

            var position = tra.localPosition;
            var scale    = tra.localScale;
            var rotation = tra.localRotation;


            if (exportStr != null)
            {
                exportStr.AppendFormat(NewEntityWithName, entityName, tra.name);

                if (tra.parent)
                {
                    //Set Parent
                    exportStr.AppendFormat(SetParent, entityName, GetIdentityName(tra.parent.gameObject));
                }
                else
                {
                    //Entity
                    exportStr.AppendFormat(AddEntity, entityName);
                }

                //Transform
                exportStr.AppendFormat(SetTransform, entityName, position.x, position.y, position.z);
                exportStr.AppendFormat(SetRotation, entityName, rotation.x, rotation.y, rotation.z, rotation.w);
                exportStr.AppendFormat(SetScale, entityName, scale.x, scale.y, scale.z);
            }

            ProcessShape(tra, entityName, exportStr, resourceRecorder, statistics);

            if (exportStr != null && dclObject.dclNodeType == EDclNodeType.gltf) //reverse 180° along local y-axis because of DCL's special purpose.
            {
                rotation = Quaternion.AngleAxis(180, tra.up) * rotation;
                exportStr.AppendFormat(SetRotation, entityName, rotation.x, rotation.y, rotation.z, rotation.w);
            }

            if (dclObject.dclNodeType != EDclNodeType.gltf)
            {
                ProcessText(tra, entityName, exportStr, statistics);
            }

            if (dclObject.dclNodeType != EDclNodeType.gltf)
            {
                ProcessMaterial(tra, false, entityName, resourceRecorder.primitiveMaterialsToExport, exportStr, statistics);

                if (tra.GetComponent <MeshRenderer>())
                {
                    var meshFilter   = tra.GetComponent <MeshFilter>();
                    var meshRenderer = tra.GetComponent <MeshRenderer>();

                    //Statistics
                    if (statistics != null)
                    {
                        if (meshFilter && meshFilter.sharedMesh)
                        {
                            statistics.triangleCount += meshFilter.sharedMesh.triangles.LongLength / 3;
                            statistics.bodyCount     += 1;
                        }

                        var curHeight = meshRenderer.bounds.max.y;
                        if (curHeight > statistics.maxHeight)
                        {
                            statistics.maxHeight = curHeight;
                        }
                    }

                    //Warnings
                    if (warningRecorder != null)
                    {
                        //OutOfLand
                        if (_sceneMeta)
                        {
                            var isOutOfLand = false;
                            var startParcel = SceneUtil.GetParcelCoordinates(meshRenderer.bounds.min);
                            var endParcel   = SceneUtil.GetParcelCoordinates(meshRenderer.bounds.max);
                            for (int x = startParcel.x; x <= endParcel.x; x++)
                            {
                                for (int y = startParcel.y; y <= endParcel.y; y++)
                                {
                                    if (!_sceneMeta.parcels.Exists(parcel => parcel == new ParcelCoordinates(x, y)))
                                    {
                                        warningRecorder.OutOfLandWarnings.Add(new SceneWarningRecorder.OutOfLand(meshRenderer));
                                        isOutOfLand = true;
                                        break;
                                    }
                                }

                                if (isOutOfLand)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }

                if (exportStr != null)
                {
                    exportStr.Append('\n');
                }

                //        if (tra.GetComponent<DclCustomNode>())
                //        {
                //            var customNode = tra.GetComponent<DclCustomNode>();
                //            nodeName = customNode.nodeName;
                //            nodeType = EDclNodeType.CustomNode;

                //if(customNode.propertyPairs!=null){
                //	foreach (var propertyPair in customNode.propertyPairs)
                //	{
                //		extraProperties.AppendFormat(" {0}={1}", propertyPair.name, propertyPair.value);
                //	}
                //}
                //        }
                //        else
                //        {

                if (dclObject)
                {
                    //TODO: if (dclObject.visible != true) extraProperties.Append(" visible={false}");
                }

                //GameObjectToNodeTypeDict.Add(tra.gameObject, nodeType);

                foreach (Transform child in tra)
                {
                    RecursivelyTraverseTransform(child, exportStr, resourceRecorder, indentLevel + 1, statistics, warningRecorder);
                }
            }
            else
            {
                if (statistics != null)
                {
                    statistics.gltfMaterials.Clear();
                }
                RecursivelyTraverseIntoGLTF(tra, 0, statistics, warningRecorder);
                if (statistics != null)
                {
                    statistics.materialCount += statistics.gltfMaterials.Count;
                }
            }

            ProcessAudio(tra, entityName, exportStr);
        }