Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Painting/drawing in Leadwerks?

Recommended Posts

It's easy, you just need the model to me UV mapped so that each area is unique (otherwise you will see duplicate changes all over the model), and you need to turn the picked position into a UV coordinate. Coincidentally, I was dealing with that problem yesterday. Here is my code. The position should be in local space (transformed from world space to the entity). "res" is the position I am finding the UV coordinate for:

if (diffusemap)
	diffuse[0] = 255;
	diffuse[1] = 0;
	diffuse[2] = 255;
	diffuse[3] = 255;

	V1 = a, V2 = b, V3 = c;

	DX1 = vertices[V2].position.x - vertices[V1].position.x;
	DY1 = vertices[V2].position.y - vertices[V1].position.y;
	DZ1 = vertices[V2].position.z - vertices[V1].position.z;
	DX2 = vertices[V3].position.x - vertices[V1].position.x;
	DY2 = vertices[V3].position.y - vertices[V1].position.y;
	DZ2 = vertices[V3].position.z - vertices[V1].position.z;

	NX = DY1 * DZ2 - DY2 * DZ1;
	NY = DX2 * DZ1 - DX1 * DZ2;
	NZ = DX1 * DY2 - DY1 * DX2;
	UX = NY * DZ2 - DY2 * NZ;
	UY = DX2 * NZ - NX * DZ2;
	UZ = NX * DY2 - NY * DX2;

	InvLength1 = 1.0f / sqrt(UX * UX + UY * UY + UZ * UZ);
	Length2 = sqrt(DX2 * DX2 + DY2 * DY2 + DZ2 * DZ2);
	InvLength2 = 1.0f / Length2;

	UX = UX * InvLength1;
	UY = UY * InvLength1;
	UZ = UZ * InvLength1;
	DX2 = DX2 * InvLength2;
	DY2 = DY2 * InvLength2;
	DZ2 = DZ2 * InvLength2;

	T1 = 0.0f;
	S1 = 0.0f;
	T2 = DX1 * UX + DY1 * UY + DZ1 * UZ;
	S2 = DX1 * DX2 + DY1 * DY2 + DZ1 * DZ2;
	T3 = 0.0f;
	S3 = Length2;
	T4 = (res.x - vertices[V1].position.x) * UX + (res.y - vertices[V1].position.y) * UY + (res.z - vertices[V1].position.z) * UZ;
	S4 = (res.x - vertices[V1].position.x) * DX2 + (res.y - vertices[V1].position.y) * DY2 + (res.z - vertices[V1].position.z) * DZ2;

	Denominator = 1.0f / ((S2 - S3) * (T1 - T3) + (T3 - T2) * (S1 - S3));
	Lambda1 = ((S2 - S3) * (T4 - T3) + (T3 - T2) * (S4 - S3)) * Denominator;
	Lambda2 = ((S3 - S1) * (T4 - T3) + (T1 - T3) * (S4 - S3)) * Denominator;
	Lambda3 = 1.0f - Lambda1 - Lambda2;

	texcoords.x = vertices[V1].texcoords.x * Lambda1 + vertices[V2].texcoords.x * Lambda2 + vertices[V3].texcoords.x * Lambda3;
	texcoords.y = vertices[V1].texcoords.y * Lambda1 + vertices[V2].texcoords.y * Lambda2 + vertices[V3].texcoords.y * Lambda3;

	rgba = diffusemap->ReadPixel(Floor(Mod(texcoords.x, 1.0f) * float(diffusemap->size.x)), Floor(Mod(texcoords.y, 1.0f) * float(diffusemap->size.y)));
	diffuse[0] = Red(rgba);
	diffuse[1] = Green(rgba);
	diffuse[2] = Blue(rgba);
	diffuse[3] = 255;


  • Like 1
Link to post
Share on other sites

Ok. I'll have to take some time to pull this apart.  I can write something that puts a dot on the ground under the player but what part of this leaves the trail? Am I right about needing to create a frame buffer or some such buffer?

  • Like 1
Link to post
Share on other sites

That's a pretty sick shader but it has like 5 buffers. It's insane. The problem would still be how to do trails. I can draw a point at the player. But how to make it remember every point the player has been and draw that line? Sounds like Josh is saying use a render to texture - which makes me think of the minimap. Seems like maybe in script all I need is to bind a texture? I just need to start doing experiments.

Link to post
Share on other sites

Yeah that's crazy, sure.

I will try more experiments with decals but I don't expect to reach anything with movements like this mud (video) is moving under the force of the tires. 

I thought about using csgs too or adding sprites but still nothing really great.

You did that veggies moving shader by going through, maybe it could generate such an illlusion, moving  texture slowly apart, as mud has another physics.. I don't know just some confuse ideas..

Here I found others examples, with less buffers I don't know maybe could inspire 



Link to post
Share on other sites

The decals thing might work but you'd have to loan me the script. If so it would just be a matter of connecting the decals to a tesselation shader and figuring out some kind of tesselation lod. It would look cooler if it was drawing in height rather than on the diffuse, and it's possible you could randomize a few textures to get that mud up on the sides of the tires if a sprite sheet doesn't work.

The veggies moving shader only records a small area around the player. It doesn't remember where the player has been - so no trails still. But if I could get trails that one could be useful, especially for improving that particular shader.

  • Like 1
Link to post
Share on other sites

I don't understand what kind of script you need with the decals?

I just use a script to put the decals - assigned with a tire track material - on the ground or terrain and align each decals to the spline the car takes in its movement, I don't have anything else as script.

My idea about the "veggies moving shader" trails mud is that especially mud does not stay long on its form after a tire has been going through so it could take its original form back after the tire went through, at a speed depending on the tire speed? I don't know if it would work..

That's my problem with decals, they can give a great illusion of mud spread on a car but how do you want to give it the movements of mud waving slowly and going back down..?

There is a last option they could have used in the video..maybe it is possible to create a surface with mud material and move it, as you want using the vertex coordinates. With this method it was easy to recreate little waves like water could do, so it could be possible to move a surface as mud would do? 

Link to post
Share on other sites

"There is a last option they could have used in the video..maybe it is possible to create a surface with mud material and move it, as you want using the vertex coordinates. With this method it was easy to recreate little waves like water could do, so it could be possible to move a surface as mud would do?"

Yeah that's pretty much the idea. But I need to move the vertex according to the trails that are left in the wake of the tires. So the trails from the tires act as the heightmap. Getting the trails to "stay on the ground" is the trouble. If I can get that I can work out the rest of it.

You posted a partial script for your trails recently:

I think I would need that whole script - or some simplified version of it. From there I think I could work out a shader. I don't think it's the "proper" way, though, but it might work. I also don't know if I could get that working on terrain. On a model more likely.

From what I understand the correct way to do it is to create a buffer and somehow mix the buffer and the color texture together so that the renderer saves the created trail path.

  • Like 1
Link to post
Share on other sites

The whole script would be in this case the entire car script, means including a lot of non usable things for that.

The decal are always staying there where you position them.. There just gone erased with a scripted time factor and you place new ones on the terrain each few seconds.

So the question is for me: Could you work on a decal set on the terrain or model? Like that:

 - I have to say I tried to create a whole effect using normals... But it is nonsense, I mean, I didn't get anything usable



  • Like 1
Link to post
Share on other sites

Ok I tried downloading a few cars and scripts. I cant get it working it's getting too complicated. Would it be easy for you to write a single track script in the 3rd person player script so that the player leaves one trail behind? I just need something simple. I think you understand this decal thing better.

Link to post
Share on other sites

@Yue yeah they had this mud wonder simulation yet in games earlier than snowrunner.

@havenphillip ok, please follow this steps, I added quickly the feature tire tracks to an old jeep script:

1) Open the workshop in engine and look at the jeep and please install it in a project


2) Replace the script on the jeep with this one:

In the same folder .../scripts/objects/vehicules


3) Add this Material in the decals assets:


4) Start the map, drive the car, you need a FPSPlayer Leadwerks PLayer character.

Okay the car script is olddated, but the car should be drivable enough to get the tire tracks on the ground lol

You should get this:



You can  find the tire tracks script in the UpdateWolrd function of the car script (uploaded above.)

Please tell if problems thx.


  • Like 2
Link to post
Share on other sites

So well I tried a lot things with vertex but it comes to no satisfying result because the vertex are not bound - so moving down a vertex positoin just made a hole in the mesh :(

Then I tried moving csgs around the tires, with collision detection and I think - as I'm sure not going to make a snowrunner 2 (^^) - well it should be enough for my little game, until maybe Havenphillip finds something better with shadering...

Here is the result of this:

  • Like 2
  • Upvote 1
Link to post
Share on other sites

I know that is not the effect you were looking for, but that is pretty cool!

In Ultra Engine, decals are drawn in the same forward rendering pass as the geometry, so it will be possible to use a decal to modify displacement of tessellated terrain.

In Leadwerks, you probably need to modify the terrain height, and since the vertices aren't that close together, it's not going to make very detailed ruts.

  • Like 2
Link to post
Share on other sites
1 hour ago, havenphillip said:

Can you make them smaller, though? Can you manipulate the size and shape at all?

Yes size and shape can be changed as you want, but smaller, it will require to change the appearing rhythm or it will look a way less uniform (as mud should!)


1 hour ago, havenphillip said:

This takes some pressure off me I wasn't getting anywhere.

Please keep really cool.. nobody would never complain for becoming help,  or whatever, and those things are really difficult, that must stay 100% hobby.

  • Like 1
Link to post
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.

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.

  • Create New...