From 1aea0c56058ccacd5806d0b07751208289f1afab Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Fri, 22 Jun 2018 18:08:39 +0900 Subject: [PATCH] Bake particle mesh to render by CanvasRenderer --- Assets/Coffee.meta | 9 + Assets/Coffee/UIExtentions.meta | 9 + Assets/Coffee/UIExtentions/UIParticle.meta | 8 + .../UIExtentions/UIParticle/UIAdditive.mat | 83 ++++++++++ .../UIParticle/UIAdditive.mat.meta | 8 + .../UIExtentions/UIParticle/UIAdditive.shader | 93 +++++++++++ .../UIParticle/UIAdditive.shader.meta | 9 + .../UIExtentions/UIParticle/UIParticle.cs | 156 ++++++++++++++++++ .../UIParticle/UIParticle.cs.meta | 11 ++ 9 files changed, 386 insertions(+) create mode 100644 Assets/Coffee.meta create mode 100644 Assets/Coffee/UIExtentions.meta create mode 100644 Assets/Coffee/UIExtentions/UIParticle.meta create mode 100644 Assets/Coffee/UIExtentions/UIParticle/UIAdditive.mat create mode 100644 Assets/Coffee/UIExtentions/UIParticle/UIAdditive.mat.meta create mode 100644 Assets/Coffee/UIExtentions/UIParticle/UIAdditive.shader create mode 100644 Assets/Coffee/UIExtentions/UIParticle/UIAdditive.shader.meta create mode 100644 Assets/Coffee/UIExtentions/UIParticle/UIParticle.cs create mode 100644 Assets/Coffee/UIExtentions/UIParticle/UIParticle.cs.meta diff --git a/Assets/Coffee.meta b/Assets/Coffee.meta new file mode 100644 index 0000000..3d78345 --- /dev/null +++ b/Assets/Coffee.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 272b36b29229a4b77a55f62ec7f66f59 +folderAsset: yes +timeCreated: 1527427554 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Coffee/UIExtentions.meta b/Assets/Coffee/UIExtentions.meta new file mode 100644 index 0000000..937bdda --- /dev/null +++ b/Assets/Coffee/UIExtentions.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 4629233b7b58b4920825072af1f30974 +folderAsset: yes +timeCreated: 1527426508 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Coffee/UIExtentions/UIParticle.meta b/Assets/Coffee/UIExtentions/UIParticle.meta new file mode 100644 index 0000000..05229c7 --- /dev/null +++ b/Assets/Coffee/UIExtentions/UIParticle.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d22d727aa6f9843e98eff7de5e0f44d6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.mat b/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.mat new file mode 100644 index 0000000..8543b3e --- /dev/null +++ b/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.mat @@ -0,0 +1,83 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_Name: UIAdditive + m_Shader: {fileID: 4800000, guid: ecfa8f5732b504ef98fba10aa18d0326, type: 3} + m_ShaderKeywords: + 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: 2800000, guid: 975570a90d56c477582e12d440dc9931, type: 3} + 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 + - _ColorMask: 15 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _Stencil: 0 + - _StencilComp: 8 + - _StencilOp: 0 + - _StencilReadMask: 255 + - _StencilWriteMask: 255 + - _UVSec: 0 + - _UseUIAlphaClip: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} diff --git a/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.mat.meta b/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.mat.meta new file mode 100644 index 0000000..9a9e47c --- /dev/null +++ b/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e44a36783dcfc43bdab399a1adcef4f5 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.shader b/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.shader new file mode 100644 index 0000000..9a4b61c --- /dev/null +++ b/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.shader @@ -0,0 +1,93 @@ +Shader "UI/Additive" +{ + Properties + { + _MainTex ("Sprite Texture", 2D) = "white" {} + _Color ("Tint", Color) = (1,1,1,1) + + _StencilComp ("Stencil Comparison", Float) = 8 + _Stencil ("Stencil ID", Float) = 0 + _StencilOp ("Stencil Operation", Float) = 0 + _StencilWriteMask ("Stencil Write Mask", Float) = 255 + _StencilReadMask ("Stencil Read Mask", Float) = 255 + + _ColorMask ("Color Mask", Float) = 15 + } + + SubShader + { + Tags + { + "Queue"="Transparent" + "IgnoreProjector"="True" + "RenderType"="Transparent" + "PreviewType"="Plane" + "CanUseSpriteAtlas"="True" + } + + Stencil + { + Ref [_Stencil] + Comp [_StencilComp] + Pass [_StencilOp] + ReadMask [_StencilReadMask] + WriteMask [_StencilWriteMask] + } + + Cull Off + Lighting Off + ZWrite Off + ZTest [unity_GUIZTestMode] + Fog { Mode Off } + Blend One One + + ColorMask [_ColorMask] + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + #include "UnityCG.cginc" + + struct appdata_t + { + float4 vertex : POSITION; + float4 color : COLOR; + float2 texcoord : TEXCOORD0; + }; + + struct v2f + { + float4 vertex : SV_POSITION; + fixed4 color : COLOR; + half2 texcoord : TEXCOORD0; + }; + + fixed4 _Color; + + v2f vert(appdata_t IN) + { + v2f OUT; + OUT.vertex = UnityObjectToClipPos(IN.vertex); + OUT.texcoord = IN.texcoord; +#ifdef UNITY_HALF_TEXEL_OFFSET + OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1); +#endif + OUT.color = IN.color * _Color; + return OUT; + } + + sampler2D _MainTex; + + fixed4 frag(v2f IN) : SV_Target + { + half4 color = tex2D(_MainTex, IN.texcoord) * IN.color; + color.rgb *= color.a; + clip (color.a - 0.01); + return color; + } + ENDCG + } + } +} diff --git a/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.shader.meta b/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.shader.meta new file mode 100644 index 0000000..58d20b9 --- /dev/null +++ b/Assets/Coffee/UIExtentions/UIParticle/UIAdditive.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: ecfa8f5732b504ef98fba10aa18d0326 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Coffee/UIExtentions/UIParticle/UIParticle.cs b/Assets/Coffee/UIExtentions/UIParticle/UIParticle.cs new file mode 100644 index 0000000..ea32fc0 --- /dev/null +++ b/Assets/Coffee/UIExtentions/UIParticle/UIParticle.cs @@ -0,0 +1,156 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Profiling; +using UnityEngine.UI; + +//[RequireComponent(typeof(ParticleSystem))] +[ExecuteInEditMode] +public class UIParticle : MaskableGraphic +{ + static readonly int s_IdMainTex = Shader.PropertyToID("_MainTex"); + static readonly List s_Vertices = new List(); + + [SerializeField] ParticleSystem m_ParticleSystem; + [SerializeField] UIParticle m_TrailParticle; + + Mesh _mesh; + ParticleSystemRenderer _renderer; + + [HideInInspector] [SerializeField] bool m_IsTrail = false; + + + + public override Texture mainTexture + { + get + { + var mat = _renderer ? _renderer.sharedMaterial : defaultGraphicMaterial; + return mat && mat.HasProperty(s_IdMainTex) ? mat.mainTexture : s_WhiteTexture; ; + } + } + + public override Material GetModifiedMaterial(Material baseMaterial) + { + return base.GetModifiedMaterial(_renderer ? _renderer.sharedMaterial : baseMaterial); + } + + protected override void OnEnable() + { + m_ParticleSystem = m_ParticleSystem ? m_ParticleSystem : GetComponent(); + _renderer = m_ParticleSystem ? m_ParticleSystem.GetComponent() : null; + + _mesh = new Mesh(); + _mesh.MarkDynamic(); + CheckTrail(); + base.OnEnable(); + } + + protected override void OnDisable() + { + DestroyImmediate(_mesh); + _mesh = null; + CheckTrail(); + base.OnDisable(); + } + + void CheckTrail() + { + if (isActiveAndEnabled && !m_IsTrail && m_ParticleSystem && m_ParticleSystem.trails.enabled) + { + if (!m_TrailParticle) + { + m_TrailParticle = new GameObject("[UIParticle] Trail").AddComponent(); + var trans = m_TrailParticle.transform; + trans.SetParent(transform); + trans.localPosition = Vector3.zero; + trans.localRotation = Quaternion.identity; + trans.localScale = Vector3.one; + + m_TrailParticle._renderer = GetComponent(); + m_TrailParticle.m_ParticleSystem = GetComponent(); + m_TrailParticle.m_IsTrail = true; + } + m_TrailParticle.enabled = true; + } + else if (m_TrailParticle) + { + m_TrailParticle.enabled = false; + } + } + + protected override void UpdateGeometry() + { + } + + void Update() + { + Profiler.BeginSample("CheckTrail"); + CheckTrail(); + Profiler.EndSample(); + + if (m_ParticleSystem) + { + Profiler.BeginSample("Disable ParticleSystemRenderer"); + if (Application.isPlaying) + { + _renderer.enabled = false; + } + Profiler.EndSample(); + + Profiler.BeginSample("Make Matrix"); + var cam = canvas.worldCamera ?? Camera.main; + bool useTransform = false; + Matrix4x4 matrix = default(Matrix4x4); + switch (m_ParticleSystem.main.simulationSpace) + { + case ParticleSystemSimulationSpace.Local: + matrix = + Matrix4x4.Rotate(m_ParticleSystem.transform.rotation).inverse + * Matrix4x4.Scale(m_ParticleSystem.transform.lossyScale).inverse; + useTransform = true; + break; + case ParticleSystemSimulationSpace.World: + matrix = m_ParticleSystem.transform.worldToLocalMatrix; + break; + case ParticleSystemSimulationSpace.Custom: + break; + } + Profiler.EndSample(); + + _mesh.Clear(); + if (0 < m_ParticleSystem.particleCount) + { + Profiler.BeginSample("Bake Mesh"); + if (m_IsTrail) + { + _renderer.BakeTrailsMesh(_mesh, cam, useTransform); + } + else + { + _renderer.BakeMesh(_mesh, cam, useTransform); + } + Profiler.EndSample(); + + // Apply matrix. + Profiler.BeginSample("Apply matrix to position"); + _mesh.GetVertices(s_Vertices); + var count = s_Vertices.Count; + for (int i = 0; i < count; i++) + { + s_Vertices[i] = matrix.MultiplyPoint3x4(s_Vertices[i]); + } + _mesh.SetVertices(s_Vertices); + s_Vertices.Clear(); + Profiler.EndSample(); + } + + + // Set mesh to CanvasRenderer. + Profiler.BeginSample("Set mesh to CanvasRenderer"); + canvasRenderer.SetMesh(_mesh); + Profiler.EndSample(); + //canvasRenderer.SetColor(color); + } + } +} diff --git a/Assets/Coffee/UIExtentions/UIParticle/UIParticle.cs.meta b/Assets/Coffee/UIExtentions/UIParticle/UIParticle.cs.meta new file mode 100644 index 0000000..ab809ed --- /dev/null +++ b/Assets/Coffee/UIExtentions/UIParticle/UIParticle.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 16f0b0b6d0b7542bfbd20a3e05b04ff1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: -100 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: