You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

代码生成的env_lightglow无可见效果,求实现联合军地雷式灯光

问题描述

我正在创建一个SENT,需要添加一个闪烁的发光灯光,要求不能照亮环境,效果类似联合军地雷顶部的灯光。目前尝试用env_lightglow实现,但实体已生成(IsValid()返回true),游戏里完全看不到效果。已调整spawnflags等多个键值,也给实体命名了,但没用。

我的代码:

function ENT:Initialize()
    self:SetModel( "models/Items/combine_rifle_ammo01.mdl" )
    self:PhysicsInit( SOLID_VPHYSICS )
    self:SetMoveType( MOVETYPE_VPHYSICS )
    self:PhysWake()
    self:SetIsActive( false )

    -- Initialize the light object
    local light = ents.Create( "env_lightglow" )
    light:SetPos( self:GetPos() + Vector( 0, 0, 35 ) )
    light:SetAngles( Angle( 0, 0, 0 ) )
    light:SetParent( self )
    light:SetName( "MotionSensorLight" )
    light:SetKeyValue( "spawnflags", "0" )
    light:SetKeyValue( "rendercolor", "255 255 255 255" )
    light:SetKeyValue( "HorizontalGlowSize", "250" )
    light:SetKeyValue( "VerticalGlowSize", "250" )
    light:SetKeyValue( "MinDist", "800" )
    light:SetKeyValue( "MaxDist", "600" )
    light:Spawn()
    self.LightEntity = light
end

目前仅能通过该方法成功使用env_projectedtexture、light_spot或point_spotlight(记不清具体是哪个)。

解决方案

1. 替换实体类型:用env_sprite模拟联合军地雷灯光

联合军地雷的顶部灯光本质是无环境光照的精灵(sprite),env_lightglow本身设计用于光晕渲染,在SENT父实体下容易出现渲染异常,且默认会附带环境光照。用env_sprite能完美匹配需求:

function ENT:Initialize()
    self:SetModel( "models/Items/combine_rifle_ammo01.mdl" )
    self:PhysicsInit( SOLID_VPHYSICS )
    self:SetMoveType( MOVETYPE_VPHYSICS )
    self:PhysWake()
    self:SetIsActive( false )

    -- 创建模拟地雷灯光的精灵
    local lightSprite = ents.Create( "env_sprite" )
    lightSprite:SetLocalPos( Vector( 0, 0, 35 ) ) -- 用局部坐标绑定父实体
    lightSprite:SetParent( self )
    lightSprite:SetKeyValue( "model", "sprites/light_glow02_add.vmt" ) -- 地雷同款发光材质
    lightSprite:SetKeyValue( "scale", "0.2" ) -- 调整大小匹配地雷灯光
    lightSprite:SetKeyValue( "rendermode", "5" ) -- 加性渲染,不照亮环境
    lightSprite:SetKeyValue( "rendercolor", "255 0 0 255" ) -- 红色,和地雷灯光一致
    lightSprite:Spawn()
    self.LightEntity = lightSprite

    -- 添加闪烁逻辑
    timer.Create( "MotionSensorLightBlink_" .. self:EntIndex(), 0.5, 0, function()
        if not IsValid( self ) or not IsValid( lightSprite ) then 
            timer.Destroy( "MotionSensorLightBlink_" .. self:EntIndex() ) 
            return 
        end
        local currentAlpha = tonumber(lightSprite:GetKeyValue( "alpha" )) or 255
        lightSprite:SetKeyValue( "alpha", currentAlpha == 255 and "100" or "255" )
    end )
end

2. 修复env_lightglow的键值错误(若坚持使用该实体)

你的代码存在两个关键错误:

  • MinDist大于MaxDist,导致光晕无法显示
  • 未设置spawnflags禁用环境光照

修复后的代码:

function ENT:Initialize()
    self:SetModel( "models/Items/combine_rifle_ammo01.mdl" )
    self:PhysicsInit( SOLID_VPHYSICS )
    self:SetMoveType( MOVETYPE_VPHYSICS )
    self:PhysWake()
    self:SetIsActive( false )

    local light = ents.Create( "env_lightglow" )
    light:SetLocalPos( Vector( 0, 0, 35 ) )
    light:SetParent( self )
    light:SetName( "MotionSensorLight" )
    light:SetKeyValue( "spawnflags", "1" ) -- 禁用环境光照
    light:SetKeyValue( "rendercolor", "255 255 255 255" )
    light:SetKeyValue( "HorizontalGlowSize", "250" )
    light:SetKeyValue( "VerticalGlowSize", "250" )
    light:SetKeyValue( "MinDist", "600" ) -- 正确顺序:Min < Max
    light:SetKeyValue( "MaxDist", "800" )
    light:Spawn()
    self.LightEntity = light

    -- 添加闪烁定时器
    timer.Create( "LightGlowBlink_" .. self:EntIndex(), 0.5, 0, function()
        if not IsValid( self ) or not IsValid( light ) then 
            timer.Destroy( "LightGlowBlink_" .. self:EntIndex() ) 
            return 
        end
        local colorParts = light:GetKeyValue( "rendercolor" ):Split( " " )
        local currentAlpha = tonumber(colorParts[4]) or 255
        local newAlpha = currentAlpha == 255 and "100" or "255"
        light:SetKeyValue( "rendercolor", string.format( "%s %s %s %s", colorParts[1], colorParts[2], colorParts[3], newAlpha ) )
    end )
end

3. 关键注意事项

  • 优先用SetLocalPos绑定父实体,避免父实体移动时子实体位置偏移
  • 加性渲染模式(rendermode 5)是确保灯光不照亮环境的核心
  • 定时器必须绑定实体索引,避免多个SENT互相干扰,且要在实体失效时销毁定时器

内容的提问来源于stack exchange,提问作者giorgio_developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 23:30:50