unity shader之LOD以及渲染队列
发布日期:2021-05-07 17:59:10 浏览次数:32 分类:技术文章

本文共 6005 字,大约阅读时间需要 20 分钟。

LOD设置

LOD全称Level of Detail

作用:unity引擎会根据不同的LOD值在不同的平台上使用不同的SubShader
注意:在上几篇博客中已经说过在一个着色器中会有一到多个SubShader,但是系统每次只会执行一个子着色器,选择子着色器的标准就是根据子着色器所设置的LOD的值来进行选择,每一次使用着色器,都会选择第一个小于等于LOD值的子着色器。

如何设置Shader的LOD的值

:通过Shader maximumlOOD来设置最大的LOD值即Shader.globalMaximumLOD;

unity内置着色器分LOD等级

在这里插入图片描述
注意在设置最大LOD值的时候不能小于 Ver texLit kind of shaders 的值(100),否则unity将不会显示使用此着色器的物体
Demo:
场景
在这里插入图片描述
shader代码

Shader "Custom/LODShader"{
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 }//每一次只会使用一个SubShader(根据LOD的值来使用)//找到第一个<=Shader.maximumLOD这个subShader执行;如果所有的均不符合,则最后使用fallback SubShader {
Tags {
"RenderType"="Opaque" } LOD 600 CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; struct Input {
float2 uv_MainTex; }; half _Glossiness; half _Metallic; fixed4 _Color; // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props) void surf (Input IN, inout SurfaceOutputStandard o) {
o.Albedo = fixed4(1.0,0.0,0.0,1.0); } ENDCG } SubShader {
Tags {
"RenderType" = "Opaque" } LOD 500 CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; struct Input {
float2 uv_MainTex; }; half _Glossiness; half _Metallic; fixed4 _Color; // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props) void surf(Input IN, inout SurfaceOutputStandard o) {
o.Albedo = fixed4(0.0,1.0,0.0,1.0); } ENDCG } SubShader {
Tags {
"RenderType" = "Opaque" } LOD 400 CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; struct Input {
float2 uv_MainTex; }; half _Glossiness; half _Metallic; fixed4 _Color; // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props) void surf(Input IN, inout SurfaceOutputStandard o) {
o.Albedo = fixed4(0.0,0.0,1.0,1.0); } ENDCG } FallBack "Diffuse"}

c#控制LOD值的代码

using System.Collections;using System.Collections.Generic;using UnityEngine;/// /// 根据不同的平台选择不同的SubShader /// public class My_Scripts : MonoBehaviour{
public Shader shader; public int LOD_Value = 600; //这个值最小为100,如果小于100将什么都不显示(系统内置的shaderLOD值最小为100,如果小于100将什么都不显示) // Start is called before the first frame update void Start() {
Debug.Log(this.shader.maximumLOD); //this.shader.maximumLOD = this.LOD_Value; } // Update is called once per frame void Update() {
//当前这个shader最大的LOD值 this.shader.maximumLOD = this.LOD_Value; }}

显示效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

渲染队列

渲染队列

Unity引擎会将需要渲染的物体分成几个渲染的队列
Unity默认有几个渲染队列
背景1000、Geometry(几何队列)2000、Alpha Test Alpha测试(2450)、Transparent(透明)对应值为3000、Overlay(覆盖)对应值为4000这个队列是最后的渲染队列
从数值小的开始一直到数值最大(需要注意是否透明)
Unity渲染模式:普通物体从前向后渲染Alpha从后向前绘制
Demo:
场景:
将红球放置在篮球的后面
在这里插入图片描述
摄像机观测场景如下
红球shader代码

Shader "Custom/Render_text"{
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 } SubShader {
Tags {
"RenderType" = "Opaque" "Queue" = "Geometry" } LOD 200 ZTest off//关闭深度测试 CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; struct Input {
float2 uv_MainTex; }; half _Glossiness; half _Metallic; fixed4 _Color; // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props) void surf (Input IN, inout SurfaceOutputStandard o) {
o.Albedo = _Color.rgb; } ENDCG } FallBack "Diffuse"}

注意如果要修改某着色器的渲染队列并且令其起作用的话,需要将深度测试关闭

修改后的场景
在这里插入图片描述

转载地址:https://blog.csdn.net/lfanyize/article/details/104118085 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:unity-shader之混合模式、面剔除、Alpha测试、深度测试、通道遮罩
下一篇:unity-表面shader需要具备的基本知识

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年03月27日 07时07分50秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

SAP Spartacus里的injector 2019-04-27
SAP Spartacus B2B User list页面的数据加载逻辑 2019-04-27
SAP Spartacus 和Jerry Sandbox应用Store引用的source字段类型差异 2019-04-27
kitten编程猫 在线课程第七讲要点 2019-04-27
kitten编程猫 在线课程第八讲要点 - 变量的使用 2019-04-27
SAP Spartacus CMS 页面加载逻辑和性能的优化 2019-04-27
kitten编程猫 在线课程第十讲要点 - 克隆的使用,第十一讲 - 数组的使用 2019-04-27
Kitten编程猫 里的克隆体无法进行边缘检测 2019-04-27
如何实现 kitten编程猫 里虚拟手柄效果 2019-04-27
kitten编程猫里的函数定义,函数实现和函数调用原理 2019-04-27
如何成批导入数据到 kitten编程猫 类型为列表的变量中 2019-04-27
如何将 kitten编程猫里的以分号分隔的长字符串转换成列表结构 2019-04-27
Kitten编程猫 里的一步积木设计原理 2019-04-27
如何把 Kitten编程猫上开发出来的项目打包成安卓平台上可以安装的apk文件 2019-04-27
Kitten编程猫的工程文件 bcm,能发布成Android平台的apk文件吗 2019-04-27
Kitten编程猫里如何先后播放不同的背景音乐 2019-04-27
ABAP 数据结构激活时的错误消息 - combination reference table field XXX does not exist 2019-04-27
一个好用的Visual Studio Code扩展 - Live Server,适用于前端小工具开发 2019-04-27
什么是 SAP UI5 的 Component-preload.js, 什么是Minification和Ugification 2019-04-27
一个好用的 SAP UI5 本地打包(build)工具,自动生成Component-preload.js 2019-04-27