Clouds.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /******************************************************************************/
  2. struct LayeredClouds
  3. {
  4. struct Layer // Cloud Layer
  5. {
  6. Color color ; // texture color , , default=WHITE
  7. Flt scale ; // texture scale , 0..Inf, default={0.35, 0.41, 0.50, 0.62}
  8. Vec2 position, // texture position, -Inf..Inf,
  9. velocity; // texture velocity, -Inf..Inf, default={0.010, 0.008, 0.006, 0.004}
  10. ImagePtr image ; // texture
  11. };
  12. Bool merge_with_sky, // if draw clouds together with the sky, this can result in better performance, but supports only up to 1 layer of clouds and Sky.frac() must be set to 1, this also results in drawing astronomical objects on top of clouds, default=false (true for Mobile)
  13. draw_in_mirror; // if draw clouds in mirrors, such as water reflections, default=false
  14. Layer layer[4] ; // layer array
  15. // set / get
  16. LayeredClouds& set (Byte active_layers, C ImagePtr &image=null); Int layers ()C {return _layers ;} // set/get number of active layers 0..4, if 'image'!=null layers will have 'image' set as their texture
  17. LayeredClouds& frac (Flt frac ); Flt frac ()C {return _frac ;} // set/get cloud viewport fraction , 0..1 , default=0.9 , fraction of the Viewport range where clouds start (1 is the fastest)
  18. LayeredClouds& scaleY (Flt scale ); Flt scaleY ()C {return _scale_y;} // set/get y scaling , 1..2 , default=1.05, setting this value higher than 1 helps covering the empty gap between flat ground and the clouds
  19. LayeredClouds& rayMaskContrast(Flt contrast ); Flt rayMaskContrast()C {return _rmc ;} // set/get sun rays masking contrast, 1..Inf, default=4 , this is used when "Sun.rays_mode==SUN_RAYS_HIGH"
  20. void update(); // update layers, this needs to be called once per frame to update the cloud texture animation movement (move the layer texture positions according to velocities)
  21. #if !EE_PRIVATE
  22. private:
  23. #endif
  24. Byte _layers;
  25. Flt _frac, _scale_y, _rmc;
  26. MeshRender _mshr;
  27. LayeredClouds();
  28. #if EE_PRIVATE
  29. void del ();
  30. void create ();
  31. void commit ();
  32. void draw ();
  33. void shadowMap();
  34. Bool wantDepth()C;
  35. #endif
  36. };
  37. /******************************************************************************/
  38. struct VolumetricCloud
  39. {
  40. struct Settings
  41. {
  42. // cloud opacity formula = "LerpRS(noise_min, noise_max, noise)*density"
  43. Byte detail ; // number of extra details, 0..3
  44. Flt density , // density scale factor, 0..1
  45. noise_min , // noise value at which a cloud is formed (start of the cloud), -1..1, currently ignored for object clouds
  46. noise_max , // noise value at which a cloud is fully dense (end of the cloud), -1..1, currently ignored for object clouds
  47. brightness , // cloud brightness, 0..1
  48. ambient , // ambient light , 0..1
  49. light_power; // light power , 0..1
  50. Vec light_pos ; // light position
  51. #if EE_PRIVATE
  52. void zero() {Zero(T);}
  53. #endif
  54. Settings() {detail=3; density=0.75f; noise_min=0.35f; noise_max=1.0f; brightness=0.8f; ambient=D.ambientPower(); light_power=Sun.light_color.max(); light_pos=Sun.pos;}
  55. };
  56. // get
  57. Int width ()C {return _image.h();}
  58. Int height()C {return _image.w();}
  59. Int depth ()C {return _image.d();}
  60. // manage
  61. void del();
  62. void create (Int size , Int height, Int frequency, const_mem_addr Threads *threads=null, UInt seed=0, Flt noise_gain=0.555f);
  63. void createAsObject(Int width, Int height, Int depth, Flt frequency, const_mem_addr Threads *threads=null, UInt seed=0, Flt noise_gain=0.555f);
  64. // operations
  65. void update(C Settings &settings);
  66. // draw
  67. void draw(Flt size, C VecD &pos); // draw object cloud with specified 'size' and 'pos', this should be called only for custom 'VolumetricCloud' objects, and not for the global 'Clouds.volumetric'
  68. VolumetricCloud();
  69. ~VolumetricCloud() {del();}
  70. #if !EE_PRIVATE
  71. private:
  72. #endif
  73. struct Voxel
  74. {
  75. #if 0 // Flt - slower (37 fps) but best quality
  76. Flt density;
  77. #if EE_PRIVATE
  78. void set(Flt density) {T.density=density;}
  79. Flt get( )C {return density;}
  80. Flt positive( )C {return density*0.5f+0.5f;}
  81. #endif
  82. #else // Byte - faster (48 fps) slightly lower quality (but not noticeable)
  83. Byte density;
  84. #if EE_PRIVATE
  85. void set(Flt density) {T.density=SFltToUByte(density);}
  86. Flt get( )C {return UByteToSFlt(density);}
  87. Flt positive( )C {return ByteToFlt (density);}
  88. #endif
  89. #endif
  90. };
  91. struct Src
  92. {
  93. VecI2 dir;
  94. Flt mul;
  95. };
  96. SimplexNoise _noise;
  97. Settings _cur, _build;
  98. Bool _build_finished, _creating, _object;
  99. Byte _building, _srcs;
  100. Int _pitch, _level;
  101. Flt _noise_scale, _noise_gain, _noise_mul, _noise_add, _sqr_density, _dy2;
  102. VecI _tile;
  103. Src _src[3];
  104. Voxel *_voxels;
  105. Flt *_light;
  106. Byte *_image_data;
  107. Threads *_threads;
  108. Image _image;
  109. #if EE_PRIVATE
  110. Voxel& voxel(Int x, Int y, Int z) {return _voxels[y + x*_image.w() + z*_pitch];}
  111. void createEx(Int width, Int height, Int depth, Flt frequency, const_mem_addr Threads *threads, UInt seed, Flt noise_gain, Bool object);
  112. void zero();
  113. void setDensity();
  114. void setDensityRow(Int z);
  115. void setImageRow (Int z);
  116. void build();
  117. void cancelCreate();
  118. void cancelBuild ();
  119. void checkBuild ();
  120. #endif
  121. NO_COPY_CONSTRUCTOR(VolumetricCloud);
  122. };
  123. /******************************************************************************/
  124. struct VolumetricClouds
  125. {
  126. VolumetricCloud cloud;
  127. Bool draw_in_mirror; // if draw clouds in mirrors, such as water reflections, default=false
  128. Int res_h ; // max resolution height for the clouds render target, 1..Inf, default=540 (1080/2)
  129. Flt size , // cloud dome size in meters , 0..Inf , default=100
  130. curve , // cloud dome curviness , 0..1 , default=0.05
  131. tex_scale, // texture coordinates scale , 0..Inf , default=0.5
  132. shadow ; // shadow intensity , 0..1 , default=0.35
  133. Vec color , // cloud color , (0,0,0)..(1,1,1) , default=(1,1,1)
  134. pos ; // world space position in meters, (-Inf,-Inf,-Inf)..(Inf,Inf,Inf), default=(0,0,0)
  135. #if !EE_PRIVATE
  136. private:
  137. #endif
  138. VolumetricClouds();
  139. #if EE_PRIVATE
  140. void del () {cloud.del();}
  141. Bool drawable ()C;
  142. void draw ();
  143. void shadowMap();
  144. Bool wantDepth()C {return drawable();}
  145. #endif
  146. };
  147. /******************************************************************************/
  148. struct AllClouds
  149. {
  150. Bool draw;
  151. LayeredClouds layered;
  152. VolumetricClouds volumetric;
  153. #if EE_PRIVATE
  154. void del () {layered.del(); volumetric.del();}
  155. void create () {draw=true; layered.create();}
  156. void drawAll ();
  157. void shadowMap() {layered.shadowMap(); volumetric.shadowMap();}
  158. Bool wantDepth()C {return draw && (layered.wantDepth() || volumetric.wantDepth());}
  159. #endif
  160. }extern
  161. Clouds;
  162. /******************************************************************************/