Repository navigation
Expand file tree
/
Copy pathWorld.cs
More file actions
148 lines (94 loc) · 3.23 KB
/
Copy pathWorld.cs
File metadata and controls
148 lines (94 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.IO;
using UnityEngine;
using Geodesy;
using OmeLoader.Loader.Data.OctreeData;
using OmeLoader.Loader.Data.DataStore;
using OmeLoader.Loader.Loaders;
using OmeLoader.Loader.TypeParsers;
public class World : MonoBehaviour {
public Material _material;
public GameObject TerrainCache;
public GameObject MaterialCache;
public OmeGenerator[] _chunks;
public string url;
public string omeFileUrl;
public bool debug = false;
private OmeLoaderFactory _omeLoaderFactory = new OmeLoaderFactory();
private IOmeLoader _omeLoader;
private Stream _omeStream;
private byte[] _omeFileBytes;
private bool _omeFileRequest = false;
private List<Octree> _octrees = new List<Octree>();
private List<Vector3> _vertices = new List<Vector3>();
private KeyedPool<string, GameObject> _terrainCache;
public List<Octree> Octrees {
get { return _octrees; }
}
public List<Vector3> Vertices {
get { return _vertices; }
}
private LoadResult _loadResult;
private bool _loaded = false;
public void Awake ( ) {
_terrainCache = TerrainCache.GetComponent<TerrainPool>;
_omeLoaderFactory = new OmeLoaderFactory();
_omeLoader = _omeLoaderFactory.Create();
url = "jar:file://" + Application.dataPath + "!/assets/OME/Whistler.ome";
// url = "file://" + Application.streamingAssetsPath + "/OME/Whistler.ome";
WWW request = new WWW(url);
StartCoroutine(WaitForRequest(request));
}
public void Update ( ) {
if ( _omeFileRequest ) {
if ( !(_loaded) ) {
Debug.Log("World: Update: parsing OME stream");
_omeStream = new MemoryStream(_omeFileBytes);
_loadResult = _omeLoader.Load(_omeStream);
Debug.Log("World: Update: OME file loaded");
//
// load header data
//
for ( int i = 0; i < _loadResult.Octrees.Count; i++ ) {
_octrees.Add( _loadResult.Octrees[i] );
}
for ( int i = 0; i < _loadResult.Vertices.Count; i++ ) {
_vertices.Add( _loadResult.Vertices[i] );
}
_chunks = new OmeGenerator[_octrees.Count];
_loaded = true;
}
}
}
IEnumerator WaitForRequest ( WWW www ) {
yield return www;
_omeFileBytes = www.bytes;
_omeFileRequest = true;
}
public void LoadChunk ( int index ) {
var node = "Terrain" + index + "GO";
var go = _terrainCache.Borrow(node);
go.renderer.material = _material;
var octmesh = go.GetComponent("OmeGenerator") as OmeGenerator;
octmesh.AddVertices(_octrees[index].Indices);
octmesh.AddTriangles(_octrees[index].Triangles);
octmesh.index = index;
octmesh.debug = debug;
octmesh.update = true;
_chunks[index] = octmesh;
}
public void UnloadChunk ( int index ) {
var go = _chunks[index].gameObject;
var octmesh = go.GetComponent("OmeGenerator") as OmeGenerator;
octmesh.ClearMesh();
_terrainCache.Release(go);
_chunks[index] = null;
}
public bool isLoaded ( ) {
return _loaded;
}
}