Esempio n. 1
0
    //////////////////////////////////////////////////////////////////////////////
    //                                                                          //
    //Variables                                                                 //
    //                                                                          //
    //////////////////////////////////////////////////////////////////////////////



    //////////////////////////////////////////////////////////////////////////////
    //                                                                          //
    //Unity Functions                                                           //
    //                                                                          //
    //////////////////////////////////////////////////////////////////////////////
    // Use this for initialization
    void Start()
    {
        //Build info
        MM_INFO[] buidInfo      = new MM_INFO[] { MM_INFO.Ok };
        string[]  buildInfoFull = new string[] { "" };

        //MMdata
        MMData_SurfaceInfo      meshInfo    = new MMData_SurfaceInfo(MM_SURFACE_TYPE.Original, true, true, Color.green);
        MMData_MeshMainTexture  mainTexture = new MMData_MeshMainTexture(true, "_MainTex", MM_TEXTURE_SAMPLING_TYPE.FlatSmooth);
        MMData_MeshDisplace     dispalce    = new MMData_MeshDisplace(true, "_MainTex", MM_DISPLACE_READ_CHANNEL.Grayscale, 1.0f, 2.0f, MM_DISPLACE_SAVE_TYPE.DisplaceVertex, MM_SAVE_CHANNEL.RGB);
        MMData_AmbientOcclusion ao          = new MMData_AmbientOcclusion(true, MM_COLOR_SAMPLING_TYPE.Smooth, MM_SAVE_CHANNEL.RGB, Color.black, 64, 1.0f, 1.0f, 1.0f);


        //Generating flat mesh with displace and AO
        Mesh flatMesh = MMGenerator.MaterializeMesh(GetComponent <Renderer>(), ref buidInfo, ref buildInfoFull, meshInfo, mainTexture, dispalce, ao);

        //Print build info
        foreach (var str in buildInfoFull)
        {
            Debug.Log(str);
        }

        //Assign new mesh
        if (flatMesh != null)
        {
            GetComponent <MeshFilter>().mesh = flatMesh;
        }

        //Disabling texture inside material, rendering only vertex colors
        GetComponent <Renderer>().material.DisableKeyword("V_MM_TEXTURE_AND_COLOR_ON");
    }
Esempio n. 2
0
    //////////////////////////////////////////////////////////////////////////////
    //                                                                          //
    //Unity Functions                                                           //
    //                                                                          //
    //////////////////////////////////////////////////////////////////////////////
    void Start()
    {
        if (targetTerrain == null)
        {
            return;
        }



        //Will contain bake results
        //Need - array - as materializing terrain returns mesh array depending on chunks count described in 'terrainToMesh'
        Mesh[] newMesh = null;

        //Will contain baking reports, will help if something goes wrong
        MMGenerator.CONVERTION_INFO[] convertionInfo;

        //Same as above but with more detail info
        string[] convertionInfoString;



        //Terrain Materializer
        newMesh = MMGenerator.MaterializeTerrain(targetTerrain, out convertionInfo, out convertionInfoString, surfaceInfo, terrainToMesh, terrainTexture);

        //Check reports
        if (convertionInfoString != null)
        {
            for (int i = 0; i < convertionInfoString.Length; i++)
            {
                Debug.LogWarning(convertionInfoString[i]);
            }
        }


        //Successful conversation
        if (newMesh != null)
        {
            for (int i = 0; i < newMesh.Length; i++)
            {
                //Create new gameobject for each chunk
                GameObject chunk = new GameObject(newMesh[i].name);
                chunk.AddComponent <MeshFilter>().sharedMesh       = newMesh[i];
                chunk.AddComponent <MeshRenderer>().sharedMaterial = vertexColorMaterial;


                //Move to parent
                chunk.transform.parent        = this.gameObject.transform;
                chunk.transform.localPosition = Vector3.zero;
            }
        }
    }
Esempio n. 3
0
    //////////////////////////////////////////////////////////////////////////////
    //                                                                          //
    //Unity Functions                                                           //
    //                                                                          //
    //////////////////////////////////////////////////////////////////////////////
    void Start()
    {
        //First check if meshes inside 'meshfilterCollection' can be combined
        MMGenerator.COMBINE_INFO combineInfo;

        combineInfo = MMGenerator.CanBeCombined(meshfilterCollection);
        if (combineInfo != MMGenerator.COMBINE_INFO.OK)
        {
            //Houston we have a problem
            Debug.LogError(combineInfo.ToString());

            return;
        }



        //Will contain bake results
        Mesh newMesh = null;

        //Will contain baking reports, will help if something goes wrong
        MMGenerator.CONVERTION_INFO[] convertionInfo;

        //Same as above but with more detail info
        string[] convertionInfoString;



        //Mesh Group Materializer
        newMesh = MMGenerator.MaterializeMeshGroup(meshfilterCollection, out convertionInfo, out convertionInfoString, surfaceInfo, meshTextureAndColor);


        //Check reports
        if (convertionInfoString != null)
        {
            for (int i = 0; i < convertionInfoString.Length; i++)
            {
                if (convertionInfo[i] != MMGenerator.CONVERTION_INFO.Ok)
                {
                    Debug.LogWarning(convertionInfoString[i]);
                }
            }
        }


        //Successful conversation
        if (newMesh != null)
        {
            gameObject.AddComponent <MeshFilter>().sharedMesh       = newMesh;
            gameObject.AddComponent <MeshRenderer>().sharedMaterial = vertexColorMaterial;
        }
    }
Esempio n. 4
0
    Material SetupTerrainToMeshMaterial_Basemap()
    {
        Material newMaterial = new Material(Shader.Find("Legacy Shaders/Diffuse"));


        //Export basemaps (diffuse and normal)
        Texture2D basemapDiffuse = null;
        Texture2D basemapNormal  = null;

        MMGenerator.ExtractTerrainBasemap(targetTerrain, out basemapDiffuse, out basemapNormal, 1024, 1024);


        newMaterial.mainTexture = basemapDiffuse;

        return(newMaterial);
    }
Esempio n. 5
0
    // Use this for initialization
    void Start()
    {
        float time = Time.realtimeSinceStartup;

        //Cointins OBJ data
        string strOBJ = MMGenerator.TerrainToOBJ(terrain, vertexCountHorizontal, vertexCountVertical);

        Debug.Log("OBJ convertion in: " + (Time.realtimeSinceStartup - time) + " sec");



        //Create directory
        if (Directory.Exists(GetGenerateDirectory()) == false)
        {
            Directory.CreateDirectory(GetGenerateDirectory());
        }


        //File path
        string objFilePath = GetGenerateDirectory() + "/" + terrain.name + ".obj";


        //Save OBJ file
        StreamWriter sw = new StreamWriter(objFilePath);

        try
        {
            sw.WriteLine(strOBJ);
        }
        catch (System.Exception err)
        {
            Debug.LogError("Houston, we have a problem: " + err.Message);
        }
        sw.Close();



        //Refresh asset database
        AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);


        //Load OBJ mesh
        gameObject.AddComponent <MeshFilter>().sharedMesh       = (Mesh)AssetDatabase.LoadAssetAtPath(objFilePath, typeof(Mesh));
        gameObject.AddComponent <MeshRenderer>().sharedMaterial = new Material(Shader.Find("Diffuse"));
    }
Esempio n. 6
0
    //////////////////////////////////////////////////////////////////////////////
    //                                                                          //
    //Unity Functions                                                           //
    //                                                                          //
    //////////////////////////////////////////////////////////////////////////////
    void Start()
    {
        Mesh origianlMesh = null;

        //Get original mesh from MeshFilter or SkinnedMeshRenderer
        MeshFilter          meshFilter          = GetComponent <MeshFilter>();
        SkinnedMeshRenderer skinnedMeshRenderer = GetComponent <SkinnedMeshRenderer>();

        if (meshFilter != null)
        {
            origianlMesh = meshFilter.sharedMesh;
        }

        if (origianlMesh == null && skinnedMeshRenderer != null)
        {
            origianlMesh = skinnedMeshRenderer.sharedMesh;
        }


        //Oops, no mesh found
        if (origianlMesh == null)
        {
            return;
        }



        //Will contain bake results
        Mesh newMesh = null;

        //Will contain baking reports, will help if something goes wrong
        MMGenerator.CONVERTION_INFO[] convertionInfo;

        //Same as above but with more detail info
        string[] convertionInfoString;



        //Mesh Materializer
        newMesh = MMGenerator.MaterializeMesh(GetComponent <Renderer>(), out convertionInfo, out convertionInfoString, surfaceInfo, meshTextureAndColor, ambientOcclusion, indirectLighting, displace);


        //Check reports
        if (convertionInfoString != null)
        {
            for (int i = 0; i < convertionInfoString.Length; i++)
            {
                Debug.Log(convertionInfoString[i]);
            }
        }


        //Successful conversation
        if (newMesh != null)
        {
            //Replace old mesh with new one
            if (meshFilter != null)
            {
                meshFilter.sharedMesh = newMesh;
            }
            else if (skinnedMeshRenderer != null)
            {
                skinnedMeshRenderer.sharedMesh = newMesh;
            }


            //Replace material to make baked data visible
            GetComponent <Renderer>().sharedMaterials = new Material[] { vertexColorMaterial };
        }
    }
Esempio n. 7
0
    //////////////////////////////////////////////////////////////////////////////
    //                                                                          //
    //Custom Functions                                                          //
    //                                                                          //
    //////////////////////////////////////////////////////////////////////////////
    Material SetupTerrainToMeshMaterial_Splatmap()
    {
        Material newMaterial = null;


        //Export terrain splatmaps
        Texture2D[] splatMap = MMGenerator.ExtractTerrainSplatmaps(targetTerrain);
        if (splatMap == null || splatMap.Length == 0)
        {
            return(newMaterial);
        }


        //Export diffuse/normal textures
        Texture2D[] diffuseTextures;
        Texture2D[] normalTextures;
        Vector2[]   uvScale;
        Vector2[]   uvOffset;


        int usedTexturesCount = MMGenerator.ExtractTerrainTexturesInfo(targetTerrain, out diffuseTextures, out normalTextures, out uvScale, out uvOffset);

        if (usedTexturesCount == 0 || diffuseTextures == null)
        {
            //Problems with terrain
            Debug.LogWarning("usedTexturesCount == 0");

            return(newMaterial);
        }
        else if (usedTexturesCount == 1)
        {
            //There is no need to use TerrainToMesh shaders with one texture
            Shader shader = Shader.Find("Legacy Shaders/Diffuse");
            if (shader != null)
            {
                newMaterial = new Material(shader);

                //Texture
                newMaterial.mainTexture = diffuseTextures[0];

                //Scale & Offset
                newMaterial.mainTextureScale  = uvScale[0];
                newMaterial.mainTextureOffset = uvOffset[0];
            }

            return(newMaterial);
        }


        //Terrain To Mesh shaders support max 8 textures blend
        usedTexturesCount = Mathf.Clamp(usedTexturesCount, 2, 8);


        //Select proper shader - One Directional Light
        Shader ttmShader = Shader.Find(string.Format("VacuumShaders/Terrain To Mesh/One Directional Light/Diffuse/{0} Textures", usedTexturesCount));

        if (ttmShader == null)
        {
            Debug.LogWarning("Shader not found: " + string.Format("VacuumShaders/Terrain To Mesh/Standard/Diffuse/{0} Textures", usedTexturesCount));

            return(newMaterial);
        }


        //Select shader
        newMaterial = new Material(ttmShader);

        //Set up controll textures
        if (splatMap.Length == 1)
        {
            newMaterial.SetTexture("_V_T2M_Control", splatMap[0]);
        }
        else
        {
            if (splatMap.Length > 2)
            {
                Debug.Log("TerrainToMesh shaders support max 2 control textures. Current terrain uses " + splatMap.Length);
            }

            newMaterial.SetTexture("_V_T2M_Control", splatMap[0]);
            newMaterial.SetTexture("_V_T2M_Control2", splatMap[1]);
        }


        //Assign textures
        for (int i = 0; i < usedTexturesCount; i++)
        {
            //Texture
            newMaterial.SetTexture(string.Format("_V_T2M_Splat{0}", i + 1), diffuseTextures[i]);

            //Scale
            newMaterial.SetFloat(string.Format("_V_T2M_Splat{0}_uvScale", i + 1), uvScale[i].x);
        }


        return(newMaterial);
    }
Esempio n. 8
0
    Material SetupTerrainToMeshMaterial_Basemap()
    {
        //Export basemaps (diffuse and normal)
        Texture2D basemapDiffuse = null;
        Texture2D basemapNormal  = null;

        MMGenerator.ExtractTerrainBasemap(targetTerrain, out basemapDiffuse, out basemapNormal, 1024, 1024);


        Material newMaterial = null;

        if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset != null)
        {
            Shader srpShader = Shader.Find("Lightweight Render Pipeline/Lit");
            if (srpShader == null)
            {
                srpShader = Shader.Find("VacuumShaders/Terrain To Mesh/SRP Default");
            }

            newMaterial = new Material(srpShader);
        }

        else
        {
            newMaterial = new Material(Shader.Find(basemapNormal != null ? "Legacy Shaders/Bumped Diffuse" : "Legacy Shaders/Diffuse"));
        }


        if (newMaterial.HasProperty("_MainTex"))
        {
            newMaterial.SetTexture("_MainTex", basemapDiffuse);
        }
        if (newMaterial.HasProperty("_BaseMap"))
        {
            newMaterial.SetTexture("_BaseMap", basemapDiffuse);
        }

        if (basemapNormal != null)
        {
            if (newMaterial.HasProperty("_BumpMap"))
            {
                newMaterial.SetTexture("_BumpMap", basemapNormal);
            }
        }

        if (newMaterial.HasProperty("_BaseColor"))
        {
            newMaterial.SetColor("_BaseColor", Color.white);
        }
        if (newMaterial.HasProperty("_Color"))
        {
            newMaterial.SetColor("_Color", Color.white);
        }

        if (newMaterial.HasProperty("_Smoothness"))
        {
            newMaterial.SetFloat("_Smoothness", 1);
        }

        return(newMaterial);
    }
Esempio n. 9
0
    //////////////////////////////////////////////////////////////////////////////
    //                                                                          //
    //Custom Functions                                                          //
    //                                                                          //
    //////////////////////////////////////////////////////////////////////////////
    Material SetupTerrainToMeshMaterial_Splatmap()
    {
        Material newMaterial = null;


        //Export terrain splatmaps
        Texture2D[] splatMap = MMGenerator.ExtractTerrainSplatmaps(targetTerrain);
        if (splatMap == null || splatMap.Length == 0)
        {
            return(newMaterial);
        }


        //Export diffuse/normal textures
        Texture2D[] diffuseTextures;
        Texture2D[] normalTextures;
        Vector2[]   uvScale;
        Vector2[]   uvOffset;
        float[]     metalic;
        float[]     smoothness;

        int usedTexturesCount = MMGenerator.ExtractTerrainTexturesInfo(targetTerrain, out diffuseTextures, out normalTextures, out uvScale, out uvOffset, out metalic, out smoothness);

        if (usedTexturesCount == 0 || diffuseTextures == null)
        {
            //Problems with terrain
            Debug.LogWarning("usedTexturesCount == 0");

            return(newMaterial);
        }
        else if (usedTexturesCount == 1)
        {
            //There is no need to use TerrainToMesh shaders with one texture
            Shader shader = null;
            if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset != null)
            {
                shader = Shader.Find("Lightweight Render Pipeline/Lit");
            }
            else
            {
                shader = Shader.Find("Legacy Shaders/Diffuse");
            }
            if (shader != null)
            {
                newMaterial = new Material(shader);

                //Texture
                newMaterial.mainTexture = diffuseTextures[0];

                //Scale & Offset
                newMaterial.mainTextureScale  = uvScale[0];
                newMaterial.mainTextureOffset = uvOffset[0];
            }

            return(newMaterial);
        }


        //Terrain To Mesh shaders support max 8 textures blend
        usedTexturesCount = Mathf.Clamp(usedTexturesCount, 2, 8);


        //Select proper shader - One Directional Light
        string shaderName = string.Empty;

        if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset != null)
        {
            shaderName = string.Format("VacuumShaders/Terrain To Mesh/Lightweight Render Pipeline/Lit" + "/{0} Textures", usedTexturesCount);
        }
        else
        {
            if (usedTexturesCount < 5)
            {
                shaderName = string.Format("VacuumShaders/Terrain To Mesh/One Directional Light/Bumped/{0} Textures", usedTexturesCount);
            }
            else
            {
                shaderName = string.Format("VacuumShaders/Terrain To Mesh/One Directional Light/Diffuse/{0} Textures", usedTexturesCount);
            }
        }

        Shader ttmShader = Shader.Find(shaderName);

        if (ttmShader == null)
        {
            if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset != null)
            {
                Debug.LogWarning("Shader not found: '" + shaderName + "'.\nLightweight Render Pipeline shaders (http://u3d.as/1jFw) are not installed.\n");
            }
            else
            {
                Debug.LogWarning("Shader not found: " + shaderName);
            }

            return(newMaterial);
        }


        //Select shader
        newMaterial = new Material(ttmShader);

        //Set up controll textures
        if (splatMap.Length == 1)
        {
            newMaterial.SetTexture("_V_T2M_Control", splatMap[0]);
        }
        else
        {
            if (splatMap.Length > 2)
            {
                Debug.Log("TerrainToMesh shaders support max 2 control textures. Current terrain uses " + splatMap.Length);
            }

            newMaterial.SetTexture("_V_T2M_Control", splatMap[0]);
            newMaterial.SetTexture("_V_T2M_Control2", splatMap[1]);
        }


        //Assign textures
        for (int i = 0; i < usedTexturesCount; i++)
        {
            //Texture
            newMaterial.SetTexture(string.Format("_V_T2M_Splat{0}", i + 1), diffuseTextures[i]);

            //Scale
            newMaterial.SetFloat(string.Format("_V_T2M_Splat{0}_uvScale", i + 1), uvScale[i].x);
        }

        //Metalic & Gloss
        for (int i = 0; i < metalic.Length; i++)
        {
            newMaterial.SetFloat(string.Format("_V_T2M_Splat{0}_Metallic", i + 1), metalic[i]);
            newMaterial.SetFloat(string.Format("_V_T2M_Splat{0}_Glossiness", i + 1), smoothness[i]);
        }


        //Bumpmap
        for (int i = 0; i < normalTextures.Length; i++)
        {
            string propName = string.Format("_V_T2M_Splat{0}_bumpMap", i + 1);

            if (newMaterial.HasProperty(propName))
            {
                newMaterial.SetTexture(propName, normalTextures[i]);
            }
        }


        return(newMaterial);
    }