[Unity] unity地形之splatalpha研究 地形贴图导出更换与绘制

查看:2679 |回复:1 | 2021-7-2 22:30:39

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 _Mr_L 于 2021-7-3 15:35 编辑

unity中的地图贴图的绘制常常使用的是paint texture里面的

但是这个方式往往费时很多,却只能做出很少的效果,这里要介绍的就是通过外部绘制splatalpha

来替换,达到unity中地形更强的效果

使用软件基本有worldmachine,ps,unity
image.png
image.png







地形一般的流程通常是创建了地形,先刷高度,然后再进行贴图的绘制,

可以看到保存文件之后,project中会生成一个terrain文件,并且可以点开




里面通常会至少有一个splatalpha,这个存储的就是你画贴图的位置

其实这个地形贴图相当于组合而成的四个mask,分别表示了4种贴图所占的位置以及混合程度

每4种会产生一个新的splatalpha,这其实一个texture格式,包含了4个通道,红绿蓝和alpha通道



然后大家可能发现这些图片是不能导出也不能在外部修改的,

如果是外部软件比如worldmachine生成的就只能当成一张大的texture贴上去,会造成很大的性能浪费,

而且不能当场mask使用新的更细致的纹理

所以这里给出一种新的方式,当然也是worldmachine官网的方式

可以替换splatalpha达到使用外部生成的地形纹理mask的目的

https://www.world-machine.com/learn.php?page=workflow&workflow=wfunity

下面是我组合好了官网的脚本ReplaceSplatmap.js


import System.IO;
//combine by shenmifangke  
class ReplaceSplatmap extends ScriptableWizard
{
var Splatmap: Texture2D;
var New : Texture2D;
var FlipVertical : boolean;

function OnWizardUpdate(){
        helpString = "Replace the existing splatmap of your terrain with a new one.\nDrag the embedded splatmap texture of your terrain to the 'Splatmap box'.\nThen drag the replacement splatmap texture to the 'New' box\nThen hit 'Replace'.";
        isValid = (Splatmap != null) && (New != null);
    }
       
function OnWizardCreate () {

           if (New.format != TextureFormat.ARGB32 && New.format != TextureFormat.RGB24) {
                EditorUtility.DisplayDialog("Wrong format", "Splatmap must be converted to ARGB 32 bit format.\nMake sure the type is Advanced and set the format!", "Cancel");
                return;
        }
       
        var w = New.width;
        if (Mathf.ClosestPowerOfTwo(w) != w) {
                EditorUtility.DisplayDialog("Wrong size", "Splatmap width and height must be a power of two!", "Cancel");
                return;       
        }  

    try {
            var pixels = New.GetPixels();       
                if (FlipVertical) {
                        var h = w; // always square in unity
                        for (var y = 0; y < h/2; y++) {
                                var otherY = h - y - 1;       
                                for (var x  = 0; x < w; x++) {
                                        var swapval = pixels[y*w + x];                                       
                                        pixels[y*w + x] = pixels[otherY*w + x];
                                        pixels[otherY*w + x] = swapval;
                                }               
                        }
                }
                Splatmap.Resize (New.width, New.height, New.format, true);
                Splatmap.SetPixels (pixels);
                Splatmap.Apply();
    }
    catch (err) {
                EditorUtility.DisplayDialog("Not readable", "The 'New' splatmap must be readable. Make sure the type is Advanced and enable read/write and try again!", "Cancel");
                return;
        }                       
}
//http://blog.csdn.net/shenmifangke
@MenuItem("Terrain/Replace Splatmap...")
static function Replace (){
    ScriptableWizard.DisplayWizard(
        "ReplaceSplatmap", ReplaceSplatmap, "Replace");
}


@MenuItem("Terrain/Export Texture")
    static function Apply()
{
    var texture : Texture2D = Selection.activeObject as Texture2D;
    if (texture == null)
    {
        EditorUtility.DisplayDialog("Select Texture", "You Must Select a Texture first!", "Ok");
        return;
    }

    var bytes = texture.EncodeToPNG();
    File.WriteAllBytes(Application.dataPath + "/exported_texture.png", bytes);
}
}
放在Editor目录下,就会发现多出来terrain菜单里面两项
第一个是替换splatalpha,第二个是把splatalpha贴图导出成png图片,会保存在工程目录assets里
————————————————
版权声明:本文为CSDN博主「神米米」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/shenmifangke/article/details/52638716
image.png
2021-7-2 22:30:39  
 赞 赞 0

使用道具 登录

1个回答,把该问题分享到群,邀请大神一起回答。
2#
好用
回复 收起回复
2025-2-19 00:45:26   回复
 赞 赞 0

使用道具 登录

CG 游戏行业专业问题

图文教程技术文章技术文库手机游戏引擎手游引擎
显示全部 9
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表