--[[--/* SlicedGyroidFlyThough.fuse Based on https://www.shadertoy.com/view/dssXzN a WebGL shader created by byt3_m3chanic. Converted to DCTL and embeddet into a Lua Fuse by JiPi (https://www.youtube.com/c/JiPi_YT). Place this file in your Fusion's and/or DaVinci Resolve's 'Fuses/' folder to use it. */--]]-- -- /* local ShaderFuse = require("Shaderfuse/ShaderFuse") ShaderFuse.init() -- // ------------------------------------------------------------------------ -- // Registry declaration -- // ------------------------------------------------------------------------ FuRegisterClass(ShaderFuse.FuRegister.Name, CT_SourceTool, { ShaderFuse.FuRegister.Attributes, REG_NoObjMatCtrls = true, REG_NoMotionBlurCtrls = true, REG_Source_GlobalCtrls = false, REG_Source_SizeCtrls = true, REG_Source_AspectCtrls = true, REG_Source_DepthCtrls = true, REG_OpNoMask = true, REG_TimeVariant = true, }) -- // ------------------------------------------------------------------------ -- // DCTL kernel parameters -- // ------------------------------------------------------------------------ -- */ ShaderParameters = [[ float iResolution[2]; float iTime; float iMouse[4]; float ColFog1[4]; float ColFog2[4]; float ViewDXY[2]; float ViewDZ; float ViewXY[2]; float ViewZ; float ofx; int width,height; int compOrder; ]] -- /* -- // ------------------------------------------------------------------------ -- DCTL kernel compatibility code -- // ------------------------------------------------------------------------ -- */ ShaderCompatibilityCode = [[ #if defined(DEVICE_IS_METAL) #define in #define out thread #define inout thread #else #define in #define out #define inout #endif #undef USE_NATIVE_METAL_IMPL #undef USE_NATIVE_CUDA_IMPL #undef USE_NATIVE_OPENCL_IMPL // 0 to use the generic implementations; 1 for Metal, OpenCL, Cuda specific code if existing #if 1 #if defined(DEVICE_IS_METAL) #define USE_NATIVE_METAL_IMPL 1 #elif defined(DEVICE_IS_CUDA) #define USE_NATIVE_CUDA_IMPL 1 #elif defined(DEVICE_IS_OPENCL) #define USE_NATIVE_OPENCL_IMPL 1 #endif #endif #if defined(USE_NATIVE_METAL_IMPL) #define swi2(A,a,b) (A).a##b #define swi3(A,a,b,c) (A).a##b##c #define swi2S(a,b,c,d) a.b##c = d #else #define swi2(A,a,b) to_float2((A).a,(A).b) #define swi3(A,a,b,c) to_float3((A).a,(A).b,(A).c) #define swi2S(a,b,c,d) {float2 tmp = d; (a).b = tmp.x; (a).c = tmp.y;} #endif // ---------------------------------------------------------------------------------------------------------- // mat2 implementation // ---------------------------------------------------------------------------------------------------------- #if defined(USE_NATIVE_METAL_IMPL) typedef float2x2 mat2; #define to_mat2(A,B,C,D) mat2((A),(B),(C),(D)) #define mul_f2_mat2(A,B) ((A)*(B)) #else typedef struct { float2 r0; float2 r1; } mat2; __DEVICE__ inline mat2 to_mat2 ( float a, float b, float c, float d) { mat2 t; t.r0.x = a; t.r0.y = b; t.r1.x = c; t.r1.y = d; return t; } __DEVICE__ inline float2 mul_f2_mat2( float2 v, mat2 m ) { float2 t; t.x = v.x*m.r0.x + v.y*m.r0.y; t.y = v.x*m.r1.x + v.y*m.r1.y; return t; } #endif // end of mat2 implementation #if defined(USE_NATIVE_METAL_IMPL) #define mod_f(a,b) fmod((a),(b)) #define sin_f3(i) sin(i) #define cos_f3(i) cos(i) #define pow_f3(a,b) pow(a,b) #else #if defined(USE_NATIVE_OPENCL_IMPL) #define reflect(I,N) (I-2.0f*dot(N,I)*N) #define fract(a) ((a)-_floor(a)) // oder Pointer bauen: gentype fract(gentype x, gentype *itpr) #define mod_f(a,b) _fmod(a,b) #define sin_f3(i) sin(i) #define cos_f3(i) cos(i) #define pow_f3(a,b) pow(a,b) #else // Generic #define fract(a) ((a)-_floor(a)) #define mod_f(a,b) ((a)-(b)*_floor((a)/(b))) #define sin_f3(i) to_float3( _sinf((i).x), _sinf((i).y), _sinf((i).z)) #define cos_f3(i) to_float3( _cosf((i).x), _cosf((i).y), _cosf((i).z)) #define pow_f3(a,b) to_float3(_powf((a).x,(b).x),_powf((a).y,(b).y),_powf((a).z,(b).z)) #endif #endif ]] -- /* -- // ------------------------------------------------------------------------ -- DCTL kernel implementation -- // ------------------------------------------------------------------------ -- */ ShaderKernelCode = [[ // ---------------------------------------------------------------------------------- // - Image - // ---------------------------------------------------------------------------------- /** License: Creative Commons Attribution-NonCommercial-ShareAlike 3.0f Unported License Sliced Gyroid Fly Though 11/16/22 | @byt3_m3chanic music for mood only - Greenhouse Gasses | by Cevin Key Original post here about sliced SDF's. Kind of updated version (extra loops to help backside of sdf's) @smjtyazdi https://twitter.com/smjtyazdi/status/1484828390104485896 */ #define R iResolution #define M iMouse #define T iTime #define PI 3.1415926f __DEVICE__ mat2 rot (float a) { return to_mat2(_cosf(a),_sinf(a),-_sinf(a),_cosf(a)); } __DEVICE__ float hash21(float2 a){ return fract(_sinf(dot(a,to_float2(22.34f,35.34f)))*483434.0f);} #ifdef GLOBALS //globals mat2 trot; float glow=0.0f,tglow=0.0f; const float ofx = 1.25f; #endif __DEVICE__ float sdGry(float3 p, float s, float t, float b, float ofx, inout float *glow, inout float *tglow) { p *= s; return _fabs(dot(sin_f3(p*ofx),cos_f3(swi3(p,z,x,y)))-b)/(s*ofx)-t; } __DEVICE__ float2 map(float3 p, mat2 trot, float iTime, float ofx, inout float *glow, inout float *tglow) { float2 res =to_float2(1e5f,1.0f); float3 q = p; float tt = 0.5f+0.5f*_sinf(T*0.2f); float k = 8.0f/dot(p,p); float mul = 1.0f/k; p*=k; swi2S(p,x,y, mul_f2_mat2(swi2(p,x,y) , trot)); swi2S(p,y,z, swi2(p,y,z) + to_float2(-1.25f, T)); float d = 0.365f, mf = 1e5; float mm = 0.25f+0.25f*_sinf(T*0.75f); for(float j=-2.0f;j<2.0f;j++){ float3 nf =p; nf.z=round(nf.z/d+j)*d; float ids = mod_f(nf.z,2.0f); float fd= sdGry(nf, 0.725f, 0.05f, 0.65f, ofx, glow, tglow); nf.z=clamp(p.z,nf.z-d/4.0f,nf.z+d/4.0f); fd=length(to_float2(_fmaxf(0.0f,fd), nf.z-p.z)); float idx = mod_f(nf.z,5.0f); if(idxmm) *tglow+=0.002f/(0.015f+fd*fd); if(idsmm) *glow+=0.002f/(0.015f+fd*fd); mf=_fminf(mf,fd); } if(mf= params->width || fusion_y >= params->height) return; float2 iResolution = to_float2(params->iResolution[0], params->iResolution[1]); float iTime = params->iTime; float4 iMouse = to_float4(params->iMouse[0],params->iMouse[1],params->iMouse[2],params->iMouse[3]); float4 O = to_float4_s(0.0f); float2 F = to_float2(fusion_x,fusion_y); float4 ColFog1 = to_float4(params->ColFog1[0], params->ColFog1[1], params->ColFog1[2], params->ColFog1[3]); float4 ColFog2 = to_float4(params->ColFog2[0], params->ColFog2[1], params->ColFog2[2], params->ColFog2[3]); float2 ViewDXY = to_float2(params->ViewDXY[0], params->ViewDXY[1]); float ViewDZ = params->ViewDZ; float2 ViewXY = to_float2(params->ViewXY[0], params->ViewXY[1]); float ViewZ = params->ViewZ; float ofx = params->ofx; // -------- //globals float glow=0.0f,tglow=0.0f; mat2 trot = rot(T*0.072f); float2 uv = (2.0f* swi2(F,x,y)-swi2(R,x,y))/_fmaxf(R.x,R.y); float3 ro = to_float3(0,0,0.5f) + to_float3_aw(ViewXY, ViewZ); float3 rd = normalize(to_float3_aw(uv, -1) + to_float3_aw(ViewDXY, ViewDZ)); float x = (M.x == 0.0f && M.y == 0.0f) || M.z < 0.0f ? 0.0f : -(M.y/R.y * 0.5f - 0.25f) * PI; float y = (M.x == 0.0f && M.y == 0.0f) || M.z < 0.0f ? 0.0f : -(M.x/R.x * 2.0f - 1.0f) * PI; mat2 rx = rot(x), ry = rot(y); swi2S(ro,y,z, mul_f2_mat2(swi2(ro,y,z) , rx)); swi2S(ro,x,z, mul_f2_mat2(swi2(ro,x,z) , ry)); swi2S(rd,y,z, mul_f2_mat2(swi2(rd,y,z) , rx)); swi2S(rd,x,z, mul_f2_mat2(swi2(rd,x,z) , ry)); float3 C = to_float3_s(0); float3 p = ro; float d=0.0f,m=0.0f; for(int i=0;i<164;i++){ float2 t = map(p, trot, iTime, ofx, &glow, &tglow); d += i<64? t.x*0.35f:t.x*0.75f; m = t.y; p = ro + rd * d; if(t.x45.0f) break; } float sp = 0.2f+0.2f*_sinf(uv.x*4.1f+T); // float3 fog = _mix(to_float3(0.043f,0.153f,0.255f),to_float3(0.235f,0.302f,0.000f),clamp((uv.y+0.5f-sp),0.0f,1.0f)); float3 fog = _mix(swi3(ColFog1,x,y,z),swi3(ColFog2,x,y,z),clamp((uv.y+0.5f-sp),0.0f,1.0f)); C = _mix(C,to_float3(glow,glow*0.65f,glow*0.15f),clamp(glow,0.0f,0.6f)); C = _mix(C,to_float3(tglow*0.15f,tglow*0.465f,tglow),clamp(tglow,0.0f,0.8f)); C = _mix(C,fog, 1.0f-_expf(-20.0f*d*d*d)); C = clamp(C,to_float3_s(0),to_float3_s(1)); O = to_float4_aw(pow_f3(C, to_float3_s(0.4545f)),1); _tex2DVec4Write(destinationTexture, fusion_x, fusion_y, O); } ]] -- /* -- // ------------------------------------------------------------------------ -- // Create -- // ------------------------------------------------------------------------ function Create() ShaderFuse.begin_create() ----- Inspector Panel Controls -- Speed Slider InFrequency = self:AddInput("Speedup", "speed", { LINKID_DataType = "Number", INPID_InputControl = "SliderControl", INP_Default = 1.0, INP_MinScale = 0.0, INP_MaxScale = 5.0, SLCS_LowName = "stop", SLCS_HighName = "5x", }) -- iMouse Controls InMouseXY = self:AddInput("iMouse.xy", "iMouseXY", { LINKID_DataType = "Point", INPID_InputControl = "OffsetControl", INP_DoNotifyChanged = false, --INP_Passive = true, INPID_PreviewControl = "CrosshairControl", }) InMouseZW = self:AddInput("iMouse.zw", "iMouseZW", { LINKID_DataType = "Point", INPID_InputControl = "OffsetControl", INP_DoNotifyChanged = false, --INP_Passive = true, INPID_PreviewControl = "CrosshairControl", INP_Disabled = true, }) InMouseDrag = self:AddInput("Mouse Button Pressed", "iMouseClick", { LINKID_DataType = "Number", INPID_InputControl = "CheckboxControl", INP_DoNotifyChanged = false, --INP_Passive = true, INP_MinScale = 0, INP_MaxScale = 1, INP_Default = 0, }) self:BeginControlNest("ColFog1", "ColFog1", true, {}) ctrl_grp_cnt = (ctrl_grp_cnt==nil) and 1 or (ctrl_grp_cnt+1) attrs = { ICS_Name = "ColFog1", LINKID_DataType = "Number", INPID_InputControl = "ColorControl", INP_MinScale = 0.0, INP_MaxScale = 1.0, IC_ControlGroup = ctrl_grp_cnt, } InColFog1ColorR = self:AddInput("Red", "ColFog1Red", { INP_Default = 0.043, IC_ControlID = 0, attrs}) InColFog1ColorG = self:AddInput("Green", "ColFog1Green", { INP_Default = 0.153, IC_ControlID = 1, attrs}) InColFog1ColorB = self:AddInput("Blue", "ColFog1Blue", { INP_Default = 0.255, IC_ControlID = 2, attrs}) InColFog1ColorA = self:AddInput("Alpha", "ColFog1Alpha", { INP_Default = 1.0, IC_ControlID = 3, attrs}) self:EndControlNest() self:BeginControlNest("ColFog2", "ColFog2", true, {}) ctrl_grp_cnt = (ctrl_grp_cnt==nil) and 1 or (ctrl_grp_cnt+1) attrs = { ICS_Name = "ColFog2", LINKID_DataType = "Number", INPID_InputControl = "ColorControl", INP_MinScale = 0.0, INP_MaxScale = 1.0, IC_ControlGroup = ctrl_grp_cnt, } InColFog2ColorR = self:AddInput("Red", "ColFog2Red", { INP_Default = 0.235, IC_ControlID = 0, attrs}) InColFog2ColorG = self:AddInput("Green", "ColFog2Green", { INP_Default = 0.302, IC_ControlID = 1, attrs}) InColFog2ColorB = self:AddInput("Blue", "ColFog2Blue", { INP_Default = 0.000, IC_ControlID = 2, attrs}) InColFog2ColorA = self:AddInput("Alpha", "ColFog2Alpha", { INP_Default = 1.0, IC_ControlID = 3, attrs}) self:EndControlNest() InViewDXYPoint = self:AddInput("ViewDXY", "ViewDXY", { LINKID_DataType = "Point", INPID_InputControl = "OffsetControl", INPID_PreviewControl = "CrosshairControl", INP_DefaultX = 0.0, INP_DefaultY = 0.0, }) InViewDZSlider = self:AddInput("ViewDZ", "ViewDZ", { LINKID_DataType = "Number", INPID_InputControl = "SliderControl", INP_MinScale = -10.0, INP_MaxScale = 10.0, INP_Default = 0.0, }) InViewXYPoint = self:AddInput("ViewXY", "ViewXY", { LINKID_DataType = "Point", INPID_InputControl = "OffsetControl", INPID_PreviewControl = "CrosshairControl", INP_DefaultX = 0.0, INP_DefaultY = 0.0, }) InViewZSlider = self:AddInput("ViewZ", "ViewZ", { LINKID_DataType = "Number", INPID_InputControl = "SliderControl", INP_MinScale = -10.0, INP_MaxScale = 10.0, INP_Default = 0.0, }) InofxSlider = self:AddInput("ofx", "ofx", { LINKID_DataType = "Number", INPID_InputControl = "SliderControl", INP_MinScale = -1.0, INP_MaxScale = 1.0, INP_Default = 1.25, }) Sep3 = self:AddInput(string.rep("_", 152), "Separator3", { LINKID_DataType = "Text", INPID_InputControl = "LabelControl", INP_External = false, INP_Passive = true, IC_Visible = true, INP_DoNotifyChanged = true, IC_NoLabel = true, }) ----- Size & Depth InSize = self:AddInput("Size", "Size_Fuse", { LINKID_DataType = "Number", INPID_InputControl = "ComboControl", INP_DoNotifyChanged = true, INP_Default = 0, INP_Integer = true, ICD_Width = 1, { CCS_AddString = "Default", }, { CCS_AddString = "Manually", }, { CCS_AddString = "Image0", }, { CCS_AddString = "1920x1080", }, { CCS_AddString = "1200x675", }, { CCS_AddString = "800x450", }, { CCS_AddString = "640x360", }, CC_LabelPosition = "Horizontal", ICS_ControlPage = "Image", }) InWidth = self:AddInput("Width", "_Width", { LINKID_DataType = "Number", INPID_InputControl = "SliderControl", INP_Default = 1920, INP_Integer = true, INP_MinScale = 0, INP_MaxScale = 4096, }) InHeight = self:AddInput("Height", "_Height", { LINKID_DataType = "Number", INPID_InputControl = "SliderControl", INP_Default = 1080, INP_Integer = true, INP_MinScale = 0, INP_MaxScale = 4096, }) InDepth = self:AddInput("Depth_Fuse", "Depth_Fuse", { LINKID_DataType = "Number", INPID_InputControl = "ComboControl", INP_DoNotifyChanged = true, INP_Default = 0, INP_Integer = true, ICD_Width = 1, { CCS_AddString = "Default", }, { CCS_AddString = "int8", }, { CCS_AddString = "int16", }, { CCS_AddString = "float16", }, { CCS_AddString = "float32", }, CC_LabelPosition = "Horizontal", ICS_ControlPage = "Image", }) InMyWidth = self:FindInput("Width") InMyWidth:SetAttrs({ IC_Visible = false }) InMyHeight = self:FindInput("Height") InMyHeight:SetAttrs({ IC_Visible = false }) InMyDepth = self:FindInput("Depth") InMyDepth:SetAttrs({ IC_Visible = false }) ----- In/Out OutImage = self:AddOutput("Output", "Output", { LINKID_DataType = "Image", LINK_Main = 1, }) ShaderFuse.end_create() end -- // ------------------------------------------------------------------------ -- // Process -- // ------------------------------------------------------------------------ function Process(req) -- Imagesize and Depth if (InSize:GetValue(req).Value >= 1) then if (InSize:GetValue(req).Value == 2) then if (InChannel0:GetValue(req) ~= nil) then Width = InChannel0:GetValue(req).Width Height = InChannel0:GetValue(req).Height end else Width = InWidth:GetValue(req).Value Height = InHeight:GetValue(req).Value end end -- Alle ( int und float ) if (InDepth:GetValue(req).Value > 0) then if InDepth:GetValue(req).Value == 1 then SourceDepth = 5 else if InDepth:GetValue(req).Value == 2 then SourceDepth = 6 else if InDepth:GetValue(req).Value == 3 then SourceDepth = 7 else SourceDepth = 8 end end end end local imgattrs = { IMG_Document = self.Comp, { IMG_Channel = "Red", }, { IMG_Channel = "Green", }, { IMG_Channel = "Blue", }, { IMG_Channel = "Alpha", }, IMG_Width = Width, IMG_Height = Height, IMG_XScale = XAspect, IMG_YScale = YAspect, IMAT_OriginalWidth = realwidth, -- nil !?! IMAT_OriginalHeight = realheight, -- nil !?! IMG_Quality = not req:IsQuick(), IMG_MotionBlurQuality = not req:IsNoMotionBlur(), IMG_DeferAlloc = true, IMG_ProxyScale = ( (not req:IsStampOnly()) and 1 or nil), IMG_Depth = ( (SourceDepth~=0) and SourceDepth or nil ) } local dst = Image(imgattrs) local black = Pixel({R=0,G=0,B=0,A=0}) dst:Fill(black) if req:IsPreCalc() then local out = Image({IMG_Like = dst, IMG_NoData = true}) OutImage:Set(req, out) return end node = DVIPComputeNode(req, "SlicedGyroidFlyThoughFuse", ShaderCompatibilityCode..ShaderKernelCode, "Params", ShaderParameters ) -- Extern texture or create a new one -- DCTL parameters local framerate = self.Comp:GetPrefs("Comp.FrameFormat.Rate") local params = {} params = node:GetParamBlock(ShaderParameters) params.iResolution[0] = dst.Width params.iResolution[1] = dst.Height params.iTime = (req.Time / framerate) * InFrequency:GetValue(req).Value -- iMouse local mouse_xy = InMouseXY:GetValue(req) local mouse_zw = InMouseZW:GetValue(req) params.iMouse[0] = mouse_xy.X params.iMouse[1] = mouse_xy.Y params.iMouse[2] = mouse_zw.X params.iMouse[3] = mouse_zw.Y if InMouseDrag:GetValue(req).Value ~= 0 then if params.iMouse[2]==-1 and params.iMouse[3]==-1 then params.iMouse[2]=params.iMouse[0] params.iMouse[3]=params.iMouse[1] end else params.iMouse[2] = -1 params.iMouse[3] = -1 end if mouse_zw.X ~= params.iMouse[2] or mouse_zw.Y ~= params.iMouse[3] then InMouseZW:SetAttrs({INP_Disabled=false}) InMouseZW:SetSource(Point(params.iMouse[2],params.iMouse[3]),0,0) InMouseZW:SetAttrs({INP_Disabled=true}) end params.iMouse[0] = params.iMouse[0] * Width params.iMouse[1] = params.iMouse[1] * Height if params.iMouse[2] == -1 and params.iMouse[3] == -1 then params.iMouse[2] = 0 params.iMouse[3] = 0 else params.iMouse[2] = params.iMouse[2] * Width params.iMouse[3] = params.iMouse[3] * Height end params.ColFog1 = { InColFog1ColorR:GetValue(req).Value, InColFog1ColorG:GetValue(req).Value, InColFog1ColorB:GetValue(req).Value,InColFog1ColorA:GetValue(req).Value } params.ColFog2 = { InColFog2ColorR:GetValue(req).Value, InColFog2ColorG:GetValue(req).Value, InColFog2ColorB:GetValue(req).Value,InColFog2ColorA:GetValue(req).Value } params.ViewDXY = {InViewDXYPoint:GetValue(req).X,InViewDXYPoint:GetValue(req).Y} params.ViewDZ = InViewDZSlider:GetValue(req).Value params.ViewXY = {InViewXYPoint:GetValue(req).X,InViewXYPoint:GetValue(req).Y} params.ViewZ = InViewZSlider:GetValue(req).Value params.ofx = InofxSlider:GetValue(req).Value -- Resolution params.width = dst.Width params.height = dst.Height -- Per channel time and resolution node:SetParamBlock(params) node:AddSampler("RowSampler", TEX_FILTER_MODE_LINEAR,TEX_ADDRESS_MODE_MIRROR, TEX_NORMALIZED_COORDS_TRUE) node:AddOutput("dst", dst) local ok = node:RunSession(req) if (not ok) then dst = nil dump(node:GetErrorLog()) end OutImage:Set(req,dst) collectgarbage(); end -- // ------------------------------------------------------------------------ -- // Callback -- // ------------------------------------------------------------------------ function NotifyChanged(inp, param, time) if (param ~= nil) then if inp == InSize then if param.Value == 1 then InWidth:SetAttrs({ IC_Visible = true }) InHeight:SetAttrs({ IC_Visible = true }) else InWidth:SetAttrs({ IC_Visible = false }) InHeight:SetAttrs({ IC_Visible = false }) end if param.Value == 3 then --1920x1080 InWidth:SetSource(Number(1920),0,0) InHeight:SetSource(Number(1080),0,0) end if param.Value == 4 then --1200x675 InWidth:SetSource(Number(1200),0,0) InHeight:SetSource(Number(675),0,0) end if param.Value == 5 then --800x450 InWidth:SetSource(Number(800),0,0) InHeight:SetSource(Number(450),0,0) end if param.Value == 6 then --640x360 InWidth:SetSource(Number(640),0,0) InHeight:SetSource(Number(360),0,0) end end end end -- */