reepblue Posted February 14, 2017 Share Posted February 14, 2017 This was a quick test, and it worked first try. This is a Beam class derived from the Sprite class. All the beam really is is a sprite that streaches from one point to another. Use Josh's tolua++ generator to expose it. The only way to create a beam is with something like this: Script.EndPoint = nil --Entity function Script:Start() self.beam = Beam:Create() self.beam:SetStartPoint(self.entity) self.beam:SetEndPoint(self.EndPoint) self.beam:SetMaterial(self.entity:GetMaterial()) end le_beam_class.zip 6 Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
gamecreator Posted February 15, 2017 Share Posted February 15, 2017 Nice. For my , I used a 3d laser model that I simply pointed and scaled (and hid and showed as needed). laser->Point(x, y, z, 2, 1, 0); laser->SetScale(0.4, 0.4, laser->GetDistance(rock[closestrockindex].model) / 10); Quote Link to comment Share on other sites More sharing options...
Einlander Posted February 15, 2017 Share Posted February 15, 2017 Reepblue we seem to have the same idea all the time! I also have a lua based laser beam script for leadwerks. Though I suspect yours will handle materials better than mine can. These are the notes to the script: Notes: If you want your beam to be see through, the material need to be see through. My Recomendation is to set the material to alpha and to set the last diffuse box to 128 Since we are streching a quad (thats what a sprite is in Leadwerks) your material will be unrecognizable Script in spoiler. --[[ Title: Laser Beam Author: Einlander Version: .01 Start Date: 1-22-2016 Description: Draws a laser beam between 2 pivots Notes: If you want your beam to be see through, the material need to be see through. My Recomendation is to set the material to alpha and to set the last diffuse box to 128 Since we are streching a quad (thats what a sprite is in Leadwerks) your material will be unrecognizable --]] Script.enabled = true --bool "Enabled" Script.EndPoint = nil --entity "End Point:" Script.BeamMaterialFile = "" --path "Beam Material:" "Material File (*.mat):mat|Material" Script.BeamHeight = .2 --float "Beam Height cm:" Script.PhysicsUpdate = true --bool "Update in Physics:" function Script:Start() self.BeamHeight = self.BeamHeight / 10 -- Check End Point Debug:Assert(self.EndPoint ~= nil , "End point missing. Was an entity added?") -- Load the Material self.BeamMaterial = Material:Load(self.BeamMaterialFile) Debug:Assert(self.BeamMaterial ~= nil , "Error loading the material.") self.BeamMaterial = tolua.cast(self.BeamMaterial:Copy(),"Material") -- force a copy of the asset because we will need to modify it. self.BeamMaterial:SetSortMode(true) -- force zsorting (It will be used in a sprite,) -- Create sprite self.LaserBeam = Sprite:Create() self.LaserBeam:SetSize(self.BeamHeight,.1) self.LaserBeam:SetPickMode(0) self.LaserBeam:SetMaterial(self.BeamMaterial) -- assign material self.LaserBeam:SetShadowMode(2) self.LaserBeam:Show() --draw Initial Beam if self.enabled then self:DebugRay(self.entity:GetPosition(true), self.EndPoint:GetPosition(true), self.LaserBeam) end self.OldStartPosition = self.entity:GetPosition(true) self.OldEndPosition = self.EndPoint:GetPosition(true) end -- Draws the Beam function Script:DebugRay(startpos_as_vec3, endpos_as_vec3, spriteEntity_as_sprite) local startpos = startpos_as_vec3 local endpos = endpos_as_vec3 local spriteEntity = spriteEntity_as_sprite local midpoint = ((startpos + endpos) / 2) local raydistance = math.sqrt((endpos.x - startpos.x)^2 + (endpos.y - startpos.y)^2 + (endpos.z - startpos.z)^2) spriteEntity:SetPosition(midpoint,true) local allignvector = startpos - endpos spriteEntity:SetViewMode(6) spriteEntity:AlignToVector(allignvector:Normalize(),2) spriteEntity:SetSize(self.BeamHeight, raydistance) end function Script:UpdateWorld() --check if the positions moved if (self.PhysicsUpdate == false) and self.enabled then if ((self.OldStartPosition == self.entity:GetPosition(true)) ~= (self.OldEndPosition == self.EndPoint:GetPosition(true))) then -- Only update if moved self:DebugRay(self.entity:GetPosition(true), self.EndPoint:GetPosition(true), self.LaserBeam) self.OldStartPosition = self.entity:GetPosition(true) self.OldEndPosition = self.EndPoint:GetPosition(true) end end end function Script:UpdatePhysics() -- update in physics to save some processing cycles --check if the positions moved if (self.PhysicsUpdate == true) and self.enabled then if ((self.OldStartPosition == self.entity:GetPosition(true)) ~= (self.OldEndPosition == self.EndPoint:GetPosition(true))) then -- Only update if moved self:DebugRay(self.entity:GetPosition(true), self.EndPoint:GetPosition(true), self.LaserBeam) self.OldStartPosition = self.entity:GetPosition(true) self.OldEndPosition = self.EndPoint:GetPosition(true) end end end function Script:Enable()--in if self.enabled==false then self.enabled=true self.component:CallOutputs("_Enabled") end end function Script:Disable()--in if self.enabled then self.enabled=false self.component:CallOutputs("_Disabled") end end --This function will be called when the entity is deleted. function Script:Detach() self.BeamMaterial:Release() self.LaserBeam:Release() end 1 Quote Link to comment Share on other sites More sharing options...
reepblue Posted February 15, 2017 Author Share Posted February 15, 2017 This class is actually based off my beam script, but much simpler. This is written in C++, and all it does is stretch and orient the sprite to two points. You can use this class to make lasers like in Portal 2, simple elevator cables or physic ropes with a script. This just makes it so you don't have to do the setup and math each time. Since the beam is derived from the Sprite Class, everything when it comes to loading a material, releasing and such; that's inherited from the base entity/object class. All I really did was did the math in the Draw call. Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Einlander Posted February 15, 2017 Share Posted February 15, 2017 Yeah just checked your script, and we do the same things. 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.