Recent

Author Topic: LAMW - paintshader sample  (Read 402 times)

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
LAMW - paintshader sample
« on: December 09, 2023, 05:05:40 am »
Good morning, @MrJmPessoa, is there any work around doing paintshader on canvas? i did bind id on paintshader but got nothing happened   :)

Thank you

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: LAMW - paintshader sample
« Reply #1 on: December 09, 2023, 02:20:44 pm »
By kordal
Code: Pascal  [Select][+][-]
  1. object AndroidModule1: TAndroidModule1
  2.   Left = 438
  3.   Top = 127
  4.   Width = 320
  5.   Height = 454
  6.   MarginLeft = 0
  7.   MarginTop = 0
  8.   MarginRight = 0
  9.   MarginBottom = 0
  10.   Enabled = True
  11.   Text = 'AndroidModule1'
  12.   ActivityMode = actMain
  13.   BackgroundColor = colbrDefault
  14.   ActionBarTitle = abtDefault
  15.   AnimationDurationIn = 1500
  16.   AnimationDurationOut = 1500
  17.   AnimationMode = animNone
  18.   OnCloseQuery = AndroidModule1CloseQuery
  19.   OnJNIPrompt = AndroidModule1JNIPrompt
  20.   OnShow = AndroidModule1Show
  21.   object jView1: jView
  22.     Left = 8
  23.     Top = 8
  24.     Width = 296
  25.     Height = 424
  26.     MarginLeft = 0
  27.     MarginTop = 0
  28.     MarginRight = 0
  29.     MarginBottom = 0
  30.     Enabled = True
  31.     Visible = True
  32.     PosRelativeToAnchor = []
  33.     PosRelativeToParent = []
  34.     LayoutParamWidth = lpWrapContent
  35.     LayoutParamHeight = lpWrapContent
  36.     Canvas = jCanvas1
  37.     BackgroundColor = colbrDefault
  38.     OnDraw = jView1Draw
  39.   end
  40.   object jCanvas1: jCanvas
  41.     PaintShader = jPaintShader1
  42.     PaintStrokeWidth = 1
  43.     PaintStyle = psStroke
  44.     PaintTextSize = 20
  45.     PaintColor = colbrRed
  46.     Typeface = ffNormal
  47.     Left = 57
  48.     Top = 111
  49.   end
  50.   object jPaintShader1: jPaintShader
  51.     Left = 136
  52.     Top = 112
  53.   end
  54.   object jBitmap1: jBitmap
  55.     FilePath = fpathData
  56.     ImageIdentifier = 'sky'
  57.     Width = 0
  58.     Height = 0
  59.     Left = 207
  60.     Top = 112
  61.   end
  62.   object jSensorManager1: jSensorManager
  63.     OnChanged = jSensorManager1Changed
  64.     Left = 57
  65.     Top = 176
  66.   end
  67. end
  68.  
  69.  

Code: Pascal  [Select][+][-]
  1. {Hint: save all files to location: J:\!work\FPC\TestView\jni }
  2. unit uMain;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  10.   cthreads,
  11.   {$ENDIF}{$ENDIF}
  12.   Classes, SysUtils, AndroidWidget, Laz_And_Controls, paintshader, And_jni,
  13.   sensormanager;
  14.  
  15. type
  16.  
  17.   { TAndroidModule1 }
  18.   TVec3f = record
  19.     X: Single;
  20.     Y: Single;
  21.     Z: Single;
  22.   public
  23.     constructor Create(aX, aY, aZ: Single);
  24.   end;
  25.  
  26.   TAndroidModule1 = class(jForm)
  27.     jBitmap1: jBitmap;
  28.     jCanvas1: jCanvas;
  29.     jPaintShader1: jPaintShader;
  30.     jSensorManager1: jSensorManager;
  31.     jView1: jView;
  32.     procedure AndroidModule1CloseQuery(Sender: TObject; var CanClose: boolean);
  33.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  34.     procedure AndroidModule1Show(Sender: TObject);
  35.     procedure jSensorManager1Changed(Sender: TObject; sensor: jObject;
  36.       sensorType: TSensorType; values: array of single; timestamp: int64);
  37.     procedure jView1Draw(Sender: TObject);
  38.   private
  39.     Screen: TWH;
  40.     axes: TVec3f;
  41.     half: TPoint;
  42.     rad: Integer;
  43.     bg, surf, sh1, sh2, sh3, planet: SmallInt;
  44.     pulsar: Single;
  45.   public
  46.     {public declarations}
  47.   end;
  48.  
  49. var
  50.   AndroidModule1: TAndroidModule1;
  51.  
  52. implementation
  53.  
  54. {$R *.lfm}
  55.  
  56. function Clamp(x, Min, Max: Single): Single;
  57. begin
  58.   if x < min then
  59.     Result := min
  60.   else
  61.     if x > max then
  62.       Result := max
  63.     else
  64.       Result := x;
  65. end;
  66.  
  67. function RandF(Min, Max: Single): Single;
  68. var
  69.   Float: Single;
  70. begin
  71.   Float := Random;
  72.   Result := Min + float * (Max - Min);
  73. end;
  74.  
  75. constructor TVec3f.Create(aX, aY, aZ: Single);
  76. begin
  77.   X := aX;
  78.   Y := aY;
  79.   Z := aZ;
  80. end;
  81.  
  82. { TAndroidModule1 }
  83.  
  84. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  85. begin
  86.   // disable hardware acceleration for the `JPaintShader.Combine` method to work correctly !
  87.   jView1.SetLayerType(ltSOFTWARE); // need VERSION.SDK_INT >= 11
  88.  
  89.   // the default number of shaders is 6, if this is not enough, set property `CountShader` or use method `JPaintShader.SetCount`
  90.   // jView1.Canvas.PaintShader.SetCount(..)
  91.  
  92.   // set manually ID
  93.   sh3 := 3;
  94.   surf := 4;
  95.   planet := 5;
  96.  
  97.   jBitmap1.ImageIdentifier := 'sky2';
  98.   // background
  99.   bg := jView1.Canvas.PaintShader.NewBitmapShader(jBitmap1.GetImage, tmREPEAT, tmREPEAT, 1.2, 1.2, 45);
  100.   // planet surface
  101.   sh1 := jView1.Canvas.PaintShader.NewSweepGradient(120, 50, [$FFFF0707, $FFFF6A07, {$FFFFD507, $FFB8FF07, $FF51FF07,} $FF07FF24, $FF07FF8F,
  102.                                                              $FF07FFFF, $FF0797FF, $FF072CFF, $FF4907FF, $FFB007FF, $FFFF07DE{, $FFFF0772}], []);
  103.   jBitmap1.ImageIdentifier := 'surf';
  104.   sh2 := jView1.Canvas.PaintShader.NewBitmapShader(jBitmap1.GetImage, tmREPEAT, tmREPEAT);
  105.   jView1.Canvas.PaintShader.Combine(sh2, sh1, PorterDuff.OVERLAY, surf);
  106.  
  107.   if jSensorManager1.SensorExists(stAccelerometer) then
  108.     jSensorManager1.RegisterListeningSensor(stAccelerometer);
  109. end;
  110.  
  111. procedure TAndroidModule1.AndroidModule1CloseQuery(Sender: TObject; var CanClose: boolean);
  112. begin
  113.   jSensorManager1.StopListeningAll(); //finalize jni process here ....
  114.   CanClose := True;
  115. end;
  116.  
  117. procedure TAndroidModule1.AndroidModule1Show(Sender: TObject);
  118. begin
  119.   Screen := Self.ScreenWH;
  120.   half.X := Screen.Width div 2;
  121.   half.Y := Screen.Height div 2;
  122.   rad := half.X div 2;
  123. end;
  124.  
  125. procedure TAndroidModule1.jSensorManager1Changed(Sender: TObject;
  126.   sensor: jObject; sensorType: TSensorType; values: array of single;
  127.   timestamp: int64);
  128. begin
  129.   case sensorType of
  130.     stAccelerometer: begin
  131.       axes.X := values[0] * -20;
  132.       axes.Y := values[1] * 5.5;
  133.       axes.Z := values[0] * 2.3; // ufo angle
  134.       pulsar := RandF(0, 2.5);
  135.  
  136.       jView1.Refresh(); // repaint scene
  137.     end;
  138.   end;
  139. end;
  140.  
  141. procedure TAndroidModule1.jView1Draw(Sender: TObject);
  142. begin
  143.   jView1.Canvas.Clear($FFFFFFFF);
  144.   jView1.Canvas.PaintStyle := psDEFAULT;
  145.   jView1.Canvas.PaintStrokeWidth := 22;
  146.   // draw space
  147.   jView1.Canvas.PaintShader.Bind(bg);
  148.   jView1.Canvas.DrawRect(0, 0, Screen.Width, Screen.Height);
  149.   jView1.Canvas.DrawText('Star Space!', 100, 100);
  150.   // axes
  151.   jView1.Canvas.DrawText('Axes: ' + Format('x: %f, y: %f, ang: %f', [axes.X, axes.Y, axes.Z]), 100, 150);
  152.   //jView1.Canvas.DrawText('Pulsar: ' + FloatToStr(Pulsar), 100, 200);
  153.  
  154.   jView1.Canvas.PaintShader.SetIdentity(surf);
  155.   // big planet
  156.   // atmosphere & fog
  157.   jView1.Canvas.PaintShader.NewRadialGradient(half.X + 80, half.Y div 2, rad + 100, $CC000000, $FFFFFFFF, tmCLAMP, sh3);
  158.   jView1.Canvas.PaintShader.Combine(surf, sh3, PorterDuff.ADD, planet);
  159.   // draw planet
  160.   jView1.Canvas.PaintShader.Bind(planet);
  161.   jView1.Canvas.DrawCircle(half.X + 100, half.Y div 2, rad + Clamp(pulsar, 0, 0.55));
  162.  
  163.   // small planet
  164.   // blur
  165.   jView1.Canvas.PaintStyle := psSTROKE;
  166.   jView1.Canvas.PaintShader.NewRadialGradient(half.X - 95, Screen.Height - (half.Y div 2) - 5, rad, $CC0000CC, $30FFCFCF, tmCLAMP, sh3);
  167.   jView1.Canvas.PaintShader.Bind(sh3);
  168.   jView1.Canvas.DrawCircle(half.X - 95, Screen.Height - (half.Y div 2) - 5, rad - 47 - pulsar);
  169.   jView1.Canvas.PaintStyle := psDEFAULT;
  170.   // atmosphere
  171.   jView1.Canvas.PaintShader.SetScale(0.5, 0.45, surf);
  172.   jView1.Canvas.PaintShader.SetRotate(-37, half.X - 100, Screen.Height - (half.Y div 2), surf);
  173.   jView1.Canvas.PaintShader.NewRadialGradient(half.X - 120 - pulsar, Screen.Height - (half.Y div 2) - pulsar, rad + 30, $CC0000CC, $FFCCFFCF, tmCLAMP, sh3);
  174.   jView1.Canvas.PaintShader.Combine(surf, sh3, PorterDuff.MULTIPLY, planet);
  175.   // draw planet
  176.   jView1.Canvas.PaintShader.Bind(planet);
  177.   jView1.Canvas.DrawCircle(half.X - 100, Screen.Height - (half.Y div 2), rad - 50 - Clamp(pulsar, 0, 0.55));
  178.  
  179.   // draw ufo
  180.   jBitmap1.ImageIdentifier := 'ufo';
  181.   jView1.Canvas.DrawFrame(jBitmap1.GetImage, 0, 0, jBitmap1.Width, jBitmap1.Height, half.X - (jBitmap1.Width div 2) - axes.X, half.Y - 70 - axes.Y, jBitmap1.Width, jBitmap1.Height, axes.Z);
  182. end;
  183.  
  184.  
  185. end.
  186.  
  187.  
« Last Edit: December 09, 2023, 02:31:32 pm by Mongkey »

 

TinyPortal © 2005-2018