//
    // Exports this object.
    //
    public bool ExportGeometry(IntPtr scene)
    {
        if (NumVertices == 0 || NumTriangles == 0)
            return false;

        Phonon.Vector3[] vertices = new Phonon.Vector3[NumVertices];
        Phonon.Triangle[] triangles = new Phonon.Triangle[NumTriangles];

        if (AttachedMesh != null)
            ExportMesh(vertices, triangles);
        else if (AttachedTerrain != null)
            ExportTerrain(vertices, triangles);
        else
            return false;

        Phonon.Export.iplSetObjectVertices(scene, vertices.Length, vertices);
        Phonon.Export.iplSetObjectTriangles(scene, triangles.Length, triangles);
        return true;
    }
Exemple #2
0
		public static extern Error	iplSetObjectTriangles(IntPtr scene, int numTriangles, Triangle[] triangles);
    //
    // Exports a terrain.
    //
    void ExportTerrain(Phonon.Vector3[] vertices, Phonon.Triangle[] triangles)
    {
        int w = AttachedTerrain.terrainData.heightmapWidth;
        int h = AttachedTerrain.terrainData.heightmapHeight;
        int s = Mathf.Min(w - 1, Mathf.Min(h - 1, (int)Mathf.Pow(2.0f, (float)TerrainSimplificationLevel)));

        if (s == 0)
            s = 1;

        w = ((w - 1) / s) + 1;
        h = ((h - 1) / s) + 1;

        Vector3 position = AttachedTerrain.transform.position;
        float[,] heights = AttachedTerrain.terrainData.GetHeights(0, 0, AttachedTerrain.terrainData.heightmapWidth, AttachedTerrain.terrainData.heightmapHeight);

        int vertexIndex = 0;
        for (int v = 0; v < AttachedTerrain.terrainData.heightmapHeight; v += s)
        {
            for (int u = 0; u < AttachedTerrain.terrainData.heightmapWidth; u += s)
            {
                float height = heights[v, u];

                float x = position.x + (((float) u / AttachedTerrain.terrainData.heightmapWidth) * AttachedTerrain.terrainData.size.x);
                float y = position.y + (height * AttachedTerrain.terrainData.size.y);
                float z = position.z + (((float) v / AttachedTerrain.terrainData.heightmapHeight) * AttachedTerrain.terrainData.size.z);

                vertices[vertexIndex++] = Phonon.Common.ConvertVector(new Vector3 { x = x, y = y, z = z });
            }
        }

        int triangleIndex = 0;
        for (int v = 0; v < h - 1; ++v)
        {
            for (int u = 0; u < w - 1; ++u)
            {
                int i0 = v * w + u;
                int i1 = (v + 1) * w + u;
                int i2 = v * w + (u + 1);

                triangles[triangleIndex++] = new Phonon.Triangle { index0 = i0, index1 = i1, index2 = i2 };

                i0 = v * w + (u + 1);
                i1 = (v + 1) * w + u;
                i2 = (v + 1) * w + (u + 1);

                triangles[triangleIndex++] = new Phonon.Triangle { index0 = i0, index1 = i1, index2 = i2 };
            }
        }
    }