@dave1707 @Simeon @John - this has been a problem for a while and it makes learning and using Craft less attractive. Does anyone know if it’s a problem with the builtin texture points or the number/positions of vertices in the model?
primitives:sphere is also incorrect. I think the problem is the vertices. I therefore use @loopspace’s extensions instead.
@piinthesky this is cool, I didn’t get it until I started going down the same rabbit hole.
Do you think this trick could be used with complex custom shaders like the @yojimbo2000 explosion shader I’m experimenting with in this thread?
@UberGoober, i think it could be used for many types of shaders. The explosion shader is quite sophisticated so for that case i am not sure. In a previous thread i had asked @John if the explosion shader could be implemented on craft objects-his answer was not obviously optimistic. Consequently, i did make class which converts a craft object into its constituent triangles and does an explosion within codea. Although much slower, it is much easier to use than a shader:
https://codea.io/talk/discussion/10639/class-to-explode-craft-models
@piinthesky I’m trying to understand where the old-style shader is in the code you posted as the solution, but maybe I’m missing the point.
The old shaders always had two parts, the vertex part and the fragments part, and in the solution you posted I see only a basically-empty vertex part and no fragment part at all.
Maybe I misunderstood what you meant by “ the ability to use an inline code shader with craft models is quite powerful”—I thought you meant you’d found a way to insert the old kind of shader code into the format a craft model needs, but is it more that you figured out a different way to code the same effect?
@UberGoober i not at all expert and essentially guessing! but i think void vertex corresponds to the vertex shader, while void surface corresponds to the fragment shader. i.e. the interface has changed but the functionality may be the same. I hope it might be possible to just change the interface while the main guts of the shader code would be similar to before. Would be nice if @John or @simeon gave some hints!
@piinthesky So this is one of the shaders in the basic folder in the Shader Lab, “Invert”. It looks very very simple. I’ve been trying and trying to get it into that custom shader format and all I get is errors. I’m amazed you got a texture to work, is there any chance that what you learned by doing that could help with this?
//
// A basic vertex shader
//
//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
//
// A basic fragment shader
//
//Default precision qualifier
precision highp float;
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
void main()
{
//Sample the texture at the interpolated coordinate
lowp vec4 col = texture2D( texture, vTexCoord ) * vColor;
//Invert the texture color and premultiply it by alpha
gl_FragColor = vec4( (1.0 - col.rgb) * col.a, col.a );
}
I got invert working.
On the downside, it took a lot of work to figure out, but on the upside, it’s massively simpler to do than with the old shaders.
Sadly though I don’t think it will ever be as easy as cutting and pasting the old shaders.
I’m not sure but I think it’s possible that someone more skilled than I could write a conversion script.
@piinthesky this version has a slight bit of animation along with an interesting discovery.
If you declare a property time
it apparently automatically gets fed a time-since-launch number by Craft; if you look in my code I declare the property but don’t directly modify it ever, yet the models still animate.
Another cool way Craft shaders make things easier, and boy do I wish there was some documentation of all this!
@UberGoober - thanks for showing me how to do this. I think we have needed someone to play around with Craft for some time. Thanks again.
Trivial - throws up asset errors
inverse1.model = craft.model(asset.builtin.Primitives.Sphere)
— and
inverse1.material.map = readImage(asset.builtin.Blocks.Wood)
Thanks @Bri_G. I don’t suppose you could grok why this version of the Blend Images shader doesn’t work? It seems like I did the same thing I did with the Invert shader.
@UberGoober - not sure what you want, seemed OK to me.
Try this:
local blendedSlab = scene:entity()
blendedSlab.model = craft.model(asset.builtin.Primitives.RoundedCube)
blendedSlab.scale = vec3(1, 1, 0.15)
blendedSlab.material = craft.material("blendImages")
blendedSlab.material.texture1 = readImage(asset.builtin.Cargo_Bot.Codea_Icon)
blendedSlab.material.texture2 = readImage(asset.builtin.Planet_Cute.Heart)
blendedSlab.position = vec3(0, 1, 5)
blendedSlab.eulerAngles = vec3(70, 40, 0)
@Bri_G attached is what the blended image is supposed to look like. This is how it appears in the Shader Lab Blended Image sample shader. This is what should be showing on the slab, but instead it just shows the heart.
It seems like this line isn’t working in the shader:
colMix = mix(col1, col2, mixAmount);
…and that’s pretty much exactly lifted from the sample shader.
Any thoughts?
Got it!
I had sent the default mix amount to 1.0, instead of 0.5 as it is in the original shader.
Works nice and smooth now.
Now the Ripple shader works too.
@dave1707, @Bri_G, @West, @skar, @piinthesky : i’m trying to work my way up to adapting @yojimbo2000’s explosion shader, in baby steps.
So far I’ve mostly done color modifications, and I’ve only moved the actual position of fragments all at once in the simplest way.
There are so many shaders on the forums—do you think any of you could recommend a good “baby steps“ shader that moves fragments around in a cool but fairly simple way?