Jump to content

How would I attach a light to a model?


L B
 Share

Recommended Posts

One way to do that is to use

 

light:SetParent(model)

 

light = your light entity and model = your model

How would I access these objects? I just want to place my streetlamp entity and it already has the light on it. I copied the code from the point light script into the streetlamp script, and it works fine. I have a point light, but at the origin of the model, which is way too low.

 

How would I go making "entity.light" move up?

I tried "entity.light:Move(0, -2, 0)" but this doesn't work.

 

dofile("Scripts/base.lua") -- We need the base class entity

function InitDialog(grid)
base_InitDialog(grid)
group=grid:AddGroup("Light")
group:AddProperty("Resolution",PROPERTY_CHOICE,"256,512,1024,2048")
group:AddProperty("linearoffset",PROPERTY_FLOAT,"0,1,2","Linear offset")
group:AddProperty("multoffset",PROPERTY_FLOAT,"0,1,2","Multiplicative offset")
group:AddProperty("Range",PROPERTY_FLOAT,"1,100,0")
group:Expand(1)
end

function Spawn(model)
local entity=base_Spawn(model)
entity.model=model
entity.light=CreatePointLight(10,model)
return entity
end

function GetKey( model, key, value )
local entity=entitytable[model]
if entity==nil then return value end
if entity.model==nil then return end
if entity.light==nil then
	return base_GetKey(model,key,value)
else
	if key=="linearoffset" then
		return entity.light:GetShadowOffset(0,0)--..","..entity.light:GetShadowOffset(0,1)..","..entity.light:GetShadowOffset(0,2)
	elseif key=="multoffset" then
		return entity.light:GetShadowOffset(1,0)--..","..entity.light:GetShadowOffset(1,1)..","..entity.light:GetShadowOffset(1,2)
	elseif key=="range" then
		return entity.light:GetRange()
	else
		return base_GetKey(model,key,value)
	end
end

end

function SetKey(model, key,value)
local entity=entitytable[model]
if entity==nil then return 1 end
if entity.model==nil then return 1 end
if entity.light==nil then
	base_SetKey(model,key,value)
else
	if key=="resolution" then
		if value=="0" then
			entity.light:SetShadowmapSize(256)
		elseif value=="1" then
			entity.light:SetShadowmapSize(512)
		elseif value=="2" then
			entity.light:SetShadowmapSize(1024)
		elseif value=="3" then
			entity.light:SetShadowmapSize(2048)
		end
	elseif key=="range" then
		entity.light:SetRange(value)
	elseif key=="shadowresolution" then
		resolution=entity.light:GetShadowmapSize()
		if resolution==256 then
			return 0
		elseif resolution==512 then
			return 1
		elseif resolution==1024 then
			return 2	
		elseif resolution==2048 then
			return 3
		else
			return -1
		end
	elseif key=="multoffset" then
		entity.light:SetShadowOffset(entity.light:GetShadowOffset(0,0),value,0)
	elseif key=="linearoffset" then
		entity.light:SetShadowOffset(value,entity.light:GetShadowOffset(1,0),0)
	else
		return base_SetKey(model,key,value)
	end
end
return 1
end

--[[

dofile("Scripts/baseclass.lua") -- We need the base class entity
dofile("Scripts/table.lua") -- We need the table.Inherit function

-- Set this script"s Entity table to an inherit of BaseClass"s entity table
EntityNew = { }
Entity = table.Inherit(EntityNew, Entity)

function Entity:Spawn(model)
if (model == nil) then Notify("Entity passed to Entity:Spawn is nil") end
Entity.BaseClass:Spawn(model)
-- Since the entity object is passed to the BaseCLass spawn, it is in Entity.BaseClass.model, we need to either remove the above line and uncomment the one below, or simply leave both
-- If you are wondering why Entity.model isn"t inherited at the top of the script when we called table.Inherit, it is because we had not yet defined an Entity.model variable
-- You will come to notice that variables we assign via BaseClass functions AFTER inheritence will not exist from this Entity scope. We can still access them via Entity.BaseClass.variablename
--Entity.model = Entity.BaseClass.model -- Hold onto you for later use and add it to this scope 
self.model = model
model:SetKey("intensity","1")
self.light = CreatePointLight(10,model)
self.speed=1.0
end

function Entity:SetKey(key,value)
if self.model==nil then return 1 end
if self.light==nil then
	self.BaseClass:SetKey(key,value)
else
	if key=="resolution" then
		if value=="0" then
			self.light:SetShadowmapSize(256)
		elseif value=="1" then
			self.light:SetShadowmapSize(512)
		elseif value=="2" then
			self.light:SetShadowmapSize(1024)
		elseif value=="3" then
			self.light:SetShadowmapSize(2048)
		end
	elseif key=="muloffset" then
		self.light:SetShadowOffset(0,tonumber(value),0)
	elseif key=="range" then
		self.light:SetRange(value)
	elseif key=="shadowresolution" then
		resolution=self.light:GetShadowmapSize()
		if resolution==256 then
			return 0
		elseif resolution==512 then
			return 1
		elseif resolution==1024 then
			return 2	
		elseif resolution==2048 then
			return 3
		else
			return -1
		end
	else
		return self.BaseClass:SetKey(key,value)
	end
end
return 1
end

function Entity:InitDialog(grid)
self.BaseClass:InitDialog(grid)
group=grid:AddGroup( "Light" )
group:AddProperty( "Resolution", "1|256,512,1024,2048", PROPERTY_CHOICE, "" )
group:AddProperty( "muloffset","|0,1,2",PROPERTY_FLOAT,"Mult offset" )
group:AddProperty( "range","|1,100,0",PROPERTY_FLOAT,"" )
group:Expand(1)
end

function Entity:ReceiveMessage(message,extra)
if message=="toggle" then
	if self.model:Hidden()==1 then
		self.model:Show()
	else
		self.model:Hide()
	end
else
end
end
]]--

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...