shader Category

Category:是渲染命令的逻辑组,着色器可以多个子着色器,他们需要共同的效果


// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Original code by Nora
// http://stereoarts.jp
//
// Retrieved from:
// https://developer.oculusvr.com/forums/viewtopic.php?f=37&t=844#p28982

Shader "Panorama/2dSphere" {
  Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Texture", 2D) = "white" {}
  }
  Category {
   Tags { "Queue"="Background" }
    ZWrite Off
    Lighting Off
    Fog {Mode Off}
    BindChannels {
      Bind "Color", color
      Bind "Vertex", vertex
      Bind "TexCoord", texcoord
    }
    SubShader {
    cull off
      Pass {
        SetTexture [_MainTex] {
          Combine texture
        }
        SetTexture [_MainTex] {
          constantColor [_Color]
          combine previous * constant
        }
      }
    }
  }
}



ShaderLab 语法:绑定通道 (BindChannels)        
绑定通道 (BindChannels) 命令让您可以指定顶点数据如何绘制到图形硬件中。
使用可编程的顶点着色器时,绑定通道无效,因为这种情况下,捆绑由顶点着色器输入控制



默认情况下,Unity 为您进行绑定,但是在有些情况下,您需要自定义使用的绑定。
例如:您可以绘制在第一个纹理阶段使用的主要 UV 集和第二个纹理阶段使用的辅助 UV 集


;或者通知硬件要考虑顶点颜色。
语法
BindChannels { Bind "source", target }
指定顶点数据源 (source) 映射到硬件目标 (target)。
源 (Source)  可以是:
顶点 (Vertex):顶点位置
法线 (Normal):顶点法线
切线 (Tangent):顶点切线
纹理坐标 (Texcoord):主要 UV 坐标
纹理坐标 1 (Texcoord1):次要 UV 坐标
颜色 (Color):逐顶点颜色
目标 (Target) 可以是:
顶点 (Vertex):顶点位置
法线 (Normal:顶点法线
切线 (Tangent):顶点切线
Texcoord0, Texcoord1, ...:相应纹理阶段的纹理坐标
纹理坐标 (Texcoord):所有纹理阶段的纹理坐标
颜色 (Color):顶点颜色
详细信息
Unity 在哪些源可绘制到哪些目标上有一些限制。源和目标在 顶点 (Vertex)、法线 


(Normal)、切线 (Tangent) 和 颜色 (Color) 上必须是相匹配的。网格的纹理坐标


(Texcoord 和 Texcoord1)可绘制到纹理坐标目标(对于所有的纹理阶段为 Texcoord 或者


对于特定阶段为 TexcoordN)。
BindChannels 的两个经典使用案例:
考虑顶点颜色的着色器。
使用两个 UV 集的着色器。
示例
// 将第一个 UV 集绘制到第一个纹理阶段// 并将第二个 UV 集绘制到第二个纹理阶段


BindChannels {   Bind "Vertex", vertex   Bind "texcoord", texcoord0   Bind 


"texcoord1", texcoord1} 
// 将第一个 UV 集绘制到所有纹理阶段// 并使用顶点颜色BindChannels {   Bind 


"Vertex", vertex   Bind "texcoord", texcoord   Bind "Color", color} 

转载于:https://www.cnblogs.com/nafio/p/9137248.html

### 如何用Shader实现透明物体的阴影效果 #### 使用HLSL实现在Unity中的透明物体阴影效果 为了在Unity中使用HLSL实现透明物体的阴影效果,通常需要自定义表面着色器或顶点片段着色器。对于半透明对象来说,重要的是处理好混合模式和深度测试/写入。 ```csharp // TransparentShadow.hlsl Shader "Custom/TransparentWithShadows" { Properties { _MainTex ("Texture", 2D) = "white" {} _Color ("Main Color", Color) = (1,1,1,1) } SubShader { Tags { "RenderType"="Transparent" "Queue"="Transparent"} Pass // ShadowCastingPass { Name "ShadowCaster" Tags{"LightMode"="ShadowCaster"} ZWrite On Cull Off CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; }; struct v2f { V2F_SHADOW_CASTER; }; v2f vert(appdata_t v) { v2f o; TRANSFER_SHADOW_CASTER_NORMALOFFSET(o) return o; } half4 frag(v2f i) : SV_Target { SHADOW_CASTER_FRAGMENT(i) } ENDCG } CGPROGRAM #pragma surface surf Lambert alpha:blend sampler2D _MainTex; fixed4 _Color; struct Input { float2 uv_MainTex; }; void surf(Input IN, inout SurfaceOutput o) { fixed4 col = tex2D(_MainTex, IN.uv_MainTex) * _Color; o.Albedo = col.rgb; o.Alpha = col.a; } ENDCG } } ``` 这段代码展示了如何设置一个带有Alpha通道纹理的地图,并确保该材质能够正确投射阴影[^1]。 #### 利用GLSL编写透明物体阴影着色器 当采用OpenGL ES作为渲染API时,则会涉及到GLSL语言下的具体实现方式: ```glsl // Vertex Shader #version 300 es precision mediump float; layout(location=0)in vec4 a_position; uniform mat4 u_mvpMatrix; out highp vec4 v_shadowCoord; void main() { gl_Position=a_position*u_mvpMatrix; } // Fragment Shader #version 300 es precision mediump float; in highp vec4 v_shadowCoord; uniform sampler2D shadowMap; uniform lowp int useSoftShadows; float textureProj(sampler2D shadowMap,vec4 sc){ if(sc.w>0.0){ return step(0.5,texelFetch(shadowMap,int(sc.xy),int(sc.z)).r); }else{return 1.0;} } void main(){ float visibility=(useSoftShadows==1)?textureProjPCF(shadowMap,v_shadowCoord):textureProj(shadowMap,v_shadowCoord); FragColor=floor(vec4(visibility)); } ``` 这里给出了简单的vertex shader 和fragment shader 来计算来自光源方向上的遮挡情况并应用到最终颜色上[^3]。 #### Unity ShaderLab结合CG/HLSL实现方案 考虑到兼容性和易用性的因素,在实际项目开发过程中推荐优先考虑基于Unity ShaderLab的方式来进行此类功能扩展。下面是一个简化版的例子说明怎样利用内置函数完成上述需求: ```csharp Shader "Custom/TranslucentObjectWithShadow" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _Opacity ("Opacity", Range(0.0,1.0)) = 0.75 } Category { Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} Blend SrcAlpha OneMinusSrcAlpha Lighting Off ZWrite Off BindChannels { Bind "Vertex", vertex ; Bind "TexCoord", texcoord ; } SubShader { Pass { SetTexture [_MainTex] { combine texture * primary } AlphaTest Greater[_Cutoff] } UsePass "Legacy Shaders/Transparent/Diffuse/FORWARD" } } } ``` 此脚本不仅实现了基础的颜色叠加运算还加入了alpha测试以过滤掉不必要的像素输出[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值