Jump to content

Drawing dynamic material on models and similar?


Pastaspace
 Share

Recommended Posts

Is it possible that one can draw on a model.

That is, normally, I would have a UI by drawing 2d images with the context object, what I want to instead is have a 3d object, a plane that displays the player's health and other attributes, that evidently updates with the relevant information. Does Leadwerks support this?

Link to comment
Share on other sites

well it can be done by creating your own buffer, drawing to it, getting its the color component, and then binding that to the diffuse texture of model's material.

function App:Start()
       self.window = Window:Create("dynamic material example",0,0,800,600,Window.Titlebar+Window.Center)
       self.context = Context:Create(self.window)
       self.world = World:Create()
       self.buffer = Buffer:GetCurrent()
       self.camera = Camera:Create()
       self.light = DirectionalLight:Create()
       self.light:SetRotation(45,45,45)
       self.box1 = Model:Box()
       self.mat1 = Material:Create()
       self.shader = Shader:Load("Shaders/model/diffuse.shader")
       self.mat1:SetShader(self.shader)
       self.shader:Release()
       self.box1:SetPosition(0,0,3)
       self.box1:SetMaterial(self.mat1)

       self.mybuffer = Buffer:Create(100,100,1,1)

       return true
end

function App:Loop()
       if self.window:Closed() or self.window:KeyHit(Key.Escape) then return false end

       self.box1:Turn(Time:GetSpeed()*0.5,Time:GetSpeed()*0.5,0)

       Time:Update()
       self.world:Update()
       Buffer:SetCurrent(self.buffer)
       self.world:Render()

       Buffer:SetCurrent(self.mybuffer)
       self.context:SetBlendMode(Blend.Alpha)
       self.context:SetColor(1,0,0,1)
       self.context:DrawRect(0,0,100,100)
       self.context:SetColor(1,.5,0,1)
       self.context:DrawText(string.format("FPS: %.2f",Time:UPS()),15,40)

       self.tex = self.mybuffer:GetColorTexture(0)
       self.mat1:SetTexture(self.tex,0)

       self.context:Sync(true)

       return true
end

  • Upvote 2

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

  • 1 year later...

Quick updated code:

 

--Initialize Steamworks (optional)
Steamworks:Initialize()

--Set the application title
title="test"

--Create a window
local windowstyle = window.Titlebar
if System:GetProperty("fullscreen")=="1" then windowstyle=windowstyle+window.FullScreen end
window=Window:Create(title,0,0,System:GetProperty("screenwidth","1024"),System:GetProperty("screenheight","768"),windowstyle)
window:HideMouse()

--Create the graphics context
context=Context:Create(window,0)
if context==nil then return end

--Create a world
world=World:Create()
world:SetLightQuality((System:GetProperty("lightquality","1")))

--create buffer, acamera and material
buffer = Buffer:GetCurrent()
camera = Camera:Create()
light = DirectionalLight:Create()
light:SetRotation(45,45,45)
box1 = Model:Box()
mat1 = Material:Create()
shader = Shader:Load("Shaders/model/diffuse.shader")
mat1:SetShader(shader)
shader:Release()
box1:SetPosition(0,0,3)
box1:SetMaterial(mat1)

mybuffer = Buffer:Create(100,100,1,1)

while window:KeyDown(Key.Escape)==false do

   box1:Turn(1,1,1)

   --If window has been closed, end the program
   if window:Closed() then break end

   --Update the app timing
   Time:Update()

   --Update the world
   world:Update()

   --Render the world
   world:Render()


   --draw context to buffer
   Buffer:SetCurrent(mybuffer)
		    context:SetBlendMode(Blend.Alpha)
		    context:SetColor(.1,.1,.1,1)
		    context:DrawRect(0,0,100,100)
		    context:SetColor(1,.5,0,1)
		    context:DrawText(string.format("FPS: %.2f",Time:UPS()),15,40)
		    tex = mybuffer:GetColorTexture(0)
		    mat1:SetTexture(tex,0)
   Buffer:SetCurrent(context)

   --Refresh the screen
   context:Sync(true)
end


 

Pic

post-747-0-74911700-1468018516.jpg

  • Upvote 1

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

Here's a quick and dirty method version of drawing text onto a textured object using the emissions shader:

window = Window:Create("CLOCK",0,0,500,500,Window.Titlebar+Window.Center)
context = Context:Create(window)
world = World:Create()
camera = Camera:Create()
light = DirectionalLight:Create()
light:SetRotation(30,35,0)

mat1 = Material:Create()
tex0 = Texture:Load("Materials/Concrete/concrete_clean_diff.tex")
tex1 = Texture:Load("Materials/Concrete/concrete_clean_dot3.tex")
tex2 = Texture:Load("Materials/Concrete/concrete_clean_spec.tex")
mat1:SetTexture(tex0,0)
mat1:SetTexture(tex1,1)
mat1:SetTexture(tex2,2)
shader = Shader:Load("Shaders/model/diffuse+normal+specular+emission.shader")
mat1:SetShader(shader)

box1 = Model:Box()
box1:SetPosition(0,0,1.5)
box1:SetMaterial(mat1)

buffer = Buffer:GetCurrent()
mybuffer = Buffer:Create(100,100,1,1)

while not window:KeyHit(Key.Escape) do
	if window:Closed() then return false end
	
	box1:Turn(0,Time:GetSpeed()*0.5,0)

 	Time:Update()
	world:Update()
	world:Render()
	 
	Buffer:SetCurrent(mybuffer)
	context:SetBlendMode(Blend.Alpha)
	context:SetColor(0,0,0,0)
	mybuffer:Clear()
	context:SetColor(1,0,0,1)
	context:DrawText(os.date("%I:%M:%S"),25,45)
	tex4 = mybuffer:GetColorTexture(0)
	mat1:SetTexture(tex4,4)
	Buffer:SetCurrent(buffer)
	context:Sync(true)
end

post-14-0-99390000-1468019563_thumb.jpg

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

For the sake of completeness, here is how I use a custom shader to procedurally create textures...

 

-- NB: we have created previously a model and stored it into the model var
local surface = model:GetSurface(0)
local material = surface:GetMaterial()
local shader = Shader:Load("Shaders/Model/diffuse.shader")
material:SetShader(shader)

-- Save ref to the current shader & buffer.
local context = Context:GetCurrent()
local defaultShader = context:GetShader()
local defaultBuffer = Buffer:GetCurrent()

-- Specify a TEXTURE size.
local textureSize = 128

-- Use a custom SHADER to draw a texture in the GPU.
local customShader = Shader:Load("Scripts/Custom/drawimage.shader")

-- Create a custom BUFFER to draw a texture to.
-- static Buffer* Create(const int width, const int height, const int colorcomponents=1,
-- const int depthbuffer=1, const int multisamplemode=0);//lua
local customBuffer = Buffer:Create(textureSize, textureSize, 1, 1)
Buffer:SetCurrent(customBuffer)

-- must be called after Buffer:SetCurrent()
context:SetShader(customShader)

-- DRAW the texture.
--context:SetBlendMode(Blend.Alpha)
local texture = customBuffer:GetColorTexture(0)
context:DrawImage(texture, 0, 0)
material:SetTexture(texture, 0)

-- Restore previous buffer and shader.
Buffer:SetCurrent(defaultBuffer)
context:SetShader(defaultShader)

  • Like 1
  • Upvote 1
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...