Example #1
0
    private void OnGroundComplete(GND.Mesh mesh)
    {
        width  = mesh.width;
        height = mesh.height;

        Ground ground = new Ground();

        ground.BuildMesh(mesh);
        ground.InitTextures(mesh);
        ground.Render();

        if (mesh.waterVertCount > 0)
        {
            water = new Water();
            water.InitTextures(mesh, world.water);
            water.BuildMesh(mesh);
        }

        //initialize sounds
        for (int i = 0; i < world.sounds.Count; i++)
        {
            world.sounds[i].pos[0] += mesh.width;
            world.sounds[i].pos[1] *= -1;
            world.sounds[i].pos[2] += mesh.height;
            //world.sounds[i].pos[2] = tmp;
            world.sounds[i].range *= 0.3f;
            world.sounds[i].tick   = 0;
        }

        groundCompleted = true;
    }
Example #2
0
    public void InitTextures(GND.Mesh compiledMesh, RSW.WaterInfo waterInfo)
    {
        info = waterInfo;

        for (int i = 0; i < 32; i++)
        {
            var texture = FileManager.Load(waterInfo.images[i]) as Texture2D;
            textures[i] = texture;
        }
    }
Example #3
0
    private GND LoadGround(string mapname, RSW world, Action <string, string, object> callback)
    {
        string gndPath = "data/" + GetFilePath(WorldLoader.files.gnd);
        GND    ground  = FileManager.Load(gndPath) as GND;

        if (ground == null)
        {
            throw new Exception("Could not load gnd for " + mapname);
        }

        GND.Mesh compiledGround = GroundLoader.Compile(ground, world.water.level, world.water.waveHeight);
        LoadGroundTexture(world, compiledGround);

        Progress += 1;
        callback.Invoke(mapname, "MAP_GROUND", compiledGround);

        return(ground);
    }
Example #4
0
    public void InitTextures(GND.Mesh compiledMesh)
    {
        var textures = compiledMesh.textures;
        var count    = textures.Length;
        var _width   = Math.Round(Math.Sqrt(count));
        int width    = (int)Math.Pow(2, Math.Ceiling(Math.Log(_width * 258) / Math.Log(2)));
        int height   = (int)Math.Pow(2, Math.Ceiling(Math.Log(Math.Ceiling(Math.Sqrt(count)) * 258) / Math.Log(2)));

        RenderTexture renderTexture = RenderTexture.GetTemporary(width, height);

        RenderTexture.active = renderTexture;

        GL.Clear(false, true, Color.clear);

        material.SetPass(0);
        GL.PushMatrix();
        GL.LoadPixelMatrix(0, width, 0, height);

        for (int i = 0; i < count; i++)
        {
            var texture = FileManager.Load(textures[i]) as Texture2D;
            var x       = (float)(i % _width) * 258;
            var y       = (float)Math.Floor(i / _width) * 258;

            Graphics.DrawTexture(new Rect(x, y, 264, 264), texture);
            Graphics.DrawTexture(new Rect(x + 1, y + 1, 256, 256), texture);
        }

        GL.PopMatrix();
        GL.End();

        atlas          = new Texture2D(width, height, TextureFormat.RGBAFloat, true);
        atlas.wrapMode = TextureWrapMode.Clamp;
        atlas.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        atlas.mipMapBias = -0.5f;

        atlas.Apply();

        RenderTexture.active = null;
        RenderTexture.ReleaseTemporary(renderTexture);

        lightmap = compiledMesh.lightmap;
        tintmap  = compiledMesh.tileColor;
    }
Example #5
0
    private void LoadGroundTexture(RSW world, GND.Mesh ground)
    {
        LinkedList <string> textures = new LinkedList <string>();

        FileManager.InitBatch();

        //queue water textures
        if (ground.waterVertCount > 0)
        {
            var path = "data/texture/\xbf\xf6\xc5\xcd/water" + world.water.type;
            for (int i = 0; i < 32; i++)
            {
                string num = (i < 10) ? ("0" + i) : ("" + i);
                textures.AddLast(path + num + ".jpg");
                FileManager.Load(textures.Last.Value);
            }
        }

        //queue ground textures
        for (int i = 0; i < ground.textures.Length; i++)
        {
            textures.AddLast("data/texture/" + ground.textures[i]);
            FileManager.Load(textures.Last.Value);
        }

        FileManager.EndBatch();

        //splice water textures from ground textures
        List <string> waterTextures = new List <string>();

        if (ground.waterVertCount > 0)
        {
            for (int i = 0; i < 32; i++)
            {
                waterTextures.Add(textures.First.Value);
                textures.RemoveFirst();
            }
        }

        waterTextures.CopyTo(world.water.images);
        ground.textures = new string[textures.Count];
        textures.CopyTo(ground.textures, 0);
    }
Example #6
0
    public void BuildMesh(GND.Mesh compiledMesh)
    {
        meshes = new Mesh[(int)Math.Ceiling(compiledMesh.meshVertCount / (float)MapRenderer.MAX_VERTICES)];

        for (int nMesh = 0; nMesh < meshes.Length; nMesh++)
        {
            List <Vector3> vertices   = new List <Vector3>();
            List <int>     triangles  = new List <int>();
            List <Vector3> normals    = new List <Vector3>();
            List <Vector2> uv         = new List <Vector2>();
            List <Vector2> tintUv     = new List <Vector2>();
            List <Vector2> lightmapUv = new List <Vector2>();

            float[] vertexData = new float[12];
            int     v = 0, h = 0;
            for (int i = 0, ended = 0; vertices.Count < MapRenderer.MAX_VERTICES && ended == 0; i++)
            {
                Vertex[] vs = new Vertex[4];
                for (int j = 0; j < 4; j++)
                {
                    var vIndex = i * 4 + j + nMesh * MapRenderer.MAX_VERTICES;

                    if (vIndex * vertexData.Length >= compiledMesh.mesh.Length)
                    {
                        ended = 1;
                        break;
                    }

                    Array.ConstrainedCopy(compiledMesh.mesh, vIndex * vertexData.Length, vertexData, 0, vertexData.Length);
                    Vertex vertex = BuildVertex(vertexData);
                    vs[j] = vertex;
                    vertices.Add(vertex.position);
                    normals.Add(vertex.normal);
                    uv.Add(vertex.texCoord);
                    lightmapUv.Add(vertex.lightCoord);
                    tintUv.Add(vertex.tileCoord);
                }

                if (ended == 0)
                {
                    if (vs[0].normal.z == 1)
                    {
                        v++;
                        triangles.AddRange(new int[] {
                            i * 4 + 0, i * 4 + 1, i * 4 + 2, //left triangle
                            i * 4 + 2, i * 4 + 3, i * 4 + 0, //right triangle
                        });
                    }
                    else if (vs[0].normal.x == 1)
                    {
                        v++;
                        triangles.AddRange(new int[] {
                            i * 4 + 0, i * 4 + 2, i * 4 + 1, //left triangle
                            i * 4 + 2, i * 4 + 3, i * 4 + 1, //right triangle
                        });
                    }
                    else
                    {
                        h++;
                        triangles.AddRange(new int[] {
                            i * 4 + 0, i * 4 + 2, i * 4 + 3, //left triangle
                            i * 4 + 0, i * 4 + 1, i * 4 + 2, //right triangle
                        });
                    }
                }
            }

            Mesh mesh = new Mesh();
            mesh.vertices  = vertices.ToArray();
            mesh.triangles = triangles.ToArray();
            mesh.normals   = normals.ToArray();
            mesh.uv        = uv.ToArray();
            mesh.uv2       = lightmapUv.ToArray();
            mesh.uv3       = tintUv.ToArray();

            meshes[nMesh] = mesh;
        }
    }
Example #7
0
    public void BuildMesh(GND.Mesh compiledMesh)
    {
        meshes = new Mesh[(int)Math.Ceiling(compiledMesh.waterVertCount / (float)MapRenderer.MAX_VERTICES)];

        for (int nMesh = 0; nMesh < meshes.Length; nMesh++)
        {
            List <Vector3> vertices  = new List <Vector3>();
            List <int>     triangles = new List <int>();
            List <Vector3> normals   = new List <Vector3>();
            List <Vector2> uv        = new List <Vector2>();

            float[] vertexData = new float[5];
            for (int i = 0, ended = 0; vertices.Count < MapRenderer.MAX_VERTICES && ended == 0; i++)
            {
                Ground.Vertex[] vs = new Ground.Vertex[4];
                for (int j = 0; j < 4; j++)
                {
                    var vIndex = i * 4 + j + nMesh * MapRenderer.MAX_VERTICES;

                    if (vIndex * vertexData.Length >= compiledMesh.waterMesh.Length)
                    {
                        ended = 1;
                        break;
                    }

                    Array.ConstrainedCopy(compiledMesh.waterMesh, vIndex * vertexData.Length, vertexData, 0, vertexData.Length);
                    Ground.Vertex vertex = BuildVertex(vertexData);
                    vs[j] = vertex;
                    vertices.Add(vertex.position);
                    normals.Add(vertex.normal);
                    uv.Add(vertex.texCoord);
                }

                if (ended == 0)
                {
                    triangles.AddRange(new int[] {
                        i * 4 + 0, i * 4 + 2, i * 4 + 3, //left triangle
                        i * 4 + 0, i * 4 + 1, i * 4 + 2, //right triangle
                    });
                }
            }

            Mesh mesh = new Mesh();
            mesh.vertices  = vertices.ToArray();
            mesh.triangles = triangles.ToArray();
            mesh.normals   = normals.ToArray();
            mesh.uv        = uv.ToArray();

            meshes[nMesh] = mesh;
        }

        objects = new GameObject[meshes.Length];

        renderers = new MeshRenderer[meshes.Length];

        GameObject water = new GameObject("_Water");

        water.transform.parent = MapRenderer.mapParent.transform;

        for (int i = 0; i < meshes.Length; i++)
        {
            Mesh mesh = meshes[i];

            GameObject gameObject = new GameObject("Water[" + i + "]");
            gameObject.transform.parent = water.transform;
            gameObject.layer            = 4;
            var mf = gameObject.AddComponent <MeshFilter>();
            mf.mesh = mesh;
            var mr = gameObject.AddComponent <MeshRenderer>();
            renderers[i]            = mr;
            mr.material             = material;
            mr.material.mainTexture = textures[0];

            Vector3 scale = gameObject.transform.localScale;
            scale.Set(1f, -1f, 1f);
            gameObject.transform.localScale = scale;

            objects[i] = gameObject;
        }

        material.SetFloat("Wave Height", info.waveHeight);
        material.SetFloat("Wave Pitch", info.wavePitch);
    }
Example #8
0
        public static GND.Mesh Compile(GND gnd, float WATER_LEVEL, float WATER_HEIGHT)
        {
            var normals = GetSmoothNormal(gnd);

            var meshData      = new List <float>();
            var waterMeshData = new List <float>();

            for (int y = 0; y < gnd.height; y++)
            {
                for (int x = 0; x < gnd.width; x++)
                {
                    var cellA = gnd.surfaces[x + y * gnd.width];
                    var h_a   = cellA.height;

                    float lu1 = x / (float)gnd.width;
                    float lu2 = (x + 1) / (float)gnd.width;
                    float lv1 = y / (float)gnd.height;
                    float lv2 = (y + 1) / (float)gnd.height;

                    // Check tile up
                    if (cellA.tileUp > -1)
                    {
                        var tile = gnd.tiles[cellA.tileUp];

                        // Check if has texture
                        var n = normals[x + y * gnd.width];

                        meshData.AddRange(new float[] {
                            (x + 0) * 2, h_a[0], (y + 0) * 2, n[0][0], n[0][1], n[0][1], tile.textureStart[0], tile.textureEnd[0], lu1, lv1, (x + 0.5f) / gnd.width, (y + 0.5f) / gnd.height,
                            (x + 1) * 2, h_a[1], (y + 0) * 2, n[1][0], n[1][1], n[1][1], tile.textureStart[1], tile.textureEnd[1], lu2, lv1, (x + 1.5f) / gnd.width, (y + 0.5f) / gnd.height,
                            (x + 1) * 2, h_a[3], (y + 1) * 2, n[2][0], n[2][1], n[2][1], tile.textureStart[3], tile.textureEnd[3], lu2, lv2, (x + 1.5f) / gnd.width, (y + 1.5f) / gnd.height,
                            //(x + 1) * 2, h_a[3], (y + 1) * 2, n[2][0], n[2][1], n[2][1], tile.textureStart[3], tile.textureEnd[3], lu2, lv2, (x + 1.5f) / gnd.width, (y + 1.5f) / gnd.height,
                            (x + 0) * 2, h_a[2], (y + 1) * 2, n[3][0], n[3][1], n[3][1], tile.textureStart[2], tile.textureEnd[2], lu1, lv2, (x + 0.5f) / gnd.width, (y + 1.5f) / gnd.height,
                            //(x + 0) * 2, h_a[0], (y + 0) * 2, n[0][0], n[0][1], n[0][1], tile.textureStart[0], tile.textureEnd[0], lu1, lv1, (x + 0.5f) / gnd.width, (y + 0.5f) / gnd.height
                        });

                        // Add water only if it's upper than the ground.
                        if (h_a[0] > WATER_LEVEL - WATER_HEIGHT ||
                            h_a[1] > WATER_LEVEL - WATER_HEIGHT ||
                            h_a[2] > WATER_LEVEL - WATER_HEIGHT ||
                            h_a[3] > WATER_LEVEL - WATER_HEIGHT)
                        {
                            float o    = 5f;
                            var   texx = (x + 1) % o / o;
                            if (texx == 0)
                            {
                                texx = 1;
                            }
                            var texy = (y + 1) % o / o;
                            if (texy == 0)
                            {
                                texy = 1;
                            }
                            waterMeshData.AddRange(new float[] {
                                //        vec3 pos            |            vec2 texcoords
                                (x + 0) * 2, WATER_LEVEL, (y + 0) * 2, (x + 0) % o / o, (y + 0) % o / o,
                                (x + 1) * 2, WATER_LEVEL, (y + 0) * 2, texx, (y + 0) % o / o,
                                (x + 1) * 2, WATER_LEVEL, (y + 1) * 2, texx, texy,
                                //(x + 1) * 2, WATER_LEVEL, (y + 1) * 2, texx, texy,
                                (x + 0) * 2, WATER_LEVEL, (y + 1) * 2, (x + 0) % o / o, texy,
                                //(x + 0) * 2, WATER_LEVEL, (y + 0) * 2, ((x + 0) % 5 / 5), ((y + 0) % 5 / 5)
                            });
                        }
                    }

                    // Check tile front
                    if (cellA.tileFront > -1 && y + 1 < gnd.height)
                    {
                        var tile = gnd.tiles[cellA.tileFront];

                        var cellB = gnd.surfaces[x + (y + 1) * gnd.width];
                        var h_b   = cellB.height;

                        meshData.AddRange(new float[] {
                            //      vec3 pos           |  vec3 normals     |    vec2 texcoords      |   vec2 lightcoord  |   vec2 tileCoords
                            (x + 0) * 2, h_b[0], (y + 1) * 2, 0.0f, 0.0f, 1.0f, tile.textureStart[2], tile.textureEnd[2], lu1, lv2, 0, 0,
                            (x + 0) * 2, h_a[2], (y + 1) * 2, 0.0f, 0.0f, 1.0f, tile.textureStart[0], tile.textureEnd[0], lu1, lv1, 0, 0,
                            (x + 1) * 2, h_a[3], (y + 1) * 2, 0.0f, 0.0f, 1.0f, tile.textureStart[1], tile.textureEnd[1], lu2, lv1, 0, 0,
                            (x + 1) * 2, h_b[1], (y + 1) * 2, 0.0f, 0.0f, 1.0f, tile.textureStart[3], tile.textureEnd[3], lu2, lv2, 0, 0,
                        });
                    }

                    // Check tile right
                    if (cellA.tileRight > -1 && x + 1 < gnd.width)
                    {
                        var tile = gnd.tiles[cellA.tileRight];

                        var cellB = gnd.surfaces[x + 1 + y * gnd.width];
                        var h_b   = cellB.height;

                        meshData.AddRange(new float[] {
                            //      vec3 pos           |  vec3 normals    |    vec2 texcoords      |   vec2 lightcoord   |    vec2 tileCoords
                            (x + 1) * 2, h_a[1], (y + 0) * 2, 1, 0, 0, tile.textureStart[1], tile.textureEnd[1], lu2, lv1, 0, 0,
                            (x + 1) * 2, h_a[3], (y + 1) * 2, 1, 0, 0, tile.textureStart[0], tile.textureEnd[0], lu1, lv1, 0, 0,
                            (x + 1) * 2, h_b[0], (y + 0) * 2, 1, 0, 0, tile.textureStart[3], tile.textureEnd[3], lu2, lv2, 0, 0,
                            //(x + 1) * 2, h_b[0], (y + 0) * 2, 1.0f, 0.0f, 0.0f, tile.textureStart[3], tile.textureEnd[3], lu2, lv2, 0, 0,
                            (x + 1) * 2, h_b[2], (y + 1) * 2, 1, 0, 0, tile.textureStart[2], tile.textureEnd[2], lu1, lv2, 0, 0,
                            //(x + 1) * 2, h_a[3], (y + 1) * 2, 1.0f, 0.0f, 0.0f, tile.textureStart[0], tile.textureEnd[0], lu1, lv1, 0, 0
                        });
                    }
                }
            }

            var mesh = new GND.Mesh();

            mesh.width    = gnd.width;
            mesh.height   = gnd.height;
            mesh.textures = gnd.textures;

            mesh.lightmap  = CreateLightmapImage(gnd);
            mesh.tileColor = CreateTilesColorImage(gnd);
            //mesh.shadowMap = CreateShadowmapData(gnd);

            mesh.mesh          = meshData.ToArray();
            mesh.meshVertCount = meshData.Count / 12;

            mesh.waterMesh      = waterMeshData.ToArray();
            mesh.waterVertCount = waterMeshData.Count / 5;

            return(mesh);
        }