Skip to content

Commit

Permalink
dispose native arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Mooibroek committed Feb 23, 2023
1 parent eff3aba commit 2c9c21f
Show file tree
Hide file tree
Showing 24 changed files with 326 additions and 61 deletions.
24 changes: 22 additions & 2 deletions lib/three3d/core/buffer_geometry.dart
Expand Up @@ -57,6 +57,8 @@ class BufferGeometry with EventDispatcher {
int? maxInstanceCount;
int? instanceCount;

Float32Array? array;

BufferGeometry();

BufferGeometry.fromJSON(Map<String, dynamic> json, Map<String, dynamic> rootJSON) {
Expand Down Expand Up @@ -268,8 +270,9 @@ class BufferGeometry with EventDispatcher {
}
}

final array = Float32Array.from(position);
setAttribute('position', Float32BufferAttribute(array, 3, false));
array?.dispose();
array = Float32Array.from(position);
setAttribute('position', Float32BufferAttribute(array!, 3, false));

return this;
}
Expand Down Expand Up @@ -947,6 +950,23 @@ class BufferGeometry with EventDispatcher {
print(" BufferGeometry dispose ........... ");

dispatchEvent(Event({"type": "dispose"}));

if (attributes["color"] is BufferAttribute) {
(attributes["color"] as BufferAttribute).array.dispose();
}
if (attributes["position"] is BufferAttribute) {
(attributes["position"] as BufferAttribute).array.dispose();
}

if (attributes["normal"] is BufferAttribute) {
(attributes["normal"] as BufferAttribute).array.dispose();
}
if (attributes["uv"] is BufferAttribute) {
(attributes["uv"] as BufferAttribute).array.dispose();
}

index?.array.dispose();
array?.dispose();
}
}

Expand Down
19 changes: 16 additions & 3 deletions lib/three3d/geometries/box_geometry.dart
Expand Up @@ -6,6 +6,10 @@ class BoxGeometry extends BufferGeometry {
late int groupStart;
late int numberOfVertices;

NativeArray? positionsArray;
NativeArray? normalsArray;
NativeArray? uvsArray;

BoxGeometry([
width = 1,
height = 1,
Expand Down Expand Up @@ -170,9 +174,9 @@ class BoxGeometry extends BufferGeometry {
// build geometry

setIndex(indices);
setAttribute('position', Float32BufferAttribute(Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(Float32Array.from(normals), 3, false));
setAttribute('uv', Float32BufferAttribute(Float32Array.from(uvs), 2, false));
setAttribute('position', Float32BufferAttribute(positionsArray = Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(normalsArray = Float32Array.from(normals), 3, false));
setAttribute('uv', Float32BufferAttribute(uvsArray = Float32Array.from(uvs), 2, false));
}

static fromJSON(data) {
Expand All @@ -185,4 +189,13 @@ class BoxGeometry extends BufferGeometry {
data["depthSegments"],
);
}

@override
void dispose() {
positionsArray?.dispose();
normalsArray?.dispose();
uvsArray?.dispose();

super.dispose();
}
}
19 changes: 16 additions & 3 deletions lib/three3d/geometries/circle_geometry.dart
Expand Up @@ -3,6 +3,10 @@ import 'package:three_dart/three3d/core/index.dart';
import 'package:three_dart/three3d/math/index.dart';

class CircleGeometry extends BufferGeometry {
NativeArray? positionsArray;
NativeArray? normalsArray;
NativeArray? uvsArray;

CircleGeometry({radius = 1, segments = 8, thetaStart = 0, thetaLength = Math.pi * 2}) : super() {
type = 'CircleGeometry';

Expand Down Expand Up @@ -59,8 +63,17 @@ class CircleGeometry extends BufferGeometry {
// build geometry

setIndex(indices);
setAttribute('position', Float32BufferAttribute(Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(Float32Array.from(normals), 3, false));
setAttribute('uv', Float32BufferAttribute(Float32Array.from(uvs), 2, false));
setAttribute('position', Float32BufferAttribute(positionsArray = Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(normalsArray = Float32Array.from(normals), 3, false));
setAttribute('uv', Float32BufferAttribute(uvsArray = Float32Array.from(uvs), 2, false));
}

@override
void dispose() {
positionsArray?.dispose();
normalsArray?.dispose();
uvsArray?.dispose();

super.dispose();
}
}
14 changes: 12 additions & 2 deletions lib/three3d/geometries/convex_geometry.dart
Expand Up @@ -3,6 +3,9 @@ import 'package:three_dart/three3d/core/index.dart';
import 'package:three_dart/three3d/math/index.dart';

class ConvexGeometry extends BufferGeometry {
NativeArray? verticesArray;
NativeArray? normalsArray;

ConvexGeometry(points) : super() {
List<double> vertices = [];
List<double> normals = [];
Expand Down Expand Up @@ -32,8 +35,15 @@ class ConvexGeometry extends BufferGeometry {
}

// build geometry
setAttribute('position', Float32BufferAttribute(verticesArray = Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(normalsArray = Float32Array.from(normals), 3, false));
}

@override
void dispose() {
verticesArray?.dispose();
normalsArray?.dispose();

setAttribute('position', Float32BufferAttribute(Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(Float32Array.from(normals), 3, false));
super.dispose();
}
}
18 changes: 15 additions & 3 deletions lib/three3d/geometries/cylinder_geometry.dart
Expand Up @@ -3,6 +3,10 @@ import 'package:three_dart/three3d/core/index.dart';
import 'package:three_dart/three3d/math/index.dart';

class CylinderGeometry extends BufferGeometry {
NativeArray? verticesArray;
NativeArray? normalsArray;
NativeArray? uvsArray;

CylinderGeometry([
radiusTop = 1,
radiusBottom = 1,
Expand Down Expand Up @@ -237,9 +241,9 @@ class CylinderGeometry extends BufferGeometry {
// build geometry

setIndex(indices);
setAttribute('position', Float32BufferAttribute(Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(Float32Array.from(normals), 3, false));
setAttribute('uv', Float32BufferAttribute(Float32Array.from(uvs), 2, false));
setAttribute('position', Float32BufferAttribute(verticesArray = Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(normalsArray = Float32Array.from(normals), 3, false));
setAttribute('uv', Float32BufferAttribute(uvsArray = Float32Array.from(uvs), 2, false));
}

static CylinderGeometry fromJSON(data) {
Expand All @@ -254,4 +258,12 @@ class CylinderGeometry extends BufferGeometry {
data["thetaLength"],
);
}

@override
void dispose() {
verticesArray?.dispose();
normalsArray?.dispose();
uvsArray?.dispose();
super.dispose();
}
}
10 changes: 9 additions & 1 deletion lib/three3d/geometries/edges_geometry.dart
Expand Up @@ -8,6 +8,8 @@ class EdgesGeometry extends BufferGeometry {
final _normal = Vector3.init();
final _triangle = Triangle.init();

NativeArray? verticesArray;

EdgesGeometry(BufferGeometry geometry, thresholdAngle) : super() {
type = "EdgesGeometry";
parameters = {"thresholdAngle": thresholdAngle};
Expand Down Expand Up @@ -102,6 +104,12 @@ class EdgesGeometry extends BufferGeometry {
}
}

setAttribute('position', Float32BufferAttribute(Float32Array.from(vertices), 3, false));
setAttribute('position', Float32BufferAttribute(verticesArray = Float32Array.from(vertices), 3, false));
}

@override
void dispose() {
verticesArray?.dispose();
super.dispose();
}
}
17 changes: 15 additions & 2 deletions lib/three3d/geometries/extrude_geometry.dart
Expand Up @@ -529,9 +529,12 @@ class ExtrudeGeometry extends BufferGeometry {
}

// build geometry
final nativeVertices = Float32Array.from(verticesArray);
final nativeUv = Float32Array.from(uvArray);
arrays.addAll([nativeVertices, nativeUv]);

setAttribute('position', Float32BufferAttribute(Float32Array.from(verticesArray), 3, false));
setAttribute('uv', Float32BufferAttribute(Float32Array.from(uvArray), 2, false));
setAttribute('position', Float32BufferAttribute(nativeVertices, 3, false));
setAttribute('uv', Float32BufferAttribute(nativeUv, 2, false));

computeVertexNormals();

Expand All @@ -549,6 +552,16 @@ class ExtrudeGeometry extends BufferGeometry {

return toJSON2(shapes, options, data);
}

final arrays = <NativeArray>[];

@override
void dispose() {
for (var element in arrays) {
element.dispose();
}
super.dispose();
}
}

class WorldUVGenerator {
Expand Down
19 changes: 16 additions & 3 deletions lib/three3d/geometries/lathe_geometry.dart
Expand Up @@ -3,6 +3,10 @@ import 'package:three_dart/three3d/core/index.dart';
import 'package:three_dart/three3d/math/index.dart';

class LatheGeometry extends BufferGeometry {
NativeArray? verticesArray;
NativeArray? uvsArray;
NativeArray? normalsArray;

LatheGeometry(
points, {
segments = 12,
Expand Down Expand Up @@ -142,8 +146,17 @@ class LatheGeometry extends BufferGeometry {
// build geometry

setIndex(indices);
setAttribute('position', Float32BufferAttribute(Float32Array.from(vertices), 3, false));
setAttribute('uv', Float32BufferAttribute(Float32Array.from(uvs), 2, false));
setAttribute('normal', Float32BufferAttribute(Float32Array.from(normals), 3, false));
setAttribute('position', Float32BufferAttribute(verticesArray = Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(normalsArray = Float32Array.from(normals), 3, false));
setAttribute('uv', Float32BufferAttribute(uvsArray = Float32Array.from(uvs), 2, false));
}

@override
void dispose() {
verticesArray?.dispose();
uvsArray?.dispose();
normalsArray?.dispose();

super.dispose();
}
}
19 changes: 16 additions & 3 deletions lib/three3d/geometries/parametric_geometry.dart
Expand Up @@ -3,6 +3,10 @@ import 'package:three_dart/three3d/core/index.dart';
import 'package:three_dart/three3d/math/index.dart';

class ParametricGeometry extends BufferGeometry {
NativeArray? verticesArray;
NativeArray? uvsArray;
NativeArray? normalsArray;

ParametricGeometry(func, slices, stacks) : super() {
type = "ParametricGeometry";
parameters = {"func": func, "slices": slices, "stacks": stacks};
Expand Down Expand Up @@ -91,8 +95,17 @@ class ParametricGeometry extends BufferGeometry {
// build geometry

setIndex(indices);
setAttribute('position', Float32BufferAttribute(Float32Array.from(vertices), 3));
setAttribute('normal', Float32BufferAttribute(Float32Array.from(normals), 3));
setAttribute('uv', Float32BufferAttribute(Float32Array.from(uvs), 2));
setAttribute('position', Float32BufferAttribute(verticesArray = Float32Array.from(vertices), 3));
setAttribute('normal', Float32BufferAttribute(normalsArray = Float32Array.from(normals), 3));
setAttribute('uv', Float32BufferAttribute(uvsArray = Float32Array.from(uvs), 2));
}

@override
void dispose() {
verticesArray?.dispose();
uvsArray?.dispose();
normalsArray?.dispose();

super.dispose();
}
}
18 changes: 15 additions & 3 deletions lib/three3d/geometries/plane_geometry.dart
Expand Up @@ -3,6 +3,10 @@ import 'package:three_dart/three3d/core/index.dart';
import 'package:three_dart/three3d/math/index.dart';

class PlaneGeometry extends BufferGeometry {
NativeArray? verticesArray;
NativeArray? normalsArray;
NativeArray? uvsArray;

PlaneGeometry([num width = 1, num height = 1, num widthSegments = 1, num heightSegments = 1]) : super() {
type = 'PlaneGeometry';

Expand Down Expand Up @@ -55,12 +59,20 @@ class PlaneGeometry extends BufferGeometry {
}

setIndex(indices);
setAttribute('position', Float32BufferAttribute(Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(Float32Array.from(normals), 3, false));
setAttribute('uv', Float32BufferAttribute(Float32Array.from(uvs), 2, false));
setAttribute('position', Float32BufferAttribute(verticesArray = Float32Array.from(vertices), 3, false));
setAttribute('normal', Float32BufferAttribute(normalsArray = Float32Array.from(normals), 3, false));
setAttribute('uv', Float32BufferAttribute(uvsArray = Float32Array.from(uvs), 2, false));
}

static fromJSON(data) {
return PlaneGeometry(data["width"], data["height"], data["widthSegments"], data["heightSegments"]);
}

@override
void dispose() {
verticesArray?.dispose();
normalsArray?.dispose();
uvsArray?.dispose();
super.dispose();
}
}
22 changes: 17 additions & 5 deletions lib/three3d/geometries/polyhedron_geometry.dart
Expand Up @@ -4,6 +4,10 @@ import 'package:three_dart/three3d/dart_helpers.dart';
import 'package:three_dart/three3d/math/index.dart';

class PolyhedronGeometry extends BufferGeometry {
NativeArray? verticesArray;
NativeArray? normalsArray;
NativeArray? uvsArray;

PolyhedronGeometry(vertices, indices, [radius = 1, detail = 0]) : super() {
type = "PolyhedronGeometry";
// default buffer data
Expand Down Expand Up @@ -217,16 +221,24 @@ class PolyhedronGeometry extends BufferGeometry {

// build non-indexed geometry

setAttribute('position', Float32BufferAttribute(Float32Array.from(vertexBuffer), 3, false));
setAttribute('normal', Float32BufferAttribute(Float32Array.from(slice<double>(vertexBuffer, 0)), 3, false));
setAttribute('uv', Float32BufferAttribute(Float32Array.from(uvBuffer), 2, false));
setAttribute('position', Float32BufferAttribute(verticesArray = Float32Array.from(vertexBuffer), 3, false));
setAttribute(
'normal', Float32BufferAttribute(normalsArray = Float32Array.from(slice<double>(vertexBuffer, 0)), 3, false));
setAttribute('uv', Float32BufferAttribute(uvsArray = Float32Array.from(uvBuffer), 2, false));

if (detail == 0) {
computeVertexNormals(); // flat normals

} else {
normalizeNormals(); // smooth normals

}
}

@override
void dispose() {
verticesArray?.dispose();
normalsArray?.dispose();
uvsArray?.dispose();

super.dispose();
}
}

0 comments on commit 2c9c21f

Please sign in to comment.