Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE HTML >
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title> #NEXIENT# </title>
- <!-- AUTHOR : KanjiCoder On Youtube -->
- <!-- TUTORIALS: www.tinyurl.com/BOMB-CODE -->
- <!-- EMAIL : KanjiCoder@GMAIL.com -->
- <!-- SSSSSSSSS SHADER_SECTION START SSSSSSSSS -->
- <!-- SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS -->
- <!-- SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS -->
- <style>
- p{ font-size:12pt;}
- div,h3,p,input,button,br{
- padding:0px;
- margin:0px;
- font-family:"Andale Mono";
- }
- button,input{
- padding:10px;
- }
- </style>
- <script id="VERT_SHADER" type="NOT_JAVASCRIPT">
- precision highp float;
- attribute vec2 a_position;
- void main() {
- gl_Position = vec4(a_position, 0, 1);
- }
- </script>
- <!-- Simpler shader for when you need to -->
- <!-- simplify and hunt down a problem -->
- <script id="FRAG_SHADER_SIMPLE" type="NOT_JAVASCRIPT">
- #ifdef GL_FRAGMENT_PRECISION_HIGH
- precision highp float;
- precision highp int;
- #else
- precision mediump float;
- precision mediump int;
- #endif
- uniform int HAS_TEXTURE_BEEN_PUSHED_FROM_JAVASCRIPT;
- uniform sampler2D u_texture; //<-- "uSampler" here: https://www.john-smith.me/hassles-with-array-access-in-webgl-and-a-couple-of-workarounds.html
- void main(){
- gl_FragColor = vec4( 0.5, 0.5, 0.5, 1.0);
- }
- </script>
- <!-- ORIGINAL ID: FRAG_SHADER -->
- <script id="FRAG_SHADER_HEAD" type="NOT_JAVASCRIPT">
- //Must declare precision before declaring
- //any uniforms:
- ////////////////////////////////////////////////
- #ifdef GL_FRAGMENT_PRECISION_HIGH
- precision highp float;
- precision highp int;
- #else
- precision mediump float;
- precision mediump int;
- #endif
- ////////////////////////////////////////////////
- //:Width and height of tilemap in TILES:
- uniform int CMAP_WID;
- uniform int CMAP_HIG;
- //:For scrolling the canvas:
- uniform float SCROLL_X;
- uniform float SCROLL_Y;
- //SOURCE: https://stackoverflow.com/questions/36324295/webgl-access-buffer-from-shader
- //: QUOTE[
- //: Make sure your texture filtering
- //: is set to gl.NEAREST.
- //: ]QUOTE
- //SDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD//
- uniform int HAS_TEXTURE_BEEN_PUSHED_FROM_JAVASCRIPT;
- uniform sampler2D u_texture; //<-- "uSampler" here: https://www.john-smith.me/hassles-with-array-access-in-webgl-and-a-couple-of-workarounds.html
- vec2 textureSize = vec2(128.0, 128.0);
- vec4 getValueFromTexture(float index) {
- float column = mod(index, textureSize.x);
- float row = floor(index / textureSize.x);
- vec2 uv = vec2(
- (column + 0.5) / textureSize.x,
- (row + 0.5) / textureSize.y);
- return texture2D(u_texture, uv);
- }
- //Integer version of function:
- vec4 TVi( int index_i ){
- //return getValueFromTexture( float(index_i) );
- vec4 vf = getValueFromTexture( float(index_i) );
- //Convert into range 0-25:
- //So we can use as tile values:
- vf = (vf * 255.0) / 10.0;
- return vf;
- }
- //Return Red Channel Value As Tile Value:
- int TVi_R( int index_i ){
- vec4 vf = TVi( index_i );
- return int( vf.x );
- //return 0;
- }
- //SDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD//
- #define CANVAS_WID 640.0
- #define CANVAS_HIG 480.0
- float ASPECT_RATIO_YX = CANVAS_HIG / CANVAS_WID;
- //https://stackoverflow.com/questions/27888323/what-does-iresolution-mean-in-a-shader
- vec3 iResolution = vec3(CANVAS_WID , CANVAS_HIG , 1.0);
- //xxxx.x = float( CANVAS_WID );
- //iResolution.y = CANVAS_HIG;
- #define TIL_WID 64.0
- #define TIL_HIG 64.0
- //#define INVERT_RAMP 1
- // For touching values, we assign these unique
- // letters, and then give them names:
- // A: Ape // 1
- // B: Bat // 2
- // C: Cat // 3
- // D: Dog // 4
- // e: elf // 5
- // f: fox // 6
- // g: gun // 7
- // h: hog // 8
- // Y++ (UP)
- // ^ This is a left-handed
- // ^ coordinate system because
- // ^ of the way web-gl works.
- // |
- // +---+---+---+ Take note of this when
- // | e | A | f | converting to go-code
- // +---+---+---+ with inverted Y-axis.
- // | D | | B |
- // +---+---+---+
- // | g | C | h |
- // +---+---+---+ --- X++ --->
- //
- // Shapes then can be identified by variables
- // such as:
- // BatGunApe
- // To avoid confusion, we always concat
- // by circling clockwise, starting at "A".
- //
- // Thus: DogApeBat is INVALID.
- //Different offsets relative to tile in
- //focus to get the different named cells:
- //OSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOS//
- #define APE_OS_X 0
- #define APE_OS_Y 1
- #define BAT_OS_X 1
- #define BAT_OS_Y 0
- #define CAT_OS_X 0
- #define CAT_OS_Y ( -1 )
- #define DOG_OS_X ( -1 )
- #define DOG_OS_Y 0
- #define elf_OS_X ( -1 )
- #define elf_OS_Y 1
- #define fox_OS_X 1
- #define fox_OS_Y 1
- #define gun_OS_X ( -1 )
- #define gun_OS_Y ( -1 )
- #define hog_OS_X 1
- #define hog_OS_Y ( -1 )
- //OSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOSOS//
- int MODE_NO_PULSE = 0;
- int MODE_PULSE = 1;
- int MODE_INVERSION_TICK = 2;
- int MODE_SELECTED = MODE_NO_PULSE;
- float INVERSION_MIN = 0.35; //<--FOR: MODE_INVERSION_TICK
- float INVERSION_MAX = 0.65; //<--FOR: MODE_INVERSION_TICK
- //int DO_INVERSION = 1;
- int DO_GRADIENT_OFFSET_X = 1;
- int DO_GRADIENT_OFFSET_Y = 1;
- //Gradient offset modes:
- int GOM_PIE = 1;
- int GOM_MULTIPLY = 2;
- int GOM_PULSE = 3;
- int GRAD_OFFSET_MODE_X = GOM_MULTIPLY;
- int GRAD_OFFSET_MODE_Y = GOM_MULTIPLY;
- //Uniforms exposed to HTML/JAVASCRIPT:
- uniform float TIL_WID_EDIT;
- uniform float TIL_HIG_EDIT;
- //Time modulates between 0 and 1:
- uniform float TIME_TICKER;
- //Matches iTime of shadertoy website;
- uniform float TIME_UPUP;
- //Hard code to support a 16x16 tilemap:
- uniform float TIL_MAP[256];
- //uniform float TIL_MAP[8];
- float til_wid;
- float til_hig;
- //Shader-toy functions:
- //TFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTF//
- float saturate (float f)
- {
- return clamp(f, 0.0, 1.0);
- //return f;
- }
- //Immulsion field shader functions:
- //https://www.shadertoy.com/view/XtffDM
- //IMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIM//
- // Simplex 3D Noise
- // by Ian McEwan, Ashima Arts
- //
- vec4 permute(vec4 x){return mod(((x*34.0)+1.0)*x, 289.0);}
- vec4 taylorInvSqrt(vec4 r){return 1.79284291400159 - 0.85373472095314 * r;}
- float snoise(vec3 v){
- const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
- const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
- // First corner
- vec3 i = floor(v + dot(v, C.yyy) );
- vec3 x0 = v - i + dot(i, C.xxx) ;
- // Other corners
- vec3 g = step(x0.yzx, x0.xyz);
- vec3 l = 1.0 - g;
- vec3 i1 = min( g.xyz, l.zxy );
- vec3 i2 = max( g.xyz, l.zxy );
- // x0 = x0 - 0. + 0.0 * C
- vec3 x1 = x0 - i1 + 1.0 * C.xxx;
- vec3 x2 = x0 - i2 + 2.0 * C.xxx;
- vec3 x3 = x0 - 1. + 3.0 * C.xxx;
- // Permutations
- i = mod(i, 289.0 );
- vec4 p = permute( permute( permute(
- i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
- + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
- + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
- // Gradients
- // ( N*N points uniformly over a square, mapped onto an octahedron.)
- float n_ = 1.0/7.0; // N=7
- vec3 ns = n_ * D.wyz - D.xzx;
- vec4 j = p - 49.0 * floor(p * ns.z *ns.z); // mod(p,N*N)
- vec4 x_ = floor(j * ns.z);
- vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
- vec4 x = x_ *ns.x + ns.yyyy;
- vec4 y = y_ *ns.x + ns.yyyy;
- vec4 h = 1.0 - abs(x) - abs(y);
- vec4 b0 = vec4( x.xy, y.xy );
- vec4 b1 = vec4( x.zw, y.zw );
- vec4 s0 = floor(b0)*2.0 + 1.0;
- vec4 s1 = floor(b1)*2.0 + 1.0;
- vec4 sh = -step(h, vec4(0.0));
- vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
- vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
- vec3 p0 = vec3(a0.xy,h.x);
- vec3 p1 = vec3(a0.zw,h.y);
- vec3 p2 = vec3(a1.xy,h.z);
- vec3 p3 = vec3(a1.zw,h.w);
- //Normalise gradients
- vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
- p0 *= norm.x;
- p1 *= norm.y;
- p2 *= norm.z;
- p3 *= norm.w;
- // Mix final noise value
- vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
- m = m * m;
- return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
- dot(p2,x2), dot(p3,x3) ) );
- }
- //IMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIMIM//
- //Use gl_FragCoord to calculate tile data:
- //X: X position of tile.
- //Y: Y position of tile.
- //I: # INDEX of tile.
- //V: # VALUE of tile. (Type of tile)
- ivec4 GetTile_XYIV(){
- if(CMAP_WID == 0 || CMAP_HIG == 0){
- return ivec4(0,0,0,0);
- }
- //If uniforms have not set by user,
- //use the default values set by the #define(s)
- //==========================================//
- if(TIL_WID_EDIT > 0.0){
- til_wid = TIL_WID_EDIT;
- }else{
- til_wid = TIL_WID;
- }
- if(TIL_HIG_EDIT > 0.0){
- til_hig = TIL_HIG_EDIT;
- }else{
- til_hig = TIL_HIG;
- }
- //==========================================//
- //NOTE: on "gl_FragCoord" range:
- //******************************************//
- //web-gl: In terms of pixel/canvas coords.
- // X-AXIS: 0 to canvas.width - 1
- // Y-AXIS: 0 to canvas.height- 1
- //OpenGL: In terms of 0 to 1.
- //******************************************//
- //:Calculate number of tiles shown on screen:
- //:This may be fractional:
- float NUM_TIL_X = CANVAS_WID / til_wid;
- float NUM_TIL_Y = CANVAS_HIG / til_hig;
- //DETERMINE WHAT tile you are on:
- float TC_X; //tile coordinate X
- float TC_Y; //tile coordinate Y
- //:FOS: "Frag_Coord Offset":
- float FOS_X = gl_FragCoord.x + SCROLL_X;
- float FOS_Y = gl_FragCoord.y + SCROLL_Y;
- TC_X = floor( FOS_X / til_wid );
- TC_Y = floor( FOS_Y / til_hig );
- int i_X = int( TC_X ); //integer version of TC_X
- int i_Y = int( TC_Y ); //integer version of TC_Y
- int DEX = (i_Y * CMAP_WID) + i_X;
- float tile_value;
- float DEX_F = float(DEX);
- vec4 V4 ; //tile_RGBA
- if( HAS_TEXTURE_BEEN_PUSHED_FROM_JAVASCRIPT > 0 ){
- V4 = getValueFromTexture( DEX_F );
- tile_value = floor( (V4.x*255.0)/10.0 );
- }
- return ivec4(
- i_X,
- i_Y,
- DEX,
- int(tile_value)
- );
- }
- //int[4] GetTile_XYIV(){
- // return int[4];
- //}
- //TFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTF//
- //: n==0: render standard geometric shapes.
- //: n==1: process into normal map.
- //: n==2: render FLAT surfaces.
- vec3 RenderTileBase(in int n, in int di){
- vec3 OUT_DATA;
- //Don't render until tile-map size in tiles
- //has been set by javascript:
- if(CMAP_WID == 0){
- return OUT_DATA;
- }
- if(CMAP_HIG == 0){
- return OUT_DATA;
- }
- //If uniforms have not set by user,
- //use the default values set by the #define(s)
- //==========================================//
- if(TIL_WID_EDIT > 0.0){
- til_wid = TIL_WID_EDIT;
- }else{
- til_wid = TIL_WID;
- }
- if(TIL_HIG_EDIT > 0.0){
- til_hig = TIL_HIG_EDIT;
- }else{
- til_hig = TIL_HIG;
- }
- //==========================================//
- //NOTE: on "gl_FragCoord" range:
- //******************************************//
- //web-gl: In terms of pixel/canvas coords.
- // X-AXIS: 0 to canvas.width - 1
- // Y-AXIS: 0 to canvas.height- 1
- //OpenGL: In terms of 0 to 1.
- //******************************************//
- //:Calculate number of tiles shown on screen:
- //:This may be fractional:
- float NUM_TIL_X = CANVAS_WID / til_wid;
- float NUM_TIL_Y = CANVAS_HIG / til_hig;
- ivec4 T_XYIV = GetTile_XYIV() ;
- int i_X = T_XYIV.x ;
- int i_Y = T_XYIV.y ;
- float tile_value = float( T_XYIV.w );
- int DEX = T_XYIV.z ;
- //Using current tile in focus ( [i_X, i_Y] ),
- //determine coordinates of adjacent tiles.
- ///////////////////////////
- int APE_X = i_X + APE_OS_X;
- int APE_Y = i_Y + APE_OS_Y;
- int BAT_X = i_X + BAT_OS_X;
- int BAT_Y = i_Y + BAT_OS_Y;
- int CAT_X = i_X + CAT_OS_X;
- int CAT_Y = i_Y + CAT_OS_Y;
- int DOG_X = i_X + DOG_OS_X;
- int DOG_Y = i_Y + DOG_OS_Y;
- int elf_X = i_X + elf_OS_X;
- int elf_Y = i_Y + elf_OS_Y;
- int fox_X = i_X + fox_OS_X;
- int fox_Y = i_Y + fox_OS_Y;
- int gun_X = i_X + gun_OS_X;
- int gun_Y = i_Y + gun_OS_Y;
- int hog_X = i_X + hog_OS_X;
- int hog_Y = i_Y + hog_OS_Y;
- ///////////////////////////
- //Determine indicies of [X,Y] positions:
- //iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii//
- int APE_DEX = (APE_Y * CMAP_WID) + APE_X;
- int BAT_DEX = (BAT_Y * CMAP_WID) + BAT_X;
- int CAT_DEX = (CAT_Y * CMAP_WID) + CAT_X;
- int DOG_DEX = (DOG_Y * CMAP_WID) + DOG_X;
- int elf_DEX = (elf_Y * CMAP_WID) + elf_X;
- int fox_DEX = (fox_Y * CMAP_WID) + fox_X;
- int gun_DEX = (gun_Y * CMAP_WID) + gun_X;
- int hog_DEX = (hog_Y * CMAP_WID) + hog_X;
- //iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii//
- vec2 FC_MOD;
- //:FC_MOD.x = gl_FragCoord.x;
- //:FC_MOD.y = gl_FragCoord.y;
- //:FOS: "Frag_Coord Offset":
- float FOS_X = gl_FragCoord.x + SCROLL_X;
- float FOS_Y = gl_FragCoord.y + SCROLL_Y;
- //float FW_X = cos(
- //You want all tiles to have the full range
- //of colors, so you always modulate by
- //CANVAS_WID and CANVAS_HIG, You scale by the
- //# of tiles on each axis which means the
- //gradient becomes steeper as the # of tiles
- //increases.
- FC_MOD.x = mod( FOS_X*NUM_TIL_X, CANVAS_WID );
- FC_MOD.y = mod( FOS_Y*NUM_TIL_Y, CANVAS_HIG );
- //[N]ormalize values into range 0 to 1:
- //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN//
- float norm_X = (FC_MOD.x) / CANVAS_WID;
- float norm_Y = (FC_MOD.y) / CANVAS_HIG;
- //norm_X = 1.0 - norm_X;
- //norm_Y = 1.0 - norm_Y;
- float NMAP_X; // norm_X value mapped.
- float NMAP_Y; // norm_Y value mapped.
- float NMAP_Z; // norm_Z value mapped.
- //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN//
- //Use [B]lue channel because why not?
- //BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB//
- //:float GRAD_X = gl_FragCoord.x / CANVAS_WID;
- //:float GRAD_Y = gl_FragCoord.y / CANVAS_HIG;
- float STATIC_GRAD_X = gl_FragCoord.x / CANVAS_WID;
- float STATIC_GRAD_Y = gl_FragCoord.y / CANVAS_HIG;
- //:This will cause psychedellic pattern
- //:To Scroll Along as well:
- float SCROLL_GRAD_X = cos( FOS_X / CANVAS_WID) ;
- float SCROLL_GRAD_Y = sin( FOS_Y / CANVAS_HIG) ;
- float GRAD_X = mix(STATIC_GRAD_X, SCROLL_GRAD_X, 0.9);
- float GRAD_Y = mix(STATIC_GRAD_Y, SCROLL_GRAD_Y, 0.9);
- //GRAD_X = mod( (GRAD_X + SCROLL_X)*255.0 , 256.0);
- //GRAD_Y = mod( (GRAD_Y + SCROLL_Y)*255.0 , 256.0);
- //GRAD_X = GRAD_X / 255.0;
- //GRAD_Y = GRAD_Y / 255.0;
- //if(GRAD_X > 1.0 || GRAD_X < 0.0){ return OUT_DATA; }
- //if(GRAD_Y > 1.0 || GRAD_Y < 0.0){ return OUT_DATA; }
- //Set to 1 if tile is on:
- ///////////////
- int APE_ON = 0;
- int BAT_ON = 0;
- int CAT_ON = 0;
- int DOG_ON = 0;
- ///////////////
- int elf_ON = 0;
- int fox_ON = 0;
- int gun_ON = 0;
- int hog_ON = 0;
- ///////////////
- //HACK: **************************************
- //REFERENCE:
- //URL[
- // https://www.john-smith.me/hassles-
- // with-array-access-in-webgl-and-a-
- // couple-of-workarounds.html
- // ] **************************************
- //float TV; //Tile Value. (Temp)
- if( HAS_TEXTURE_BEEN_PUSHED_FROM_JAVASCRIPT > 0 ){
- //Bail out. No Tile Here:
- if(tile_value < 1.0 ){
- return vec3(0.0,0.0,0.0);
- }
- int tv_R = int( tile_value );
- if( TVi_R( APE_DEX ) == tv_R){ APE_ON =1; }
- if( TVi_R( BAT_DEX ) == tv_R){ BAT_ON =1; }
- if( TVi_R( CAT_DEX ) == tv_R){ CAT_ON =1; }
- if( TVi_R( DOG_DEX ) == tv_R){ DOG_ON =1; }
- if( TVi_R( elf_DEX ) == tv_R){ elf_ON =1; }
- if( TVi_R( fox_DEX ) == tv_R){ fox_ON =1; }
- if( TVi_R( gun_DEX ) == tv_R){ gun_ON =1; }
- if( TVi_R( hog_DEX ) == tv_R){ hog_ON =1; }
- }
- if( tile_value==0.0 ){
- NMAP_X = 0.0;
- NMAP_Y = 0.0;
- NMAP_Z = 0.0;
- }else
- if(n==0 || n==1){
- //:n==0 : Standard Geometry
- //:n==1 : Normal Map Render
- //:n==2 : FLAT color render.
- //If tile in center is NOT OFF, then we need
- //To do the correct auto-tiling for it.
- //Inverted distance:
- float DX = distance(norm_X, 0.5);
- float DY = distance(norm_Y, 0.5);
- //:Force Merge By Altering distance values
- //:Depending on what tiles are adjacent:
- //:FMFMFMFMFMFMFMFMFMFMFMFMFMFMFMFMFMFM://
- //:Vertical Merging:
- if(APE_ON>0){if(norm_Y > 0.5){ DY = DY-1.0 ;}}
- if(CAT_ON>0){if(norm_Y < 0.5){ DY = DY-1.0 ;}}
- //:Horizontal Merging:
- if(BAT_ON>0){if(norm_X > 0.5){ DX = DX-1.0;}}
- if(DOG_ON>0){if(norm_X < 0.5){ DX = DX-1.0;}}
- //:FMFMFMFMFMFMFMFMFMFMFMFMFMFMFMFMFMFM://
- if(DX < (-0.5) && DY < (-0.5)){///////////
- //Fix problematic Intersections:
- //PIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPI//
- if(DX < DY){
- NMAP_Z = (DX+1.0);
- NMAP_X = (norm_X);
- }else{
- NMAP_Z = (DY+1.0);
- NMAP_Y = (norm_Y);
- }
- NMAP_Z = NMAP_Z*2.0;
- //PIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPIPI//
- }else{////////////////////////////////////
- if(DX > DY){
- //X distance from center is greatest,
- //So color square gradient based on X.
- if(DX < 0.5){
- //ramp up:
- NMAP_Z = DX * 2.0;
- }else{
- //:invert and ramp down:
- NMAP_Z = ( 1.0-(DX-0.5) ) * 2.0;
- }
- NMAP_X = (norm_X);
- }else{
- //Y distance from center is greatest,
- //So color square gradient based on Y.
- //Ditto formula, but for Y:
- if(DY < 0.5){
- NMAP_Z = DY * 2.0;
- }else{
- NMAP_Z = ( 1.0-(DY-0.5) ) * 2.0;
- }
- NMAP_Y = (norm_Y);
- }
- }/////////////////////////////////////////
- //NMAP_X = NMAP_Z;
- //NMAP_Y = NMAP_Z;
- //When done, always invert to get convex pipe:
- //float PRE_FLIP_X = NMAP_X;
- //float PRE_FLIP_Y = NMAP_Y;
- //float PRE_FLIP_Z = NMAP_Z;
- if( n > 0 ){
- //return vec3(0.0,0.0,0.0);
- //Make normal map:
- //Flip to make convex:
- NMAP_Z = 1.0 - NMAP_Z;
- //Divide by 2, then add 0.5.
- //Because Z is height-map, and
- //anything below 0.5 is negative
- //height. No such negative heights
- //on a convex normal map like the
- //one we are making:
- NMAP_Z = 0.5 + (NMAP_Z/2.0);
- }else{
- //Set both X and Y to "Z" value.
- //Z being our height map. Because
- //since we are NOT making normal
- //map of face angles, we want
- //smooth transitions:
- NMAP_X = NMAP_Z;
- NMAP_Y = NMAP_Z;
- }
- //Originally: DO_INVERSION
- if((n <=0 && di > 0) || ( n > 0 && di <= 0) ){
- //CASE: (n <=0 && di > 0):
- //Object is NOT a normal map.
- //To make default convexity,
- //we have to invert the shape:
- //CASE: ( n > 0 && di <= 0):
- //Object IS a normal map.
- //To make default convexity,
- //we don't do anything, because
- //shape making was handled in
- //the ( n > 0 ) condition before
- //this condition.
- //: n>0 : making normal map.
- //: di>0: "flipping by not flipping".
- }else{
- NMAP_X = 1.0 - NMAP_X;
- NMAP_Y = 1.0 - NMAP_Y;
- NMAP_Z = 1.0 - NMAP_Z;
- }
- //Animate the inversion:
- if(MODE_SELECTED==MODE_NO_PULSE){
- //DO nothing.
- }else
- if(MODE_SELECTED==MODE_PULSE){
- NMAP_X = NMAP_X - (TIME_TICKER);
- NMAP_Y = NMAP_Y - (TIME_TICKER);
- NMAP_Z = NMAP_Z - (TIME_TICKER);
- }else
- if(MODE_SELECTED==MODE_INVERSION_TICK){
- float P = TIME_TICKER;
- //By watching the pulse animate, I have
- //identified that there is a certain value of P
- //that gets good results with some shaders.
- //using these Max/Min values to hone in on that.
- if(P > INVERSION_MAX){ P = INVERSION_MAX; }
- if(P < INVERSION_MIN){ P = INVERSION_MIN; }
- NMAP_X = (P-(NMAP_X*P)) + ( NMAP_X-(NMAP_X*P));
- NMAP_Y = (P-(NMAP_Y*P)) + ( NMAP_Y-(NMAP_Y*P));
- NMAP_Z = mix(NMAP_X,NMAP_Y,0.5);
- }
- #define M_PI 3.14159
- }else{
- //: n==2, flat color:
- //This looks much more correct than 0.5:
- NMAP_X = 1.0;
- NMAP_Y = 1.0;
- NMAP_Z = 1.0;
- }
- //BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB//
- if(tile_value != 0.0){
- if(DO_GRADIENT_OFFSET_X > 0){
- if(GRAD_OFFSET_MODE_X==GOM_PIE){
- NMAP_X = ( cos( (NMAP_X+GRAD_X)/1.0*M_PI ) + 1.0);
- }else
- if(GRAD_OFFSET_MODE_X==GOM_MULTIPLY){
- NMAP_X = NMAP_X * (GRAD_X+(0.0));
- }else
- if(GRAD_OFFSET_MODE_X==GOM_PULSE){
- NMAP_X = NMAP_X * (GRAD_X+(TIME_TICKER/1.0));
- }
- }
- if(DO_GRADIENT_OFFSET_Y > 0){
- if(GRAD_OFFSET_MODE_Y==GOM_PIE){
- NMAP_Y = ( cos( (NMAP_Y+GRAD_Y)/1.0*M_PI ) + 1.0);
- }else
- if(GRAD_OFFSET_MODE_Y==GOM_MULTIPLY){
- NMAP_Y = NMAP_Y * (GRAD_Y+(0.0));
- }else
- if(GRAD_OFFSET_MODE_Y==GOM_PULSE){
- NMAP_Y = NMAP_Y * (GRAD_Y+(1.0-(TIME_TICKER/1.0)));
- }
- }
- }
- OUT_DATA.x = NMAP_X;
- OUT_DATA.y = NMAP_Y;
- OUT_DATA.z = NMAP_Z;
- //OUT_DATA.x = norm_X;
- //OUT_DATA.y = norm_Y;
- //OUT_DATA.z = 0.0;
- return OUT_DATA;
- }
- </script>
- <script id="FRAG_SHADER_TILE_R01" type="NOT_JAVASCRIPT">
- //Original Name: "NeonBeams"
- vec4 TIFA_NeonBeams( in vec2 uv ){
- float scale = 0.2;
- float rate = 5.0;
- float t;
- t = TIME_UPUP/rate;
- float result = 0.0;
- float result2 = 0.0;
- float result3 = 0.0;
- //octaves
- for (float i = 0.0; i < 1.0; i++){
- result += snoise(vec3((uv.x*2.0)/scale, (uv.y)/scale, t*2.0))/pow(2.0, i);
- result2 += snoise(vec3((uv.x*4.0)/scale, (uv.y*2.0)/scale, t*2.0))/pow(2.0, i);
- result3 += snoise(vec3((uv.x*8.0)/scale, (uv.y*4.0)/scale, t*2.0))/pow(2.0, i);
- scale /= 2.0;
- }
- //tweaking
- result /= 2.0;
- result = abs(result);
- result = pow(result, .4);
- result = 1.0 - result;
- if (result < .8) result = pow(result, 4.0);
- result2 /= 2.0;
- result2 = abs(result2);
- result2 = pow(result2, .6);
- result2 = 1.0 - result2;
- if (result2 < .8) result2 = pow(result2, 4.0);
- result3 /= 2.0;
- result3 = abs(result3);
- result3 = pow(result3, .4);
- result3 = 1.0 - result3;
- if (result3 < .8) result3 = pow(result3, 4.0);
- result *= 2.0;
- // // // result2 *= 2.0;
- result3 *= 2.0;
- //power for coloring
- float g = pow(result, 4.0);
- float g2 = pow(result2, 4.0);
- vec4 FAKE_FRAG_OUT;
- FAKE_FRAG_OUT = vec4(max(max(g2, result3), g), max(max(result3, 0.0), result), max(max(result, result2), 0.0), 1.0);
- return FAKE_FRAG_OUT;
- }
- </script>
- <script id="FRAG_SHADER_TILE_R02" type="NOT_JAVASCRIPT">
- vec4 TIFA_GalacticDance(in vec2 uv){
- //Original Shader Name: "GalacticDance"
- //SHADER: Galactic Dance.
- //URL: https://www.shadertoy.com/view/XtlGWs
- #define gd_tao 6.28
- #define gd_overbright 4.5
- #define gd_armCount 8.0
- #define gd_armRot 0.005
- #define gd_innerColor vec4(9.0,0.5,0.1,1.0)
- #define gd_outerColor vec4(5.8,0.6,1.0,1.0)
- #define gd_white vec4(0.01,0.21,0.01,1.0)
- float time = TIME_UPUP;
- //constant slow rotation
- float cost = cos(-time*0.2);
- float sint = sin(-time*0.2);
- mat2 trm = mat2 (cost,sint,-sint,cost);
- //scale 0.0-1.0 uv to -1.0-1.0 p
- vec2 p = uv*2.0 - 1.0;
- //apply slow rotation
- p = p * trm;
- //calc distance
- float d = length(p);
- //build arm rotation matrix
- float cosr = cos(gd_armRot*sin(gd_armRot*time));
- float sinr = sin(gd_armRot*cos(gd_armRot*time));
- mat2 rm = mat2 (cosr,sinr,-sinr,cosr);
- //calc arm rotation based on distance
- p = mix(p,p * rm,d);
- //find angle to middle
- float angle = (atan(p.y,p.x)/gd_tao) * 0.5 + 0.5;
- //add the crinkle
- angle += sin(-time*5.0+fract(d*d*d)*10.0)*0.004;
- //calc angle in terms of arm number
- angle *= 2.0 * gd_armCount;
- angle = fract(angle);
- //build arms & wrap the angle around 0.0 & 1.0
- float arms = abs(angle*2.0-1.0);
- //sharpen arms
- arms = pow(arms,10.0*d*d + 5.0);
- //calc radial falloff
- float bulk = 1.0 - saturate(d);
- //create glowy center
- float core = pow(bulk,9.0);
- //calc color
- vec4 color = mix(gd_innerColor,gd_outerColor,d*2.0);
- //JMIM: Remove glowy core:
- vec4 FAKE_FRAG_OUT;
- FAKE_FRAG_OUT = bulk * arms * color + (core/4.0) + bulk*0.25*mix(color,gd_white,0.95);
- //gl_FragColor = bulk * arms * color + core + bulk*0.25*mix(color,gd_white,0.5);
- FAKE_FRAG_OUT = (gd_overbright * FAKE_FRAG_OUT);
- return FAKE_FRAG_OUT;
- }
- </script>
- <!-- #NAMESPACE_PREFIX#[as] -->
- <script id="FRAG_SHADER_TILE_R03" type="NOT_JAVASCRIPT">
- //SOURCE:https://www.shadertoy.com/view/Mty3Dw
- //Original Name: "Alien Surface"
- //-----------------SETTINGS---------------------//
- #define as_TIMES_DETAILED (1.0+.1*sin(as_time*as_PI*1.0))
- #define as_SPIRAL_BLUR_SCALAR (1.0+.1*sin(as_time*as_PI*1.0))
- //-----------------USEFUL-----------------------//
- #define as_PI 3.14159265359
- #define as_EPS 10.0/max(iResolution.x, iResolution.y)
- #define as_circle(x) (vec2(cos(x),sin(x)))
- #define as_flux(x) (vec3(cos(x),cos(4.0*as_PI/3.0+x),cos(2.0*as_PI/3.0+x))*.5+.5)
- #define as_time ((as_saw(float(__LINE__))+1.0)*(TIME_UPUP+12345.12345)/as_PI/2.0)
- #define as_sphereN(uv) (normalize(vec3((uv).xy, sqrt(clamp(1.0-length((uv)), 0.0, 1.0)))))
- #define as_rotatePoint(p,n,theta) (p*cos(theta)+cross(n,p)*sin(theta)+n*dot(p,n) *(1.0-cos(theta)))
- float as_cross2( in vec2 a, in vec2 b ) { return a.x*b.y - a.y*b.x; }
- float as_seedling;
- float as_saw(float x)
- {
- float f = mod(floor(abs(x)), 2.0);
- float m = mod(abs(x), 1.0);
- return f*(1.0-m)+(1.0-f)*m;
- }
- vec2 as_saw(vec2 x)
- {
- return vec2(as_saw(x.x), as_saw(x.y));
- }
- vec3 as_saw(vec3 x)
- {
- return vec3(as_saw(x.x), as_saw(x.y), as_saw(x.z));
- }
- //-----------------SIMPLEX----------------------//
- vec3 as_random3(vec3 c) {
- float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0)));
- vec3 r;
- r.z = fract(512.0*j);
- j *= .125;
- r.x = fract(512.0*j);
- j *= .125;
- r.y = fract(512.0*j);
- return r-0.5;
- }
- float as_simplex3d(vec3 p) {
- const float F3 = 0.3333333;
- const float G3 = 0.1666667;
- vec3 s = floor(p + dot(p, vec3(F3)));
- vec3 x = p - s + dot(s, vec3(G3));
- vec3 e = step(vec3(0.0), x - x.yzx);
- vec3 i1 = e*(1.0 - e.zxy);
- vec3 i2 = 1.0 - e.zxy*(1.0 - e);
- vec3 x1 = x - i1 + G3;
- vec3 x2 = x - i2 + 2.0*G3;
- vec3 x3 = x - 1.0 + 3.0*G3;
- vec4 w, d;
- w.x = dot(x, x);
- w.y = dot(x1, x1);
- w.z = dot(x2, x2);
- w.w = dot(x3, x3);
- w = max(0.6 - w, 0.0);
- d.x = dot(as_random3(s), x);
- d.y = dot(as_random3(s + i1), x1);
- d.z = dot(as_random3(s + i2), x2);
- d.w = dot(as_random3(s + 1.0), x3);
- w *= w;
- w *= w;
- d *= w;
- return dot(d, vec4(52.0));
- }
- //-----------------IMAGINARY--------------------//
- vec2 as_cmul(vec2 v1, vec2 v2) {
- return vec2(v1.x * v2.x - v1.y * v2.y, v1.y * v2.x + v1.x * v2.y);
- }
- vec2 as_cdiv(vec2 v1, vec2 v2) {
- return vec2(v1.x * v2.x + v1.y * v2.y, v1.y * v2.x - v1.x * v2.y) / dot(v2, v2);
- }
- //-----------------RENDERING--------------------//
- float as_zoom;
- vec2 as_invBilinear( in vec2 p, in vec2 a, in vec2 b, in vec2 c, in vec2 d )
- {
- vec2 e = b-a;
- vec2 f = d-a;
- vec2 g = a-b+c-d;
- vec2 h = p-a;
- float k2 = as_cross2( g, f );
- float k1 = as_cross2( e, f ) + as_cross2( h, g );
- float k0 = as_cross2( h, e );
- float w = k1*k1 - 4.0*k0*k2;
- w = sqrt(abs( w ));
- float v1 = ((-k1 - w)/(2.0*k2));
- float v2 = ((-k1 + w)/(2.0*k2));
- float u1 = ((h.x - f.x*v1)/(e.x + g.x*v1));
- float u2 = ((h.x - f.x*v2)/(e.x + g.x*v2));
- bool b1a = v1>0.0 && v1<1.0;
- bool b1b = u1>0.0 && u1<1.0;
- bool b2a = v2>0.0 && v2<1.0;
- bool b2b = u2>0.0 && u2<1.0;
- vec2 res = vec2(min(abs(u1), abs(u2)), min(abs(v1), abs(v2)));
- return (res);
- }
- vec2 as_SinCos( const in float x )
- {
- return vec2(sin(x), cos(x));
- }
- vec3 as_RotateZ( const in vec3 vPos, const in vec2 vSinCos )
- {
- return vec3( vSinCos.y * vPos.x + vSinCos.x * vPos.y, -vSinCos.x * vPos.x + vSinCos.y * vPos.y, vPos.z);
- }
- vec3 as_RotateZ( const in vec3 vPos, const in float fAngle )
- {
- return as_RotateZ( vPos, as_SinCos(fAngle) );
- }
- vec2 as_RotateZ( const in vec2 vPos, const in float fAngle )
- {
- return as_RotateZ( vec3(vPos, 0.0), as_SinCos(fAngle) ).xy;
- }
- mat4 as_RotateZ( const in mat4 vPos, const in float fAngle )
- {
- return mat4(as_RotateZ( vec3(vPos[0].xy, 0.0), as_SinCos(fAngle) ).xy, 0.0, 0.0,
- as_RotateZ( vec3(vPos[1].xy, 0.0), as_SinCos(fAngle) ).xy, 0.0, 0.0,
- as_RotateZ( vec3(vPos[2].xy, 0.0), as_SinCos(fAngle) ).xy, 0.0, 0.0,
- as_RotateZ( vec3(vPos[3].xy, 0.0), as_SinCos(fAngle) ).xy, 0.0, 0.0);
- }
- mat4 as_translate( const in mat4 vPos, vec2 offset )
- {
- return mat4(vPos[0].xy+offset, 0.0, 0.0,
- vPos[1].xy+offset, 0.0, 0.0,
- vPos[2].xy+offset, 0.0, 0.0,
- vPos[3].xy+offset, 0.0, 0.0);
- }
- mat4 as_scale( const in mat4 vPos, vec2 factor )
- {
- return mat4(vPos[0].xy*factor, 0.0, 0.0,
- vPos[1].xy*factor, 0.0, 0.0,
- vPos[2].xy*factor, 0.0, 0.0,
- vPos[3].xy*factor, 0.0, 0.0);
- }
- float as_magnification;
- vec2 as_tree(vec2 uv)
- {
- float w1 = as_saw(as_time);
- mat4 square = mat4(as_EPS, as_EPS, 0.0, 0.0,
- 1.0-as_EPS, as_EPS, 0.0, 0.0,
- 1.0-as_EPS, 1.0-as_EPS, 0.0, 0.0,
- 0.0, 1.0-as_EPS, 0.0, 0.0);
- float size = .5;
- square = as_scale(square, vec2(2.0));
- square = as_translate(square, vec2(-1.0));
- float t1 = as_time;
- //square = as_scale(square, (1.0-as_saw(uv.xy))*vec2(as_saw(t1)+.5));
- square = as_RotateZ(square, as_time);
- square = as_scale(square, .5+.25*vec2(as_simplex3d(vec3(uv.x, uv.y, as_time))));
- square = as_scale(square, vec2(.5));
- square = as_translate(square, vec2(.5));
- float t = as_time;
- vec3 f = as_flux(as_time);
- vec2 dxdy = sqrt(4.0)/iResolution.xy;
- vec2 a = uv+vec2(0.0, 0.0);
- vec2 b = uv+vec2(1.0, 0.0);
- vec2 c = uv+vec2(0.0, 1.0);
- vec2 a2 = as_invBilinear(a, square[0].xy, square[1].xy, square[2].xy, square[3].xy);
- vec2 b2 = as_invBilinear(b, square[0].xy, square[1].xy, square[2].xy, square[3].xy);
- vec2 c2 = as_invBilinear(c, square[0].xy, square[1].xy, square[2].xy, square[3].xy);
- as_magnification = abs(as_cross2(square[1].xy-square[0].xy, square[3].xy-square[0].xy)/as_cross2(b2-a2, c2-a2));
- return as_saw(a2);
- }
- //void mainImage( out vec4 fragColor, in vec2 fragCoord )
- vec4 TIFA_AlienSurface(in vec2 uv){
- const int max_i =4;
- float stretch = 1.0;
- float ifs = 1.0;
- float depth = 0.0;
- float as_zoom = 2.5+1.0*sin(as_time);
- uv = uv*2.0-1.0;
- uv *= as_zoom;
- uv = uv*.5+.5;
- for(int i = 0; i < max_i; i++)
- {
- as_seedling = float(i)/float(max_i);
- vec2 next = as_tree((uv));
- ifs /= clamp(as_magnification+1.0, 0.0, 2.0);
- float weight = pow(ifs, 1.0/float(i+1));
- depth += weight;
- uv = next;//+as_circle(weight);//+uv*(1.0-weight);
- }
- ////FAKE_FRAG_OUT = vec4(uv, 0.0, 1.0);
- float weight = pow(ifs, 1.0/float(max_i));
- float black = smoothstep(0.0, 1.0/5.0, as_saw(depth-as_time));
- float white = smoothstep(4.0/5.0, 1.0, as_saw(depth-as_time));
- //
- // ////FAKE_FRAG_OUT = vec4(uv, 0.0, 1.0)*weight;
- //
- vec4 FAKE_FRAG_OUT = vec4(clamp(weight, 0.0, 1.0)*(as_flux(as_time+depth)*black+white), 1.0);
- return FAKE_FRAG_OUT;
- //return vec4( 0.0 , 0.0, 0.0 , 0.0 );
- }
- </script>
- <script id="FRAG_SHADER_TAIL" type="NOT_JAVASCRIPT">
- void main() {
- ivec4 T_IXYV = GetTile_XYIV();
- vec3 FLAT = RenderTileBase( 2 , 0);
- if(FLAT.x == 0.0){
- gl_FragColor = vec4(0.0, 0.0, 0.0, 0.5);
- return;
- }
- vec3 WARP = RenderTileBase( 0 , 0);
- vec2 F = vec2( FLAT.x, FLAT.y );
- vec2 W = vec2( WARP.x, WARP.y );
- float mix_amount;
- vec4 f1;
- vec4 f2;
- if(T_IXYV.w == 1){
- f1 = TIFA_NeonBeams( F ); //NeonBeams
- f2 = TIFA_NeonBeams( W ); //NeonBeams
- mix_amount = 0.3;
- }else
- if(T_IXYV.w == 2){
- f1 = TIFA_GalacticDance( F ); //GalacticDance
- f2 = TIFA_GalacticDance( W ); //GalacticDance
- mix_amount = 0.6;
- }else{
- f1 = TIFA_AlienSurface( F ); //GalacticDance
- f2 = TIFA_AlienSurface( W ); //GalacticDance
- mix_amount = 0.6;
- }
- f1.w = WARP.z*1.5;
- gl_FragColor = mix( f1, f2, mix_amount );
- return;
- }
- </script>
- <!-- SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS -->
- <!-- SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS -->
- <!-- SSSSSSSSSS SHADER_SECTION END SSSSSSSSSS -->
- </head>
- <!-- HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH -->
- <!-- BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB -->
- <body onload="ON_LOADED_FUNCTION()" >
- <!-- BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB -->
- <h3> Clickable WebGL Tilemap: <h3>
- <p> Author: John Mark Isaac Madison <p>
- <p> Email : J4M4I5M7@hotmail.com <p>
- <canvas id="glCanvas"></canvas>
- <div id="CONTROLS_DIV" style="background:yellow; overflow-visible;">
- <div id="TILE_SIZES_DIV"
- style="float:left;background-color:red; width:300px;" >
- </br>
- <button onClick="PUT_WID();">TILE_WIDTH__IN_PIXELS</button>
- <input type="text" id="INPUT_WID" value="45"
- style="width:32px;"
- >
- </br>
- </br>
- <button onClick="PUT_HIG();">TILE_HEIGHT_IN_PIXELS</button>
- <input type="text" id="INPUT_HIG" value="45"
- style="width:32px;"
- >
- </br>
- SET SIZE OF SINGE TILE ↑
- </div>
- <!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -->
- <div id="DATA_BOUND_SLIDER_CONTAINER_01"
- style=" /* ********************** */
- float:left ;
- width:128px ;
- padding-right:30px ;
- border:1px solid black;
- /* ***************************** */"
- >
- <label> TILE VALUE TO SET: </label>
- <div hidden id="CODE_DIV_01" style="width:0px;">
- //Get Slider:
- var SLIDER = document.getElementById(
- "MY_SLIDER_01"
- );
- if(!SLIDER){ alert("[SLIDER_Get_Fail]");}
- //Get Display:
- var VALUE = document.getElementById(
- "MY_SLIDER_VALUE_01"
- );
- if(!VALUE){ alert("[VAL_DISP_Get_Fail]");}
- //Bind value:
- var V = SLIDER.value;
- VALUE.value = V;
- PUT_UI_TILE_BRUSH_VALUE( V );
- console.log("V==",V);
- </div>
- <input id="MY_SLIDER_01"
- style="width:100%;"
- type="range"
- oninput="/* ******************* */
- cid='CODE_DIV_01';
- var code_div = document.getElementById(cid);
- if(!code_div){alert('[BAD:cid]');}
- code=code_div.innerHTML;
- if(!code){alert('NO_CODE');}
- eval( code );
- /* *************************** */"
- min="0" max="3" value="1" >
- </br>
- <input id="MY_SLIDER_VALUE_01"
- type="text"
- value="1"
- style="width:100%; background-color:#AAAAAA;"
- disabled
- >
- </div>
- <!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -->
- </div>
- <!-- SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS -->
- <script id="UI_SCRIPTS">
- //
- function someFunc(){
- alert("some_func");
- }
- </script>
- <script id="BOILER_PLATE_CODE">
- //BHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBH//
- //This code was added to make an interactive
- //resume for battle-house games.
- //https://www.battlehouse.com/jobs/
- //https://www.indeed.com/jobs?q=WebGl%20&l=Remote&vjk=542ddd54b33610ec
- //All characters are 7 tiles high, but may be
- //Wider or narrower on the x-axis.
- //Padding is NOT included in the glyph patterns.
- var GlyphClass = function(){
- this.WID = 0 ; //:Width in tiles of glyph
- this.HIG = 0 ; //:Height in tiles of glyph
- this.MAP = []; //:Tile values. 0-24 inclusive.
- }
- var TileWriter = function(){
- //:Publically Exposed Functions:
- //:PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP://
- this.LoadMessage = _loadMessage;
- //:PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP://
- //:Values that capital glyphs are drawn with:
- var A = 2;
- var B = 2;
- var C = 2;
- var D = 2;
- var E = 2;
- var F = 2;
- var G = 2;
- var H = 2;
- var I = 2;
- var J = 2;
- var K = 2;
- var L = 2;
- var M = 2;
- var N = 2;
- var O = 2;
- var P = 2;
- var Q = 2;
- var R = 2;
- var S = 2;
- var T = 2;
- var U = 2;
- var V = 2;
- var W = 2;
- var X = 2;
- var Y = 2;
- var Z = 2;
- _A = new GlyphClass();
- _A.WID = 4;
- _A.HIG = 7;
- _A.MAP = [ A,A,A,A,
- A,0,0,A,
- A,A,A,A,
- A,0,0,A,
- A,0,0,A,
- A,0,0,A,
- A,0,0,A ];
- _B = new GlyphClass();
- _B.WID = 4;
- _B.HIG = 7;
- _B.MAP = [ B,B,B,0,
- B,0,0,B,
- B,0,0,B,
- B,B,B,0,
- B,0,0,B,
- B,0,0,B,
- B,B,B,0 ];
- _C = new GlyphClass();
- _C.WID = 4;
- _C.HIG = 7;
- _C.MAP = [ 0,C,C,0,
- C,0,0,C,
- C,0,0,0,
- C,0,0,0,
- C,0,0,0,
- C,0,0,C,
- 0,C,C,0 ];
- _D = new GlyphClass();
- _D.WID = 4;
- _D.HIG = 7;
- _D.MAP = [ D,D,D,0,
- D,0,0,D,
- D,0,0,D,
- D,0,0,D,
- D,0,0,D,
- D,0,0,D,
- D,D,D,0 ];
- _E = new GlyphClass();
- _E.WID = 4;
- _E.HIG = 7;
- _E.MAP = [ E,E,E,E,
- E,0,0,0,
- E,0,0,0,
- E,E,E,0,
- E,0,0,0,
- E,0,0,0,
- E,E,E,E ];
- _F = new GlyphClass();
- _F.WID = 4;
- _F.HIG = 7;
- _F.MAP = [ F,F,F,F,
- F,0,0,0,
- F,0,0,0,
- F,F,F,0,
- F,0,0,0,
- F,0,0,0,
- F,0,0,0 ];
- _G = new GlyphClass();
- _G.WID = 4;
- _G.HIG = 7;
- _G.MAP = [ G,G,G,G,
- G,0,0,0,
- G,0,0,0,
- G,0,G,G,
- G,0,0,G,
- G,0,0,G,
- G,G,G,G ];
- _H = new GlyphClass();
- _H.WID = 4;
- _H.HIG = 7;
- _H.MAP = [ H,0,0,H,
- H,0,0,H,
- H,0,0,H,
- H,H,H,H,
- H,0,0,H,
- H,0,0,H,
- H,0,0,H ];
- _I = new GlyphClass();
- _I.WID = 4;
- _I.HIG = 7;
- _I.MAP = [ I,I,I,I,
- I,I,I,I,
- 0,I,I,0,
- 0,I,I,0,
- 0,I,I,0,
- I,I,I,I,
- I,I,I,I ];
- _J = new GlyphClass();
- _J.WID = 5;
- _J.HIG = 7;
- _J.MAP = [ J,J,J,J,J,
- 0,0,J,0,0,
- 0,0,J,0,0,
- 0,0,J,0,0,
- J,0,J,0,0,
- J,0,J,0,0,
- 0,J,0,0,0];
- _K = new GlyphClass();
- _K.WID = 5;
- _K.HIG = 7;
- _K.MAP = [ K,0,0,0,K,
- K,0,0,K,0,
- K,K,K,0,0,
- K,K,K,0,0,
- K,0,K,0,0,
- K,0,0,K,0,
- K,0,0,0,K];
- _L = new GlyphClass();
- _L.WID = 3;
- _L.HIG = 7;
- _L.MAP = [ L,0,0,
- L,0,0,
- L,0,0,
- L,0,0,
- L,0,0,
- L,0,0,
- L,L,L];
- _M = new GlyphClass();
- _M.WID = 5;
- _M.HIG = 7;
- _M.MAP = [ M,M,M,M,M,
- M,0,M,0,M,
- M,0,M,0,M,
- M,0,M,0,M,
- M,0,M,0,M,
- M,0,0,0,M,
- M,0,0,0,M];
- _N = new GlyphClass();
- _N.WID = 6;
- _N.HIG = 7;
- _N.MAP = [ N,0,0,0,0,N,
- N,N,0,0,0,N,
- N,N,N,0,0,N,
- N,0,N,N,0,N,
- N,0,0,N,N,N,
- N,0,0,0,N,N,
- N,0,0,0,0,N];
- _O = new GlyphClass();
- _O.WID = 4;
- _O.HIG = 7;
- _O.MAP = [ O,O,O,O,
- O,0,0,O,
- O,0,0,O,
- O,0,0,O,
- O,0,0,O,
- O,0,0,O,
- O,O,O,O];
- _P = new GlyphClass();
- _P.WID = 4;
- _P.HIG = 7;
- _P.MAP = [ P,P,P,P,
- P,0,0,P,
- P,P,P,P,
- P,0,0,0,
- P,0,0,0,
- P,0,0,0,
- P,0,0,0];
- _Q = new GlyphClass();
- _Q.WID = 6;
- _Q.HIG = 7;
- _Q.MAP = [ 0,Q,Q,Q,0,0,
- Q,0,0,0,Q,0,
- Q,0,0,0,Q,0,
- Q,0,Q,0,Q,0,
- Q,0,0,Q,Q,0,
- Q,0,0,0,Q,0,
- 0,Q,Q,Q,0,Q];
- _R = new GlyphClass();
- _R.WID = 5;
- _R.HIG = 7;
- _R.MAP = [ R,R,R,0,0,
- R,0,0,R,0,
- R,0,0,R,0,
- R,R,R,0,0,
- R,0,0,R,0,
- R,0,0,0,R,
- R,0,0,0,R];
- _S = new GlyphClass();
- _S.WID = 5;
- _S.HIG = 7;
- _S.MAP = [ 0,S,S,S,S,
- S,0,0,0,0,
- S,0,0,0,0,
- 0,S,S,S,0,
- 0,0,0,0,S,
- 0,0,0,0,S,
- S,S,S,S,0];
- _T = new GlyphClass();
- _T.WID = 5;
- _T.HIG = 7;
- _T.MAP = [ T,T,T,T,T,
- 0,0,T,0,0,
- 0,0,T,0,0,
- 0,0,T,0,0,
- 0,0,T,0,0,
- 0,0,T,0,0,
- 0,0,T,0,0];
- _U = new GlyphClass();
- _U.WID = 5;
- _U.HIG = 7;
- _U.MAP = [ U,0,0,0,U,
- U,0,0,0,U,
- U,0,0,0,U,
- U,0,0,0,U,
- U,0,0,0,U,
- U,0,0,0,U,
- 0,U,U,U,0];
- _V = new GlyphClass();
- _V.WID = 5;
- _V.HIG = 7;
- _V.MAP = [ V,0,0,0,V,
- V,0,0,0,V,
- V,0,0,0,V,
- V,0,0,0,V,
- V,0,0,0,V,
- 0,V,0,V,0,
- 0,0,V,0,0];
- _W = new GlyphClass();
- _W.WID = 5;
- _W.HIG = 7;
- _W.MAP = [ W,0,0,0,W,
- W,0,0,0,W,
- W,0,W,0,W,
- W,0,W,0,W,
- W,0,W,0,W,
- W,0,W,0,W,
- 0,W,W,W,0];
- _X = new GlyphClass();
- _X.WID = 7;
- _X.HIG = 7;
- _X.MAP = [ X,0,0,0,0,0,X,
- 0,X,0,0,0,X,0,
- 0,0,X,0,X,0,0,
- 0,0,0,X,0,0,0,
- 0,0,X,0,X,0,0,
- 0,X,0,0,0,X,0,
- X,0,0,0,0,0,X];
- _Y = new GlyphClass();
- _Y.WID = 5;
- _Y.HIG = 7;
- _Y.MAP = [ Y,0,0,0,Y,
- 0,Y,0,Y,0,
- 0,0,Y,0,0,
- 0,0,Y,0,0,
- 0,0,Y,0,0,
- 0,0,Y,0,0,
- 0,0,Y,0,0];
- _Z = new GlyphClass();
- _Z.WID = 7;
- _Z.HIG = 7;
- _Z.MAP = [ Z,Z,Z,Z,Z,Z,Z,
- Z,0,0,0,0,Z,0,
- 0,0,0,0,Z,0,0,
- 0,0,0,Z,0,0,0,
- 0,0,Z,0,0,0,0,
- 0,Z,0,0,0,0,Z,
- Z,Z,Z,Z,Z,Z,Z];
- _SPACE = new GlyphClass();
- _SPACE.WID = 3;
- _SPACE.HIG = 7;
- _SPACE.MAP = [ 0,0,0,
- 0,0,0,
- 0,0,0,
- 0,0,0,
- 0,0,0,
- 0,0,0,
- 0,0,0];
- //:Sliver of empty space used between
- //:characters of the same word.
- _SLIVER = new GlyphClass();
- _SLIVER.WID = 1;
- _SLIVER.HIG = 7;
- _SLIVER.MAP = [ 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0];
- _DOT = new GlyphClass();
- _DOT.WID = 2;
- _DOT.HIG = 7;
- _DOT.MAP = [ 0,0,
- 0,0,
- 0,0,
- 0,0,
- 0,0,
- 1,1,
- 1,1];
- _COMMA = new GlyphClass();
- _COMMA.WID = 3;
- _COMMA.HIG = 7;
- _COMMA.MAP = [ 0,0,0,
- 0,0,0,
- 0,0,0,
- 0,0,0,
- 0,1,1,
- 0,1,1,
- 1,1,0];
- var _CHAR_MAPS = {}; //:empty object.
- _CHAR_MAPS.A = _A; //: 01 ://
- _CHAR_MAPS.B = _B; //: 02 ://
- _CHAR_MAPS.C = _C; //: 03 ://
- _CHAR_MAPS.D = _D; //: 04 ://
- _CHAR_MAPS.E = _E; //: 05 ://
- _CHAR_MAPS.F = _F; //: 06 ://
- _CHAR_MAPS.G = _G; //: 07 ://
- _CHAR_MAPS.H = _H; //: 08 ://
- _CHAR_MAPS.I = _I; //: 09 ://
- _CHAR_MAPS.J = _J; //: 10 ://
- _CHAR_MAPS.K = _K; //: 11 ://
- _CHAR_MAPS.L = _L; //: 12 ://
- _CHAR_MAPS.M = _M; //: 13 ://
- _CHAR_MAPS.N = _N; //: 14 ://
- _CHAR_MAPS.O = _O; //: 15 ://
- _CHAR_MAPS.P = _P; //: 16 ://
- _CHAR_MAPS.Q = _Q; //: 17 ://
- _CHAR_MAPS.R = _R; //: 18 ://
- _CHAR_MAPS.S = _S; //: 19 ://
- _CHAR_MAPS.T = _T; //: 20 ://
- _CHAR_MAPS.U = _U; //: 21 ://
- _CHAR_MAPS.V = _V; //: 22 ://
- _CHAR_MAPS.W = _W; //: 23 ://
- _CHAR_MAPS.X = _X; //: 24 ://
- _CHAR_MAPS.Y = _Y; //: 25 ://
- _CHAR_MAPS.Z = _Z; //: 26 ://
- _CHAR_MAPS.SPACE = _SPACE;
- _CHAR_MAPS.SLIVER= _SLIVER;
- _CHAR_MAPS.DOT = _DOT;
- _CHAR_MAPS.COMMA = _COMMA;
- function _loadMessage(
- msg,//:Message to write.
- PIX,//:Tile-Data
- WID,//:Width of tile-data
- HIG //:Height of tile-data
- ){
- var cm = null; //character map object.
- var c = "" ; //current char string.
- var os_x = 0; //:offset x.
- var next_os_x = 0;
- var num_chars = msg.length;
- for(var i =0; i < num_chars; i++){
- c = msg.charAt( i );
- if(c==""){
- 慌("[EMPTY_CHARACTER_FOUND]");
- }else
- if(c==" "){
- cm = _CHAR_MAPS.SPACE;
- }else
- if(c=="."){
- cm = _CHAR_MAPS.DOT;
- }else
- if(c==","){
- cm = _CHAR_MAPS.COMMA;
- }else{
- cm = _CHAR_MAPS[ c ];
- }
- _stamp(cm,os_x,0, PIX,WID,HIG);
- //:Next offset origin:
- next_os_x = os_x +
- cm .WID +
- _SLIVER.WID;
- os_x = next_os_x;
- }
- }
- function _stamp(
- cm , //Character map to stamp.
- os_x, //Glyph stamping origin/offset.
- os_y, //Glyph stamping origin/offset.
- PIX , //Data/tilemap to stamp onto.
- //This is RGBA byte array. We
- //Use the red channel for tile values.
- WID , //Width of tilemap.
- HIG //Height of tilemap.
- ){
- if(!WID || !HIG){
- throw("[Problem_WIDHIG]");
- }
- if(!cm){
- throw("[null cm input]");
- }
- if(!cm.WID || !cm.HIG){
- throw("[Glyph_Has_Issues]");
- }
- var max_x = 0;
- var max_y = 0;
- max_x = cm.WID-1;
- max_y = cm.HIG-1;
- var dst_x = 0; //destination X.
- var dst_y = 0; //destination Y.
- var dst_i = 0; //destination INDEX
- var src_i = 0; //source INDEX
- var first_component = 0;
- var sy = 0;
- var tile_val = 0;
- var rgb_val = 0;
- for(var x = 0; x<=max_x; x++){
- for(var y = 0; y<=max_y; y++){
- //:Get source and destination indicies;
- //////////////////////////
- sy = (cm.HIG-1-y); //<<Flip Glyph.
- src_i = (cm.WID*sy)+x;
- dst_x = os_x + x;
- dst_y = os_y + y;
- dst_i = (WID*dst_y)+dst_x;
- //////////////////////////
- //:Convert index value to
- //:RGB:0-255 byte scale:
- tile_val = cm.MAP[ src_i ];
- rgb_val = ( tile_val * 10) + 5;
- //:Must multiply by 4 because there
- //:are 4 bytes per pixel.
- first_component = dst_i * 4;
- //:Put value into tilemap:
- if(tile_val > 0){ //<--No Erasing.
- PIX[ first_component ] = rgb_val;
- }
- }}
- }
- } //:end of tile-writer class.
- //BHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBHBH//
- //The width and height in PIXELS of a
- //single tile on our tile-map:
- GLOBAL_TIL_WID = 64;
- GLOBAL_TIL_HIG = 64;
- var UI_TILE_BRUSH_VALUE = 1; //<--script global
- function PUT_UI_TILE_BRUSH_VALUE( V ){
- if(V < 0 || V > 24){
- alert("[OUT_OF_RANGE]");
- }
- UI_TILE_BRUSH_VALUE = Math.floor( V );
- //alert( UI_TILE_BRUSH_VALUE);
- }
- //SOURCE:https://www.john-smith.me/hassles-with-array-access-in-webgl-and-a-couple-of-workarounds.html
- var HACK_CANVAS = document.createElement('canvas');
- HACK_CANVAS.width = 128*128;
- HACK_CANVAS.height= 1;
- //Global Array Container:
- var GLOBAL_GAC;
- var SCROLL_X = (0-(10*GLOBAL_TIL_WID));
- var SCROLL_Y = 0;
- var SCROLL_X_MAX_TILES = 95;
- var SCROLL_SPEED = 1;
- var FRAME_DELTA = SCROLL_SPEED;
- var TIME_UPUP = 0;
- var TIME_0255 = 0;
- var TIME_2PI = 0.0;
- var TIME_TICKER = 0.0;
- function UPDATE_TIMER(){
- TIME_UPUP = TIME_UPUP + 0.01;
- TIME_0255 += 0.75;
- if(TIME_0255 > 255){ TIME_0255 = 0; }
- TIME_2PI = (TIME_0255 / 255) * (Math.PI * 2);
- //Use cos to modulate value between 0 and 1:
- //To do so, need to offset by 1 and divide
- //by two to transform [-1, 1] range
- //into [0, 1] range.
- TIME_TICKER = ( Math.cos(TIME_2PI) + 1) / 2;
- //console.log( TIME_TICKER);
- SET_ATTR("TIME_TICKER", TIME_TICKER);
- SET_ATTR("TIME_UPUP" , TIME_UPUP );
- //:NOTE: If you scroll by fractional pixels,
- //:you will get artifacts along the seems
- //:between tiles.
- X_MAX = SCROLL_X_MAX_TILES * GLOBAL_TIL_WID;
- SCROLL_X += FRAME_DELTA;
- if( SCROLL_X > X_MAX && FRAME_DELTA > 0){
- FRAME_DELTA = (0-SCROLL_SPEED);
- }else
- if( SCROLL_X <= 0 && FRAME_DELTA < 0){
- FRAME_DELTA = (0+SCROLL_SPEED);
- }
- SCROLL_Y = 0;
- SET_ATTR("SCROLL_X", SCROLL_X);
- SET_ATTR("SCROLL_Y", SCROLL_Y);
- }
- function ON_LOADED_FUNCTION(){
- console.log("[ON_LOADED_FUNCTION]");
- main();
- }
- //:Takes the gl context object, if the input
- //:is null, we likely failed to get the
- //:context.
- function HAS_OPEN_GL_CHECK(gl){
- // Only continue if WebGL is
- // available and working
- if (!gl) {
- var msg = "";
- msg += "[Unable to initialize WebGL.]";
- msg += "[your browser or machine may]";
- msg += "[not support it.]"
- alert( msg );
- return;
- }
- }
- function GET_INPUT_BOX_VALUE( elem_id ){
- var box; //DOM input box
- var val; //Value in input box.
- box = document.getElementById( elem_id );
- val = box.value;
- return (0 + val); //cast to number.
- }
- function PUT_WID(){
- assert_program_and_gl_exist();
- var val = GET_INPUT_BOX_VALUE("INPUT_WID");
- var res = SET_ATTR_UI("TIL_WID_EDIT", val);
- //For click listener on canvas:
- if(res){ GLOBAL_TIL_WID = val; }
- }
- function PUT_HIG(){
- assert_program_and_gl_exist();
- var val = GET_INPUT_BOX_VALUE("INPUT_HIG");
- var res = SET_ATTR_UI("TIL_HIG_EDIT", val);
- //For click listener on canvas:
- if(res){ GLOBAL_TIL_HIG = val; }
- }
- //Integer Version of set-attribute helper:
- function SET_ATTR_INT(gl_var_name, val){
- var loc; //<--location of variable
- loc = gl.getUniformLocation(
- program,
- gl_var_name
- );
- gl.useProgram(program);
- gl.uniform1i(loc, val );
- }
- //Returns TRUE if successful:
- function SET_ATTR(gl_var_name, val){
- var loc; //<--location of variable.
- loc = gl.getUniformLocation(
- program ,
- gl_var_name
- );
- gl.useProgram(program);
- gl.uniform1f(loc, val);
- return true;
- }
- //Version of SET_ATTR used for the
- //User Interface (UI):
- function SET_ATTR_UI(gl_var_name, val){
- if(val < 0 || val > 256 ){
- alert("choose value between 0 to 256");
- //Call was ignored, so return false:
- return false;
- }
- return SET_ATTR(gl_var_name, val);
- }
- function assert_program_and_gl_exist(){
- if(!program){慌("[NO_PROGRAM_EXISTS]");}
- if(!gl ){慌("[NO_GL_EXISTS]");}
- }
- //慌: "disconcerted, be confused, lose one's head"
- //慌: In Code: ~Panic~
- function 慌( panic_message ){
- console.log( panic_message );
- alert ( panic_message );
- throw ( panic_message );
- }
- function makeOpenGlContextUsingCanvas(c){
- //:Try what works in chrome and all the
- //:respectable browsers first:
- gl = c.getContext("webgl");
- if(!gl){
- console.log("[Probably_In_IE]");
- gl = c.getContext("experimental-webgl");
- }else{
- console.log("[Probably_NOT_IE]");
- }
- HAS_OPEN_GL_CHECK( gl );
- return gl;
- }
- function CANVAS_CLICK_FUNCTION(event){
- var mp = getMousePos(canvas,event);
- //Correct to match openGL orientation:
- mp.y = canvas.height - mp.y;
- console.log( mp );
- //:Offset by scroll amount:
- var MP_X = mp.x + SCROLL_X;
- var MP_Y = mp.y + SCROLL_Y;
- //Determine tile clicked on:
- var tx = MP_X / GLOBAL_TIL_WID;
- var ty = MP_Y / GLOBAL_TIL_HIG;
- tx = Math.floor(tx);
- ty = Math.floor(ty);
- console.log("tx:", tx, "ty:", ty);
- //Convert to index:
- var dex;
- dex = (GLOBAL_GAC.WID * ty) + tx;
- //val = GLOBAL_GAC.Get_R( dex );
- //val = GLOBAL_GAC.Get_TileValue( dex );
- val = UI_TILE_BRUSH_VALUE;
- var MAX_TILE_VALUE = 3;
- // No more auto-increment:
- // if(val >= MAX_TILE_VALUE ){
- // val = 0;
- // }else{
- // val = val + 1;
- // }
- if( val < 0 || val > MAX_TILE_VALUE){
- 慌("[Out_Of_Valid_Tile_Value_Bounds]");
- }
- //GLOBAL_GAC.Put_R( dex , val );
- if(val==0){ console.log("[Putting0]");}
- GLOBAL_GAC.Put_TileValue( dex, val );
- //Update The Shader With New TileMap:
- GLOBAL_GAC.pushToGL();
- }
- function getMousePos(GMP_canvas, evt) {
- var rect = GMP_canvas.getBoundingClientRect();
- return {
- x: evt.clientX - rect.left,
- y: evt.clientY - rect.top
- };
- }
- //: No "var" prefix, making them global:
- function initGlobals(){
- canvas = document.querySelector("#glCanvas");
- if(!canvas){
- alert("FAILED_TO_GET_CANVAS");
- }else{
- console.log("[GOT_CANVAS]");
- }
- //Add listener to canvas:
- canvas.addEventListener('click',CANVAS_CLICK_FUNCTION);
- gl = makeOpenGlContextUsingCanvas(canvas);
- //These dimensions are hard-coded into
- //fragment shader code, so be careful
- //about changing them:
- canvas.width = 640;
- canvas.height= 480;
- buffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
- gl.bufferData(
- gl.ARRAY_BUFFER,
- new Float32Array([
- -1.0, -1.0,
- 1.0, -1.0,
- -1.0, 1.0,
- -1.0, 1.0,
- 1.0, -1.0,
- 1.0, 1.0]),
- gl.STATIC_DRAW
- );
- //G == Global Container.
- //To fix problems with rendering in I.E.
- //(Internet Explorer)
- //GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG//
- var G = {};
- G.canvas = canvas;
- G.gl = gl;
- G.buffer = buffer;
- if( ! G.canvas ||
- ! G.gl ||
- ! G.buffer ){
- 慌("[Global_Container_Broken]");
- }
- return G;
- //GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG//
- }
- function main(){
- G = initGlobals();
- HAS_OPEN_GL_CHECK( G );
- gl.viewport(0,0,gl.drawingBufferWidth ,
- gl.drawingBufferHeight);
- setup();
- render();
- SETUP_TILE_MAP_DATA();
- _initMessage();
- }
- function _initMessage(){
- var TW = new TileWriter();
- var PIX = GLOBAL_GAC.PIX;
- var WID = GLOBAL_GAC.WID;
- var HIG = GLOBAL_GAC.HIG;
- TW.LoadMessage("HELLO MATT",PIX,WID,HIG);
- GLOBAL_GAC.pushToGL();
- }
- //Need texture/buffer container: 斗
- var GLArrayContainer = function(){
- var _self = this;
- //Max is 245 because:
- //Math.floor( 245/10 ) --> 24.
- //We are only using 24 tile values.
- var _MAX_RGB_VAL = 245;
- //web-gl texture handle:
- this.TEX = null;
- //Pixel Array:
- this.PIX = null;
- //Number of pixels in Pixel Array:
- this.PIX_NUM = 0;
- this.WID = 0; //width in pixels.
- this.HIG = 0; //height in pixels. Sure pixels?
- //Publically Exposed Functions:
- //PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP//
- this.setPixelByIndex = _setPixelByIndex;
- this.makePattern = _makePattern ;
- this.solidFill = _solidFill ;
- this.pushToGL = _pushToGL ;
- this.Put_TileValue = _put_TileValue ;
- this.Get_TileValue = _get_TileValue ;
- this.Get_R = _get_R;
- this.Put_R = _put_R;
- //PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP//
- //: i == index.
- function _get_R( i ){
- var RED_INDEX = 0;
- var r = _self.PIX[(i*4) + RED_INDEX];
- return r;
- }
- //: i == index.
- function _put_R( i, val){
- var RED_INDEX = 0;
- _self.PIX[(i*4) + RED_INDEX] = val;
- }
- //Put a tile-value by INDEX:
- function _get_TileValue( i ){
- var _0245 = _get_R( i );
- var TV = _0245_to_index24( _0245 );
- return TV;
- }
- //Get a tile-value by INDEX:
- function _put_TileValue( i, TV ){
- var _0245 = _index24_to_0245( TV );
- _put_R( i, _0245 );
- }
- //////////////////////////////////////////
- //Array stores RGB values in 0-255 range,
- //But we only want to store 24 tile
- //values. We need to spread out the
- //values, since OpenGL is going to normalize
- //the values into [0.0 to 1.0] range,
- //Spreading them out will make it easier
- //to determine what tile values you have
- //when you get to the fragment shader.
- //////////////////////////////////////////
- //Convert RGB 0-255 value to range 0-24
- function _0245_to_index24( _0245 ){
- if(!(_0245>=0 && _0245 <= 245)){
- 慌("[0245_INPUT_OOB]:"+_0245);
- }
- var op = Math.floor( _0245/10 );
- if(op < 0 || op > 24){
- 慌("[BAD_OUTPUT:RGB->DEX24]:" + op);
- }
- return op;
- }
- //Convert 0-24 to RGB 0-255
- function _index24_to_0245( _index24 ){
- if( !(_index24>=0 && _index24<=24)){
- 慌("[INPUT_0-24_OUT_OF_RANGE]:" + _index24);
- return 0;
- }
- //Multiply by 10 to maximize spacing
- //with an easy to comprehend value.
- //Add 5 so when values are normalized,
- //our conversion calculations wont
- //get #EDGE_MISCALCULATIONS#
- //example: 0 --> 5
- //example: 1 --> 15
- var op = ((_index24*10)+5);
- if(op < 0 || op > 245){
- 慌("[Calcs_Are_Wrong]");
- }
- return op;
- }
- //////////////////////////////////////////
- function _setPixelByIndex(i, r,g,b, a){
- //Make sure RGB is in valid range:
- //////////////////////////////
- if(!(r>=0 && r<=_MAX_RGB_VAL)){
- 慌("[SPBI:R-OOB]" + r);
- }
- if(!(g>=0 && g<=_MAX_RGB_VAL)){
- 慌("[SPBI:G-OOB]" + g);
- }
- if(!(b>=0 && b<=_MAX_RGB_VAL)){
- 慌("[SPBI:B-OOB]" + b);
- }
- //////////////////////////////
- //Make sure alpha is in valid range:
- //Lets use same Cap for consistency.
- //////////////////////////////////////
- if(!(a>=0 && a<=_MAX_RGB_VAL)){
- 慌("[SPBI:A-OOB]" + a);
- }
- //////////////////////////////////////
- //Multiply by 4 to get address
- //of first component of pixel [i]
- //Because each pixel is 4-bytes:
- var b = i*4;
- //RGBA (alpha last) (NOT : argb)
- _self.PIX[ b + 0 ] = r;
- _self.PIX[ b + 1 ] = g;
- _self.PIX[ b + 2 ] = b;
- _self.PIX[ b + 3 ] = a;
- }
- function _solidFill(){
- //PIX_NUM: Number of pixels on buffer.
- if(_self.PIX_NUM <= 0){
- 慌("[BAD_PIX_NUM]");
- }
- var r = 245;
- var g = 22;
- var b = 64;
- var a = _MAX_RGB_VAL;
- for(var i = 0; i < _self.PIX_NUM; i++){
- _setPixelByIndex(i,r,g,b,a);
- }
- }
- //Make a checker pattern:
- function _makePattern(){
- var EVERY_OTHER = false;
- var i_r = 0;
- var i_g = 0;
- var i_b = 0;
- for(var i = 0; i < _self.PIX_NUM; i++){
- EVERY_OTHER = (!EVERY_OTHER);
- if( EVERY_OTHER ){
- i_r = _MAX_RGB_VAL;
- i_g = _MAX_RGB_VAL;
- i_b = _MAX_RGB_VAL;
- }else{
- //alert("HEY");
- i_r = 0;
- i_g = 0;
- i_b = 0;
- }
- _setPixelByIndex(
- // DEX| R | G | B | ALPHA //////
- i, i_r, i_g, i_b,_MAX_RGB_VAL
- );
- }
- }
- //Push changes to Web-GL so fragment
- //shader can use the values:
- function _pushToGL(){
- if(!_self.TEX){慌("[TEX_PROBLEM]");}
- console.log("[push_to_gl]");
- gl.activeTexture( gl.TEXTURE1 );
- gl.bindTexture(
- gl.TEXTURE_2D,
- _self.TEX
- );
- //Will get error:
- //[ WebGL: INVALID_OPERATION: ]
- //[ texParameter: no texture bound to target ]
- //If no texture is bound to active slot before doing this.
- //:SOURCE:https://stackoverflow.com/questions/42358462/no-texture-bound-to-the-unit-0
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
- //:will also throw error if no texture
- //in active texture slot:
- gl.texImage2D(
- gl.TEXTURE_2D ,
- 0 , //LEVEL
- gl.RGBA , //internalFormat,
- _self.WID ,
- _self.HIG ,
- 0 , //border
- gl.RGBA , //srcFormat
- gl.UNSIGNED_BYTE , //srcType
- _self.PIX //<--ArrayBufferView object
- );
- SET_ATTR_INT(
- "HAS_TEXTURE_BEEN_PUSHED_FROM_JAVASCRIPT",
- 1 );
- //Let shader know tile-map width and height:
- SET_ATTR_INT("CMAP_WID", _self.WID);
- SET_ATTR_INT("CMAP_HIG", _self.HIG);
- }
- }
- TILE_MAP_HAS_BEEN_SETUP = false;
- function SETUP_TILE_MAP_DATA(){
- if(TILE_MAP_HAS_BEEN_SETUP){
- alert("[MAP_ALREADY_SETUP]");
- return;
- }
- TILE_MAP_HAS_BEEN_SETUP = true;
- //Decided on 128x128:
- //Because it is maximum dimensions of
- //a project I am working on.
- const WID = 128;
- const HIG = 128;
- //4 components/bytes in 1 ARGB pixel:
- const NUM_COM_ARGB = 4; //4;
- const ARRAY_LENGTH = WID*HIG*NUM_COM_ARGB;
- const pixel = new Uint8Array( ARRAY_LENGTH );
- //Create Texure slot at TEXTURE1 because
- //seems to be bug "there is no texture bound to unit 0"
- //in chrome.
- gl.activeTexture(gl.TEXTURE1);
- const texture = gl.createTexture();
- if(!texture){慌("[NOT_TEXTURE]");}
- //Our texture sampler needs to be bound to
- //the same texture slot. Hence the "1"
- //https://www.john-smith.me/hassles-with-array-access-in-webgl-and-a-couple-of-workarounds.html
- gl.uniform1i(
- gl.getUniformLocation(
- program ,
- "u_texture" //<--Sampler2D's Name.
- ),
- 1 //<--Texture Slot To Bind To.
- );
- //Our pixel array is 4-component, so we
- //can use alignment 4. An alignment of
- //1 will also work.
- //SOURCE: https://webglfundamentals.org/webgl/lessons/webgl-data-textures.html
- const alignment = 4;
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, alignment);
- //Populate our helper container:
- //PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP//
- GLOBAL_GAC = new GLArrayContainer();
- GLOBAL_GAC.PIX = pixel;
- GLOBAL_GAC.TEX = texture;
- GLOBAL_GAC.PIX_NUM = WID*HIG;
- GLOBAL_GAC.WID = WID;
- GLOBAL_GAC.HIG = HIG;
- //GLOBAL_GAC.makePattern();
- GLOBAL_GAC.solidFill();
- GLOBAL_GAC.pushToGL();
- //setTimeout( 1, GLOBAL_GAC.pushToGL() );
- //PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP//
- }
- function setup(){
- var frag_src = "";
- var frag_dom_H = document.getElementById(
- "FRAG_SHADER_HEAD"
- );
- var frag_dom_T = document.getElementById(
- "FRAG_SHADER_TAIL"
- );
- var DOM_R01 = document.getElementById(
- "FRAG_SHADER_TILE_R01"
- );
- var DOM_R02 = document.getElementById(
- "FRAG_SHADER_TILE_R02"
- );
- var DOM_R03 = document.getElementById(
- "FRAG_SHADER_TILE_R03"
- );
- var FRAG_SRC_HEAD = frag_dom_H.text;
- var FRAG_SRC_TAIL = frag_dom_T.text;
- var FRAG_SRC_R_01 = DOM_R01.text;
- var FRAG_SRC_R_02 = DOM_R02.text;
- var FRAG_SRC_R_03 = DOM_R03.text;
- var frag_src =
- FRAG_SRC_HEAD +
- FRAG_SRC_R_01 +
- FRAG_SRC_R_02 +
- FRAG_SRC_R_03 +
- FRAG_SRC_TAIL ;
- //console.log( frag_src );
- F = createShader(
- gl,gl.FRAGMENT_SHADER, frag_src
- );
- var vert_dom = document.getElementById("VERT_SHADER");
- var vert_src = vert_dom.text;
- console.log( vert_src );
- V = createShader(
- gl, gl.VERTEX_SHADER, vert_src
- );
- //**** MAKE "program" a GLOBAL VAR ****//
- program = createProgram(gl,V,F);
- gl.useProgram( program );
- if(!program){ 慌("[PROGRAM_IS_NULL]");}
- }
- function render(){
- window.requestAnimationFrame(render,canvas);
- // Set clear color to black, fully opaque
- gl.clearColor(0.0, 0.0, 0.5, 1.0);
- // Clear the color buffer with specified clear color
- gl.clear(gl.COLOR_BUFFER_BIT);
- //Directly before call to gl.drawArrays:
- positionLocation = gl.getAttribLocation(program, "a_position");
- gl.enableVertexAttribArray( positionLocation );
- gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
- UPDATE_TIMER();
- gl.drawArrays(gl.TRIANGLES, 0, 6);
- }
- function createShader(gl,type,source){
- //:Error Check For Bad Inputs:
- if(!gl ){慌("[NULL_GL]");}
- if(!type ){慌("[NULL_TY]");}
- if(!source){慌("[NULL_SR]");}
- var shader = gl.createShader(type);
- gl.shaderSource(shader, source);
- gl.compileShader(shader);
- var res = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
- if( res ){
- console.log("[SHADER_COMPILED!]");
- return shader;
- }
- console.log(gl.getShaderInfoLog(shader));
- gl.deleteShader(shader);
- 慌("[FAILED_TO_COMPILE_SHADER]");
- }
- //:gl : openGL context :
- //:vert: vertex shader :
- //:frag: fragment shader:
- function createProgram(gl,vert, frag){
- var program = gl.createProgram();
- gl.attachShader(program, vert);
- gl.attachShader(program, frag);
- gl.linkProgram (program);
- var res = gl.getProgramParameter(program, gl.LINK_STATUS);
- if( res ){
- console.log("[PROGRAM_CREATED!]");
- return program;
- }
- console.log(gl.getProgramInfoLog(program));
- gl.deleteProgram(program);
- }
- </script>
- <!-- SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS -->
- <!-- BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB -->
- </body>
- </html>
- <!-- ####### #EDGE_MISCALCULATIONS# ######### -->
- <!--
- Above text taken from my daily log (data.xml):
- SPECIFICALLY ENTRY: | 09_25PM |
- | 2017_11_30 |
- | ENTRY_0107 |
- Why the 0.5 ?
- ASCII_DIAGRAM[ ******************************
- Your texture coordinates go like this
- 0 0.25 0.5 0.75 《- texture coords you're computing
- | | | |
- V V V V
- +-----+-----+-----+-----+
- | | | | |
- | | | | | 《- texels
- +-----+-----+-----+-----+
- But the texture coords you want are like this
- 0.125 0.375 0.625 0.875
- | | | |
- V V V V
- +-----+-----+-----+-----+
- | | | | |
- | | | | | 《- texels
- +-----+-----+-----+-----+
- The easiest fix is to add 1/2 a texel
- ]ASCII_DIAGRAM ******************************
- URL[
- https://stackoverflow.com/questions/27414701/
- webgl-three-js-particles-colored-by-texture-
- flicker-erratically-while-moving/27439675#27439675
- ]URL
- -->
Add Comment
Please, Sign In to add comment