1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.jme3.terrain.geomipmap.grid;
6
7import com.jme3.asset.AssetManager;
8import com.jme3.export.InputCapsule;
9import com.jme3.export.JmeExporter;
10import com.jme3.export.JmeImporter;
11import com.jme3.export.OutputCapsule;
12import com.jme3.math.Vector3f;
13import com.jme3.terrain.geomipmap.TerrainGridTileLoader;
14import com.jme3.terrain.geomipmap.TerrainQuad;
15import java.io.IOException;
16import java.util.logging.Level;
17import java.util.logging.Logger;
18
19/**
20 *
21 * @author normenhansen
22 */
23public class AssetTileLoader implements TerrainGridTileLoader {
24
25    private AssetManager manager;
26    private String assetPath;
27    private String name;
28    private int size;
29    private int patchSize;
30    private int quadSize;
31
32    public AssetTileLoader() {
33    }
34
35    public AssetTileLoader(AssetManager manager, String name, String assetPath) {
36        this.manager = manager;
37        this.name = name;
38        this.assetPath = assetPath;
39    }
40
41    public TerrainQuad getTerrainQuadAt(Vector3f location) {
42        String modelName = assetPath + "/" + name + "_" + Math.round(location.x) + "_" + Math.round(location.y) + "_" + Math.round(location.z) + ".j3o";
43        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Load terrain grid tile: {0}", modelName);
44        TerrainQuad quad = null;
45        try {
46            quad = (TerrainQuad) manager.loadModel(modelName);
47        } catch (Exception e) {
48//            e.printStackTrace();
49        }
50        if (quad == null) {
51            Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Could not load terrain grid tile: {0}", modelName);
52            quad = createNewQuad(location);
53        } else {
54            Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Loaded terrain grid tile: {0}", modelName);
55        }
56        return quad;
57    }
58
59    public String getAssetPath() {
60        return assetPath;
61    }
62
63    public String getName() {
64        return name;
65    }
66
67    public void setPatchSize(int patchSize) {
68        this.patchSize = patchSize;
69    }
70
71    public void setQuadSize(int quadSize) {
72        this.quadSize = quadSize;
73    }
74
75    private TerrainQuad createNewQuad(Vector3f location) {
76        TerrainQuad q = new TerrainQuad("Quad" + location, patchSize, quadSize, null);
77        return q;
78    }
79
80    public void write(JmeExporter ex) throws IOException {
81        OutputCapsule c = ex.getCapsule(this);
82        c.write(assetPath, "assetPath", null);
83        c.write(name, "name", null);
84    }
85
86    public void read(JmeImporter im) throws IOException {
87        InputCapsule c = im.getCapsule(this);
88        manager = im.getAssetManager();
89        assetPath = c.readString("assetPath", null);
90        name = c.readString("name", null);
91    }
92}