Skip to content

Commit

Permalink
[unity] Working render textures with unity backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyfa committed Nov 17, 2020
1 parent 8c7937c commit 12d0eab
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
35 changes: 28 additions & 7 deletions plugins/unity/runtime/src/backend/Draw.hx
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,43 @@ class Draw #if !completion implements spec.Draw #end {
// TODO update unity render target
//var renderTexture:backend.impl.CeramicRenderTexture = cast renderTarget.backendItem;
//luxeRenderer.target = renderTexture;
var backendItem:TextureImpl = renderTarget.backendItem;
var unityRenderTexture = backendItem.unityRenderTexture;

untyped __cs__('UnityEngine.Rendering.CommandBuffer cmd = (UnityEngine.Rendering.CommandBuffer){0}', commandBuffer);
untyped __cs__('cmd.SetRenderTarget((UnityEngine.RenderTexture){0})', unityRenderTexture);

untyped __cs__('var cameraHeight = 2*UnityEngine.Camera.main.orthographicSize');
untyped __cs__('var cameraWidth = cameraHeight*UnityEngine.Camera.main.aspect');

var camWidth:Int = untyped __cs__('(int)cameraWidth');
var camHeight:Int = untyped __cs__('(int)cameraHeight');

updateProjectionMatrix(
renderTarget.width,
renderTarget.height
);

// Not really ideal, we invert main camera transformation
// because it is used for everything, including render targets
// That works though

var translateX = ((backendItem.width * camWidth / renderTarget.width) - backendItem.width) * 0.5;
var translateY = ((backendItem.height * camHeight / renderTarget.height) - backendItem.height) * 0.5;

_renderTargetTransform.identity();
_renderTargetTransform.scale(renderTarget.density, renderTarget.density);
_renderTargetTransform.scale(
renderTarget.density * camWidth / renderTarget.width,
renderTarget.density * camHeight / renderTarget.height
);
_renderTargetTransform.translate(-translateX, -translateY);

updateViewMatrix(
renderTarget.density,
renderTarget.width,
renderTarget.height,
_renderTargetTransform,
-1
-1, -1
);

updateCurrentMatrix();
Expand All @@ -262,7 +284,6 @@ class Draw #if !completion implements spec.Draw #end {
// TODO clear
//if (renderTarget.clearOnRender) Luxe.renderer.clear(blackTransparentColor);
if (renderTarget.clearOnRender) {
untyped __cs__('UnityEngine.Rendering.CommandBuffer cmd = (UnityEngine.Rendering.CommandBuffer){0}', commandBuffer);
untyped __cs__('cmd.ClearRenderTarget(true, true, new UnityEngine.Color(0f, 0f, 0f, 0f), 1f)');
}

Expand Down Expand Up @@ -337,7 +358,7 @@ class Draw #if !completion implements spec.Draw #end {

}

inline function updateViewMatrix(density:Float, width:Float, height:Float, ?transform:ceramic.Transform, flipY:Float = 1):Void {
inline function updateViewMatrix(density:Float, width:Float, height:Float, ?transform:ceramic.Transform, flipX:Float = 1, flipY:Float = 1):Void {

if (transform != null) {
_modelViewTransform.setToTransform(transform);
Expand All @@ -352,13 +373,13 @@ class Draw #if !completion implements spec.Draw #end {
_modelViewTransform.scale(density, density);
_modelViewTransform.translate(tx, ty);

if (flipY == -1) {
// Flip vertically (needed when we are rendering to texture)
if (flipX == -1 || flipY == -1) {
// Flip vertically/horizontally (may be needed when we are rendering to texture)
_modelViewTransform.translate(
-width * 0.5,
-height * 0.5
);
_modelViewTransform.scale(1, -1);
_modelViewTransform.scale(flipX, flipY);
_modelViewTransform.translate(
width * 0.5,
height * 0.5
Expand Down
9 changes: 7 additions & 2 deletions plugins/unity/runtime/src/backend/Materials.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Materials {
* If such material doesn't exist yet, creates and instance
*/
public function get(
texture:backend.Texture,
texture:backend.TextureImpl,
shader:backend.Shader,
srcRgb:backend.BlendMode,
dstRgb:backend.BlendMode,
Expand Down Expand Up @@ -53,7 +53,12 @@ class Materials {
repository.push(materialData);

if (texture != null) {
untyped __cs__('material.mainTexture = {0}', texture.unityTexture);
if (texture.unityTexture != null) {
untyped __cs__('material.mainTexture = (UnityEngine.Texture2D){0}', texture.unityTexture);
}
else if (texture.unityRenderTexture != null) {
untyped __cs__('material.mainTexture = (UnityEngine.RenderTexture){0}', texture.unityRenderTexture);
}
}
else {
untyped __cs__('material.mainTexture = {0}', null);
Expand Down
7 changes: 6 additions & 1 deletion plugins/unity/runtime/src/backend/Screen.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package backend;

class Screen implements tracker.Events #if !completion implements spec.Screen #end {

public function new() {}
public function new() {

width = untyped __cs__('UnityEngine.Screen.width');
height = untyped __cs__('UnityEngine.Screen.height');

}

var width:Int = 0;

Expand Down
7 changes: 4 additions & 3 deletions plugins/unity/runtime/src/backend/TextureImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ class TextureImpl {

public function new(path:String, unityTexture:Texture2D, unityRenderTexture:Dynamic) {

this.textureId = unityTexture.GetInstanceID();
this.path = path;
this.unityTexture = unityTexture;
this.unityRenderTexture = unityRenderTexture;

if (unityTexture != null) {
this.width = unityTexture.width;
this.height = unityTexture.height;
this.textureId = unityTexture.GetInstanceID();
}
else if (unityRenderTexture != null) {
this.width = untyped __cs__('((UnityEngine.RenderTexture){0}).width', unityRenderTexture);
this.height = untyped __cs__('((UnityEngine.RenderTexture){0}).height', unityRenderTexture);
this.width = untyped __cs__('(int)((UnityEngine.RenderTexture){0}).width', unityRenderTexture);
this.height = untyped __cs__('(int)((UnityEngine.RenderTexture){0}).height', unityRenderTexture);
this.textureId = untyped __cs__('(int)((UnityEngine.RenderTexture){0}).GetInstanceID()', unityRenderTexture);
}

}
Expand Down
5 changes: 4 additions & 1 deletion plugins/unity/runtime/src/backend/Textures.hx
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,12 @@ class Textures implements spec.Textures {
else {
loadedTextures.remove(id);
loadedTexturesRetainCount.remove(id);
if (id.startsWith('pixels:') || id.startsWith('render:')) {
if (id.startsWith('pixels:')) {
untyped __cs__('UnityEngine.Object.Destroy({0})', (texture:TextureImpl).unityTexture);
}
else if (id.startsWith('render:')) {
untyped __cs__('((UnityEngine.RenderTexture){0}).Release()', (texture:TextureImpl).unityRenderTexture);
}
else {
untyped __cs__('UnityEngine.Resources.UnloadAsset({0})', (texture:TextureImpl).unityTexture);
}
Expand Down

0 comments on commit 12d0eab

Please sign in to comment.