| 1 |
{ |
| 2 |
Copyright 2020-2020 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Rendering utilities for TCastleScene. } |
| 17 |
unit SceneUtilities; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses CastleVectors, CastleColors, CastleShapes, CastleScene; |
| 22 |
|
| 23 |
type |
| 24 |
{ Callback used by SetSceneColors. |
| 25 |
|
| 26 |
Given here VertexPosition is in local coordinates of the given Shape. |
| 27 |
Multiply by Shape.State.Transform to convert to scene coordinate space. |
| 28 |
|
| 29 |
VertexIndex is the direct index to Shape.Geometry.Coordinates. } |
| 30 |
TVertexColorEvent = function ( |
| 31 |
const Shape: TShape; |
| 32 |
const VertexPosition: TVector3; |
| 33 |
const VertexIndex: Integer): TCastleColorRGB of object; |
| 34 |
|
| 35 |
{ Set per-vertex color for all vertexes by a provided callback. |
| 36 |
This modifies scene shapes to add/modify TColorNode instances. |
| 37 |
|
| 38 |
This makes some assumptions about per-vertex colors setup of every Shape: |
| 39 |
- Assumes colorIndex is empty (default). |
| 40 |
- Assumes ColorPerVertex is TRUE (default). |
| 41 |
IOW, this routine assumes that we have one color corresponding to one coordinate (position). |
| 42 |
This is usually the case. |
| 43 |
} |
| 44 |
procedure SetSceneColors(const Scene: TCastleScene; const OnVertexColor: TVertexColorEvent); |
| 45 |
|
| 46 |
procedure RemoveSceneColors(const Scene: TCastleScene); |
| 47 |
|
| 48 |
implementation |
| 49 |
|
| 50 |
uses X3DFields, X3DNodes, CastleRenderOptions; |
| 51 |
|
| 52 |
procedure SetSceneColors(const Scene: TCastleScene; const OnVertexColor: TVertexColorEvent); |
| 53 |
var |
| 54 |
Geometry: TAbstractGeometryNode; |
| 55 |
State: TX3DGraphTraverseState; |
| 56 |
ShapeList: TShapeList; |
| 57 |
Shape: TShape; |
| 58 |
Coord: TMFVec3f; |
| 59 |
CoordVectors, ColorVectors: TVector3List; |
| 60 |
ColorField: TSFNode; |
| 61 |
I: Integer; |
| 62 |
ColorNode: TColorNode; |
| 63 |
begin |
| 64 |
ShapeList := Scene.Shapes.TraverseList( |
| 65 |
{ OnlyActive } false, { OnlyVisible } true, { OnlyCollidable } false); |
| 66 |
for Shape in ShapeList do |
| 67 |
begin |
| 68 |
{ Do not try to modify the "proxy" objects |
| 69 |
(like IndexedFaceSet auto-generated to render the Sphere). |
| 70 |
It makes no effect, as the "changed color" event causes the proxy to be regenerated. |
| 71 |
So we look only at OriginalGeometry/State. } |
| 72 |
Geometry := Shape.OriginalGeometry; |
| 73 |
State := Shape.OriginalState; |
| 74 |
|
| 75 |
if not Geometry.InternalCoord(State, Coord) then Continue; |
| 76 |
CoordVectors := Coord.Items; |
| 77 |
|
| 78 |
ColorField := Geometry.ColorField; |
| 79 |
if ColorField = nil then Continue; |
| 80 |
|
| 81 |
// calculate ColorNode, ColorVectors |
| 82 |
if ColorField.Value is TColorNode then |
| 83 |
ColorNode := TColorNode(ColorField.Value) |
| 84 |
else |
| 85 |
begin |
| 86 |
ColorNode := TColorNode.Create; |
| 87 |
ColorNode.Mode := cmModulate; |
| 88 |
ColorField.Value := ColorNode; |
| 89 |
// ColorField.Changed; // TODO: not enough, so we do Scene.ChangedAll |
| 90 |
end; |
| 91 |
ColorVectors := ColorNode.FdColor.Items; |
| 92 |
|
| 93 |
ColorVectors.Count := CoordVectors.Count; |
| 94 |
|
| 95 |
for I := 0 to CoordVectors.Count - 1 do |
| 96 |
ColorVectors.L[I] := OnVertexColor(Shape, CoordVectors.L[I], I); |
| 97 |
|
| 98 |
// ColorNode.FdColor.Changed; // TODO: not enough, so we do Scene.ChangedAll |
| 99 |
end; |
| 100 |
|
| 101 |
{ TODO: For some reason, the above Xxx.Changed are not enough, |
| 102 |
even with Scene.ProcessEvents. |
| 103 |
For now, just do (costly) ChangedAll. } |
| 104 |
Scene.ChangedAll; |
| 105 |
end; |
| 106 |
|
| 107 |
procedure RemoveSceneColors(const Scene: TCastleScene); |
| 108 |
var |
| 109 |
Geometry: TAbstractGeometryNode; |
| 110 |
//State: TX3DGraphTraverseState; |
| 111 |
ShapeList: TShapeList; |
| 112 |
Shape: TShape; |
| 113 |
ColorField: TSFNode; |
| 114 |
begin |
| 115 |
ShapeList := Scene.Shapes.TraverseList( |
| 116 |
{ OnlyActive } false, { OnlyVisible } true, { OnlyCollidable } false); |
| 117 |
for Shape in ShapeList do |
| 118 |
begin |
| 119 |
Geometry := Shape.OriginalGeometry; |
| 120 |
//State := Shape.OriginalState; |
| 121 |
|
| 122 |
ColorField := Geometry.ColorField; |
| 123 |
if ColorField = nil then Continue; |
| 124 |
|
| 125 |
ColorField.Value := nil; |
| 126 |
end; |
| 127 |
|
| 128 |
Scene.ChangedAll; |
| 129 |
end; |
| 130 |
|
| 131 |
end. |