Example #1
0
    public bool loadFile(string filePath, bool editor, UnityEngine.Object prefab)
    {
        if (editor)
        {
            loadedObject.Clear();
        }

        m_PartsCount = 0;
        m_PolyCount  = 0;

        progressStatus = "Initializing PiXYZ";
        progress       = 0.0f;

        try
        {
            errorMsg = "";
            PiXYZ4UnityWrapper.setResourcesFolder(Application.dataPath + "\\PiXYZ\\Resources\\");
            PiXYZ4UnityWrapper.initialize();

            progressStatus = "Importing file in PiXYZ";
            progress      += 0.05f;
            int assembly = -1;
            m_PartsToLoadCount = PiXYZ4UnityWrapper.importFile(filePath, out assembly);

            progress = 0.5f;

            materials = new Dictionary <int, Material>();

            PiXYZ4UnityWrapper.ScenePath root = null;
            if (assembly > 0)
            {
                int[] path = new int[2];
                path[0] = PiXYZ4UnityWrapper.getSceneRoot();
                path[1] = assembly;
                root    = new PiXYZ4UnityWrapper.ScenePath(path);
            }
            else
            {
                root = new PiXYZ4UnityWrapper.ScenePath(PiXYZ4UnityWrapper.getSceneRoot());
            }
            progressStatus = "Importing into Unity";

            //PiXYZ4UnityWrapper.removeSymmetryMatrices(root.node());  //used to fix odd negative scale
            Dictionary <int, List <Renderer> > renderers      = new Dictionary <int, List <Renderer> >();
            Dictionary <int, double>           lodToThreshold = new Dictionary <int, double>();
            loadSubTree(null, root, 0, editor, prefab, false, -1, ref renderers, ref lodToThreshold);

            progress       = 1.0f;
            progressStatus = "Finalizing";
        }
        catch (Exception e)
        {
            PiXYZ4UnityWrapper.clear();
            Debug.LogException(e);
            return(false);
        }

        PiXYZ4UnityWrapper.clear();
        return(true);
    }
Example #2
0
    public IEnumerator loadFileRuntime(GameObject rootObject, string filePath, bool editor, UnityEngine.Object prefab)
    {
        if (editor)
        {
            loadedObject.Clear();
        }

        int assembly = -1;

        PiXYZ4UnityWrapper.setResourcesFolder(Application.dataPath + "/PiXYZ/Resources/");
        Thread _thread = new Thread(() => importThread(filePath, out assembly));

        m_PartsCount = 0;
        m_PolyCount  = 0;

        _thread.Start();

        while (_thread.IsAlive)
        {
            progress       = progress >= 0.05f ? 0.05f + (float)(PiXYZ4UnityWrapper.getProgress() * 0.45f) : progress;
            progressStatus = PiXYZ4UnityWrapper.getProgressStatus();
            yield return(null);
        }

        if (getErrorMessage() != "")
        {
            yield break;
        }

        materials = new Dictionary <int, Material>();

        PiXYZ4UnityWrapper.ScenePath root = null;
        if (assembly > 0)
        {
            int[] path = new int[2];
            path[0] = PiXYZ4UnityWrapper.getSceneRoot();
            path[1] = assembly;
            root    = new PiXYZ4UnityWrapper.ScenePath(path);
        }
        else
        {
            root = new PiXYZ4UnityWrapper.ScenePath(PiXYZ4UnityWrapper.getSceneRoot());
        }

        //PiXYZ4UnityWrapper.removeSymmetryMatrices(root.node());  //used to fix odd negative scale
        Dictionary <int, List <Renderer> > renderers      = new Dictionary <int, List <Renderer> >();
        Dictionary <int, double>           lodToThreshold = new Dictionary <int, double>();

        loadSubTree(null, root, 0, editor, prefab, false, -1, ref renderers, ref lodToThreshold);

        foreach (KeyValuePair <int, Material> kvpair in PiXYZ4UnityWrapper.getCreatedMaterials())
        {
            if (!materials.ContainsKey(kvpair.Key))
            {
                materials.Add(kvpair.Key, kvpair.Value);
            }
        }
        PiXYZ4UnityWrapper.clear();
    }
Example #3
0
    void loadSubTree(GameObject parent, PiXYZ4UnityWrapper.ScenePath subTree, int currentMateriaId, bool editor, UnityEngine.Object prefab, bool importLines, int curLOD, ref Dictionary <int, List <Renderer> > lodToRenderers, ref Dictionary <int, double> lodToThreshold, bool isMaterialOverride = false)
    {
        if (!isPart(subTree.node()) && !isAssembly(subTree.node()) || isHidden(subTree)) //Last part is HOOPS workaround.
        {
            return;                                                                      //no light or camera
        }
        GameObject obj = new GameObject();

        Transform parentTransform = parent != null ? parent.transform : null;

        obj.name             = PiXYZ4UnityWrapper.getNodeName(subTree.node());
        obj.transform.parent = parentTransform;
        Matrix4x4 matrix = PiXYZ4UnityWrapper.getLocalMatrix(subTree.node());

        int parentInstance = subTree.parentInstance();

        if (parentInstance > 0)
        {
            Matrix4x4 parentMatrix = PiXYZ4UnityWrapper.getLocalMatrix(parentInstance);
            matrix = parentMatrix * matrix;
        }
        SetTransformFromMatrix(obj.transform, ref matrix);
        if (parent == null)  //apply mirror and rotation once
        {
            lastImportedObject        = obj;
            obj.transform.localScale *= _scale;
            if (_mirrorX)
            {
                obj.transform.localScale = new Vector3(-obj.transform.localScale.x, obj.transform.localScale.y, obj.transform.localScale.z);
            }
            if (_rotation.eulerAngles.sqrMagnitude > 0)
            {
                obj.transform.localRotation = _rotation;
            }
        }

        if (!isMaterialOverride || isAssembly(subTree.node()) || currentMateriaId == 0)
        {
            int materialId = PiXYZ4UnityWrapper.getOccurrenceMaterial(subTree);
            if (materialId > 0)
            {
                currentMateriaId = materialId;
            }
        }

        int  lodCount   = 0;
        bool isLodGroup = PiXYZ4UnityWrapper.isLODGroup(subTree.node(), out lodCount);

        if (_lodsMode != LODsMode.NONE)
        {
            if (isLodGroup)
            {
                lodToRenderers.Clear();
                lodToThreshold.Clear();
            }

            if (PiXYZ4UnityWrapper.isLOD(subTree.node()))
            {
                double threshold;
                curLOD = PiXYZ4UnityWrapper.getLODNumber(subTree.node(), out threshold);
                if (!lodToThreshold.ContainsKey(curLOD))
                {
                    lodToThreshold.Add(curLOD, threshold);
                }
            }
        }
        if (isPart(subTree.node()))
        {
            loadPart(obj, subTree.node(), currentMateriaId, editor, prefab, importLines, curLOD, ref lodToRenderers);
        }
        else if (isAssembly(subTree.node()))
        {
            foreach (PiXYZ4UnityWrapper.ScenePath child in PiXYZ4UnityWrapper.getChildren(subTree))
            {
                try
                {
                    loadSubTree(obj, child, currentMateriaId, editor, prefab, importLines, curLOD, ref lodToRenderers, ref lodToThreshold, isMaterialOverride || isAssembly(subTree.node()));
                }
                catch (KeyNotFoundException knf)
                {
                    Debug.LogError("Key not found: " + knf.ToString());
                }
            }
        }

        if (_lodsMode != LODsMode.NONE && isLodGroup)
        {
            LOD[]        lods         = new LOD[lodCount];
            const double e            = 0.00001;
            double       minThreshold = 1;
            for (int iLOD = 0; iLOD < lodCount; ++iLOD)
            {
                double threshold = Math.Min(lodToThreshold[iLOD], minThreshold);
                if (!lodToRenderers.ContainsKey(iLOD))
                {
                    Debug.LogError("Key '" + iLOD + "' not found.");
                    continue;
                }
                Renderer[] renderers = new Renderer[lodToRenderers[iLOD].Count];
                for (int iRenderer = 0; iRenderer < lodToRenderers[iLOD].Count; ++iRenderer)
                {
                    renderers[iRenderer] = lodToRenderers[iLOD][iRenderer];
                }
                lods.SetValue(new LOD((float)threshold, renderers), iLOD);
                minThreshold = threshold - e;
            }
            LODGroup lodGroup = obj.AddComponent <LODGroup>();
            lodGroup.SetLODs(lods);
            lodToRenderers.Clear();
            lodToThreshold.Clear();
        }
    }
Example #4
0
 //HOOPS workaround
 bool isHidden(PiXYZ4UnityWrapper.ScenePath subTree)
 {
     return(PiXYZ4UnityWrapper.isHidden(subTree));
 }