U3D Honey Hex Framework Code interpretation record (ii)

Source: Internet
Author: User

file:worldoven.cs///<summary>///If Baking were requested this function would handle starting all required Proce SSEs and default settings required for initial pass///</summary>///<returns></returns>void Update () { If there is a dirtychunk, start baking, if it is already baked, skip, avoid repeating baking//previous World Object Loop Call function Preparechunkdata, add 9 chunk to Dirtychunks,        So here Dirtychunks has 9 if (Dirtychunks.count > 0 && baking = = false) {baking = true;        Setting up the baking camera bakingcamera.orthographicsize = Chunk.chunksizeinworld * 0.5f;        Bakingcamera.aspect = 1.0f; Quadbase is the prototype of the Hex Gameobject, which is Worldoven's child node, that is quadBase.transform.parent = Worldoven.transform quadbase.transform        . Localscale = new Vector3 (Hex.hextexturescale * 2.0f, Hex.hextexturescale * 2.0f, Hex.hextexturescale * 2.0f);        Draw the processing of the river and temporarily skip gameobject root = Gameobject.find ("Riverroot"); foreach (Transform t in Root.transform) {Meshrenderer Mr = T.getcomponent<meshrenDerer> ();            if (Mr! = NULL) {Riversections.add (MR);        }} root = Gameobject.find ("Riversmoothener");            foreach (Transform t in Root.transform) {Meshrenderer Mr = T.getcomponent<meshrenderer> ();            if (Mr! = NULL) {Riversmoothenersections.add (MR);    }}//Start baking startcoroutine ("baking"); }}///<summary>///most complex functionality which ensures all stages of baking take place in order and that they a Llow World to update form time to time instead of freezing everything.///</summary>///<returns></returns& Gt;ienumerator Baking () {///Croutinhelper is a helper function that is used primarily to determine if the co-execution time is too long, if it is too long, the execution is paused, the next frame continues to execute//This function is long,    So this time only see Preparationstage,bakingmixerstage and bakingheightstage three stages Coroutinehelper.starttimer (); while (Dirtychunks.count > 0) {//setup chunk scene in order required by blending PreparatioNstage (); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer (); }//Mixer//this Texture later defines how different hexes blend per other.        It bakes highest domination value taking into account distance from Hex center and mixer texture. Uses "Blending Max" which produces result by comparing previous color value with new result from shader://r = R1 > R2?         R1:R2; G = G1 > G2?        G1:G2; B = B1 > B2?         B1:B2; A = A1 > A2?        A1:A2;        Bakingmixerstage (); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer (); }//Height//defines how high terrain is. At the this stage we blend using Mixer textures and previously prepared sum of mixers. By comparing both we know what strong our hex is within this point//Height is written to R, while writting strengh T is based on A Channel//Camera orthoGraphicsize is half the height of the view, so multiply here by 0.5 bakingcamera.orthographicsize = Chunk.chunksizeinworld * 0.5f;        Bakingheightstage ();        Blurtexture (HEIGHTRT, 1, 1, 1); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer (); }//Offset heights is baked using the same settings//small Offset allows with comparison find shadow Bor        DERs ensuring its sharpness//big offset ensures shadow body and coverages in irregular terrain and data artifacts  bakingCamera.transform.localPosition = lightsourcedirection.normalized * 0.15f;        Small offset for shadow detail Baketo (ref heightRTOffset1, 1);        Blurtexture (HeightRTOffset1, 1, 1, 1); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer (); } bakingCamera.transform.localPosition = lightsourcedirection.normalized * 0.3F;        Higher offset to get shadow body Baketo (ref heightRTOffset2, 1); Blurtexture (HEIGHTRTOffset2, 1, 1, 1);        BakingCamera.transform.localPosition = new Vector3 (0.0f, 0.0f, 0.0f); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer ();        } SHADOWSANDHEIGHTRT = Produceshadowsandheighttexture (HEIGHTRT, HeightRTOffset1, HeightRTOffset2); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer ();            }//Bake diffuse foreach (Meshrenderer Mr in Hexoutlinecollection) {            Vector3 pos = mr.transform.localPosition;            Pos.z = 5;        Mr.transform.localPosition = pos;        } reorderhexesplacingwaterontop (); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer ();        } bakingdiffusestage (); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer (); }//turn off everything from Camera view foreach (Meshrenderer Mr. in quadCollection) {if (Mr. Getcomponent<renderer> (). Material! = null) {Gameobject.destroy (Mr. Getcomponent<renderer> (). material);        } mr.gameObject.SetActive (FALSE);        } foreach (Meshrenderer Mr in Hexoutlinecollection) {mr.gameObject.SetActive (false);         }//Copy Height to Texture2d (ARGB) because we cant render directly to Alpha8.        Texture2d texture;        Rendertexture.active = SHADOWSANDHEIGHTRT;        Texture = new Texture2d (chunk.texturesize >> 1, chunk.texturesize >> 1, Textureformat.argb32, false);        Texture.wrapmode = Texturewrapmode.clamp; Texture.        Readpixels (New Rect (0, 0, chunk.texturesize >> 1, chunk.texturesize >> 1), 0, 0); Texture.                        Apply (); Convert height to Alpha8, its reasonably cheap and good even uncompressed format. Not compressed format saves us form artifacts near shaped water borders and mountain tops if (COroutinehelper.checkifpassed (()) {yield return null; Coroutinehelper.starttimer (); } texture2d Gscale = new Texture2d (chunk.texturesize >> 1, chunk.texturesize >> 1, textureformat.alpha8        , false);        Gscale.wrapmode = Texturewrapmode.clamp; color32[] data = texture.        GetPixels32 ();        GSCALE.SETPIXELS32 (data);        Gscale.apply ();        Gscale.name = "Height" + currentchunk.position; If this is chunk the refresh we need to destroy old texture soon if (currentchunk.height! = null) Currentchunk.textur        Esforcleanup.add (Currentchunk.height);        Currentchunk.height = Gscale;        Source texture won't be used anymore gameobject.destroy (texture); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer (); }//render Shadow and light to scaled down texture and copy it to texture2d int scaledownpower = 4; Scaling it down 4 powers'll resize e.g.: from 2048 to making it Marginally small Rendertexture shadowtarget = rendertargetmanager.getnewtexture (chunk.texturesize >> ScaleDownPow        Er, chunk.texturesize >> scaledownpower);        Graphics.blit (SHADOWSANDHEIGHTRT, shadowtarget);        Rendertexture.active = Shadowtarget; Texture = new Texture2d (chunk.texturesize >> scaledownpower, chunk.texturesize >> scaledownpower,        Textureformat.rgb24, false);        Texture.wrapmode = Texturewrapmode.clamp; Texture.        Readpixels (New Rect (0, 0, chunk.texturesize >> scaledownpower, chunk.texturesize >> scaledownpower), 0, 0); Texture.      Apply ();        Saveimage.savejpg (texture, currentChunk.position.ToString () + "H", randomindex.tostring ()); Texture.compress (FALSE);        Rgb24 would compress to 4 bits per pixel. Texture.        Apply (); If this is chunk the refresh we need to destroy old texture soon if (currentchunk.shadows! = null) currentchunk.textu       Resforcleanup.add (currentchunk.shadows); Currentchunk.shadows = texture;        Rendertexture.active = null;        Shadowtarget.release (); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer ();        }//Copy diffuse to texture2d rendertexture.active = Diffusert;        Texture = new Texture2d (chunk.texturesize, Chunk.texturesize, Textureformat.rgb24, false);        Texture.wrapmode = Texturewrapmode.clamp; Texture.        Readpixels (New Rect (0, 0, chunk.texturesize, chunk.texturesize), 0, 0);        Texture.name = "diffuse" + currentchunk.position; If this is chunk the refresh we need to destroy old texture soon if (currentchunk.diffuse! = null) currentchunk.textu        Resforcleanup.add (Currentchunk.diffuse);     Currentchunk.diffuse = texture;        Saveimage.savejpg (texture, currentChunk.position.ToString () + "D", randomindex.tostring ());        Currentchunk.compressdiffuse ();        Rendertexture.active = null; if (COROUTINEHELPER.CHECKIFPASsed (()) {yield return null; Coroutinehelper.starttimer ();                        } bakingcamera.targettexture = null;        Currentchunk.creategameobjectwithtextures ();        Dirtychunks.removeat (0); World.getinstance ().        Readytopolishchunk (Currentchunk);                Rendertargetmanager.releaseall (); if (coroutinehelper.checkifpassed ()) {yield return null; Coroutinehelper.starttimer (); }} if (World.getinstance (). Startpolishingworld ()) {meshrenderer[] Rarray = Getcomponentsinchildren<meshrenderer> (true) as MeshRender        Er[]; foreach (Meshrenderer r in Rarray) {if (r.material! = null) {Gameobject.dest            Roy (R.material);        }} rendertargetmanager.destroyallunusedtextures ();        Cleanup ();        Destroyobject (Gameobject);    instance = null; }}///<summary>///Preparation of the scene for baking process///</summary>///<returns></retuRns>void Preparationstage () {displaycollection = new Dictionary

U3D Honey Hex Framework Code interpretation record (ii)

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.