Reorganize assets folder and tweak shaders

Moved rim shading and underwater fog to the common cginc
This commit is contained in:
Andrew Cassidy 2024-07-25 23:29:42 -07:00
parent 14640c1dca
commit 7286202be9
42 changed files with 23 additions and 196 deletions

15
.gitignore vendored
View File

@ -1,10 +1,8 @@
# Unity Project Directories
Assets/*
!Assets/Shaders/
!Assets/Textures/
!Assets/Scripts/
!Assets/UI/
!Assets/ConformalDecals/
!Assets/ConformalDecals
Assets/ConformalDecals/Fonts
Assets/ConformalDecals/Parts
KSP/
Library/
Logs/
@ -12,6 +10,9 @@ Packages/
ProjectSettings/
Temp/
# Autogenerated shaders
Assets/ConformalDecals/Shaders/Generated/*.shader
# Unity Assetbundle Manifest Files
GameData/ConformalDecals/Resources/Resources
GameData/ConformalDecals/Resources/*.manifest
@ -41,9 +42,6 @@ ExportedObj/
*.opendb
*.VC.db
# Autogenerated shaders
Assets/Shaders/Generated/*.shader
# Binaries and support files
Source/ConformalDecals/dlls
Source/ConformalDecals/bin
@ -58,3 +56,4 @@ Source/ConformalDecals/bin
obj
*.swp
@thumbs

View File

@ -1,6 +1,4 @@
#ifndef DECALS_COMMON_INCLUDED
// Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct v2f members screenUV)
#pragma exclude_renderers d3d11
#define DECALS_COMMON_INCLUDED
#include "AutoLight.cginc"
@ -117,10 +115,6 @@ struct v2f
#ifdef UNITY_PASS_FORWARDADD
UNITY_LIGHTING_COORDS(5,6)
#endif //UNITY_PASS_FORWARDADD
#ifdef UNITY_PASS_DEFERRED
float3 screenUV : TEXCOORD5;
#endif
};
@ -138,6 +132,8 @@ inline float CalcMipLevel(float2 texture_coord) {
return 0.5 * log2(delta_max_sqr);
}
// Decal bounds distance function
// takes in a world position, world normal, and projector normal and outputs a unitless signed distance from the
inline float BoundsDist(float3 p, float3 normal, float3 projNormal) {
float3 q = abs(p - 0.5) - 0.5; // 1x1 square/cube centered at (0.5,0.5)
//float dist = length(max(q,0)) + min(max(q.x,max(q.y,q.z)),0.0); // true SDF
@ -212,14 +208,6 @@ v2f vert(appdata_decal v)
// pass shadow and, possibly, light cookie coordinates to pixel shader
UNITY_TRANSFER_LIGHTING(o, 0.0);
#ifdef UNITY_PASS_DEFERRED
o.screenUV = o.pos.xyw;
// Correct flip when rendering with a flipped projection matrix.
// (I've observed this differing between the Unity scene & game views)
o.screenUV.y *= _ProjectionParams.x;
#endif
return o;
}
@ -279,14 +267,21 @@ SurfaceOutput frag_common(v2f IN, out float3 viewDir, out UnityGI gi) {
// initialize surface output
o.Albedo = 0.0;
o.Emission = 0.0;
o.Specular = 0.0;
o.Alpha = 0.0;
o.Specular = 0.4;
o.Alpha = _DecalOpacity;
o.Gloss = 0.0;
o.Normal = fixed3(0,0,1);
// call surface function
surf(i, o);
// apply KSP fog. In the deferred pass this is a no-op
o.Albedo = UnderwaterFog(i.worldPosition, o.Albedo).rgb;
// apply KSP rim lighting
half rim = 1.0 - saturate(dot(normalize(i.viewDir), o.Normal));
o.Emission += o.Emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a;
// compute world normal
float3 worldN;
worldN.x = dot(_unity_tbn_0, o.Normal);

View File

@ -3,15 +3,13 @@
void surf(DecalSurfaceInput IN, inout SurfaceOutput o) {
float4 color = tex2D(_Decal, IN.uv_decal);
o.Albedo = color.rgb;
o.Specular = 0.4;
o.Gloss = _Shininess;
o.Albedo = UnderwaterFog(IN.worldPosition, color).rgb;
o.Alpha = _DecalOpacity;
#ifdef DECAL_BASE_NORMAL
float3 normal = IN.normal;
float wearFactor = 1 - normal.z;
float wearFactorAlpha = saturate(_EdgeWearStrength * wearFactor);
o.Alpha *= saturate(1 + _EdgeWearOffset - saturate(_EdgeWearStrength * wearFactor));
#endif
@ -24,10 +22,6 @@ void surf(DecalSurfaceInput IN, inout SurfaceOutput o) {
o.Specular = specular;
#endif
half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
o.Emission = (_RimColor.rgb * pow(rim, _RimFalloff)) * _RimColor.a;
#ifdef DECAL_EMISSIVE
o.Emission += tex2D(_Emissive, IN.uv_emissive).rgb * _Emissive_Color.rgb * _Emissive_Color.a;
#endif

View File

@ -136,6 +136,10 @@
#pragma multi_compile_local __ DECAL_BASE_NORMAL
//{{ self.pragmas() }}
//{% block prepass_skip_variants %}
#pragma skip_variants DECAL_SPECMAP DECAL_EMISSIVE
//{% endblock %}
#define DECAL_PREPASS
//{{ self.body() }}

View File

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 40 KiB

View File

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 470 KiB

After

Width:  |  Height:  |  Size: 470 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

View File

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB

View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 6.2 KiB

View File

Before

Width:  |  Height:  |  Size: 260 B

After

Width:  |  Height:  |  Size: 260 B

View File

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

View File

Before

Width:  |  Height:  |  Size: 169 B

After

Width:  |  Height:  |  Size: 169 B

View File

@ -1,46 +0,0 @@
using System;
using UnityEngine;
[ExecuteInEditMode]
public class DecalProjectorTest : MonoBehaviour
{
public GameObject target = null;
public Material targetMaterial = null;
public MeshRenderer targetRenderer;
public float aspectRatio = 1.0f;
public float size = 1.0f;
public float factor = 1.0f;
private Matrix4x4 _projectionMatrix;
private Matrix4x4 _OrthoMatrix;
private int _matrixID;
private int _normalID;
public int _tangentID;
// Start is called before the first frame update
void Awake()
{
_projectionMatrix = Matrix4x4.identity;
targetRenderer = target.GetComponent<MeshRenderer>();
}
// Update is called once per frame
void Update()
{
Vector3 pos =new Vector3( 0.5f ,0.5f, 0);
Vector3 scale = new Vector3(1 / size, 1 / (aspectRatio * size), 1);
_OrthoMatrix.SetTRS(pos, Quaternion.identity, scale);
//Debug.Log(_OrthoMatrix);
var targetToProjector = transform.worldToLocalMatrix * targetRenderer.localToWorldMatrix;
var projectorToTarget = targetRenderer.worldToLocalMatrix * transform.localToWorldMatrix;
_projectionMatrix = _OrthoMatrix * targetToProjector;
targetMaterial.SetMatrix("_ProjectionMatrix", _projectionMatrix);
targetMaterial.SetVector("_DecalNormal", projectorToTarget.MultiplyVector(Vector3.back).normalized);
targetMaterial.SetVector("_DecalTangent", projectorToTarget.MultiplyVector(Vector3.right).normalized);
}
}

View File

@ -1,119 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Rendering;
public class TextRenderTest : MonoBehaviour {
//[InspectorButton("go")] public bool button;
public Camera _camera;
public GameObject _cameraObject;
public TextMeshPro _tmp;
public Material _blitMaterial;
public Material _targetMaterial;
public RenderTexture renderTex;
private float pixelDensity = 8;
private int MaxTextureSize = 4096;
private static readonly int Decal = Shader.PropertyToID("_Decal");
public const TextureFormat TextTextureFormat = TextureFormat.RG16;
public const RenderTextureFormat TextRenderTextureFormat = RenderTextureFormat.R8;
// Start is called before the first frame update
void Start() {
Debug.Log("starting...");
StartCoroutine(OnRender());
var thing = new GameObject();
thing.AddComponent<TextMeshPro>();
}
// Update is called once per frame
void Update() { }
private IEnumerator OnRender() {
Debug.Log("starting...2");
// calculate camera and texture size
_tmp.ForceMeshUpdate();
var mesh = _tmp.mesh;
mesh.RecalculateBounds();
var bounds = mesh.bounds;
Debug.Log(bounds.size);
var width = bounds.size.x * pixelDensity;
var height = bounds.size.y * pixelDensity;
var widthPoT = Mathf.NextPowerOfTwo((int) width);
var heightPoT = Mathf.NextPowerOfTwo((int) height);
if (widthPoT > MaxTextureSize) {
widthPoT /= widthPoT / MaxTextureSize;
heightPoT /= widthPoT / MaxTextureSize;
}
if (heightPoT > MaxTextureSize) {
widthPoT /= heightPoT / MaxTextureSize;
heightPoT /= heightPoT / MaxTextureSize;
}
widthPoT = Mathf.Min(widthPoT, MaxTextureSize);
heightPoT = Mathf.Min(heightPoT, MaxTextureSize);
var widthRatio = widthPoT / width;
var heightRatio = heightPoT / height;
var sizeRatio = Mathf.Min(widthRatio, heightRatio);
Debug.Log(sizeRatio);
int scaledHeight = (int) (sizeRatio * height);
int scaledWidth = (int) (sizeRatio * width);
Debug.Log($"width = {scaledWidth}");
Debug.Log($"height = {scaledHeight}");
_camera.orthographicSize = scaledHeight / pixelDensity / 2;
_camera.aspect = (float) widthPoT / heightPoT;
_cameraObject.transform.localPosition = new Vector3(bounds.center.x, bounds.center.y, -1);
var halfHeight = heightPoT / pixelDensity / 2 / sizeRatio;
var halfWidth = widthPoT / pixelDensity / 2 / sizeRatio;
var matrix = Matrix4x4.Ortho(bounds.center.x - halfWidth, bounds.center.x + halfWidth,
bounds.center.y - halfHeight, bounds.center.y + halfHeight, -1, 1);
// setup texture
var texture = new Texture2D(widthPoT, heightPoT, TextTextureFormat, true);
_targetMaterial.SetTexture(Decal, texture);
// setup render texture
renderTex = RenderTexture.GetTemporary(widthPoT, heightPoT, 0, TextRenderTextureFormat, RenderTextureReadWrite.Linear, 1);
renderTex.autoGenerateMips = false;
RenderTexture.active = renderTex;
GL.PushMatrix();
GL.LoadProjectionMatrix(matrix);
_blitMaterial.SetPass(0);
Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
GL.PopMatrix();
// setup material
_blitMaterial.mainTexture = _tmp.font.atlas;
yield return null;
RenderTexture.active = renderTex;
texture.ReadPixels(new Rect(0, 0, widthPoT, heightPoT), 0, 0, true);
texture.Apply(false, true);
RenderTexture.ReleaseTemporary(renderTex);
}
}