Welcome to WSL!

Make yourself at home, but before posting, please may I ask you to read the following topics.


Posting 101
Server space, screenshots, and you

Thank you!

PS. please pretty please:


Image

Error when trying to use Z Buffer values in a Fuse

User avatar
Shem Namo
Fusionista
Posts: 540
Joined: Sun Oct 06, 2019 9:15 pm
Location: North Israel
Real name: David Kohen
Been thanked: 15 times

Error when trying to use Z Buffer values in a Fuse

#1

Post by Shem Namo » Fri Jul 31, 2020 8:40 am

Hi everyone, how are you?

I'm trying to write a fuse that uses z buffer values, and ran into a little issue.

When I use this attached scene, Resolve just crashes, but Fusion 9 still runs.
I'm pretty sure that the issue is because some of the the z values sample -1.0e+30
Which I'm pretty sure is similar to #INF

Here is a small setup that shows how the fuse crashes.
Does anyone know how I can fix this? Is there a way to get rid of these weird values?

Thanks in Advanced,
David

Code: Select all

{
	Tools = ordered() {
		Camera3D1 = Camera3D {
			Inputs = {
				["Transform3DOp.Translate.Z"] = Input { Value = 9.20676806140393, },
				AoV = Input { Value = 19.2642683071402, },
				["Stereo.Mode"] = Input { Value = FuID { "Mono" }, },
				FilmGate = Input { Value = FuID { "BMD_URSA_4K_16x9" }, },
				ApertureW = Input { Value = 0.831496062992126, },
				ApertureH = Input { Value = 0.467716535433071, },
				["SurfacePlaneInputs.ObjectID.ObjectID"] = Input { Value = 3, },
				["MtlStdInputs.MaterialID"] = Input { Value = 2, },
			},
			ViewInfo = OperatorInfo { Pos = { 770, 49.5 } },
		},
		Shape3D3 = Shape3D {
			Inputs = {
				["Transform3DOp.Translate.Z"] = Input { Value = -3.26174617549861, },
				Shape = Input { Value = FuID { "SurfaceCylinderInputs" }, },
				["MtlStdInputs.MaterialID"] = Input { Value = 4, },
				["SurfacePlaneInputs.ObjectID.ObjectID"] = Input { Value = 6, },
				["SurfaceCylinderInputs.Radius"] = Input { Value = 0.29, },
				["SurfaceCylinderInputs.ObjectID.ObjectID"] = Input { Value = 7, }
			},
			ViewInfo = OperatorInfo { Pos = { 605, 82.5 } },
		},
		Shape3D1 = Shape3D {
			Inputs = {
				["Transform3DOp.Translate.X"] = Input { Value = 1.71508597122628, },
				["Transform3DOp.Translate.Y"] = Input { Value = 0.0418397108746192, },
				["Transform3DOp.Translate.Z"] = Input { Value = -0.0241560675185154, },
				["Transform3DOp.Rotate.X"] = Input { Value = -32.0335740009323, },
				["Transform3DOp.Rotate.Y"] = Input { Value = -45.5757244010456, },
				Shape = Input { Value = FuID { "SurfaceSphereInputs" }, },
				["MtlStdInputs.MaterialID"] = Input { Value = 1, },
				["SurfacePlaneInputs.ObjectID.ObjectID"] = Input { Value = 1, },
				["SurfaceSphereInputs.SubdivisionLevelBase"] = Input { Value = 100, },
				["SurfaceSphereInputs.SubdivisionLevelHeight"] = Input { Value = 100, },
				["SurfaceSphereInputs.ObjectID.ObjectID"] = Input { Value = 2, }
			},
			ViewInfo = OperatorInfo { Pos = { 605, 115.5 } },
		},
		Merge3D1 = Merge3D {
			Inputs = {
				SceneInput1 = Input {
					SourceOp = "DirectionalLight1",
					Source = "Output",
				},
				SceneInput2 = Input {
					SourceOp = "Shape3D1",
					Source = "Output",
				},
				SceneInput3 = Input {
					SourceOp = "Camera3D1",
					Source = "Output",
				},
				SceneInput4 = Input {
					SourceOp = "Shape3D2",
					Source = "Output",
				},
				SceneInput5 = Input {
					SourceOp = "Shape3D3",
					Source = "Output",
				},
			},
			ViewInfo = OperatorInfo { Pos = { 715, 148.5 } },
		},
		Renderer3D1 = Renderer3D {
			CustomData = {
				ToolVersion = 2,
			},
			Inputs = {
				Width = Input { Value = 3840, },
				Height = Input { Value = 2160, },
				["Gamut.SLogVersion"] = Input { Value = FuID { "SLog2" }, },
				SceneInput = Input {
					SourceOp = "Merge3D1",
					Source = "Output",
				},
				["RendererSoftware.Channels.Z"] = Input { Value = 1, },
				["RendererSoftware.LightingEnabled"] = Input { Value = 1, },
			},
			ViewInfo = OperatorInfo { Pos = { 825, 148.5 } },
		},
		ZRemap1 = Fuse.ZRemap {
			CtrlWZoom = false,
			Inputs = {
				Znear = Input { Value = -8.23244094848633, },
				ZFar = Input { Value = -16.2684860229492, },
				Image = Input {
					SourceOp = "Renderer3D1",
					Source = "Output",
				},
			},
			ViewInfo = OperatorInfo { Pos = { 935, 148.5 } },
			Version = 100
		},
		DirectionalLight1 = LightDirectional {
			Inputs = {
				["Transform3DOp.Translate.Z"] = Input { Value = 2.45637371055244, },
				["Transform3DOp.Rotate.X"] = Input { Value = -33.6711173057556, },
				["Transform3DOp.Rotate.Y"] = Input { Value = -41.2982196398079, },
			},
			ViewInfo = OperatorInfo { Pos = { 605, 148.5 } },
		},
		Shape3D2 = Shape3D {
			Inputs = {
				["Transform3DOp.Translate.X"] = Input { Value = -2.74291366664428, },
				["Transform3DOp.Translate.Z"] = Input { Value = -6.81529388494821, },
				["Transform3DOp.Rotate.X"] = Input { Value = -45.1, },
				["Transform3DOp.Rotate.Y"] = Input { Value = -37.9, },
				Shape = Input { Value = FuID { "SurfaceCubeInputs" }, },
				["MtlStdInputs.MaterialID"] = Input { Value = 3, },
				["SurfacePlaneInputs.ObjectID.ObjectID"] = Input { Value = 4, },
				["SurfaceCubeInputs.Width"] = Input { Value = 1.27, },
				["SurfaceCubeInputs.ObjectID.ObjectID"] = Input { Value = 5, }
			},
			ViewInfo = OperatorInfo { Pos = { 605, 181.5 } },
		}
	},
	ActiveTool = "ZRemap1"
}
  1. --[[--
  2. Z ReMap | by Learn Now FX.
  3. This fuse creates a normalized Z Buffer using a method described in this amazing tutorial by Simon Ubsdell
  4. https://youtu.be/iQTYOivZnSI
  5.  
  6. I huge thank to June Yu From CGS Fusion for showing how to pick Z values from an image in his CGS_NormalZ Macro
  7. https://www.steakunderwater.com/wesuckless/viewtopic.php?f=16&t=3026&p=33337#p23634
  8. --]]--
  9.  
  10.  
  11. FuRegisterClass("ZRemap", CT_Tool, {
  12.     REGS_Category = "Learn Now FX",
  13.     REGS_OpIconString = "ZRM",
  14.     REGS_Name = "Z Remap",
  15.     REGS_OpDescription = "Normalized Z Buffer",
  16.     REG_Version = 100,
  17.     REGS_Company = "Learn Now FX",
  18.     REGS_URL = "http://www.youtube.com/LearnNowFX",
  19.     REGS_HelpTopic = "https://www.facebook.com/groups/DavinciResolveFusion",
  20.   --REG_Fuse_NoEdit = true,
  21.   --REG_Fuse_NoReload = true,              
  22.     })
  23.  
  24. function Create()
  25. InInvert = self:AddInput("Invert Depth Map", "InvertDepthMap", {
  26.     LINKID_DataType     = "Number",
  27.     INPID_InputControl  = "CheckboxControl",
  28.     INP_Integer         = true,
  29.     INP_Default         = 0,
  30. })
  31. InZNear = self:AddInput("Z Near", "Znear", {
  32.     LINKS_Name = "Z Near",
  33.     LINKID_DataType = "Number",
  34.     INPID_InputControl = "ColorControl",
  35.     INP_Integer = false,
  36.     INP_MinScale = 0,
  37.     INP_MaxScale = 1,
  38.     INP_MinAllowed = -1000000,
  39.     INP_MaxAllowed = 1000000,
  40.     IC_ControlGroup = 1,
  41.     IC_ControlID = 12,
  42.     CLRC_ColorSpace = "HSV",
  43.     CLRC_ShowWheel = false,
  44.     ICS_ControlPage = "Controls"
  45. })
  46. InZFar = self:AddInput("Z Far", "ZFar", {
  47.     LINKS_Name = "Z Far",
  48.     LINKID_DataType = "Number",
  49.     INPID_InputControl = "ColorControl",
  50.     INP_Default = -1000,
  51.     INP_Integer = false,
  52.     INP_MinScale = 0,
  53.     INP_MaxScale = 1,
  54.     INP_MinAllowed = -1000000,
  55.     INP_MaxAllowed = 1000000,
  56.     IC_ControlGroup = 2,
  57.     IC_ControlID = 12,
  58.     CLRC_ColorSpace = "HSV",
  59.     CLRC_ShowWheel = false,
  60.     ICS_ControlPage = "Controls"
  61. })
  62. InImage = self:AddInput("Image", "Image", {
  63.     LINKID_DataType = "Image",
  64.     LINK_Main = 1,
  65.     INP_AcceptsGPUImages = true,
  66. })
  67.  
  68. OutImage = self:AddOutput("Output", "Output", {
  69.     LINKID_DataType = "Image",
  70.     LINK_Main = 1,
  71. })             
  72. end
  73.  
  74. function zFunc(p,near, far, invert)
  75.  
  76. if p.Z ~= p.Z
  77.   then p.Z = nil
  78. end
  79.   local buffer = (p.Z - far) * 1.0 / (near - far)
  80.  
  81.   p.R = math.max(math.min(buffer, 1.0), 0.0)
  82.   p.G = math.max(math.min(buffer, 1.0), 0.0)
  83.   p.B = math.max(math.min(buffer, 1.0), 0.0)
  84.    
  85. if invert == 1 then
  86.   p.R = 1.0 - p.R
  87.   p.G = 1.0 - p.G
  88.   p.B = 1.0 - p.B  
  89. end
  90.   p.A = 1.0
  91.   return p
  92. end
  93.            
  94. function Process(req)
  95.     local img = InImage:GetValue(req)
  96.     local invert = InInvert:GetValue(req).Value
  97.     local zNear = InZNear:GetValue(req).Value
  98.     local zFar = InZFar:GetValue(req).Value
  99.    
  100.     local out = Image({IMG_Like = img})
  101.    
  102.         self:DoMultiProcess(nil, { In = img, Out = out,
  103.             Invert = invert, ZNear = zNear,
  104.             ZFar = zFar,ZFunc = zFunc, }, img.Height, function (y)
  105.             local p = Pixel()
  106.            
  107.             for x=0,In.Width-1 do
  108.             In:GetPixel(x,y, p)
  109.                
  110.             p = ZFunc(p, ZNear, ZFar, Invert)
  111.                
  112.             Out:SetPixel(x,y, p)
  113.   end          
  114. end)
  115.    
  116.     OutImage:Set(req, out)
  117. end
  118.  

User avatar
Midgardsormr
Fusionator
Posts: 1850
Joined: Wed Nov 26, 2014 8:04 pm
Answers: 16
Location: Los Angeles, CA, USA
Been thanked: 121 times
Contact:

Re: Error when trying to use Z Buffer values in a Fuse

#2

Post by Midgardsormr » Fri Jul 31, 2020 1:31 pm

Are your Z values otherwise positive? You could just do max(0, value) to clamp it.

User avatar
Chad
Fusionator
Posts: 1491
Joined: Fri Aug 08, 2014 1:11 pm
Been thanked: 12 times

Re: Error when trying to use Z Buffer values in a Fuse

#3

Post by Chad » Fri Jul 31, 2020 2:58 pm

There's no check that zNear ~= zFar, which could result in a divide by zero. Not saying that's the issue, but it's a potential problem.


Also, why are you doing the clamping for each RGB? You only have to do for one and then copy to the others. That's just a performance thing, though.

User avatar
Shem Namo
Fusionista
Posts: 540
Joined: Sun Oct 06, 2019 9:15 pm
Location: North Israel
Real name: David Kohen
Been thanked: 15 times

Re: Error when trying to use Z Buffer values in a Fuse

#4

Post by Shem Namo » Mon Aug 03, 2020 3:13 am

Midgardsormr wrote:
Fri Jul 31, 2020 1:31 pm
Are your Z values otherwise positive? You could just do max(0, value) to clamp it.
Thanks Bryan, the Z values I've been using so far are negative, but I think that if I move the camera and the objects, they will become positive.
It's just that I'm trying to make this fuse as durable as possible. So I don't want to limit the fuse to only positive or only negative values.
Chad wrote:
Fri Jul 31, 2020 2:58 pm
There's no check that zNear ~= zFar, which could result in a divide by zero. Not saying that's the issue, but it's a potential problem.


Also, why are you doing the clamping for each RGB? You only have to do for one and then copy to the others. That's just a performance thing, though.
Thanks for the advice Chad, I applied all of your tips in this newer version.

This newer version still crashes Resolve, but I think I found the problem,
in this new setup I used the methods described by @SecondMan in this post
to get rid on #INF values using a Custom Tool.

The custom tool in the setup keeps Resolve from crashing, but as soon as I remove it, Resolve crashes.
I don't know why this happen, because I already wrote the same thing that happen in the Custom Tool into the fuse.

Can someone please point me in the right direction?
Are there any fuses that use Z Buffer values?

Thanks again,
David


  1. --[[--
  2. Z ReMap | by Learn Now FX.
  3. This fuse creates a normalized Z Buffer using a method described in this amazing tutorial by Simon Ubsdell
  4. https://youtu.be/iQTYOivZnSI
  5.  
  6. I huge thank to June Yu From CGS Fusion for showing how to pick Z values from an image in his CGS_NormalZ Macro
  7. https://www.steakunderwater.com/wesuckless/viewtopic.php?f=16&t=3026&p=33337#p23634
  8. --]]--
  9.  
  10.  
  11. FuRegisterClass("ZRemap", CT_Tool, {
  12.     REGS_Category = "Learn Now FX",
  13.     REGS_OpIconString = "ZRM",
  14.     REGS_Name = "Z Remap",
  15.     REGS_OpDescription = "Normalized Z Buffer",
  16.     REG_Version = 100,
  17.     REGS_Company = "Learn Now FX",
  18.     REGS_URL = "http://www.youtube.com/LearnNowFX",
  19.     REGS_HelpTopic = "https://www.facebook.com/groups/DavinciResolveFusion",
  20.  -- REG_Fuse_NoEdit = true,
  21.  -- REG_Fuse_NoReload = true,              
  22.     })
  23.  
  24. function Create()
  25. InInvert = self:AddInput("Invert Depth Map", "InvertDepthMap", {
  26.     LINKID_DataType     = "Number",
  27.     INPID_InputControl  = "CheckboxControl",
  28.     INP_Integer         = true,
  29.     INP_Default         = 0,
  30. })
  31. InZNear = self:AddInput("Z Near", "Znear", {
  32.     LINKS_Name = "Z Near",
  33.     LINKID_DataType = "Number",
  34.     INPID_InputControl = "ColorControl",
  35.     INP_Integer = false,
  36.     INP_MinScale = 0,
  37.     INP_MaxScale = 1,
  38.     INP_MinAllowed = -1000000,
  39.     INP_MaxAllowed = 1000000,
  40.     IC_ControlGroup = 1,
  41.     IC_ControlID = 12,
  42.     CLRC_ColorSpace = "HSV",
  43.     CLRC_ShowWheel = false,
  44.     ICS_ControlPage = "Controls"
  45. })
  46. InZFar = self:AddInput("Z Far", "ZFar", {
  47.     LINKS_Name = "Z Far",
  48.     LINKID_DataType = "Number",
  49.     INPID_InputControl = "ColorControl",
  50.     INP_Default = -1000,
  51.     INP_Integer = false,
  52.     INP_MinScale = 0,
  53.     INP_MaxScale = 1,
  54.     INP_MinAllowed = -1000000,
  55.     INP_MaxAllowed = 1000000,
  56.     IC_ControlGroup = 2,
  57.     IC_ControlID = 12,
  58.     CLRC_ColorSpace = "HSV",
  59.     CLRC_ShowWheel = false,
  60.     ICS_ControlPage = "Controls"
  61. })
  62. InImage = self:AddInput("Image", "Image", {
  63.     LINKID_DataType = "Image",
  64.     LINK_Main = 1,
  65.     INP_AcceptsGPUImages = true,
  66. })
  67.  
  68. OutImage = self:AddOutput("Output", "Output", {
  69.     LINKID_DataType = "Image",
  70.     LINK_Main = 1,
  71. })             
  72. end
  73.  
  74. function clampFunc(p)
  75. if p.Z then
  76.   if p.Z < -1000 then p.Z = -100000
  77.   end
  78. end
  79.  
  80.   return p
  81. end
  82.  
  83. function zFunc(p,near, far, invert)
  84. if p.Z < 100000000 and p.Z > -100000000 then
  85.  
  86. if near ~= far then
  87.   local buffer = (p.Z - far) * 1.0 / (near - far)
  88.   local normal = math.max(math.min(buffer, 1.0), 0.0)
  89.  
  90.   p.R = normal
  91.   p.G = normal
  92.   p.B = normal
  93.   end  
  94. end
  95.  
  96. if invert == 1 then
  97.   p.R = 1.0 - p.R
  98.   p.G = 1.0 - p.G
  99.   p.B = 1.0 - p.B  
  100.   end  
  101.  
  102.   p.A = 1.0
  103.   return p
  104. end
  105.            
  106. function Process(req)
  107.     local img = InImage:GetValue(req)
  108.     local invert = InInvert:GetValue(req).Value
  109.     local zNear = InZNear:GetValue(req).Value
  110.     local zFar = InZFar:GetValue(req).Value
  111.    
  112.     local out = Image({IMG_Like = img})
  113.    
  114.         self:DoMultiProcess(nil, { In = img, Out = out,
  115.           Invert = invert, ZNear = zNear,
  116.           ZFar = zFar,ZFunc = zFunc, ClampFunc = clampFunc, }, img.Height, function (y)
  117.           local p = Pixel()
  118.            
  119.             for x=0,In.Width-1 do
  120.             In:GetPixel(x,y, p)
  121.             p = ClampFunc(p)   
  122.             p = ZFunc(p, ZNear, ZFar, Invert)
  123.                
  124.             Out:SetPixel(x,y, p)
  125.   end          
  126. end)
  127.    
  128.     OutImage:Set(req, out)
  129. end

Code: Select all

{
	Tools = ordered() {
		Shape3D1 = Shape3D {
			Inputs = {
				["SurfacePlaneInputs.ObjectID.ObjectID"] = Input { Value = 1, },
				["Transform3DOp.Translate.X"] = Input { Value = 1.71508597122628, },
				["Transform3DOp.Translate.Y"] = Input { Value = 0.0418397108746192, },
				["Transform3DOp.Translate.Z"] = Input { Value = -0.0241560675185154, },
				["Transform3DOp.Rotate.X"] = Input { Value = -32.0335740009323, },
				["Transform3DOp.Rotate.Y"] = Input { Value = -45.5757244010456, },
				Shape = Input { Value = FuID { "SurfaceSphereInputs" }, },
				["MtlStdInputs.MaterialID"] = Input { Value = 1, },
				["SurfaceSphereInputs.SubdivisionLevelBase"] = Input { Value = 100, },
				["SurfaceSphereInputs.SubdivisionLevelHeight"] = Input { Value = 100, },
				["SurfaceSphereInputs.ObjectID.ObjectID"] = Input { Value = 2, }
			},
			ViewInfo = OperatorInfo { Pos = { 330, 280.5 } },
		},
		Shape3D3 = Shape3D {
			Inputs = {
				["SurfacePlaneInputs.ObjectID.ObjectID"] = Input { Value = 6, },
				["Transform3DOp.Translate.Z"] = Input { Value = -3.26174617549861, },
				Shape = Input { Value = FuID { "SurfaceCylinderInputs" }, },
				["MtlStdInputs.MaterialID"] = Input { Value = 4, },
				["SurfaceCylinderInputs.Radius"] = Input { Value = 0.29, },
				["SurfaceCylinderInputs.ObjectID.ObjectID"] = Input { Value = 7, }
			},
			ViewInfo = OperatorInfo { Pos = { 330, 313.5 } },
		},
		Merge3D1 = Merge3D {
			Inputs = {
				SceneInput1 = Input {
					SourceOp = "DirectionalLight1",
					Source = "Output",
				},
				SceneInput2 = Input {
					SourceOp = "Shape3D1",
					Source = "Output",
				},
				SceneInput3 = Input {
					SourceOp = "Camera3D1",
					Source = "Output",
				},
				SceneInput5 = Input {
					SourceOp = "Shape3D3",
					Source = "Output",
				},
				SceneInput4 = Input {
					SourceOp = "Shape3D2",
					Source = "Output",
				},
			},
			ViewInfo = OperatorInfo { Pos = { 440, 346.5 } },
		},
		DirectionalLight1 = LightDirectional {
			Inputs = {
				["Transform3DOp.Translate.Z"] = Input { Value = 2.45637371055244, },
				["Transform3DOp.Rotate.X"] = Input { Value = -33.6711173057556, },
				["Transform3DOp.Rotate.Y"] = Input { Value = -41.2982196398079, },
			},
			ViewInfo = OperatorInfo { Pos = { 330, 346.5 } },
		},
		Renderer3D1 = Renderer3D {
			CustomData = { ToolVersion = 2, },
			Inputs = {
				GlobalOut = Input { Value = 119, },
				Width = Input { Value = 3840, },
				Height = Input { Value = 2160, },
				["Gamut.SLogVersion"] = Input { Value = FuID { "SLog2" }, },
				SceneInput = Input {
					SourceOp = "Merge3D1",
					Source = "Output",
				},
				["RendererSoftware.Channels.Z"] = Input { Value = 1, },
				["RendererSoftware.LightingEnabled"] = Input { Value = 1, },
			},
			ViewInfo = OperatorInfo { Pos = { 550, 346.5 } },
		},
		Shape3D2 = Shape3D {
			Inputs = {
				["SurfacePlaneInputs.ObjectID.ObjectID"] = Input { Value = 4, },
				["Transform3DOp.Translate.X"] = Input { Value = -2.74291366664428, },
				["Transform3DOp.Translate.Z"] = Input { Value = -6.81529388494821, },
				["Transform3DOp.Rotate.X"] = Input { Value = -45.1, },
				["Transform3DOp.Rotate.Y"] = Input { Value = -37.9, },
				Shape = Input { Value = FuID { "SurfaceCubeInputs" }, },
				["MtlStdInputs.MaterialID"] = Input { Value = 3, },
				["SurfaceCubeInputs.Width"] = Input { Value = 1.27, },
				["SurfaceCubeInputs.ObjectID.ObjectID"] = Input { Value = 5, }
			},
			ViewInfo = OperatorInfo { Pos = { 330, 379.5 } },
		},
		Fixes_Error = Custom {
			NameSet = true,
			Inputs = {
				NumberIn1 = Input { Value = -1000, },
				LUTIn1 = Input {
					SourceOp = "ClampINFLUTIn1",
					Source = "Value",
				},
				LUTIn2 = Input {
					SourceOp = "ClampINFLUTIn2",
					Source = "Value",
				},
				LUTIn3 = Input {
					SourceOp = "ClampINFLUTIn3",
					Source = "Value",
				},
				LUTIn4 = Input {
					SourceOp = "ClampINFLUTIn4",
					Source = "Value",
				},
				RedExpression = Input { Value = "c1", },
				GreenExpression = Input { Value = "c1", },
				BlueExpression = Input { Value = "c1", },
				AlphaExpression = Input { Value = "c1", },
				AuxChannelNest = Input { Value = 1, },
				ZExpression = Input { Value = "if(c1<n1,-100000,c1)", },
				Image1 = Input {
					SourceOp = "Renderer3D1",
					Source = "Output",
				},
			},
			ViewInfo = OperatorInfo { Pos = { 660, 346.5 } },
		},
		ClampINFLUTIn1 = LUTBezier {
			KeyColorSplines = {
				[0] = {
					[0] = { 0, RH = { 0.333333333333333, 0.333333333333333 }, Flags = { Linear = true } },
					[1] = { 1, LH = { 0.666666666666667, 0.666666666666667 }, Flags = { Linear = true } }
				}
			},
			SplineColor = { Red = 204, Green = 0, Blue = 0 },
			NameSet = true,
		},
		ClampINFLUTIn2 = LUTBezier {
			KeyColorSplines = {
				[0] = {
					[0] = { 0, RH = { 0.333333333333333, 0.333333333333333 }, Flags = { Linear = true } },
					[1] = { 1, LH = { 0.666666666666667, 0.666666666666667 }, Flags = { Linear = true } }
				}
			},
			SplineColor = { Red = 0, Green = 204, Blue = 0 },
			NameSet = true,
		},
		ClampINFLUTIn3 = LUTBezier {
			KeyColorSplines = {
				[0] = {
					[0] = { 0, RH = { 0.333333333333333, 0.333333333333333 }, Flags = { Linear = true } },
					[1] = { 1, LH = { 0.666666666666667, 0.666666666666667 }, Flags = { Linear = true } }
				}
			},
			SplineColor = { Red = 0, Green = 0, Blue = 204 },
			NameSet = true,
		},
		ClampINFLUTIn4 = LUTBezier {
			KeyColorSplines = {
				[0] = {
					[0] = { 0, RH = { 0.333333333333333, 0.333333333333333 }, Flags = { Linear = true } },
					[1] = { 1, LH = { 0.666666666666667, 0.666666666666667 }, Flags = { Linear = true } }
				}
			},
			SplineColor = { Red = 204, Green = 204, Blue = 204 },
			NameSet = true,
		},
		Camera3D1 = Camera3D {
			Inputs = {
				["Transform3DOp.Translate.Z"] = Input { Value = 9.20676806140393, },
				AoV = Input { Value = 19.2642683071402, },
				["Stereo.Mode"] = Input { Value = FuID { "Mono" }, },
				FilmGate = Input { Value = FuID { "BMD_URSA_4K_16x9" }, },
				ApertureW = Input { Value = 0.831496062992126, },
				ApertureH = Input { Value = 0.467716535433071, },
				["SurfacePlaneInputs.ObjectID.ObjectID"] = Input { Value = 3, },
				["MtlStdInputs.MaterialID"] = Input { Value = 2, },
			},
			ViewInfo = OperatorInfo { Pos = { 330, 412.5 } },
		},
		ZRemap = Fuse.ZRemap {
			CtrlWZoom = false,
			NameSet = true,
			Inputs = {
				Znear = Input { Value = -8.24378776550293, },
				ZFar = Input { Value = -16.371244430542, },
				Image = Input {
					SourceOp = "Fixes_Error",
					Source = "Output",
				},
			},
			ViewInfo = OperatorInfo { Pos = { 770, 346.5 } },
			Version = 100
		}
	},
	ActiveTool = "ZRemap"
}

User avatar
Shem Namo
Fusionista
Posts: 540
Joined: Sun Oct 06, 2019 9:15 pm
Location: North Israel
Real name: David Kohen
Been thanked: 15 times

Re: Error when trying to use Z Buffer values in a Fuse

#5

Post by Shem Namo » Mon Aug 03, 2020 11:02 am

I really want to thanks @chrisr Chris Ridings for helping solve the issue!!
He send me an email explaining that the problem was in line 64 INP_AcceptsGPUImages = true

Which was there because this fuse was originally meant to be a DCTL fuse, but I decided to make it a regular fuse for stability reasons.
Thanks Chris, I really appreciate it!!

Here is an updated version of the fuse, if you guys like it I'll submit it to Reactor.

Thanks again,
David
  1. --[[--
  2. Z ReMap | by Learn Now FX.
  3. This fuse creates a normalized Z Buffer using a method described in this amazing tutorial by Simon Ubsdell
  4. https://youtu.be/iQTYOivZnSI
  5.  
  6. A huge thank to June Yu From CGS Fusion for showing how to pick Z values from an image in his CGS_NormalZ Macro
  7. https://www.steakunderwater.com/wesuckless/viewtopic.php?f=16&t=3026&p=33337#p23634
  8.  
  9. I would also like to thank by Friend Chris Ridings for fixing an error that made this fuse volatile inside Davinci Resolve.
  10. --]]--
  11.  
  12.  
  13. FuRegisterClass("ZRemap", CT_Tool, {
  14.     REGS_Category = "Learn Now FX",
  15.     REGS_OpIconString = "ZRM",
  16.     REGS_Name = "Z Remap",
  17.     REGS_OpDescription = "Normalized Z Buffer",
  18.     REG_Version = 100,
  19.     REGS_Company = "Learn Now FX",
  20.     REGS_URL = "http://www.youtube.com/LearnNowFX",
  21.     REGS_HelpTopic = "https://www.facebook.com/groups/DavinciResolveFusion",
  22.     REG_Fuse_NoEdit = true,
  23.     REG_Fuse_NoReload = true,              
  24.     })
  25.  
  26. function Create()
  27. InInvert = self:AddInput("Invert Depth Map", "InvertDepthMap", {
  28.     LINKID_DataType     = "Number",
  29.     INPID_InputControl  = "CheckboxControl",
  30.     INP_Integer         = true,
  31.     INP_Default         = 0,
  32. })
  33. InZNear = self:AddInput("Z Near", "Znear", {
  34.     LINKS_Name = "Z Near",
  35.     LINKID_DataType = "Number",
  36.     INPID_InputControl = "ColorControl",
  37.     INP_Integer = false,
  38.     INP_MinScale = 0,
  39.     INP_MaxScale = 1,
  40.     INP_MinAllowed = -1000000,
  41.     INP_MaxAllowed = 1000000,
  42.     IC_ControlGroup = 1,
  43.     IC_ControlID = 12,
  44.     CLRC_ColorSpace = "HSV",
  45.     CLRC_ShowWheel = false,
  46.     ICS_ControlPage = "Controls"
  47. })
  48. InZFar = self:AddInput("Z Far", "ZFar", {
  49.     LINKS_Name = "Z Far",
  50.     LINKID_DataType = "Number",
  51.     INPID_InputControl = "ColorControl",
  52.     INP_Default = -1000,
  53.     INP_Integer = false,
  54.     INP_MinScale = 0,
  55.     INP_MaxScale = 1,
  56.     INP_MinAllowed = -1000000,
  57.     INP_MaxAllowed = 1000000,
  58.     IC_ControlGroup = 2,
  59.     IC_ControlID = 12,
  60.     CLRC_ColorSpace = "HSV",
  61.     CLRC_ShowWheel = false,
  62.     ICS_ControlPage = "Controls"
  63. })
  64. InImage = self:AddInput("Image", "Image", {
  65.     LINKID_DataType = "Image",
  66.     LINK_Main = 1,
  67. })
  68.  
  69. OutImage = self:AddOutput("Output", "Output", {
  70.     LINKID_DataType = "Image",
  71.     LINK_Main = 1,
  72. })             
  73. end
  74.  
  75. function clampFunc(p)
  76. if p.Z then
  77.   if p.Z < -1000 then p.Z = -100000
  78.   end
  79. end
  80.  
  81.   return p
  82. end
  83.  
  84. function zFunc(p,near, far, invert)
  85. if p.Z < 100000000 and p.Z > -100000000 then
  86.  
  87. if near ~= far then
  88.   local buffer = (p.Z - far) * 1.0 / (near - far)
  89.   local normal = math.max(math.min(buffer, 1.0), 0.0)
  90.  
  91.   p.R = normal
  92.   p.G = normal
  93.   p.B = normal
  94.   end  
  95. end
  96.  
  97. if invert == 1 then
  98.   p.R = 1.0 - p.R
  99.   p.G = 1.0 - p.G
  100.   p.B = 1.0 - p.B  
  101.   end  
  102.  
  103.   p.A = 1.0
  104.   return p
  105. end
  106.            
  107. function Process(req)
  108.     local img = InImage:GetValue(req)
  109.     local invert = InInvert:GetValue(req).Value
  110.     local zNear = InZNear:GetValue(req).Value
  111.     local zFar = InZFar:GetValue(req).Value
  112.    
  113.     local out = Image({IMG_Like = img})
  114.    
  115.         self:DoMultiProcess(nil, { In = img, Out = out,
  116.           Invert = invert, ZNear = zNear,
  117.           ZFar = zFar,ZFunc = zFunc, ClampFunc = clampFunc, }, img.Height, function (y)
  118.           local p = Pixel()
  119.            
  120.             for x=0,In.Width-1 do
  121.             In:GetPixel(x,y, p)
  122.             p = ClampFunc(p)   
  123.             p = ZFunc(p, ZNear, ZFar, Invert)
  124.                
  125.             Out:SetPixel(x,y, p)
  126.   end          
  127. end)
  128.    
  129.     OutImage:Set(req, out)
  130. end