Page 1 of 1

EntityCustomParameter

Posted: Thu Oct 25, 2012 3:40 am
by idle
to use EntityCustomParameter in glsl hlsl you need to use param_named_auto in you material script

param_named_auto Var custom Index

Code: Select all

vertex_program_ref Ocean2VS
			{
				param_named_auto WaveFreq custom 1 
				param_named_auto WaveAmp custom 2 
				param_named_auto BumpScale custom 3 
				param_named_auto ScaleX custom 4 
				param_named_auto ScaleY custom 5
				param_named_auto SpeedX custom 6 
				param_named_auto SpeedY custom 7 				
				param_named_auto eyePosition camera_position_object_space
				param_named_auto time time_0_x 100.0
			}

Then in your vertex shader

Code: Select all

 
uniform float WaveFreq;
uniform	float WaveAmp;
uniform	float BumpScale; 
uniform	float ScaleX;
uniform	float ScaleY;
uniform	float SpeedX;
uniform	float SpeedY;
uniform vec3 eyePosition;
uniform float time;  
In your source Enumerate your shader inputs

Code: Select all

Enumeration 1
#WaveFreq  
#WaveAmp 
#BumpScale  
#TextureScaleX  
#TextureScaleY 
#WaveSpeedX  
#WaveSpeedY 
EndEnumeration 

Then once you've loaded and assigned your material to an entity

Code: Select all

 
     
      ocean\matOcean2 = GetScriptMaterial(#PB_Any,"Ocean2") 
      
      CreatePlane(1,5000,5000,50,50,1,1)  ;create a surface to render the ocean on 
      ocean\entity = CreateEntity(#PB_Any, MeshID(1), MaterialID(ocean\matOcean2)) ;set the material to the entity
      
      ;set up the shader input control variables       
      WaveFreq = 0.028 
      WaveAmp = 1.8 
      BumpScale = 0.2 
      TextureScaleX = 25
      TextureScaleY = 25 
      WaveSpeedX = 0.015
      WaveSpeedY = 0.005
      ;set shader values 
      EntityCustomParameter(ocean\entity,0,#WaveFreq,WaveFreq,0,0,0)
      EntityCustomParameter(ocean\entity,0,#WaveAmp,WaveAmp,0,0,0)
      EntityCustomParameter(ocean\entity,0,#BumpScale,BumpScale,0,0,0)
      EntityCustomParameter(ocean\entity,0,#TextureScaleX,TextureScaleX,0,0,0)
      EntityCustomParameter(ocean\entity,0,#TextureScaleY,TextureScaleY,0,0,0)
      EntityCustomParameter(ocean\entity,0,#WaveSpeedX,WaveSpeedX,0,0,0)
      EntityCustomParameter(ocean\entity,0,#WaveSpeedY,WaveSpeedY,0,0,0)

Re: EntityCustomParameter

Posted: Wed Aug 14, 2024 5:38 pm
by Mesa
Could you create a fully functional example for the doc, please ?

Mesa (Documentation moderator)

Re: EntityCustomParameter

Posted: Wed Aug 14, 2024 6:09 pm
by idle
There was a full example of it in the 3d examples, ocean shader but it got removed from the distribution due to changes to the 3d engine. That's some time ago.

Re: EntityCustomParameter

Posted: Wed Aug 14, 2024 11:45 pm
by pf shadoko
I made a shader using 2 custom parameters
- einflate (entity inflation)
at the start of the “vertex” program: ... uniform float einflate;//+85 1
- ecolor (entity color)
at start of “fragment” program: ... uniform vec4 ecolor;//+85 2

+85 means that uniform is a custom parameter (see “CreateShader” documentation)
1 and 2 are parameter indexes

Code: Select all

;  ----------------------------------------------------------------------------------------------------------
;   shader - EntityCustomParameter
; ----------------------------------------------------------------------------------------------------------

InitEngine3D():InitSprite():InitKeyboard():InitMouse()

ExamineDesktops()
OpenWindow(0, 0,0, DesktopWidth(0)*0.8,DesktopHeight(0)*0.8, "CreateShader - [Esc] quit",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)

CreateCamera(0, 0, 0, 100, 100):MoveCamera(0,0,0,-10):CameraLookAt(0,0,0,0)
CreateLight(0,$ffffff, -10000, 10000, 0)
AmbientColor($111111*3)
CameraBackColor(0,$444488)

Define.s vert_pg,frag_pg
vert_pg="%#version 130%%uniform float einflate;//+85 1%uniform mat4 worldviewproj_matrix;//+24%uniform vec4 camera_pos_os;//+77%uniform vec4 light_pos_os;//+44 0%uniform vec4 light_att;//+41 0%uniform vec4 fog_params;//+31%%varying vec3 oviewdir;%varying vec3 olightdir;%varying vec3 onormal;%varying vec4 ovcolor;%varying float olightatt;%varying float ofogf;%%void main()%{%vec3 vertex=gl_Vertex.xyz+(gl_Normal*einflate);%oviewdir=normalize(camera_pos_os.xyz-vertex);%olightdir=normalize(light_pos_os.xyz-vertex);%gl_Position=worldviewproj_matrix*vec4(vertex,1);%onormal=gl_Normal;%float Dist=distance(light_pos_os,gl_Vertex);%olightatt=1/(light_att.y+light_att.z*Dist+light_att.w*Dist*Dist);%ovcolor=gl_Color;%ofogf=fog_params.z>0?min(abs(gl_Position.z)*fog_params.w,1):0;%}%%%%%%%%%%"
frag_pg="%#version 130%%uniform vec4 ecolor;//+85 2%uniform vec4 diffuse_ml;//+69 0%uniform vec4 specular_ml;//+70 0%uniform vec4 ambient_ml;//+67 0%uniform float shininess;//+36%uniform vec4 fog_colour;//+30%%varying vec3 oviewdir;%varying vec3 olightdir;%varying vec3 onormal;%varying vec4 ovcolor;%varying float olightatt;%varying float ofogf;%%void main()%{%vec3 normal=normalize(onormal);if(gl_FrontFacing==false)normal*=-1;%vec3 viewdir=normalize(oviewdir);%vec3 lightdir=normalize(olightdir);%float dif=max(dot(lightdir,normal),0)*olightatt;%float spe=pow(max(dot(normalize(lightdir+viewdir),normal),0),shininess);%vec4 color=ecolor*(ambient_ml+diffuse_ml*dif)+specular_ml*spe;%gl_FragColor=mix(color,fog_colour,ofogf);%}%%%%%%%"

CreateShader(0,vert_pg,frag_pg)
CreateShaderMaterial(0,0)
MaterialShininess(0,64,$ffffff)
CreateTorus(0,1,0.5,32,32)
For i=0 To 4
  CreateEntity(i,MeshID(0),MaterialID(0),(i-2)*3,0,0)
  col=Random($ffffff)
  ;we can define a different color for each entity
  EntityCustomParameter(i,0,2,Red(col)/255,Green(col)/255,Blue(col)/255,0); color must be converted in 4 float (0->1)
Next


Define.f MouseX,Mousey,depx,depz,dist,ag

Repeat
  While WindowEvent():Wend
  ExamineKeyboard()
  ExamineMouse()
  ag+0.1
  For i=0 To 4
    RotateEntity(i,0.2,0.2,0.2, #PB_Relative)
    ;we can Define a different inflate value For each entity
    EntityCustomParameter(i,0,1,Sin(ag+i)*0.25,0,0,0)
  Next  
  RenderWorld()
  FlipBuffers()    
Until KeyboardReleased(#PB_Key_Escape) Or MouseButton(3)