Jump to content

Script LOD Systeme


Dad72
 Share

Recommended Posts

Hi,
 
I create a small script of LOD and I want to share it. I don't know if it's the best ways to make, so if you want to improve it please do not hesitate.
 

			var BABYLON = BABYLON || {};			(function () {					// DistanceLOD1 = medium				// DistanceLOD2 = low				// DistanceMax = hide				BABYLON.DistanceLOD1 = 10;				BABYLON.DistanceLOD2 = 30; 				BABYLON.DistanceMax = 50;											// LOD_0 = hight				// LOD_1 = medium				// LOD_2 = low					BABYLON.LOD = function(Camera, LOD_0, LOD_1, LOD_2)				{					this.camera = Camera;					this._verifInProgress = false;									this._distance = 0;					this.LOD0 = LOD_0;					this.LOD1 = LOD_1;					this.LOD2 = LOD_2; 					this.positionLOD0 = this.LOD0.position;					if (this.LOD1 != undefined) this.LOD1.setEnabled(false);      					if (this.LOD2 != undefined) this.LOD2.setEnabled(false); 				};   								BABYLON.LOD.prototype._distanceTransition = function() 				{					if (this.LOD1 != undefined || this.LOD2 != undefined)					{						this._distance = this.camera.position.subtract(this.positionLOD0).length();											var enableLOD0 = this._distance < BABYLON.DistanceLOD1;						this.LOD0.setEnabled(enableLOD0);						var enableLOD1 = !enableLOD0 && this._distance < BABYLON.DistanceLOD2;						if (this.LOD1 != undefined)						{							this.LOD1.setEnabled(enableLOD1);						}						if (this.LOD2 != undefined)						{							this.LOD2.setEnabled(!enableLOD0 && !enableLOD1 && this._distance <= BABYLON.DistanceMax);						} 					}						};					BABYLON.LOD.prototype.Update = function()				{					if (this._verifInProgress) return;							this._verifInProgress = true;					this._distanceTransition();					this._verifInProgress = false;				};							})();						

Use:

							//Sphere Hight				var sphere0 = BABYLON.Mesh.CreateSphere("Sphere_Higth", 50.0, 1.0, scene);				var materialSphere0 = new BABYLON.StandardMaterial("color1", scene);				materialSphere0.diffuseColor = new BABYLON.Color3(1.0, 0.2, 0.7);				sphere0.material = materialSphere0;				//Sphere Medium				var sphere1 = BABYLON.Mesh.CreateSphere("Sphere_Medium", 7.0, 1.0, scene);				var materialSphere1 = new BABYLON.StandardMaterial("color2", scene);				materialSphere1.diffuseColor = new BABYLON.Color3(1.0, 0, 0);				sphere1.material = materialSphere1;				//Sphere Low				var sphere2 = BABYLON.Mesh.CreateSphere("Sphere_Low", 2.0, 1.0, scene);				var materialSphere2 = new BABYLON.StandardMaterial("color3", scene);				materialSphere2.diffuseColor = new BABYLON.Color3(0, 0, 0);				sphere2.material = materialSphere2;				// Initialise LOD 				var lodObjet = new BABYLON.LOD(camera, sphere0, sphere1, sphere2);												engine.runRenderLoop(function () {					scene.render();					lodObjet.Update(); // Update Transition LOD				});

I have yet to test the script, but this should work.

Demo:
http://www.castorengine.com/babylon/LOD/

Link to comment
Share on other sites

Thanks for sharing.

 

Here are my feedbacks:

 

* that would be better to set verifInProgress only in one function and to indicate that's a private member

BABYLON.LOD.prototype.Update = function() {    if (this._verifInProgress) {        return;    }    this._verifInProgress = true;    this._distanceTransition();    this._verifInProgress = false;};

* DistanceTransition should also be marked as private _distanceTransition instead of DistanceTransition

* this.distance is correct if LOD0 is in the same space as camera (they share the same parent or they both don't have parents); it should also be private since it should only be used internally

* "if, else" can be simplified

* Why do you always call BABYLON.LOD.sleep even if this.LOD1 == undefined && this.LOD2 == undefined? By the way, why do you wait an amount of seconds at the end of _distanceTransition?

BABYLON.LOD.prototype._distanceTransition = function() {   if (this.LOD1 != undefined || this.LOD2 != undefined) {         this._distance = this.camera.position.subtract(this.positionLOD0).length();					         var enableLOD0 = this._distance < BABYLON.LOD.DistanceLOD1;         this.LOD0.setEnabled(enableLOD0);         var enableLOD1 = !enableLOD0 && this._distance < BABYLON.LOD.DistanceLOD2;         if (this.LOD1 != undefined) {            this.LOD1.setEnabled(enableLOD1);         }         if (this.LOD2 != undefined) {            this.LOD2.setEnabled(!enableLOD0 && !enableLOD1 && this._distance <= BABYLON.LOD.DistanceMax);         }       }    BABYLON.LOD.sleep(BABYLON.LOD.delaiSeconds);};

* You can modify BABYLON.LOD.sleep as is:

BABYLON.LOD.sleep = function(seconds) {    var start = new Date().getTime();    while((new Date().getTime() - start) <= (seconds * 1000)) {     // wait    }};
Link to comment
Share on other sites

Thanks for the changes. I've edit the first post to reflect the changes. I think that DeltaKosh will do a system more adequate in the futures.

To BABYLON.LOD.sleep, it is to give a pause of  +- 5 seconds between each loop of runRenderLoop towers. but I confess to not being sure about this approach and the result.

 

Like I said ,I do not have tested my script, I made this script to pass the time and I said to myself that this could be useful for testing the LOD while waiting for Deltakosh creates the official LOD system.

Link to comment
Share on other sites

  • 7 months later...

I add a direct link test system LOD:

 

Use the keyboard arrows to move back and forth, you will see the transitions

 

http://www.castorengine.com/babylon/LOD/

 

Nice dad72 - seems to work well :)

 

while waiting for Deltakosh creates the official LOD system

 

I remember seeing LOD on the to-do-path when I first joined this forum - but then DK and crew have a lot of stuff to do, so I guess it is a matter of priorities :)

 

cheers, gryff :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...