Exemplo n.º 1
0
        public void RecursiveSearch(GameObject root)
        {
            if (room.IgnoreInactiveObjects &&
                !root.activeInHierarchy)
            {
                return;
            }

            // loop thorugh all the gameObjects components
            // to see what we can extract
            Component[] comps = root.GetComponents <Component>();
            if (!compExtractor.CanExport(comps))
            {
                compExtractor.Process(comps);
                return;
            }

            for (int i = 0; i < comps.Length; i++)
            {
                Component comp = comps[i];
                if (!comp)
                {
                    continue;
                }

                if (comp is MeshRenderer)
                {
                    MeshRenderer meshRen = (MeshRenderer)comp;
                    MeshFilter   filter  = comps.FirstOrDefault(c => c is MeshFilter) as MeshFilter;
                    if (filter == null)
                    {
                        continue;
                    }

                    Mesh mesh = filter.sharedMesh;
                    if (mesh == null ||
                        !room.CanExportObj(comps) ||
                        mesh.GetTopology(0) != MeshTopology.Triangles)
                    {
                        continue;
                    }

                    if (!room.ExportDynamicGameObjects &&
                        !root.isStatic)
                    {
                        continue;
                    }

                    sceneBounds.Encapsulate(meshRen.bounds);

                    // Only export the mesh if we never exported this one mesh
                    BruteForceMeshExportData exp;
                    if (!meshesToExport.TryGetValue(mesh, out exp))
                    {
                        exp      = new BruteForceMeshExportData();
                        exp.Mesh = mesh;
                        // generate name
                        string meshId = mesh.name;
                        if (string.IsNullOrEmpty(meshId) ||
                            meshNames.Contains(meshId))
                        {
                            meshId = "ExportedMesh" + meshNames.Count;
                        }
                        meshNames.Add(meshId);

                        // keep our version of the data
                        exp.MeshId = meshId;
                        meshesToExport.Add(mesh, exp);

                        // but also supply the data to the Janus Room so the Html can be built
                        AssetObject asset = new AssetObject();
                        exp.Asset = asset;

                        asset.id  = meshId;
                        asset.src = meshId + ".fbx";

                        room.AddAssetObject(asset);
                    }

                    // We are brute force, so all objects become Room Objects
                    RoomObject obj = new RoomObject();
                    obj.id = exp.Asset.id;
                    obj.SetUnityObj(root, room);
                    room.AddRoomObject(obj);
                    compExtractor.ProcessNewRoomObject(obj, comps);

                    // let the material scanner process this object
                    materialScanner.PreProcessObject(meshRen, mesh, exp.Asset, obj, true);
                }
            }

            // loop through all this GameObject children
            foreach (Transform child in root.transform)
            {
                RecursiveSearch(child.gameObject);
            }
        }
Exemplo n.º 2
0
        public void PreProcessObject(MeshRenderer renderer, Mesh mesh, AssetObject obj, RoomObject rObj, bool assignLightmapScale)
        {
            LightmapExportType  lightmapExportType = room.LightmapType;
            ExportTextureFormat format             = room.TextureFormat;

            if (lightmapExportType != LightmapExportType.None)
            {
                int lightMap = renderer.lightmapIndex;
                if (lightMap != -1)
                {
                    // Register mesh for lightmap render
                    List <RoomObject> toRender;
                    if (!lightmapped.TryGetValue(lightMap, out toRender))
                    {
                        toRender = new List <RoomObject>();
                        lightmapped.Add(lightMap, toRender);
                    }

                    toRender.Add(rObj);

                    if (assignLightmapScale)
                    {
                        if (lightmapExportType == LightmapExportType.Packed)
                        {
                            Vector4 lmap = renderer.lightmapScaleOffset;
                            lmap.x = Mathf.Clamp(lmap.x, -2, 2);
                            lmap.y = Mathf.Clamp(lmap.y, -2, 2);
                            lmap.z = Mathf.Clamp(lmap.z, -2, 2);
                            lmap.w = Mathf.Clamp(lmap.w, -2, 2);
                            rObj.SetLightmap(lmap);
                        }
                    }

                    if (lightmapExportType != LightmapExportType.BakedMaterial)
                    {
                        // check if we already have the texture
                        string     lmapId    = "Lightmap" + lightMap;
                        AssetImage lmapImage = room.TryGetTexture(lmapId);
                        if (lmapImage == null)
                        {
                            lmapImage     = new AssetImage();
                            lmapImage.id  = lmapId;
                            lmapImage.src = lmapId;
                            room.AddAssetImage(lmapImage);
                        }
                        rObj.lmap_id = lmapImage.id;
                    }
                }
            }

            switch (lightmapExportType)
            {
            case LightmapExportType.BakedMaterial:
            case LightmapExportType.Unpacked:
            {
                AssetImage image = new AssetImage();
                string     imgId = obj.id + "_Baked";

                image.id  = imgId;
                image.src = imgId;

                rObj.image_id = image.id;

                room.AddAssetImage(image);
            }
            break;
            }

            if (lightmapExportType != LightmapExportType.BakedMaterial &&
                room.ExportMaterials)
            {
                // search for textures/color on object
                Texture2D texture  = null;
                Color     objColor = Color.white;

                Material[] mats = renderer.sharedMaterials;
                for (int j = 0; j < mats.Length; j++)
                {
                    Material mat = mats[j];

                    Vector2 sca = mat.mainTextureScale;
                    Vector2 off = mat.mainTextureOffset;
                    if (sca != Vector2.one || off != Vector2.zero)
                    {
                        rObj.tiling = JanusUtil.FormatVector4(new Vector4(sca.x, sca.y, off.x, off.y));
                    }

                    Shader shader = mat.shader;
                    int    props  = ShaderUtil.GetPropertyCount(shader);
                    for (int k = 0; k < props; k++)
                    {
                        string name = ShaderUtil.GetPropertyName(shader, k);

                        ShaderUtil.ShaderPropertyType propType = ShaderUtil.GetPropertyType(shader, k);
                        if (propType == ShaderUtil.ShaderPropertyType.TexEnv)
                        {
                            if (JanusGlobals.SemanticsMainTex.Contains(name.ToLower()))
                            {
                                // main texture texture
                                Texture matTex = mat.GetTexture(name);
                                if (matTex is Texture2D)
                                {
                                    texture = (Texture2D)matTex;
                                }
                            }
                        }
                        else if (propType == ShaderUtil.ShaderPropertyType.Color)
                        {
                            if (JanusGlobals.SemanticsColor.Contains(name.ToLower()))
                            {
                                objColor = mat.GetColor(name);
                            }
                        }
                    }
                }

                rObj.col = JanusUtil.FormatColor(objColor);

                if (room.ExportTextures && texture != null)
                {
                    if (textureNames.Contains(texture.name))
                    {
                        AssetImage img = room.AssetImages.FirstOrDefault(c => c.Texture == texture);
                        if (img != null)
                        {
                            rObj.image_id = img.id;
                        }
                        return;
                    }
                    textureNames.Add(texture.name);

                    AssetImage image = new AssetImage();
                    image.Texture = texture;
                    image.id      = texture.name;
                    image.src     = texture.name;
                    rObj.image_id = image.id;
                    room.AddAssetImage(image);
                }
            }
        }
Exemplo n.º 3
0
        public void RecursiveSearch(GameObject root)
        {
            if (room.IgnoreInactiveObjects &&
                !root.activeInHierarchy)
            {
                return;
            }

            // loop thorugh all the gameObjects components
            // to see what we can extract
            Component[] comps = root.GetComponents <Component>();
            if (!compExtractor.CanExport(comps))
            {
                compExtractor.Process(comps);
                return;
            }

            for (int i = 0; i < comps.Length; i++)
            {
                Component comp = comps[i];
                if (!comp)
                {
                    continue;
                }

                if (comp is MeshRenderer)
                {
                    MeshRenderer meshRen = (MeshRenderer)comp;
                    MeshFilter   filter  = comps.FirstOrDefault(c => c is MeshFilter) as MeshFilter;
                    if (filter == null || !meshRen.isVisible)
                    {
                        continue;
                    }

                    Mesh mesh = filter.sharedMesh;
                    if (mesh == null ||
                        !room.CanExportObj(comps))
                    {
                        continue;
                    }

                    Material mat = meshRen.sharedMaterial;
                    if (!mat)
                    {
                        continue;
                    }

                    if (!room.ExportDynamicGameObjects &&
                        !root.isStatic)
                    {
                        continue;
                    }

                    sceneBounds.Encapsulate(meshRen.bounds);

                    PerMaterialMeshExportData data;
                    if (!meshesToExport.TryGetValue(mat, out data))
                    {
                        data = new PerMaterialMeshExportData();
                        meshesToExport.Add(mat, data);

                        AssetObject asset = new AssetObject();
                        data.Asset = asset;

                        asset.id  = mat.name;
                        asset.src = mat.name + ".fbx";
                        room.AddAssetObject(asset);

                        RoomObject obj = new RoomObject();
                        data.Object = obj;
                        obj.id      = mat.name;
                        obj.SetNoUnityObj(room);

                        room.AddRoomObject(obj);
                        compExtractor.ProcessNewRoomObject(obj, comps);
                    }

                    materialScanner.PreProcessObject(meshRen, mesh, data.Asset, data.Object, false);

                    PerMaterialMeshExportDataObj dObj = new PerMaterialMeshExportDataObj();
                    dObj.Mesh      = mesh;
                    dObj.Transform = root.transform;
                    dObj.Renderer  = meshRen;
                    data.Meshes.Add(dObj);
                }
            }

            // loop through all this GameObject children
            foreach (Transform child in root.transform)
            {
                RecursiveSearch(child.gameObject);
            }
        }
Exemplo n.º 4
0
 public void AddAssetObject(AssetObject assetObj)
 {
     AllAssets.Add(assetObj);
     AssetObjects.Add(assetObj);
 }