Skip to content

Commit 3aaa545

Browse files
author
Rui Wang
committed
add Terrain support but not with textures at present
1 parent f17445c commit 3aaa545

13 files changed

+352
-41
lines changed

osgExport/BundleMeshCollider.cs

+20-17
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,28 @@ public override SceneComponent GetObjectData()
2727
if ( unityMeshCollider!=null )
2828
{
2929
Mesh unityMesh = unityMeshCollider.sharedMesh;
30-
sceneData.mesh = new SceneMesh();
31-
sceneData.mesh.name = unityMesh.name;
32-
33-
// submeshes
34-
sceneData.mesh.subMeshCount = unityMesh.subMeshCount;
35-
sceneData.mesh.triangles = new int[unityMesh.subMeshCount][];
36-
for ( int i=0; i<unityMesh.subMeshCount;i++ )
30+
if ( unityMesh!=null )
3731
{
38-
sceneData.mesh.triangles[i] = unityMesh.GetTriangles(i);
32+
sceneData.mesh = new SceneMesh();
33+
sceneData.mesh.name = unityMesh.name;
34+
35+
// submeshes
36+
sceneData.mesh.subMeshCount = unityMesh.subMeshCount;
37+
sceneData.mesh.triangles = new int[unityMesh.subMeshCount][];
38+
for ( int i=0; i<unityMesh.subMeshCount;i++ )
39+
{
40+
sceneData.mesh.triangles[i] = unityMesh.GetTriangles(i);
41+
}
42+
43+
// Vertices
44+
sceneData.mesh.vertexCount = unityMesh.vertexCount;
45+
sceneData.mesh.vertexPositions = unityMesh.vertices;
46+
sceneData.mesh.vertexUV = unityMesh.uv;
47+
sceneData.mesh.vertexUV2 = unityMesh.uv2;
48+
sceneData.mesh.vertexColors = unityMesh.colors;
49+
sceneData.mesh.vertexNormals = unityMesh.normals;
50+
sceneData.mesh.vertexTangents = unityMesh.tangents;
3951
}
40-
41-
// Vertices
42-
sceneData.mesh.vertexCount = unityMesh.vertexCount;
43-
sceneData.mesh.vertexPositions = unityMesh.vertices;
44-
sceneData.mesh.vertexUV = unityMesh.uv;
45-
sceneData.mesh.vertexUV2 = unityMesh.uv2;
46-
sceneData.mesh.vertexColors = unityMesh.colors;
47-
sceneData.mesh.vertexNormals = unityMesh.normals;
48-
sceneData.mesh.vertexTangents = unityMesh.tangents;
4952
}
5053
return sceneData;
5154
}

osgExport/BundleTerrain.cs

+37-13
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,24 @@ override public void Preprocess()
1212
{
1313
terrain = unityComponent as Terrain;
1414
terrainData = terrain.terrainData;
15+
if ( terrainData==null ) return;
16+
17+
SplatPrototype[] splats = terrainData.splatPrototypes;
18+
for ( int i=0; i<splats.Length; ++i )
19+
{
20+
textures.Add( BundleTexture.RegisterTexture(splats[i].texture, "SplatTex") );
21+
textureSizes.Add( splats[i].tileSize );
22+
textureOffsets.Add( splats[i].tileOffset );
23+
}
1524
}
1625

1726
override public void Process()
1827
{
28+
if ( terrainData==null ) return;
1929
heightmapHeight = terrainData.heightmapHeight;
2030
heightmapWidth = terrainData.heightmapWidth;
21-
heightmapResolution = terrainData.heightmapResolution;
22-
heightmapScale = terrainData.heightmapScale;
2331
size = terrainData.size;
24-
32+
2533
float[,] heights = terrainData.GetHeights(0, 0, heightmapWidth, heightmapHeight);
2634
float[] arrayHeight = new float[heightmapWidth * heightmapHeight];
2735
for ( int y=0; y<heightmapHeight; y++ )
@@ -70,8 +78,6 @@ public override SceneComponent GetObjectData()
7078
sceneData.type = "Terrain";
7179
sceneData.heightmapHeight = heightmapHeight;
7280
sceneData.heightmapWidth = heightmapWidth;
73-
sceneData.heightmapResolution = heightmapResolution;
74-
sceneData.heightmapScale = heightmapScale;
7581
sceneData.size = size;
7682
sceneData.alphamapWidth = alphamapWidth;
7783
sceneData.alphamapHeight = alphamapHeight;
@@ -84,27 +90,45 @@ public override SceneComponent GetObjectData()
8490
sceneData.alphamapTexture = new SceneTexture();
8591
sceneData.alphamapTexture.base64PNG = base64Alpha;
8692
sceneData.alphamapTexture.base64PNGLength = base64AlphaLength;
93+
94+
if ( textures.Count>0 )
95+
{
96+
sceneData.textureIDs = new int[textures.Count];
97+
for ( int i=0; i<textures.Count; i++ )
98+
sceneData.textureIDs[i] = textures[i].uniqueID;
99+
100+
sceneData.textureTilingOffsets = new Vector4[textures.Count];
101+
for ( int i=0; i<textures.Count; i++ )
102+
{
103+
Vector2 sc = textureSizes[i], of = textureOffsets[i];
104+
sceneData.textureTilingOffsets[i] = new Vector4(sc.x, sc.y, of.x, of.y);
105+
}
106+
}
107+
108+
if ( terrain!=null )
109+
{
110+
sceneData.lightmapIndex = terrain.lightmapIndex;
111+
sceneData.lightmapTilingOffset = terrain.lightmapScaleOffset;
112+
}
87113
return sceneData;
88114
}
89115

90116
public Terrain terrain;
91117
private TerrainData terrainData;
92118

93-
int heightmapHeight;
94-
int heightmapWidth;
95-
int heightmapResolution;
96-
Vector3 heightmapScale;
97119
Vector3 size;
98-
99-
int alphamapWidth;
100-
int alphamapHeight;
101-
int alphamapLayers;
120+
int heightmapHeight, heightmapWidth;
121+
int alphamapWidth, alphamapHeight, alphamapLayers;
102122

103123
public string base64Height;
104124
public int base64HeightLength;
105125

106126
public string base64Alpha;
107127
public int base64AlphaLength;
128+
129+
public List<BundleTexture> textures = new List<BundleTexture>();
130+
public List<Vector2> textureSizes = new List<Vector2>();
131+
public List<Vector2> textureOffsets = new List<Vector2>();
108132
}
109133

110134
}

osgExport/ExportTerrain.cs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.IO;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using UnityEditor;
6+
using UnityEditor.SceneManagement;
7+
8+
namespace nwTools
9+
{
10+
11+
public class TerrainExporter
12+
{
13+
public static string ExportTerrain( ref SceneData sceneData, ref SceneTerrain st, string spaces )
14+
{
15+
string osgData = spaces + "Size " + st.size.x + " " + st.size.y + " " + st.size.z + "\n";
16+
17+
// Handle heightmap data
18+
osgData += spaces + "HeightMap " + st.heightmapWidth + " " + st.heightmapHeight + " {\n";
19+
byte[] heightData = System.Convert.FromBase64String(st.heightmapTexture.base64PNG);
20+
System.Text.StringBuilder sb = new System.Text.StringBuilder();
21+
for ( int y=0; y<st.heightmapHeight; y++ )
22+
{
23+
sb.Append( spaces + " " );
24+
for ( int x=0; x<st.heightmapWidth; x++ )
25+
{
26+
int index = (y * st.heightmapWidth) + x;
27+
sb.Append( System.BitConverter.ToSingle(heightData, index * 4) + " " );
28+
}
29+
sb.Append( "\n" );
30+
}
31+
osgData += sb.ToString() + spaces + "}\n";
32+
33+
// Handle alphamap layers
34+
osgData += spaces + "AlphaMap " + st.alphamapWidth + " " + st.alphamapHeight
35+
+ " " + st.alphamapLayers + " {\n";
36+
byte[] alphaData = System.Convert.FromBase64String(st.alphamapTexture.base64PNG);
37+
System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
38+
for ( int i=0; i<st.alphamapLayers; i++ )
39+
{
40+
sb2.Append( spaces + " Layer " + i + " {\n" );
41+
for ( int y=0; y<st.alphamapHeight; y++ )
42+
{
43+
sb2.Append( spaces + " " );
44+
for ( int x=0; x<st.alphamapWidth; x++ )
45+
{
46+
int index = i * (st.alphamapHeight * st.alphamapWidth) + (y * st.alphamapWidth) + x;
47+
sb2.Append( System.BitConverter.ToSingle(alphaData, index * 4) + " " );
48+
}
49+
sb2.Append( "\n" );
50+
}
51+
sb2.Append( spaces + " }\n" );
52+
}
53+
osgData += sb2.ToString() + spaces + "}\n";
54+
55+
// Handle all splat textures
56+
for ( int i=0; i<st.textureIDs.Length; i++ )
57+
{
58+
int texID = st.textureIDs[i];
59+
SceneTexture texture = sceneData.resources.GetTexture(texID, false);
60+
if ( texture==null ) continue;
61+
62+
Vector4 off = st.textureTilingOffsets[i];
63+
osgData += spaces + "Splat" + i + " \"" + texture.name + "\""
64+
+ " \"" + texture.path + "\"\n";
65+
osgData += spaces + "SplatTilingOffset" + i + " "
66+
+ off.x + " " + off.y + " " + off.z + " " + off.w + "\n";
67+
}
68+
69+
// Handle lightmaps
70+
if ( st.lightmapIndex>=0 )
71+
{
72+
SceneTexture texture = sceneData.resources.lightmaps[st.lightmapIndex];
73+
if ( texture!=null )
74+
{
75+
osgData += spaces + "Lightmap \"" + texture.name + "\""
76+
+ " \"" + texture.path + "\"\n";
77+
osgData += spaces + "LightmapTilingOffset " + st.lightmapTilingOffset.x + " "
78+
+ st.lightmapTilingOffset.y + " " + st.lightmapTilingOffset.z + " "
79+
+ st.lightmapTilingOffset.w + "\n";
80+
}
81+
}
82+
return osgData;
83+
}
84+
85+
public static void Reset()
86+
{
87+
}
88+
}
89+
90+
}

osgExport/SceneDataClasses.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ public class SceneTerrain : SceneComponent
176176
{
177177
public int heightmapHeight;
178178
public int heightmapWidth;
179-
public int heightmapResolution;
180-
181-
public Vector3 heightmapScale;
182179
public Vector3 size;
183180

184181
public int alphamapWidth;
@@ -187,6 +184,11 @@ public class SceneTerrain : SceneComponent
187184

188185
public SceneTexture heightmapTexture;
189186
public SceneTexture alphamapTexture;
187+
188+
public int[] textureIDs;
189+
public Vector4[] textureTilingOffsets;
190+
public int lightmapIndex;
191+
public Vector4 lightmapTilingOffset;
190192
}
191193

192194
public class SceneResources

osgExport/SceneExporter.cs

+12
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ private static string ExportHierarchy( ref SceneData sceneData, SceneGameObject
158158
//SceneMeshCollider smc = (SceneMeshCollider)component;
159159
// TODO
160160
}
161+
else if ( component.type=="Terrain" )
162+
{
163+
SceneTerrain st = (SceneTerrain)component;
164+
osgSubData += spaces + " Geode {\n"
165+
+ ExportCommonAttr(st.type, spaces + " ")
166+
+ subSpaces + "num_drawables 1\n";
167+
osgSubData += subSpaces + "nwTools::Terrain {\n"
168+
+ TerrainExporter.ExportTerrain(ref sceneData, ref st, subSpaces + " ")
169+
+ subSpaces + "}\n";
170+
osgSubData += spaces + " }\n";
171+
numChildren++;
172+
}
161173
else if ( component.type=="MeshRenderer" )
162174
{
163175
SceneMeshRenderer smr = (SceneMeshRenderer)component;

viewer/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ LINK_DIRECTORIES(${OPENSCENEGRAPH_LIB_DIR})
4747
SET(SOURCE_FILES
4848
viewer.cpp
4949
shader_data.cpp
50+
terrain_data.cpp
5051
utilities.cpp
5152
user_data_classes.h
5253
)

viewer/Default.frag

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ uniform sampler2D specularTexture;
66
uniform sampler2D emissionTexture;
77
uniform samplerCube reflectTexture;
88

9+
uniform vec3 lightColor, lightDirection;
10+
uniform mat4 osg_ViewMatrix;
11+
12+
varying vec4 eyeVec;
13+
varying vec3 normalVec, tangentVec, binormalVec;
14+
915
void main()
1016
{
1117
vec4 color = texture2D(mainTexture, gl_TexCoord[0].st);
1218
vec4 light = texture2D(lightTexture, gl_TexCoord[1].st);
13-
gl_FragColor = color * light;
19+
20+
vec3 lightDir = normalize(mat3(osg_ViewMatrix) * lightDirection);
21+
float diff = max(0.0, dot(normalVec.xyz, lightDir));
22+
gl_FragColor.rgb = color.rgb * light.rgb;
23+
gl_FragColor.a = color.a;
1424
}

viewer/Default.vert

+8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
varying vec4 gl_TexCoord[gl_MaxTextureCoords];
22
uniform mat4 gl_TextureMatrix[gl_MaxTextureCoords];
3+
attribute vec3 tangent;
4+
5+
varying vec4 eyeVec;
6+
varying vec3 normalVec, tangentVec, binormalVec;
37

48
void main()
59
{
610
gl_Position = ftransform();
711
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
812
gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1;
13+
normalVec = normalize(vec3(gl_NormalMatrix * gl_Normal));
14+
tangentVec = normalize(vec3(gl_NormalMatrix * tangent));
15+
binormalVec = cross(normalVec, tangentVec);
16+
eyeVec = gl_ModelViewMatrix * gl_Vertex;
917
}
+21-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
varying vec4 gl_TexCoord[gl_MaxTextureCoords];
22
uniform sampler2D mainTexture;
3-
uniform sampler2D lightTexture;
43
uniform sampler2D normalTexture;
5-
uniform sampler2D specularTexture;
6-
uniform sampler2D emissionTexture;
7-
uniform samplerCube reflectTexture;
4+
uniform vec3 lightColor, lightDirection;
5+
uniform mat4 osg_ViewMatrix;
6+
7+
varying vec4 eyeVec;
8+
varying vec3 normalVec, tangentVec, binormalVec;
89

910
void main()
1011
{
11-
vec4 color = texture2D(mainTexture, gl_TexCoord[0].st);
12-
vec4 light = texture2D(lightTexture, gl_TexCoord[1].st);
13-
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0) * color; // just test
12+
vec2 uv = gl_TexCoord[0].st;
13+
vec4 color = texture2D(mainTexture, uv);
14+
vec4 normal = texture2D(normalTexture, uv);
15+
16+
vec3 lightDir = normalize(mat3(osg_ViewMatrix) * lightDirection);
17+
float diff = max(0.0, dot(normalVec.xyz, lightDir));
18+
gl_FragColor.rgb = color.rgb * lightColor * diff;
19+
gl_FragColor.a = color.a;
1420
}
21+
22+
// Legacy Shaders/Bumped Specular
23+
// Legacy Shaders/Reflective/Bumped Specular
24+
// Legacy Shaders/Transparent/Diffuse
25+
// Legacy Shaders/Transparent/Cutout/Diffuse
26+
// Legacy Shaders/Transparent/Bumped Diffuse
27+
// Legacy Shaders/Transparent/Cutout/Bumped Specular
28+
// Legacy Shaders/Reflective/Bumped Diffuse

viewer/shader_data.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void applyUserShaders( ShaderDataProxyMap& sdMap, const std::string& dbPath )
2727
osg::ref_ptr<osg::Program> program = new osg::Program;
2828
program->addShader( osgDB::readShaderFile(osg::Shader::VERTEX, vertFile) );
2929
program->addShader( osgDB::readShaderFile(osg::Shader::FRAGMENT, fragFile) );
30+
program->addBindAttribLocation( "tangent", 6 );
3031
s_sharedShaderPrograms[name] = program;
3132
}
3233
itr->second->removeAttribute( itr->first );

0 commit comments

Comments
 (0)