Quantcast
Channel: Latest Questions by Yanger_xy
Viewing all articles
Browse latest Browse all 61

how to assign texture on the terrain at runtime

$
0
0

I wanna to change terrain's texture when i hit a GUI button.the code below seems to doesn't work at all.how can i make it work?

ps:i have loaded 2 textures on the 'Paint Texture'. and i reference the page below link text, that's also my question. but now i am not very clear about it. hope u help me. thanks a lot!

    //Convert a Word Cordinate to a Terrain cordinate.
private Vector3 ConvertWordCor2TerrCor(Vector3 wordCor)
{
    Vector3 vecRet = new Vector3();
    Terrain ter = Terrain.activeTerrain;
    Vector3 terPosition =  ter.transform.position;
    vecRet.x = ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth;
    vecRet.z = ((wordCor.y - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight;
    /*
     * 

Is this right? or i should change it like this: vecRet.x *= ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth; vecRet.z *= ((wordCor.y - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight; * */ return vecRet; } void OnGUI() { if(GUI.Button(new Rect(0,0, 100, 100), "TextureDynamic") == true) { Terrain ter = Terrain.activeTerrain; TerrainData terData = ter.terrainData; if(terData == null) { print("terData is null"); return; }

        SplatPrototype splt = new SplatPrototype();
        splt.texture = (Texture2D)Resources.Load("jpg", typeof(Texture2D));
        splt.tileSize = new Vector2(0, 0);
        splt.tileOffset = new Vector2(2, 2);
        SplatPrototype splt2 = new SplatPrototype();
        splt2.texture = (Texture2D)Resources.Load("grass", typeof(Texture2D));
        splt2.tileOffset = new Vector2(0, 0);
        splt2.tileSize = new Vector2(5, 5);
        if(terData.splatPrototypes == null)
        {
            print("terData.spaltPrototypes is null");
        }
        print(terData.splatPrototypes.Length.ToString());
        terData.splatPrototypes[0] = splt;
        terData.splatPrototypes[1] = splt2;
        Vector3 vec = ConvertWordCor2TerrCor(new Vector3(0, 0,0));
        float[ , , ] splatmapData = terData.GetAlphamaps((int)vec.x, (int)vec.z, 200, 200);
        print(splatmapData.Length);
        splatmapData[0, 0, 0] = 0.0f;
        splatmapData[0, 0, 1] = 1.0f;
        terData.SetAlphamaps(0, 0, splatmapData);
        Terrain.activeTerrain.terrainData = terData;
    }
}

Viewing all articles
Browse latest Browse all 61

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>