Jump to content

Is it possible to texture a surface at runtime instead of designtime?


MisterGravity
 Share

Recommended Posts

Let's say there was a scenario where the color of this side of a brush was white unless something was true, and then it was red. Is that possible at runtime?

 

 

If thisVariable = True then thatWallColor = Red

I'm sure I'll have a million questions in my quest to master the Leadwerks Engine. Thank you for your patience.

Link to comment
Share on other sites

You'd change the material that is on that wall. If you are talking CSG then I believe in order to do this that wall needs a mass or a script (even an empty script). If CSG doesn't have mass or an empty script then all the CSG gets collapsed and there is no way to tell it apart from the other CSG. If you give it a mass or script then it acts just like a normal entity.

 

http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/entity/entitysetmaterial-r183

 

This is how you would change textures/materials on an entity.

 

You could create a script that takes an entity and material as parameters. Drag the CSG as the entity and select a material you want to change it to have. Then you could create and input to use with the flowgraph that does the actual setting of the material on the CSG/Brush.

 

Then you'd have to decide on the condition that you'd want. What are you specifically thinking about with this? The implementation of this could be very generic or very specific and could be done in a flowgraph.

Link to comment
Share on other sites

What are you specifically thinking about with this?

 

Well I don't want to give too much away since it's a primary storyline point of the game I have in mind, but I'll give you this example:

 

The character is walking around this area and the walls are white because he hasn't found this particular item yet. So as soon as he finds it and it's in his "inventory", that wall is painted some other color now from now on. The color change/texture is based on whether or not the player has found this "thing". I used the word "inventory", but it's not so much an inventory you would find in a RPG, but instead think of it as having found a gun, or a smg, or a flamethrower. I'm treating the weapons you've found as the inventory, and when you found the flamethrower, that wall is red now from now on.

I'm sure I'll have a million questions in my quest to master the Leadwerks Engine. Thank you for your patience.

Link to comment
Share on other sites

Just to add to Rick's excellent description:You can also change a material's texture at runtime using Material:SetTexture(), where the texture at index 0 (the default) is the diffuse (main) texture.

 

But I guess in your case you may just set the material's color using Material:SetColor() as in the code example. This color is multiplied with the diffuse texture and is normally plain white (so you get the texture only). If you change that in a script (in the Update() method) to, for example, (1,0,0) the texture will get a red tint.

Link to comment
Share on other sites

OK, and finding the weapon I assume would be colliding with it? If so then here is what you'll want to do.

 

Attach and empty script to the wall you want to change the texture on so that the engine won't combine it with all the other brushes.

Attach the collision script to the weapon model in the world. This is so we can be informed via it's output when a collision happens.

Make a script that take an entity parameter (which will be your brush you want to change the material on) and a path, which will be the material you want it to change to.

 

Script.Brush = nil --entity

Script.Material = "" --path

 

Make an input method that says change material

 

function Script:ChangeMaterial()--in

self.Brush:SetMaterial(self.Material)

end

 

 

Make a pivot that is going to act like a logical entity (no visual but just to hold the new script you made). Attach the new script to it.

 

Then open up the flowgraph and drag in your weapon model that has the collision script and the pivot that has the new script. Then connect the collision output to the new scripts ChangeMaterial input.

 

 

So now you have created a nice generic script that changes a material on anything. You can use this in many different places. You could also attach this script to the brush in question and remove the entity parameter and just use self.entity to change the material on.

Link to comment
Share on other sites

OK, and finding the weapon I assume would be colliding with it? If so then here is what you'll want to do.

 

Attach and empty script to the wall you want to change the texture on so that the engine won't combine it with all the other brushes.

Attach the collision script to the weapon model in the world. This is so we can be informed via it's output when a collision happens.

Make a script that take an entity parameter (which will be your brush you want to change the material on) and a path, which will be the material you want it to change to.

 

Script.Brush = nil --entity

Script.Material = "" --path

 

Make an input method that says change material

 

function Script:ChangeMaterial()--in

self.Brush:SetMaterial(self.Material)

end

 

 

Make a pivot that is going to act like a logical entity (no visual but just to hold the new script you made). Attach the new script to it.

 

Then open up the flowgraph and drag in your weapon model that has the collision script and the pivot that has the new script. Then connect the collision output to the new scripts ChangeMaterial input.

 

 

So now you have created a nice generic script that changes a material on anything. You can use this in many different places. You could also attach this script to the brush in question and remove the entity parameter and just use self.entity to change the material on.

 

This sounds like exactly what I should aim for because once you find it, it happens everywhere, and it's likely you'll be backtracking to places that previously didn't have a texture at all. I also liked the previous example where Rastar was just changing the color, but the walls will have a texture definitely, not so much just a color. So like this wallpaper or that wall will have a red-ish texture, like red brick, not just be the color red. So since that brick is red, when you find that flamethrower, you can see those bricks the way they're supposed to be.

 

Now as I was thinking it through in my head a moment ago, I thought, will it work like this?

 

"Everything is white until that variable is true, and then it'll draw it's real texture."

 

So that brick wall really is a brick wall texture, but you don't see it that way until you have that flamethrower. So is the script really suppressing the real texture and drawing a white texture, or a nil/no texture until that is true? That seems like it would work the cleanest because I don't want to be "painting new textures" at runtime, more of a "time you see it, which one do I show based on what's in your inventory" kinda thing.

I'm sure I'll have a million questions in my quest to master the Leadwerks Engine. Thank you for your patience.

Link to comment
Share on other sites

So is the script really suppressing the real texture and drawing a white texture, or a nil/no texture until that is true? That seems like it would work the cleanest because I don't want to be "painting new textures" at runtime, more of a "time you see it, which one do I show based on what's in your inventory" kinda thing.

 

I think you have to ask yourself what is the difference between suppressing the real texture and switching textures? I'd say nothing. Paint a white material on it to start the game with. Then when events happen in your game (player collides with a weapon) switch materials to the brick texture. Now, if you want that to persist over runs, you'd have to come up with some code that runs when the game starts to check the inventory and change textures accordingly.

Link to comment
Share on other sites

Or you could start with no texture and later change it using SetTexture(). A downside would be maybe that the texture name would have to be stored in your script. Or you could assign the diffuse texture in the editor during design time, then get a reference to it and remove it in the Start() method of your script and later on assign it again when the player has found that flamethrower.

 

So in Start() do

 

self.texture = material:GetTexture()

material:SetTexture(nil)

 

and later in Update()

 

if (itemFound) then material:SetTexture(self.texture)

 

or something similar.

Link to comment
Share on other sites

You can use the path parameter type to select a texture to use. Changing textures in a material or changing materials, either way I don't think it matters all that much. The place it would matter is if you wanted a different look and feel in the white look vs the final look. Then you'd do materials since they define different looks and feels the texture should have.

 

Script.Texture = "" --path (allows the user of the script to select the texture in the assets folders)

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.

 Share

×
×
  • Create New...