Jump to content

getHeightAtCoordinates return wrong value


_ElementoZerO_
 Share

Recommended Posts

Hi! i'm trying to add some grass mesh on a height map, but the getHeightAtCoordinates return sometimes a wrong value ( and the grass is up in the sky..) i using the same code of the instances example:

 BABYLON.SceneLoader.ImportMesh("", "./assets/mesh/world/", "grass.babylon", this.GameKernel.scene, function (grass) {
            var x1 = 0;
            var z1 = 0;
            grass[0].material.opacityTexture = null;
            grass[0].material.backFaceCulling = false;
            grass[0].isVisible = true;
            grass[0].position.y = ground.getHeightAtCoordinates(x1, z1); // Getting height from ground object
            grass[0].position.x = x1;
            grass[0].position.z = z1;
            grass[0].material.specularColor = new BABYLON.Color3(0, 0, 0);
            grass[0].material.ambientColor = new BABYLON.Color3(1, 1, 1);
            //newMeshes[0].scale = 0.5
            //shadowGenerator.getShadowMap().renderList.push(newMeshes[0]);

            var range = 200;
            var count = 100;
            var x,y,z;
            var newGrassInstance;
            for(var j = 0; j < count; j++)
            {
                x =(grass[0].position.x)-((range / 2) - (Math.random() * range));    

                for (var i = 0; i < count; i++) 
                {
                    newGrassInstance = grass[0].createInstance("x" + x +"y" + y+ "z" + z );
                    z = (grass[0].position.z)-((range / 2) - (Math.random() * range));
                    y = ground.getHeightAtCoordinates(x, z); // Getting height from ground object
                    //console.log("y: " + y );
                    newGrassInstance.position = new BABYLON.Vector3(x, y, z);
                    /*
                    if(ground.intersectsMesh(newGrassInstance, true))
                        console.log("x" + x +"y" + y+ "z" + z +"is on ground");
                    else
                        console.log("x" + x +"y" + y+ "z" + z + " not on ground");*/
                    newGrassInstance.rotate(BABYLON.Axis.Y, Math.random() * Math.PI * 2, BABYLON.Space.WORLD);
                    var scale = 0.5 + Math.random() * 2;
                    newGrassInstance.scaling.addInPlace(new BABYLON.Vector3(scale, scale, scale));
                } 
            }
        });

the attach image show the result..

where i'm wrong?

ps the ground is not rotate i'm read in the source code that the function return non consistent value in this case

 

grass issue.png

Link to comment
Share on other sites

28 minutes ago, adam said:

Is your ground using the same coordinate system as your grass instances?

I'm using the tree mesh take from instance demo and i have the same problem...i take this grass mesh from a free site...how i can check?

28 minutes ago, jerome said:

quite difficult to answer without a repro case showing the same issue

could you please provide a simple PG with the same problem ?

The project is very big..if you want i can send you a link in private.

Link to comment
Share on other sites

So the error is probably somewhere else ...

in the values x, z that you pass to getHeightAtCoordinates() maybe in your code or the way your ground is built

That's why I said that making a PG often helps to find where the error is (or where it's not) ;)

Link to comment
Share on other sites

Do you really need the physical geometry for the grass? That's allot of computation as I don't see the Geometry built for simulating wind dynamics or other dynamics - so might you simply use sprites? Otherwise, your framerate will crawl on mobile - as it's quite slow now. Just a thought...

DB

Link to comment
Share on other sites

9 hours ago, dbawel said:

Do you really need the physical geometry for the grass? That's allot of computation as I don't see the Geometry built for simulating wind dynamics or other dynamics - so might you simply use sprites? Otherwise, your framerate will crawl on mobile - as it's quite slow now. Just a thought...

DB

You're right but this little game is for an exams :) and i promised to my prof grass mesh...but i still not resolve the issue..

The framerate that you see in the image is from my old notebook....with desktop computer with gtx 260 (old too..) i have about 30-40 fps

Link to comment
Share on other sites

7 hours ago, jerome said:

imho, if I were you, I would restart to reimplement the code from the working PG step by step and I would test at each step if something is then getting wrong in order to localize the error

maybe is the only way.. i tried with sprite and same issue...

Link to comment
Share on other sites

Hey ! I don't know why but I got a problem with this same function.. Maybe a problem with v2.4 ?
I go inside the babylonjs code but it should work.

So I created my own function, which works well :

var groundHeightAt = function(x, z)
{
	// Create ray from position x,z at the maximum altitude and launch it vertically to hit the ground
	var maxHeight = // define your max height
                        // mine is skybox.getBoundingInfo().boundingBox.maximum.y
	var ray = new BABYLON.Ray(new BABYLON.Vector3(x, maxHeight, z),	// Origin
				  new BABYLON.Vector3(0, -1, 0),	// Direction
				  2*maxHeight);				// Length

	var res = ground.intersects(ray, true);
	return res.pickedPoint.y;
}

Where is the difference ?

Link to comment
Share on other sites

I'm finding broken features in 2.4 - and will report right now outlining one of the bugs. I switched our server back to Version 2.2 - although, this probably isn't part of your issue. I am however finding bugs with videoTexture playback in 2.4 though. As I mentioned, I'll post a bug report now.

DB

Link to comment
Share on other sites

@_ElementoZero_

Another approach could be to reduce your ground size and number sprite number and to try to isolate some cases where the grass is not at the right altitude in your code,

then to log the x z values that you are apssing to the function and what y is returned

Without any log/data or any issue repro PG we can hardly imagine what/how you're doing and what values are handled in the code. You don't tell us either how is your ground designed in your code : size, subdivisions, max/min height, position, etc

Link to comment
Share on other sites

8 hours ago, Pouet said:

Hey ! I don't know why but I got a problem with this same function.. Maybe a problem with v2.4 ?
I go inside the babylonjs code but it should work.

So I created my own function, which works well :


var groundHeightAt = function(x, z)
{
	// Create ray from position x,z at the maximum altitude and launch it vertically to hit the ground
	var maxHeight = // define your max height
                        // mine is skybox.getBoundingInfo().boundingBox.maximum.y
	var ray = new BABYLON.Ray(new BABYLON.Vector3(x, maxHeight, z),	// Origin
				  new BABYLON.Vector3(0, -1, 0),	// Direction
				  2*maxHeight);				// Length

	var res = ground.intersects(ray, true);
	return res.pickedPoint.y;
}

Where is the difference ?

 

33 minutes ago, jerome said:

@_ElementoZero_

Another approach could be to reduce your ground size and number sprite number and to try to isolate some cases where the grass is not at the right altitude in your code,

then to log the x z values that you are apssing to the function and what y is returned

Without any log/data or any issue repro PG we can hardly imagine what/how you're doing and what values are handled in the code. You don't tell us either how is your ground designed in your code : size, subdivisions, max/min height, position, etc

I'll try ! thank you guys !

this.sizeX = 2600;//2600;//2608;
this.sizeY = 2032;//2000;//2032;

var ground = BABYLON.Mesh.CreateGroundFromHeightMap("ground", "./textures/heightMap.png", this.sizeX, this.sizeY, 300, 0, 255, this.GameKernel.scene, false);
    ground.position.y = 0;
    var groundMaterial = new BABYLON.StandardMaterial("groundMaterial", this.GameKernel.scene);
	
	groundMaterial.diffuseTexture = new BABYLON.Texture("./textures/sand.jpg", this.GameKernel.scene);
	groundMaterial.diffuseTexture.uScale = 60;
	groundMaterial.diffuseTexture.vScale = 60;
	groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
	ground.material = groundMaterial;
    
    ground.receiveShadows = true;
    //this._deactivateSpecular(ground);    
	ground.checkCollisions  = true;
    ground.applyGravity = true;
	this.ground = ground;

Another think my code is based on this tutorial:

http://pixelcodr.com/tutos/shooter/shooter.html

Link to comment
Share on other sites

yep, the PG uses the latest built

and you are right, a bug (fixed in 2.5) was introduced just before the 2.4 relase in the cross product use in computeHeightQuads() , that affected getHeightAtEtc() : a simple conflict in the use of temporary pre-allocated vector3s, somethng we use under the hood to not reuse the memory and to not trigger the GC.

Have a look a the bug fixes here please : https://github.com/BabylonJS/Babylon.js/blob/master/dist/preview release/what's new.md

I didn't know you were using the 2.4 version, sorry.

Link to comment
Share on other sites

@_ElementoZerO_ - I don't know if this is affecting your scene - however when we removed incremental loading from our latest scene just a few days ago, our performance increased considerably. So it is certainly worth a test, and to share your results with the community. As previously mentioned, we're still using Version 2.2 as for us it has proven to be stable for all elements we are using for our current app (and for @MasterSplinter, this means applications loaded through a link in WebGL). However, I will be working with v2.4 again later this week as soon as I receive confirmation that the bug with videoTextures as well as others reported on the forum are fixed. And as I always mention, babylon.js is a very new framework, and I am always impressed by how stable each release is - even though I'm currently having issues with v2.4. Compared to past experience with new release software, babylon.js is by far the most stable and thoroughly tested for each release due to the unique group of developers building this framework - as well as the very enthusiastic group of developers and users on this forum testing and posting daily, not to mention that the builders are enguaged daily with this community. I'm quite proud to be considered a part of the history we are all building here. And I know that soon babylon.js will be in the media spotlight, as I'm aware of what others are developing, as well as a few major players in the film industry which are already in process of providing high praise for what this framework is able to accomplish and how they are making amazing use of this - across all platforms, which for the film and other industries is beyond belief.

So thank you for posting and keeping us all informed on your progress. We're all in this together, and are all benefiting greatly from it. By the way, if DK or Davrous get this far in the post, is it possible to add a spell-check to the text editor on this forum? This would help allot of users, especially those whose first language is not English. Just thought I'd ask, as I thought I remember having this feature in early 2014. But I'm probably mistaken.

Cheers,

DB

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...