Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** ************************************************ ***
- *** ***
- *** EASY_SOURCE_CODE_LINK: tinyurl.com/SDF-010 ***
- *** DIRECT_LINK_TO_SOURCE: pastebin.com/rE8av22k ***
- *** DEMO_LINK: shadertoy.com/view/WsKBzz ***
- *** ***
- *** About: Voxel Rendering For Patent Drawings. ***
- *** This version is a work in progress. ***
- *** ***
- *** ************************************************ **/
- //:MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM://
- /** M: Macros Section **/
- #define V_4 vec4
- #define V_3 vec3
- #define V_2 vec2
- #define F32 float
- #define I32 int
- /** M: Macros Section **/
- //:MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM://
- //:DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD://
- /** D: Data Section **/
- //:DEBUGGING:====================================://
- /** Nothing here yet **/
- //:====================================:DEBUGGING://
- //:CONFIGURATION:================================://
- /** SLICE_RENDER_USING_CAMERA_PLANE **/
- int CFG_SLICE_RENDER=( 1 );
- //:================================:CONFIGURATION://
- //:RAYMARCHING_AND_VOXEL_CONSTANTS:==============://
- //:These defines are for ray marching when using
- //:fragment coordinates as our coordinate space.
- #define MAX_STE 1000 //:Max Step
- #define MIN_DIS 0.02 //:Min Dist (SurfaceDist)
- #define MAX_DIS 1000.0 //:Max Dist
- //:Number of tiles on each axis:
- #define NTX 8 //:Num_Tiles ( in_voxel_map )
- #define NTY 4 //:Num_Tiles ( in_voxel_map )
- #define NTZ 3 //:Num_Tiles ( in_voxel_map )
- //:Size_Of_A_Voxel_Tile_Measured_In_Pixels:
- #define NPX 16 //:Num_Pixels_X( in_a_tile )
- #define NPY 16 //:Num_Pixels_Y( in_a_tile )
- #define NPZ 16 //:Num_Pixels_Z( in_a_tile )
- //:Total__number_of__Pixels__in_entire_voxel_map
- #define TPX ( NTX * NPX ) //: Total_Pixels_X
- #define TPY ( NTY * NPY ) //: Total_Pixels_Y
- #define TPZ ( NTZ * NPZ ) //: Total_Pixels_Z
- #define _ 0
- #define X 1
- int VAT[ NTX * NTY * NTZ ]=int[ 8 * 4 * 3 ](
- /** TODO: Eventually use an integer texture **/
- /** for this tilemap data. **/
- //:Highest Z Cross Section Is First Tile Map
- //: 1 2 3 4 5 6 7 8 --- -----------------
- X,X,X,X,X,X,X,X, //: 1 | ^
- X,_,_,_,_,_,_,X, //: 2 | Z == 0 |
- X,_,_,_,_,_,_,X, //: 3 | |
- X,X,X,X,X,X,X,X, //: 4 | |
- //: --- |
- //: 1 2 3 4 5 6 7 8 --- Cross Sections
- X,_,_,_,_,_,_,X, //: 1 | Can be thought
- _,_,_,_,_,_,_,_, //: 2 | of as differ-
- _,_,_,_,_,_,_,_, //: 3 | -ent 2D
- X,_,_,_,_,_,_,X, //: 4 | tilemaps.
- //: --- |
- //: 1 2 3 4 5 6 7 8 --- |
- X,_,_,_,_,_,_,X, //: 1 | |
- X,_,_,_,_,_,_,X, //: 2 | Z == 2 |
- X,_,_,_,_,_,_,X, //: 3 | |
- X,_,_,_,_,_,_,X //: 4 | V
- //: --- -----------------
- );
- #undef _
- #undef X
- //:==============:RAYMARCHING_AND_VOXEL_CONSTANTS://
- //:STRUCTS:======================================://
- //:RWC_AND_RWN:------------------------------://
- struct RWC_AND_RWN{
- V_3 rwC ;
- V_3 rwN;
- };
- //:------------------------------:RWC_AND_RWN://
- //:VOX_000:------------------------------://
- /** Current Voxel Information **/
- struct VOC{ //:VOC: VOX_CUR (VoxelCurrent)
- uint has; //: voxel:exists?
- int val; //: voxel:tile_value
- int dex; //: voxel:1d_index
- int t_x; //: voxel:tile_x
- int t_y; //: voxel:tile_y
- int t_z; //: voxel:tile_z
- };
- /** Distance Information **/
- struct VOD{ //:VOD: VOX_DIS (VoxelDistance)
- F32 nex; //:Distance_To_Next
- // F32 d_S; //:Distance_To_Surface
- // //:Within_Current_Voxel
- };
- struct VOX_000{
- VOC vox_cur; //:Voxel_Current_Info
- VOD vox_dis; //:Voxel_Distance_Info
- };
- //:------------------------------:VOX_000://
- //:======================================:STRUCTS://
- /** D: Data Section **/
- //:DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD://
- //:IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII://
- /** I: Inifnite Degree Functions. Always At Top. **/
- //:NO_HALT_USE_ERROR_PIXEL:======================://
- //:PIX_ERR:======================================://
- #define ERR_001 1 /** msg_err # 1 **/
- #define ERR_002 2 /** msg_err # 2 **/
- V_4 PIX_ERR(
- int msg_err /** EX: MSG_ERR_001 **/
- , V_4 pix_err /** Previous Pixel Error **/
- , int o_k
- )
- {
- /** Muddy Pastel Yellow For When You **/
- /** forget to set the pix_err_out value. **/
- V_4 pix_err_out=V_4(0.5,0.5,0.2,1);
- float flux=( mod(iTime,1.0) );
- //:COLOR_PICKER_DEBUG_HEX:---------------://
- /** Make it easy to find source of error **/
- /** in code by using color picker on **/
- /** shader output and then doing a **/
- /** CTRL+F with that hex code to find **/
- /** the offending source code. **/
- F32 _ =F32( 0.0 );
- F32 MAX =F32( 255.0 ); // MaxIntensity
- F32 A =F32( 1.0 ); // Alpha_Max
- //:ERROR_CODE_STROBE_COLORS:---------://
- F32 _01_ =( F32(0x01) / MAX );
- F32 _10_ =( F32(0x10) / MAX );
- //
- F32 _02_ =( F32(0x02) / MAX );
- F32 _20_ =( F32(0x20) / MAX );
- //
- F32 _03_ =( F32(0x03) / MAX );
- F32 _30_ =( F32(0x30) / MAX );
- //
- F32 _04_ =( F32(0x04) / MAX );
- F32 _40_ =( F32(0x40) / MAX );
- //
- F32 _05_ =( F32(0x05) / MAX );
- F32 _50_ =( F32(0x50) / MAX );
- //
- F32 _06_ =( F32(0x06) / MAX );
- F32 _60_ =( F32(0x60) / MAX );
- //
- F32 _07_ =( F32(0x07) / MAX );
- F32 _70_ =( F32(0x70) / MAX );
- //
- F32 _08_ =( F32(0x08) / MAX );
- F32 _80_ =( F32(0x80) / MAX );
- //
- F32 _09_ =( F32(0x09) / MAX );
- F32 _90_ =( F32(0x90) / MAX );
- V_4 _0x010101_ = V_4(_01_,_01_,_01_, A);
- V_4 _0x101010_ = V_4(_10_,_10_,_10_, A);
- V_4 _0x020202_ = V_4(_02_,_02_,_02_, A);
- V_4 _0x202020_ = V_4(_20_,_20_,_20_, A);
- V_4 _0x030303_ = V_4(_03_,_03_,_03_, A);
- V_4 _0x303030_ = V_4(_30_,_30_,_30_, A);
- V_4 _0x040404_ = V_4(_04_,_04_,_04_, A);
- V_4 _0x404040_ = V_4(_40_,_40_,_40_, A);
- V_4 _0x050505_ = V_4(_05_,_05_,_05_, A);
- V_4 _0x505050_ = V_4(_50_,_50_,_50_, A);
- V_4 _0x060606_ = V_4(_06_,_06_,_06_, A);
- V_4 _0x606060_ = V_4(_60_,_60_,_60_, A);
- V_4 _0x070707_ = V_4(_07_,_07_,_07_, A);
- V_4 _0x707070_ = V_4(_70_,_70_,_70_, A);
- V_4 _0x080808_ = V_4(_08_,_08_,_08_, A);
- V_4 _0x808080_ = V_4(_80_,_80_,_80_, A);
- V_4 _0x090909_ = V_4(_09_,_09_,_09_, A);
- V_4 _0x909090_ = V_4(_90_,_90_,_90_, A);
- //:---------:ERROR_CODE_STROBE_COLORS://
- //:ERROR_ZERO_COLORS:----------------://
- #define F float
- #define V vec4
- /** If you forget to set error code, **/
- /** you will see flashing red and **/
- /** blue. ( _0xFF0666_ & _0x6660FF_ )**/
- F _FF_=( F32(0xFF) / MAX );
- // _06_=( F32(0x06) / MAX );
- // _60_=( F32(0x60) / MAX );
- F _66_=( F32(0x66) / MAX );
- V _0xFF0666_ =V_4(_FF_,_06_,_66_,A);
- V _0x6660FF_ =V_4(_66_,_60_,_FF_,A);
- #undef F
- #undef V
- //:----------------:ERROR_ZERO_COLORS://
- //:BAD_OK_ERROR_COLOR:---------------://
- #define F float
- #define V vec4
- /** You will see this if you init **/
- /** o_k to a value other than 1 in **/
- /** your source code. **/
- /** Strobes between orange and lime. **/
- /** ( _0xFF7700_ & _0x77FF00_ ) **/
- // _FF_=( F32(0xFF) / MAX );
- F _77_=( F32(0x77) / MAX );
- F _00_=( F32(0x00) / MAX );
- V _0xFF7700_ =V_4(_FF_,_77_,_00_,A);
- V _0x77FF00_ =V_4(_77_,_FF_,_00_,A);
- #undef F
- #undef V
- //:---------------:BAD_OK_ERROR_COLOR://
- //:---------------:COLOR_PICKER_DEBUG_HEX://
- if( o_k <= 0 ){
- pix_err_out = pix_err;
- }else
- if( 1 == o_k ){
- /** table of error "messages" #0 **/
- V_4 tab_err_000[10]=V_4[10](
- // 0: Invalid Error Code
- _0xFF0666_ // RED_FLASH
- // Odd Frame Error Colors:
- , _0x010101_ // ERR_001 : ODD_FRAME
- , _0x020202_ // ERR_002 : ODD_FRAME
- , _0x030303_ // ERR_003 : ODD_FRAME
- , _0x040404_ // ERR_004 : ODD_FRAME
- , _0x050505_ // ERR_005 : ODD_FRAME
- , _0x060606_ // ERR_006 : ODD_FRAME
- , _0x070707_ // ERR_007 : ODD_FRAME
- , _0x080808_ // ERR_008 : ODD_FRAME
- , _0x090909_ // ERR_009 : ODD_FRAME
- );;
- /** table of error "messages" #1 **/
- V_4 tab_err_001[10]=V_4[10](
- // 0: Invalid Error Code
- _0x6660FF_ // BLUE_FLASH
- // Even Frame Error Colors:
- , _0x101010_ // ERR_001 : EVE_FRAME
- , _0x202020_ // ERR_002 : EVE_FRAME
- , _0x303030_ // ERR_003 : EVE_FRAME
- , _0x404040_ // ERR_004 : EVE_FRAME
- , _0x505050_ // ERR_005 : EVE_FRAME
- , _0x606060_ // ERR_006 : EVE_FRAME
- , _0x707070_ // ERR_007 : EVE_FRAME
- , _0x808080_ // ERR_008 : EVE_FRAME
- , _0x909090_ // ERR_009 : EVE_FRAME
- );;
- if( mod(iTime*16.0,2.0) < 1.0 ){
- pix_err_out =( tab_err_000
- [ msg_err ] );;
- }else{
- pix_err_out =( tab_err_001
- [ msg_err ] );;
- };;
- }else{
- /** Orange strobe for an o_k value **/
- /** that is NOT expected. (o_k >= 2) **/
- if( mod(iTime*2.0,2.0) < 1.0 ){
- pix_err_out = _0xFF7700_; // ORANGE
- }else{
- pix_err_out = _0x77FF00_; // LIME
- };;
- };;
- return( pix_err_out );
- }
- //:======================================:PIX_ERR://
- //:======================:NO_HALT_USE_ERROR_PIXEL://
- /** I: Inifnite Degree Functions. Always At Top. **/
- //:IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII://
- //:22222222222222222222222222222222222222222222222222://
- //:INTERPOLATION_3D:=============================://
- //:sfd_i3d:======================================://
- V_3 sdf_i3d(
- /**/V_3 _1_
- , V_3 _2_
- , F32 f_p
- ){
- return( _1_ + ( ( _2_ - _1_ )*f_p ) );
- }
- //:=============================:INTERPOLATION_3D://
- //:======================================:sfd_i3d://
- //:FIRST_VOXEL_QUERY_NEEDED:=====================://
- #define T_X vox_000.vox_cur.t_x
- #define T_Y vox_000.vox_cur.t_y
- #define T_Z vox_000.vox_cur.t_z
- /** rwN used to find distance to NEXT voxel. **/
- VOX_000
- GET_vox_000_USE_xyz_rwN(
- V_3 xyz
- , V_3 rwN
- )
- {
- VOX_000 vox_000;
- //:CURRENT_VOXEL_CELL:-------------------://
- vox_000.vox_cur.val = int( 0 - 1 );
- T_X = ( int(floor( xyz.x / float( NPX ))));
- T_Y = ( int(floor( xyz.y / float( NPY ))));
- T_Z = ( int(floor( xyz.z / float( NPZ ))));
- //:-------------------:CURRENT_VOXEL_CELL://
- //:GET_VOXEL_CELL_VALUE:-----------------://
- /** Voxel Volume Is Hard Coded To have **/
- /** voxel__tile[0,0,0] stuck at world **/
- /** world_coord[0,0,0] **/
- if( T_X >= 0 && T_X < NTX
- && T_Y >= 0 && T_Y < NTY
- && T_Z >= 0 && T_Z < NTZ
- ){
- //: Index2D -and- Index3D
- int D2D = T_X + ( NTX * T_Y );
- int D3D = D2D + ( NTX * NTY * T_Z );
- vox_000.vox_cur.dex=( D3D );
- vox_000.vox_cur.val=( VAT[ D3D ] );
- /** NOTE: Voxel value 0 will NOT get **/
- /** special treatment. It could**/
- /** contain geometry if we **/
- /** really wanted it to. **/
- vox_000.vox_cur.has=uint( 1 );
- }else{
- /** Using a "has" flag so we **/
- /** can keep more logic UNSIGNED. **/
- /** (No need for negative value to ) **/
- /** (be used for invalid dex or val) **/
- vox_000.vox_cur.dex= int( 0 );
- vox_000.vox_cur.val= int( 0 );
- vox_000.vox_cur.has=uint( 0 );
- };;
- //:-----------------:GET_VOXEL_CELL_VALUE://
- //:TODO: Figure out distance to the NEXT voxel.
- return( vox_000 );
- }
- #undef T_X
- #undef T_Y
- #undef T_Z
- //:=====================:FIRST_VOXEL_QUERY_NEEDED://
- //:INTEGER_MODULO:===============================://
- #ifndef I32
- #define I32 int
- #endif
- /** PRIVATE: Called only by I32_MOD **/
- I32 i32_mod_neg( I32 neg_a , I32 pos_d ){
- /** ************************************ ***
- Function Calculates:
- FIXED: (d-1)-[ mod(abs(a)+1 , d) ]
- GOAL: Negatives keep exact same tiling
- pattern as the positives.
- IN : -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6
- OUT: 2 3 0 1 2 3 0 1 2 3 0 1 2
- 0123 -> 0123 -> 0123 -> 0123 -> 0123 -> ect
- *** ************************************ **/
- I32 pos_a = ( 0 - neg_a ) + 1;
- F32 A = F32( pos_a );
- F32 D = F32( pos_d );
- //: GLSL_MODULUS_WITH_INTEGERS_FORMULA
- //: WARD:Wholepart,All,Remainder,Divisor
- int W = int( trunc( A / ( D )) );
- int R = int( A - ( F32(W) * D ) );
- return( (pos_d-1) - R );
- /// I32 pos_a = 0 - neg_a; //:make "a" positive.
- ///
- /// F32 A = F32( pos_a );
- /// F32 D = F32( pos_d );
- ///
- /// //: GLSL_MODULUS_WITH_INTEGERS_FORMULA
- /// //: WARD:Wholepart,All,Remainder,Divisor
- /// int W = int( trunc( A / ( D )) );
- /// int R = int( A - ( F32(W) * D ) );
- ///
- /// return( pos_d - R );
- }
- I32
- I32_MOD(
- /**/I32 a /** ALL : CAN BE NEGATIVE **/
- , I32 d /** DIVISOR : ALWAYS POSITIVE **/
- ){
- /** ************************************ ***
- Allow for I32_MOD to be used for wrapping
- even when the input to wrap[ a ] goes
- negative. d should always be positive.
- EX: mod( x , 2 ) , where x == -1
- | -1 | 0 [ 1 ] 2 |
- | 1 | 0 [ 1 ] 0 | 1 | 0 |
- d + 1 == 2 + (-1) == 1
- *** ************************************ **/
- //
- int R;
- if( a < 0 ){
- //:This is CLOSE but then new problem
- //:of lots of green checkers is showing
- //:up. No clue...
- R = i32_mod_neg( a , d );
- //// a = d + a;
- ////
- //// F32 A = F32( a );
- //// F32 D = F32( d );
- ////
- //// //: WARD:Wholepart,All,Remainder,Divisor
- //// int W = int( trunc( A / ( D )) );
- //// R = int( A - ( F32(W) * D ) );
- }else{
- F32 A = F32( a );
- F32 D = F32( d );
- //: GLSL_MODULUS_WITH_INTEGERS_FORMULA
- //: WARD:Wholepart,All,Remainder,Divisor
- int W = int( trunc( A / ( D )) );
- R = int( A - ( F32(W) * D ) );
- };;
- #ifndef F32
- #define F32 float
- #endif
- //:EXAMPLE: I32_MOD( 9,2 )
- //: W == 4
- //: R == 1 (9 - ( 4 * 2 ) )
- return( R );
- }
- //:===============================:INTEGER_MODULO://
- //:22222222222222222222222222222222222222222222222222://
- //:11111111111111111111111111111111111111111111111111://
- //:FRAGCOORD_TO_FRAGPERCENT:=====================://
- //:f_c_CTO_f_p:==================================://
- V_2 f_c_CTO_f_p( V_2 f_c ){
- V_2 f_p;
- f_p = f_c / ( iResolution.xy - 1.0 );
- return( f_p );
- }
- //:==================================:f_c_CTO_f_p://
- //:=====================:FRAGCOORD_TO_FRAGPERCENT://
- //:FRAGPER_TO_CAMERA_RAY:========================://
- //:f_p_CTO_rwC_AND_rwN:==========================://
- #define _0_ (0.0)
- #define _ (0.0)
- RWC_AND_RWN
- f_p_CTO_rwC_AND_rwN(
- V_2 f_p /** 2 dimensional percentage **/
- )
- {
- V_3 rwC; //:ray_word__COORDINATE
- V_3 rwN; //:ray_world_NORMAL____
- //:#FIND_POINT_ON_SCREEN_PLANE#
- /** ************************************ ***
- Could possibly use these as slice planes.
- But for now using these to navigate our
- voxel map bounds.
- [ A ]Is_Directly_Above[ E ]On_Z_Axis
- [ B ]Is_Directly_Above[ F ]On_Z_Axis
- [ C ]Is_Directly_Above[ G ]On_Z_Axis
- [ D ]Is_Directly_Above[ H ]On_Z_Axis
- (In World/Frag Coords)
- [0,0,0]
- \
- A_B \
- A----|------B A --> +=======+ <-- B
- | | | /. .\
- | rwC_001 | / . . \
- | | | / . . \
- C----|------D C->+===============+<-D
- C_D [ ._....._. ]
- [ / \ ]
- [ . . ]
- [/ \]
- +===============+<-H
- +===============+
- [\ /]
- [ . . ]
- [ \_......._/ ]
- E_F [ . . ]
- E----|------F G->+===============+<-H
- | | | \ . . /
- | rwC_002 | \ . . /
- | | | \. ./
- G----|------H E --> +=======+ <-- F
- G_H
- *** ************************************ **/
- //:Dimensions Of Voxel Volume:
- //:As maximum indexes.
- F32 M_X =( F32(TPX) - 1.0 );
- F32 M_Y =( F32(TPY) - 1.0 );
- F32 M_Z =( F32(TPZ) - 1.0 );
- //:TOP LAYER OF PIXELS OF VOXEL VOLUME:
- //:(INCLUSIVE MATH. We are inside the)
- //:(first layer of pixels of the first)
- //:(layer of voxels)
- V_3 _A_ = V_3( _-_ , _-_ , _-_ );
- V_3 _B_ = V_3( M_X , _-_ , _-_ );
- V_3 _C_ = V_3( _-_ , M_Y , _-_ );
- V_3 _D_ = V_3( M_X , M_Y , _-_ );
- //:BOTTOM LAYER OF PIXELS OF VOXEL VOLUME
- //:(Incluseive Math. We are inside the )
- //:(last layer of pixels of the last )
- //:(layer of voxels. )
- V_3 _E_ = V_3( _-_ , _-_ , M_Z );
- V_3 _F_ = V_3( M_X , _-_ , M_Z );
- V_3 _G_ = V_3( _-_ , M_Y , M_Z );
- V_3 _H_ = V_3( M_X , M_Y , M_Z );
- /** ************************************ ***
- Take our plane and pretend it is
- the top of a cube that we want to
- get plane in an isometric position to.
- Where the new plane tangents corner _D_
- A------_B_ vec_D_B
- /| \\ \
- / | |\\ \
- / | | \\ vec_D_C <---D
- _C_============_D_ ^
- | |_ _ _ _| || |
- | /E F\ || vec_H_D
- | / \||
- |/ || Corner_Vector:
- G-------------_H_
- *** *** * * * * * * **** * * * * * * *** **/
- /** Edge Vectors **/
- V_3 vec_C_D = normalize( _D_ - _C_ );
- V_3 vec_D_C = normalize( _C_ - _D_ );
- V_3 vec_D_B = normalize( _B_ - _D_ );
- V_3 vec_H_D = normalize( _D_ - _H_ );
- V_3 vec_D_H = normalize( _H_ - _D_ );
- /** *** * * * * * * **** * * * * * * *** ***
- Use Corner_Vector to calculate a 3/4
- perspective plane that tangents _D_.
- *** ************************************ **/
- /** ************************************ ***
- X axis vector is pretty easy because the
- camera X axis does not move in the Z
- direction.
- ^ /
- | / ^
- A-------B / | a_x Is Average
- | | / |
- | | / vec_D_B
- C-------D--> vec_C_D ----->
- /
- /
- /<-- goal is THIS line for X-axis.(a_x)
- /
- *** *** * * * * * * **** * * * * * * *** **/
- #define _X_ vec_C_D
- #define _Y_ vec_D_B
- V_3 a_x =normalize(
- /** 45 degree line on XY plane **/
- ( _X_ + _Y_ ) / 2.0
- );;
- #undef _X_
- #undef _Y_
- /** *** * * * * * * **** * * * * * * *** ***
- Y axis vector is a bit tricky because it
- moves on all axis(es). XYZ.
- +--B If we average vec_D_C & vec_D_B
- |\ | To get a 45 degree on the XY plane,
- | \| we can then tilt that vector up
- C--D by 45 degrees by averaging it
- | with vec_H_D
- |
- H
- *** ************************************ **/
- //: a_y: Axis_Y
- #define _X_ vec_D_C
- #define _Y_ vec_D_B
- #define _Z_ vec_H_D
- V_3 a_y =normalize(
- (
- normalize( (_X_ + _Y_)/2.0 )
- + ( _Z_ )
- ) /2.0
- );;
- #undef _X_
- #undef _Y_
- #undef _Z_
- /** Looking into the voxel volume from **/
- /** a 3/4 isometric direction. **/
- V_3 a_z = normalize(
- ( vec_D_C + vec_D_B + vec_D_H ) / 3.0
- );;
- /** ************************************ ***
- We can now use point-normal form to
- build our camera polygon. We will start
- from point _D_ and walk HALFWAY the
- WIDTH in both directions on our altered
- x-axis vector( a_x ) and HALFWAY the
- HEIGHT in both directions on our altered
- y-axis vector( a_y ).
- HEIGHT: The height of our camera plane.
- WIDTH: The width of our camera plane.
- P_X +---+
- +------+------+ | |
- | | | +---+---+
- N_Y +---- _D_ ----+ P_Y |_D_|
- | | | +---+---+
- +------+------+ | |#DIA_CAMCENTER#
- N_X +---+
- #CAMCENTER#:
- In order for the camera to be 100%
- centered, it may be necessary to
- squash or stretch the camera by 1
- pixel IF the camera is NOT an odd
- number of pixels on that axis.
- SEE_DIAGRAM[ #DIA_CAMCENTER# ]
- +0 +1 +2 +2.5 +0 +1 +2 +3
- [D] [+] || [+]
- [ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ]
- |<---- 5 ---->| |<------ 6 ----->|
- *** ************************************ **/
- #define ZOOMED_IN_CAMERA_2020_11_26 ( 1 )
- float H_W ;
- float H_H ;
- if( ZOOMED_IN_CAMERA_2020_11_26 >= 1 ){
- /** Camera surface smaller than **/
- /** client viewport means zoomed in **/
- H_W = ( F32(TPX) / 2.0 );
- H_H = ( F32(TPY) / 2.0 );
- }else
- if( ZOOMED_IN_CAMERA_2020_11_26 <= 0 ){
- /** Camera surface exactly same size **/
- /** as client viewport means no zoom.**/
- H_W = ( iResolution.x / 2.0 );
- H_H = ( iResolution.y / 2.0 );
- };;
- #undef ZOOMED_IN_CAMERA_2020_11_26
- /** To be pixel-perfect, floor and ceil **/
- /** need to be used. This is an OCD **/
- /** optimization that can probably **/
- /** be removed without noticable **/
- /** difference in the result. **/
- //- P_X = floor( _D_ + ( a_x * H_W ) ); -//
- //- N_X = ceil( _D_ - ( a_x * H_W ) ); -//
- //- P_Y = floor( _D_ + ( a_y * H_H ) ); -//
- //- N_Y = ceil( _D_ - ( a_y * H_H ) ); -//
- //+ We can't do it this way, because the +//
- //+ plane is not aligned with our native +//
- //+ XYZ axis. +//
- //+ What I mean is we can't get the top +//
- //+ left corner by saying: +//
- //+ vec3( N_Y.x , P_X.y , _D_.z ) +//
- /** ************************************ ***
- RASTER_GRAPHICS_STYLE_TOP_LEFT_ORIGIN
- \
- +----------- +X ------------>
- |
- | _I_ _J_
- | \ P_X /
- | +------+------+ ^
- | | | | |
- +Y N_Y +---- _D_ ----+ P_Y [ -y ]
- | | | | |
- | +------+------+ |
- | / N_X \ |
- V _K_ _L_ |
- |
- <---------[ -x ]------------+
- *** *** * * * * * * **** * * * * * * *** **/
- V_3 _I_ = _D_ - ( a_x * H_W )
- - ( a_y * H_H ) ;;
- V_3 _J_ = _D_ + ( a_x * H_W )
- - ( a_y * H_H ) ;;
- V_3 _K_ = _D_ - ( a_x * H_W )
- + ( a_y * H_H ) ;;
- V_3 _L_ = _D_ + ( a_x * H_W )
- + ( a_y * H_H ) ;;
- /** ************************************ **/
- #define USE_ANIMATED_DOLLY_FOR_DEBUG ( 1 )
- if( USE_ANIMATED_DOLLY_FOR_DEBUG >= 1 ){
- F32 max_dolly =max(
- F32(TPX)
- ,
- max(
- F32(TPY)
- ,
- F32(TPZ)
- )
- );;
- F32 dolly_amount =(
- cos( iTime ) * max_dolly
- );;
- _I_ += ( a_z * dolly_amount );
- _J_ += ( a_z * dolly_amount );
- _K_ += ( a_z * dolly_amount );
- _L_ += ( a_z * dolly_amount );
- };;
- #undef USE_ANIMATED_DOLLY_FOR_DEBUG
- V_3 I_J = sdf_i3d( _I_ , _J_ , f_p.x );
- V_3 K_L = sdf_i3d( _K_ , _L_ , f_p.x );
- rwC = sdf_i3d( I_J , K_L , f_p.y );
- //:#POSITIVE_Z_DIVES_INTO_SCREEN#://
- rwN = normalize( a_z );
- RWC_AND_RWN
- rwC_AND_rwN;
- rwC_AND_rwN.rwC = rwC;
- rwC_AND_rwN.rwN = rwN;
- return( rwC_AND_rwN );
- }
- #undef _0_
- #undef _
- //:==========================:f_p_CTO_rwC_AND_rwN://
- //:========================:FRAGPER_TO_CAMERA_RAY://
- //:RENDER_SCENE:=================================://
- //:sdf_RenderScene:==============================://
- #define T_X vox_000.vox_cur.t_x
- #define T_Y vox_000.vox_cur.t_y
- #define T_Z vox_000.vox_cur.t_z
- #define HAS vox_000.vox_cur.has
- V_4 sdf_RenderScene( RWC_AND_RWN rwC_AND_rwN )
- {
- I32 o_k = I32( 1 );
- V_4 c4d = V_4(1,1,1,1);
- V_3 rwN = rwC_AND_rwN.rwN;
- V_3 rwC = rwC_AND_rwN.rwC;
- V_3 xyz = rwC;
- VOX_000
- vox_000;
- //:nex: Distance To NEXT voxel.
- //:d_S: Distance To Surface Geometry inside
- //: the current voxel. In world coords.
- vox_000.vox_dis.nex =( 0.0 );
- //: vox_000.vox_dis.d_S =( 0.0 );
- //:Voxels will be thought of as 3D tiles:
- vox_000.vox_cur.has = uint( 0 );
- vox_000.vox_cur.dex = int( 0 );
- vox_000.vox_cur.t_x = int( 0 );
- vox_000.vox_cur.t_y = int( 0 );
- vox_000.vox_cur.t_z = int( 0 );
- //:RAY_MARCH_LOOP:-----------------------://
- /** #ABOUT_RAY_MARCH_LOOP# **/
- //:March to VOXEL:
- for( int i = 0; i < MAX_STE ; i++ ){
- //:March by distance to next voxel:
- //:Point_Normal_Form_To_Get:xyz
- xyz =
- rwC
- + ( rwN * vox_000.vox_dis.nex )
- ;;
- vox_000 =
- GET_vox_000_USE_xyz_rwN(
- xyz,rwN );;
- //:If voxel is not empty, ray march
- //:inside of the voxel.
- if( vox_000.vox_cur.val > 0 ){
- for( int v = 0; v < MAX_STE ; v++ ){
- };;};;
- /** Don't move the ray anywhere **/
- /** We are going to slice into **/
- /** whatever voxels the camera **/
- /** plane passes through. **/
- if( CFG_SLICE_RENDER >= 1 ){
- break;
- };;
- };;
- //:-----------------------:RAY_MARCH_LOOP://
- if( CFG_SLICE_RENDER >= 1 ){
- //:Layer value, 1 or 0
- int lay = I32_MOD( T_Z, 2 );
- //:#MODULO_CHECKER_PATTERN#://
- int chk =(
- I32_MOD( //:<<<<<<<<<<<<<<:SET_003
- I32_MOD( T_Y, 2 ) //:SET_YYY
- + I32_MOD( T_X, 2 ) //:SET_XXX
- , 2
- )
- );;
- if( 0 != 0
- || ( lay < 0 )
- || ( lay > 1 )
- || ( chk < 0 || chk > 1 )
- || ( HAS > uint(1) )
- ){
- //: _0x010101_ & _0x101010_
- c4d=PIX_ERR(ERR_001,c4d,o_k--);
- };;
- F32 M =( 255.0); //:Max RGB value.
- F32 X =( 1.0 ); //:FOR_DEBUGGING_COLORS
- //:F32 ...........; Replace with "_"
- //:F32 ...........; when done debugging.
- F32 a =( 1.0 ); //:ALPHA
- F32 _ =( 0.0 );
- F32 g =( F32(0x33)/M ); //:DARK-GREY
- F32 G =( F32(0x38)/M );
- F32 w =( F32(0xE5)/M ); //:WHITE-GREY
- F32 W =( F32(0xF2)/M );
- V_4 _0x333333_ =V_4(g,g,g,a);
- V_4 _0x383838_ =V_4(G,G,G,a);
- V_4 _0xE5E5E5_ =V_4(w,w,w,a);
- V_4 _0xF2F2F2_ =V_4(W,W,W,a);
- V_4 _0x003300_ =V_4(_,g,_,a);
- V_4 _0x003800_ =V_4(_,G,_,a);
- V_4 _0x00E500_ =V_4(_,w,_,a);
- V_4 _0x00F200_ =V_4(_,W,_,a);
- V_4 tab_000[8]=V_4[8](
- //:OUT OF BOUNDS: //:HAS?
- _0x333333_ , _0x383838_ //:LAY0
- ,_0xE5E5E5_ , _0xF2F2F2_ //:LAY1
- //: | CHK0 | CHK1 |::::::::://
- //:IN BOUNDS: //:HAS?
- ,_0x003300_ , _0x003800_ //:LAY0
- ,_0x00E500_ , _0x00F200_ //:LAY1
- //: | CHK0 | CHK1 |::::::::://
- );;
- I32 pik = (
- (4 * ( HAS >= uint(1) ? 1 : 0 ))
- + (2 * lay ) //: LAY0 -or- LAY1
- + (1 * chk ) //: CHK0 -or- CHK1
- );;
- if( pik < 0 || pik >= 8 ){
- c4d=PIX_ERR(ERR_002,c4d,o_k--);
- };;
- //:TODO: Why is not rendering as
- //: checker?
- if( 1 == o_k ){
- c4d = tab_000[ pik ];
- };;
- }else{
- //:DEBUG ONLY.
- c4d=V_4(1,1,1,1);
- };;
- return( c4d );
- }
- #undef T_X
- #undef T_Y
- #undef T_Z
- #undef HAS
- //:==============================:sdf_RenderScene://
- //:=================================:RENDER_SCENE://
- //:11111111111111111111111111111111111111111111111111://
- //:00000000000000000000000000000000000000000000000000://
- void mainImage(
- out vec4 fragColor
- , in V_2 fragCoord
- )
- {
- #define R_Y iResolution.y
- V_2 f_c = V_2(
- //:( FLIP? ) ( Discrete XY ) ://
- ( 0.0 ) + ( fragCoord.x - 0.5 )
- , (R_Y-1.0) - ( fragCoord.y - 0.5 )
- );;
- #undef R_Y
- V_2 f_p = f_c_CTO_f_p( f_c );
- //:CAMERA_RAY_FOR_CURRENT_PIXEL:-------------://
- /** The coordinate and direction of the **/
- /** camera ray [rwC,rwN] are closely **/
- /** related, so return them using the **/
- /** same function. This will save **/
- /** processing power when we decide **/
- /** to create a camera lense that **/
- /** is a 360 degree cylinder around **/
- /** an object. (for fun) **/
- RWC_AND_RWN
- rwC_AND_rwN;
- rwC_AND_rwN = f_p_CTO_rwC_AND_rwN( f_p );
- //:-------------:CAMERA_RAY_FOR_CURRENT_PIXEL://
- fragColor = sdf_RenderScene( rwC_AND_rwN );
- }
- //:00000000000000000000000000000000000000000000000000://
- //:DOCUMENTATION:====================================://
- /** ************************************************ ***
- ABBREVIATIONS:
- f : fragment
- p : percent (Never Position, use C for coord)
- c : coordinate
- r : ray
- n : normal, for directions.
- d : distance. NEVER DIRECTION.( use: n:normal )
- IDENTIFIERS:
- CTO: Convert_TO
- rwC: RayWorldCoord
- rwN: RayWorldNormal
- f_p: FragPercent
- f_c: FragCoord (With Discrete Pixel Coords)
- (instead of pixel centers )
- dad: Distance_And_inDEX
- Index is 1D index of XYZ voxel tile
- coordinate.
- vat: Voxel_Array__of__Tiles
- c4d: Color_4_Dimensional(RGBA)
- FUNCTIONS:
- f_c_CTO_f_p : FragmentCoord -CTO- FragPercent
- f_c_CTO_per : USE[ f_c_CTO_f_p ]
- sdf_i3d : Interpolate_Two_3D_Points
- EXTRACTED_BLOCK_COMMENTS:
- #ABOUT_RAY_MARCH_LOOP###########################
- Ray marching loop should be able to look
- at the current voxel coordinate returned
- and decide to STOP marching if it wants.
- By stopping immediately without marching
- at all we can make the camera phosphore
- surface a slice plane that renders
- cross sectional views of tilemap data.
- ##########################ABOUT_RAY_MARCH_LOOP##
- #POSITIVE_Z_DIVES_INTO_SCREEN###################
- Positive Z is further back into screen.
- This way to help normalize voxel grid math.
- ###################POSITIVE_Z_DIVES_INTO_SCREEN#
- #FIND_POINT_ON_SCREEN_PLANE#####################
- ORIGINAL_COMMENT:
- Polygon in 3d space to serve as the
- phosphor surface the CRT monitor
- electrons will be projected onto.
- Could probably use a mat4 for this.
- MORE_INFORMATION_IN_RETROSPECT:
- "Screen Plane" is also known as
- "CRT Phospore" or "PHO" for short.
- This is a plane put into 3D space that
- represents the pixels of the client
- viewport (physical monitor).
- The "IMAGE" plane from this diagram:
- https://tinyurl.com/IMAGE-PLANE
- #####################FIND_POINT_ON_SCREEN_PLANE#
- #MODULO_CHECKER_PATTERN#########################
- // Checker pattern value, 1 or 0. //
- // Combine Sets so that they create //
- // a checkerboard of odd/even values, //
- // and then convert even to 0 and //
- // odd to 1. //
- // //
- // SET_YYY:[ 0 1 0 1 0 1 0 ] //
- // SET_XXX:[ 0 1 0 1 0 1 0 ] //
- // //
- // SET_XXX: [ 0 1 0 1 0 1 0 ] //
- // | | | | | | | //
- // SET_YYY:[ 0 ]- 0 1 0 1 0 1 0 //
- // [ 1 ]- 1 2 1 2 1 2 1 //
- // [ 0 ]- 0 1 0 1 0 1 0 //
- // [ 1 ]- 1 2 1 2 1 2 1 //
- // [ 0 ]- 0 1 0 1 0 1 0 //
- // [ 1 ]- 1 2 1 2 1 2 1 //
- // [ 0 ]- 0 1 0 1 0 1 0 //
- int chk =(
- I32_MOD( // <<<<<<<<<<<<<< SET_003
- I32_MOD( T_Y, 2 ) // SET_YYY
- + I32_MOD( T_X, 2 ) // SET_XXX
- , 2
- )
- );;
- #########################MODULO_CHECKER_PATTERN#
- *** ************************************************ **/
- //:====================================:DOCUMENTATION://
- //:UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU://
- /** U : Undefine Macros Section **/
- #undef V_4
- #undef V_3
- #undef V_2
- #undef F32
- #undef I32
- /** U : Undefine Macros Section **/
- //:UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU://
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement