Browse Source

Success callback after height map created

In a height map vertices data is correct only after the image was loaded
and processed. This happens async (image loading). Functions like
simplification, which relays on the vertices data, will not run
correctly if executed right after the mesh creation.
A different approach will be promises, but that would require changing
other parts of the framework as well...
Raanan Weber 10 năm trước cách đây
mục cha
commit
ba31904cda
2 tập tin đã thay đổi với 23 bổ sung4 xóa
  1. 12 2
      Babylon/Mesh/babylon.mesh.js
  2. 11 2
      Babylon/Mesh/babylon.mesh.ts

+ 12 - 2
Babylon/Mesh/babylon.mesh.js

@@ -841,7 +841,7 @@ var BABYLON;
         };
 
         // Geometric tools
-        Mesh.prototype.applyDisplacementMap = function (url, minHeight, maxHeight) {
+        Mesh.prototype.applyDisplacementMap = function (url, minHeight, maxHeight, onSuccess) {
             var _this = this;
             var scene = this.getScene();
 
@@ -860,6 +860,11 @@ var BABYLON;
                 var buffer = context.getImageData(0, 0, heightMapWidth, heightMapHeight).data;
 
                 _this.applyDisplacementMapFromBuffer(buffer, heightMapWidth, heightMapHeight, minHeight, maxHeight);
+
+                //execute success callback, if set
+                if (onSuccess) {
+                    onSuccess(_this);
+                }
             };
 
             BABYLON.Tools.LoadImage(url, onload, function () {
@@ -1167,7 +1172,7 @@ var BABYLON;
             return tiledGround;
         };
 
-        Mesh.CreateGroundFromHeightMap = function (name, url, width, height, subdivisions, minHeight, maxHeight, scene, updatable) {
+        Mesh.CreateGroundFromHeightMap = function (name, url, width, height, subdivisions, minHeight, maxHeight, scene, updatable, onReady) {
             var ground = new BABYLON.GroundMesh(name, scene);
             ground._subdivisions = subdivisions;
 
@@ -1191,6 +1196,11 @@ var BABYLON;
                 vertexData.applyToMesh(ground, updatable);
 
                 ground._setReady(true);
+
+                //execute ready callback, if set
+                if (onReady) {
+                    onReady(ground);
+                }
             };
 
             BABYLON.Tools.LoadImage(url, onload, function () {

+ 11 - 2
Babylon/Mesh/babylon.mesh.ts

@@ -844,7 +844,7 @@
         }
 
         // Geometric tools
-        public applyDisplacementMap(url: string, minHeight: number, maxHeight: number): void {
+        public applyDisplacementMap(url: string, minHeight: number, maxHeight: number, onSuccess?: (mesh: Mesh) => void): void {
             var scene = this.getScene();
 
             var onload = img => {
@@ -862,6 +862,10 @@
                 var buffer = context.getImageData(0, 0, heightMapWidth, heightMapHeight).data;
 
                 this.applyDisplacementMapFromBuffer(buffer, heightMapWidth, heightMapHeight, minHeight, maxHeight);
+                //execute success callback, if set
+                if (onSuccess) {
+                    onSuccess(this);
+                }
             };
 
             Tools.LoadImage(url, onload,() => { }, scene.database);
@@ -1171,7 +1175,7 @@
             return tiledGround;
         }
 
-        public static CreateGroundFromHeightMap(name: string, url: string, width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, scene: Scene, updatable?: boolean): GroundMesh {
+        public static CreateGroundFromHeightMap(name: string, url: string, width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, scene: Scene, updatable?: boolean, onReady?:(mesh: GroundMesh) => void): GroundMesh {
             var ground = new GroundMesh(name, scene);
             ground._subdivisions = subdivisions;
 
@@ -1195,6 +1199,11 @@
                 vertexData.applyToMesh(ground, updatable);
 
                 ground._setReady(true);
+
+                //execute ready callback, if set
+                if (onReady) {
+                    onReady(ground);
+                }
             };
 
             Tools.LoadImage(url, onload,() => { }, scene.database);