It looks like you're new here. If you want to get involved, click one of these buttons!
In the reference document(http://codea.io/reference/Shaders.html#buffer), there is a example, I want to try it, but it failed, and return error: “attempt to index a nil value( global ‘myAttribute’)"
At the same time, the uniform example code is ok. I checked it again and again,but can not find the reason.
I added 1 line to my vertex shader code:
attribute float myAttribute;
below is the example code:
-- Accessing a shader buffer
------------------------
-- Create a mesh
myMesh = mesh()
-- Assign a shader to myMesh
-- This shader has an attribute named 'myAttribute'
myMesh.shader = shader("Documents:MyShader")
-- Fetch the attribute array
myAttrBuffer = myMesh:buffer("myAttribute")
-- Resize our buffer
myAttrBuffer:resize(30)
-- Add some random numbers
for i = 1,30 do
myAttrBuffer[i] = math.random(-50,50)
end
-- The shader will now use this data
-- when myMesh is drawn
Comments
Can't see a problem, sorry
@Ignatz thanks for your response, the below is the whole code:
This is the error info:
You will kick yourself #-o
@Ignatz sorry for this low level mistake, I copy the demo code and comment the line , but the error still there:
Why did you comment out the line
It should not be commented out
@Ignatz What I mean is that the result is the same--error. If I do not comment the line, the error will occur at "myAttrBuffer:resize(30)" , if I comment the line, the error will also occur at "myAttrBuffer:resize(30)".
this is the whole code without comment:
@binaryblues I'm not sure what you're doing with myAttrBuffer, but if I comment out its 3 uses, the code runs without problems. It show a black/grey image of me from the camera.
@binaryblues
The problem is this. I've run into this issue many times with shaders. An interesting thing about custom attribute buffers and uniform arrays in shaders is, you have to actually use them in the shader for them to exist. Just declaring them in the shader preamble is not enough. So, for instance, if in the vertex shader you change
vColor = color;
tovColor = color * myAttribute;
the error disappears.I ran into the exact same thing. I was thinking "always code iteratively" so you gradually add an attribute buffer one piece at a time. But until the attribute is being used for something in the main void loop, it's as if it doesn't exist, and you'll get errors like this.
This also applies to uniform arrays, but not to any other kind of variable.
If only Codea Talk had an "upvote this answer" button.... B-) ~O)
@dave1707 @yojimbo2000 thanks!
I understand it! We must use a var in the function, otherwise it will set nil.
Add "vColor* myAttribute;" into the main of vertex shader, then it will get the value! I will write it in my Codea FAQ
Thanks guys! It took me two days, yesterday, in my dream, I am thinking about it....