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

Find a vertex from global position


Recommended Posts

Imagine I'm walking with a player on a ground created with a box model scaled at 1500.

Now I would like to catch the vertex of the ground-box model that is nearest to the player, how could I do this?

https://www.leadwerks.com/learn?page=API-Reference_Object_Surface_Pick

if I use a pick on the surface, is there anything to find the nearest vertex or triangle ?

Thx a lot, I need to know this.

Link to post
Share on other sites

You would need to get the surface of the model. 
Not tested here I will give you a tip:
 

local model = Model:Load("Models/Grass/grass01.mdl")  
local surface = model:GetSurface(0)
local smallest = nil
for v=0,surface:CountVertices()-1 do
    local position = surface:GetVertexPosition(v)
  	--multiply by scale (you mentioned 1500)
  	local vertDistance = self.entity:GetDistance(model:GetPosition()+(surface:GetVertexPosition(v)* 1500))
	if smallest == nil or vertDistance < currDistance then
    	currDistance = vertDistance
		smallest = v
    end
end
local posOfNearestVert = model:GetPosition()+(surface:GetVertexPosition(smallest)* 1500)

This probably wont work on the first try, but is an example on how to approach this.
Also you cannot directly get the vertex, but the position as presented

Link to post
Share on other sites

Hi Slas,

It was an idea I got for finding how to calculate the waves movement generated by a shader,

but I'm thinking now shader does not move the vertices, does it?

So I doubt I can use this method...

I'm out of idea how to do this.

Calculating the movement with a script seems impossible, while I need to know at a time t which height has a wave to know if a car or whatever is underwater or not....

But thx whatever

Edit

Tested: Shader doesn't move the vertice so this can't be a method for calculation of shader movement.

Link to post
Share on other sites

The shader still calculates the height of the wave trough a function. It must contain sin / cos.
If you copy that function to lua with the same parameters, it will have mathematically the same result. Which parameters would need to be converted from leadwerks global space to whatever space the shader operates.

Link to post
Share on other sites

Yes it could be, but it is not, or not exactly like that.

Shaders have this genious thing to modify the surface using other textures.

In this case, havenphilip uses a displacement texture to create the basic waves. So this uses no sin functions in shader.

1991129485_Capturedcran(60).thumb.png.df7bb98c2e6556d8a1e467b345202140.png

But it is still true that it has a sin composition, so that's my idea too, from a script to try to reproduce this sinusoide that the image containts.

It is difficult but not impossible.

Once that would be  found, I could add fúrther more complex operations (like sin) that the shader adds.

I see no other solution. For now I obtained this: The ball floating is coded with a script (no physics)

 

Link to post
Share on other sites

I see.
You don't need a displacement map. This can easily be calculated
this is basically a cos function in the range of [0, 8*pi]
pixel(x,y) = cos((y/height)*3.1415926*8)

Just to give you a headstart.
To get the color information you would use the function from above like this disp = (pixel(x,y)*0.5+1)/255
because cos goes from -1 to 1 and if we add one it goes from 0 to 2, then multiply by 0.5 to get from 0 to 1 and divide by 255 to get the color intensity.
 

  • Thanks 1
Link to post
Share on other sites

That's possible. I don't know how to do it. But I guess you could write the waves in the script. Just a sin wave on the surface. If we could do that we'd have wave height.

Macklebee did something like that:

 

Link to post
Share on other sites

Here's the post where he shows the script. I copied it down:

 

 

window = Window:Create("terrain example",0,0,800,600,Window.Titlebar+Window.Center)
context = Context:Create(window)
world = World:Create()
camera = Camera:Create()
camera:SetPosition(0,5,-5)
camera:SetMultisampleMode(8)
skybox = Texture:Load("Materials/Sky/skybox_texture.tex")
camera:SetSkybox(skybox)
light = DirectionalLight:Create()
light:SetRotation(35,35,0)

terrain = Terrain:Create(64,true)
terrain:SetLayerTexture(0, Texture:Load("Materials/Developer/Bluegrid.tex"), 0)
terrain:SetScale(1,100,1)

ball = Model:Sphere(32)
ballmat = Material:Load("Materials/Developer/Orangegrid.mat")
ball:SetMaterial(ballmat)
ball:SetScale(4,4,4)
shape = Shape:Sphere(0,0,0, 0,0,0, 1,1,1)
ball:SetShape(shape)
shape:Release()
ball:SetCollisionType(Collision.Prop)
ball:SetSweptCollisionMode(true)
ball:SetPosition(24,6,0)
ball:SetMass(1)

camera:Point(ball)

while window:KeyDown(Key.Escape)==false do
	if window:Closed() then break end
	
	pos = ball:GetPosition(true)
	camera:SetPosition(pos.x,6,pos.z-10)
	camera:Point(ball)
	
	if terrain~=nil then
		for x = 0, 64  do
			local y = Time:Millisecs()/600
			for z = 0, 64 do
				local height = math.sin(y+x) / 40
				terrain:SetHeight(x,z,height)
			end
		end
		terrain:UpdateNormals()
	end
 
	Time:Update()
	world:Update()
	world:Render()
	
	context:Sync(true)
end
  • Thanks 2
Link to post
Share on other sites

Yes. But now no idea how to make it in shader language?

 

Quote

window = Window:Create("terrain example",0,0,800,600,Window.Titlebar+Window.Center)
context = Context:Create(window)
world = World:Create()
camera = Camera:Create()
camera:SetPosition(0,5,-5)
camera:SetMultisampleMode(8)
skybox = Texture:Load("Materials/Sky/skybox_texture.tex")
camera:SetSkybox(skybox)
light = DirectionalLight:Create()
light:SetRotation(35,35,0)

terrain = Terrain:Create(64,true)
terrain:SetLayerTexture(0, Texture:Load("Materials/Developer/Bluegrid.tex"), 0)
terrain:SetScale(1,100,1)

ball = Model:Sphere(32)
ballmat = Material:Load("Materials/Developer/Orangegrid.mat")
ball:SetMaterial(ballmat)
ball:SetScale(4,4,4)
shape = Shape:Sphere(0,0,0, 0,0,0, 1,1,1)
ball:SetShape(shape)
shape:Release()
ball:SetCollisionType(Collision.Prop)
ball:SetSweptCollisionMode(true)
--ball:SetPosition(24,6,0)
ball:SetPosition(0,0,0)
ball:SetMass(1)

camera:Point(ball)

while window:KeyDown(Key.Escape)==false do
    if window:Closed() then break end
    
    pos = ball:GetPosition(true)
    camera:SetPosition(pos.x,6,pos.z-10)
    camera:Point(ball)
    
    if terrain~=nil then
            local y = Time:Millisecs()/600

        for x = 0, 64  do
            for z = 0, 64 do
                local height = math.sin(y+(x*math.sin(z))) / 40
                terrain:SetHeight(x,z,(height)*height*10)
            end
        end
        terrain:UpdateNormals()
    end
 
    Time:Update()
    world:Update()
    world:Render()
    
    context:Sync(true)
end

 

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.

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.

×
×
  • Create New...