Exemple #1
0
        public override GameObject BuildScene(string name, bool saveToDisk = false)
        {
            try {
                if (_vertices.Length == 0 || _triangles.Length == 0)
                {
                    state = SceneLoadingStatus.eCancelled;
                    return(gameObject);
                }
                Mesh mesh = new Mesh();
                mesh.vertices  = _vertices;
                mesh.triangles = _triangles;
                if (_uvs != null && _uvs.Length != 0)
                {
                    mesh.uv = _uvs;
                }
                mesh.RecalculateNormals();
                mesh.RecalculateBounds();

                MeshFilter filter = gameObject.AddComponent <MeshFilter> ();
                filter.sharedMesh = mesh;
                MeshRenderer renderer = gameObject.AddComponent <MeshRenderer> ();
                renderer.sharedMaterial = ForgeLoaderEngine.GetDefaultMaterial();
                if (createCollider)
                {
                    MeshCollider collider = gameObject.AddComponent <MeshCollider>();
                    collider.sharedMesh = mesh;
                }
#if UNITY_EDITOR
                if (saveToDisk)
                {
                    AssetDatabase.CreateAsset(mesh, ForgeConstants._resourcesPath + this.loader.PROJECTID + "/" + name + ".asset");
                    //AssetDatabase.SaveAssets () ;
                    //AssetDatabase.Refresh () ;
                    //mesh =AssetDatabase.LoadAssetAtPath<Mesh> (ForgeConstants._resourcesPath + this.loader.PROJECTID + "/" + name + ".asset") ;
                }
#endif

                base.BuildScene(name, saveToDisk);
                state = SceneLoadingStatus.eWaitingMaterial;
            } catch (Exception ex) {
                Debug.Log(ForgeLoader.GetCurrentMethod() + " " + ex.Message);
                state = SceneLoadingStatus.eError;
            }
            return(gameObject);
        }
        protected Material CreateMaterialV1(StandardMaterial lmvMat)
        {
            // https://docs.unity3d.com/Manual/StandardShaderMetallicVsSpecular.html
            // Standard: The shader exposes a “metallic” value that states whether the material is metallic or not.
            // Standard (Specular setup): Choose this shader for the classic approach.
            Material mat = new Material(
                lmvMat.isMetal == true ?
                Shader.Find("Standard")
                                : Shader.Find("Standard (Specular setup)")
                );

            try {
                if (lmvMat.specular_tex != null)
                {
                    mat.EnableKeyword("_SPECULARHIGHLIGHTS_OFF");
                    mat.SetFloat("_SpecularHighlights", 0f);
                }
                //mat.DisableKeyword ("_SPECULARHIGHLIGHTS_OFF") ;
                //mat.SetFloat ("_SpecularHighlights", 1f) ;
                mat.EnableKeyword("_GLOSSYREFLECTIONS_OFF");
                mat.SetFloat("_GlossyReflections", 0f);

                var ambiant = lmvMat.ambient;
                if (ambiant != Color.clear)
                {
                    mat.SetColor("_Color", ambiant);
                }

                var diffuse = lmvMat.diffuse;
                if (diffuse != Color.clear)
                {
                    mat.SetColor("_Color", diffuse);
                }

                var emissive = lmvMat.emissive;
                if (emissive != Color.clear)
                {
                    mat.SetColor("_EmissionColor", emissive);
                }

                var specular = lmvMat.specular;
                if (specular != Color.clear &&
                    (
                        lmvMat.isMetal == true &&                         // In Unity3d, the texture would not show
                        lmvMat.diffuse_tex != null &&
                        specular != Color.white
                    )
                    )
                {
                    mat.SetColor("_SpecColor", specular);
                }

                var transparent = lmvMat.transparent;
                if (transparent)
                {
                    mat.SetFloat("_Mode", (float)BlendMode.Transparent);
                    mat.EnableKeyword("_ALPHABLEND_ON");
                    Color color = mat.GetColor("_Color");
                    color.a = lmvMat.transparency;
                    mat.SetColor("_Color", color);
                }

                // Create a new request to get the Textures
                if (lmvMat.diffuse_tex != null)
                {
                    //TextureRequest req =new TextureRequest (loader, null, mat, Texture.TextureType.Diffuse, lmvMat.material) ;
                    TextureRequest req = new TextureRequest(loader, null, bearer, mat, lmvMat.diffuse_tex, lmvMat.material);
                    if (fireRequestCallback != null)
                    {
                        fireRequestCallback(this, req);
                    }
                }
                if (lmvMat.specular_tex != null)
                {
                    TextureRequest req = new TextureRequest(loader, null, bearer, mat, lmvMat.specular_tex, lmvMat.material);
                    if (fireRequestCallback != null)
                    {
                        fireRequestCallback(this, req);
                    }
                }
                if (lmvMat.bump_tex != null)
                {
                    TextureRequest req = new TextureRequest(loader, null, bearer, mat, lmvMat.bump_tex, lmvMat.material);
                    if (fireRequestCallback != null)
                    {
                        fireRequestCallback(this, req);
                    }
                }
            } catch (System.Exception e) {
                Debug.Log("exception " + e.Message);
                mat = ForgeLoaderEngine.GetDefaultMaterial();
            }
            return(mat);
        }
        protected Material CreateMaterial(StandardMaterial lmvMat)
        {
            // https://docs.unity3d.com/Manual/StandardShaderMetallicVsSpecular.html
            // Standard: The shader exposes a “metallic” value that states whether the material is metallic or not.
            // Standard (Specular setup): Choose this shader for the classic approach.
            bool     isMetal = lmvMat.isMetal;
            Material mat     = new Material(isMetal ? Shader.Find("Standard") : Shader.Find("Standard (Specular setup)"));

            try {
                // Color properties
                Color color    = Color.clear;
                Color diffuse  = lmvMat.diffuse;
                Color ambient  = lmvMat.ambient;
                Color specular = lmvMat.specular;

                color = diffuse != Color.clear ? diffuse : ambient;

                if (color == Color.clear && isMetal)
                {
                    color = specular;
                }

                mat.SetColor("_Color", color);

                // Metallic properties
                if (isMetal)
                {
                    mat.SetFloat("_Metallic", 1);
                }

                //This glossines code is an arbitrary placeholder until the proper
                //material properties are added
                float glossiness = Mathf.Clamp(lmvMat.glossiness / 2000f, .01f, .99f);
                mat.SetFloat("_Glossiness", glossiness);
                mat.SetFloat("_Shininess", glossiness);

                if (specular != Color.white)
                {
                    mat.SetColor("_SpecColor", color);
                }

                var specularTexture = lmvMat.specular_tex;
                if (specularTexture != null)
                {
                    mat.EnableKeyword("_SPECULARHIGHLIGHTS_OFF");
                    mat.SetFloat("_SpecularHighlights", 0f);
                    mat.EnableKeyword("_GLOSSYREFLECTIONS_OFF");
                    mat.SetFloat("_GlossyReflections", 0f);
                }
                else
                {
                    mat.DisableKeyword("_SPECULARHIGHLIGHTS_OFF");
                    mat.SetFloat("_SpecularHighlights", 1f);
                    mat.DisableKeyword("_GLOSSYREFLECTIONS_OFF");
                    mat.SetFloat("_GlossyReflections", 1f);
                }

                // Emissive properties
                var emissive = lmvMat.emissive;
                if (emissive != Color.clear)
                {
                    mat.SetColor("_EmissionColor", emissive);
                }

                //var specular = lmvMat.specular;
                //if ( specular != Color.clear
                //	&& (
                //		   lmvMat.isMetal == true // In Unity3d, the texture would not show
                //		&& lmvMat.diffuse_tex != null
                //		&& specular != Color.white
                //	)
                //)
                //	mat.SetColor ("_SpecColor", specular);

                // Transparent properties
                var transparent = lmvMat.transparent;
                if (transparent)
                {
                    mat.SetFloat("_Mode", (float)BlendMode.Transparent);
                    mat.EnableKeyword("_ALPHABLEND_ON");
                    color.a = lmvMat.transparency;
                    mat.SetColor("_Color", color);
                }

                // Create a new request to get the Textures
                if (lmvMat.diffuse_tex != null)
                {
                    //TextureRequest req =new TextureRequest (loader, null, mat, Texture.TextureType.Diffuse, lmvMat.material) ;
                    TextureRequest req = new TextureRequest(loader, null, bearer, mat, lmvMat.diffuse_tex, lmvMat.material);
                    if (fireRequestCallback != null)
                    {
                        fireRequestCallback(this, req);
                    }
                }
                if (lmvMat.specular_tex != null)
                {
                    TextureRequest req = new TextureRequest(loader, null, bearer, mat, lmvMat.specular_tex, lmvMat.material);
                    if (fireRequestCallback != null)
                    {
                        fireRequestCallback(this, req);
                    }
                }
                if (lmvMat.bump_tex != null)
                {
                    TextureRequest req = new TextureRequest(loader, null, bearer, mat, lmvMat.bump_tex, lmvMat.material);
                    if (fireRequestCallback != null)
                    {
                        fireRequestCallback(this, req);
                    }
                }
            } catch (System.Exception e) {
                Debug.Log("exception " + e.Message);
                mat = ForgeLoaderEngine.GetDefaultMaterial();
            }
            return(mat);
        }
        public override GameObject BuildScene(string name, bool saveToDisk = false)
        {
            try {
                foreach (Eppy.Tuple <int, OpenCTM.Mesh> openctm in _openctm)
                {
                    OpenCTM.Mesh ctmMesh = openctm.Item2;
                    if (ctmMesh == null || ctmMesh.getVertexCount() == 0 || ctmMesh.getTriangleCount() == 0)
                    {
                        state = SceneLoadingStatus.eCancelled;
                        return(gameObject);
                    }

                    Eppy.Tuple <int, int, GameObject, JSONNode> item = fragments.Single(x => x.Item1 == openctm.Item1);
                    GameObject meshObject = item.Item3;

                    Mesh       mesh     = new Mesh();
                    Vector3 [] vertices = MeshRequest2.getAsVector3(ctmMesh.vertices);
                    mesh.vertices  = vertices;
                    mesh.triangles = ctmMesh.indices;
                    if (ctmMesh.hasNormals())
                    {
                        mesh.normals = MeshRequest2.getAsVector3(ctmMesh.normals);
                    }
                    for (int i = 0; i < ctmMesh.getUVCount(); i++)
                    {
                        mesh.SetUVs(i, MeshRequest2.getAsVector2List(ctmMesh.texcoordinates [i].values));
                    }

                    mesh.RecalculateNormals();
                    mesh.RecalculateBounds();

                    MeshFilter filter = meshObject.AddComponent <MeshFilter> ();
                    filter.sharedMesh = mesh;
                    MeshRenderer renderer = meshObject.AddComponent <MeshRenderer> ();
                    renderer.sharedMaterial = ForgeLoaderEngine.GetDefaultMaterial();
                    if (createCollider)
                    {
                        MeshCollider collider = meshObject.AddComponent <MeshCollider> ();
                        collider.sharedMesh = mesh;
                    }
#if UNITY_EDITOR
                    if (saveToDisk)
                    {
                        AssetDatabase.CreateAsset(mesh, ForgeConstants._resourcesPath + this.loader.PROJECTID + "/" + GetName(item.Item1) + ".asset");
                        //AssetDatabase.SaveAssets () ;
                        //AssetDatabase.Refresh () ;
                        //mesh =AssetDatabase.LoadAssetAtPath<Mesh> (ForgeConstants._resourcesPath + this.loader.PROJECTID + "/" + name + ".asset") ;
                    }
#endif

                    // Add our material to the queue
                    loader.GetMgr()._materials.Add(item.Item2);
                    //if ( loader.GetMgr ()._materials.Add (item.Item2) ) {
                    //	MaterialRequest req = new MaterialRequest (loader, null, bearer, item.Item2, item.Item4);
                    //	req.gameObject = meshObject;
                    //	if ( fireRequestCallback != null )
                    //		fireRequestCallback (this, req);
                    //}
                }
                base.BuildScene(name, saveToDisk);
                state = SceneLoadingStatus.eWaitingMaterial;
            } catch (Exception ex) {
                Debug.Log(ForgeLoader.GetCurrentMethod() + " " + ex.Message);
                state = SceneLoadingStatus.eError;
            }
            return(gameObject);
        }