Unity3D实现指示灯亮灭效果

更新时间:2023-07-07 06:17:33 阅读: 评论:0

Unity3D实现指⽰灯亮灭效果
这周有个需求是实现控制指⽰灯亮灭的效果。实现起来很简单,但是找到这个⽅法还费了点时间。
先看效果。
原理是利⽤了标准着⾊器中的⾃发光属性,通过开关⾃发光属性来控制灯的亮灭。
具体看步骤:
1.创建⼀个⾃发光的材质球,合理设置主要颜⾊与⾃发光颜⾊,⾦属度Metallic与平滑度Smoothness⾃⼰看需求设置
2.再创建⼀个⽆⾃发光的材质球
3.如何⽤代码控制⾃发光的开关内?
若不想了解从哪⼉找到控制的地⽅的,这⼀段可以跳过,直接看⼯程。
既然在编辑器能够勾选⾃发光,那⼀定有⼀个编辑器脚本可以控制这部分。那么这个编辑器脚本在哪⼉呢?你需要从unity官⽹下载你对应版本的内置着⾊器源码,下载回来的⽂件夹中Editor⽂件夹下的StandardShaderGUI.cs即是标准着⾊器编辑器的源码。
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT licen ()
using System;
using UnityEngine;
namespace UnityEditor
{
internal class StandardShaderGUI : ShaderGUI
{
private enum WorkflowMode
{
Specular,
Metallic,
Dielectric
}
public enum BlendMode
{
Opaque,
Opaque,
Cutout,
Fade,  // Old school alpha-blending mode, fresnel does not affect amount of transparency
Transparent // Physically plausible transparency mode, implemented as alpha pre-multiply
}
public enum SmoothnessMapChannel
{
SpecularMetallicAlpha,
AlbedoAlpha,
}
private static class Styles
{
public static GUIContent uvSetLabel = new GUIContent("UV Set");
public static GUIContent albedoText = new GUIContent("Albedo", "Albedo (RGB) and Transparency (A)");
public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff");
public static GUIContent specularMapText = new GUIContent("Specular", "Specular (RGB) and Smoothness (A)");
public static GUIContent metallicMapText = new GUIContent("Metallic", "Metallic (R) and Smoothness (A)");
public static GUIContent smoothnessText = new GUIContent("Smoothness", "Smoothness value");
public static GUIContent smoothnessScaleText = new GUIContent("Smoothness", "Smoothness scale factor");
public static GUIContent smoothnessMapChannelText = new GUIContent("Source", "Smoothness texture and channel");
public static GUIContent highlightsText = new GUIContent("Specular Highlights", "Specular Highlights");
public static GUIContent reflectionsText = new GUIContent("Reflections", "Glossy Reflections");
public static GUIContent normalMapText = new GUIContent("Normal Map", "Normal Map");
public static GUIContent heightMapText = new GUIContent("Height Map", "Height Map (G)");
public static GUIContent occlusionText = new GUIContent("Occlusion", "Occlusion (G)");
public static GUIContent emissionText = new GUIContent("Color", "Emission (RGB)");
public static GUIContent detailMaskText = new GUIContent("Detail Mask", "Mask for Secondary Maps (A)");
public static GUIContent detailAlbedoText = new GUIContent("Detail Albedo x2", "Albedo (RGB) multiplied by 2");
public static GUIContent detailNormalMapText = new GUIContent("Normal Map", "Normal Map");
public static string primaryMapsText = "Main Maps";
public static string condaryMapsText = "Secondary Maps";
public static string forwardText = "Forward Rendering Options";红锈的意思
public static string renderingMode = "Rendering Mode";
public static string advancedText = "Advanced Options";
public static GUIContent emissiveWarning = new GUIContent("Emissive value is animated but the material has not been configured to support emis sive. Plea make sure the material itlf has some amount of emissive.");
public static readonly string[] blendNames = Enum.GetNames(typeof(BlendMode));
}
MaterialProperty blendMode = null;
MaterialProperty albedoMap = null;
MaterialProperty albedoColor = null;
MaterialProperty alphaCutoff = null;
MaterialProperty specularMap = null;
MaterialProperty specularColor = null;
MaterialProperty metallicMap = null;
MaterialProperty metallic = null;
MaterialProperty smoothness = null;汉服文化知识
MaterialProperty smoothnessScale = null;
MaterialProperty smoothnessMapChannel = null;
MaterialProperty highlights = null;
MaterialProperty reflections = null;
MaterialProperty bumpScale = null;
MaterialProperty bumpMap = null;
MaterialProperty occlusionStrength = null;
MaterialProperty occlusionMap = null;
MaterialProperty heigtMapScale = null;
MaterialProperty heightMap = null;
MaterialProperty emissionColorForRendering = null;
MaterialProperty emissionMap = null;
MaterialProperty detailMask = null;
MaterialProperty detailAlbedoMap = null;
MaterialProperty detailAlbedoMap = null;
MaterialProperty detailNormalMapScale = null;
MaterialProperty detailNormalMap = null;
MaterialProperty uvSetSecondary = null;
MaterialEditor m_MaterialEditor;
WorkflowMode m_WorkflowMode = WorkflowMode.Specular;
ColorPickerHDRConfig m_ColorPickerHDRConfig = new ColorPickerHDRConfig(0f, 99f, 1 / 99f, 3f);
bool m_FirstTimeApply = true;
public void FindProperties(MaterialProperty[] props)
{
blendMode = FindProperty("_Mode", props);
albedoMap = FindProperty("_MainTex", props);
albedoColor = FindProperty("_Color", props);
alphaCutoff = FindProperty("_Cutoff", props);
specularMap = FindProperty("_SpecGlossMap", props, fal);
specularColor = FindProperty("_SpecColor", props, fal);
metallicMap = FindProperty("_MetallicGlossMap", props, fal);
metallic = FindProperty("_Metallic", props, fal);
if (specularMap != null && specularColor != null)
m_WorkflowMode = WorkflowMode.Specular;
el if (metallicMap != null && metallic != null)
m_WorkflowMode = WorkflowMode.Metallic;
el
m_WorkflowMode = WorkflowMode.Dielectric;
smoothness = FindProperty("_Glossiness", props);
smoothnessScale = FindProperty("_GlossMapScale", props, fal);
smoothnessMapChannel = FindProperty("_SmoothnessTextureChannel", props, fal);
highlights = FindProperty("_SpecularHighlights", props, fal);
reflections = FindProperty("_GlossyReflections", props, fal);
bumpScale = FindProperty("_BumpScale", props);
bumpMap = FindProperty("_BumpMap", props);
heigtMapScale = FindProperty("_Parallax", props);
heightMap = FindProperty("_ParallaxMap", props);
occlusionStrength = FindProperty("_OcclusionStrength", props);
马里奥疯兔
occlusionMap = FindProperty("_OcclusionMap", props);
emissionColorForRendering = FindProperty("_EmissionColor", props);
emissionMap = FindProperty("_EmissionMap", props);
detailMask = FindProperty("_DetailMask", props);
detailAlbedoMap = FindProperty("_DetailAlbedoMap", props);
detailNormalMapScale = FindProperty("_DetailNormalMapScale", props);
detailNormalMap = FindProperty("_DetailNormalMap", props);胃胀痛怎么办
uvSetSecondary = FindProperty("_UVSec", props);
}
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
{
FindProperties(props); // MaterialProperties can be animated so we do not cache them but fetch them every event to ensure animated values are u pdated correctly
m_MaterialEditor = materialEditor;
Material material = materialEditor.target as Material;
// Make sure that needed tup (ie keywords/renderqueue) are t up if we're switching some existing
// material to a standard shader.
// Do this before any GUI code has been issued to prevent layout issues in subquent GUILayout statements (ca 780071)
if (m_FirstTimeApply)
{
MaterialChanged(material, m_WorkflowMode);
冰箱耗电量
m_FirstTimeApply = fal;
}
ShaderPropertiesGUI(material);
}
public void ShaderPropertiesGUI(Material material)
{
// U default labelWidth
EditorGUIUtility.labelWidth = 0f;
// Detect any changes to the material
EditorGUI.BeginChangeCheck();
{
BlendModePopup();
// Primary properties
GUILayout.Label(Styles.primaryMapsText, EditorStyles.boldLabel);
DoAlbedoArea(material);
DoSpecularMetallicArea();
m_MaterialEditor.alMapText, bumpMap, ureValue != null ? bumpScale : null);
m_MaterialEditor.TexturePropertySingleLine(Styles.heightMapText, heightMap, ureValue != null ? heigtMapScale : null);
m_MaterialEditor.lusionText, occlusionMap, ureValue != null ? occlusionStrength : null);                m_MaterialEditor.TexturePropertySingleLine(Styles.detailMaskText, detailMask);
DoEmissionArea(material);
EditorGUI.BeginChangeCheck();
m_MaterialEditor.TextureScaleOfftProperty(albedoMap);
if (EditorGUI.EndChangeCheck())
脉通胶囊说明书
EditorGUILayout.Space();
// Secondary properties
GUILayout.daryMapsText, EditorStyles.boldLabel);
m_MaterialEditor.TexturePropertySingleLine(Styles.detailAlbedoText, detailAlbedoMap);
宫崎骏千与千寻
m_MaterialEditor.TexturePropertySingleLine(Styles.detailNormalMapText, detailNormalMap, detailNormalMapScale);
m_MaterialEditor.TextureScaleOfftProperty(detailAlbedoMap);
m_MaterialEditor.ShaderProperty(uvSetSecondary, );
// Third properties
GUILayout.Label(Styles.forwardText, EditorStyles.boldLabel);
if (highlights != null)
m_MaterialEditor.ShaderProperty(highlights, Styles.highlightsText);
if (reflections != null)
m_MaterialEditor.ShaderProperty(reflections, flectionsText);
}
if (EditorGUI.EndChangeCheck())
{
淘宝特惠
foreach (var obj in blendMode.targets)
MaterialChanged((Material)obj, m_WorkflowMode);
}
EditorGUILayout.Space();
GUILayout.Label(Styles.advancedText, EditorStyles.boldLabel);
m_MaterialEditor.RenderQueueField();
m_MaterialEditor.EnableInstancingField();
}
internal void DetermineWorkflow(MaterialProperty[] props)
{
if (FindProperty("_SpecGlossMap", props, fal) != null && FindProperty("_SpecColor", props, fal) != null)
m_WorkflowMode = WorkflowMode.Specular;
el if (FindProperty("_MetallicGlossMap", props, fal) != null && FindProperty("_Metallic", props, fal) != null)
m_WorkflowMode = WorkflowMode.Metallic;
el
m_WorkflowMode = WorkflowMode.Dielectric;
}
public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)

本文发布于:2023-07-07 06:17:33,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1071279.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

下一篇:Anti-HBe
标签:发光   控制   编辑器
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图