Unity Terrain splatalpha Research Terrain Map export Replace and draw

Source: Internet
Author: User
Tags readable

Map maps in unity are often drawn using the texture inside the paint

But this approach often takes a lot of time, but can only make very little effect, here is to introduce through the external drawing Splatalpha

To replace it to achieve greater terrain in unity.

Basic use of software is worldmachine,ps,unity




The usual process of terrain is to create the terrain, brush the height first, and then paint the map,

You can see that after you save the file, a terrain file is generated in project and you can click

It usually has at least one splatalpha, which is where you draw the map.

In fact, this terrain map is equivalent to a combination of four masks, representing the position of 4 kinds of maps and the degree of mixing

Each of the 4 kinds will produce a new splatalpha, which is actually a texture format that contains 4 channels, red-green-blue and alpha-channel


Then you may find that these images are not exported and cannot be modified externally.

If external software such as worldmachine generated can only be as a large texture paste up, will cause a lot of performance waste,

And you can't use a new, more granular texture on the spot.

So here's a new way, and of course the Worldmachine official website.

Can replace splatalpha to achieve the purpose of using an externally generated terrain texture mask

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

Here is the script that I assembled the official website 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 E mbedded Splatmap texture of your terrain to the ' splatmap box '. \nthen Drag the replacement splatmap texture to the ' New ' B
        Ox\nthen hit ' Replace '. ";
    IsValid = (Splatmap! = null) && (New! = null); } function Onwizardcreate () {if (New.format! = Textureformat.argb32 && New.format! = Textureformat.rgb  {editorutility.displaydialog ("wrong format", "Splatmap must be converted to ARGB-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 PO 
		Wer 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; + +) {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 is readable. 
		Make sure the type was advanced and enable Read/write and try again! "," Cancel ");
	Return }}//http://blog.csdn.net/shenmifangke @MenuItem ("Terrain/replace splatmap ...") static function Replace () {Script
Ablewizard.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); }
}
In the editor directory, you will find more than two of the terrain menu

The first one is to replace Splatalpha, and the second is to export the Splatalpha map to a PNG image, which will be saved in the project directory assets

These two are extremely useful terrain extension scripts

The following are specific ways of speaking and the process of my research


Let's start with the second export Aplatalpha.

Export PNG, where PNG is not the normal PNG, but with four channels "full" PNG, similar to the 32-bit TGA storage of transparent channels and other formats

We usually use image processing software such as PS and so on to deal with the picture, but it has a problem, that is, when you open this PNG

The alpha channel will be treated as a transparent channel, resulting in loss of information in the color channel.

For example, the following image, directly see only half of the triangle (this picture is processed with 4 full channels of PNG terrain export PNG will also export similar PNG)


You can save it and open it in PS, but again, the color information on the left half is missing,

But I can tell you actually the left triangle is color information, but PS there is no way to display PNG as 4 channels, so only the last non-RGB channel to identify the transparent channel (what, and PS can not do things 0 0, actually, but this can be solved with some PS plugin)

This is only PS and other commonly used software can not separate four channels of PNG, other software is possible

such as unity itself, Ae,nuke, etc., the following is the separation of color and transparent channels with nuke processing


Or here's a confirmation test, with Openframeworks.

#include "ofApp.h" ofimage temp;
Ofimage Temp2;
--------------------------------------------------------------void Ofapp::setup () {temp.loadimage ("a.png");
Temp2.loadimage ("A.png");

Ofpixels pixels = temp.getpixelsref ();
Ofpixels Noalpha;
Noalpha.allocate (Temp.getwidth (), Temp.getheight (), of_image_color);
Noalpha.setchannel (0,pixels.getchannel (0));
Noalpha.setchannel (1,pixels.getchannel (1));

Noalpha.setchannel (2,pixels.getchannel (2));
Temp.setfrompixels (Noalpha);
}//--------------------------------------------------------------void Ofapp::update () {} void Ofapp::d raw () {
Temp.draw (0,0);
Temp2.draw (512,0); } void ofapp::keypressed (int key) {} void ofapp::keyreleased (int key) {} void ofapp::mousemoved (int x, int y) {} void Ofapp: : mousedragged (int x, int y, int button) {} void ofapp::mousepressed (int x, int y, int button) {} void ofapp::mousereleased (i NT x, int y, int button) {} void ofapp::windowresized (int w, int h) {} void Ofapp::gotmessage (Ofmessage msg) {} void Ofapp::d Ragevent (OFDRAginfo draginfo) {}
 


You can see that the transparent part of PNG does have RGB information


and then replace Splatalpha .

New image as Splatalpha remember to set in unity as follows, otherwise you cannot successfully replace



After replacing the picture with the first one in the Terrain menu, you'll see that it seems to be successful (4 textures are added here)



However, if you draw a pen, you will find the effect of a block, which is not what we want.


In fact, this problem is caused by your PNG, generally exported from Unity PNG will not have this problem,

So in this analysis, we find that Splatalpha has other qualifications.

That's all the channels. The white brightness of each pixel must be 1, and if not, this problem will occur.

Because the terrain you draw a map, the brightness on that layer (that is, the corresponding RGBA value) will increase, but the other layers will be correspondingly reduced, the sum remains unchanged, so we must find a way to the value of all channels and not more than 1 (insufficient will be black) beyond the words even if the transparent gradient brush, The brush will also be a block effect.

After the correction, the effect will be the following (ignoring the Black Place bar), can be further processed in unity



Of course, externally processed terrain maps are best stored in a format such as TGA to prevent people from knowing this

TGA should also be aware of the 32-bit and check the alpha channel.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.