فهرست منبع

Update terrain : Dispose -> Remove

ShiroSmith 6 سال پیش
والد
کامیت
34bb7df8c2
3فایلهای تغییر یافته به همراه90 افزوده شده و 88 حذف شده
  1. 7 7
      h3d/scene/pbr/terrain/Surface.hx
  2. 69 66
      h3d/scene/pbr/terrain/Terrain.hx
  3. 14 15
      h3d/scene/pbr/terrain/Tile.hx

+ 7 - 7
h3d/scene/pbr/terrain/Surface.hx

@@ -10,7 +10,7 @@ class Surface {
 	public var minHeight = 0.0;
 	public var maxHeight = 1.0;
 
-	public function new(?albedo : h3d.mat.Texture, ?normal : h3d.mat.Texture, ?pbr : h3d.mat.Texture){
+	public function new( ?albedo : h3d.mat.Texture, ?normal : h3d.mat.Texture, ?pbr : h3d.mat.Texture ) {
 		this.albedo = albedo;
 		this.normal = normal;
 		this.pbr = pbr;
@@ -28,9 +28,9 @@ class Surface {
 	}
 
 	public function dispose() {
-		if(albedo != null) albedo.dispose();
-		if(normal != null) normal.dispose();
-		if(pbr != null) pbr.dispose();
+		if( albedo != null ) albedo.dispose();
+		if( normal != null ) normal.dispose();
+		if( pbr != null ) pbr.dispose();
 	}
 }
 
@@ -64,8 +64,8 @@ class SurfaceArray {
 	}
 
 	public function dispose() {
-		if(albedo != null) albedo.dispose();
-		if(normal != null) normal.dispose();
-		if(pbr != null) pbr.dispose();
+		if( albedo != null ) albedo.dispose();
+		if( normal != null ) normal.dispose();
+		if( pbr != null ) pbr.dispose();
 	}
 }

+ 69 - 66
h3d/scene/pbr/terrain/Terrain.hx

@@ -15,7 +15,7 @@ class Terrain extends Object {
 	public var parallaxMaxStep : Int;
 	public var heightBlendStrength : Float;
 	public var blendSharpness : Float;
-	public var tiles: Array<Tile> = [];
+	public var tiles : Array<Tile> = [];
 	public var surfaces : Array<Surface> = [];
 	public var surfaceArray : h3d.scene.pbr.terrain.Surface.SurfaceArray;
 	public var copyPass : h3d.pass.Copy;
@@ -25,6 +25,14 @@ class Terrain extends Object {
 		copyPass = new h3d.pass.Copy();
 	}
 
+	override function onRemove() {
+		super.onRemove();
+		for( s in surfaces )
+			s.dispose();
+		if( surfaceArray != null )
+			surfaceArray.dispose();
+	}
+
 	public override function clone( ?o : h3d.scene.Object ) : h3d.scene.Object {
 		var o = new Terrain();
 		o.tileSize = tileSize;
@@ -41,7 +49,7 @@ class Terrain extends Object {
 		o.heightBlendStrength = heightBlendStrength;
 		o.blendSharpness = blendSharpness;
 
-		for( i in 0...tiles.length ){
+		for( i in 0...tiles.length ) {
 			var t = Std.instance(tiles[i].clone(), Tile);
 			t.parent = o;
 			o.tiles.push(t);
@@ -55,10 +63,10 @@ class Terrain extends Object {
 		return o;
 	}
 
-	public function getHeight(x : Float, y : Float) : Float {
+	public function getHeight( x : Float, y : Float ) : Float {
 		var z = 0.0;
 		var t = getTileAtWorldPos(x, y);
-		if(t != null){
+		if( t != null ) {
 			tmpVec.set(x, y);
 			var pos = t.globalToLocal(tmpVec);
 			z = t.getHeight(pos.x / tileSize, pos.y / tileSize);
@@ -66,89 +74,89 @@ class Terrain extends Object {
 		return z;
 	}
 
-	public function getSurface(i : Int) : Surface{
+	public function getSurface( i : Int ) : Surface {
 		if(i < surfaces.length)
 				return surfaces[i];
 		return null;
 	}
 
-	public function getSurfaceFromTex(albedo, ?normal, ?pbr) : Surface{
-		for(s in surfaces){
+	public function getSurfaceFromTex( albedo, ?normal, ?pbr ) : Surface {
+		for( s in surfaces ) {
 			var valid = false;
 			valid = s.albedo.name == albedo;
-			valid = valid && !(normal != null && s.normal.name != normal);
-			valid = valid && !(pbr != null && s.pbr.name != pbr);
-			if(valid) return s;
+			valid = valid && !( normal != null && s.normal.name != normal );
+			valid = valid && !( pbr != null && s.pbr.name != pbr );
+			if( valid ) return s;
 		}
 		return null;
 	}
 
-	public function addSurface(albedo, normal, pbr) : Surface {
+	public function addSurface( albedo, normal, pbr ) : Surface {
 		surfaces.push(new Surface(albedo, normal, pbr));
 		return surfaces[surfaces.length - 1];
 	}
 
 	public function addEmptySurface() : Surface {
-		surfaces.push(new Surface());
+		surfaces.push( new Surface() );
 		return surfaces[surfaces.length - 1];
 	}
 
 	public function generateSurfaceArray() {
-		if(surfaces.length == 0) return;
+		if( surfaces.length == 0 ) return;
 		var surfaceSize = 1;
-		for(i in 0 ... surfaces.length)
-			if(surfaces[i].albedo != null) surfaceSize = hxd.Math.ceil(hxd.Math.max(surfaces[i].albedo.width, surfaceSize));
+		for( i in 0 ... surfaces.length )
+			if( surfaces[i].albedo != null ) surfaceSize = hxd.Math.ceil(hxd.Math.max(surfaces[i].albedo.width, surfaceSize));
 
 		if(surfaceArray != null) surfaceArray.dispose();
 		surfaceArray = new h3d.scene.pbr.terrain.Surface.SurfaceArray(surfaces.length, surfaceSize);
-		for(i in 0 ... surfaces.length){
-			if(surfaces[i].albedo != null) copyPass.apply(surfaces[i].albedo, surfaceArray.albedo, null, null, i);
-			if(surfaces[i].normal != null) copyPass.apply(surfaces[i].normal, surfaceArray.normal, null, null, i);
-			if(surfaces[i].pbr != null) copyPass.apply(surfaces[i].pbr, surfaceArray.pbr, null, null, i);
+		for( i in 0 ... surfaces.length ) {
+			if( surfaces[i].albedo != null ) copyPass.apply(surfaces[i].albedo, surfaceArray.albedo, null, null, i);
+			if( surfaces[i].normal != null ) copyPass.apply(surfaces[i].normal, surfaceArray.normal, null, null, i);
+			if( surfaces[i].pbr != null ) copyPass.apply(surfaces[i].pbr, surfaceArray.pbr, null, null, i);
 		}
 		updateSurfaceParams();
 		refreshTex();
 	}
 
-	public function updateSurfaceParams(){
-		for(i in 0 ... surfaces.length){
+	public function updateSurfaceParams() {
+		for( i in 0 ... surfaces.length ) {
 			surfaceArray.params[i] = new h3d.Vector(surfaces[i].tilling, surfaces[i].offset.x, surfaces[i].offset.y, hxd.Math.degToRad(surfaces[i].angle));
 			surfaceArray.secondParams[i] = new h3d.Vector(surfaces[i].minHeight, surfaces[i].maxHeight, 0, 0);
 		}
 	}
 
-	public function refreshTiles(){
-		for(tile in tiles)
-			if(tile.needAlloc){
+	public function refreshTiles() {
+		for( tile in tiles )
+			if( tile.needAlloc ) {
 				tile.grid.alloc(h3d.Engine.getCurrent());
 				tile.needAlloc = false;
 			}
 	}
 
-	public function refreshMesh(){
-		for(tile in tiles){
+	public function refreshMesh() {
+		for( tile in tiles ) {
 			tile.x = tile.tileX * tileSize;
 			tile.y = tile.tileY * tileSize;
 			tile.refreshMesh();
 		}
-		for(tile in tiles)
+		for( tile in tiles )
 			tile.blendEdges();
 	}
 
-	public function refreshTex(){
-		for(tile in tiles){
+	public function refreshTex() {
+		for( tile in tiles ) {
 			tile.refresh();
 		}
 	}
 
-	public function refresh(){
+	public function refresh() {
 		refreshMesh();
 		refreshTex();
 	}
 
-	public function createTile(x : Int, y : Int) : Tile {
+	public function createTile( x : Int, y : Int ) : Tile {
 		var tile = getTile(x,y);
-		if(tile == null){
+		if( tile == null ) {
 			tile = new Tile(x, y, this);
 			tile.refreshMesh();
 			tile.refresh();
@@ -157,11 +165,11 @@ class Terrain extends Object {
 		return tile;
 	}
 
-	public function addTile(tile : Tile, ?replace = false){
-		for(t in tiles){
-			if(tile == t) return;
-			if(tile.tileX == t.tileX && tile.tileY == t.tileY){
-				if(replace){
+	public function addTile( tile : Tile, ?replace = false ) {
+		for( t in tiles ) {
+			if( tile == t ) return;
+			if( tile.tileX == t.tileX && tile.tileY == t.tileY ) {
+				if( replace ) {
 					removeTile(t);
 					break;
 				}else
@@ -173,46 +181,46 @@ class Terrain extends Object {
 		addChild(tile);
 	}
 
-	public function removeTileAt(x : Int, y : Int) : Bool {
+	public function removeTileAt( x : Int, y : Int ) : Bool {
 		var t = getTile(x,y);
-		if(t == null){
+		if( t == null ) {
 			removeTile(t);
 			return true;
 		}
 		return false;
 	}
 
-	public function removeTile(t : Tile) : Bool {
-		if(t == null) return false;
+	public function removeTile( t : Tile ) : Bool {
+		if( t == null ) return false;
 		var r = tiles.remove(t);
-		if(r) t.remove();
+		if( r ) t.remove();
 		return r;
 	}
 
-	public function getTileIndex(t : Tile) : Int {
-		for(i in 0 ... tiles.length)
-			if(t == tiles[i]) return i;
+	public function getTileIndex( t : Tile ) : Int {
+		for( i in 0 ... tiles.length )
+			if( t == tiles[i] ) return i;
 		return -1;
 	}
 
-	public function getTile(x : Int, y : Int) : Tile {
+	public function getTile( x : Int, y : Int ) : Tile {
 		var result : Tile = null;
-		for(tile in tiles)
-			if(tile.tileX == x && tile.tileY == y) result = tile;
+		for( tile in tiles )
+			if( tile.tileX == x && tile.tileY == y ) result = tile;
 		return result;
 	}
 
-	public function getTileAtWorldPos(x : Float, y : Float) : Tile {
+	public function getTileAtWorldPos( x : Float, y : Float ) : Tile {
 		var pos = toLocalPos(x, y);
 		var result : Tile = null;
 		var tileX = Math.floor(pos.x / tileSize);
 		var tileY = Math.floor(pos.y / tileSize);
-		for(tile in tiles)
-			if(tile.tileX == tileX && tile.tileY == tileY) result = tile;
+		for( tile in tiles )
+			if( tile.tileX == tileX && tile.tileY == tileY ) result = tile;
 		return result;
 	}
 
-	public function createTileAtWorldPos(x : Float, y : Float) : Tile {
+	public function createTileAtWorldPos( x : Float, y : Float ) : Tile {
 		var pos = toLocalPos(x, y);
 		var tileX = Math.floor(pos.x / tileSize);
 		var tileY = Math.floor(pos.y / tileSize);
@@ -220,9 +228,9 @@ class Terrain extends Object {
 		return result == null ? createTile(tileX, tileY) : result;
 	}
 
-	public function getTiles(x : Float, y : Float, range : Float, ?create = false) : Array<Tile> {
+	public function getTiles( x : Float, y : Float, range : Float, ?create = false ) : Array<Tile> {
 		var pos = toLocalPos(x, y);
-		if(create != null && create){
+		if( create != null && create ) {
 			var maxTileX = Math.floor((pos.x + range)/ tileSize);
 			var minTileX = Math.floor((pos.x - range)/ tileSize);
 			var maxTileY = Math.floor((pos.y + range)/ tileSize);
@@ -232,30 +240,25 @@ class Terrain extends Object {
 					createTile(x, y);
 		}
 		var result : Array<Tile> = [];
-		for(tile in tiles)
+		for( tile in tiles)
 			if( Math.abs(pos.x - (tile.tileX * tileSize + tileSize * 0.5)) <= range + (tileSize * 0.5)
-			&& Math.abs(pos.y - (tile.tileY * tileSize + tileSize * 0.5)) <= range + (tileSize * 0.5))
+			&& Math.abs(pos.y - (tile.tileY * tileSize + tileSize * 0.5)) <= range + (tileSize * 0.5) )
 				result.push(tile);
 		return result;
 	}
 
-	public function getVisibleTiles(c : Camera) : Array<Tile> {
+	public function getVisibleTiles( c : Camera ) : Array<Tile> {
 		var res = [];
-		var bounds : h3d.col.Bounds = null;
-		for(tile in tiles){
-			if(bounds == null){
-				bounds = tile.getBounds();
-				bounds.zMax = 10000;
-				bounds.zMin = -10000;
-			}
-			if(c.frustum.hasBounds(bounds))
+		for( tile in tiles ) {
+			var bounds = tile.getBounds();
+			if( c.frustum.hasBounds(bounds) )
 				res.push(tile);
 		}
 		return res;
 	}
 
 	static var tmpVec = new h3d.Vector();
-	inline function toLocalPos(x : Float, y : Float) {
+	inline function toLocalPos( x : Float, y : Float ) {
 		tmpVec.set(x, y);
 		globalToLocal(tmpVec);
 		return tmpVec;

+ 14 - 15
h3d/scene/pbr/terrain/Tile.hx

@@ -32,6 +32,13 @@ class Tile extends h3d.scene.Mesh {
 		name = "tile_" + x + "_" + y;
 	}
 
+	override function onRemove() {
+		if( heightMap != null ) heightMap.dispose();
+		if( surfaceIndexMap != null ) surfaceIndexMap.dispose();
+		for( i in 0 ... surfaceWeights.length )
+			if( surfaceWeights[i] != null ) surfaceWeights[i].dispose();
+	}
+
 	public override function clone( ?o : h3d.scene.Object ) : h3d.scene.Object {
 		var o = new Tile(tileX, tileY, parent);
 		o.heightMap = heightMap.clone();
@@ -106,7 +113,7 @@ class Tile extends h3d.scene.Mesh {
 		computeEdgesNormals();
 	}
 
-	public function refresh(){
+	public function refresh() {
 		if( heightMap == null || heightMap.width != getTerrain().heightMapResolution + 1 ) {
 			var oldHeightMap = heightMap;
 			heightMap = new h3d.mat.Texture(getTerrain().heightMapResolution + 1, getTerrain().heightMapResolution + 1, [Target], RGBA32F );
@@ -164,7 +171,7 @@ class Tile extends h3d.scene.Mesh {
 			if( surfaceWeights[i] != null ) getTerrain().copyPass.apply(surfaceWeights[i], surfaceWeightArray, None, null, i);
 	}
 
-	public function computeEdgesHeight(flag : haxe.EnumFlags<Direction>){
+	public function computeEdgesHeight( flag : haxe.EnumFlags<Direction> ) {
 
 		if( heightMap == null ) return;
 		var pixels : hxd.Pixels.PixelsFloat = getHeightPixels();
@@ -471,7 +478,7 @@ class Tile extends h3d.scene.Mesh {
 		needAlloc = true;
 	}
 
-	public function getHeight(u : Float, v : Float, ?fast = false) : Float {
+	public function getHeight( u : Float, v : Float, ?fast = false ) : Float {
 		var pixels = getHeightPixels();
 		if( pixels == null ) return 0.0;
 		if( heightMap.filter == Linear && !fast ) {
@@ -499,14 +506,6 @@ class Tile extends h3d.scene.Mesh {
 			return pixels.getPixelF(x, y).r;
 		}
 	}
-
-	public function dispose() {
-		if( heightMap != null ) heightMap.dispose();
-		if( surfaceIndexMap != null ) surfaceIndexMap.dispose();
-		for( i in 0 ... surfaceWeights.length )
-			if( surfaceWeights[i] != null ) surfaceWeights[i].dispose();
-	}
-
 	var cachedBounds : h3d.col.Bounds;
 	var cachedHeightBound : Bool = false;
 	function computeBounds() {
@@ -515,7 +514,7 @@ class Tile extends h3d.scene.Mesh {
 			cachedBounds.zMax = 0;
 			cachedBounds.zMin = 0;
 		}
-		if( cachedBounds != null && cachedHeightBound == false && heightMap != null ){
+		if( cachedBounds != null && cachedHeightBound == false && heightMap != null ) {
 			for( u in 0 ... heightMap.width ) {
 				for( v in 0 ... heightMap.height ) {
 					var h = getHeight(u / heightMap.width, v / heightMap.height, true);
@@ -531,7 +530,7 @@ class Tile extends h3d.scene.Mesh {
 	}
 
 	public dynamic function beforeEmit() : Bool { return true; };
-	override function emit( ctx:RenderContext ){
+	override function emit( ctx:RenderContext ) {
 		if( !isReady() ) return;
 		computeBounds();
 		insideFrustrum = ctx.camera.frustum.hasBounds(cachedBounds);
@@ -553,7 +552,7 @@ class Tile extends h3d.scene.Mesh {
 		shader.primSize = getTerrain().tileSize;
 		shader.cellSize = getTerrain().cellSize;
 
-		if( !shader.CHECKER && !shader.COMPLEXITY ){
+		if( !shader.CHECKER && !shader.COMPLEXITY ) {
 			shader.albedoTextures = getTerrain().surfaceArray.albedo;
 			shader.normalTextures = getTerrain().surfaceArray.normal;
 			shader.pbrTextures = getTerrain().surfaceArray.pbr;
@@ -572,7 +571,7 @@ class Tile extends h3d.scene.Mesh {
 		}
 	}
 
-	function isReady(){
+	function isReady() {
 		if( primitive == null )
 			return false;