Skip to content

Commit

Permalink
Merge pull request #123 from mark-nicepants/main
Browse files Browse the repository at this point in the history
Fix lints and properly dispose NativeArrays when Object3d's and Buffergeometry gets dispose
  • Loading branch information
wasabia committed Mar 1, 2023
2 parents 2ce6bb1 + 8ed2739 commit ebe842a
Show file tree
Hide file tree
Showing 33 changed files with 390 additions and 150 deletions.
21 changes: 6 additions & 15 deletions example/lib/webgl_debug_for_macos.dart
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand All @@ -11,8 +10,7 @@ import 'package:three_dart/three_dart.dart' as three;
class WebGlDebugForMacos extends StatefulWidget {
final String fileName;

const WebGlDebugForMacos({Key? key, required this.fileName})
: super(key: key);
const WebGlDebugForMacos({Key? key, required this.fileName}) : super(key: key);

@override
State<WebGlDebugForMacos> createState() => _MyAppState();
Expand Down Expand Up @@ -138,13 +136,10 @@ class _MyAppState extends State<WebGlDebugForMacos> {
child: Builder(builder: (BuildContext context) {
if (kIsWeb) {
return three3dRender.isInitialized
? HtmlElementView(
viewType: three3dRender.textureId!.toString())
? HtmlElementView(viewType: three3dRender.textureId!.toString())
: Container();
} else {
return three3dRender.isInitialized
? Texture(textureId: three3dRender.textureId!)
: Container();
return three3dRender.isInitialized ? Texture(textureId: three3dRender.textureId!) : Container();
}
})),
],
Expand Down Expand Up @@ -221,13 +216,9 @@ class _MyAppState extends State<WebGlDebugForMacos> {
renderer!.shadowMap.type = three.BasicShadowMap;

if (!kIsWeb) {
var pars = three.WebGLRenderTargetOptions({
"minFilter": three.LinearFilter,
"magFilter": three.LinearFilter,
"format": three.RGBAFormat
});
renderTarget = three.WebGLMultisampleRenderTarget(
(width * dpr).toInt(), (height * dpr).toInt(), pars);
var pars = three.WebGLRenderTargetOptions(
{"minFilter": three.LinearFilter, "magFilter": three.LinearFilter, "format": three.RGBAFormat});
renderTarget = three.WebGLMultisampleRenderTarget((width * dpr).toInt(), (height * dpr).toInt(), pars);
renderer!.setRenderTarget(renderTarget);
sourceTexture = renderer!.getRenderTargetGLTexture(renderTarget);
}
Expand Down
22 changes: 11 additions & 11 deletions example/pubspec.lock
Expand Up @@ -53,10 +53,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0
url: "https://pub.dev"
source: hosted
version: "1.17.1"
version: "1.17.0"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -204,10 +204,10 @@ packages:
dependency: transitive
description:
name: js
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7"
url: "https://pub.dev"
source: hosted
version: "0.6.7"
version: "0.6.5"
lints:
dependency: transitive
description:
Expand All @@ -220,10 +220,10 @@ packages:
dependency: transitive
description:
name: matcher
sha256: c94db23593b89766cda57aab9ac311e3616cf87c6fa4e9749df032f66f30dcb8
sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72"
url: "https://pub.dev"
source: hosted
version: "0.12.14"
version: "0.12.13"
material_color_utilities:
dependency: transitive
description:
Expand Down Expand Up @@ -252,10 +252,10 @@ packages:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: db9d4f58c908a4ba5953fcee2ae317c94889433e5024c27ce74a37f94267945b
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.8.2"
petitparser:
dependency: transitive
description:
Expand Down Expand Up @@ -329,10 +329,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "6182294da5abf431177fccc1ee02401f6df30f766bc6130a0852c6b6d7ee6b2d"
sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206
url: "https://pub.dev"
source: hosted
version: "0.4.18"
version: "0.4.16"
three_dart:
dependency: "direct main"
description:
Expand Down Expand Up @@ -397,5 +397,5 @@ packages:
source: hosted
version: "6.2.2"
sdks:
dart: ">=2.19.0 <4.0.0"
dart: ">=2.18.0 <3.0.0"
flutter: ">=3.0.1"
7 changes: 3 additions & 4 deletions lib/three3d/animation/keyframe_track.dart
Expand Up @@ -105,7 +105,6 @@ class KeyframeTrack {
setInterpolation(defaultInterpolation);
} else {
throw (message); // fatal, in this case

}
}

Expand Down Expand Up @@ -208,14 +207,14 @@ class KeyframeTrack {

var valueSize = getValueSize();
if (valueSize - Math.floor(valueSize) != 0) {
print('three.KeyframeTrack: Invalid value size in track. ${this}');
print('three.KeyframeTrack: Invalid value size in track. $this');
valid = false;
}

var times = this.times, nKeys = times.length;

if (nKeys == 0) {
print('three.KeyframeTrack: Track is empty. ${this}');
print('three.KeyframeTrack: Track is empty. $this');
valid = false;
}

Expand All @@ -225,7 +224,7 @@ class KeyframeTrack {
var currTime = times[i];

if (prevTime != null && prevTime > currTime) {
print('three.KeyframeTrack: Out of order keys.${this} i: $i currTime: $currTime prevTime: $prevTime');
print('three.KeyframeTrack: Out of order keys. $this i: $i currTime: $currTime prevTime: $prevTime');
valid = false;
break;
}
Expand Down
17 changes: 8 additions & 9 deletions lib/three3d/animation/property_binding.dart
Expand Up @@ -442,13 +442,13 @@ class PropertyBinding {
switch (objectName) {
case 'materials':
if (!targetObject.material) {
print('three.PropertyBinding: Can not bind to material as node does not have a material. ${this}');
print('three.PropertyBinding: Can not bind to material as node does not have a material. $this');
return;
}

if (!targetObject.material.materials) {
print(
'three.PropertyBinding: Can not bind to material.materials as node.material does not have a materials array. ${this}');
'three.PropertyBinding: Can not bind to material.materials as node.material does not have a materials array. $this');
return;
}

Expand All @@ -458,7 +458,7 @@ class PropertyBinding {

case 'bones':
if (!targetObject.skeleton) {
print('three.PropertyBinding: Can not bind to bones as node does not have a skeleton. ${this}');
print('three.PropertyBinding: Can not bind to bones as node does not have a skeleton. $this');
return;
}

Expand All @@ -479,7 +479,7 @@ class PropertyBinding {

default:
if (targetObject.getProperty(objectName) == null) {
print('three.PropertyBinding: Can not bind to objectName of node null. ${this}');
print('three.PropertyBinding: Can not bind to objectName of node null. $this');
return;
}

Expand All @@ -489,8 +489,7 @@ class PropertyBinding {

if (objectIndex != null) {
if (targetObject[objectIndex] == null) {
print(
'three.PropertyBinding: Trying to bind to objectIndex of objectName, but is null.${this} $targetObject');
print('three.PropertyBinding: Trying to bind to objectIndex of objectName, but is null.$this $targetObject');
return;
}

Expand Down Expand Up @@ -535,14 +534,14 @@ class PropertyBinding {
// support resolving morphTarget names into indices.
if (!targetObject.geometry) {
print(
'three.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry. ${this}');
'three.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry. $this');
return;
}

if (targetObject.geometry is BufferGeometry) {
if (!targetObject.geometry.morphAttributes) {
print(
'three.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphAttributes. ${this}');
'three.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphAttributes. $this');
return;
}

Expand All @@ -551,7 +550,7 @@ class PropertyBinding {
}
} else {
print(
'three.PropertyBinding: Can not bind to morphTargetInfluences on three.Geometry. Use three.BufferGeometry instead. ${this}');
'three.PropertyBinding: Can not bind to morphTargetInfluences on three.Geometry. Use three.BufferGeometry instead. $this');
return;
}
}
Expand Down
28 changes: 24 additions & 4 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 All @@ -282,7 +285,7 @@ class BufferGeometry with EventDispatcher {

if (position != null && position is GLBufferAttribute) {
print(
'three.BufferGeometry.computeBoundingBox(): GLBufferAttribute requires a manual bounding box. Alternatively set "mesh.frustumCulled" to "false". ${this}');
'three.BufferGeometry.computeBoundingBox(): GLBufferAttribute requires a manual bounding box. Alternatively set "mesh.frustumCulled" to "false". $this');

double inf = 9999999999.0;

Expand Down Expand Up @@ -404,7 +407,7 @@ class BufferGeometry with EventDispatcher {

if (boundingSphere?.radius == null) {
print(
'three.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values. ${this}');
'three.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values. $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
8 changes: 7 additions & 1 deletion lib/three3d/core/object_3d.dart
Expand Up @@ -877,7 +877,13 @@ class Object3D with EventDispatcher {
}
}

void dispose() {}
void dispose() {
matrix?.dispose();
matrixWorld?.dispose();
modelViewMatrix?.dispose();
normalMatrix?.dispose();
bindMatrix?.dispose();
}
}

class Object3dMeta {
Expand Down
3 changes: 2 additions & 1 deletion lib/three3d/extras/core/curve.dart
Expand Up @@ -199,7 +199,6 @@ class Curve {
break;

// DONE

}
}

Expand Down Expand Up @@ -344,6 +343,8 @@ class Curve {
}
}

mat.dispose();

return {"tangents": tangents, "normals": normals, "binormals": binormals};
}

Expand Down

0 comments on commit ebe842a

Please sign in to comment.