AnyPortrait > Script > Command Buffer
These are the functions to use Unity's Command Buffer as a target for AnyPortrait's characters.
The following functions belong to the apCustomCommandBuffer class, not apPortrait.
For instructions on how to write the code, please refer to the example code at the bottom of the page or the following page that explains an example of using the command buffer.
- Writing Command Buffer
You can also check the Unity official manual below for a detailed explanation of Unity's command buffer.
- Extending the Built-in Render Pipeline with CommandBuffers
- Scheduling and executing rendering commands in the Scriptable Render Pipeline
- CommandBuffer API
- CameraEvent API
Create a command buffer class targeting apPortrait.
Camera camera : Camera to render
apPortrait portrait : apPortrait for rendering
string commandBufferName : The name of the command buffer registered to the camera
Removes the command buffer from the registered camera.
You must call this function if any of the camera, apPortrait character or command buffer classes cease to exist.
Sets the timing that rendering is performed to the camera.
This function should be called when using the built-in rendering pipeline.
UnityEngine.Rendering.CameraEvent cameraEvent : An enum variable for rendering time
Sets the timing that rendering is performed to the camera.
This function should be called when using the Scriptable Render Pipeline (SRP).
This function is only supported in Unity 2019.1 or later versions that officially support SRP.
apCustomCommandBuffer.SRPRenderEvent srpRenderEvent : Render time in SRP
Specifies that a different material is applied when rendering the character using the command buffer.
The properties of the Material input as an argument are duplicated and reserved to be used for rendering, but "_MainTex" and "_Color" are not duplicated.
The shader of the material being replaced must follow the rules as "Custom Shader".
After this function is called, a drawing request must be made using the "DrawAllMeshesWithAlternativeMaterials" function.
If you specify materials as a Dictionary-type variable, different materials can be applied and rendered for multiple images of a character.
Material alternativeMaterial : Material to be replaced when rendering by the command buffer
Dictionary<string, Material> imageNameToAlterMaterialMap : Mapping variable assigned to replace different materials based on image name
Material unmatchedMaterial : Commonly applied material when no replacement target material can be found
Initializes the contents of the command buffer.
Sets the result of rendering the character with the camera to be saved in "Render Texture".
RenderTexture renderTexture : The texture to which the rendering result will be saved
Initializes the color or depth value of the set render target.
A function that can be called from a built-in rendering pipeline.
bool clearDepth : Whether to initialize the depth value
bool clearColor : Whether to initialize the color value
Color backgroundColor : Color that is initialized when clearColor is true
float depth : When clearDepth is true, the initialized depth value (default value is 1.0f)
Initializes the color or depth value of the set render target.
A function that can be called from the Scriptable Render Pipeline (SRP).
This function is only supported in Unity 2019.1 or later versions that officially support SRP.
UnityEngine.Rendering.RTClearFlags clearFlags : Flag variable that specifies the type of value to be initialized
Color backgroundColor : Color to be initialized
float depth : Initialized depth value (default is 1.0f)
uint stencil : Initialized stencil value (default is 0)
Specifies which value to be applied as the View Matrix to the command buffer when rendering.
If no argument is specified, the same value as the target camera's View Matrix is used for rendering.
This function is only supported in Unity 2019.1 or later, and this function must be called in that version.
Matrix4x4 customViewMatrix : View Matrix applied differently from camera settings
Specifies which value to be applied as the Projection Matrix when rendering to the command buffer.
If no parameters are specified, the same value as the target camera's Projection Matrix is used for rendering.
This function is only supported in Unity 2019.1 or later, and this function must be called in that version.
Matrix4x4 customProjectionMatrix : Projection Matrix applied differently from camera settings
Specifies that the mesh of the apOptTransform should be rendered.
The "apPortrait.GetOptTransform" function will help.
You can render with arbitrary changes to the Material or the World Matrix.
apOptTransform optTransform : apOptTransform with mesh to be rendered
Material material : Alternative material to be rendered
Matrix4x4 localToWorldMatrix : User-specified World Matrix
Specifies that all meshes of apPortrait currently displayed on the screen are to be rendered.
bool sortOrder : Whether to recalculate the rendering order
bool excludeClippedChildMeshes : Whether to exclude from rendering clipped meshes that are not compatible with the command buffer.
Material material : Alternative material used when rendering
It performs the same role as the "DrawAllMeshes" function, which specifies that all meshes of apPortrait displayed on the screen are to be rendered, but the alternative materials registered from the "CreateAlternativeMaterials" function are used.
bool sortOrder : Whether to recalculate the rendering order
bool excludeClippedChildMeshes : Whether to exclude from rendering clipped meshes that are not compatible with the command buffer.
Returns the Unity's command buffer instance.
UnityEngine.Rendering.CommandBuffer : Command buffer instance
using UnityEngine;
using AnyPortrait;
public class CmdBufferExample : MonoBehaviour
{
// Target Objects
public apPortrait portrait;
public Camera targetCamera;
// Custom Command Buffer
private apCustomCommandBuffer _commandBuffer = null;
void Start()
{
if (_commandBuffer == null)
{
portrait.Initialize();
_commandBuffer = new apCustomCommandBuffer(targetCamera, portrait, "Custom Command Buffer");
_commandBuffer.AddToCamera(UnityEngine.Rendering.CameraEvent.AfterForwardAlpha);
}
}
void OnDestroy()
{
if (_commandBuffer != null)
{
_commandBuffer.Destory();
_commandBuffer = null;
}
}
void LateUpdate()
{
if (_commandBuffer != null)
{
_commandBuffer.ClearCommands();
_commandBuffer.SetViewMatrix();
_commandBuffer.SetProjectionMatrix();
_commandBuffer.DrawAllMeshes(true, true);
}
}
}