浏览代码

Polygon prefab: cache primitive

trethaller 7 年之前
父节点
当前提交
abe017f2aa
共有 1 个文件被更改,包括 33 次插入28 次删除
  1. 33 28
      hide/prefab/l3d/Polygon.hx

+ 33 - 28
hide/prefab/l3d/Polygon.hx

@@ -4,6 +4,8 @@ import h3d.col.Point;
 class Polygon extends Object3D {
 
 	var data : Array<Float> = null;
+	var primitive : h3d.prim.Polygon;  // TODO: When to dispose? https://github.com/HeapsIO/heaps/issues/336
+
 	public var diffuseMap : String;
 	public var normalMap : String;
 	public var specularMap : String;
@@ -33,13 +35,11 @@ class Polygon extends Object3D {
 		mat.mainPass.culling = None;
 		mat.shadows = false;
 
-		if(diffuseMap == null || diffuseMap.length == 0) {
-			var layer = getParent(Layer);
+		var layer = getParent(Layer);
+		if(layer != null && (diffuseMap == null || diffuseMap.length == 0)) {
 			setColor(ctx, layer != null ? (layer.color | 0x40000000) : 0x40ff00ff);
 		}
 		else {
-			setColor(ctx, 0xffffffff);
-
 			inline function getTex(path: String) {
 				var t = path != null && path.length > 0 ? ctx.loadTexture(path) : null;
 				if(t != null)
@@ -49,37 +49,42 @@ class Polygon extends Object3D {
 			mat.texture = getTex(diffuseMap);
 			mat.normalMap = getTex(normalMap);
 			mat.specularTexture = getTex(specularMap);
+			mat.color.setColor(0xffffffff);
 		}
 	}
 
 	override function makeInstance(ctx:Context):Context {
 		ctx = ctx.clone(this);
-		var layer = getParent(Layer);
-		var points = [];
-		var d = this.data;
-		if(d == null) {
-			d = [-0.5, -0.5,
-				  0.5, -0.5,
-				  0.5,  0.5,
-				 -0.5,  0.5];
-		}
-		var npts = Std.int(d.length / 2);
-		for(i in 0...npts) {
-			var x = d[(i<<1)];
-			var y = d[(i<<1) + 1];
-			var vert = new h2d.col.Point(x, y);
-			points.push(vert);
+
+		if(primitive == null) {
+			var points = [];
+			var d = this.data;
+			if(d == null) {
+				d = [-0.5, -0.5,
+					0.5, -0.5,
+					0.5,  0.5,
+					-0.5,  0.5];
+			}
+			var npts = Std.int(d.length / 2);
+			for(i in 0...npts) {
+				var x = d[(i<<1)];
+				var y = d[(i<<1) + 1];
+				var vert = new h2d.col.Point(x, y);
+				points.push(vert);
+			}
+			var poly2d = new h2d.col.Polygon(points);
+			var indices = poly2d.fastTriangulate();
+			var verts = [for(p in points) new h3d.col.Point(p.x, p.y, 0.)];
+			primitive = new h3d.prim.Polygon(verts, cast indices);
+			var n = new h3d.col.Point(0, 0, 1.);
+			primitive.normals = [for(p in points) n];
+			primitive.uvs = [for(p in points) new h3d.prim.UV(p.x + 0.5, p.y + 0.5)];
+			primitive.colors = [for(p in points) new h3d.col.Point(1,1,1)];
 		}
-		var poly2d = new h2d.col.Polygon(points);
-		var indices = poly2d.fastTriangulate();
-		var verts = [for(p in points) new h3d.col.Point(p.x, p.y, 0.)];
-		var prim = new h3d.prim.Polygon(verts, cast indices);
-		var n = new h3d.col.Point(0, 0, 1.);
-		prim.normals = [for(p in points) n];
-		prim.uvs = [for(p in points) new h3d.prim.UV(p.x, p.y)];
-		prim.colors = [for(p in points) new h3d.col.Point(1,1,1)];
 
-		var mesh = new h3d.scene.Mesh(prim, ctx.local3d);
+		var mesh = new h3d.scene.Mesh(primitive, ctx.local3d);
+		mesh.material.props = h3d.mat.MaterialSetup.current.getDefaults("ui");
+		mesh.material.blendMode = Alpha;
 		ctx.local3d = mesh;
 		ctx.local3d.name = name;
 		applyPos(ctx.local3d);