--- a/rts/Game/WebbarBrowser.h +++ b/rts/Game/WebbarBrowser.h @@ -122,6 +122,7 @@ #include "WebbarNativeHealthbars.h" #include "WebbarNativeOrbs.h" #include "WebbarNativeDeferredLights.h" +#include "WebbarNativeLasers.h" #include "WebbarNativeEta.h" #include "WebbarNativeScum.h" #include "WebbarMetalRead.h" --- a/rts/Game/WebbarNativeBeams.h +++ b/rts/Game/WebbarNativeBeams.h @@ -364,6 +364,7 @@ static void PublishWebbarNativeScumDraw(int32_t sequence); static void PublishWebbarNativeOrbs(const WebbarBeamDrawRequest&, int32_t, uint32_t); static void PublishWebbarNativeDeferredLights(const WebbarBeamDrawRequest&, int32_t, uint32_t); +static void PublishWebbarNativeLasers(const WebbarBeamDrawRequest&, int32_t, uint32_t); static void UpdateWebbarNativeBeams() { PrepareWebbarNativeBeams(); @@ -389,6 +390,7 @@ if(request.distortions) PublishWebbarNativeDistortion(request,sequence); PublishWebbarNativeOrbs(request,sequence,webbarNativeBeams->epoch); PublishWebbarNativeDeferredLights(request,sequence,webbarNativeBeams->epoch); + PublishWebbarNativeLasers(request,sequence,webbarNativeBeams->epoch); if((request.flags & 2u) && request.frame == uint32_t(gs->frameNum)) PublishWebbarNativeEtaDraw(request.fraction,sequence,webbarNativeBeams->epoch); if((request.flags & 2u) && request.frame == uint32_t(gs->frameNum)) PublishWebbarNativeScumDraw(sequence); __atomic_store_n(&webbarBeamDrawMailbox.acknowledged,sequence,__ATOMIC_RELEASE); --- /dev/null +++ b/rts/Game/WebbarNativeLasers.h @@ -0,0 +1,40 @@ +/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ +#pragma once +#include +#include +#include +#include +#include +#include +#include "Sim/Projectiles/WeaponProjectiles/LaserProjectile.h" +#include "Sim/Projectiles/WeaponProjectiles/WeaponProjectileTypes.h" +// Optional WLP1: original CLaserProjectile draw state, coupled to the current +// native body draw and read perspective. It never changes bridge9/simulation. +static void PublishWebbarNativeLasers(const WebbarBeamDrawRequest& request,int32_t sequence,uint32_t epoch) +{ + if(!std::getenv("WEBBAR_NATIVE_LASER_VBO"))return; + WebbarNativeHealthbars::Prepare(); + std::vector packet{0x31504c57u,1,uint32_t(gs->frameNum),epoch,uint32_t(sequence),0,160,WebbarNativeHealthbars::epoch, + uint32_t(gu->myPlayerNum),uint32_t(gu->myTeam),uint32_t(gu->myAllyTeam),uint32_t(gu->spectating)|(uint32_t(gu->spectatingFullView)<<1),request.flags,0,0,0}; + const float time[]={float(gs->frameNum),spring_tomsecs(globalRendering->grTime)*.001f,(gs->GetLuaSimFrame()+request.fraction)/GAME_SPEED,request.fraction}; + for(float value:time){uint32_t word;std::memcpy(&word,&value,4);packet.push_back(word);} + bool ok=true; + for(const auto* p:projectileHandler.GetActiveProjectiles(true)){ + // Same read admission as the existing weapon channel. No hidden owner/target + // lookup and no derived access from ProjectileCreated's base constructor. + if(!p->weapon||p->deleteMe||!WebbarProjectileVisible(p)||p->GetProjectileType()!=WEAPON_LASER_PROJECTILE||p->model!=nullptr)continue; + const auto* laser=static_cast(p);const auto* definition=laser->GetWeaponDef(); + if(!definition)continue; + if(packet[5]>=65536){ok=false;break;} + std::array row{};row[0]=uint32_t(p->id);row[1]=uint32_t(definition->id);row[2]=uint32_t(p->checkCol)|(uint32_t(p->luaDraw)<<1); + float values[36];laser->GetWebbarLaserState(request.fraction,gs->frameNum,values); + // Original Draw emits nothing for a non-positive clamped visible length. + // The original (possibly hidden) start position never leaves this method. + if(values[3]<=0.0f)continue; + for(int i=0;i<36;++i){if(!std::isfinite(values[i])){ok=false;break;}std::memcpy(&row[4+i],&values[i],4);} + if(!ok)break; + packet.insert(packet.end(),row.begin(),row.end());++packet[5]; + } + if(!ok){MAIN_THREAD_EM_ASM({if(Module['webbarNativeLaserError'])Module['webbarNativeLaserError']('Original laser draw state unavailable or capacity exceeded');});return;} + MAIN_THREAD_EM_ASM({if(Module['webbarNativeLaserVBO'])Module['webbarNativeLaserVBO'](HEAPU8.slice($0,$0+$1).buffer);},packet.data(),packet.size()*4); +} --- a/rts/Sim/Projectiles/WeaponProjectiles/LaserProjectile.h +++ b/rts/Sim/Projectiles/WeaponProjectiles/LaserProjectile.h @@ -4,6 +4,10 @@ #define LASER_PROJECTILE_H #include "WeaponProjectile.h" +#if defined(__EMSCRIPTEN__) +#include "System/SpringMath.h" +#include +#endif class CLaserProjectile : public CWeaponProjectile { @@ -23,13 +27,68 @@ int GetProjectilesCount() const override; +#if defined(__EMSCRIPTEN__) + // Read-only original fully constructed CLaserProjectile draw state. No + // update, collision, RNG, membership, texture or simulation mutation. + void GetWebbarLaserState(float fraction, int frame, float* out) const { + const float3 draw=GetDrawPos(fraction); + const auto& v=weaponDef->visuals; + float3 clampedPrevDrawPos=draw-dir*curLength; + clampedPrevDrawPos=startPos+std::max(0.0f,dir.dot(clampedPrevDrawPos-startPos))*dir; + const float curDrawLen=dir.dot(draw-clampedPrevDrawPos); + const float3 tail=draw-dir*curDrawLen; + float a1=0,a2=0;WebbarLaserAnim(animParams1,a1,frame,fraction);WebbarLaserAnim(animParams2,a2,frame,fraction); + const float values[]={ + draw.x,draw.y,draw.z,curDrawLen, + dir.x,dir.y,dir.z,maxLength, + tail.x,tail.y,tail.z,speedf, + color.x,color.y,color.z,intensity, + color2.x,color2.y,color2.z,float(stayTime), + v.thickness,v.corethickness,float(v.lodDistance),GetDrawRadius(), + animParams1.x,animParams1.y,animParams1.z,a1, + animParams2.x,animParams2.y,animParams2.z,a2, + pos.x,pos.y,pos.z,float(drawOrder) + }; + static_assert(sizeof(values)==36*sizeof(float)); + for(int i=0;i<36;++i)out[i]=values[i]; + } +#endif + + private: void UpdateIntensity(); void UpdateLength(); void UpdatePos(const float4& oldSpeed); void CollisionCommon(const float3& oldPos); +#if defined(__EMSCRIPTEN__) private: +void WebbarLaserAnim(const float3& ap, float& p, int frame, float fraction) const +{ + if (static_cast(ap.x) <= 1 && static_cast(ap.y) <= 1) { + p = 0.0f; + return; + } + + const float t = (frame - createFrame + fraction); + const float animSpeed = math::fabs(ap.z); + if (ap.z < 0.0f) { + #if 0 + p = math::fmod(t, 2.0f * animSpeed) / animSpeed; + if (p > 1.0) + p = 2.0f - p; + #else + p = 1.0f - math::fabs(math::fmod(t, 2.0f * animSpeed) / animSpeed - 1.0f); + #endif + } + else { + p = math::fmod(t, animSpeed) / animSpeed; + } +} + +#endif + +private: float speedf; float maxLength;