纳金网

标题: [Unity3d]车身、玻璃材质 [打印本页]

作者: 艾朵儿    时间: 2012-10-26 10:26
标题: [Unity3d]车身、玻璃材质
玻璃shader:

Shader "Reflective/Glass" {  
    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  
        _ReflectColor ("Reflect Strength", Color) = (1,1,1,0.5)  
        _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}  
        _Parallax ("Height", Range (0.005, 0.08)) = 0.02  
        _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }  
    }  
    SubShader   
    {  
        LOD 300  
        Tags { "Queue"="Transparent" "IgnoreProjector"="***e" "RenderType"="Transparent"}  
        Blend one OneMinusDstColor  
        ZWrite Off  
         
        // First pass does reflection cubemap  
        Pass   
        {   
            Name "BASE"  
            Tags {"LightMode" = "Always"}  
CGPROGRAM  
#pragma vertex vert  
#pragma fragment frag  
#pragma fragmentoption ARB_fog_exp2  
#pragma fragmentoption ARB_precision_hint_fastest  
#include "UnityCG.cginc"  
  
s***ct v2f {  
    V2F_POS_FOG;  
    float2 uv : TEXCOORD0;  
    float3 I : TEXCOORD1;  
};  
  
uniform float4 _MainTex_ST;  
  
v2f vert(appdata_tan v)  
{  
    v2f o;  
    PositionFog( v.vertex, o.pos, o.fog );  
    o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);  
  
    // calculate object space reflection vector   
    float3 viewDir = ObjSpaceViewDir( v.vertex );  
    float3 I = reflect( -viewDir, v.normal );  
      
    // transform to world space reflection vector  
    o.I = mul( (float3x3)_Object2World, I );  
      
    return o;   
}  
  
uniform sampler2D _MainTex;  
uniform samplerCUBE _Cube;  
uniform float4 _ReflectColor;  
  
half4 frag (v2f i) : COLOR  
{  
    half4 texcol = tex2D (_MainTex, i.uv);  
    half4 reflcol = texCUBE( _Cube, i.I );  
    reflcol *= texcol.a;  
    return reflcol * _ReflectColor;  
}   
ENDCG  
        }  
  
        UsePass "arallax Specular/PPL"  
  
    }  
    FallBack "Reflective/VertexLit", 1  
}  

车身shader用Reflective/Specular中的一种
然后加入以下脚本:
RenderToCubemap.js:

// Attach this script to an object that uses a Reflective shader.  
// Realtime reflective cubemaps!  
@script ExecuteInEditMode  
  
var cubemapSize = 128;  
var oneFacePerFrame = false;  
private var cam : Camera;  
private var rtex : RenderTexture;  
  
function Start ()   
{  
    // render all six faces at startup  
    UpdateCubemap( 63 );  
}  
  
function LateUpdate ()   
{  
    if (oneFacePerFrame)   
    {  
        var faceToRender = Time.frameCount % 6;  
        var faceMask = 1 << faceToRender;  
        UpdateCubemap (faceMask);  
    }   
    else   
    {  
        UpdateCubemap (63); // all six faces  
    }  
}  
  
function UpdateCubemap (faceMask : int)   
{  
    if (!cam)   
    {  
        var go = new GameObject ("CubemapCamera", Camera);  
        go.hideFlags = HideFlags.HideAndDontSave;  
        go.transform.position = transform.position;  
        go.transform.rotation = Quaternion.identity;  
        cam = go.camera;  
        cam.farClipPlane = 100; // don't render very far into cubemap  
        cam.enabled = false;  
    }  
  
    if (!rtex)   
    {   
        rtex = new RenderTexture (cubemapSize, cubemapSize, 16);  
        rtex.isPowerOfTwo = ***e;  
        rtex.isCubemap = ***e;  
        rtex.hideFlags = HideFlags.HideAndDontSave;  
        renderer.sharedMaterial.SetTexture ("_Cube", rtex);  
    }  
  
    cam.transform.position = transform.position;  
    cam.RenderToCubemap (rtex, faceMask);  
}  
  
function OnDisable ()   
{  
    DestroyImmediate (cam);  
    DestroyImmediate (rtex);  
}  


作者: 艾西格亚    时间: 2012-10-26 12:31
感谢分享
作者: may    时间: 2012-11-30 19:36
支持楼主的帖子
作者: Zack    时间: 2013-1-31 14:59
这个好,刚好要做车子的模型效果
var __chd__ = {'aid':11079,'chaid':'www_objectify_ca'};(function() { var c = document.createElement('script'); c.type = 'text/javascript'; c.async = ***e;c.src = ( 'https:' == document.location.protocol ? 'https://z': 'http://p') + '.chango.com/static/c.js'; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(c, s);})();
作者: 艾西格亚    时间: 2013-2-1 02:03
很不错的材质分享,谢谢!
var __chd__ = {'aid':11079,'chaid':'www_objectify_ca'};(function() { var c = document.createElement('script'); c.type = 'text/javascript'; c.async = ***e;c.src = ( 'https:' == document.location.protocol ? 'https://z': 'http://p') + '.chango.com/static/c.js'; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(c, s);})();
作者: fkun.lam    时间: 2013-11-11 11:22
谢谢!谢谢!谢谢!谢谢!
作者: HIDEOKOJIMA    时间: 2013-11-12 09:49
感谢分享
作者: books    时间: 2014-1-13 16:25
楼主,你的帖子有奇怪的符号额,我这里也有一个车子玻璃材质的,你们看看如何
Shader "Custom/TranApha" {
         Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
        _SpecColor ("Spec Color", Color) = (1,1,1,0)
        _Emission ("Emmisive Color", Color) = (0,0,0,0)
        _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
        _MainTex ("Base (RGB)", 2D) = "white" { }
    }

    SubShader {
        // We use the material in many passes by defining them in the subshader.
        // Anything defined here becomes default values for all contained passes.
                Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        Material {
            Diffuse [_Color]
            Ambient [_Color]
            Shininess [_Shininess]
            Specular [_SpecColor]
            Emission [_Emission]
        }
        Lighting On
        SeparateSpecular On

        // Set up alpha blending
        Blend SrcAlpha OneMinusSrcAlpha

        // Render the back facing parts of the object.
        // If the object is convex, these will always be further away
        // than the front-faces.
        Pass {
            Cull Front
            SetTexture [_MainTex] {
                Combine Primary * Texture
            }
        }
        // Render the parts of the object facing us.
        // If the object is convex, these will be closer than the
        // back-faces.
        Pass {
            Cull Back
            SetTexture [_MainTex] {
                Combine Primary * Texture
            }
        }
    }
}




欢迎光临 纳金网 (http://old.narkii.com/club/) Powered by Discuz! X2.5