public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode) { if (!generationMode.IsPreview()) { return; } base.CollectShaderProperties(properties, generationMode); GradientUtil.GetGradientPropertiesForPreview(properties, GetVariableNameForNode(), gradient); }
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode) { var matOwner = owner as AbstractMaterialNode; if (matOwner == null) { throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode))); } if (generationMode != GenerationMode.Preview) { return; } GradientUtil.GetGradientPropertiesForPreview(properties, matOwner.GetVariableNameForSlot(id), value); }
public override string GetDefaultValue(GenerationMode generationMode) { var matOwner = owner as AbstractMaterialNode; if (matOwner == null) { throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode))); } if (generationMode.IsPreview()) { return(GradientUtil.GetGradientForPreview(matOwner.GetVariableNameForSlot(id))); } return(ConcreteSlotValueAsVariable()); }
public static void GetGradientPropertiesForPreview(PropertyCollector properties, string name, Gradient value) { properties.AddShaderProperty(new Vector1ShaderProperty() { overrideReferenceName = $"{name}_Type", value = (int)value.mode, generatePropertyBlock = false }); properties.AddShaderProperty(new Vector1ShaderProperty() { overrideReferenceName = $"{name}_ColorsLength", value = value.colorKeys.Length, generatePropertyBlock = false }); properties.AddShaderProperty(new Vector1ShaderProperty() { overrideReferenceName = $"{name}_AlphasLength", value = value.alphaKeys.Length, generatePropertyBlock = false }); for (int i = 0; i < 8; i++) { properties.AddShaderProperty(new Vector4ShaderProperty() { overrideReferenceName = $"{name}_ColorKey{i}", value = i < value.colorKeys.Length ? GradientUtil.ColorKeyToVector(value.colorKeys[i]) : Vector4.zero, generatePropertyBlock = false }); } for (int i = 0; i < 8; i++) { properties.AddShaderProperty(new Vector2ShaderProperty() { overrideReferenceName = $"{name}_AlphaKey{i}", value = i < value.alphaKeys.Length ? GradientUtil.AlphaKeyToVector(value.alphaKeys[i]) : Vector2.zero, generatePropertyBlock = false }); } }
protected override string ConcreteSlotValueAsVariable() { return(GradientUtil.GetGradientValue(value, "")); }
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode mode) { // preview is always generating a full shader, even when previewing within a subgraph bool isGeneratingSubgraph = owner.isSubGraph && (mode != GenerationMode.Preview); switch (property.propertyType) { case PropertyType.Boolean: sb.AppendLine($"$precision {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Float: sb.AppendLine($"$precision {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Vector2: sb.AppendLine($"$precision2 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Vector3: sb.AppendLine($"$precision3 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Vector4: sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Color: switch (property.sgVersion) { case 0: case 2: sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case 1: case 3: //Exposed color properties get put into the correct space automagikally by Unity UNLESS tagged as HDR, then they just get passed in as is. //for consistency with other places in the editor, we assume HDR colors are in linear space, and correct for gamma space here if ((property as ColorShaderProperty).colorMode == ColorMode.HDR) { sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = IsGammaSpace() ? LinearToSRGB({property.GetHLSLVariableName(isGeneratingSubgraph, mode)}) : {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); } else { sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); } break; default: throw new Exception($"Unknown Color Property Version on property {property.displayName}"); } break; case PropertyType.Matrix2: sb.AppendLine($"$precision2x2 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Matrix3: sb.AppendLine($"$precision3x3 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Matrix4: sb.AppendLine($"$precision4x4 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Texture2D: sb.AppendLine($"UnityTexture2D {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Texture3D: sb.AppendLine($"UnityTexture3D {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Texture2DArray: sb.AppendLine($"UnityTexture2DArray {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Cubemap: sb.AppendLine($"UnityTextureCube {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.SamplerState: sb.AppendLine($"UnitySamplerState {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); break; case PropertyType.Gradient: if (mode == GenerationMode.Preview) { sb.AppendLine($"Gradient {GetVariableNameForSlot(OutputSlotId)} = {GradientUtil.GetGradientForPreview(property.GetHLSLVariableName(isGeneratingSubgraph, mode))};"); } else { sb.AppendLine($"Gradient {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph, mode)};"); } break; } if (property.isConnectionTestable) { // If in a subgraph, the value will be read from a function parameter. // If generating preview mode code, we always inline the value, according to code gen requirements. // The parent graph always sets the explicit value to be passed to a subgraph function. sb.AppendLine("bool {0} = {1};", GetConnectionStateVariableNameForSlot(OutputSlotId), (mode == GenerationMode.Preview || !isGeneratingSubgraph) ? (IsSlotConnected(OutputSlotId) ? "true" : "false") : property.GetConnectionStateHLSLVariableName()); } }
public void SetMaterialPropertyBlockValue(Material mat) { if ((propType == PropertyType.Texture2D || propType == PropertyType.Texture2DArray || propType == PropertyType.Texture3D) && textureValue != null) { mat.SetTexture(name, m_ClassData.textureValue); } else if (propType == PropertyType.Cubemap && cubemapValue != null) { mat.SetTexture(name, m_ClassData.cubemapValue); } else if (propType == PropertyType.Color) { mat.SetColor(name, m_StructData.colorValue); } else if (propType == PropertyType.Vector2 || propType == PropertyType.Vector3 || propType == PropertyType.Vector4) { mat.SetVector(name, m_StructData.vector4Value); } else if (propType == PropertyType.Vector1) { mat.SetFloat(name, m_StructData.floatValue); } else if (propType == PropertyType.Boolean) { mat.SetFloat(name, m_StructData.booleanValue ? 1 : 0); } else if (propType == PropertyType.Matrix2 || propType == PropertyType.Matrix3 || propType == PropertyType.Matrix4) { mat.SetMatrix(name, m_StructData.matrixValue); } else if (propType == PropertyType.Gradient) { mat.SetFloat(string.Format("{0}_Type", name), (int)m_ClassData.gradientValue.mode); mat.SetFloat(string.Format("{0}_ColorsLength", name), m_ClassData.gradientValue.colorKeys.Length); mat.SetFloat(string.Format("{0}_AlphasLength", name), m_ClassData.gradientValue.alphaKeys.Length); for (int i = 0; i < 8; i++) { mat.SetVector(string.Format("{0}_ColorKey{1}", name, i), i < m_ClassData.gradientValue.colorKeys.Length ? GradientUtil.ColorKeyToVector(m_ClassData.gradientValue.colorKeys[i]) : Vector4.zero); } for (int i = 0; i < 8; i++) { mat.SetVector(string.Format("{0}_AlphaKey{1}", name, i), i < m_ClassData.gradientValue.alphaKeys.Length ? GradientUtil.AlphaKeyToVector(m_ClassData.gradientValue.alphaKeys[i]) : Vector2.zero); } } }
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode) { switch (property.propertyType) { case PropertyType.Boolean: sb.AppendLine($"$precision {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Float: sb.AppendLine($"$precision {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Vector2: sb.AppendLine($"$precision2 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Vector3: sb.AppendLine($"$precision3 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Vector4: sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Color: switch (property.sgVersion) { case 0: sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case 1: //Exposed color properties get put into the correct space automagikally by Unity UNLESS tagged as HDR, then they just get passed in as is. //for consistency with other places in the editor, we assume HDR colors are in linear space, and correct for gamma space here if ((property as ColorShaderProperty).colorMode == ColorMode.HDR) { sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = IsGammaSpace() ? LinearToSRGB({property.referenceName}) : {property.referenceName};"); } else { sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); } break; default: throw new Exception($"Unknown Color Property Version on property {property.displayName}"); } break; case PropertyType.Matrix2: sb.AppendLine($"$precision2x2 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Matrix3: sb.AppendLine($"$precision3x3 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Matrix4: sb.AppendLine($"$precision4x4 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.SamplerState: sb.AppendLine($"SamplerState {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Gradient: if (generationMode == GenerationMode.Preview) { sb.AppendLine($"Gradient {GetVariableNameForSlot(OutputSlotId)} = {GradientUtil.GetGradientForPreview(property.referenceName)};"); } else { sb.AppendLine($"Gradient {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); } break; } }
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode) { // preview is always generating a full shader, even when previewing within a subgraph bool isGeneratingSubgraph = owner.isSubGraph && (generationMode != GenerationMode.Preview); switch (property.propertyType) { case PropertyType.Boolean: sb.AppendLine($"$precision {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Float: sb.AppendLine($"$precision {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Vector2: sb.AppendLine($"$precision2 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Vector3: sb.AppendLine($"$precision3 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Vector4: sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Color: switch (property.sgVersion) { case 0: case 2: sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case 1: case 3: //Exposed color properties get put into the correct space automagikally by Unity UNLESS tagged as HDR, then they just get passed in as is. //for consistency with other places in the editor, we assume HDR colors are in linear space, and correct for gamma space here if ((property as ColorShaderProperty).colorMode == ColorMode.HDR) { sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = IsGammaSpace() ? LinearToSRGB({property.GetHLSLVariableName(isGeneratingSubgraph)}) : {property.GetHLSLVariableName(isGeneratingSubgraph)};"); } else { sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); } break; default: throw new Exception($"Unknown Color Property Version on property {property.displayName}"); } break; case PropertyType.Matrix2: sb.AppendLine($"$precision2x2 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Matrix3: sb.AppendLine($"$precision3x3 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Matrix4: sb.AppendLine($"$precision4x4 {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Texture2D: sb.AppendLine($"UnityTexture2D {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Texture3D: sb.AppendLine($"UnityTexture3D {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Texture2DArray: sb.AppendLine($"UnityTexture2DArray {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Cubemap: sb.AppendLine($"UnityTextureCube {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.SamplerState: sb.AppendLine($"UnitySamplerState {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); break; case PropertyType.Gradient: if (generationMode == GenerationMode.Preview) { sb.AppendLine($"Gradient {GetVariableNameForSlot(OutputSlotId)} = {GradientUtil.GetGradientForPreview(property.GetHLSLVariableName(isGeneratingSubgraph))};"); } else { sb.AppendLine($"Gradient {GetVariableNameForSlot(OutputSlotId)} = {property.GetHLSLVariableName(isGeneratingSubgraph)};"); } break; } }
public void SetValueOnMaterialPropertyBlock(MaterialPropertyBlock mat) { if ((propType == PropertyType.Texture2D || propType == PropertyType.Texture2DArray || propType == PropertyType.Texture3D)) { if (m_ClassData.textureValue == null) { // there's no way to set the texture back to NULL // and no way to delete the property either // so instead we set the value to what we know the default will be // (all textures in ShaderGraph default to white) mat.SetTexture(name, Texture2D.whiteTexture); } else { mat.SetTexture(name, m_ClassData.textureValue); } } else if (propType == PropertyType.Cubemap) { if (m_ClassData.cubemapValue == null) { // there's no way to set the texture back to NULL // and no way to delete the property either // so instead we set the value to what we know the default will be // (all textures in ShaderGraph default to white) // there's no Cubemap.whiteTexture, but this seems to work mat.SetTexture(name, Texture2D.whiteTexture); } else { mat.SetTexture(name, m_ClassData.cubemapValue); } } else if (propType == PropertyType.Color) { mat.SetColor(name, m_StructData.colorValue); } else if (propType == PropertyType.Vector2 || propType == PropertyType.Vector3 || propType == PropertyType.Vector4) { mat.SetVector(name, m_StructData.vector4Value); } else if (propType == PropertyType.Vector1) { mat.SetFloat(name, m_StructData.floatValue); } else if (propType == PropertyType.Boolean) { mat.SetFloat(name, m_StructData.booleanValue ? 1 : 0); } else if (propType == PropertyType.Matrix2 || propType == PropertyType.Matrix3 || propType == PropertyType.Matrix4) { mat.SetMatrix(name, m_StructData.matrixValue); } else if (propType == PropertyType.Gradient) { mat.SetFloat(string.Format("{0}_Type", name), (int)m_ClassData.gradientValue.mode); mat.SetFloat(string.Format("{0}_ColorsLength", name), m_ClassData.gradientValue.colorKeys.Length); mat.SetFloat(string.Format("{0}_AlphasLength", name), m_ClassData.gradientValue.alphaKeys.Length); for (int i = 0; i < 8; i++) { mat.SetVector(string.Format("{0}_ColorKey{1}", name, i), i < m_ClassData.gradientValue.colorKeys.Length ? GradientUtil.ColorKeyToVector(m_ClassData.gradientValue.colorKeys[i]) : Vector4.zero); } for (int i = 0; i < 8; i++) { mat.SetVector(string.Format("{0}_AlphaKey{1}", name, i), i < m_ClassData.gradientValue.alphaKeys.Length ? GradientUtil.AlphaKeyToVector(m_ClassData.gradientValue.alphaKeys[i]) : Vector2.zero); } } else if (propType == PropertyType.VirtualTexture) { // virtual texture assignments are not supported via the material property block, we must assign them to the materials } }
public void GenerateNodeCode(ShaderStringBuilder sb, GraphContext graphContext, GenerationMode generationMode) { if (generationMode.IsPreview()) { sb.AppendLine("Gradient {0} = {1};", GetVariableNameForSlot(outputSlotId), GradientUtil.GetGradientForPreview(GetVariableNameForNode())); } else { sb.AppendLine("Gradient {0} = {1}", GetVariableNameForSlot(outputSlotId), GradientUtil.GetGradientValue(gradient, ";")); } }
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode) { var property = owner.properties.FirstOrDefault(x => x.guid == propertyGuid); if (property == null) { return; } switch (property.propertyType) { case PropertyType.Boolean: sb.AppendLine($"$precision {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Vector1: sb.AppendLine($"$precision {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Vector2: sb.AppendLine($"$precision2 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Vector3: sb.AppendLine($"$precision3 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Vector4: sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Color: sb.AppendLine($"$precision4 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Matrix2: sb.AppendLine($"$precision2x2 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Matrix3: sb.AppendLine($"$precision3x3 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Matrix4: sb.AppendLine($"$precision4x4 {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.SamplerState: sb.AppendLine($"SamplerState {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); break; case PropertyType.Gradient: if (generationMode == GenerationMode.Preview) { sb.AppendLine($"Gradient {GetVariableNameForSlot(OutputSlotId)} = {GradientUtil.GetGradientForPreview(property.referenceName)};"); } else { sb.AppendLine($"Gradient {GetVariableNameForSlot(OutputSlotId)} = {property.referenceName};"); } break; } }