1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package com.jme3.terrain.geomipmap.grid;
6 
7 import com.jme3.asset.AssetManager;
8 import com.jme3.export.InputCapsule;
9 import com.jme3.export.JmeExporter;
10 import com.jme3.export.JmeImporter;
11 import com.jme3.export.OutputCapsule;
12 import com.jme3.math.Vector3f;
13 import com.jme3.terrain.geomipmap.TerrainGridTileLoader;
14 import com.jme3.terrain.geomipmap.TerrainQuad;
15 import java.io.IOException;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
18 
19 /**
20  *
21  * @author normenhansen
22  */
23 public 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 
AssetTileLoader()32     public AssetTileLoader() {
33     }
34 
AssetTileLoader(AssetManager manager, String name, String assetPath)35     public AssetTileLoader(AssetManager manager, String name, String assetPath) {
36         this.manager = manager;
37         this.name = name;
38         this.assetPath = assetPath;
39     }
40 
getTerrainQuadAt(Vector3f location)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 
getAssetPath()59     public String getAssetPath() {
60         return assetPath;
61     }
62 
getName()63     public String getName() {
64         return name;
65     }
66 
setPatchSize(int patchSize)67     public void setPatchSize(int patchSize) {
68         this.patchSize = patchSize;
69     }
70 
setQuadSize(int quadSize)71     public void setQuadSize(int quadSize) {
72         this.quadSize = quadSize;
73     }
74 
createNewQuad(Vector3f location)75     private TerrainQuad createNewQuad(Vector3f location) {
76         TerrainQuad q = new TerrainQuad("Quad" + location, patchSize, quadSize, null);
77         return q;
78     }
79 
write(JmeExporter ex)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 
read(JmeImporter im)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 }