Browse Source

Add some clone()

ShiroSmith 6 years ago
parent
commit
6a77f6ff27

+ 6 - 0
h3d/col/IPoint.hx

@@ -23,4 +23,10 @@ class IPoint {
 		this.z = z;
 	}
 
+	public inline function load( p : IPoint ) {
+		this.x = p.x;
+		this.y = p.y;
+		this.z = p.z;
+	}
+
 }

+ 1 - 1
h3d/prim/Plane2D.hx

@@ -2,7 +2,7 @@ package h3d.prim;
 
 class Plane2D extends Primitive {
 
-	function new() {
+	public function new() {
 	}
 
 	override function triCount() {

+ 13 - 0
h3d/scene/Box.hx

@@ -18,6 +18,19 @@ class Box extends Graphics {
 		if( !depth ) material.mainPass.depth(true, Always);
 	}
 
+	public override function clone( ?o : h3d.scene.Object ) : h3d.scene.Object {
+		var b = o == null ? new Box(color, bounds.clone(), material.mainPass.depthWrite, null) : cast o;
+		super.clone(b);
+		b.bounds = bounds.clone();
+		b.prevXMin = prevXMin;
+		b.prevYMin = prevYMin;
+		b.prevZMin = prevZMin;
+		b.prevXMax = prevXMax;
+		b.prevYMax = prevYMax;
+		b.prevZMax = prevZMax;
+		return b;
+	}
+
 	override function getLocalCollider() {
 		return null;
 	}

+ 6 - 0
h3d/scene/pbr/DirLight.hx

@@ -11,6 +11,12 @@ class DirLight extends Light {
 		if( dir != null ) setDirection(dir);
 	}
 
+	public override function clone( ?o : h3d.scene.Object ) : h3d.scene.Object {
+		var dl = o == null ? new DirLight(null) : cast o;
+		super.clone(dl);
+		return dl;
+	}
+
 	override function getShadowDirection() : h3d.Vector {
 		return absPos.front();
 	}

+ 8 - 0
h3d/scene/pbr/PointLight.hx

@@ -17,6 +17,14 @@ class PointLight extends Light {
 		primitive = h3d.prim.Sphere.defaultUnitSphere();
 	}
 
+	public override function clone( ?o : h3d.scene.Object ) : h3d.scene.Object {
+		var pl = o == null ? new PointLight(null) : cast o;
+		super.clone(pl);
+		pl.size = size;
+		pl.range = range;
+		return pl;
+	}
+
 	function get_range() {
 		return cullingDistance;
 	}

+ 12 - 0
h3d/scene/pbr/SpotLight.hx

@@ -21,6 +21,18 @@ class SpotLight extends Light {
 		lightProj.screenRatio = 1.0;
 	}
 
+	public override function clone( ?o : h3d.scene.Object ) : h3d.scene.Object {
+		var sl = o == null ? new SpotLight(null) : cast o;
+		super.clone(sl);
+		sl.range = range;
+		sl.maxRange = maxRange;
+		sl.angle = angle;
+		sl.fallOff = fallOff;
+		sl.cookie = cookie;
+		sl.lightProj.load(lightProj);
+		return sl;
+	}
+
 	function get_maxRange() {
 		return cullingDistance;
 	}

+ 11 - 0
h3d/scene/pbr/VolumetricLightmap.hx

@@ -42,6 +42,17 @@ class VolumetricLightmap extends h3d.scene.Mesh {
 		voxelSize = new h3d.Vector(1,1,1);
 	}
 
+	public override function clone( ?o : h3d.scene.Object ) : h3d.scene.Object {
+		var vm = o == null ? new VolumetricLightmap(null) : cast o;
+		vm.shOrder = shOrder;
+		vm.strength = strength;
+		vm.useAlignedProb = useAlignedProb;
+		vm.lightProbeTexture = lightProbeTexture != null ? lightProbeTexture.clone() : null;
+		vm.probeCount.load(probeCount);
+		vm.voxelSize.load(voxelSize);
+		return vm;
+	}
+
 	public function getProbeSH(coords : h3d.col.IPoint, ?pixels : hxd.Pixels.PixelsFloat ) : SphericalHarmonic {
 
 		if(lightProbeTexture == null)

+ 13 - 0
h3d/scene/pbr/terrain/Surface.hx

@@ -15,6 +15,14 @@ class Surface {
 		this.offset = new h3d.Vector(0);
 	}
 
+	public function clone() : Surface {
+		var o = new Surface(albedo, normal, pbr);
+		o.tilling = tilling;
+		o.offset.load(offset);
+		o.angle = angle;
+		return o;
+	}
+
 	public function dispose() {
 		if(albedo != null) albedo.dispose();
 		if(normal != null) normal.dispose();
@@ -38,6 +46,11 @@ class SurfaceArray {
 		pbr.wrap = Repeat;
 	}
 
+	public function clone() : SurfaceArray {
+		var o = new SurfaceArray(albedo.layerCount, albedo.width);
+		return o;
+	}
+
 	public function dispose() {
 		if(albedo != null) albedo.dispose();
 		if(normal != null) normal.dispose();

+ 31 - 0
h3d/scene/pbr/terrain/Terrain.hx

@@ -25,6 +25,37 @@ class Terrain extends Object {
 		copyPass = new h3d.pass.Copy();
 	}
 
+	public override function clone( ?o : h3d.scene.Object ) : h3d.scene.Object {
+		var o = new Terrain();
+		o.tileSize = tileSize;
+		o.cellSize = cellSize;
+		o.cellCount = cellCount;
+		o.heightMapResolution = heightMapResolution;
+		o.weightMapResolution = weightMapResolution;
+		o.showGrid = showGrid;
+		o.showChecker = showChecker;
+		o.showComplexity = showComplexity;
+		o.parallaxAmount = parallaxAmount;
+		o.parallaxMinStep = parallaxMinStep;
+		o.parallaxMaxStep = parallaxMaxStep;
+		o.heightBlendStrength = heightBlendStrength;
+		o.heightBlendSharpness = heightBlendSharpness;
+
+		o.tiles.resize(tiles.length);
+		for( i in 0...tiles.length ){
+			o.tiles[i] = Std.instance(tiles[i].clone(), Tile);
+			o.tiles[i].parent = o;
+		}
+
+		o.surfaces.resize(surfaces.length);
+		for( i in 0...surfaces.length )
+			o.surfaces[i] = surfaces[i].clone();
+
+		o.surfaceArray = surfaceArray.clone();
+
+		return o;
+	}
+
 	public function getHeight(x : Float, y : Float) : Float {
 		var z = 0.0;
 		var t = getTileAtWorldPos(x, y);

+ 20 - 0
h3d/scene/pbr/terrain/Tile.hx

@@ -30,6 +30,26 @@ class Tile extends h3d.scene.Mesh {
 		name = "tile_" + x + "_" + y;
 	}
 
+	public override function clone( ?o : h3d.scene.Object ) : h3d.scene.Object {
+		var o = new Tile(tileX, tileY, parent);
+		o.heightMap = heightMap.clone();
+		o.surfaceIndexMap = surfaceIndexMap.clone();
+
+		o.surfaceWeights.resize(surfaceWeights.length);
+		for( i in 0...surfaceWeights.length )
+			o.surfaceWeights[i] = surfaceWeights[i].clone();
+
+		o.surfaceWeightArray = new h3d.mat.TextureArray(getTerrain().weightMapResolution, getTerrain().weightMapResolution, surfaceWeights.length, [Target], R8);
+		o.surfaceWeightArray.wrap = Clamp;
+		o.surfaceWeightArray.preventAutoDispose();
+		o.surfaceWeightArray.realloc = null;
+		for(i in 0 ... surfaceWeights.length)
+			if(surfaceWeights[i] != null) getTerrain().copyPass.apply(surfaceWeights[i], o.surfaceWeightArray, None, null, i);
+
+		o.heightmapPixels = heightmapPixels.clone();
+		return o;
+	}
+
 	function set_heightMap(v){
 		shader.heightMap = v;
 		return heightMap = v;