Dad72 Posted March 15, 2014 Share Posted March 15, 2014 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/ Quote Link to comment Share on other sites More sharing options...
gwenael Posted March 16, 2014 Share Posted March 16, 2014 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 memberBABYLON.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 }}; Quote Link to comment Share on other sites More sharing options...
Dad72 Posted March 16, 2014 Author Share Posted March 16, 2014 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. Quote Link to comment Share on other sites More sharing options...
Dad72 Posted November 4, 2014 Author Share Posted November 4, 2014 Update LOD script that works now. I tested. I set the aperture first post with a complete code (html + js) to test Quote Link to comment Share on other sites More sharing options...
Dad72 Posted November 4, 2014 Author Share Posted November 4, 2014 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/ gryff 1 Quote Link to comment Share on other sites More sharing options...
gryff Posted November 4, 2014 Share Posted November 4, 2014 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 Quote Link to comment Share on other sites More sharing options...
Temechon Posted November 4, 2014 Share Posted November 4, 2014 I think they are working on it... But great job Dad ! Works great ! Quote Link to comment Share on other sites More sharing options...
Dad72 Posted November 4, 2014 Author Share Posted November 4, 2014 Thanks guys ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.