Skip to content
This repository has been archived by the owner on Feb 11, 2024. It is now read-only.

Projected Grid By Jmickle66666666 #185

Merged
merged 12 commits into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Gizmos/ButtonProjectedGrid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions Gizmos/ButtonProjectedGrid.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 48 additions & 3 deletions Internal/Shaders/BrushPreview.shader
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Shader "SabreCSG/BrushPreview"
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_GridSize("Grid Size", float) = 1.0
_GridStrength("Grid Strength", Range(0,1)) = 0.25
_GridThickness("Grid Thickness", Range(0.01,0.1)) = 0.05
[HideInInspector] _GridToggle("Grid Toggle", float) = 1.0
[HideInInspector] _FaceToggle("Face Toggle", float) = 1.0
}

SubShader
Expand All @@ -16,17 +21,57 @@ Shader "SabreCSG/BrushPreview"
#pragma surface surf Lambert alpha:blend nofog

fixed4 _Color;
float _GridStrength;
float _GridSize;
float _GridThickness;
half _GridToggle;
half _FaceToggle;

struct Input
{
fixed4 color; // Can't declare empty structs in CG, so just put a filler variable to get it compiling
float3 worldPos;
float3 worldNormal;
};

float mod(float val, float mod) {

while (val < 0) {
val += mod*100;
}

return fmod(val, mod);
}

void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
c.a *= _FaceToggle;

float3 worldNormal = abs(IN.worldNormal);

worldNormal.x = (worldNormal.x > worldNormal.y && worldNormal.x > worldNormal.z)?1:0;
worldNormal.y = (worldNormal.y > worldNormal.x && worldNormal.y > worldNormal.z)?1:0;
worldNormal.z = (worldNormal.z > worldNormal.y && worldNormal.z > worldNormal.x)?1:0;

float3 worldspace = IN.worldPos;
worldspace -= _GridThickness * 0.5;

float2 grid = float2(
(worldspace.z * worldNormal.x) + (worldspace.x * worldNormal.z) + (worldspace.x * worldNormal.y),
(worldspace.y * worldNormal.x) + (worldspace.y * worldNormal.z) + (worldspace.z * worldNormal.y)
);

_GridSize = max(0.01, _GridSize);

grid.x = step((1.0 - _GridThickness)*_GridSize, mod(grid.x, _GridSize));
grid.y = step((1.0 - _GridThickness)*_GridSize, mod(grid.y, _GridSize));

float g = saturate(grid.x + grid.y);

//o.Alpha = g;

o.Albedo = c.rgb + (g * _GridStrength * _GridToggle);
o.Alpha = c.a + (g * _GridStrength * _GridToggle);
}
ENDCG
}
Expand Down
Binary file modified Materials/Add.mat
Binary file not shown.
Binary file modified Materials/Collision.mat
Binary file not shown.
Binary file modified Materials/NoCSG.mat
Binary file not shown.
Binary file modified Materials/Subtract.mat
Binary file not shown.
Binary file modified Materials/Volume.mat
Binary file not shown.
2 changes: 1 addition & 1 deletion Scripts/Brushes/PrimitiveBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ public override void UpdateVisibility()
if (meshRenderer != null)
{
#if UNITY_EDITOR
meshRenderer.enabled = mode == CSGMode.Volume || (isVisible && !CurrentSettings.ShowBrushesAsWireframes);
meshRenderer.enabled = mode == CSGMode.Volume || isVisible;
#else
meshRenderer.enabled = isVisible;
#endif
Expand Down
25 changes: 25 additions & 0 deletions Scripts/CSGModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,28 @@ private void OnGenericKeyAction(SceneView sceneView, Event e)
}
e.Use();
}
else if (KeyMappings.EventsMatch(e, Event.KeyboardEvent(KeyMappings.Instance.ToggleProjectedGrid))
&& !SabreGUIHelper.AnyControlFocussed)
{
if (e.type == EventType.KeyUp)
{
CurrentSettings.ProjectedGridEnabled = !CurrentSettings.ProjectedGridEnabled;

SceneView.RepaintAll();
}
e.Use();
}
else if (KeyMappings.EventsMatch(e, Event.KeyboardEvent(KeyMappings.Instance.ToggleBrushesAsWireframes))
&& !SabreGUIHelper.AnyControlFocussed)
{
if (e.type == EventType.KeyUp)
{
CurrentSettings.ShowBrushesAsWireframes = !CurrentSettings.ShowBrushesAsWireframes;

SceneView.RepaintAll();
}
e.Use();
}
else if (KeyMappings.EventsMatch(e, Event.KeyboardEvent(KeyMappings.Instance.EnableRadialMenu))
&& !SabreGUIHelper.AnyControlFocussed)
{
Expand Down Expand Up @@ -1602,6 +1624,9 @@ public bool EditMode
// Force the scene views to repaint (shows our own UI)
SceneView.RepaintAll();

// Update the projected grid material to match the scale of this CSG Model
CurrentSettings.ChangePosSnapDistance(1.0f);

// if(Event.current != null)
// {
// Event.current.Use();
Expand Down
29 changes: 29 additions & 0 deletions Scripts/CurrentSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ public static bool ShowBrushesAsWireframes
set
{
PlayerPrefs.SetInt(KEY_PREFIX + "ShowBrushesAsWireframes", value ? 1 : 0);
float faceToggle = value ? 0.0f : 1.0f;
SabreCSGResources.GetAddMaterial().SetFloat("_FaceToggle", faceToggle);
SabreCSGResources.GetSubtractMaterial().SetFloat("_FaceToggle", faceToggle);
SabreCSGResources.GetVolumeMaterial().SetFloat("_FaceToggle", faceToggle);
SabreCSGResources.GetNoCSGMaterial().SetFloat("_FaceToggle", faceToggle);
SabreCSGResources.GetCollisionMaterial().SetFloat("_FaceToggle", faceToggle);
}
}

Expand Down Expand Up @@ -214,6 +220,24 @@ public static Material ForegroundMaterial
}
}

public static bool ProjectedGridEnabled
{
get
{
return PlayerPrefs.GetInt(KEY_PREFIX + "ProjectedGridEnabled", 1) != 0;
}
set
{
PlayerPrefs.SetInt(KEY_PREFIX + "ProjectedGridEnabled", value ? 1 : 0);
float gridToggle = value ? 1.0f : 0.0f;
SabreCSGResources.GetAddMaterial().SetFloat("_GridToggle", gridToggle);
SabreCSGResources.GetSubtractMaterial().SetFloat("_GridToggle", gridToggle);
SabreCSGResources.GetVolumeMaterial().SetFloat("_GridToggle", gridToggle);
SabreCSGResources.GetNoCSGMaterial().SetFloat("_GridToggle", gridToggle);
SabreCSGResources.GetCollisionMaterial().SetFloat("_GridToggle", gridToggle);
}
}

public static GridMode GridMode
{
get
Expand All @@ -237,6 +261,11 @@ public static GridMode GridMode
public static void ChangePosSnapDistance(float multiplier)
{
PositionSnapDistance *= multiplier;
SabreCSGResources.GetAddMaterial().SetFloat("_GridSize", PositionSnapDistance);
SabreCSGResources.GetSubtractMaterial().SetFloat("_GridSize", PositionSnapDistance);
SabreCSGResources.GetVolumeMaterial().SetFloat("_GridSize", PositionSnapDistance);
SabreCSGResources.GetNoCSGMaterial().SetFloat("_GridSize", PositionSnapDistance);
SabreCSGResources.GetCollisionMaterial().SetFloat("_GridSize", PositionSnapDistance);
}

public static void ChangeAngSnapDistance(float multiplier)
Expand Down
2 changes: 2 additions & 0 deletions Scripts/Input/KeyMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public static KeyMappings Instance
public string IncreaseAngSnapping = "#.";

public string ToggleBrushesHidden = "h";
public string ToggleProjectedGrid = "0";
public string ToggleBrushesAsWireframes = "9";

[Header("General")]
public string ChangeBrushToAdditive = "a";
Expand Down
8 changes: 8 additions & 0 deletions Scripts/UI/SabreCSGResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ public static Texture2D ButtonShapeEditorTexture
}
}

public static Texture2D ButtonProjectedGridTexture
{
get
{
return (Texture2D)LoadObject("Gizmos/ButtonProjectedGrid.png");
}
}

public static Texture2D GroupHeaderTexture
{
get
Expand Down
21 changes: 17 additions & 4 deletions Scripts/UI/Toolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,13 @@ private static void OnBottomToolbarGUI(int windowID)
GUI.color = Color.green;
}
csgModel.AutoRebuild = SabreGUILayout.Toggle(csgModel.AutoRebuild, "Auto Rebuild");
GUI.color = Color.white;

GUILayout.Label(csgModel.BuildMetrics.BuildMetaData.ToString(), SabreGUILayout.GetForeStyle(), GUILayout.Width(140));

GUI.color = Color.white;
#if SABRE_CSG_DEBUG
GUILayout.Label(csgModel.BuildMetrics.BuildMetaData.ToString(), SabreGUILayout.GetForeStyle(), GUILayout.Width(140));
#else
EditorGUILayout.Space();
#endif
bool lastBrushesHidden = CurrentSettings.BrushesHidden;
if(lastBrushesHidden)
{
Expand Down Expand Up @@ -363,7 +366,17 @@ private static void OnBottomToolbarGUI(int windowID)
menu.DropDown(gridRect);
}

if (Event.current.type == EventType.Repaint)
GUIStyle toggleProjectedGridStyle = new GUIStyle(EditorStyles.toolbarButton);
toggleProjectedGridStyle.fixedHeight = 18;

bool lastProjectedGridEnabled = CurrentSettings.ProjectedGridEnabled;
CurrentSettings.ProjectedGridEnabled = GUILayout.Toggle(CurrentSettings.ProjectedGridEnabled, SabreCSGResources.ButtonProjectedGridTexture, toggleProjectedGridStyle);
if (CurrentSettings.ProjectedGridEnabled != lastProjectedGridEnabled)
{
SceneView.RepaintAll();
}

if (Event.current.type == EventType.Repaint)
{
gridRect = GUILayoutUtility.GetLastRect();
gridRect.width = 100;
Expand Down