import { targetGLSL } from "@thi.ng/shader-ast-glsl/target" ; import { canvasRenderer , targetJS } from "@thi.ng/shader-ast-js" ; import { fit0111 , fit1101 } from "@thi.ng/shader-ast-stdlib/math/fit" ; import type { FloatSym , Vec2Sym } from "@thi.ng/shader-ast/api/syms" ; import { defn , ret } from "@thi.ng/shader-ast/ast/function" ; import { float , vec3 , vec4 } from "@thi.ng/shader-ast/ast/lit" ; import { add , div , mul } from "@thi.ng/shader-ast/ast/ops" ; import { $x , $y } from "@thi.ng/shader-ast/ast/swizzle" ; import { sym } from "@thi.ng/shader-ast/ast/sym" ; import { cos , dot , sin } from "@thi.ng/shader-ast/builtin/math" ; const js = targetJS ( ) ; const gl = targetGLSL ( ) ; const main = defn ( "vec4" , "main" , [ [ "vec2" , "fragCoord" ] , [ "vec2" , "res" ] , [ "float" , "time" ] , ] , ( frag , res , time ) => { let a ; let p ; let uv ; let sp ; let dp ; let p2 ; let m ; return [ ( a = sym ( add ( mul ( sin ( time ) , float ( 2 ) ) , float ( 3 ) ) ) ) , ( p = sym ( add ( mul ( $y ( frag ) , $x ( res ) ) , $x ( frag ) ) ) ) , ( uv = sym ( fit0111 ( div ( frag , res ) ) ) ) , ( sp = sym ( mul ( uv , a ) ) ) , ( dp = sym ( dot ( sp , sp ) ) ) , ( p2 = sym ( add ( mul ( $y ( sp ) , $x ( res ) ) , $x ( sp ) ) ) ) , ( m = sym ( mul ( div ( add ( p2 , mul ( p , a ) ) , mul ( $x ( res ) , $y ( res ) ) ) , mul ( $x ( sp ) , $y ( sp ) ) ) ) ) , ret ( vec4 ( fit1101 ( cos ( mul ( add ( sin ( mul ( vec3 ( 1 , 2 , 3 ) , m ) ) , dp ) , float ( 2 ) ) ) ) , 1 ) ) , ] ; } ) ; console . log ( "JS:" ) ; console . log ( js ( main ) ) ; console . log ( "GLSL:" ) ; console . log ( gl ( main ) ) ; const fn = js . compile ( main ) . main ; const W = 640 ; const H = 480 ; const size = [ W , H ] ; const canvas = document . createElement ( "canvas" ) ; canvas . width = W ; canvas . height = H ; document . body . appendChild ( canvas ) ; const rt = canvasRenderer ( canvas ) ; const t0 = Date . now ( ) ; setInterval ( ( ) => { const time = ( Date . now ( ) - t0 ) * 0.001 ; rt ( ( frag ) => fn ( frag , size , time ) ) ; } , 16 ) ;