- Home /
a shader i am using that uses vertex colors for normals renders differently when used on a skinned mesh renderer
ok.... i am using a toon shader by DominoM, which he posted https://forum.unity.com/threads/dd-standard-toon.496744/page-2 i then edit the toon shader to be used in MMD4MECHENIM's shaders which is posted http://stereoarts.jp/ they are toon shaders... i really liked what DominoM did with the outlines in his shader and really like the actual toon ramped shading tht MMD4MECHENIM does.... heres the partial shader
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) // Modifed to use David Leon's toon BRDF by Domino Marama // Not for redistribution without the author's express written permission //credites DominoM, https://forum.unity.com/threads/dd-standard-toon.496744/page-2 // MMD4MECHENIMhttp://stereoarts.jp/ //hacked together by ... me
Pass { Name "Outline" Tags { "LightMode" = "Always" }
Cull Front
Zwrite On ZTest Less
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#include "HLSLSupport.cginc"
#include "UnityShaderVariables.cginc"
#include "Lighting.cginc"
#include "MMD4Mecanim-MMDLit-AutoLight.cginc"
#include "MMD4Mecanim-MMDLit-Lighting.cginc"
#include "MMD4Mecanim-MMDLit-Compatible.cginc"
#include "UnityCG.cginc"
#pragma target 2.0
#pragma exclude_renderers flash
#pragma vertex vert_surf
#pragma fragment frag_surf
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_fwdbase nolightmap nodirlightmap novertexlight
#pragma multi_compile _ AMB2DIFF_ON
// make fog work
#pragma multi_compile_fog
#pragma shader_feature _ _ALPHABLEND_ON _ALPHATEST_ON
#pragma shader_feature _ _DD_USE_TOON_DATA
float _OutlineEnabled;
float _Outline;
float4 _OutlineColor;
float _OutlineColorMix;
float _OutlineFade;
float _Mode;
float4 _Color;
#if defined(_ALPHATEST_ON)
float _Cutoff;
#endif
sampler2D _MainTex;
float4 _MainTex_ST;
#define MMDLIT_EDGE_ZOFST (0.00001)
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
#if defined(_DD_USE_TOON_DATA)
float4 color : COLOR;
#endif
};
struct v2f_surf
{
float4 pos : POSITION;
float3 normal : NORMAL;
UNITY_FOG_COORDS( 2 )
float4 color : COLOR;
float2 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
v2f_surf vert_surf (appdata v)
{
v2f_surf o;
UNITY_INITIALIZE_OUTPUT(v2f_surf, o);
#if 0
o.pos = _UnityObjectToClipPos(v.vertex);
o.pos.z += MMDLIT_EDGE_ZOFST * v.vertex.w;
#else
o.pos = _UnityObjectToClipPos(v.vertex);
#endif
o.worldPos = mul(_UNITY_OBJECT_TO_WORLD, v.vertex);
#if defined(_DD_USE_TOON_DATA)
float3 norm = v.color.rgb;
norm = mul( ( float3x3 )MMDLit_GetMatrixMV(), norm );
#else
float3 norm = mul( ( float3x3 )MMDLit_GetMatrixMV(), v.normal );
#endif
float2 offset = TransformViewToProjection( norm.xy );
float viewDistance = -(_UnityObjectToViewPos( v.vertex ).z);
viewDistance = _ProjectionParams.z * _OutlineFade / viewDistance;
#if defined(_DD_USE_TOON_DATA)
//o.pos.y += (min(_Outline, _Outline * viewDistance) * v.color.a) * offset;
//o.pos.y += (min(_Outline, _Outline * viewDistance) * v.color.a) * offset;
o.pos.xy += (min(_Outline, _Outline * viewDistance) * v.color.a) * offset;
#else
o.pos.xy += min(_Outline, _Outline * viewDistance) * offset;
#endif
#if defined(UNITY_REVERSED_Z)
o.pos.z -= 0.00015;
#else
o.pos.z += 0.00015;
#endif
o.uv = TRANSFORM_TEX( v.uv, _MainTex );
o.color.rgb = _OutlineColor.rgb;
o.color.a = saturate(_OutlineColor.a * viewDistance);
#ifdef _ALPHABLEND_ON
o.color.a *= _Color.a;
#endif
return o;
}
fixed4 frag_surf (v2f_surf IN) : MMDLIT_SV_TARGET
{
half4 texColor = tex2D( _MainTex, IN.uv );
#if defined(_ALPHATEST_ON)
clip((_Color.a * texColor.a) - _Cutoff);
#endif
IN.color.rgb = lerp(IN.color.rgb, _Color.rgb * texColor.rgb, _OutlineColorMix);
UNITY_APPLY_FOG( IN.fogCoord, IN.color );
return IN.color;
}
ENDCG
now with a wholes days worth of trying to figure out why this is happing
i figured out these 2 lines of the shader code which are these
#if defined(_DD_USE_TOON_DATA) float3 norm = v.color.rgb; norm = mul( ( float3x3 )MMDLit_GetMatrixMV(), norm); #else float3 norm = mul( ( float3x3 )MMDLit_GetMatrixMV(), v.normal ); #endif
now when i change norm = mul( ( float3x3 )MMDLit_GetMatrixMV(), norm); to use the meshes v.normals they do this... however when using the norm var which is the vertex colors of the mesh it does 1st image above this one...
what i am asking is...
why is it with a mesh filter (on the right) has diferent vertex colors than the skinned mesh renederer (on the left) when they use the same exact mesh!!!!!
they are the same materials and meshes.... so the only difence between these 2 game objects ... are tht
1 of the uses a skinned mesh reneder while the other uses a mesh filter
one of them has bones while the other is static
which for some odd reason results in the above image.... i also want to note tht i am using the forward renedered with no posteffects and the mesh with the vertex colors was geneated by DominosM's shaders generate vertex colors option which then save a new mesh in the assets folder which i then set as both of the above game objects mesh meaning these SHOULD have the same vertex colors.... however something is changing the skinned mesh renderes mesh on reneder......
so.... i know that the reason why this is happing is the fact tht the mesh has the same vertex colors as the skinned mesh renederer however the skinned mesh rendnerer is somehow editing them.. but how do i fix it????
Answer by ZoeZoePixel · Feb 19, 2018 at 07:34 PM
Appartently what happened was tht my normals werent converted to worldspace! heres the code to fix tht
v.color.rgb = UnityObjectToWorldNormal(v.color.rgb);
float3 norm = v.color.rgb;
@DominoM finally got it working!!! it also looks the same for a mesh renderer AND a skinned mesh renderer!!!!
Your answer
Follow this Question
Related Questions
Displace and show normals in fragment shader 1 Answer
How to blend color when using shadergraph wich uses normals? 0 Answers
Vertex Color Shader 0 Answers
How to mask textures by vertex color? 0 Answers
Persistent data values in shaders 3 Answers