Marcousik Posted May 16, 2021 Share Posted May 16, 2021 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. Quote Link to comment Share on other sites More sharing options...
Slastraf Posted May 16, 2021 Share Posted May 16, 2021 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 Quote Link to comment Share on other sites More sharing options...
Marcousik Posted May 16, 2021 Author Share Posted May 16, 2021 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. Quote Link to comment Share on other sites More sharing options...
Slastraf Posted May 17, 2021 Share Posted May 17, 2021 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. Quote Link to comment Share on other sites More sharing options...
Marcousik Posted May 17, 2021 Author Share Posted May 17, 2021 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. 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) TEMP 2021-05-17 11-46-39.mp4 Quote Link to comment Share on other sites More sharing options...
Slastraf Posted May 17, 2021 Share Posted May 17, 2021 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. 1 Quote Link to comment Share on other sites More sharing options...
havenphillip Posted May 17, 2021 Share Posted May 17, 2021 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: Quote Link to comment Share on other sites More sharing options...
havenphillip Posted May 17, 2021 Share Posted May 17, 2021 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 2 Quote Link to comment Share on other sites More sharing options...
Marcousik Posted May 18, 2021 Author Share Posted May 18, 2021 Yes. But now no idea how to make it in shader language? terrain example 2021-05-18 07-48-01.mp4 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 Quote Link to comment Share on other sites More sharing options...
havenphillip Posted May 18, 2021 Share Posted May 18, 2021 You scripted the waves? 1 Quote Link to comment Share on other sites More sharing options...
Marcousik Posted May 18, 2021 Author Share Posted May 18, 2021 Ok so here I could reproduce the wave equation from the shader (transparent in the video) and apply the same movement on the terrain (with a script)... So waves are now - in a basic form - predictable ? TEMP 2021-05-18 15-29-41.mp4 1 Quote Link to comment Share on other sites More sharing options...
Slastraf Posted May 18, 2021 Share Posted May 18, 2021 this is enough for collision but to maek your actual wayve more interesting you could add smaller noises at higher frequency than the big wave. https://www.shadertoy.com/view/MtsXzl 1 Quote Link to comment Share on other sites More sharing options...
Marcousik Posted May 18, 2021 Author Share Posted May 18, 2021 Sure I will - I want to make something like this, maybe with more main wave move: TEMP 2021-05-18 12-28-23.mp4 Adding waves forces directions, and with this shader, a beautiful water feature should now be possible in good performance. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.