Jump to content

Terrain Materials from Unity


JBlack_UTD
 Share

Recommended Posts

Hi @JBlack_UTD 

please see: 

Docs: http://doc.babylonjs.com/extensions/Terrain

PG: http://www.babylonjs-playground.com/#E6OZX#7 (same link can be found in the docs)

 

for terrain material,

please note that it's an extension and therefore must be included seperately from the main babylon.js in your project :)

 

You can make a simple 2d image in any image editor, aproximately same size as your plane/ground and then apply different colors where you want them, which then turns into different textures on your scene :)

 

Edit: with same "size" i ofc mean dimensions/shape.

 

Tips:

When working with the terrain material and pre-made 3d scene files, It's always a good idea to design your level first in 2d, e.g. choose where you want your roads, grass, etc to be.

You can then import that image as a texture for your plane/ground in your 3d editor to use as a template to follow and apply any height changes you wish to have, place 3d objects, etc, 

before you then export to .babylon and then finally apply the texture('s) via the terrain material extension :) 

 

Link to comment
Share on other sites

RIght, sorry - I wasn't specific enough.

I've emulated the terrain material shader in Unity using shaderforge, and would ideally like to export that straight into BabylonJS.

I've also manually created a terrain material in BabylonJS to understand how it works (via the extension).

The disconnect is that the Unity exporter (via SceneBuilder.Material.cs) has  DumpStandardMaterial and even  DumpPBRMaterial , but not a DumpTerrainMaterial function. I'd be happy to make one, but the problem then becomes that the BabylonExport.Entities.dll doesn't seem to contain a definition for the terrain material (which is reasonable because it's an extension!).

Should I be trying to add a custom definition akin to the entities, or should I be exporting data (such as paths) to BabylonJS so it can use the extension locally? And this goes for any custom shader/material that I might want to export from Unity to BabylonJS. I'm pretty far down the rabbit hole so I might as well...

Link to comment
Share on other sites

Cool!

I've made my terrain material entity, a DumpTerrainMaterial function in SceneBuilder.Materials.cs, and then I check to see if the shader matches my emulated one in SceneBuilder.Meshes.cs:

...else if (renderer.sharedMaterial.shader.name.Equals("BabylonJS/Terrain"))
{
     babylonMesh.materialId = DumpTerrainMaterial(renderer.sharedMaterial, renderer).id;
}
else
{
     babylonMesh.materialId = DumpMaterial(renderer.sharedMaterial, renderer).id;
}

which produces... another standard material.

Which is more than I had before! Before the above code wouldn't even give me that much.

The material in BabylonJS shows up as an unlit white solid color, which tells me it's not getting texture/shader data.

To be honest I don't know how any of the materials are getting shader data since I'm doing all this through a gulp server, and I don't see any .fx or .ts files anywhere.

Somewhere along the way, I'm not telling BabylonJS enough to create a terrain material. Unless I'm mistaken, it's falling back to creating a standard material, and then it fails to load a texture because I'm trying to assign "diffuseTexture1" instead of just "diffuseTexture".

Link to comment
Share on other sites

I feel like I am still missing a vital piece of information... this other question is along the same train of thought:

I would love to be able to get Unity to export certain properties. Among the various things, such as name, id, and other properties that get serialized and sent to BabylonJS and parsed by the .max.js file, however, I don't see how the BabylonJS engine is getting type information.

Let's say I wanted to use a shader I made for unity to work in BabylonJS. I have a .shader which is assigned to a .material that has a few various .png images that determine color information. All of those are types of assets that can be packaged (copied), parsed, and then re-created in BabylonJS. Where the disconnect is happening is if I want that custom shader to look the same.

I can't come in and define my own material type - I have to conform to ones already available.

How could I get BabylonJS to look at external files for material data?

Link to comment
Share on other sites

Link to comment
Share on other sites

Yes I did!

private BabylonTerrainMaterial DumpTerrainMaterial(Material material, Renderer renderer)
{
   ///construct the material data
   ...
   if (material.HasProperty("_SplatMap"))
   {
      ...
      //copy material data and texture
   ...
   terrainMaterialsDictionary.Add(bMat.name, bMat);

   return bMat;
}

So I've emulated the functionality and it imports into Babylon with the mix, diffuse, and bump textures, except for instead of creating a terrain material, it creates a standard material. I've stepped through the code and it's even calling the functions inside babylon.terrainMaterial.js!

Edit: although it isn't returning anything...

Link to comment
Share on other sites

Cool! So i added a customType string to the BabylonTerrainMaterial.cs file and it gave me this.

babylon.max.js:4413 Uncaught TypeError: Cannot read property 'BabylonTerrainMaterial' of undefined

I also tried passing just 'TerrainMaterial' and it gives the previously observed error.

Edit: Because the customType has to be "BABYLON.TerrainMaterial" !!

So it now imports into BabylonJS as a terrain material - exciting. Now I have to make it not black.

Edit: which is apparently caused by alpha being 0. Set it to 1 and I can now see it.

Link to comment
Share on other sites

Yay! So I'm getting all the expected data from the textures and materials.

I've left the BabylonJS terrain shaders alone, and I'm wondering if it's causing the terrainMaterials to not get light from point lights. There's a screenshot attached showing the unity scene and then the BabylonJS with a bright point light and a small spot light. The exporter is tweaked a bit to mess with exponents and intensity, but everything should be conducive to emulate the lighting conditions from Unity to BabylonJS!

TerrainLighting1.png

Also the standard material that gets imported loses its diffuse color, but I think that's a different problem that I've inadvertently caused elsewhere.

Link to comment
Share on other sites

  • 3 weeks later...

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