Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
1.0.0:water [2023/10/24 14:17] – [Example] admin1.0.0:water [2023/10/24 14:19] (current) – [Example] admin
Line 135: Line 135:
  
 <file pascal> <file pascal>
-unit Unit1; +procedure TForm1.CreateWater(const AAssetsPath String); 
- +var LTexPath : String;
-interface +
- +
-uses +
-  System.SysUtils, +
-  System.Types, +
-  System.UITypes, +
-  System.Classes, +
-  System.Variants, +
-  System.IOUtils, +
-  System.Math.Vectors, +
-  FMX.Types, +
-  FMX.Controls, +
-  FMX.Forms, +
-  FMX.Graphics, +
-  FMX.Dialogs, +
-  FMX.Types3D, +
-  FMX.Controls3D, +
-  FMX.Objects3d, +
-  FMX.MaterialSources, +
-  FMX.StdCtrls, +
-  FMX.Controls.Presentation, +
-  Gorilla.DefTypes, +
-  Gorilla.Viewport, +
-  Gorilla.Skybox, +
-  Gorilla.Cube, +
-  Gorilla.Plane, +
-  Gorilla.Terrain, +
-  Gorilla.Terrain.Utils, +
-  Gorilla.Terrain.Algorithm, +
-  Gorilla.Controller.Passes.CustomBlur, +
-  Gorilla.Controller.Passes.Reflection, +
-  Gorilla.Controller.Passes.Refraction, +
-  Gorilla.Controller.Passes.CustomDepth, +
-  Gorilla.Controller.Passes.Environment, +
-  Gorilla.Material.Water, +
-  Gorilla.Material.Lambert, +
-  System.Generics.Collections, +
-  FMX.Edit, FMX.EditBox, FMX.NumberBox, FMX.ListBox, FMX.Colors; +
- +
-type +
-  TForm1 = class(TForm) +
-    procedure FormCreate(Sender: TObject); +
-  private +
-    FGorilla  : TGorillaViewport; +
-    FLight    : TLight; +
-    FLightPos : Single; +
-    FSkyBox   : TGorillaSkyBox; +
-    FTerrain  : TGorillaTerrain; +
-    FTerrainMat : TGorillaLambertMaterialSource; +
-    FWaterPlane : TGorillaPlane; +
-    FWaterMat   : TGorillaWaterMaterialSource; +
-    FReflection : TGorillaRenderPassReflection; +
-    FRefraction : TGorillaRenderPassRefraction; +
-    FReflectBody : TGorillaCube; +
-    FReflectBodyMat : TGorillaLambertMaterialSource; +
-  public +
-    { Public-Deklarationen } +
-  end; +
- +
-var +
-  Form1: TForm1; +
- +
-implementation +
- +
-{$R *.fmx} +
- +
-uses +
-  Gorilla.Utils.Math, +
-  Gorilla.Control, +
-  Gorilla.Controller, +
-  Gorilla.Context.Texturing; +
- +
-procedure TForm1.FormCreate(SenderTObject); +
-var LPath : String;+
 begin begin
-  // Get the platform independent assets path +  LTexPath := IncludeTrailingPathDelimiter(AAssetsPath + 'water');
-{$IFDEF MSWINDOWS} +
-  LPath := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + +
-    IncludeTrailingPathDelimiter('assets'); +
-{$ELSE} +
-  LPath := IncludeTrailingPathDelimiter(TPath.GetHomePath()); +
-{$ENDIF}+
  
-  // Create the Gorilla3D viewport +  /// create the water plane 
-  FGorilla := TGorillaViewport.Create(Self); +  FWaterPlane := TGorillaPlane.Create(FViewport); 
-  FGorilla.Name := 'GorillaViewport1'; +  FWaterPlane.Name := 'WaterPlane1'; 
-  FGorilla.Color := TAlphaColorRec.Black; +  FWaterPlane.BeginUpdate();
-  FGorilla.OnMouseUp := DoOnViewportMouseUp; +
-  FGorilla.FXAA := 0; +
- +
-  // Create a light source +
-  FLight := TLight.Create(FGorilla); +
-  FLight.Parent := FGorilla; +
-  FLight.Name := 'GorillaLight1'; +
-  FLight.LightType := TLightType.Point; +
-  FLight.AbsolutePosition := Vector3D(0, -25, -5); +
-  FLightPos := 0; +
- +
-  // Skybox to have more reflections +
-  FSkyBox := TGorillaSkyBox.Create(FGorilla); +
-  FSkyBox.Parent := FGorilla; +
- +
-  // Terrain with random generation +
-  FTerrain := TGorillaTerrain.Create(FGorilla); +
-  FTerrain.Parent := FGorilla; +
-  fTerrain.BeginUpdate();+
   try   try
-    FTerrain.ResolutionX := 128+    FWaterPlane.Parent := FViewport
-    FTerrain.ResolutionY := 128+    FWaterPlane.HitTest := false
-    FTerrain.HitTest := false+    FWaterPlane.RotationAngle.X := -90
-    FTerrain.Height := 1+    FWaterPlane.Width  := MAP_SIZE
-    FTerrain.Scale.X := 10+    FWaterPlane.Height := MAP_SIZE
-    FTerrain.Scale.Y := 1; +    FWaterPlane.Depth  := 1; 
-    FTerrain.Scale.:= 10+    FWaterPlane.SubdivisionsHeight := 64; 
-    FTerrain.RandomTerrain(TRandomTerrainAlgorithmType.DiamondSquare, true);+    FWaterPlane.SubdivisionsWidth  := 64
 +    FWaterPlane.Position.Y := -25;
   finally   finally
-    FTerrain.EndUpdate();+    FWaterPlane.EndUpdate();
   end;   end;
  
-  // Create a material for the terrain mesh +  /// create reflection render pass for water material 
-  FTerrainMat := TGorillaLambertMaterialSource.Create(fTerrain); +  FReflection := TGorillaRenderPassReflection.Create(FViewport); 
-  FTerrainMat.Parent := FTerrain; +  FReflection.Viewport := FViewport
-  TGorillaLambertMaterialSource(FTerrainMat).Texture.LoadFromFile(LPath + 'terrain.jpg'); +  FReflection.Camera := FCamera;
-  FTerrain.MaterialSource := FTerrainMat; +
-   +
-  // Water surface plane to render water    +
-  FWaterPlane := TGorillaPlane.Create(FGorilla); +
-  FWaterPlane.SetHitTestValue(false); +
-  FWaterPlane.Parent := FGorilla; +
-  FWaterPlane.RotationAngle.X := 90; +
-  FWaterPlane.Width  := 10; +
-  FWaterPlane.Height := 10; +
-  FWaterPlane.Position.Y := -0.5; +
-   +
-  // High resolution for ripple testing - vertex shader modifies it +
-  FWaterPlane.SubdivisionsWidth  := 256; +
-  FWaterPlane.SubdivisionsHeight := 256; +
- +
-  // Water shader material attached to our water surface plane +
-  FWaterMat := TGorillaWaterMaterialSource.Create(FWaterPlane); +
-  FWaterMat.Parent := FWaterPlane; +
- +
-  FWaterMat.DisplacementMap.LoadFromFile(LPath + 'water-height.jpg'); +
-  FWaterMat.DisplIntensity := 0.1; +
-  FWaterMat.NormalMap.LoadFromFile(LPath + 'water-normal.jpg'); +
-  FWaterMat.DUDVTexture.LoadFromFile(LPath + 'water-dudv.jpg'); +
-  FWaterMat.SpecularMap.LoadFromFile(LPath + 'water-specular.jpg'); +
-  FWaterMat.FoamTexture.LoadFromFile(LPath + 'water-foam.jpg'); +
-  FWaterMat.TextureTiling[WATER_TEX_FOAM] := PointF(4, 4); +
- +
-  FWaterMat.ShoreTexture.LoadFromFile(LPath + 'water-shore.png'); +
-  FWaterMat.TextureTiling[WATER_TEX_SHORE] := PointF(2, 2); +
- +
-  // Link material to the water surface plane +
-  FWaterPlane.MaterialSource := FWaterMat; +
- +
-  // Reflection render pass +
-  FReflection := TGorillaRenderPassReflection.Create(FGorilla); +
-  FReflection.Viewport := FGorilla; +
-   +
-  // Ignore the water plane, otherwise the rendering might be black+
   FReflection.IgnoreControl(FWaterPlane);   FReflection.IgnoreControl(FWaterPlane);
   FReflection.MirrorSize := FWaterPlane.Width;   FReflection.MirrorSize := FWaterPlane.Width;
      
-  // The mirror surface position is important to to render correctly +  // set the current position of the water plane as mirror plane 
-  // Because displacement mapping is activated, we need to render reflections +  // this needs to be updated, if water plane moves 
-  // with the displacement offset +  FReflection.MirrorPosition := TPoint3D(FWaterPlane.AbsolutePosition);
-  FReflection.MirrorPosition.Point := TPoint3D(FWaterPlane.AbsolutePosition) +
-    Point3D(0, -FWaterMat.DisplIntensity, 0); +
-  +
-  // We do not need fullscreen rendering for reflections, half of the size is enough +
-  FReflection.ViewportScale := 0.5;+
   FReflection.Enabled := true;   FReflection.Enabled := true;
  
-  // Refraction render pass +  /// create refraction render pass for water material 
-  FRefraction := TGorillaRenderPassRefraction.Create(fGorilla); +  FRefraction := TGorillaRenderPassRefraction.Create(FViewport); 
-  FRefraction.Viewport := fGorilla; +  FRefraction.Viewport := FViewport
-   +  FRefraction.IgnoreControl(FWaterPlane);
-  // Ignore the water plane, otherwise the rendering might be black +
-  FRefraction.IgnoreControl(fWaterPlane); +
-   +
-  // The mirror surface position is important to to render correctly +
-  // Because displacement mapping is activated, we need to render reflections +
-  // with the displacement offset +
-  FRefraction.SurfacePosition.Point := TPoint3D(FWaterPlane.AbsolutePosition+
-    Point3D(0, -FWaterMat.DisplIntensity, 0); +
-  +
-  // We do not need fullscreen rendering for refractions, half of the size is enough +
-  FRefraction.ViewportScale := 0.5;+
   FRefraction.Enabled := true;   FRefraction.Enabled := true;
  
-  // Link reflection and refraction render passes to the material +  /// create material source 
-  FWaterMat.ReflectionPass := fReflection+  FWaterMaterial := TGorillaWaterMaterialSource.Create(FWaterPlane)
-  FWaterMat.Reflections := true+  FWaterMaterial.Parent := FWaterPlane
-  FWaterMat.ReflectionPower := 1+  FWaterMaterial.NormalMap.LoadFromFile(LTexPath + 'water_normal.png')
-  FWaterMat.RefractionPass := fRefraction+  FWaterMaterial.DUDVTexture.LoadFromFile(LTexPath + 'water3-dudv.jpg')
-  FWaterMat.Refractions := true+  FWaterMaterial.DisplacementMap.LoadFromFile(LTexPath + 'water_height.png')
-  FWaterMat.RefractionPower := 1;+  FWaterMaterial.SpecularMap.LoadFromFile(LTexPath + 'water_height.png'); 
 +  FWaterMaterial.FoamTexture.LoadFromFile(LTexPath + 'foam.png');
  
-  // Create a reflective body and material to test reflections / refraction +  /// link reflection and refraction render pass to water material 
-  FReflectBody := TGorillaCube.Create(FGorilla)+  FWaterMaterial.ReflectionPass := FReflection
-  FReflectBody.Parent := FGorilla;+  FWaterMaterial.RefractionPass := FRefraction;
  
-  FReflectBodyMat := TGorillaLambertMaterialSource.Create(FReflectBody); +  FWaterPlane.MaterialSource := FWaterMaterial;
-  FReflectBodyMat.Parent := FReflectBody; +
-  FReflectBodyMat.UseTexture0 := false; +
-  FReflectBody.MaterialSource := FReflectBodyMat;+
 end; end;
- 
-end. 
 </file> </file>
  
 Next step: [[bokeh|Bokeh]] Next step: [[bokeh|Bokeh]]