Jump to content

Sending table data to shader array problem


Dzsozi (h03)

Recommended Posts

Hello dear people of the MTA community!

I started experiencing with shaders, I would like to learn shader writing, also would like to create a simple gradient drawing resource for the community, but to finish that project, first I must come to the community for some help. ?

I've watched about 7 hours of Unity HLSL tutorial and read many documents and forums to just understand the basics and to know what is what and etc. Surprisingly I understood it fairly quickly, so I jumped into re-writing the unefficient gradient shader I made before.

So to the point: I would like to make the gradient shader based on an array of colors, the maximum colors you can specify for a gradient would be 8. My problem is that for some reason only the first color from the array (colors[0]) gets passed to the shader, the rest remain "unspecified"(?). However, I don't get any errors, and the shader seems to be working fine when adding more colors in script (the red color gets shifted accordingly), but they are simply just transparent, also when trying to debug with return colors[1]; it doesn't output any colors (should output green color, colors[2] should output blue only).

A more compact question of the problem is: How do I pass data from lua to a shader array?

I don't know if the problem is in the lua or the shader file, my guess is that there is a problem in the lua script file, when setting the shader property. Let me show you a screenshot of the problem:

spacer.png

This gradient should go from red to green to blue, but only red color gets noticed by the shader script. Here are my codes:

gradient.fx

#include "util.fx" //this is not necessary right now, all I have here is inverseLerp and rotationMatrix functions

half4 colors[8];
int colorCount;

half4 PixelShaderFunction(float2 uv : TEXCOORD0) : COLOR {
	//float4 finalColor = lerp(colors[0], colors[1], uv.x);
	//return colors[1]; //doesn't output anything!!! ??????!!?!?
	
	half4 finalColor = colors[0];
	for ( int i = 0 ; i < colorCount ; ++i ) {
		finalColor = max(0, lerp(finalColor, colors[i], uv.x)); // using max to prevent going into negative values
	}
	
	return finalColor;
}

technique tec0 {
	pass P0 {
        PixelShader = compile ps_2_0 PixelShaderFunction();
	}
}

 

parts of gradientC.lua where I update and create the gradient

function Gradient.new(x, y, w, h, rotation, colorTable, gradientSize, rotateToCenter)
	if not tonumber(x or y or w or h) then return false end
	--if rotateToCenter == nil then rotateToCenter = false end
	
	local data = setmetatable({}, Gradient)
	data.shader = DxShader(":sa_gradient/res/gradient.fx", 0, 0, false, "other")
	data.id = #CREATED_GRADIENTS+1
	
	data.position = Vector2(x, y)
	data.size = Vector2(w, h)
	data.rotation = tonumber(rotation) or 0
	data.gradientSize = tonumber(gradientSize) and math.max(0, math.min(1, gradientSize)) or 0.5
	data.colors = type(colorTable) == "table" and colorTable or {Vector4(1,0,0,1), Vector4(0,1,0,1), Vector4(0,0,1,1)}
	data.colorCount = #data.colors
	--data.rotateToCenter = rotateToCenter
	
	CREATED_GRADIENTS[data.id] = data
	return data
end


function Gradient:update()
	if not self.shader then return false end
	
	--self.shader:setValue("rotateToCenter", self.rotateToCenter)
	--self.shader:setValue("rectangleSize", self.size)
	--self.shader:setValue("rotation", self.rotation)
	--self.shader:setValue("gradientSize", self.gradientSize)
  
	self.shader:setValue("colors", unpack(self.colors)) -- MY GUESS IS THAT THE PROBLEM IS HERE, IDK THO :((
	self.shader:setValue("colorCount", self.colorCount)
	
	CREATED_GRADIENTS[self.id] = self
	
	return true
end

 

I also have few more questions regarding MTA and shaders:

- Should I use half4 instead of float4? I know there is a difference between quality if I remember correctly, but does it make things run faster in MTA?

- I seen in a tutorial video that fragment / pixel shader doesn't actually use TEXCOORD0 for position, and that you can use TEXCOORD1, TEXCOORD2 etc to be any kind of data? Is this the case in MTA as well, or it is some Unity thing?

 

What would be a solution for this problem? I like making things compact and simple, so that's why I use tables/arrays for the colors, that way I don't have to make different shader files for 2 or 3 or 4 color gradients.

 

Thank you for your help in advance!

Sorry for tagging, but I don't know how many people are experienced with shaders when talking about HLSL and MTA, I know one for a fact, maybe you could help me out quickly @Ren_712

Link to comment

Update: As I was playing around with setting the shader value in the lua, I figured out that I can actually set the gradient color just fine, but I needed to use it like this:

self.shader:setValue("colors", 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1)

But that's not how I would like this script to work. How can I use/read arrays inside arrays in the shader? Now it is clear for me that the problem must be inside the shader's for loop, I don't know how to select arrays inside arrays. Could somebody explain please how this should look like?

Edited by Dzsozi (h03)
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...