Using custom shader in Rendered display mode

In Eyeshot 5 we added support for shaders for graphics cards with OpenGL greater than 3.3 to create a variety of effects in the Rendered display mode (shadows, Phong lighting, fade out of planar reflections...).

The user can create a custom shader and use it to affect the way entities are drawn.

Beware that this will override the shaders used by Eyeshot to draw that entity so you will lose all Eyeshot's effects.

Here's how to do it.

1. Define a class derived from Eyeshot's Shader class and a class to store the shader parameters:

    class MyShaderParameters 
    { 
        public int MyParam1; 

        public MyShaderParameters(int myParam1) 
        { 
            MyParam1 = myParam1; 
        } 
    } 

    private class MyShader : Shader 
    { 
        public MyShader(ShaderType type, string vertexCode, string fragmentCode) 
            : base(type, vertexCode, fragmentCode) 
        { 
        } 

        public override void SetParameters(object shaderParams) 
        { 
            Enable(); 

            MyShaderParameters shaderParameters = (MyShaderParameters)shaderParams; 

            gl.Uniform1i(GetUniformLocation("MyParam1"), shaderParameters.MyParam1); 

            Disable(); 
        } 
    }

 

2. Define a derived entity class that stores the custom shader and enables it in the SetShader() method:

    class MyMesh : Mesh 
    { 
        public Shader shader; 

        protected override void SetShader(HqrData hqrData) 
        { 
            // enable the custom shader for this entity 
            shader.Enable(); 
        } 
    }

 

3. After the Form has been loaded (for instance in the Form.OnLoad() method) create the shader, compile it, assing the parameters and assign the shader to the custom entity. Finally add the entity to the ViewportLayout:

   // Define the strings with the shader programs source code 
   string vertexShaderSource = "/*Vertex Shader Program goes here.*/"; 
   string fragmentShaderSource = "/*Fragment Shader Program goes here.*/"; 

   // Create the shader and compile it 
   // Parameter ShaderType is not meaningful in this case 
   Shader myShader = new MyShader(ShaderType.Standard, vertexShaderSource, fragmentShaderSource); 
   bool success = myShader.Compile(); 

   if (success) 
   { 
   	// Create the parameters and set them to the shader 
	int myParam1 = 3; 
	MyShaderParameters shaderParams = new MyShaderParameters(myParam1); 
        myShader.SetParameters(shaderParams); 

        // Create the custom entity and assign the shader to it 
        MyMesh myMesh = Mesh.CreateSphere<MyMesh>(10, 10, 10); 
        myMesh.shader = myShader; 

        // add the custom entity to the ViewportLayout             
        viewportLayout1.Entities.Add(myMesh); 
   }
Was this article helpful?
3 out of 3 found this helpful
Have more questions? Submit a request

Comments

2 comments
  • Is this still valid for the EyeShot 7?

    If it is, it is missing some crucial steps: for almost any shader, I am going to need MVP matrix. It is precalculated somewhere inside EyeShot, can I get to it somehow, or do I need to recalculate it?

  • Hi,

    yes, it's still valid for Eyeshot 7.

    You can get the modelview and projection matrices from the Camera:

    viewportLayout.Camera.modelViewMatrix;
    viewportLayout.Camera.projectionMatrix;

Please sign in to leave a comment.