Unity3d makes dynamic mesh and can be undulating with the ground

Source: Internet
Author: User

Applicable scenario: The protagonist with a halo, halo with a map, to be affixed to the ground, and with the ground bumps and downs

Code

Using unityengine;using System.Collections; [Requirecomponent (typeof (Meshfilter))] [Requirecomponent (typeof (Meshrenderer))]public class Test:monobehaviour {private Meshfilter mfilter;private Mesh mmesh;public int quadnum = 1;public float quadlength = 1;//Use this for initializationvoid Start () {mfilter = Gameobject . Getcomponent<meshfilter> (); Mmesh = new Mesh (); Mfilter.sharedmesh = Mmesh;mmesh.markdynamic (); var len = 2 * QuadNum * Quadlength;var H_len = quadnum * quadlength;var vertices = new Vector3[quadnum * 2 * quadnum * 2 * 4];var UV = new Vecto R2[quadnum * 2 * quadnum * 2 * 4];var triangle = new Int[quadnum * 2 * quadnum * 2 * 6];var offset = new Vector3 (-h_len, 0 ,-h_len); int idx = 0;for (int i = 0; i < Quadnum * 2; i++) {for (int j = 0; J < Quadnum * 2; + j) {var p0 = offset + New Vector3 (J * quadlength, 0, I * quadlength), var p1 = offset + new Vector3 (J * quadlength, 0, (i + 1) * quadlength); var P2 = offset + new Vector3 ((j + 1) * quadlength, 0, (i + 1) * quadlength)var p3 = offset + new Vector3 ((j + 1) * quadlength, 0, I * quadlength); int quad = (j + i * quadnum * 2) * 4;vertices[quad ] = P0;vertices[quad + 1] = P1;vertices[quad + 2] = P2;vertices[quad + 3] = P3;uv[quad] = new Vector2 ((p0.x + h_len)/len , (p0.z + h_len)/len), Uv[quad + 1] = new Vector2 ((p1.x + h_len)/len, (p1.z + h_len)/len); Uv[quad + 2] = new Vector2 ( (p2.x + h_len)/len, (p2.z + h_len)/len), Uv[quad + 3] = new Vector2 ((p3.x + h_len)/len, (p3.z + h_len)/len); Triangl e[idx++] = quad;triangle[idx++] = quad + 1;triangle[idx++] = quad + 2;triangle[idx++] = quad;triangle[idx++] = quad + 2;tr iangle[idx++] = quad + 3;}} mmesh.vertices = Vertices;mmesh.uv = Uv;mmesh.triangles = triangle; }//update is called once per frameprotected virtual void Update () {var len = 2 * Quadnum * Quadlength;var H_len = Quadnu M * quadlength;var vertices = new Vector3[quadnum * 2 * quadnum * 2 * 4];var offset = new Vector3 (-h_len, 0,-h_len); for ( int i = 0; I < Quadnum * 2; i++) {for (int j = 0; J < Quadnum * 2; J + +) {var p0 = offset + new Vector3 (J * quadlength, 0, I * quadlength), var p1 = offset + new Vector3 (J * quadlength, 0, (i + 1) * quadlength); var P2 = offset + new Vector3 ((j + 1) * quadlength, 0, (i + 1) * quadlength), var p3 = offset + new Vector3 ((j + 1) * QUADL  Ength, 0, I * quadlength); int quad = (j + i * quadnum * 2) * 4;vertices[quad] = P0;vertices[quad + 1] = P1;vertices[quad + 2] = P2;vertices[quad + 3] = p3;}}  var ray = new Ray (Vector3.down, Vector3.down), var hit = new Raycasthit (), for (int i = 0; i < Quadnum * 2; i++) {for (int j = 0; J < Quadnum * 2; J + +) {var p0 = offset + new Vector3 (J * quadlength, 0, I * quadlength), var p1 = offset + new Vector3 (J * quadlength, 0, (i + 1) * quadlength), var p2 = offset + new Vector3 ((j + 1) * quadlength, 0, (i + 1) * quadlength); var p3 = offset + new Vect Or3 ((j + 1) * quadlength, 0, I * quadlength); ray.origin = transform.localToWorldMatrix.MultiplyPoint3x4 (p0) +new Vector3 (0 , 10,0); if (Physics.raycast (ray,out hit, 100){p0 = transform.worldToLocalMatrix.MultiplyPoint3x4 (hit.point);} Ray.origin = transform.localToWorldMatrix.MultiplyPoint3x4 (p1) + new Vector3 (0, 0); if (Physics.raycast (Ray, out hit, ) {P1 = transform.worldToLocalMatrix.MultiplyPoint3x4 (hit.point);} Ray.origin = transform.localToWorldMatrix.MultiplyPoint3x4 (p2) + new Vector3 (0, 0); if (Physics.raycast (Ray, out hit, ) {P2 = transform.worldToLocalMatrix.MultiplyPoint3x4 (hit.point);} Ray.origin = transform.localToWorldMatrix.MultiplyPoint3x4 (p3) + new Vector3 (0, 0); if (Physics.raycast (Ray, out hit, ) {p3 = transform.worldToLocalMatrix.MultiplyPoint3x4 (hit.point);} int quad = (j + i * quadnum * 2) * 4;vertices[quad] = P0;vertices[quad + 1] = P1;vertices[quad + 2] = P2;vertices[quad + 3 ] = p3;}} Mmesh.vertices = vertices;}}

  

Unity3d makes dynamic mesh and can be undulating with the ground

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.