CopyShader ( shader )
Parameters
| shader - shader handle to copy |
Description
|
Returns an independent copy of a shader with the same current settings. The copy shares the expensive compiled GPU program but has its own parameter values and texture bindings, taken from the original at the moment of copying. After that the two are independent: give one rim-light shader a blue rim and its copy an orange rim, and each model keeps its own colour. Prefer copying over calling LoadShader again when a new instance should start from an existing instance's tuned settings rather than the asset defaults. It is also the way to give two cameras independent versions of the same effect, since attaching one handle to both cameras shares its settings. Free the copy with FreeShader like any other shader handle. For the full story, see copying and lifetime in the shader guide. Requires Extended mode. See also: LoadShader, FreeShader, CameraEffect, ShaderColor. |
Example
; CopyShader Example ; ------------------ ; Requires Extended mode. Graphics3D 640,480,0,2 SetBuffer BackBuffer() camera=CreateCamera() PositionEntity camera,0,0,-6 light=CreateLight() RotateEntity light,30,-30,0 ; Copies share compiled code but keep independent parameter values original=LoadShader("assets/help_surface.b3shader") copy=CopyShader(original) ShaderColor original,"Tint",255,120,20 left=CreateSphere(24) PositionEntity left,-1.3,0,0 EntityShader left,original right=CreateSphere(24) PositionEntity right,1.3,0,0 EntityShader right,copy angle#=0 While Not KeyDown(1) ; Recolour only the copy - the original keeps its orange tint angle=angle+1 ShaderColor copy,"Tint",128+Sin(angle)*127,120,255 TurnEntity left,0,0.5,0 TurnEntity right,0,-0.5,0 ; Arrow keys move the camera If KeyDown(200) Then MoveEntity camera,0,0,0.1 If KeyDown(208) Then MoveEntity camera,0,0,-0.1 If KeyDown(203) Then TurnEntity camera,0,1,0 If KeyDown(205) Then TurnEntity camera,0,-1,0 RenderWorld Color 255,255,255 Text 0,0,"Arrow keys: move camera Esc: exit" Text 0,20,"Left: original shader Right: copy=CopyShader(original)" Text 0,40,"Only the copy's Tint changes each frame" Flip Wend FreeShader original FreeShader copy End
Index