Jump to content
3DCoat Forums

Getting a vertex painted model into Unity


arumiat
 Share

Go to solution Solved by arumiat,

Recommended Posts

  • Advanced Member

Hi all

 

Was wondering whether any of you had any experience getting vertex painted models working in Unity? I've watched this video but it comes via Blender. 

So far I've painted some primitives in 3D coat and exported them as both obj and fbx's for testing purposes.

I've created a simple vertex shader using Unity's documentation

Code (CSharp):
Shader "VertexInputSimple" {
  SubShader {
    Pass {
      CGPROGRAM
      #pragma vertex vert
      #pragma fragment frag
      #include "UnityCG.cginc"
 
      struct v2f {
          float4 pos : SV_POSITION;
          fixed4 color : COLOR;
      };
   
      v2f vert (appdata_base v)
      {
          v2f o;
          o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
          o.color.xyz = v.normal * 0.5 + 0.5;
          o.color.w = 1.0;
          return o;
      }
 
      fixed4 frag (v2f i) : COLOR0 { return i.color; }
      ENDCG
    }
  }
}

I've created a new material, set the shader on the material to be VertexInputSimple (ie to use the above shader) and applied this to my objects. However I just am getting rainbow colours on them, rather than the simple vertex painted models I created in 3DC. Is anyone able to enlighten me as to any steps I'm missing?

 

post-38412-0-86831000-1422800843_thumb.ppost-38412-0-58619500-1422800844_thumb.ppost-38412-0-98414000-1422800844_thumb.p

 

Thanks in advance, 

T

Edited by arumiat
Link to comment
Share on other sites

  • Contributor

Hi Arumiat.

I never programmed in Unity, so I might be mistaken, but I looked on your shader's code and I can't find where you actually assign the vertex colour. What I see is that you assign normalized normal to X, Y and Z components of vertices' colour instead.

 

o.color.xyz = v.normal * 0.5 + 0.5;
 

So you probably need to replace this assignment with something else.

 

Maybe with:

o.color = v.color;
Link to comment
Share on other sites

  • Advanced Member

Hi ajz3D,

 

that unfortunately didn't fix it but it got me thinking and I started trying different vertex shaders I could find online.

 

This shader 

 
Shader "BumpedSpecularVtxCol" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    _BumpMap ("Normalmap", 2D) = "bump" {}
}
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 400
   
CGPROGRAM
#pragma surface surf BlinnPhong
 
 
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
half _Shininess;
 
struct Input {
    float2 uv_MainTex;
    float2 uv_BumpMap;
    half4 color : COLOR0;
};
 
void surf (Input IN, inout SurfaceOutput o) {
    fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    o.Albedo = tex.rgb * _Color.rgb * IN.color.rgb;
    o.Gloss = tex.a;
    o.Alpha = tex.a * _Color.a;
    o.Specular = _Shininess;
    o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
 
FallBack "Specular"
}
 
 

got me this effect (pretty nasty)

post-38412-0-72906600-1422888013_thumb.p

 

 

 

This shader

 Shader "LiveMesh/Simple" {
      SubShader {
         Tags {"RenderType"="Transparent" "Queue"="Transparent"}
         Fog { Mode Off }
         Lighting On
 
        Pass {
         ZWrite On
         Cull Back
         Blend SrcAlpha OneMinusSrcAlpha
 
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
 
         struct VertexInput {
             fixed4 vertex : POSITION;
             fixed4 color : COLOR;
             //fixed3 normal : NORMAL;
             //fixed4 tangent : TANGENT;
             //fixed4 texcoord : TEXCOORD;
             //fixed4 texcoord1 : TEXCOORD1;
         };
         struct VertexOutput {
             fixed4 pos : SV_POSITION;
             fixed4 col : COLOR;
         };
 
         VertexOutput vert (VertexInput v) {
 
             VertexOutput o;
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             o.col = v.color;
             
             return o;
         }
 
         fixed4 frag(VertexOutput o) : COLOR {
             return o.col;
         }
 
          ENDCG
         }
      }
    }

got me this effect (it doesn't receive light or cast shadows). Also not ideal but better. I will keep looking for more shaders that will hopefully find one that works in all the ways needed.

post-38412-0-72088200-1422888120_thumb.p

 

Thanks for taking a look for me!

Edited by arumiat
Link to comment
Share on other sites

  • Advanced Member
  • Solution

A vertex shader that does the job I was looking for..

Shader "Custom/VertexColor_Specular" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
}
 
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 300
   
CGPROGRAM
#pragma surface surf BlinnPhong vertex:vert
 
fixed4 _Color;
half _Shininess;
 
struct Input {
    fixed4 vColor;
};
 
void vert( inout appdata_full v, out Input o )
{
    o.vColor = v.color;
}
 
void surf (Input IN, inout SurfaceOutput o) {
    o.Albedo = IN.vColor.rgb * _Color.rgb;
    o.Gloss = IN.vColor.a;
    o.Alpha = IN.vColor.a * _Color.a;
    o.Specular = _Shininess;
}
ENDCG
}
 
Fallback "VertexLit"
}
 
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...