Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by AlexHardyHax · Nov 23, 2013 at 01:05 AM · terrain3dstreaminglod

Terrain streaming doesn't work. HEEELP!!!

Hi, I'm making a terrain streaming thingy that reads in 16 bit RAW terrains at a LOD dependant on the distance between the camera and the terrain position. I have to split the terrain into multiple pieces for two reasons:

1.Unity limits the number of vertices in a mesh to 65000.

2.Each piece can have a separate LOD level.

It was working when I only had one piece and set the MeshFilter to the mesh, but when I create multiple GameObjects (actually there are 32*32 of them) nothing comes up. The MeshFilters and MeshRendered are added to the GameObjects, but when I transform the GameObjects (in code) and click on them in the hierachy while playing, they are not transformed.

Here is the code for the multi piece terrain streaming thingy.

 using UnityEngine;
 using System.IO;
 using System;
 
 class advancedTerrain : MonoBehaviour
 {
     public string path;
     public int terrainSize;
     public int tileSize;
     public int[] minLodDistances;
     public int[] maxLodDistances;
     private int[,] lod;
     private int[,] prevLod;
     private GameObject[,] gameObjects;
     void load(int x, int y)
     {
         FileStream fileStream = new FileStream(path, FileMode.Open);
         byte[] bytes = new byte[2];
         Vector3[] vertices = new Vector3[tileSize * tileSize];
         int[] triangles = new int[(tileSize * tileSize) * 6];
         for (int yy = y * tileSize; yy < (y + 1) * tileSize; yy += lod[y, x])
         {
             for (int xx = x * tileSize; xx < (x + 1) * tileSize; xx += lod[y, x])
             {
                 fileStream.Position = (yy * terrainSize) + xx;
                 fileStream.Read(bytes, 0, 2);
                 vertices[((yy - (y * tileSize)) * tileSize) + (xx - (x * tileSize))] = new Vector3((x * tileSize) + xx, BitConverter.ToInt16(bytes, 0), (y * tileSize) + yy);
                 if (xx < ((x + 1) * tileSize) - 1 && yy < ((y + 1) * tileSize) - 1)
                 {
                     triangles[(((yy - (y * tileSize)) * tileSize) + (xx - (x * tileSize))) * 6] = ((yy - (y * tileSize)) * tileSize) + (xx - (x * tileSize));
                     triangles[((((yy - (y * tileSize)) * tileSize) + (xx - (x * tileSize))) * 6) + 1] = (((yy + 1) - (y * tileSize)) * tileSize) + (xx - (x * tileSize));
                     triangles[((((yy - (y * tileSize)) * tileSize) + (xx - (x * tileSize))) * 6) + 2] = ((yy - (y * tileSize)) * tileSize) + ((xx + 1) - (x * tileSize));
                 }
             }
         }
         Mesh mesh = new Mesh();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         gameObjects[y, x].GetComponent<MeshFilter>().mesh = mesh;
         gameObjects[y, x].transform.position = new Vector3(transform.position.x + (x * tileSize), transform.position.y, transform.position.z + (y * tileSize));
         fileStream.Close();
     }
     void Start()
     {
         lod = new int[terrainSize / tileSize, terrainSize / tileSize];
         prevLod = new int[terrainSize / tileSize, terrainSize / tileSize];
         gameObjects = new GameObject[terrainSize / tileSize, terrainSize / tileSize];
         for (int y = 0; y < terrainSize / tileSize; y++)
         {
             for (int x = 0; x < terrainSize / tileSize; x++)
             {
                 lod[y, x] = 1;
                 prevLod[y, x] = 0;
                 gameObjects[y, x] = new GameObject();
                 gameObjects[y, x].AddComponent<MeshFilter>();
                 gameObjects[y, x].AddComponent<MeshRenderer>();
             }
         }
     }
     void Update()
     {
         for (int y = 0; y < terrainSize / tileSize; y++)
         {
             for (int x = 0; x < terrainSize / tileSize; x++)
             {
                 double distance = Vector3.Distance(Camera.main.transform.position, new Vector3(transform.position.x + (x * tileSize), transform.position.y, transform.position.z + (y * tileSize)));
                 for (int a = 0; a < minLodDistances.Length; a++)
                 {
                     if (distance >= minLodDistances[a] && distance <= maxLodDistances[a])
                     {
                         lod[y, x] = a;
                         if (prevLod[y, x] != a)
                         {
                             prevLod[y, x] = a;
                             load(x, y);
                         }
                     }
                 }
             }
         }
     }
 }

Here is the code for the single piece terrain. Max terrain size is limited to 128 coz 256^2 > 65000

 //WARNING!!! THE FOLLOWING CODE SAMPLE IS DEPRECATED!
 //ALL PRACTICIONERS OF THE FOLLOWING DEVICE SHALL BE
 //PROSECUTED WITHOUT REGARD AND/OR WITH DISREGARD TO
 //LOCAL/INTERNATIONAL PEACE AND HARMONY LAWS/RULE BY
 //MEMBERS OF THE FEDERAL TORTURE BINARY TWINS!!! :(
 
 using UnityEngine;
 using System.IO;
 using System;
 
 class advancedTerrain : MonoBehaviour
 {
     public string path;
     public int size;
     public float depth;
     public int[] minLodDistances;
     public int[] maxLodDistances;
     private int lod = 1;
     private int prevLod = 0;
     void load()
     {
         FileStream fileStream = new FileStream(path, FileMode.Open);
         byte[] bytes = new byte[2];
         Vector3[] vertices = new Vector3[(size / lod) * (size / lod)];
         int[] triangles = new int[((size / lod) * (size / lod)) * 6];
         for (int x = 0; x < size / lod; x++)
         {
             for (int y = 0; y < size / lod; y++)
             {
                 fileStream.Position = (((y * lod) * size) + (x * lod)) * 2;
                 fileStream.Read(bytes, 0, 2);
                 vertices[(y * (size / lod)) + x] = new Vector3(x * lod, BitConverter.ToInt16(bytes, 0) * depth, y * lod);
                 if (x < (size / lod) - 1 && y < (size / lod) - 1)
                 {
                     triangles[((y * (size / lod)) + x) * 6] = (y * (size / lod)) + x;
                     triangles[(((y * (size / lod)) + x) * 6) + 1] = ((y + 1) * (size / lod)) + x;
                     triangles[(((y * (size / lod)) + x) * 6) + 2] = (y * (size / lod)) + (x + 1);
                     triangles[(((y * (size / lod)) + x) * 6) + 3] = (y * (size / lod)) + (x + 1);
                     triangles[(((y * (size / lod)) + x) * 6) + 4] = ((y + 1) * (size / lod)) + x;
                     triangles[(((y * (size / lod)) + x) * 6) + 5] = ((y + 1) * (size / lod)) + (x + 1);
                 }
             }
         }
         Mesh mesh = new Mesh();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         GetComponent<MeshFilter>().mesh = mesh;
         fileStream.Close();
     }
     void Update()
     {
         float distance = Vector3.Distance(Camera.main.transform.position, transform.position);
         for (int a = 0; a < minLodDistances.Length - 1; a++)
         {
             if (distance >= minLodDistances[a] && distance <= maxLodDistances[a])
             {
                 lod = a + 1;
                 if (prevLod != lod)
                 {
                     prevLod = lod;
                     load();
                     break;
                 }
             }
         }
     }
 }
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Textures on Terrain 3 Answers

Megaterrainish-type thing? 1 Answer

A node in a childnode? 1 Answer

How can i make terrains from 2d map images 0 Answers

how do i make terrain in the new update unity 4.2.1 and i can't make terrain 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