Forráskód Böngészése

Merge pull request #7 from ncannasse/master

sync
Mathieu Capdegelle 9 éve
szülő
commit
2829d503fc
3 módosított fájl, 83 hozzáadás és 32 törlés
  1. 3 3
      h2d/Flow.hx
  2. 16 11
      h3d/Matrix.hx
  3. 64 18
      h3d/scene/World.hx

+ 3 - 3
h2d/Flow.hx

@@ -454,7 +454,7 @@ class Flow extends Sprite {
 					px = xmax;
 					xmax -= horitontalSpacing;
 				case Middle:
-					if( midSpace != 0 ) {
+					if( midSpace == 0 ) {
 						var remSize = p.calculatedWidth;
 						for( j in i + 1...childs.length ) {
 							var p = properties[i];
@@ -545,7 +545,7 @@ class Flow extends Sprite {
 			cw = x + maxColWidth + paddingRight + borderWidth;
 
 
-			// horizontal align
+			// vertical align
 			if( minHeight != null && ch < minHeight ) ch = minHeight;
 			var endY = ch - (paddingBottom + borderHeight);
 			var ymin : Float = startY, ymax : Float = endY;
@@ -570,7 +570,7 @@ class Flow extends Sprite {
 					py = ymax;
 					ymax -= verticalSpacing;
 				case Middle:
-					if( midSpace != 0 ) {
+					if( midSpace == 0 ) {
 						var remSize = p.calculatedHeight;
 						for( j in i + 1...childs.length ) {
 							var p = properties[i];

+ 16 - 11
h3d/Matrix.hx

@@ -465,20 +465,25 @@ class Matrix {
 	static inline var lumG = 0.71516;
 	static inline var lumB = 0.072169;
 
+	static inline var SQ13 = 0.57735026918962576450914878050196; // sqrt(1/3)
 	public function colorHue( hue : Float ) {
 		if( hue == 0. )
 			return;
-		var cv = Math.cos(hue);
-		var sv = Math.sin(hue);
-		tmp._11 = lumR + cv * (1 - lumR) - sv * lumR;
-		tmp._12 = lumR - cv * lumR + sv * 0.143;
-		tmp._13 = lumR - cv * lumR - sv * (1 - lumR);
-		tmp._21 = lumG - cv * lumG - sv * lumG;
-		tmp._22 = lumG + cv * (1 - lumG) + sv * 0.140;
-		tmp._23 = lumG - cv * lumG + sv * lumG;
-		tmp._31 = lumB - cv * lumB - sv * lumB;
-		tmp._32 = lumB - cv * lumB - sv * 0.283;
-		tmp._33 = lumB + cv * (1 - lumB) + sv * lumB;
+
+		var cosA = Math.cos(-hue);
+		var sinA = Math.sin(-hue);
+		var ch = (1 - cosA) / 3;
+
+		tmp._11 = cosA + ch;
+		tmp._12 = ch - SQ13 * sinA;
+		tmp._13 = ch + SQ13 * sinA;
+		tmp._21 = ch + SQ13 * sinA;
+		tmp._22 = cosA + ch;
+		tmp._23 = ch - SQ13 * sinA;
+		tmp._31 = ch - SQ13 * sinA;
+		tmp._32 = ch + SQ13 * sinA;
+		tmp._33 = cosA + ch;
+
 		tmp._34 = 0;
 		tmp._41 = 0;
 		tmp._42 = 0;

+ 64 - 18
h3d/scene/World.hx

@@ -1,5 +1,22 @@
 package h3d.scene;
 
+class WorldElement {
+	public var model : WorldModel;
+	public var x : Float;
+	public var y : Float;
+	public var z : Float;
+	public var scale : Float;
+	public var rotation : Float;
+	public function new( model, x, y, z, scale = 1., rotation = 0. ) {
+		this.model = model;
+		this.x = x;
+		this.y = y;
+		this.z = z;
+		this.scale = scale;
+		this.rotation = rotation;
+	}
+}
+
 class WorldChunk {
 
 	public var cx : Int;
@@ -12,10 +29,12 @@ class WorldChunk {
 	public var bounds : h3d.col.Bounds;
 	public var initialized = false;
 	public var lastFrame : Int;
+	public var elements : Array<WorldElement>;
 
 	public function new(cx, cy) {
 		this.cx = cx;
 		this.cy = cy;
+		elements = [];
 		root = new h3d.scene.Object();
 		buffers = new Map();
 		bounds = new h3d.col.Bounds();
@@ -92,7 +111,7 @@ class World extends Object {
 	var textures : Map<String, WorldMaterial>;
 	public var enableSpecular = false;
 
-	public function new( chunkSize : Int, worldSize : Int, ?parent ) {
+	public function new( chunkSize : Int, worldSize : Int, ?parent, ?autoCollect = true ) {
 		super(parent);
 		chunks = [];
 		bigTextures = [];
@@ -104,6 +123,17 @@ class World extends Object {
 		this.chunkSize = 1 << chunkBits;
 		this.worldSize = worldSize;
 		this.worldStride = Math.ceil(worldSize / chunkSize);
+		if( autoCollect )
+			h3d.Engine.getCurrent().mem.garbage = garbage;
+	}
+
+	public function garbage() {
+		var last : WorldChunk = null;
+		for( c in allChunks )
+			if( c.initialized && (last == null || c.lastFrame < last.lastFrame) )
+				last = c;
+		if( last != null )
+			cleanChunk(last);
 	}
 
 	function buildFormat() {
@@ -294,7 +324,7 @@ class World extends Object {
 			}
 	}
 
-	function initChunk( c : WorldChunk ) {
+	function initChunkSoil( c : WorldChunk ) {
 		var cube = new h3d.prim.Cube(chunkSize, chunkSize, 0);
 		cube.addNormals();
 		cube.addUVs();
@@ -305,6 +335,33 @@ class World extends Object {
 		soil.material.shadows = true;
 	}
 
+	function initChunkElements( c : WorldChunk ) {
+		for( e in c.elements ) {
+			var model = e.model;
+			for( g in model.geometries ) {
+				var b = c.buffers.get(g.m.bits);
+				if( b == null ) {
+					b = new h3d.scene.Mesh(new h3d.prim.BigPrimitive(getStride(model), true), c.root);
+					b.name = g.m.name;
+					c.buffers.set(g.m.bits, b);
+					initMaterial(b, g.m);
+				}
+				var p = Std.instance(b.primitive, h3d.prim.BigPrimitive);
+				p.addSub(model.buf, model.idx, g.startVertex, Std.int(g.startIndex / 3), g.vertexCount, Std.int(g.indexCount / 3), e.x, e.y, e.z, e.rotation, e.scale, model.stride);
+			}
+		}
+	}
+
+	function cleanChunk( c : WorldChunk ) {
+		if( !c.initialized ) return;
+		c.initialized = false;
+		for( b in c.buffers ) {
+			b.dispose();
+			b.remove();
+		}
+		c.buffers = new Map();
+	}
+
 	function updateChunkBounds(c : WorldChunk, model : WorldModel, rotation : Float, scale : Float) {
 		var cosR = Math.cos(rotation);
 		var sinR = Math.sin(rotation);
@@ -362,19 +419,7 @@ class World extends Object {
 
 	public function add( model : WorldModel, x : Float, y : Float, z : Float, scale = 1., rotation = 0. ) {
 		var c = getChunk(x, y, true);
-
-		for( g in model.geometries ) {
-			var b = c.buffers.get(g.m.bits);
-			if( b == null ) {
-				b = new h3d.scene.Mesh(new h3d.prim.BigPrimitive(getStride(model), true), c.root);
-				b.name = g.m.name;
-				c.buffers.set(g.m.bits, b);
-				initMaterial(b, g.m);
-			}
-			var p = Std.instance(b.primitive, h3d.prim.BigPrimitive);
-			p.addSub(model.buf, model.idx, g.startVertex, Std.int(g.startIndex / 3), g.vertexCount, Std.int(g.indexCount / 3), x, y, z, rotation, scale, model.stride);
-		}
-
+		c.elements.push(new WorldElement(model, x, y, z, scale, rotation));
 		updateChunkBounds(c, model, rotation, scale);
 	}
 
@@ -384,11 +429,12 @@ class World extends Object {
 		for( c in allChunks ) {
 			c.root.visible = c.bounds.inFrustum(ctx.camera.m);
 			if( c.root.visible ) {
-				if( !c.initialized) {
+				c.lastFrame = ctx.frame;
+				if( !c.initialized ) {
 					c.initialized = true;
-					initChunk(c);
+					initChunkSoil(c);
+					initChunkElements(c);
 				}
-				c.lastFrame = ctx.frame;
 			}
 		}
 	}