Skip to content

Instantly share code, notes, and snippets.

@SableRaf
Created November 23, 2020 17:09
Show Gist options
  • Save SableRaf/6c5b70296d447998b5d7e1214da64c2d to your computer and use it in GitHub Desktop.
Save SableRaf/6c5b70296d447998b5d7e1214da64c2d to your computer and use it in GitHub Desktop.
Sohail Mehra – Spark AR Scripting - Ep 3 – Updated for Spark AR Studio v102
const S = require('Shaders');
const M = require('Materials');
const T = require('Textures');
const R = require('Reactive');
const Time = require('Time');
async function main()
{
// CPU code
const defaultMat = await M.findFirst('material0'); // const defaultMat = M.get('material0'); // before v85
const cameraTex = await T.findFirst('cameraTexture0'); // const cameraTex = T.get('cameraTexture0'); // before v85
const seconds = R.mul(Time.ms,0.001);
const curve = R.abs(R.sin(seconds));
const uv_curve = R.mul(R.sin(seconds),0.1);
// Vertex Shader
const uv = S.vertexAttribute({"variableName" : S.VertexAttribute.TEX_COORDS});
// Interpolators
// uv (vertex shader) -> uv (fragment shader)
const fuv = S.fragmentStage(uv);
// Fragment Shader (Pixel Shader)
const color = S.textureSampler( cameraTex.signal, uv );
const left_color = R.add(color,R.pack4(curve,0,0,1));
const right_color = R.add(color,R.pack4(0,0,curve,1));
const anim_uv = R.add(fuv,uv_curve);
const split = R.step(anim_uv.x, 0.5); // x < 0.5 ? 0 : 1
const ss = R.smoothStep(anim_uv.x,0.3,0.6);
const uv_color = R.pack4(ss,ss,ss,1);
// mix (lerp)
// a,b,t [0,1]
// 0 -> a * 1 + b * 0
// a
// 1 -> a * 0 + b * 1
// b
// 0.5 -> a * 0.5 + b * 0.5
const blend_color = R.mix(left_color,right_color,ss);
const final_color = blend_color;
const textureSlot = S.DefaultMaterialTextures.DIFFUSE;
defaultMat.setTextureSlot(textureSlot, final_color);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment