Coder Social home page Coder Social logo

asdlei99 / textureupdateexample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from keijiro/textureupdateexample

0.0 1.0 0.0 246 KB

An example showing how to update textures from a native plugin in Unity.

Home Page: https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.IssuePluginCustomTextureUpdate.html

Shell 2.68% C++ 25.62% C 68.16% C# 3.54%

textureupdateexample's Introduction

TextureUpdateExample

gif

Old-school plasma effect generated by C++ code

This is an example that shows how to use the CustomTextureUpdate callback that allows native plugins to update contents of textures in a thread safe and platform agnostic way.

How to implement the CustomTextureUpdate callback

The callback function should be implemented with the following signature:

void TextureUpdateCallback(int eventID, void *data)
{
  UnityRenderingExtTextureUpdateParamsV2 *params = data;
}

The type of the event will be given to eventID, and the attributes of the target texture will be given with UnityRenderingExtTextureUpdateParamsV2 struct carried by the data pointer.

The possible values of eventID are defined in UnityRenderingExtEventType; Only kUnityRenderingExtEventUpdateTextureBeginV2 and kUnityRenderingExtEventUpdateTextureEndV2 are relevant to the texture update callback.

kUnityRenderingExtEventUpdateTextureBeginV2 event

This event is invoked right before updating the texture. You can give raw image data via the texData pointer in the parameter struct.

if (eventID == kUnityRenderingExtEventUpdateTextureBeginV2)
{
  uint8_t *img = malloc(params->width * params->height * 4);

  // Fill image data here.

  params->texData = img;
}

You can also give nullptr to texData when you don't like to update the texture in this frame.

kUnityRenderingExtEventUpdateTextureEndV2 event

This event is invoked right after updating the texture. You can safely release resources used to update the texture.

if (event == kUnityRenderingExtEventUpdateTextureEndV2)
{
  free(params->texData);
}

Interface function

You have to implement an interface function that is used to retrieve the pointer of the callback function.

UnityRenderingEventAndData UNITY_INTERFACE_EXPORT GetTextureUpdateCallback()
{
  return TextureUpdateCallback;
}

For further details of the plugin implementation, please see the example source code contained in this repository.

How to update texture from C# script

In order to request texture update from a C# script, you can use IssuePluginCustomTextureUpdateV2 with a CommandBuffer. The pointer to the callback function and a reference to a texture object should be given to the command. You can also give a single uint value to the command that can be used as user data to the callback.

[DllImport("DllName")] static extern IntPtr GetTextureUpdateCallback();

var callback = GetTextureUpdateCallback();
m_CommandBuffer.IssuePluginCustomTextureUpdateV2(callback, texture, userData);
Graphics.ExecuteCommandBuffer(m_CommandBuffer);

textureupdateexample's People

Contributors

keijiro avatar wip- avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.