mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
[unity] Added example component SkeletonRenderTexture to render to a render texture. Added fadeout example scene named 'RenderTexture FadeOut Transparency'. Closes #1337.
This commit is contained in:
parent
20c421c1d4
commit
9acc768353
@ -163,6 +163,7 @@
|
|||||||
* Improved `Advanced - Fix Prefab Override MeshFilter` property for `SkeletonRenderer` (and subclasses`SkeletonAnimation` and `SkeletonMecanim`), now providing an additional option to use a global value which can be set in `Edit - Preferences - Spine`.
|
* Improved `Advanced - Fix Prefab Override MeshFilter` property for `SkeletonRenderer` (and subclasses`SkeletonAnimation` and `SkeletonMecanim`), now providing an additional option to use a global value which can be set in `Edit - Preferences - Spine`.
|
||||||
* Timeline naming improvements: `Spine AnimationState Clip` Inspector parameter `Custom Duration` changed and inverted to `Default Mix Duration` for more clarity. Shortened all Timeline add track menu entries from: `Spine.Unity.Playables - <track type>` to `Spine - <track type>`, `Spine Animation State Track` to `SkeletonAnimation Track`, `Spine AnimationState Graphic Track` to `SkeletonGraphic Track`, and `Spine Skeleton Flip Track` to `Skeleton Flip Track`.
|
* Timeline naming improvements: `Spine AnimationState Clip` Inspector parameter `Custom Duration` changed and inverted to `Default Mix Duration` for more clarity. Shortened all Timeline add track menu entries from: `Spine.Unity.Playables - <track type>` to `Spine - <track type>`, `Spine Animation State Track` to `SkeletonAnimation Track`, `Spine AnimationState Graphic Track` to `SkeletonGraphic Track`, and `Spine Skeleton Flip Track` to `Skeleton Flip Track`.
|
||||||
* Timeline track appearance and Inspector: Tracks now show icons and track colors to make them easier to distinguish. When a Track is selected, the Inspector now shows an editable track name which was previously only editable at the Timeline asset.
|
* Timeline track appearance and Inspector: Tracks now show icons and track colors to make them easier to distinguish. When a Track is selected, the Inspector now shows an editable track name which was previously only editable at the Timeline asset.
|
||||||
|
* Added example component `SkeletonRenderTexture` to render a `SkeletonRenderer` to a `RenderTexture`, mainly for proper transparency. Added an example scene named `RenderTexture FadeOut Transparency` that demonstrates usage for a fadeout transparency effect.
|
||||||
|
|
||||||
* **Changes of default values**
|
* **Changes of default values**
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 28687d6c4c7e84e48a6907ea466198f9
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -0,0 +1,52 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
* Spine Runtimes License Agreement
|
||||||
|
* Last updated January 1, 2020. Replaces all prior versions.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013-2022, Esoteric Software LLC
|
||||||
|
*
|
||||||
|
* Integration of the Spine Runtimes into software or otherwise creating
|
||||||
|
* derivative works of the Spine Runtimes is permitted under the terms and
|
||||||
|
* conditions of Section 2 of the Spine Editor License Agreement:
|
||||||
|
* http://esotericsoftware.com/spine-editor-license
|
||||||
|
*
|
||||||
|
* Otherwise, it is permitted to integrate the Spine Runtimes into software
|
||||||
|
* or otherwise create derivative works of the Spine Runtimes (collectively,
|
||||||
|
* "Products"), provided that each user of the Products must obtain their own
|
||||||
|
* Spine Editor license and redistribution of the Products in any form must
|
||||||
|
* include this license and copyright notice.
|
||||||
|
*
|
||||||
|
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
|
||||||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
|
||||||
|
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Events;
|
||||||
|
|
||||||
|
namespace Spine.Unity.Examples {
|
||||||
|
public class RenderTextureFadeoutExample : MonoBehaviour {
|
||||||
|
|
||||||
|
public SkeletonRenderTexture skeletonRenderTexture;
|
||||||
|
public SkeletonRenderer normalSkeletonRenderer;
|
||||||
|
|
||||||
|
public void Update () {
|
||||||
|
float fadeoutAlpha = 1.0f - ((0.5f * Time.time) % 1.0f);
|
||||||
|
|
||||||
|
// changing transpacency at a MeshRenderer does not yield the desired effect
|
||||||
|
// due to overlapping attachment meshes.
|
||||||
|
normalSkeletonRenderer.Skeleton.SetColor(new Color(1, 1, 1, fadeoutAlpha));
|
||||||
|
|
||||||
|
// Thus we render the whole skeleton to a RenderTexture first using the
|
||||||
|
// SkeletonRenderTexture component.
|
||||||
|
// Changing transparency at a single quad with a RenderTexture works as desired.
|
||||||
|
skeletonRenderTexture.color.a = fadeoutAlpha;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: a4cc2b5fcffcac846aacb02b6dad0440
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1b5dc12395d030642b857afc9dff2ae2
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -0,0 +1,90 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!21 &2100000
|
||||||
|
Material:
|
||||||
|
serializedVersion: 6
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_Name: RenderQuadMaterial
|
||||||
|
m_Shader: {fileID: 4800000, guid: 1e0cc951f440af74dacaf86ac4ae2602, type: 3}
|
||||||
|
m_ShaderKeywords: _ALPHAPREMULTIPLY_ON _USE8NEIGHBOURHOOD_ON
|
||||||
|
m_LightmapFlags: 4
|
||||||
|
m_EnableInstancingVariants: 0
|
||||||
|
m_DoubleSidedGI: 0
|
||||||
|
m_CustomRenderQueue: -1
|
||||||
|
stringTagMap: {}
|
||||||
|
disabledShaderPasses: []
|
||||||
|
m_SavedProperties:
|
||||||
|
serializedVersion: 3
|
||||||
|
m_TexEnvs:
|
||||||
|
- _BumpMap:
|
||||||
|
m_Texture: {fileID: 0}
|
||||||
|
m_Scale: {x: 1, y: 1}
|
||||||
|
m_Offset: {x: 0, y: 0}
|
||||||
|
- _DetailAlbedoMap:
|
||||||
|
m_Texture: {fileID: 0}
|
||||||
|
m_Scale: {x: 1, y: 1}
|
||||||
|
m_Offset: {x: 0, y: 0}
|
||||||
|
- _DetailMask:
|
||||||
|
m_Texture: {fileID: 0}
|
||||||
|
m_Scale: {x: 1, y: 1}
|
||||||
|
m_Offset: {x: 0, y: 0}
|
||||||
|
- _DetailNormalMap:
|
||||||
|
m_Texture: {fileID: 0}
|
||||||
|
m_Scale: {x: 1, y: 1}
|
||||||
|
m_Offset: {x: 0, y: 0}
|
||||||
|
- _EmissionMap:
|
||||||
|
m_Texture: {fileID: 0}
|
||||||
|
m_Scale: {x: 1, y: 1}
|
||||||
|
m_Offset: {x: 0, y: 0}
|
||||||
|
- _MainTex:
|
||||||
|
m_Texture: {fileID: 0}
|
||||||
|
m_Scale: {x: 1, y: 1}
|
||||||
|
m_Offset: {x: 0, y: 0}
|
||||||
|
- _MetallicGlossMap:
|
||||||
|
m_Texture: {fileID: 0}
|
||||||
|
m_Scale: {x: 1, y: 1}
|
||||||
|
m_Offset: {x: 0, y: 0}
|
||||||
|
- _OcclusionMap:
|
||||||
|
m_Texture: {fileID: 0}
|
||||||
|
m_Scale: {x: 1, y: 1}
|
||||||
|
m_Offset: {x: 0, y: 0}
|
||||||
|
- _ParallaxMap:
|
||||||
|
m_Texture: {fileID: 0}
|
||||||
|
m_Scale: {x: 1, y: 1}
|
||||||
|
m_Offset: {x: 0, y: 0}
|
||||||
|
m_Floats:
|
||||||
|
- _BumpScale: 1
|
||||||
|
- _Cutoff: 0.5
|
||||||
|
- _DarkColorAlphaAdditive: 0
|
||||||
|
- _DetailNormalMapScale: 1
|
||||||
|
- _DstBlend: 10
|
||||||
|
- _GlossMapScale: 1
|
||||||
|
- _Glossiness: 0.5
|
||||||
|
- _GlossyReflections: 1
|
||||||
|
- _Metallic: 0
|
||||||
|
- _Mode: 3
|
||||||
|
- _OcclusionStrength: 1
|
||||||
|
- _OutlineMipLevel: 0
|
||||||
|
- _OutlineReferenceTexWidth: 1024
|
||||||
|
- _OutlineSmoothness: 1
|
||||||
|
- _OutlineWidth: 3
|
||||||
|
- _Parallax: 0.02
|
||||||
|
- _SmoothnessTextureChannel: 0
|
||||||
|
- _SpecularHighlights: 1
|
||||||
|
- _SrcBlend: 1
|
||||||
|
- _StencilComp: 8
|
||||||
|
- _StencilRef: 1
|
||||||
|
- _StraightAlphaInput: 0
|
||||||
|
- _ThresholdEnd: 0.25
|
||||||
|
- _UVSec: 0
|
||||||
|
- _Use8Neighbourhood: 1
|
||||||
|
- _ZWrite: 0
|
||||||
|
m_Colors:
|
||||||
|
- _Black: {r: 0, g: 0, b: 0, a: 0}
|
||||||
|
- _Color: {r: 1, g: 1, b: 1, a: 1}
|
||||||
|
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
|
||||||
|
- _OutlineColor: {r: 1, g: 1, b: 0, a: 1}
|
||||||
|
m_BuildTextureStacks: []
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 4c507f887c6274a44a603d96e0eabf2a
|
||||||
|
NativeFormatImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
mainObjectFileID: 2100000
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -0,0 +1,104 @@
|
|||||||
|
// Simple shader for e.g. a Quad that renders a RenderTexture.
|
||||||
|
// Texture color is multiplied by a color property, mostly for alpha fadeout.
|
||||||
|
Shader "Spine/RenderQuad" {
|
||||||
|
Properties{
|
||||||
|
_Color("Color", Color) = (1,1,1,1)
|
||||||
|
[NoScaleOffset] _MainTex("MainTex", 2D) = "white" {}
|
||||||
|
_Cutoff("Shadow alpha cutoff", Range(0,1)) = 0.1
|
||||||
|
[HideInInspector] _StencilRef("Stencil Reference", Float) = 1.0
|
||||||
|
[HideInInspector][Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Comparison", Float) = 8 // Set to Always as default
|
||||||
|
}
|
||||||
|
SubShader{
|
||||||
|
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane" }
|
||||||
|
Blend One OneMinusSrcAlpha
|
||||||
|
Cull Off
|
||||||
|
ZWrite Off
|
||||||
|
Lighting Off
|
||||||
|
|
||||||
|
Stencil {
|
||||||
|
Ref[_StencilRef]
|
||||||
|
Comp[_StencilComp]
|
||||||
|
Pass Keep
|
||||||
|
}
|
||||||
|
|
||||||
|
Pass {
|
||||||
|
Name "Normal"
|
||||||
|
|
||||||
|
CGPROGRAM
|
||||||
|
#pragma vertex vert
|
||||||
|
#pragma fragment frag
|
||||||
|
#include "UnityCG.cginc"
|
||||||
|
sampler2D _MainTex;
|
||||||
|
float4 _Color;
|
||||||
|
|
||||||
|
struct VertexInput {
|
||||||
|
float4 vertex : POSITION;
|
||||||
|
float2 uv : TEXCOORD0;
|
||||||
|
float4 vertexColor : COLOR;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexOutput {
|
||||||
|
float4 pos : SV_POSITION;
|
||||||
|
float2 uv : TEXCOORD0;
|
||||||
|
float4 vertexColor : COLOR;
|
||||||
|
};
|
||||||
|
|
||||||
|
VertexOutput vert(VertexInput v) {
|
||||||
|
VertexOutput o = (VertexOutput)0;
|
||||||
|
o.uv = v.uv;
|
||||||
|
o.vertexColor = v.vertexColor;
|
||||||
|
o.pos = UnityObjectToClipPos(v.vertex);
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
float4 frag(VertexOutput i) : SV_Target {
|
||||||
|
float4 texColor = tex2D(_MainTex,i.uv);
|
||||||
|
_Color.rgb *= _Color.a;
|
||||||
|
return texColor * _Color;
|
||||||
|
}
|
||||||
|
ENDCG
|
||||||
|
}
|
||||||
|
|
||||||
|
Pass {
|
||||||
|
Name "Caster"
|
||||||
|
Tags { "LightMode" = "ShadowCaster" }
|
||||||
|
Offset 1, 1
|
||||||
|
ZWrite On
|
||||||
|
ZTest LEqual
|
||||||
|
|
||||||
|
Fog { Mode Off }
|
||||||
|
Cull Off
|
||||||
|
Lighting Off
|
||||||
|
|
||||||
|
CGPROGRAM
|
||||||
|
#pragma vertex vert
|
||||||
|
#pragma fragment frag
|
||||||
|
#pragma multi_compile_shadowcaster
|
||||||
|
#pragma fragmentoption ARB_precision_hint_fastest
|
||||||
|
#include "UnityCG.cginc"
|
||||||
|
sampler2D _MainTex;
|
||||||
|
fixed _Cutoff;
|
||||||
|
|
||||||
|
struct VertexOutput {
|
||||||
|
V2F_SHADOW_CASTER;
|
||||||
|
float4 uvAndAlpha : TEXCOORD1;
|
||||||
|
};
|
||||||
|
|
||||||
|
VertexOutput vert(appdata_base v, float4 vertexColor : COLOR) {
|
||||||
|
VertexOutput o;
|
||||||
|
o.uvAndAlpha = v.texcoord;
|
||||||
|
o.uvAndAlpha.a = vertexColor.a;
|
||||||
|
TRANSFER_SHADOW_CASTER(o)
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
float4 frag(VertexOutput i) : SV_Target {
|
||||||
|
fixed4 texcol = tex2D(_MainTex, i.uvAndAlpha.xy);
|
||||||
|
clip(texcol.a* i.uvAndAlpha.a - _Cutoff);
|
||||||
|
SHADOW_CASTER_FRAGMENT(i)
|
||||||
|
}
|
||||||
|
ENDCG
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FallBack "Diffuse"
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1e0cc951f440af74dacaf86ac4ae2602
|
||||||
|
ShaderImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
defaultTextures: []
|
||||||
|
nonModifiableTextures: []
|
||||||
|
preprocessorOverride: 0
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -0,0 +1,223 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
* Spine Runtimes License Agreement
|
||||||
|
* Last updated January 1, 2020. Replaces all prior versions.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013-2022, Esoteric Software LLC
|
||||||
|
*
|
||||||
|
* Integration of the Spine Runtimes into software or otherwise creating
|
||||||
|
* derivative works of the Spine Runtimes is permitted under the terms and
|
||||||
|
* conditions of Section 2 of the Spine Editor License Agreement:
|
||||||
|
* http://esotericsoftware.com/spine-editor-license
|
||||||
|
*
|
||||||
|
* Otherwise, it is permitted to integrate the Spine Runtimes into software
|
||||||
|
* or otherwise create derivative works of the Spine Runtimes (collectively,
|
||||||
|
* "Products"), provided that each user of the Products must obtain their own
|
||||||
|
* Spine Editor license and redistribution of the Products in any form must
|
||||||
|
* include this license and copyright notice.
|
||||||
|
*
|
||||||
|
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
|
||||||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
|
||||||
|
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Rendering;
|
||||||
|
|
||||||
|
namespace Spine.Unity.Examples {
|
||||||
|
|
||||||
|
[RequireComponent(typeof(SkeletonRenderer))]
|
||||||
|
public class SkeletonRenderTexture : MonoBehaviour {
|
||||||
|
public Color color = Color.white;
|
||||||
|
public Material quadMaterial;
|
||||||
|
public Camera targetCamera;
|
||||||
|
public int maxRenderTextureSize = 1024;
|
||||||
|
protected SkeletonRenderer skeletonRenderer;
|
||||||
|
protected MeshRenderer meshRenderer;
|
||||||
|
protected MeshFilter meshFilter;
|
||||||
|
public GameObject quad;
|
||||||
|
protected MeshRenderer quadMeshRenderer;
|
||||||
|
protected MeshFilter quadMeshFilter;
|
||||||
|
protected Mesh quadMesh;
|
||||||
|
public RenderTexture renderTexture;
|
||||||
|
|
||||||
|
private CommandBuffer commandBuffer;
|
||||||
|
private MaterialPropertyBlock propertyBlock;
|
||||||
|
private readonly List<Material> materials = new List<Material>();
|
||||||
|
|
||||||
|
protected Vector2Int requiredRenderTextureSize;
|
||||||
|
protected Vector2Int allocatedRenderTextureSize;
|
||||||
|
|
||||||
|
void Awake () {
|
||||||
|
meshRenderer = this.GetComponent<MeshRenderer>();
|
||||||
|
meshFilter = this.GetComponent<MeshFilter>();
|
||||||
|
skeletonRenderer = this.GetComponent<SkeletonRenderer>();
|
||||||
|
if (targetCamera == null)
|
||||||
|
targetCamera = Camera.main;
|
||||||
|
|
||||||
|
commandBuffer = new CommandBuffer();
|
||||||
|
propertyBlock = new MaterialPropertyBlock();
|
||||||
|
|
||||||
|
CreateQuadChild();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnDestroy () {
|
||||||
|
if (renderTexture)
|
||||||
|
RenderTexture.ReleaseTemporary(renderTexture);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CreateQuadChild () {
|
||||||
|
quad = new GameObject(this.name + " RenderTexture", typeof(MeshRenderer), typeof(MeshFilter));
|
||||||
|
quad.transform.SetParent(this.transform.parent, false);
|
||||||
|
quadMeshRenderer = quad.GetComponent<MeshRenderer>();
|
||||||
|
quadMeshFilter = quad.GetComponent<MeshFilter>();
|
||||||
|
|
||||||
|
quadMesh = new Mesh();
|
||||||
|
quadMesh.MarkDynamic();
|
||||||
|
quadMesh.name = "RenderTexture Quad";
|
||||||
|
quadMesh.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor;
|
||||||
|
|
||||||
|
if (quadMaterial != null)
|
||||||
|
quadMeshRenderer.material = new Material(quadMaterial);
|
||||||
|
else
|
||||||
|
quadMeshRenderer.material = new Material(Shader.Find("Spine/RenderQuad"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnEnable () {
|
||||||
|
skeletonRenderer.OnMeshAndMaterialsUpdated += RenderOntoQuad;
|
||||||
|
meshRenderer.forceRenderingOff = true;
|
||||||
|
if (quadMeshRenderer)
|
||||||
|
quadMeshRenderer.gameObject.SetActive(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnDisable () {
|
||||||
|
skeletonRenderer.OnMeshAndMaterialsUpdated -= RenderOntoQuad;
|
||||||
|
meshRenderer.forceRenderingOff = false;
|
||||||
|
if (quadMeshRenderer)
|
||||||
|
quadMeshRenderer.gameObject.SetActive(false);
|
||||||
|
if (renderTexture)
|
||||||
|
RenderTexture.ReleaseTemporary(renderTexture);
|
||||||
|
allocatedRenderTextureSize = Vector2Int.zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RenderOntoQuad (SkeletonRenderer skeletonRenderer) {
|
||||||
|
PrepareForMesh();
|
||||||
|
RenderToRenderTexture();
|
||||||
|
AssignAtQuad();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void PrepareForMesh () {
|
||||||
|
Bounds boundsLocalSpace = meshFilter.sharedMesh.bounds;
|
||||||
|
Vector3 meshMinWorldSpace = transform.TransformPoint(boundsLocalSpace.min);
|
||||||
|
Vector3 meshMaxWorldSpace = transform.TransformPoint(boundsLocalSpace.max);
|
||||||
|
Vector3 meshMinXMaxYWorldSpace = new Vector3(meshMinWorldSpace.x, meshMaxWorldSpace.y);
|
||||||
|
Vector3 meshMaxXMinYWorldSpace = new Vector3(meshMaxWorldSpace.x, meshMinWorldSpace.y);
|
||||||
|
|
||||||
|
// We need to get the min/max of all four corners, close position and rotation of the skeleton
|
||||||
|
// in combination with perspective projection otherwise might lead to incorrect screen space min/max.
|
||||||
|
Vector3 meshMinProjected = targetCamera.WorldToScreenPoint(meshMinWorldSpace);
|
||||||
|
Vector3 meshMaxProjected = targetCamera.WorldToScreenPoint(meshMaxWorldSpace);
|
||||||
|
Vector3 meshMinXMaxYProjected = targetCamera.WorldToScreenPoint(meshMinXMaxYWorldSpace);
|
||||||
|
Vector3 meshMaxXMinYProjected = targetCamera.WorldToScreenPoint(meshMaxXMinYWorldSpace);
|
||||||
|
// To handle 180 degree rotation and thus min/max inversion, we get min/max of all four corners
|
||||||
|
Vector3 meshMinScreenSpace =
|
||||||
|
Vector3.Min(meshMinProjected, Vector3.Min(meshMaxProjected,
|
||||||
|
Vector3.Min(meshMinXMaxYProjected, meshMaxXMinYProjected)));
|
||||||
|
Vector3 meshMaxScreenSpace =
|
||||||
|
Vector3.Max(meshMinProjected, Vector3.Max(meshMaxProjected,
|
||||||
|
Vector3.Max(meshMinXMaxYProjected, meshMaxXMinYProjected)));
|
||||||
|
|
||||||
|
requiredRenderTextureSize = new Vector2Int(
|
||||||
|
Mathf.Min(maxRenderTextureSize, Mathf.CeilToInt(Mathf.Abs(meshMaxScreenSpace.x - meshMinScreenSpace.x))),
|
||||||
|
Mathf.Min(maxRenderTextureSize, Mathf.CeilToInt(Mathf.Abs(meshMaxScreenSpace.y - meshMinScreenSpace.y))));
|
||||||
|
|
||||||
|
PrepareRenderTexture();
|
||||||
|
PrepareCommandBuffer(meshMinWorldSpace, meshMaxWorldSpace);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void PrepareCommandBuffer (Vector3 meshMinWorldSpace, Vector3 meshMaxWorldSpace) {
|
||||||
|
commandBuffer.Clear();
|
||||||
|
commandBuffer.SetRenderTarget(renderTexture);
|
||||||
|
commandBuffer.ClearRenderTarget(true, true, Color.clear);
|
||||||
|
|
||||||
|
Matrix4x4 projectionMatrix = Matrix4x4.Ortho(
|
||||||
|
meshMinWorldSpace.x, meshMaxWorldSpace.x,
|
||||||
|
meshMinWorldSpace.y, meshMaxWorldSpace.y,
|
||||||
|
float.MinValue, float.MaxValue);
|
||||||
|
|
||||||
|
commandBuffer.SetProjectionMatrix(projectionMatrix);
|
||||||
|
commandBuffer.SetViewport(new Rect(Vector2.zero, requiredRenderTextureSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void RenderToRenderTexture () {
|
||||||
|
meshRenderer.GetPropertyBlock(propertyBlock);
|
||||||
|
meshRenderer.GetSharedMaterials(materials);
|
||||||
|
|
||||||
|
for (int i = 0; i < materials.Count; i++)
|
||||||
|
commandBuffer.DrawMesh(meshFilter.sharedMesh, transform.localToWorldMatrix,
|
||||||
|
materials[i], meshRenderer.subMeshStartIndex + i, -1, propertyBlock);
|
||||||
|
Graphics.ExecuteCommandBuffer(commandBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void AssignAtQuad () {
|
||||||
|
Vector2 min = meshFilter.sharedMesh.bounds.min;
|
||||||
|
Vector2 max = meshFilter.sharedMesh.bounds.max;
|
||||||
|
|
||||||
|
Vector3[] vertices = new Vector3[4] {
|
||||||
|
new Vector3(min.x, min.y, 0),
|
||||||
|
new Vector3(max.x, min.y, 0),
|
||||||
|
new Vector3(min.x, max.y, 0),
|
||||||
|
new Vector3(max.x, max.y, 0)
|
||||||
|
};
|
||||||
|
quadMesh.vertices = vertices;
|
||||||
|
|
||||||
|
int[] indices = new int[6] { 0, 2, 1, 2, 3, 1 };
|
||||||
|
quadMesh.triangles = indices;
|
||||||
|
|
||||||
|
Vector3[] normals = new Vector3[4] {
|
||||||
|
-Vector3.forward,
|
||||||
|
-Vector3.forward,
|
||||||
|
-Vector3.forward,
|
||||||
|
-Vector3.forward
|
||||||
|
};
|
||||||
|
quadMesh.normals = normals;
|
||||||
|
|
||||||
|
float maxU = (float)(requiredRenderTextureSize.x) / allocatedRenderTextureSize.x;
|
||||||
|
float maxV = (float)(requiredRenderTextureSize.y) / allocatedRenderTextureSize.y;
|
||||||
|
Vector2[] uv = new Vector2[4] {
|
||||||
|
new Vector2(0, 0),
|
||||||
|
new Vector2(maxU, 0),
|
||||||
|
new Vector2(0, maxV),
|
||||||
|
new Vector2(maxU, maxV)
|
||||||
|
};
|
||||||
|
quadMesh.uv = uv;
|
||||||
|
quadMeshFilter.mesh = quadMesh;
|
||||||
|
quadMeshRenderer.sharedMaterial.mainTexture = this.renderTexture;
|
||||||
|
quadMeshRenderer.sharedMaterial.color = color;
|
||||||
|
|
||||||
|
quadMeshRenderer.transform.position = this.transform.position;
|
||||||
|
quadMeshRenderer.transform.rotation = this.transform.rotation;
|
||||||
|
quadMeshRenderer.transform.localScale = this.transform.localScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void PrepareRenderTexture () {
|
||||||
|
Vector2Int textureSize = new Vector2Int(
|
||||||
|
Mathf.NextPowerOfTwo(requiredRenderTextureSize.x),
|
||||||
|
Mathf.NextPowerOfTwo(requiredRenderTextureSize.y));
|
||||||
|
|
||||||
|
if (textureSize != allocatedRenderTextureSize) {
|
||||||
|
if (renderTexture)
|
||||||
|
RenderTexture.ReleaseTemporary(renderTexture);
|
||||||
|
renderTexture = RenderTexture.GetTemporary(textureSize.x, textureSize.y);
|
||||||
|
allocatedRenderTextureSize = textureSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 25e6ceb271c9af848ae53f2af1073d0d
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Loading…
x
Reference in New Issue
Block a user