Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by i ConnE · Jul 16, 2010 at 01:25 PM · meshsavegenerated

Can i save the mesh generated by the heighmap texture and material?and iff so how?

I dont know scripting(I am just an copy-paste guy :)because it's hard to learn c# or javascrip and its not in my language..I'm Romanian) and a good explanation on how to do it would be very helpful .. Here is the script.Thanx..

// This script is placed in public domain. The author takes no responsibility for any possible harm.

var heightMap : Texture2D; var material : Material; var size = Vector3(200, 30, 200);

function Start () { GenerateHeightmap(); }

function GenerateHeightmap () { // Create the game object containing the renderer gameObject.AddComponent(MeshFilter); gameObject.AddComponent("MeshRenderer"); if (material) renderer.material = material; else renderer.material.color = Color.white;

 // Retrieve a mesh instance
 var mesh : Mesh = GetComponent(MeshFilter).mesh;

 var width : int = Mathf.Min(heightMap.width, 255);
 var height : int = Mathf.Min(heightMap.height, 255);
 var y = 0;
 var x = 0;

 // Build vertices and UVs
 var vertices = new Vector3[height * width];
 var uv = new Vector2[height * width];
 var tangents = new Vector4[height * width];

 var uvScale = Vector2 (1.0 / (width - 1), 1.0 / (height - 1));
 var sizeScale = Vector3 (size.x / (width - 1), size.y, size.z / (height - 1));

 for (y=0;y<height;y++)
 {
     for (x=0;x<width;x++)
     {
         var pixelHeight = heightMap.GetPixel(x, y).grayscale;
         var vertex = Vector3 (x, pixelHeight, y);
         vertices[y*width + x] = Vector3.Scale(sizeScale, vertex);
         uv[y*width + x] = Vector2.Scale(Vector2 (x, y), uvScale);

         // Calculate tangent vector: a vector that goes from previous vertex
         // to next along X direction. We need tangents if we intend to
         // use bumpmap shaders on the mesh.
         var vertexL = Vector3( x-1, heightMap.GetPixel(x-1, y).grayscale, y );
         var vertexR = Vector3( x+1, heightMap.GetPixel(x+1, y).grayscale, y );
         var tan = Vector3.Scale( sizeScale, vertexR - vertexL ).normalized;
         tangents[y*width + x] = Vector4( tan.x, tan.y, tan.z, -1.0 );
     }
 }

 // Assign them to the mesh
 mesh.vertices = vertices;
 mesh.uv = uv;

 // Build triangle indices: 3 indices into vertex array for each triangle
 var triangles = new int[(height - 1) * (width - 1) * 6];
 var index = 0;
 for (y=0;y<height-1;y++)
 {
     for (x=0;x<width-1;x++)
     {
         // For each grid cell output two triangles
         triangles[index++] = (y     * width) + x;
         triangles[index++] = ((y+1) * width) + x;
         triangles[index++] = (y     * width) + x + 1;

         triangles[index++] = ((y+1) * width) + x;
         triangles[index++] = ((y+1) * width) + x + 1;
         triangles[index++] = (y     * width) + x + 1;
     }
 }
 // And assign them to the mesh
 mesh.triangles = triangles;

 // Auto-calculate vertex normals from the mesh
 mesh.RecalculateNormals();

 // Assign tangents after recalculating normals
 mesh.tangents = tangents;

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Jul 16, 2010 at 01:37 PM 0
Share

it is (all that code is above the code box) - you just need to select all the code and hit the code button (the little button with 0s and 1s on)

avatar image i ConnE · Jul 16, 2010 at 01:43 PM 0
Share

ahh yes i see lol im blind :))

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mike 3 · Jul 16, 2010 at 01:40 PM

You just need to do something along the lines of this:

AssetDatabase.CreateAsset(mesh, meshPrefabPath);
AssetDatabase.SaveAssets();

Where meshPrefabPath is a path as string which is relative to the project folder, and needs the .asset extension

e.g. var meshPrefabPath = "Assets/MyNewMesh.asset";

Note that if you do that, your script has to be an editor script, not a runtime script.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image i ConnE · Jul 16, 2010 at 01:50 PM 0
Share

its easy for u to say but i still dont understand:(

avatar image Mike 3 · Jul 16, 2010 at 01:52 PM 0
Share

which bit don't you understand? the code above should work as it is just pasted in, and the editor script part is a little more complex, but can be added in

avatar image i ConnE · Jul 16, 2010 at 01:56 PM 0
Share

i dont understand where to past it

avatar image i ConnE · Jul 16, 2010 at 02:06 PM 0
Share

lets say i whant to make a wall with the proper texture and material and make a relief on it and then save it ..because i think that bumped shaders dont work on my computer (i dont have vertex shader supported idk iff thats the cause) and at the graphics emulation i have just S$$anonymous$$ 1.4 idk...sounds poor.

avatar image Mike 3 · Jul 16, 2010 at 02:12 PM 0
Share

you'd paste those lines at the very bottom. and yes, your graphics card is pretty old :P

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How can I save a gameobject's mesh? 1 Answer

Generated mesh render order 0 Answers

Blocky shadows on procedural terrain meshes 2 Answers

It's possible save materials and meshes into a prefab like the unity's model importer? 2 Answers

Generated meshes/materials cannot be made into prefabs? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges