3D打印机shader特效简单实现

发表于2017-01-15
评论0 2.4k浏览
        参考了neosuo的帖子,然后整合了一下上下两部分的帖子按照帖子的代码自己写了一份基本没有多大出入的shader。跟例子一样实现了大部分效果,但是在scene视图下水波纹状的曲面动画功能上还是没有实现,去找了作者的源文档也没有发现对应的代码,鉴于自身水平有限,然后把demo放出来,希望大家可以抽空研究一下作者的文档,部分没有实现的功能如果有水平也可以尝试着实现一下。
       新建一个surfaceshader,然后按照教程的步骤以此往下堆代码,但是在
1
2
3
4
5
#pragma surface surf Unlit fullforwardshadows
inline half4 LightingUnlit (SurfaceOutput s, half3 lightDir, half atten)
{
        return _ConstructColor;
}
        这部分会报错,如果再这里用了LightingUnlit 来计算定点的话好像在Standard中不支持。作者的源网址是跟我出现了一样的错误,但是那哥们儿解决了。。。方法是把#pragma里面的语句Standard变成Custom。。。可是我按照他的办法来还是报错啊 (悲剧脸)


       However如果直接跳过这部分的运算过程,直接按照教程走下去的话。效果基本实现
       长这样
 

        上面的波纹之所以出现是因为shader里面加了
float s = +sin((IN.worldPos.x*IN.worldPos.z)*60+_Time[3]+o.Normal)/120;
        然后看上去cube里面是黑色的话其实由两部分原因,

  

        首先是加了CULL off ,cull off 意思是双面渲染,如果不加就会这样
 

       至于为什么变成黑色我也不大清楚- -个人觉得应该是跟 viewDir 有关
       然后最后的效果类似这样子


        下面是简化版的3Dprinter.shader 代码,如果有问题还请各位及时指正
       效果类似这样子

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Shader "Custom/3Dprinter1" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
  
        _ConstructY("constructY" ,float) = 1
        _ConstructGap("constructGap",float ) = 1
        _ConstructColor("constructColor",Color) = (0.5,0.5,0.5,0.5)
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
        Cull Off
        CGPROGRAM       
        #pragma surface surf Standard fullforwardshadows
        #pragma target 3.0
  
        sampler2D _MainTex;
        float _ConstructY;
        fixed4 _ConstructColor;
        float _ConstructGap;
        float3 viewDir;
        int building;
  
        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
  
        struct Input {
            float2 uv_MainTex;
            float3 worldPos;
            float3 viewDir;
        };
  
        void surf (Input IN, inout SurfaceOutputStandard o){
            viewDir = IN.viewDir;
            float s = +sin((IN.worldPos.x*IN.worldPos.z)*60+_Time[3]+o.Normal)/120;
            if (IN.worldPos.y > _ConstructY + _ConstructGap +s)
                discard;
            if(IN.worldPos.y<_ConstructY){
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
            building = 0;
            }
            else{
            o.Albedo = _ConstructColor.rgb;
            o.Alpha = _ConstructColor.a;
            building = 1;
            }
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
        }
  
  
  
        inline void LightingCustom_GI(SurfaceOutputStandard s, UnityGIInput data,inout UnityGI gi){
            LightingStandard_GI(s,data,gi);
        }
  
        inline half4 LightingCustom(SurfaceOutputStandard s,half3 lightDir,UnityGI gi){
            if(building)
                return _ConstructColor;
            if(dot(s.Normal,viewDir)>0)
                return _ConstructColor;
            return LightingStandard(s,lightDir,gi);
        }
  
  
        ENDCG
  
    }
  
    FallBack "Diffuse"
}
     相关文档的飞机票
      作者
Alan Zucconihttp://www.alanzucconi.com/2016/10/02/3d-printer-shader-effect-part-1/
      译者 neosuo
http://www.manew.com/thread-97463-1-1.html

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引