Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to use shaders from Shade

dave1707dave1707 Mod
edited April 18 in General Posts: 9,260

What’s the correct way to use a shader created in Shade in Codea. I copied the explosion shader from Shade into the Codea Dropbox folder. When I tap the screen, I start the explosion shader and stop it after a little over a second. Each time I start it, it seems to start at a different point in the explosion sequence. Also, it doesn’t seem to look like it does in the Shader example explosion. I don’t normally use Shade, so I’m not sure exactly how it’s supposed to be setup and used.

viewer.mode=FULLSCREEN

function setup()
    cnt=0
    fill(255)
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")        
    scene = craft.scene()
    skyMaterial=scene.sky.material
    skyMaterial.sky=color(158, 202, 223, 255)
    skyMaterial.horizon=color(98, 166, 114, 255)
    v=scene.camera:add(OrbitViewer, vec3(0,0,0), 450, 0, 1000)
end

function draw()
    update(DeltaTime)
    scene:draw()
    if explode then
        cnt=80
        explode=false
        createSphere(vec3(0,0,0))
    end
    if cnt>0 then
        cnt=cnt-1
        if cnt<1 then
            pt:destroy()
        end
    end
    text("tap screen for explosion",WIDTH/2,HEIGHT-100)
end

function update(dt)
    scene:update(dt)
end

function createSphere(p)
    if pt then
        pt:destroy()
    end
    pt=scene:entity()
    pt.position=vec3(p.x,p.y,p.z)
    pt.model=craft.model.icosphere(1,2)
    pt.material=craft.material(asset.documents.Dropbox.Explosion)
end

function touched(t)
    if t.state==BEGAN then
        explode=true
    end
end

Comments

  • Posts: 1,178
  • Posts: 632

    @dave1707 , i have a vague memory that you have to feed in the elapsedTime to the shader via a .time=elapsedTime.

  • dave1707dave1707 Mod
    edited April 18 Posts: 9,260

    @piinthesky This is a shader from Shade. I haven’t used Shade that much so I don’t know if there’s a way or how to pass anything to it. I’ve noticed even running the explosion in Shade multiple times, it doesn’t start at what you would consider the beginning. The first node in the shader is Time. The description for Time says it starts when the shader is loaded. So you would think that each time you start the shader it would start at 0.

    PS. I just figured out how to pass a value to a shader.

  • SimeonSimeon Admin Mod
    Posts: 5,613

    @dave1707 when you build a shader in Shade you can expose properties

    For example, if you open the "Checkers" sample shader and tap on the "Properties" button in the main side panel area, you should see "Color A", "Color B" and "Size"

    These will be available as parameters on your exported shader inside Codea

    To make something a property, when you select the appropriate node (some sort of value output node, like a texture, number, color, and so on) you can select whether it is a "property" or a "constant". If you select "property" you can give it a custom name and it will be exposed as a parameter in the exported shader

    For the "Explosion" shader I believe the time nodes are automatically exported as "time" on the shader. But you could replace the time node with a number node and give it an explicit name (this will stop the shader from animating in Shade, but assigning an animated value to that parameter in Codea will cause the shader to animate)

  • dave1707dave1707 Mod
    Posts: 9,260

    @Simeon Thanks for the info. I’m starting to play more with Shade to see how it can be used with Codea.

  • Posts: 1,178

    @dave1707 I’d love to see any progress you make.

  • Posts: 1,178

    @dave1707 have you progressed on this? If you wanted to share an updated version of the code you started this discussion with, that would be very helpful. It’s always nice when a thread contains a working demonstration of the issue brought up in the title.

  • edited April 30 Posts: 1,178
    In case anyone wants a version of @dave1707 ’s origin code with a nice little icon, I’ve attached one.

    I make these for myself because I have jillions of projects, both mine and other people’s, and it helps immensely to have icons on them so I don’t forget what they are.

    I scaled the explosion up a bit too because I wanted to see it better.
  • edited April 30 Posts: 63

    @UberGoober, pt.material=craft.material(asset.documents.Explosion) Does not exist

  • Posts: 1,178
    @krdavis ooops. Should be fixed now.
  • Posts: 63

    Cool…

  • dave1707dave1707 Mod
    Posts: 9,260

    One problem with the explosion shader is that it doesn’t always start at the beginning. It’s based on time so depending on when you start it, you’ll be at different points in time so it could be near the end of its cycle.

  • Posts: 1,178

    @dave1707 I know, that’s why I asked if you’d made any progress on this one ;)

  • dave1707dave1707 Mod
    Posts: 9,260

    @Simeon I was playing with Shade and trying to pass a color to the Properties of a simple shader I made. In the Properties panel, the name was Color (upper case C). In my Codea project I tried using the name Color but it wouldn’t work. Just for kicks I changed the name in Codea to color (lower case c) and it worked. So is the Properties name between Codea and the shader not case sensitive or something.

    I tried the Checkers shader with a Properties name Size. Codea didn’t work with Size but it did work with size.

  • Posts: 2,241

    @dave1707 - color is a reserved word in Codea does that mean you were effectively passing a vec4() to the shader?

  • dave1707dave1707 Mod
    Posts: 9,260

    @Bri_G I don’t think that was the problem because the same thing happened with Checkers. The name in the shader was Size, but the name that worked in Codea was size. So apparently even though the name is uppercase in Shade, the name should be lower case in Codea.

  • dave1707dave1707 Mod
    edited April 30 Posts: 9,260

    @UberGoober Heres an updated version of an explosion. It always starts when you tap the screen. It’s not perfect, but I’m still playing with Shade.

    PS. zip file removed. See farther down.

  • Posts: 1,178

    @dave1707 that is super cool!

    Any idea why the previous version had a bumpy exterior but this one seems to be totally spherical?

  • dave1707dave1707 Mod
    Posts: 9,260

    @UberGoober I saw that and I’m not sure why. Like I said, I just started to play around with shaders from Shade used in Codea. I’ll have to figure out what’s up since I started with the same shader in both versions.

  • dave1707dave1707 Mod
    Posts: 9,260

    @UberGoober Is this a little better.

    001e.zip 36.7K
  • Posts: 1,178

    Definitely! What’d you do? I probably won’t understand it but I’m curious still.

  • dave1707dave1707 Mod
    Posts: 9,260

    @UberGoober Instead of using the icosphere, I used the monkey.

Sign In or Register to comment.