--!strict -- Cosmetic skin for the camera viewmodel and the equipped world rifle. local Variants = require(script.Parent:WaitForChild("RifleSkinVariants") :: ModuleScript) local RifleSkin = {} type Palette = {cool: Color3, hot: Color3, phase: number, reflectance: number} local bodyPalette: {[string]: Palette} = { Rifle_TitaniumPanels = { cool = Color3.fromRGB(185, 203, 246), hot = Color3.fromRGB(242, 253, 255), phase = .82, reflectance = .13, }, Rifle_CeramicGraphite = { cool = Color3.fromRGB(43, 73, 226), hot = Color3.fromRGB(74, 133, 251), phase = 0, reflectance = .14, }, Rifle_Rubber = { cool = Color3.fromRGB(41, 71, 168), hot = Color3.fromRGB(76, 125, 218), phase = 1.7, reflectance = .035, }, } local channelPalette: {[string]: Palette} = { Rifle_CyanChannel = { cool = Color3.fromRGB(26, 150, 247), hot = Color3.fromRGB(103, 244, 255), phase = .25, reflectance = 0, }, Rifle_AmberChannel = { cool = Color3.fromRGB(51, 131, 255), hot = Color3.fromRGB(156, 247, 255), phase = 1.15, reflectance = 0, }, } local paletteSlots: {[string]: number} = { Rifle_TitaniumPanels = 1, Rifle_CeramicGraphite = 2, Rifle_Rubber = 3, Rifle_CyanChannel = 4, Rifle_AmberChannel = 5, } local flameColor = ColorSequence.new({ ColorSequenceKeypoint.new(0, Color3.fromRGB(210, 252, 255)), ColorSequenceKeypoint.new(.28, Color3.fromRGB(53, 203, 255)), ColorSequenceKeypoint.new(.72, Color3.fromRGB(22, 112, 255)), ColorSequenceKeypoint.new(1, Color3.fromRGB(34, 70, 230)), }) local flameSize = NumberSequence.new({ NumberSequenceKeypoint.new(0, .29, .06), NumberSequenceKeypoint.new(.37, .52, .11), NumberSequenceKeypoint.new(1, .22, .08), }) local flameOpacity = NumberSequence.new({ NumberSequenceKeypoint.new(0, .57), NumberSequenceKeypoint.new(.18, .44, .08), NumberSequenceKeypoint.new(.6, .61, .12), NumberSequenceKeypoint.new(1, 1), }) local locations = { Vector3.new(.26, .61, -.04), Vector3.new(.28, .62, -1.00), Vector3.new(-.27, .62, -.65), Vector3.new(.24, .61, -2.17), Vector3.new(-.22, .64, -2.63), Vector3.new(.12, .64, -3.40), } type Layer = {emitter: ParticleEmitter, rate: number, focusedRate: number} type Panel = {part: BasePart, palette: Palette} type Tongue = {base: Vector3, tip: Attachment, core: Beam, halo: Beam, phase: number} type Stream = {start: Attachment, finish: Attachment, core: Beam, halo: Beam, side: number, phase: number} export type Effect = { grip: BasePart, panels: {Panel}, anchors: {Attachment}, layers: {Layer}, tongues: {Tongue}, streams: {Stream}, variant: Variants.Effect?, light: PointLight, mode: "Hidden" | "Full" | "Focused", nextFrame: number, flashUntil: number, } local tongueCore = ColorSequence.new({ ColorSequenceKeypoint.new(0, Color3.fromRGB(197, 251, 255)), ColorSequenceKeypoint.new(.36, Color3.fromRGB(47, 220, 255)), ColorSequenceKeypoint.new(1, Color3.fromRGB(43, 124, 255)), }) local tongueHalo = ColorSequence.new(Color3.fromRGB(28, 163, 255), Color3.fromRGB(28, 90, 238)) local streamCore = ColorSequence.new({ ColorSequenceKeypoint.new(0, Color3.fromRGB(32, 123, 255)), ColorSequenceKeypoint.new(.55, Color3.fromRGB(109, 237, 255)), ColorSequenceKeypoint.new(1, Color3.fromRGB(211, 255, 255)), }) local function ribbon(base: Attachment, tip: Attachment, name: string, color: ColorSequence, transparency: NumberSequence): Beam local beam = Instance.new("Beam") beam.Name = name beam.Attachment0 = base beam.Attachment1 = tip beam.FaceCamera = true beam.Segments = 14 beam.LightEmission = 1 beam.Color = color beam.Transparency = transparency beam.Width0 = 0 beam.Width1 = 0 beam.Enabled = false beam.Parent = base return beam end local function fadeTip(transparency: number): NumberSequence return NumberSequence.new({ NumberSequenceKeypoint.new(0, transparency), NumberSequenceKeypoint.new(.44, transparency), NumberSequenceKeypoint.new(1, 1), }) end local streamFade = NumberSequence.new({ NumberSequenceKeypoint.new(0, 1), NumberSequenceKeypoint.new(.32, .45), NumberSequenceKeypoint.new(.74, .12), NumberSequenceKeypoint.new(1, 1), }) local haloFade = NumberSequence.new({ NumberSequenceKeypoint.new(0, 1), NumberSequenceKeypoint.new(.45, .81), NumberSequenceKeypoint.new(.76, .57), NumberSequenceKeypoint.new(1, 1), }) local function streamAnchor(grip: BasePart, name: string): Attachment local anchor = Instance.new("Attachment") anchor.Name = name anchor.Axis = Vector3.zAxis anchor.Parent = grip return anchor end local function streamPoint(side: number, position: number, phase: number): Vector3 local wave = math.sin(position * 15 + phase) local z = -3.7 + position * 4.32 return Vector3.new(side * (.32 + .033 * wave), .44 + .12 * math.sin(position * 9 + phase), z) end local function channel(anchor: Attachment, name: string, texture: string): ParticleEmitter local emitter = Instance.new("ParticleEmitter") emitter.Name = name emitter.Texture = texture emitter.LightEmission = 1 emitter.Orientation = Enum.ParticleOrientation.FacingCamera emitter.EmissionDirection = Enum.NormalId.Right -- attachment X axis points up from the receiver emitter.LockedToPart = true emitter.Enabled = false emitter.Parent = anchor return emitter end function RifleSkin.setMode(effect: Effect?, mode: "Hidden" | "Full" | "Focused") if not effect or effect.mode == mode then return end effect.mode = mode for _, layer in effect.layers do local rate = if mode == "Full" then layer.rate elseif mode == "Focused" then layer.focusedRate else 0 local emitter = layer.emitter emitter.Rate = rate emitter.Enabled = rate > 0 if rate == 0 then emitter:Clear() end end for _, tongue in effect.tongues do tongue.core.Enabled = mode == "Full" tongue.halo.Enabled = mode == "Full" end for _, stream in effect.streams do stream.core.Enabled = mode == "Full" stream.halo.Enabled = mode == "Full" end if effect.variant then Variants.setVisible(effect.variant, mode == "Full") end effect.light.Enabled = mode ~= "Hidden" effect.light.Brightness = if mode == "Focused" then .18 else .65 end local FLASH_TIME = .23 function RifleSkin.pulse(effect: Effect?) if effect then effect.flashUntil = os.clock() + FLASH_TIME end end function RifleSkin.animate(effect: Effect?, now: number) if not effect or effect.mode == "Hidden" or now < effect.nextFrame then return end effect.nextFrame = now + 1 / 50 local camera = workspace.CurrentCamera local grazing = 0 if camera then local ray = camera.CFrame.Position - effect.grip.Position if ray.Magnitude > .01 then grazing = (1 - math.abs(ray.Unit:Dot(effect.grip.CFrame.RightVector))) ^ 2 end end local flash = math.clamp((effect.flashUntil - now) / FLASH_TIME, 0, 1) for _, panel in effect.panels do local colors = panel.palette local t = now * 2.8 + colors.phase local heat = .42 + .31 * math.sin(t) + .08 * math.sin(t * 2.3) + .16 * grazing + .18 * flash panel.part.Color = colors.cool:Lerp(colors.hot, math.clamp(heat, 0, 1)) end if effect.mode ~= "Full" then return end effect.light.Brightness = .52 + .12 * math.sin(now * 12) + .48 * flash if effect.variant then effect.variant.animate(now, flash) return end for _, tongue in effect.tongues do local t = now * 15 + tongue.phase local sway = math.sin(t) local heat = .58 + .26 * math.sin(t * .91) + .16 * math.sin(t * 1.83) local rise = .09 + .30 * heat + .10 * flash local sweep = .20 + .44 * heat + .19 * flash tongue.tip.Position = tongue.base + Vector3.new(.11 * sway, rise, sweep) local width = .03 + .115 * heat + .055 * flash tongue.core.Width0 = width tongue.halo.Width0 = width * 1.95 tongue.core.CurveSize0 = rise * .93 tongue.halo.CurveSize0 = tongue.core.CurveSize0 tongue.core.CurveSize1 = .20 + .13 * sway tongue.halo.CurveSize1 = tongue.core.CurveSize1 end for _, stream in effect.streams do local head = (now * .9 + stream.phase) % 1 local tail = math.max(0, head - .34) local strength = math.sin(head * math.pi) ^ .7 local ripple = now * 6 + stream.phase * 5 stream.start.Position = streamPoint(stream.side, tail, ripple) stream.finish.Position = streamPoint(stream.side, head, ripple) local width = strength * (.16 + .10 * flash) stream.core.Width0 = width * .45 stream.core.Width1 = width stream.halo.Width0 = width stream.halo.Width1 = width * 2.45 end end function RifleSkin.apply(model: Instance, grip: BasePart, id: string, accent: Color3): Effect local mesh = model:FindFirstChild("RifleMesh") or model local theme = Variants.palettes[id] local panels: {Panel} = {} for _, child in mesh:GetChildren() do if not child:IsA("BasePart") then continue end local colors = bodyPalette[child.Name] if colors then -- The stock PBR map is nearly black and absorbs the blue tint. local surface = child:FindFirstChildOfClass("SurfaceAppearance") if surface then surface:Destroy() end child.Material = Enum.Material.SmoothPlastic child.Reflectance = colors.reflectance else colors = channelPalette[child.Name] end if colors then local alternate = theme and theme[paletteSlots[child.Name]] if alternate then colors = { cool = alternate.cool, hot = alternate.hot, phase = colors.phase, reflectance = colors.reflectance, } end child.Color = colors.cool:Lerp(colors.hot, .42) table.insert(panels, {part = child, palette = colors}) end end local anchors: {Attachment} = {} local layers: {Layer} = {} local tongues: {Tongue} = {} local streams: {Stream} = {} local light = Instance.new("PointLight") light.Name = "FlameGlow" light.Color = if theme then accent else Color3.fromRGB(47, 170, 255) light.Range = 3.4 light.Enabled = false local effect: Effect = { grip = grip, panels = panels, anchors = anchors, layers = layers, tongues = tongues, streams = streams, light = light, mode = "Hidden", nextFrame = 0, flashUntil = 0, } if theme then local variant = Variants.create(id, grip) light.Parent = variant.root effect.variant = variant return effect end for index, position in locations do local anchor = Instance.new("Attachment") anchor.Name = "BlueFlame" .. index anchor.Position = position anchor.Parent = grip table.insert(anchors, anchor) local flame = channel(anchor, "Flame", "rbxasset://textures/particles/fire_main.dds") flame.Color = flameColor flame.Size = flameSize flame.Transparency = flameOpacity flame.Lifetime = NumberRange.new(.24, .48) flame.Speed = NumberRange.new(.72, 1.8) flame.SpreadAngle = Vector2.new(22, 22) flame.Rotation = NumberRange.new(-25, 25) flame.RotSpeed = NumberRange.new(-65, 65) flame.Brightness = 2 table.insert(layers, {emitter = flame, rate = 12, focusedRate = if index == 6 then 4 else 0}) -- The face-camera ribbons give the softer particles a distinct flame shape. anchor.Axis = Vector3.new(0, .78, .63).Unit local tip = Instance.new("Attachment") tip.Name = "BlueFlameTip" .. index tip.Position = position + Vector3.new(0, .45, .25) tip.Axis = Vector3.new(0, .2, 1).Unit tip.Parent = grip table.insert(tongues, { base = position, tip = tip, phase = index * 3.43, core = ribbon(anchor, tip, "FlameCore", tongueCore, fadeTip(.22)), halo = ribbon(anchor, tip, "FlameHalo", tongueHalo, fadeTip(.64)), }) end -- Two staggered pulses per side follow the casing from muzzle to receiver. for _, side in {-1, 1} do for lane = 1, 2 do local start = streamAnchor(grip, "BlueFlameStream") local finish = streamAnchor(grip, "BlueFlameStreamEnd") table.insert(anchors, start) table.insert(anchors, finish) local core = ribbon(start, finish, "StreamCore", streamCore, streamFade) local halo = ribbon(start, finish, "StreamHalo", tongueHalo, haloFade) core.CurveSize0, core.CurveSize1 = .22, .18 halo.CurveSize0, halo.CurveSize1 = .22, .18 table.insert(streams, { start = start, finish = finish, core = core, halo = halo, side = side, phase = (lane - 1) * .5 + (if side == 1 then .18 else 0), }) if lane == 1 then local glint = channel(finish, "StreamGlint", "rbxasset://textures/particles/sparkles_main.dds") glint.Brightness = 2.4 glint.Color = ColorSequence.new(Color3.fromRGB(191, 252, 255), Color3.fromRGB(47, 139, 252)) glint.Size = NumberSequence.new({ NumberSequenceKeypoint.new(0, .24), NumberSequenceKeypoint.new(.3, .42), NumberSequenceKeypoint.new(1, .12), }) glint.Transparency = NumberSequence.new(.2, 1) glint.Lifetime = NumberRange.new(.11, .18) glint.Speed = NumberRange.new(.06, .18) glint.Rotation = NumberRange.new(-40, 40) table.insert(layers, {emitter = glint, rate = 8, focusedRate = 0}) end end end for _, index in {2, 5} do local embers = channel(anchors[index], "Sparks", "rbxasset://textures/particles/sparkles_main.dds") embers.Color = ColorSequence.new(Color3.fromRGB(193, 246, 255), Color3.fromRGB(26, 144, 255)) embers.Size = NumberSequence.new(.075, .015) embers.Transparency = NumberSequence.new(.13, 1) embers.Lifetime = NumberRange.new(.28, .51) embers.Speed = NumberRange.new(1.65, 3.2) embers.SpreadAngle = Vector2.new(42, 42) embers.Rotation = NumberRange.new(0, 360) embers.RotSpeed = NumberRange.new(-80, 80) table.insert(layers, {emitter = embers, rate = 9, focusedRate = 0}) end light.Parent = anchors[4] return effect end function RifleSkin.destroy(effect: Effect?) if not effect then return end if effect.variant then effect.variant.root:Destroy() end for _, anchor in effect.anchors do anchor:Destroy() end for _, tongue in effect.tongues do tongue.tip:Destroy() end end return RifleSkin