Custom Shaders (Unity Plugin)

From PopcornFX
Jump to: navigation, search
Unity-logo-g.png


Available since Unity plugin v2.7.

Getting Started

Creation

  1. In the PopcornFX Editor, choose a shader group for a renderer of your effect.
    CS RendererShaderGroup.png
  2. Create a new folder named Shaders in StreamingAssets/PackFx.
  3. In this folder, create an empty file with the extension .hlsl for DirectX or .glsl for OpenGL.
  4. In the "Asset" menu click "PopcornFX > Create Missing Shader Assets".
    This will create the Unity shader asset linked with your shader text file in Ressources/PkFxShaders/Shaders.

Settings

CS CustomShaderSettings.png

  1. Choose your shader group, matching the one in your renderer.
  2. Select your rendering API.
  3. Select the options to match the material used in your renderer.
    This will change the Shader Types bellow.
  4. Click "Write default shaders"
    This will write the vertex and pixel shaders used in PopcornFX into your text file.

Use Custom Shaders

  1. Modify the shader in your text file to see some changes, for example the color in the pixel shader.
  2. Add the shader asset in the PKFxRenderingPlugin component on your camera.
    CS AssignShaderToCamera.gif
  3. Play and you should see your effect modified by the custom shader.



Features Reference

Fields

You can use your Particle Fields by adding them in input of your vertex shader. The name must start with PK_ followed by the name of your field.
For example with the LifeRatio

DirectX 9 and DirectX 11:

struct		VS_IN										
{														
	float4	ObjPos		: POSITION;						
	float4	Color		: COLOR;						
	float2	UV		: TEXCOORD0;					
	float	PK_LifeRatio	: TEXCOORD1;	
};

OpenGL:

in vec3 	InVertex;
in vec4 	InColor;
in vec2 	InTexcoord;
in float	PK_LifeRatio;

OpenGLES:

attribute vec3		InVertex;
attribute vec4		InColor;
attribute vec2		InTexcoord;
attribute float 	PK_LifeRatio;

Shader Constants

You can add Shader Constants in your vertex and pixel shader.
For example we will add Position_offset in the vertex shader and UV_offset in the pixel shader.

DirectX 9:

//Vertex Shader
float4x4	matWVP			: register(c0);
float2		PK_VS_Position_offset	: register(c4);
//...
//Pixel Shader
float2		PK_PS_UV_offset		: register(c0);

DirectX 11:

//Vertex Shader
cbuffer PK_VS : register(b1)
{
	float2	Position_offset;
};
//...
//Pixel Shader
cbuffer PK_PS : register(b1)
{
	float2	UV_offset;
};

OpenGL:

//Vertex Shader
uniform vec2	PK_Position_offset;
//...
//Pixel Shader
uniform vec2	PK_UV_offset;

OpenGLES:

//Vertex Shader
uniform mediump vec2	PK_Position_offset;
//...
//Pixel Shader
uniform mediump vec2	PK_UV_offset;


Now you can edit ther Shader Constant values in the inspector of your shader asset.
CS ShaderConstants.png
/!\ For OpenGL, you can edit the constant only if the Unity editor is launched with OpenGL.
Or in code using:

PkFxCustomShader.SetConstant()

Normals

You can add the Normals in input of your vertex shader.

DirectX 9 and DirectX 11:

struct		VS_IN
{
	float4	ObjPos	: POSITION;
	float4	Color	: COLOR;
	float2	UV	: TEXCOORD0;
	float3	Normal	: TEXCOORD1;
};

OpenGL:

in vec3	InVertex;
in vec4	InColor;
in vec2	InTexcoord;
in vec3	InNormal;

OpenGLES:

attribute vec3	InVertex;
attribute vec4	InColor;
attribute vec2	InTexcoord;
attribute vec3	InNormal;

Normal Maps

You can pass a second texture to your shader via the Normal texture in your renderer.
CS RendererNormalMap.png
DirectX 9:

sampler2D	NormalTexture : register(s1);

DirectX 11:

Texture2D	NormalTexture	: register(t1);
SamplerState	NormalSampler	: register(s1);

OpenGL and OpenGLES:

uniform sampler2D	NormalTexture;

Implemented Features By API and Renderer

DirectX 9

Billboard

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

Mesh

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

Ribbon

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

DirectX 11

Billboard

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

Mesh

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

Ribbon

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

OpenGL

Billboard

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

Mesh

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

Ribbon

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

OpenGLES

Billboard

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

Mesh

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map

Ribbon

Feature Status
Shader Override
Get Default Shaders
Fields
Constants
Normals
Normal Map