From 18e1f9a976e03ba5a1a29d287acffc55fe41a3e5 Mon Sep 17 00:00:00 2001 From: pharan Date: Fri, 18 Nov 2016 16:52:07 +0800 Subject: [PATCH] [unity] ToddRivers Sprite Shader --- .../spine-unity/Modules/Shaders/Sprite.meta | 9 + .../Modules/Shaders/Sprite/Editor.meta | 9 + .../Sprite/Editor/SpineSpriteShaderGUI.cs | 515 ++++++++++++++++++ .../Editor/SpineSpriteShaderGUI.cs.meta | 12 + .../Modules/Shaders/Sprite/README.md | 46 ++ .../Modules/Shaders/Sprite/README.md.meta | 8 + .../Modules/Shaders/Sprite/ShaderShared.cginc | 383 +++++++++++++ .../Shaders/Sprite/ShaderShared.cginc.meta | 9 + .../Shaders/Sprite/SpriteLighting.cginc | 136 +++++ .../Shaders/Sprite/SpriteLighting.cginc.meta | 9 + .../Shaders/Sprite/SpritePixelLighting.cginc | 201 +++++++ .../Sprite/SpritePixelLighting.cginc.meta | 9 + .../Shaders/Sprite/SpriteShadows.cginc | 52 ++ .../Shaders/Sprite/SpriteShadows.cginc.meta | 9 + .../Modules/Shaders/Sprite/SpriteUnlit.cginc | 67 +++ .../Shaders/Sprite/SpriteUnlit.cginc.meta | 9 + .../Shaders/Sprite/SpriteVertexLighting.cginc | 439 +++++++++++++++ .../Sprite/SpriteVertexLighting.cginc.meta | 9 + .../Shaders/Sprite/SpritesPixelLit.shader | 133 +++++ .../Sprite/SpritesPixelLit.shader.meta | 9 + .../Shaders/Sprite/SpritesUnlit.shader | 82 +++ .../Shaders/Sprite/SpritesUnlit.shader.meta | 9 + .../Shaders/Sprite/SpritesVertexLit.shader | 102 ++++ .../Sprite/SpritesVertexLit.shader.meta | 9 + 24 files changed, 2275 insertions(+) create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor/SpineSpriteShaderGUI.cs create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor/SpineSpriteShaderGUI.cs.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/README.md create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/README.md.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/ShaderShared.cginc create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/ShaderShared.cginc.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteLighting.cginc create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteLighting.cginc.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritePixelLighting.cginc create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritePixelLighting.cginc.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteShadows.cginc create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteShadows.cginc.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteUnlit.cginc create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteUnlit.cginc.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteVertexLighting.cginc create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteVertexLighting.cginc.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesPixelLit.shader create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesPixelLit.shader.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesUnlit.shader create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesUnlit.shader.meta create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesVertexLit.shader create mode 100644 spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesVertexLit.shader.meta diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite.meta new file mode 100644 index 000000000..f99750310 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: a831a8ed72a588a48b2fb892e7f37371 +folderAsset: yes +timeCreated: 1479419399 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor.meta new file mode 100644 index 000000000..c34b55854 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 7bebbafa671002646b3a7267b32a0d60 +folderAsset: yes +timeCreated: 1479419399 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor/SpineSpriteShaderGUI.cs b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor/SpineSpriteShaderGUI.cs new file mode 100644 index 000000000..643a6cb07 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor/SpineSpriteShaderGUI.cs @@ -0,0 +1,515 @@ +/****************************************************************************** + * Spine Runtimes Software License v2.5 + * + * Copyright (c) 2013-2016, Esoteric Software + * All rights reserved. + * + * You are granted a perpetual, non-exclusive, non-sublicensable, and + * non-transferable license to use, install, execute, and perform the Spine + * Runtimes software and derivative works solely for personal or internal + * use. Without the written permission of Esoteric Software (see Section 2 of + * the Spine Software License Agreement), you may not (a) modify, translate, + * adapt, or develop new applications using the Spine Runtimes or otherwise + * create derivative works or improvements of the Spine Runtimes or (b) remove, + * delete, alter, or obscure any trademarks or any copyright, trademark, patent, + * or other intellectual property or proprietary rights notices on or in the + * Software, including any copy thereof. Redistributions in binary or source + * form must include this license and terms. + * + * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "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 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 THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + *****************************************************************************/ + +using System; +using UnityEngine; +using UnityEditor; + +public class SpineSpriteShaderGUI : ShaderGUI { + static readonly string kShaderVertexLit = "Spine/Sprite/Vertex Lit"; + static readonly string kShaderPixelLit = "Spine/Sprite/Pixel Lit"; + static readonly string kShaderUnlit = "Spine/Sprite/Unlit"; + static readonly int kSolidQueue = 2000; + static readonly int kAlphaTestQueue = 2450; + static readonly int kTransparentQueue = 3000; + + enum eBlendMode { + PreMultipliedAlpha, + StandardAlpha, + Solid, + Additive, + SoftAdditive, + Multiply, + Multiplyx2, + } + + enum eLightMode { VertexLit, PixelLit, Unlit, } + + enum eCulling { + Off = 0, + Back = 2, + Front = 1, + } + + MaterialProperty _mainTexture = null; + MaterialProperty _color = null; + MaterialProperty _blendMode = null; + + MaterialProperty _emissionMap = null; + MaterialProperty _emissionColor = null; + MaterialProperty _emissionPower = null; + + MaterialProperty _writeToDepth = null; + MaterialProperty _depthAlphaCutoff = null; + MaterialProperty _shadowAlphaCutoff = null; + MaterialProperty _renderQueue = null; + MaterialProperty _culling = null; + + MaterialProperty _overlayColor = null; + MaterialProperty _hue = null; + MaterialProperty _saturation = null; + MaterialProperty _brightness = null; + + MaterialProperty _rimPower = null; + MaterialProperty _rimColor = null; + + MaterialEditor _materialEditor; + + //Normals + MaterialProperty _bumpMap = null; + MaterialProperty _diffuseRamp = null; + MaterialProperty _fixedNormal = null; + + //Blend texture + MaterialProperty _blendTexture = null; + MaterialProperty _blendTextureLerp = null; + + bool _firstTimeApply = true; + eLightMode _lightMode; + + #region ShaderGUI + public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] properties) { + FindProperties(properties); // MaterialProperties can be animated so we do not cache them but fetch them every event to ensure animated values are updated correctly + _materialEditor = materialEditor; + Material material = materialEditor.target as Material; + + ShaderPropertiesGUI(material); + + // Make sure that needed keywords are set up if we're switching some existing + // material to a standard shader. + if (_firstTimeApply) { + SetMaterialKeywords(material); + SetLightModeFromShader(material); + _firstTimeApply = false; + } + } + + public override void AssignNewShaderToMaterial (Material material, Shader oldShader, Shader newShader) { + base.AssignNewShaderToMaterial(material, oldShader, newShader); + SetMaterialKeywords(material); + SetLightModeFromShader(material); + } + #endregion + + #region Virtual Interface + protected virtual void FindProperties(MaterialProperty[] props) { + _mainTexture = FindProperty("_MainTex", props); + _color = FindProperty("_Color", props); + _blendMode = FindProperty("_BlendMode", props); + + _emissionMap = FindProperty("_EmissionMap", props, false); + _emissionColor = FindProperty("_EmissionColor", props, false); + _emissionPower = FindProperty("_EmissionPower", props, false); + + _writeToDepth = FindProperty("_ZWrite", props); + _depthAlphaCutoff = FindProperty("_Cutoff", props); + _shadowAlphaCutoff = FindProperty("_ShadowAlphaCutoff", props); + _renderQueue = FindProperty("_RenderQueue", props); + _culling = FindProperty("_Cull", props); + + _bumpMap = FindProperty("_BumpMap", props, false); + _diffuseRamp = FindProperty("_DiffuseRamp", props, false); + _fixedNormal = FindProperty("_FixedNormal", props, false); + _blendTexture = FindProperty("_BlendTex", props, false); + _blendTextureLerp = FindProperty("_BlendAmount", props, false); + + _overlayColor = FindProperty("_OverlayColor", props, false); + _hue = FindProperty("_Hue", props, false); + _saturation = FindProperty("_Saturation", props, false); + _brightness = FindProperty("_Brightness", props, false); + + _rimPower = FindProperty("_RimPower", props, false); + _rimColor = FindProperty("_RimColor", props, false); + } + + protected virtual void ShaderPropertiesGUI(Material material) { + // Use default labelWidth + EditorGUIUtility.labelWidth = 0f; + + // Detect any changes to the material + EditorGUI.BeginChangeCheck(); + { + //GUILayout.Label("Rendering", EditorStyles.boldLabel); + { + RenderModes(material); + } + + GUILayout.Label("Main Maps", EditorStyles.boldLabel); + { + RenderTextureProperties(material); + } + + GUILayout.Label("Depth", EditorStyles.boldLabel); + { + RenderDepthProperties(material); + } + + if (_fixedNormal != null) + { + GUILayout.Label("Normals", EditorStyles.boldLabel); + RenderNormalsProperties(material); + } + + GUILayout.Label("Shadows", EditorStyles.boldLabel); + { + RenderShadowsProperties(material); + } + + GUILayout.Label("Color Adjustment", EditorStyles.boldLabel); + { + RenderColorProperties(material); + } + + if (_emissionMap != null && _emissionColor != null) + { + GUILayout.Label("Emission", EditorStyles.boldLabel); + { + RenderEmissionProperties(material); + } + } + + if (_rimColor != null) + { + GUILayout.Label("Rim Lighting", EditorStyles.boldLabel); + RenderRimLightingProperties(material); + } + + GUILayout.Label("Fog", EditorStyles.boldLabel); + { + RenderFogProperties(material); + } + } + + if (EditorGUI.EndChangeCheck()) + { + foreach (var obj in _blendMode.targets) + MaterialChanged((Material)obj); + } + } + + protected virtual void RenderModes (Material material) { + LightingModePopup(); + BlendModePopup(); + + EditorGUI.BeginChangeCheck(); + int renderQueue = EditorGUILayout.IntSlider("Renderer Queue", (int)_renderQueue.floatValue, 0, 49); + if (EditorGUI.EndChangeCheck()) + { + material.SetInt("_RenderQueue", renderQueue); + } + + EditorGUI.BeginChangeCheck(); + eCulling culling = (eCulling)Mathf.RoundToInt(_culling.floatValue); + culling = (eCulling)EditorGUILayout.EnumPopup("Culling", culling); + if (EditorGUI.EndChangeCheck()) + { + material.SetInt("_Cull", (int)culling); + } + } + + protected virtual void RenderTextureProperties (Material material) { + _materialEditor.TexturePropertySingleLine(new GUIContent("Albedo"), _mainTexture, _color); + + if (_bumpMap != null) + _materialEditor.TexturePropertySingleLine(new GUIContent("Normal Map"), _bumpMap); + + if (_diffuseRamp != null) + _materialEditor.TexturePropertySingleLine(new GUIContent("Diffuse Ramp", "A black and white gradient can be used to create a 'Toon Shading' effect."), _diffuseRamp); + + if (_blendTexture != null) + { + EditorGUI.BeginChangeCheck(); + _materialEditor.TexturePropertySingleLine(new GUIContent("Blend Texture", "When a blend texture is set the albedo will be a mix of the blend texture and main texture based on the blend amount."), _blendTexture, _blendTextureLerp); + if (EditorGUI.EndChangeCheck()) + { + SetKeyword(material, "_TEXTURE_BLEND", _blendTexture != null); + } + } + + _materialEditor.TextureScaleOffsetProperty(_mainTexture); + } + + protected virtual void RenderEmissionProperties (Material material) { + bool emission = material.IsKeywordEnabled("_EMISSION"); + + EditorGUI.BeginChangeCheck(); + emission = EditorGUILayout.Toggle("Enable Emission", emission); + if (EditorGUI.EndChangeCheck()) + { + SetKeyword(material, "_EMISSION", emission); + } + + if (emission) + { + _materialEditor.TexturePropertyWithHDRColor(new GUIContent("Emission"), _emissionMap, _emissionColor, new ColorPickerHDRConfig(0,1, 0.01010101f, 3), true); + _materialEditor.FloatProperty(_emissionPower, "Emission Power"); + } + } + + protected virtual void RenderDepthProperties (Material material) { + EditorGUI.BeginChangeCheck(); + bool writeTodepth = EditorGUILayout.Toggle(new GUIContent("Write to Depth", "Write to Depth Buffer by clipping alpha."), _writeToDepth.floatValue != 0.0f); + if (EditorGUI.EndChangeCheck()) + { + material.SetInt("_ZWrite", writeTodepth ? 1 : 0); + } + + if (writeTodepth) + { + _materialEditor.RangeProperty(_depthAlphaCutoff, "Depth Alpha Cutoff"); + } + } + + protected virtual void RenderNormalsProperties (Material material) { + EditorGUI.BeginChangeCheck(); + bool fixedNormals = material.IsKeywordEnabled("_FIXED_NORMALS"); + bool fixedNormalsBackRendering = material.IsKeywordEnabled("_FIXED_NORMALS_BACK_RENDERING"); + + bool meshNormals = EditorGUILayout.Toggle(new GUIContent("Use Mesh Normals", "If this is unticked instead of requiring mesh normals a Fixed Normal will be used instead (it's quicker and can result in better looking lighting effects on 2d objects)."), + !fixedNormals && !fixedNormalsBackRendering); + if (EditorGUI.EndChangeCheck()) + { + SetKeyword(material, "_FIXED_NORMALS", meshNormals ? false : fixedNormalsBackRendering ? false : true); + SetKeyword(material, "_FIXED_NORMALS_BACK_RENDERING", meshNormals ? false : fixedNormalsBackRendering); + } + + if (!meshNormals) + { + Vector3 normal = EditorGUILayout.Vector3Field(new GUIContent("Fixed Normal", "Defined in Camera Space. Should normally be (0,0,-1)."), _fixedNormal.vectorValue); + _fixedNormal.vectorValue = new Vector4(normal.x, normal.y, normal.z, 1.0f); + + EditorGUI.BeginChangeCheck(); + + + + bool backRendering = EditorGUILayout.Toggle(new GUIContent("Fixed Normal Back Rendering", "Tick only if you are going to rotate the sprite to face away from the camera, the fixed normal will be flipped to compensate."), + material.IsKeywordEnabled("_FIXED_NORMALS_BACK_RENDERING")); + if (EditorGUI.EndChangeCheck()) + { + SetKeyword(material, "_FIXED_NORMALS_BACK_RENDERING", backRendering); + SetKeyword(material, "_FIXED_NORMALS", !backRendering); + } + } + } + + protected virtual void RenderShadowsProperties (Material material) { + _materialEditor.FloatProperty(_shadowAlphaCutoff, "Shadow Alpha Cutoff"); + } + + protected virtual void RenderColorProperties (Material material) { + EditorGUI.BeginChangeCheck(); + bool colorAdjust = EditorGUILayout.Toggle("Enable Color Adjustment", material.IsKeywordEnabled("_COLOR_ADJUST")); + if (EditorGUI.EndChangeCheck()) + { + SetKeyword(material, "_COLOR_ADJUST", colorAdjust); + } + + if (colorAdjust) { + _materialEditor.ColorProperty(_overlayColor, "Overlay Color"); + _materialEditor.RangeProperty(_hue, "Hue"); + _materialEditor.RangeProperty(_saturation, "Saturation"); + _materialEditor.RangeProperty(_brightness, "Brightness"); + } + } + + protected virtual void RenderRimLightingProperties (Material material) { + EditorGUI.BeginChangeCheck(); + bool rimLighting = EditorGUILayout.Toggle("Enable Rim Lighting", material.IsKeywordEnabled("_RIM_LIGHTING")); + if (EditorGUI.EndChangeCheck()) + { + SetKeyword(material, "_RIM_LIGHTING", rimLighting); + } + + if (rimLighting) + { + _materialEditor.ColorProperty(_rimColor, "Rim Color"); + _materialEditor.FloatProperty(_rimPower, "Rim Power"); + } + } + + protected virtual void RenderFogProperties (Material material) { + EditorGUI.BeginChangeCheck(); + bool fog = EditorGUILayout.Toggle("Enable Fog", material.IsKeywordEnabled("_FOG")); + if (EditorGUI.EndChangeCheck()) + { + SetKeyword(material, "_FOG", fog); + } + } + #endregion + + void SetLightModeFromShader (Material material) { + if (material.shader.name == kShaderPixelLit) + { + _lightMode = eLightMode.PixelLit; + } + else if (material.shader.name == kShaderUnlit) + { + _lightMode = eLightMode.Unlit; + } + else + { + _lightMode = eLightMode.VertexLit; + } + } + + void SetShaderFromLightMode() { + Material material = _materialEditor.target as Material; + + switch (_lightMode) + { + case eLightMode.VertexLit: + if (material.shader.name != kShaderVertexLit) + _materialEditor.SetShader(Shader.Find(kShaderVertexLit), false); + break; + case eLightMode.PixelLit: + if (material.shader.name != kShaderPixelLit) + _materialEditor.SetShader(Shader.Find(kShaderPixelLit), false); + break; + case eLightMode.Unlit: + if (material.shader.name != kShaderUnlit) + _materialEditor.SetShader(Shader.Find(kShaderUnlit), false); + break; + } + + MaterialChanged(material); + } + + static void SetMaterialKeywords(Material material) { + eBlendMode blendMode = (eBlendMode)material.GetFloat("_BlendMode"); + + bool normalMap = material.HasProperty("_BumpMap") && material.GetTexture("_BumpMap") != null; + SetKeyword (material, "_NORMALMAP", normalMap); + + bool zWrite = material.GetFloat("_ZWrite") > 0.0f; + bool clipAlpha = zWrite && material.GetFloat("_Cutoff") > 0.0f; + SetKeyword(material, "_ALPHA_CLIP", clipAlpha); + + bool diffuseRamp = material.HasProperty("_DiffuseRamp") && material.GetTexture("_DiffuseRamp") != null; + SetKeyword(material, "_DIFFUSE_RAMP", diffuseRamp); + + bool blendTexture = material.HasProperty("_BlendTex") && material.GetTexture("_BlendTex") != null; + SetKeyword(material, "_TEXTURE_BLEND", blendTexture); + + SetKeyword(material, "_ALPHAPREMULTIPLY_ON", blendMode == eBlendMode.PreMultipliedAlpha); + SetKeyword(material, "_MULTIPLYBLEND", blendMode == eBlendMode.Multiply); + SetKeyword(material, "_MULTIPLYBLEND_X2", blendMode == eBlendMode.Multiplyx2); + SetKeyword(material, "_ADDITIVEBLEND", blendMode == eBlendMode.Additive); + SetKeyword(material, "_ADDITIVEBLEND_SOFT", blendMode == eBlendMode.SoftAdditive); + + int renderQueue; + + switch (blendMode) { + case eBlendMode.Solid: + { + material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); + material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); + material.SetOverrideTag("RenderType", "Opaque"); + renderQueue = kSolidQueue; + } + break; + case eBlendMode.Additive: + { + material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); + material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One); + material.SetOverrideTag("RenderType", "Transparent"); + renderQueue = kTransparentQueue; + } + break; + case eBlendMode.SoftAdditive: + { + material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); + material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcColor); + material.SetOverrideTag("RenderType", "Transparent"); + renderQueue = kTransparentQueue; + } + break; + case eBlendMode.Multiply: + { + material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.Zero); + material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.SrcColor); + material.SetOverrideTag("RenderType", "Transparent"); + renderQueue = kTransparentQueue; + } + break; + case eBlendMode.Multiplyx2: + { + material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.DstColor); + material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.SrcColor); + material.SetOverrideTag("RenderType", "Transparent"); + renderQueue = kTransparentQueue; + } + break; + case eBlendMode.PreMultipliedAlpha: + case eBlendMode.StandardAlpha: + default: + { + material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); + material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); + material.SetOverrideTag("RenderType", zWrite ? "TransparentCutout" : "Transparent"); + renderQueue = zWrite ? kAlphaTestQueue : kTransparentQueue; + } + break; + } + + material.renderQueue = renderQueue + material.GetInt("_RenderQueue"); + } + + static void MaterialChanged (Material material) { + SetMaterialKeywords(material); + } + + static void SetKeyword (Material m, string keyword, bool state) { + if (state) + m.EnableKeyword (keyword); + else + m.DisableKeyword (keyword); + } + + void LightingModePopup () + { + EditorGUI.BeginChangeCheck(); + _lightMode = (eLightMode)EditorGUILayout.Popup("Lighting Mode", (int)_lightMode, Enum.GetNames(typeof(eLightMode))); + if (EditorGUI.EndChangeCheck()) + { + SetShaderFromLightMode(); + } + } + + void BlendModePopup () { + eBlendMode mode = (eBlendMode)_blendMode.floatValue; + EditorGUI.BeginChangeCheck(); + mode = (eBlendMode)EditorGUILayout.Popup("Blend Mode", (int)mode, Enum.GetNames(typeof(eBlendMode))); + if (EditorGUI.EndChangeCheck()) + { + _materialEditor.RegisterPropertyChangeUndo("Blend Mode"); + _blendMode.floatValue = (float)mode; + } + } +} diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor/SpineSpriteShaderGUI.cs.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor/SpineSpriteShaderGUI.cs.meta new file mode 100644 index 000000000..2215e75ed --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/Editor/SpineSpriteShaderGUI.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: aef90b4c481362e42891bb46de344c1c +timeCreated: 1479458475 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/README.md b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/README.md new file mode 100644 index 000000000..15d7899d9 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/README.md @@ -0,0 +1,46 @@ +Contributed by ToddRivers + +# Unity Sprite Shaders +An Uber Shader specialised for rendering Sprites in Unity. +Even though it's designed for Sprites it can be used for a whole range of uses. It supports a wide range of optional shader features that won't effect performance unless they are used. +It also supports per-pixel effects such as normal maps and diffuse ramping whilst using Vertex Lit rendering. + +### Lighting +The shaders support lighting using both Forward Rendering and Vertex Lit Rendering. +Forward rendering is more accurate but is slower and crucially means the sprite has to write to depth using alpha clipping to avoid overdraw. +Vertex lit means all lighting can be done in one pass meaning full alpha can be used. + +### Normal Mapping +Normals maps are supported in both lighting modes (in Vertex Lit rendering data for normal mapping is packed into texture channels and then processed per pixel). + +### Blend Modes +Easily switch between blend modes including pre-multiplied alpha, additive, multiply etc. + +### Rim Lighting +Camera-space rim lighting is supported in both lighting modes. + +### Diffuse Ramp +A ramp texture is optionally supported for toon shading effects. + +### Shadows +Shadows are supported using alpha clipping. + +### Gradient based Ambient lighting +Both lighting modes support using a gradient for ambient light. In Vertex Lit mode the Spherical Harmonics is approximated from the ground, equator and sky colors. + +### Emission Map +An optional emission map is supported. + +### Camera Space Normals +As sprites are 2d their normals will always be constant. The shaders allow you to define a fixed normal in camera space rather than pass through mesh normals. +This not only saves vertex throughput but means lighting looks less 'flat' for rendering sprites with a perspective camera. + +### Color Adjustment +The shaders allow optional adjustment of hue / saturation and brightness as well as applying a solid color overlay effect for flashing a sprite to a solid color (eg. for damage effects). + +### Fog +Fog is optionally supported + + +## To Use +On your object's material click the drop down for shader and select Spine\Sprite\Pixel Lit, Vertex Lit or Unlit. diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/README.md.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/README.md.meta new file mode 100644 index 000000000..78cb908db --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/README.md.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cecd0ea162097a94c89a97af6baf0a66 +timeCreated: 1479457854 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/ShaderShared.cginc b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/ShaderShared.cginc new file mode 100644 index 000000000..aa76e3902 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/ShaderShared.cginc @@ -0,0 +1,383 @@ +#ifndef SHADER_SHARED_INCLUDED +#define SHADER_SHARED_INCLUDED + +#include "UnityCG.cginc" + +//////////////////////////////////////// +// Space functions +// + +inline float4 calculateWorldPos(float4 vertex) +{ + return mul(unity_ObjectToWorld, vertex); +} + +inline float4 calculateLocalPos(float4 vertex) +{ + return mul(UNITY_MATRIX_MVP, vertex); +} + +inline half3 calculateWorldNormal(float3 normal) +{ + return UnityObjectToWorldNormal(normal); +} + +//////////////////////////////////////// +// Maths functions +// + +inline float dotClamped(float3 a, float3 b) +{ + #if (SHADER_TARGET < 30 || defined(SHADER_API_PS3)) + return saturate(dot(a, b)); + #else + return max(0.0h, dot(a, b)); + #endif +} + +inline float oneDividedBy(float value) +{ + //Catches NANs + float sign_value = sign(value); + float sign_value_squared = sign_value*sign_value; + return sign_value_squared / ( value + sign_value_squared - 1.0); +} + +inline float4 quat_from_axis_angle(float3 axis, float angleRadians) +{ + float4 qr; + float half_angle = (angleRadians * 0.5); + qr.x = axis.x * sin(half_angle); + qr.y = axis.y * sin(half_angle); + qr.z = axis.z * sin(half_angle); + qr.w = cos(half_angle); + return qr; +} + +inline float3 rotate_vertex_position(float3 position, float3 axis, float angleRadians) +{ + float4 q = quat_from_axis_angle(axis, angleRadians); + float3 v = position.xyz; + return v + 2.0 * cross(q.xyz, cross(q.xyz, v) + q.w * v); +} + +//////////////////////////////////////// +// Normal map functions +// + +#if defined(_NORMALMAP) + +uniform sampler2D _BumpMap; + +inline half3 calculateWorldTangent(float4 tangent) +{ + return UnityObjectToWorldDir(tangent); +} + +inline half3 calculateWorldBinormal(half3 normalWorld, half3 tangentWorld, float tangentW) +{ + // For odd-negative scale transforms we need to flip the binormal + return cross(normalWorld, tangentWorld.xyz) * tangentW * unity_WorldTransformParams.w; +} + +inline half3 calculateNormalFromBumpMap(float2 texUV, half3 tangentWorld, half3 binormalWorld, half3 normalWorld) +{ + half3 localNormal = UnpackNormal(tex2D(_BumpMap, texUV)); + half3x3 rotation = half3x3(tangentWorld, binormalWorld, normalWorld); + half3 normal = normalize(mul(localNormal, rotation)); + return normal; +} + +#endif // _NORMALMAP + +#if defined(_DIFFUSE_RAMP) + +//////////////////////////////////////// +// Diffuse ramp functions +// + +uniform sampler2D _DiffuseRamp; + +inline fixed3 calculateDiffuseRamp(float ramp) +{ + return tex2D(_DiffuseRamp, float2(ramp, ramp)).rgb; +} + +inline fixed3 calculateRampedDiffuse(fixed3 lightColor, float attenuation, float angleDot) +{ + float ramp = clamp(((angleDot * 0.5) + 0.5) * attenuation, 0.0, 1.0); + return lightColor * calculateDiffuseRamp(ramp); +} +#endif // _DIFFUSE_RAMP + +//////////////////////////////////////// +// Blending functions +// + +inline fixed4 calculateLitPixel(fixed4 texureColor, fixed4 color, fixed3 lighting) : SV_Target +{ + fixed4 finalPixel; + +#if defined(_ALPHAPREMULTIPLY_ON) + //Pre multiplied alpha + finalPixel = texureColor * color; + finalPixel.rgb *= lighting * color.a; +#elif defined(_MULTIPLYBLEND) + //Multiply + finalPixel = color * texureColor; + finalPixel.rgb *= lighting; + finalPixel = lerp(fixed4(1,1,1,1), finalPixel, finalPixel.a); +#elif defined(_MULTIPLYBLEND_X2) + //Multiply x2 + finalPixel.rgb = texureColor.rgb * color.rgb * lighting * 2.0f; + finalPixel.a = color.a * texureColor.a; + finalPixel = lerp(fixed4(0.5f,0.5f,0.5f,0.5f), finalPixel, finalPixel.a); +#elif defined(_ADDITIVEBLEND) + //Additive + finalPixel = texureColor * 2.0f * color; + finalPixel.rgb *= lighting * color.a; +#elif defined(_ADDITIVEBLEND_SOFT) + //Additive soft + finalPixel = texureColor * color; + finalPixel.rgb *= lighting * finalPixel.a; +#else + finalPixel.a = texureColor.a * color.a; + finalPixel.rgb = texureColor.rgb * color.rgb * (lighting * finalPixel.a); +#endif + + return finalPixel; +} + +inline fixed4 calculateLitPixel(fixed4 texureColor, fixed3 lighting) : SV_Target +{ + fixed4 finalPixel; + +#if defined(_ALPHAPREMULTIPLY_ON) + //Pre multiplied alpha + finalPixel = texureColor; + finalPixel.rgb *= lighting; +#elif defined(_MULTIPLYBLEND) + //Multiply + finalPixel = texureColor; + finalPixel.rgb *= lighting; + finalPixel = lerp(fixed4(1,1,1,1), finalPixel, finalPixel.a); +#elif defined(_MULTIPLYBLEND_X2) + //Multiply x2 + finalPixel.rgb = texureColor.rgb * lighting * 2.0f; + finalPixel.a = texureColor.a; + finalPixel = lerp(fixed4(0.5f,0.5f,0.5f,0.5f), finalPixel, finalPixel.a); +#elif defined(_ADDITIVEBLEND) + //Additive + finalPixel = texureColor * 2.0f; + finalPixel.rgb *= lighting; +#elif defined(_ADDITIVEBLEND_SOFT) + //Additive soft + finalPixel = texureColor; + finalPixel.rgb *= lighting * finalPixel.a; +#else + finalPixel.a = texureColor.a; + finalPixel.rgb = texureColor.rgb * (lighting * finalPixel.a); +#endif + + return finalPixel; +} + +inline fixed4 calculateAdditiveLitPixel(fixed4 texureColor, fixed4 color, fixed3 lighting) : SV_Target +{ + fixed4 finalPixel; + +#if defined(_ALPHAPREMULTIPLY_ON) + //Pre multiplied alpha + finalPixel.rgb = texureColor.rgb * lighting * color.rgb * color.a; + finalPixel.a = 1.0; +#else + //All other alpha + finalPixel.rgb = (texureColor.rgb * lighting * color.rgb) * (texureColor.a * color.a); + finalPixel.a = 1.0; +#endif + + return finalPixel; +} + +inline fixed4 calculatePixel(fixed4 texureColor, fixed4 color) : SV_Target +{ + fixed4 finalPixel; + +#if defined(_ALPHAPREMULTIPLY_ON) + //Pre multiplied alpha + finalPixel = texureColor * color; + finalPixel.rgb *= color.a; +#elif defined(_MULTIPLYBLEND) + //Multiply + finalPixel = color * texureColor; + finalPixel = lerp(fixed4(1,1,1,1), finalPixel, finalPixel.a); +#elif defined(_MULTIPLYBLEND_X2) + //Multiply x2 + finalPixel.rgb = texureColor.rgb * color.rgb * 2.0f; + finalPixel.a = color.a * texureColor.a; + finalPixel = lerp(fixed4(0.5f,0.5f,0.5f,0.5f), finalPixel, finalPixel.a); +#elif defined(_ADDITIVEBLEND) + //Additive + finalPixel = texureColor * 2.0f * color; +#elif defined(_ADDITIVEBLEND_SOFT) + //Additive soft + finalPixel = color * texureColor; + finalPixel.rgb *= finalPixel.a; +#else + //Standard alpha + finalPixel.a = texureColor.a * color.a; + finalPixel.rgb = (texureColor.rgb * color.rgb) * finalPixel.a; +#endif + + return finalPixel; +} + +inline fixed4 calculatePixel(fixed4 texureColor) : SV_Target +{ + fixed4 finalPixel; + +#if defined(_ALPHAPREMULTIPLY_ON) + //Pre multiplied alpha + finalPixel = texureColor; +#elif defined(_MULTIPLYBLEND) + //Multiply + finalPixel = texureColor; + finalPixel = lerp(fixed4(1,1,1,1), finalPixel, finalPixel.a); +#elif defined(_MULTIPLYBLEND_X2) + //Multiply x2 + finalPixel.rgb = texureColor.rgb * 2.0f; + finalPixel.a = texureColor.a; + finalPixel = lerp(fixed4(0.5f,0.5f,0.5f,0.5f), finalPixel, finalPixel.a); +#elif defined(_ADDITIVEBLEND) + //Additive + finalPixel = texureColor * 2.0f; +#elif defined(_ADDITIVEBLEND_SOFT) + //Additive soft + finalPixel = texureColor; + finalPixel.rgb *= finalPixel.a; +#else + //Standard alpha + finalPixel.a = texureColor.a; + finalPixel.rgb = texureColor.rgb * finalPixel.a; +#endif + + return finalPixel; +} + +//////////////////////////////////////// +// Alpha Clipping +// + +#if defined(_ALPHA_CLIP) + +uniform fixed _Cutoff; + +#define ALPHA_CLIP(pixel, color) clip((pixel.a * color.a) - _Cutoff); + +#else + +#define ALPHA_CLIP(pixel, color) + +#endif + +//////////////////////////////////////// +// Color functions +// + +uniform fixed4 _Color; + +inline fixed4 calculateVertexColor(fixed4 color) +{ + return color * _Color; +} + +#if defined(_COLOR_ADJUST) + +uniform float _Hue; +uniform float _Saturation; +uniform float _Brightness; +uniform fixed4 _OverlayColor; + +float3 rgb2hsv(float3 c) +{ + float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g)); + float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +float3 hsv2rgb(float3 c) +{ + c = float3(c.x, clamp(c.yz, 0.0, 1.0)); + float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} + +inline fixed4 adjustColor(fixed4 color) +{ + float3 hsv = rgb2hsv(color.rgb); + + hsv.x += _Hue; + hsv.y *= _Saturation; + hsv.z *= _Brightness; + + color.rgb = hsv2rgb(hsv); + + return color; +} + +#define COLORISE(pixel) pixel.rgb = lerp(pixel.rgb, _OverlayColor.rgb, _OverlayColor.a * pixel.a); +#define COLORISE_ADDITIVE(pixel) pixel.rgb = ((1.0-_OverlayColor.a) * pixel.rgb); + +#else // !_COLOR_ADJUST + +#define COLORISE(pixel) +#define COLORISE_ADDITIVE(pixel) + +#endif // !_COLOR_ADJUST + +//////////////////////////////////////// +// Texture functions +// + +uniform sampler2D _MainTex; + +#if _TEXTURE_BLEND +uniform sampler2D _BlendTex; +uniform float _BlendAmount; + +fixed4 calculateBlendedTexturePixel(float2 texcoord) +{ + return (1.0-_BlendAmount) * tex2D(_MainTex, texcoord) + _BlendAmount * tex2D(_BlendTex, texcoord); +} +#endif // _TEXTURE_BLEND + +inline fixed4 calculateTexturePixel(float2 texcoord) +{ + fixed4 pixel; + +#if _TEXTURE_BLEND + pixel = calculateBlendedTexturePixel(texcoord); +#else + pixel = tex2D(_MainTex, texcoord); +#endif // !_TEXTURE_BLEND + +#if defined(_COLOR_ADJUST) + pixel = adjustColor(pixel); +#endif // _COLOR_ADJUST + + return pixel; +} + +uniform fixed4 _MainTex_ST; + +inline float2 calculateTextureCoord(float4 texcoord) +{ + return TRANSFORM_TEX(texcoord, _MainTex); +} + +#endif // SHADER_SHARED_INCLUDED \ No newline at end of file diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/ShaderShared.cginc.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/ShaderShared.cginc.meta new file mode 100644 index 000000000..ad2f18c26 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/ShaderShared.cginc.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: ce9f78731d2f39c49a8688633f53a524 +timeCreated: 1479457856 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteLighting.cginc b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteLighting.cginc new file mode 100644 index 000000000..7e80e75cb --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteLighting.cginc @@ -0,0 +1,136 @@ +// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' + +#ifndef SPRITE_LIGHTING_INCLUDED +#define SPRITE_LIGHTING_INCLUDED + +//Check for using mesh normals +#if !defined(_FIXED_NORMALS) && !defined(_FIXED_NORMALS_BACK_RENDERING) +#define MESH_NORMALS +#endif // _FIXED_NORMALS || _FIXED_NORMALS_BACK_RENDERING + +//////////////////////////////////////// +// Vertex structs +// + +struct VertexInput +{ + float4 vertex : POSITION; + float4 texcoord : TEXCOORD0; + float4 color : COLOR; +#if defined(MESH_NORMALS) + float3 normal : NORMAL; +#endif // MESH_NORMALS +#if defined(_NORMALMAP) + float4 tangent : TANGENT; +#endif // _NORMALMAP + +}; + +//////////////////////////////////////// +// Normal functions +// + +//Fixed Normal defined in view space +uniform float4 _FixedNormal = float4(0, 0, -1, 1); + +inline half3 calculateSpriteWorldNormal(VertexInput vertex) +{ +#if defined(MESH_NORMALS) + return calculateWorldNormal(vertex.normal); +#else //MESH_NORMALS + //Rotate fixed normal by inverse camera matrix to convert the fixed normal into world space + float3x3 invView = transpose((float3x3)UNITY_MATRIX_VP); + float3 normal = _FixedNormal.xyz; + +#if UNITY_REVERSED_Z + normal.z = -normal.z; +#endif + + return normalize(mul(invView, normal)); +#endif // !MESH_NORMALS +} + +inline half3 calculateSpriteViewNormal(VertexInput vertex) +{ +#if defined(MESH_NORMALS) + return normalize(mul((float3x3)UNITY_MATRIX_IT_MV, vertex.normal)); +#else // !MESH_NORMALS + float3 normal = _FixedNormal.xyz; +#if UNITY_REVERSED_Z + normal.z = -normal.z; +#endif + return normal; +#endif // !MESH_NORMALS +} + +//////////////////////////////////////// +// Normal map functions +// + +#if defined(_NORMALMAP) + +inline half3 calculateSpriteWorldBinormal(half3 normalWorld, half3 tangentWorld, float tangentW) +{ +#if defined(_FIXED_NORMALS_BACK_RENDERING) + //If we're using fixed normals and sprite is facing away from camera, flip tangentW + float3 zAxis = float3(0.0, 0.0, 1.0); + float3 modelForward = mul((float3x3)unity_ObjectToWorld, zAxis); + float3 cameraForward = mul((float3x3)UNITY_MATRIX_VP, zAxis); + float directionDot = dot(modelForward, cameraForward); + //Don't worry if directionDot is zero, sprite will be side on to camera so invisible meaning it doesnt matter that tangentW will be zero too + tangentW *= sign(directionDot); +#endif // _FIXED_NORMALS_BACK_RENDERING + + return calculateWorldBinormal(normalWorld, tangentWorld, tangentW); +} + +#endif // _NORMALMAP + +//////////////////////////////////////// +// Rim Lighting functions +// + +#ifdef _RIM_LIGHTING + +uniform float _RimPower; +uniform fixed4 _RimColor; + +inline fixed3 applyRimLighting(fixed3 posWorld, fixed3 normalWorld, fixed4 pixel) : SV_Target +{ + fixed3 viewDir = normalize(_WorldSpaceCameraPos - posWorld); + float invDot = 1.0 - saturate(dot(normalWorld, viewDir)); + float rimPower = pow(invDot, _RimPower); + float rim = saturate(rimPower * _RimColor.a); + +#if defined(_DIFFUSE_RAMP) + rim = calculateDiffuseRamp(rim).r; +#endif + + return lerp(pixel.rgb, _RimColor.xyz * pixel.a, rim); +} + +#endif //_RIM_LIGHTING + +//////////////////////////////////////// +// Emission functions +// + +#ifdef _EMISSION + +uniform sampler2D _EmissionMap; +uniform fixed4 _EmissionColor; +uniform float _EmissionPower; + + +#define APPLY_EMISSION(diffuse, uv) \ + { \ + diffuse += tex2D(_EmissionMap, uv).rgb * _EmissionColor.rgb * _EmissionPower; \ + } + +#else //!_EMISSION + +#define APPLY_EMISSION(diffuse, uv) + +#endif //!_EMISSION + +#endif // SPRITE_LIGHTING_INCLUDED \ No newline at end of file diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteLighting.cginc.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteLighting.cginc.meta new file mode 100644 index 000000000..279520a16 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteLighting.cginc.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 252dba02e84702448a0838ced241467d +timeCreated: 1479457856 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritePixelLighting.cginc b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritePixelLighting.cginc new file mode 100644 index 000000000..aa0331fba --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritePixelLighting.cginc @@ -0,0 +1,201 @@ +#ifndef SPRITE_PIXEL_LIGHTING_INCLUDED +#define SPRITE_PIXEL_LIGHTING_INCLUDED + +#include "ShaderShared.cginc" +#include "SpriteLighting.cginc" +#include "AutoLight.cginc" + +//////////////////////////////////////// +// Defines +// + +//Define to use spherical harmonics for ambient lighting +#define TRI_COLOR_AMBIENT + +//////////////////////////////////////// +// Vertex output struct +// + +#if defined(_NORMALMAP) + #define _VERTEX_LIGHTING_INDEX TEXCOORD5 + #define _LIGHT_COORD_INDEX_0 6 + #define _LIGHT_COORD_INDEX_1 7 + #define _FOG_COORD_INDEX 8 +#else + #define _VERTEX_LIGHTING_INDEX TEXCOORD3 + #define _LIGHT_COORD_INDEX_0 4 + #define _LIGHT_COORD_INDEX_1 5 + #define _FOG_COORD_INDEX 6 +#endif // _NORMALMAP + +struct VertexOutput +{ + float4 pos : SV_POSITION; + fixed4 color : COLOR; + float2 texcoord : TEXCOORD0; + float4 posWorld : TEXCOORD1; + half3 normalWorld : TEXCOORD2; +#if defined(_NORMALMAP) + half3 tangentWorld : TEXCOORD3; + half3 binormalWorld : TEXCOORD4; +#endif // _NORMALMAP + fixed3 vertexLighting : _VERTEX_LIGHTING_INDEX; + LIGHTING_COORDS(_LIGHT_COORD_INDEX_0, _LIGHT_COORD_INDEX_1) +#if defined(_FOG) + UNITY_FOG_COORDS(_FOG_COORD_INDEX) +#endif // _FOG +}; + +//////////////////////////////////////// +// Light calculations +// + +uniform fixed4 _LightColor0; + +inline fixed3 calculateLightDiffuse(VertexOutput input, float3 normalWorld) +{ + //For directional lights _WorldSpaceLightPos0.w is set to zero + float3 lightWorldDirection = normalize(_WorldSpaceLightPos0.xyz - input.posWorld.xyz * _WorldSpaceLightPos0.w); + + float attenuation = LIGHT_ATTENUATION(input); + float angleDot = dotClamped(normalWorld, lightWorldDirection); + +#if defined(_DIFFUSE_RAMP) + fixed3 lightDiffuse = calculateRampedDiffuse(_LightColor0.rgb, sqrt(attenuation), angleDot); +#else + fixed3 lightDiffuse = _LightColor0.rgb * (attenuation * angleDot); +#endif // _DIFFUSE_RAMP + + return lightDiffuse; +} + +inline float3 calculateNormalWorld(VertexOutput input) +{ +#if defined(_NORMALMAP) + return calculateNormalFromBumpMap(input.texcoord, input.tangentWorld, input.binormalWorld, input.normalWorld); +#else + return input.normalWorld; +#endif +} + +fixed3 calculateVertexLighting(float3 posWorld, float3 normalWorld) +{ + fixed3 vertexLighting = fixed3(0,0,0); + +#ifdef VERTEXLIGHT_ON + //Get approximated illumination from non-important point lights + vertexLighting = Shade4PointLights ( unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, + unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb, + unity_4LightAtten0, posWorld, normalWorld) * 0.5; +#endif + + return vertexLighting; +} + +fixed3 calculateAmbientLight(half3 normalWorld) +{ +#if defined(TRI_COLOR_AMBIENT) + fixed3 ambient = ShadeSH9(half4(normalWorld, 1.0)) * 0.75f; +#else + fixed3 ambient = unity_AmbientSky.rgb * 0.75; +#endif + return ambient; +} + +//////////////////////////////////////// +// Vertex program +// + +VertexOutput vert(VertexInput v) +{ + VertexOutput output; + + output.pos = calculateLocalPos(v.vertex); + output.color = calculateVertexColor(v.color); + output.texcoord = calculateTextureCoord(v.texcoord); + output.posWorld = calculateWorldPos(v.vertex); + output.normalWorld = calculateSpriteWorldNormal(v); + output.vertexLighting = calculateVertexLighting(output.posWorld, output.normalWorld); + +#if defined(_NORMALMAP) + output.tangentWorld = calculateWorldTangent(v.tangent); + output.binormalWorld = calculateSpriteWorldBinormal(output.normalWorld, output.tangentWorld, v.tangent.w); +#endif + + TRANSFER_VERTEX_TO_FRAGMENT(output) + +#if defined(_FOG) + UNITY_TRANSFER_FOG(output,output.pos); +#endif // _FOG + + return output; +} + +//////////////////////////////////////// +// Fragment programs +// + +fixed4 fragBase(VertexOutput input) : SV_Target +{ + fixed4 texureColor = calculateTexturePixel(input.texcoord); + ALPHA_CLIP(texureColor, input.color) + + //Get normal direction + fixed3 normalWorld = calculateNormalWorld(input); + + //Get Ambient diffuse + fixed3 ambient = calculateAmbientLight(normalWorld); + + //Get primary pixel light diffuse + fixed3 diffuse = calculateLightDiffuse(input, normalWorld); + + //Combine along with vertex lighting for the base lighting pass + fixed3 lighting = saturate(ambient + diffuse + input.vertexLighting); + + APPLY_EMISSION(lighting, input.texcoord) + + fixed4 pixel = calculateLitPixel(texureColor, input.color, lighting); + +#if defined(_RIM_LIGHTING) + pixel.rgb = applyRimLighting(input.posWorld, normalWorld, pixel); +#endif + + COLORISE(pixel) + +#if defined(_FOG) + fixed4 fogColor = lerp(fixed4(0,0,0,0), unity_FogColor, pixel.a); + UNITY_APPLY_FOG_COLOR(input.fogCoord, pixel, fogColor); +#endif // _FOG + + return pixel; +} + +fixed4 fragAdd(VertexOutput input) : SV_Target +{ + fixed4 texureColor = calculateTexturePixel(input.texcoord); + +#if defined(_COLOR_ADJUST) + texureColor = adjustColor(texureColor); +#endif // _COLOR_ADJUST + + ALPHA_CLIP(texureColor, input.color) + + //Get normal direction + fixed3 normalWorld = calculateNormalWorld(input); + + //Get light diffuse + fixed3 lighting = calculateLightDiffuse(input, normalWorld); + + fixed4 pixel = calculateAdditiveLitPixel(texureColor, input.color, lighting); + + COLORISE_ADDITIVE(pixel) + +#if defined(_FOG) + UNITY_APPLY_FOG_COLOR(input.fogCoord, pixel.rgb, fixed4(0,0,0,0)); // fog towards black in additive pass +#endif // _FOG + + return pixel; +} + + +#endif // SPRITE_PIXEL_LIGHTING_INCLUDED \ No newline at end of file diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritePixelLighting.cginc.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritePixelLighting.cginc.meta new file mode 100644 index 000000000..2759558df --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritePixelLighting.cginc.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 38bcbfc10385d424c9cbac5b5b9ec1af +timeCreated: 1479457856 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteShadows.cginc b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteShadows.cginc new file mode 100644 index 000000000..6b88c198f --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteShadows.cginc @@ -0,0 +1,52 @@ +#ifndef SPRITE_SHADOWS_INCLUDED +#define SPRITE_SHADOWS_INCLUDED + +#include "UnityCG.cginc" + +//////////////////////////////////////// +// Vertex structs +// + +struct vertexInput +{ + float4 vertex : POSITION; + float4 texcoord : TEXCOORD0; +}; + +struct vertexOutput +{ + V2F_SHADOW_CASTER; + float4 texcoord : TEXCOORD1; +}; + +//////////////////////////////////////// +// Vertex program +// + +uniform sampler2D _MainTex; +uniform fixed4 _MainTex_ST; + +vertexOutput vert(vertexInput v) +{ + vertexOutput o; + TRANSFER_SHADOW_CASTER(o) + o.texcoord = float4(TRANSFORM_TEX(v.texcoord, _MainTex), 0, 0); + return o; +} + +//////////////////////////////////////// +// Fragment program +// + + +uniform fixed _ShadowAlphaCutoff; + +fixed4 frag(vertexOutput IN) : COLOR +{ + fixed4 texureColor = tex2D(_MainTex, IN.texcoord.xy); + clip(texureColor.a - _ShadowAlphaCutoff); + + SHADOW_CASTER_FRAGMENT(IN) +} + +#endif // SPRITE_SHADOWS_INCLUDED \ No newline at end of file diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteShadows.cginc.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteShadows.cginc.meta new file mode 100644 index 000000000..610e02e1b --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteShadows.cginc.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 0f58b811a44f1b44cb9aac3ddfe24f37 +timeCreated: 1479457856 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteUnlit.cginc b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteUnlit.cginc new file mode 100644 index 000000000..b58a50942 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteUnlit.cginc @@ -0,0 +1,67 @@ +#ifndef SPRITE_UNLIT_INCLUDED +#define SPRITE_UNLIT_INCLUDED + +#include "ShaderShared.cginc" + +//////////////////////////////////////// +// Vertex structs +// + +struct VertexInput +{ + float4 vertex : POSITION; + float4 texcoord : TEXCOORD0; + fixed4 color : COLOR; +}; + +struct VertexOutput +{ + float4 pos : SV_POSITION; + float2 texcoord : TEXCOORD0; + fixed4 color : COLOR; +#if defined(_FOG) + UNITY_FOG_COORDS(1) +#endif // _FOG +}; + +//////////////////////////////////////// +// Vertex program +// + +VertexOutput vert(VertexInput input) +{ + VertexOutput output; + + output.pos = calculateLocalPos(input.vertex); + output.texcoord = calculateTextureCoord(input.texcoord); + output.color = calculateVertexColor(input.color); + +#if defined(_FOG) + UNITY_TRANSFER_FOG(output,output.pos); +#endif // _FOG + + return output; +} + +//////////////////////////////////////// +// Fragment program +// + +fixed4 frag(VertexOutput input) : SV_Target +{ + fixed4 texureColor = calculateTexturePixel(input.texcoord.xy); + ALPHA_CLIP(texureColor, input.color) + + fixed4 pixel = calculatePixel(texureColor, input.color); + + COLORISE(pixel) + +#if defined(_FOG) + fixed4 fogColor = lerp(fixed4(0,0,0,0), unity_FogColor, pixel.a); + UNITY_APPLY_FOG_COLOR(input.fogCoord, pixel, fogColor); +#endif // _FOG + + return pixel; +} + +#endif // SPRITE_UNLIT_INCLUDED \ No newline at end of file diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteUnlit.cginc.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteUnlit.cginc.meta new file mode 100644 index 000000000..684bd2457 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteUnlit.cginc.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: e67832d6dd9dd914ca946fa7fcaa4a9e +timeCreated: 1479457856 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteVertexLighting.cginc b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteVertexLighting.cginc new file mode 100644 index 000000000..cfb0ba75b --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteVertexLighting.cginc @@ -0,0 +1,439 @@ +#ifndef SPRITE_VERTEX_LIGHTING_INCLUDED +#define SPRITE_VERTEX_LIGHTING_INCLUDED + +#include "ShaderShared.cginc" +#include "SpriteLighting.cginc" +#include "UnityStandardUtils.cginc" + +//////////////////////////////////////// +// Defines +// + +//Define to use fake spherical harmonics for ambient lighting +#define TRI_COLOR_AMBIENT + +//Have to process lighting per pixel if using normal maps or a diffuse ramp or rim lighting +#if defined(_NORMALMAP) || defined(_DIFFUSE_RAMP) || defined(_RIM_LIGHTING) +#define PER_PIXEL_LIGHTING +#endif + +//Turn off bump mapping and diffuse ramping on older shader models as they dont support needed number of outputs +#if defined(PER_PIXEL_LIGHTING) && (SHADER_TARGET < 30) + #undef PER_PIXEL_LIGHTING + #undef _NORMALMAP + #undef _DIFFUSE_RAMP + #undef _RIM_LIGHTING +#endif + +//In D3D9 only have a max of 9 TEXCOORD so can't have diffuse ramping or fog or rim lighting if processing lights per pixel +#if defined(SHADER_API_D3D9) && defined(PER_PIXEL_LIGHTING) + #if defined(_NORMALMAP) + #undef _DIFFUSE_RAMP + #undef _FOG + #undef _RIM_LIGHTING + #elif defined(_DIFFUSE_RAMP) + #undef _FOG + #undef _RIM_LIGHTING + #elif defined(_RIM_LIGHTING) + #undef _FOG + #undef _DIFFUSE_RAMP + #else + #undef _DIFFUSE_RAMP + #undef _RIM_LIGHTING + #endif +#endif + +#if defined(PER_PIXEL_LIGHTING) + #if defined(_NORMALMAP) && defined(_DIFFUSE_RAMP) + #define ATTENUATIONS TEXCOORD9 + #if defined(_RIM_LIGHTING) + #define _POS_WORLD_INDEX TEXCOORD10 + #define _FOG_COORD_INDEX 11 + #else + #define _FOG_COORD_INDEX 10 + #endif + #elif defined(_NORMALMAP) != defined(_DIFFUSE_RAMP) + #define ATTENUATIONS TEXCOORD8 + #if defined(_RIM_LIGHTING) + #define _POS_WORLD_INDEX TEXCOORD9 + #define _FOG_COORD_INDEX 10 + #else + #define _FOG_COORD_INDEX 9 + #endif + #else //!_DIFFUSE_RAMP && !_NORMALMAP + #if defined(_RIM_LIGHTING) + #define _POS_WORLD_INDEX TEXCOORD8 + #define _FOG_COORD_INDEX 9 + #else + #define _FOG_COORD_INDEX 8 + #endif + #endif +#else //!PER_PIXEL_LIGHTING + #define _FOG_COORD_INDEX 2 +#endif + +//////////////////////////////////////// +// Vertex output struct +// + +struct VertexOutput +{ + float4 pos : SV_POSITION; + fixed4 color : COLOR; + float3 texcoord : TEXCOORD0; + +#if defined(PER_PIXEL_LIGHTING) + + half4 VertexLightInfo0 : TEXCOORD1; + half4 VertexLightInfo1 : TEXCOORD2; + half4 VertexLightInfo2 : TEXCOORD3; + half4 VertexLightInfo3 : TEXCOORD4; + half4 VertexLightInfo4 : TEXCOORD5; + + #if defined(_NORMALMAP) + half4 normalWorld : TEXCOORD6; + half4 tangentWorld : TEXCOORD7; + half4 binormalWorld : TEXCOORD8; + #else + half3 normalWorld : TEXCOORD6; + half3 VertexLightInfo5 : TEXCOORD7; + #endif + #if defined(_DIFFUSE_RAMP) + half4 LightAttenuations : ATTENUATIONS; + #endif + #if defined(_RIM_LIGHTING) + float4 posWorld : _POS_WORLD_INDEX; + #endif + +#else //!PER_PIXEL_LIGHTING + + half3 FullLighting : TEXCOORD1; + +#endif // !PER_PIXEL_LIGHTING + +#if defined(_FOG) + UNITY_FOG_COORDS(_FOG_COORD_INDEX) +#endif // _FOG +}; + +//////////////////////////////////////// +// Light calculations +// + +struct VertexLightInfo +{ + half3 lightDirection; + fixed3 lightColor; + +#if defined(_DIFFUSE_RAMP) + float attenuation; +#endif // _DIFFUSE_RAMP +}; + +inline VertexLightInfo getVertexLightAttenuatedInfo(int index, float3 viewPos) +{ + VertexLightInfo lightInfo; + + //For directional lights _WorldSpaceLightPos0.w is set to zero + lightInfo.lightDirection = unity_LightPosition[index].xyz - (viewPos.xyz * unity_LightPosition[index].w); + float lengthSq = dot(lightInfo.lightDirection, lightInfo.lightDirection); + lightInfo.lightDirection *= rsqrt(lengthSq); + + float attenuation = 1.0 / (1.0 + lengthSq * unity_LightAtten[index].z); + + //Spot light attenuation - for non-spot lights unity_LightAtten.x is set to -1 and y is set to 1 + if (-1 != unity_LightAtten[index].x || 1 != unity_LightAtten[index].y) + { + float rho = dotClamped(lightInfo.lightDirection, unity_SpotDirection[index].xyz); + float spotAtt = (rho - unity_LightAtten[index].x) * unity_LightAtten[index].y; + attenuation *= saturate(spotAtt); + } + + //If using a diffuse ramp texture then need to pass through the lights attenuation, otherwise premultiply the light color with it +#if defined(_DIFFUSE_RAMP) + lightInfo.lightColor = unity_LightColor[index].rgb; + lightInfo.attenuation = sqrt(attenuation); +#else + lightInfo.lightColor = unity_LightColor[index].rgb * attenuation; +#endif // _DIFFUSE_RAMP + + return lightInfo; +} + +fixed3 calculateAmbientLight(half3 normalWorld) +{ +#if defined(TRI_COLOR_AMBIENT) + + //Magic constants used to tweak ambient to approximate pixel shader spherical harmonics + fixed3 worldUp = fixed3(0,1,0); + float skyGroundDotMul = 2.5; + float minEquatorMix = 0.5; + float equatorColorBlur = 0.33; + + float upDot = dot(normalWorld, worldUp); + + //Fade between a flat lerp from sky to ground and a 3 way lerp based on how bright the equator light is. + //This simulates how directional lights get blurred using spherical harmonics + + //Work out color from ground and sky, ignoring equator + float adjustedDot = upDot * skyGroundDotMul; + fixed3 skyGroundColor = lerp(unity_AmbientGround, unity_AmbientSky, saturate((adjustedDot + 1.0) * 0.5)); + + //Work out equator lights brightness + float equatorBright = saturate(dot(unity_AmbientEquator.rgb, unity_AmbientEquator.rgb)); + + //Blur equator color with sky and ground colors based on how bright it is. + fixed3 equatorBlurredColor = lerp(unity_AmbientEquator, saturate(unity_AmbientEquator + unity_AmbientGround + unity_AmbientSky), equatorBright * equatorColorBlur); + + //Work out 3 way lerp inc equator light + fixed3 equatorColor = lerp(equatorBlurredColor, unity_AmbientGround, -upDot) * step(upDot, 0) + lerp(equatorBlurredColor, unity_AmbientSky, upDot) * step(0, upDot); + + //Mix the two colors together based on how bright the equator light is + return lerp(skyGroundColor, equatorColor, saturate(equatorBright + minEquatorMix)) * 0.75; + +#else + + //Flat ambient is just the sky color + return unity_AmbientSky.rgb * 0.75; + +#endif // TRI_COLOR_AMBIENT + +} + +//////////////////////////////////////// +// Light Packing Functions (this stuff gets messy!) +// + +#if defined(_DIFFUSE_RAMP) + +inline fixed3 calculateLightDiffuse(fixed3 lightColor, half3 normal, half3 lightDirection, float attenuation) +{ + float angleDot = dotClamped(normal, lightDirection); + fixed3 diffuse = calculateRampedDiffuse(lightColor, attenuation, angleDot); + return diffuse * 0.75; +} + +#else + +inline fixed3 calculateLightDiffuse(fixed3 attenuatedLightColor, half3 normal, half3 lightDirection) +{ + float angleDot = dotClamped(normal, lightDirection); + fixed3 diffuse = attenuatedLightColor * angleDot; + return diffuse * 0.75; +} + +#endif // _NORMALMAP + + +#if defined(PER_PIXEL_LIGHTING) + +inline VertexLightInfo getVertexLightAttenuatedInfoWorldSpace(int index, float3 viewPos) +{ + VertexLightInfo lightInfo = getVertexLightAttenuatedInfo(index, viewPos); + + //Convert light direction from view space to world space + lightInfo.lightDirection = normalize(mul((float3x3)UNITY_MATRIX_V, lightInfo.lightDirection)); + + return lightInfo; +} + +#define VERTEX_LIGHT_0_DIR VertexLightInfo0.xyz +#define VERTEX_LIGHT_0_R VertexLightInfo4.x +#define VERTEX_LIGHT_0_G VertexLightInfo4.y +#define VERTEX_LIGHT_0_B VertexLightInfo4.z + +#define VERTEX_LIGHT_1_DIR VertexLightInfo1.xyz +#define VERTEX_LIGHT_1_R VertexLightInfo0.w +#define VERTEX_LIGHT_1_G VertexLightInfo1.w +#define VERTEX_LIGHT_1_B VertexLightInfo2.w + +#define VERTEX_LIGHT_2_DIR VertexLightInfo2.xyz +#define VERTEX_LIGHT_2_R VertexLightInfo3.w +#define VERTEX_LIGHT_2_G VertexLightInfo4.w +#define VERTEX_LIGHT_2_B texcoord.z + +#define VERTEX_LIGHT_3_DIR VertexLightInfo3.xyz + +#if defined(_NORMALMAP) + #define VERTEX_LIGHT_3_R normalWorld.w + #define VERTEX_LIGHT_3_G tangentWorld.w + #define VERTEX_LIGHT_3_B binormalWorld.w +#else + #define VERTEX_LIGHT_3_R VertexLightInfo5.x + #define VERTEX_LIGHT_3_G VertexLightInfo5.y + #define VERTEX_LIGHT_3_B VertexLightInfo5.z +#endif + +#if defined(_DIFFUSE_RAMP) + + #define LIGHT_DIFFUSE_ATTEN_0 LightAttenuations.x + #define LIGHT_DIFFUSE_ATTEN_1 LightAttenuations.y + #define LIGHT_DIFFUSE_ATTEN_2 LightAttenuations.z + #define LIGHT_DIFFUSE_ATTEN_3 LightAttenuations.w + + #define PACK_VERTEX_LIGHT_DIFFUSE(index, output, lightInfo) \ + { \ + output.LIGHT_DIFFUSE_ATTEN_##index = lightInfo.attenuation; \ + } + + #define ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, vertexLightColor, normalDirection, vertexLightDir) \ + { \ + diffuse += calculateLightDiffuse(vertexLightColor, normalDirection, vertexLightDir, input.LIGHT_DIFFUSE_ATTEN_##index); \ + } +#else + #define PACK_VERTEX_LIGHT_DIFFUSE(index, output, lightInfo) + #define ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, vertexLightColor, normalDirection, vertexLightDir) \ + { \ + diffuse += calculateLightDiffuse(vertexLightColor, normalDirection, vertexLightDir); \ + } +#endif + +#define PACK_VERTEX_LIGHT(index, output, viewPos) \ + { \ + VertexLightInfo lightInfo = getVertexLightAttenuatedInfoWorldSpace(index, viewPos); \ + output.VERTEX_LIGHT_##index##_DIR = lightInfo.lightDirection; \ + output.VERTEX_LIGHT_##index##_R = lightInfo.lightColor.r; \ + output.VERTEX_LIGHT_##index##_G = lightInfo.lightColor.g; \ + output.VERTEX_LIGHT_##index##_B = lightInfo.lightColor.b; \ + PACK_VERTEX_LIGHT_DIFFUSE(index, output, lightInfo); \ + } + +#define ADD_VERTEX_LIGHT(index, input, normalDirection, diffuse) \ + { \ + half3 vertexLightDir = input.VERTEX_LIGHT_##index##_DIR; \ + fixed3 vertexLightColor = fixed3(input.VERTEX_LIGHT_##index##_R, input.VERTEX_LIGHT_##index##_G, input.VERTEX_LIGHT_##index##_B); \ + ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, vertexLightColor, normalDirection, vertexLightDir) \ + } + +#else //!PER_PIXEL_LIGHTING + +//////////////////////////////////////// +// Vertex Only Functions +// + +inline fixed3 calculateLightDiffuse(int index, float3 viewPos, half3 viewNormal) +{ + VertexLightInfo lightInfo = getVertexLightAttenuatedInfo(index, viewPos); + float angleDot = dotClamped(viewNormal, lightInfo.lightDirection); + + fixed3 diffuse = lightInfo.lightColor * angleDot; + return diffuse; +} + +#endif // !PER_PIXEL_LIGHTING + +//////////////////////////////////////// +// Vertex program +// + +VertexOutput vert(VertexInput input) +{ + VertexOutput output; + + output.pos = calculateLocalPos(input.vertex); + output.color = calculateVertexColor(input.color); + output.texcoord = float3(calculateTextureCoord(input.texcoord), 0); + + float3 viewPos = mul(UNITY_MATRIX_MV, input.vertex); + +#if defined(PER_PIXEL_LIGHTING) + + #if defined(_RIM_LIGHTING) + output.posWorld = calculateWorldPos(input.vertex); + #endif + + PACK_VERTEX_LIGHT(0, output, viewPos) + PACK_VERTEX_LIGHT(1, output, viewPos) + PACK_VERTEX_LIGHT(2, output, viewPos) + PACK_VERTEX_LIGHT(3, output, viewPos) + + output.normalWorld.xyz = calculateSpriteWorldNormal(input); + + #if defined(_NORMALMAP) + output.tangentWorld.xyz = calculateWorldTangent(input.tangent); + output.binormalWorld.xyz = calculateSpriteWorldBinormal(output.normalWorld, output.tangentWorld, input.tangent.w); + #endif + +#else // !PER_PIXEL_LIGHTING + + //Just pack full lighting + float3 viewNormal = calculateSpriteViewNormal(input); + + //Get Ambient diffuse + float3 normalWorld = calculateSpriteWorldNormal(input); + fixed3 ambient = calculateAmbientLight(normalWorld); + + fixed3 diffuse = calculateLightDiffuse(0, viewPos, viewNormal); + diffuse += calculateLightDiffuse(1, viewPos, viewNormal); + diffuse += calculateLightDiffuse(2, viewPos, viewNormal); + diffuse += calculateLightDiffuse(3, viewPos, viewNormal); + + output.FullLighting = saturate(ambient + diffuse); + +#endif // !PER_PIXEL_LIGHTING + +#if defined(_FOG) + UNITY_TRANSFER_FOG(output, output.pos); +#endif // _FOG + + return output; +} + +//////////////////////////////////////// +// Fragment program +// + +fixed4 frag(VertexOutput input) : SV_Target +{ + fixed4 texureColor = calculateTexturePixel(input.texcoord.xy); + ALPHA_CLIP(texureColor, input.color) + +#if defined(PER_PIXEL_LIGHTING) + + #if defined(_NORMALMAP) + half3 normalWorld = calculateNormalFromBumpMap(input.texcoord.xy, input.tangentWorld.xyz, input.binormalWorld.xyz, input.normalWorld.xyz); + #else + half3 normalWorld = input.normalWorld.xyz; + #endif + + //Get Ambient diffuse + fixed3 ambient = calculateAmbientLight(normalWorld); + + //Find vertex light diffuse + fixed3 diffuse = fixed3(0,0,0); + + //Add each vertex light to diffuse + ADD_VERTEX_LIGHT(0, input, normalWorld, diffuse) + ADD_VERTEX_LIGHT(1, input, normalWorld, diffuse) + ADD_VERTEX_LIGHT(2, input, normalWorld, diffuse) + ADD_VERTEX_LIGHT(3, input, normalWorld, diffuse) + + fixed3 lighting = saturate(ambient + diffuse); + + APPLY_EMISSION(lighting, input.texcoord.xy) + + fixed4 pixel = calculateLitPixel(texureColor, input.color, lighting); + +#if defined(_RIM_LIGHTING) + pixel.rgb = applyRimLighting(input.posWorld, normalWorld, pixel); +#endif + +#else // !PER_PIXEL_LIGHTING + + APPLY_EMISSION(input.FullLighting, input.texcoord.xy) + + fixed4 pixel = calculateLitPixel(texureColor, input.color, input.FullLighting); + +#endif // !PER_PIXEL_LIGHTING + + COLORISE(pixel) + +#if defined(_FOG) + fixed4 fogColor = lerp(fixed4(0,0,0,0), unity_FogColor, pixel.a); + UNITY_APPLY_FOG_COLOR(input.fogCoord, pixel, fogColor); +#endif // _FOG + + return pixel; +} + +#endif // SPRITE_VERTEX_LIGHTING_INCLUDED \ No newline at end of file diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteVertexLighting.cginc.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteVertexLighting.cginc.meta new file mode 100644 index 000000000..f206ccaf1 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpriteVertexLighting.cginc.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: b256f9f092407414392d98f339173a2d +timeCreated: 1479457856 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesPixelLit.shader b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesPixelLit.shader new file mode 100644 index 000000000..4fbbbb40b --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesPixelLit.shader @@ -0,0 +1,133 @@ +Shader "Spine/Sprite/Pixel Lit" +{ + Properties + { + _MainTex ("Main Texture", 2D) = "white" {} + _Color ("Color", Color) = (1,1,1,1) + _BumpMap ("Normal Map", 2D) = "bump" {} + + _EmissionColor("Color", Color) = (0,0,0,0) + _EmissionMap("Emission", 2D) = "white" {} + _EmissionPower("Emission Power", Float) = 2.0 + + _DiffuseRamp ("Diffuse Ramp Texture", 2D) = "gray" {} + + _FixedNormal ("Fixed Normal", Vector) = (0,0,-1,1) + _ZWrite ("Depth Write", Float) = 1.0 + _Cutoff ("Depth alpha cutoff", Range(0,1)) = 0.5 + _ShadowAlphaCutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1 + + _OverlayColor ("Overlay Color", Color) = (0,0,0,0) + _Hue("Hue", Range(-0.5,0.5)) = 0.0 + _Saturation("Saturation", Range(0,2)) = 1.0 + _Brightness("Brightness", Range(0,2)) = 1.0 + + _RimPower("Rim Power", Float) = 2.0 + _RimColor ("Rim Color", Color) = (1,1,1,1) + + _BlendTex ("Blend Texture", 2D) = "white" {} + _BlendAmount ("Blend", Range(0,1)) = 0.0 + + [HideInInspector] _BlendMode ("__mode", Float) = 0.0 + [HideInInspector] _SrcBlend ("__src", Float) = 1.0 + [HideInInspector] _DstBlend ("__dst", Float) = 0.0 + [HideInInspector] _RenderQueue ("__queue", Float) = 0.0 + [HideInInspector] _Cull ("__cull", Float) = 0.0 + } + + SubShader + { + Tags { "Queue"="Transparent" "RenderType"="Transparent" } + LOD 200 + + Pass + { + Name "FORWARD" + Tags { "LightMode" = "ForwardBase" } + Blend [_SrcBlend] [_DstBlend] + ZWrite [_ZWrite] + ZTest LEqual + Cull [_Cull] + + CGPROGRAM + #pragma target 3.0 + + #pragma shader_feature _ _ALPHAPREMULTIPLY_ON _ADDITIVEBLEND _ADDITIVEBLEND_SOFT _MULTIPLYBLEND _MULTIPLYBLEND_X2 + #pragma shader_feature _NORMALMAP + #pragma shader_feature _ _FIXED_NORMALS _FIXED_NORMALS_BACK_RENDERING + #pragma shader_feature _ALPHA_CLIP + #pragma shader_feature _EMISSION + #pragma shader_feature _RIM_LIGHTING + #pragma shader_feature _DIFFUSE_RAMP + #pragma shader_feature _COLOR_ADJUST + #pragma shader_feature _TEXTURE_BLEND + #pragma shader_feature _FOG + + #pragma multi_compile_fwdbase + #pragma fragmentoption ARB_precision_hint_fastest + #pragma multi_compile_fog + + #pragma vertex vert + #pragma fragment fragBase + + #include "SpritePixelLighting.cginc" + ENDCG + } + Pass + { + Name "FORWARD_DELTA" + Tags { "LightMode" = "ForwardAdd" } + Blend [_SrcBlend] One + ZWrite Off + ZTest LEqual + Cull [_Cull] + + CGPROGRAM + #pragma target 3.0 + + #pragma shader_feature _ _ALPHAPREMULTIPLY_ON _ADDITIVEBLEND _ADDITIVEBLEND_SOFT _MULTIPLYBLEND _MULTIPLYBLEND_X2 + #pragma shader_feature _NORMALMAP + #pragma shader_feature _ _FIXED_NORMALS _FIXED_NORMALS_BACK_RENDERING + #pragma shader_feature _ALPHA_CLIP + #pragma shader_feature _DIFFUSE_RAMP + #pragma shader_feature _COLOR_ADJUST + #pragma shader_feature _TEXTURE_BLEND + #pragma shader_feature _FOG + + #pragma multi_compile_fwdadd_fullshadows + #pragma fragmentoption ARB_precision_hint_fastest + #pragma multi_compile_fog + + #pragma vertex vert + #pragma fragment fragAdd + + #include "SpritePixelLighting.cginc" + ENDCG + } + Pass + { + Name "ShadowCaster" + Tags { "LightMode"="ShadowCaster" } + Offset 1, 1 + + Fog { Mode Off } + ZWrite On + ZTest LEqual + Cull Off + Lighting Off + + CGPROGRAM + #pragma multi_compile_shadowcaster + #pragma fragmentoption ARB_precision_hint_fastest + + #pragma vertex vert + #pragma fragment frag + + #include "SpriteShadows.cginc" + ENDCG + } + } + + FallBack "Spine/Sprite/Unlit" + CustomEditor "SpineSpriteShaderGUI" +} \ No newline at end of file diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesPixelLit.shader.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesPixelLit.shader.meta new file mode 100644 index 000000000..c5a0947d1 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesPixelLit.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 6f7a5a97a82637f478494bc40ea8c8a2 +timeCreated: 1479457857 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesUnlit.shader b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesUnlit.shader new file mode 100644 index 000000000..83691151e --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesUnlit.shader @@ -0,0 +1,82 @@ +Shader "Spine/Sprite/Unlit" +{ + Properties + { + _MainTex ("Main Texture", 2D) = "white" {} + _Color ("Color", Color) = (1,1,1,1) + + _ZWrite ("Depth Write", Float) = 0.0 + _Cutoff ("Depth alpha cutoff", Range(0,1)) = 0.0 + _ShadowAlphaCutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1 + + _OverlayColor ("Overlay Color", Color) = (0,0,0,0) + _Hue("Hue", Range(-0.5,0.5)) = 0.0 + _Saturation("Saturation", Range(0,2)) = 1.0 + _Brightness("Brightness", Range(0,2)) = 1.0 + + _BlendTex ("Blend Texture", 2D) = "white" {} + _BlendAmount ("Blend", Range(0,1)) = 0.0 + + [HideInInspector] _BlendMode ("__mode", Float) = 0.0 + [HideInInspector] _SrcBlend ("__src", Float) = 1.0 + [HideInInspector] _DstBlend ("__dst", Float) = 0.0 + [HideInInspector] _RenderQueue ("__queue", Float) = 0.0 + [HideInInspector] _Cull ("__cull", Float) = 0.0 + } + + SubShader + { + Tags { "Queue"="Transparent" "RenderType"="Transparent" } + LOD 100 + + Pass + { + Blend [_SrcBlend] [_DstBlend] + Lighting Off + ZWrite [_ZWrite] + ZTest LEqual + Cull [_Cull] + Lighting Off + + CGPROGRAM + #pragma shader_feature _ _ALPHAPREMULTIPLY_ON _ADDITIVEBLEND _ADDITIVEBLEND_SOFT _MULTIPLYBLEND _MULTIPLYBLEND_X2 + #pragma shader_feature _ALPHA_CLIP + #pragma shader_feature _TEXTURE_BLEND + #pragma shader_feature _COLOR_ADJUST + #pragma shader_feature _FOG + + #pragma fragmentoption ARB_precision_hint_fastest + #pragma multi_compile_fog + + #pragma vertex vert + #pragma fragment frag + + #include "SpriteUnlit.cginc" + ENDCG + } + Pass + { + Name "ShadowCaster" + Tags { "LightMode"="ShadowCaster" } + Offset 1, 1 + + Fog { Mode Off } + ZWrite On + ZTest LEqual + Cull Off + Lighting Off + + CGPROGRAM + #pragma multi_compile_shadowcaster + #pragma fragmentoption ARB_precision_hint_fastest + + #pragma vertex vert + #pragma fragment frag + + #include "SpriteShadows.cginc" + ENDCG + } + } + + CustomEditor "SpineSpriteShaderGUI" +} diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesUnlit.shader.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesUnlit.shader.meta new file mode 100644 index 000000000..0304ebb8c --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesUnlit.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 64005298b9a80bb4899eabd5140dc4a8 +timeCreated: 1479457857 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesVertexLit.shader b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesVertexLit.shader new file mode 100644 index 000000000..f34cc5d05 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesVertexLit.shader @@ -0,0 +1,102 @@ +Shader "Spine/Sprite/Vertex Lit" +{ + Properties + { + _MainTex ("Main Texture", 2D) = "white" {} + _Color ("Color", Color) = (1,1,1,1) + _BumpMap ("Normal Map", 2D) = "bump" {} + + _EmissionColor("Color", Color) = (0,0,0,0) + _EmissionMap("Emission", 2D) = "white" {} + _EmissionPower("Emission Power", Float) = 2.0 + + _DiffuseRamp ("Diffuse Ramp Texture", 2D) = "gray" {} + + _FixedNormal ("Fixed Normal", Vector) = (0,0,-1,1) + _ZWrite ("Depth Write", Float) = 0.0 + _Cutoff ("Depth alpha cutoff", Range(0,1)) = 0.0 + _ShadowAlphaCutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1 + + _OverlayColor ("Overlay Color", Color) = (0,0,0,0) + _Hue("Hue", Range(-0.5,0.5)) = 0.0 + _Saturation("Saturation", Range(0,2)) = 1.0 + _Brightness("Brightness", Range(0,2)) = 1.0 + + _RimPower("Rim Power", Float) = 2.0 + _RimColor ("Rim Color", Color) = (1,1,1,1) + + _BlendTex ("Blend Texture", 2D) = "white" {} + _BlendAmount ("Blend", Range(0,1)) = 0.0 + + [HideInInspector] _BlendMode ("__mode", Float) = 0.0 + [HideInInspector] _SrcBlend ("__src", Float) = 1.0 + [HideInInspector] _DstBlend ("__dst", Float) = 0.0 + [HideInInspector] _RenderQueue ("__queue", Float) = 0.0 + [HideInInspector] _Cull ("__cull", Float) = 0.0 + } + + SubShader + { + Tags { "Queue"="Transparent" "RenderType"="Transparent" } + LOD 150 + + Pass + { + Name "Vertex" + Tags { "LightMode" = "Vertex" } + Blend [_SrcBlend] [_DstBlend] + ZWrite [_ZWrite] + ZTest LEqual + Cull [_Cull] + Lighting On + + CGPROGRAM + #pragma target 3.0 + + #pragma shader_feature _ _ALPHAPREMULTIPLY_ON _ADDITIVEBLEND _ADDITIVEBLEND_SOFT _MULTIPLYBLEND _MULTIPLYBLEND_X2 + #pragma shader_feature _NORMALMAP + #pragma shader_feature _ _FIXED_NORMALS _FIXED_NORMALS_BACK_RENDERING + #pragma shader_feature _ALPHA_CLIP + #pragma shader_feature _EMISSION + #pragma shader_feature _DIFFUSE_RAMP + #pragma shader_feature _COLOR_ADJUST + #pragma shader_feature _RIM_LIGHTING + #pragma shader_feature _TEXTURE_BLEND + #pragma shader_feature _FOG + + #pragma fragmentoption ARB_precision_hint_fastest + #pragma multi_compile_fog + + #pragma vertex vert + #pragma fragment frag + + #include "SpriteVertexLighting.cginc" + ENDCG + } + Pass + { + Name "ShadowCaster" + Tags { "LightMode"="ShadowCaster" } + Offset 1, 1 + + Fog { Mode Off } + ZWrite On + ZTest LEqual + Cull Off + Lighting Off + + CGPROGRAM + #pragma multi_compile_shadowcaster + #pragma fragmentoption ARB_precision_hint_fastest + + #pragma vertex vert + #pragma fragment frag + + #include "SpriteShadows.cginc" + ENDCG + } + } + + FallBack "Spine/Sprite/Unlit" + CustomEditor "SpineSpriteShaderGUI" +} diff --git a/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesVertexLit.shader.meta b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesVertexLit.shader.meta new file mode 100644 index 000000000..34d232e87 --- /dev/null +++ b/spine-unity/Assets/spine-unity/Modules/Shaders/Sprite/SpritesVertexLit.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 2ce511398fb980f41b7d316c51534590 +timeCreated: 1479457856 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: