diff --git a/Assets/Materials.meta b/Assets/Materials.meta new file mode 100644 index 0000000..13cd315 --- /dev/null +++ b/Assets/Materials.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 5e8299824b0aba34eaabe4595b001197 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Materials/ComputeShaderResultMaterial.mat b/Assets/Materials/ComputeShaderResultMaterial.mat new file mode 100644 index 0000000..93809ba --- /dev/null +++ b/Assets/Materials/ComputeShaderResultMaterial.mat @@ -0,0 +1,135 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &-2777272910450527601 +MonoBehaviour: + m_ObjectHideFlags: 11 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} + m_Name: + m_EditorClassIdentifier: + version: 7 +--- !u!21 &2100000 +Material: + serializedVersion: 8 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: ComputeShaderResultMaterial + m_Shader: {fileID: 4800000, guid: 650dd9526735d5b46b79224bc6e94025, type: 3} + m_Parent: {fileID: 0} + m_ModifiedSerializedProperties: 0 + m_ValidKeywords: [] + m_InvalidKeywords: [] + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: + RenderType: Opaque + disabledShaderPasses: [] + m_LockedProperties: + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BaseMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _SpecGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_Lightmaps: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_LightmapsInd: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_ShadowMasks: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Ints: [] + m_Floats: + - _AlphaClip: 0 + - _AlphaToMask: 0 + - _Blend: 0 + - _BlendModePreserveSpecular: 1 + - _BlendOp: 0 + - _BumpScale: 1 + - _ClearCoatMask: 0 + - _ClearCoatSmoothness: 0 + - _Cull: 2 + - _Cutoff: 0.5 + - _DetailAlbedoMapScale: 1 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _DstBlendAlpha: 0 + - _EnvironmentReflections: 1 + - _GlossMapScale: 0 + - _Glossiness: 0 + - _GlossyReflections: 0 + - _Metallic: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.005 + - _QueueOffset: 0 + - _ReceiveShadows: 1 + - _SampleGI: 0 + - _Smoothness: 0.5 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _SrcBlendAlpha: 1 + - _Surface: 0 + - _WorkflowMode: 1 + - _ZWrite: 1 + m_Colors: + - _BaseColor: {r: 1, g: 1, b: 1, a: 1} + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _SpecColor: {r: 0.2, g: 0.2, b: 0.2, a: 1} + m_BuildTextureStacks: [] diff --git a/Assets/Materials/ComputeShaderResultMaterial.mat.meta b/Assets/Materials/ComputeShaderResultMaterial.mat.meta new file mode 100644 index 0000000..f128ae6 --- /dev/null +++ b/Assets/Materials/ComputeShaderResultMaterial.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d7655914d2a5f2b458b0fecf0f73713e +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5f02bbc..6982bf7 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -123,7 +123,7 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} ---- !u!1 &236875103 +--- !u!1 &702131709 GameObject: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -131,45 +131,23 @@ GameObject: m_PrefabAsset: {fileID: 0} serializedVersion: 6 m_Component: - - component: {fileID: 236875107} - - component: {fileID: 236875106} - - component: {fileID: 236875105} - - component: {fileID: 236875104} + - component: {fileID: 702131712} + - component: {fileID: 702131711} + - component: {fileID: 702131710} m_Layer: 0 - m_Name: Cube + m_Name: Quad m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 m_IsActive: 1 ---- !u!65 &236875104 -BoxCollider: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 236875103} - m_Material: {fileID: 0} - m_IncludeLayers: - serializedVersion: 2 - m_Bits: 0 - m_ExcludeLayers: - serializedVersion: 2 - m_Bits: 0 - m_LayerOverridePriority: 0 - m_IsTrigger: 0 - m_ProvidesContacts: 0 - m_Enabled: 1 - serializedVersion: 3 - m_Size: {x: 1, y: 1, z: 1} - m_Center: {x: 0, y: 0, z: 0} ---- !u!23 &236875105 +--- !u!23 &702131710 MeshRenderer: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 236875103} + m_GameObject: {fileID: 702131709} m_Enabled: 1 m_CastShadows: 1 m_ReceiveShadows: 1 @@ -183,7 +161,7 @@ MeshRenderer: m_RenderingLayerMask: 1 m_RendererPriority: 0 m_Materials: - - {fileID: 2100000, guid: 31321ba15b8f8eb4c954353edc038b1d, type: 2} + - {fileID: 2100000, guid: d7655914d2a5f2b458b0fecf0f73713e, type: 2} m_StaticBatchInfo: firstSubMesh: 0 subMeshCount: 0 @@ -205,24 +183,24 @@ MeshRenderer: m_SortingLayer: 0 m_SortingOrder: 0 m_AdditionalVertexStreams: {fileID: 0} ---- !u!33 &236875106 +--- !u!33 &702131711 MeshFilter: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 236875103} - m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} ---- !u!4 &236875107 + m_GameObject: {fileID: 702131709} + m_Mesh: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0} +--- !u!4 &702131712 Transform: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 236875103} + m_GameObject: {fileID: 702131709} serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 3.17, y: -2.7, z: 0} + m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -393,6 +371,7 @@ MonoBehaviour: _computeShader: {fileID: 7200000, guid: 3df3413c2fff3cc41a2eb5505b4c260c, type: 3} _inputTexture: {fileID: 2800000, guid: ac4ea20a37b3d4943b8889efe1821948, type: 3} _resultTexture: {fileID: 0} + _resultMaterial: {fileID: 2100000, guid: d7655914d2a5f2b458b0fecf0f73713e, type: 2} _width: 0 _height: 0 _sin: 0 @@ -407,6 +386,7 @@ GameObject: - component: {fileID: 963194228} - component: {fileID: 963194227} - component: {fileID: 963194226} + - component: {fileID: 963194229} m_Layer: 0 m_Name: Main Camera m_TagString: MainCamera @@ -482,12 +462,56 @@ Transform: m_GameObject: {fileID: 963194225} serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 1, z: -10} + m_LocalPosition: {x: 0, y: -0, z: -0.87} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &963194229 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 963194225} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a79441f348de89743a2939f4d699eac1, type: 3} + m_Name: + m_EditorClassIdentifier: + m_RenderShadows: 1 + m_RequiresDepthTextureOption: 2 + m_RequiresOpaqueTextureOption: 2 + m_CameraType: 0 + m_Cameras: [] + m_RendererIndex: -1 + m_VolumeLayerMask: + serializedVersion: 2 + m_Bits: 1 + m_VolumeTrigger: {fileID: 0} + m_VolumeFrameworkUpdateModeOption: 2 + m_RenderPostProcessing: 0 + m_Antialiasing: 0 + m_AntialiasingQuality: 2 + m_StopNaN: 0 + m_Dithering: 0 + m_ClearDepth: 1 + m_AllowXRRendering: 1 + m_AllowHDROutput: 1 + m_UseScreenCoordOverride: 0 + m_ScreenSizeOverride: {x: 0, y: 0, z: 0, w: 0} + m_ScreenCoordScaleBias: {x: 0, y: 0, z: 0, w: 0} + m_RequiresDepthTexture: 0 + m_RequiresColorTexture: 0 + m_Version: 2 + m_TaaSettings: + quality: 3 + frameInfluence: 0.1 + jitterScale: 1 + mipBias: 0 + varianceClampScale: 0.9 + contrastAdaptiveSharpening: 0 --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0 @@ -495,4 +519,4 @@ SceneRoots: - {fileID: 963194228} - {fileID: 705507995} - {fileID: 768508630} - - {fileID: 236875107} + - {fileID: 702131712} diff --git a/Assets/Scripts/ComputeController.cs b/Assets/Scripts/ComputeController.cs index 4856993..d798dc2 100644 --- a/Assets/Scripts/ComputeController.cs +++ b/Assets/Scripts/ComputeController.cs @@ -2,10 +2,11 @@ using UnityEngine; public class ComputeController : MonoBehaviour { - [Header("Conections")] + [Header("Connections")] [SerializeField] private ComputeShader _computeShader; [SerializeField] private Texture2D _inputTexture; [SerializeField] private RenderTexture _resultTexture; + [SerializeField] private Material _resultMaterial; [Header("State")] [SerializeField] private int _width; @@ -21,9 +22,12 @@ public class ComputeController : MonoBehaviour private static readonly int Result = Shader.PropertyToID("result"); private static readonly int SourceTexture = Shader.PropertyToID("source_texture"); private static readonly int SinScaled = Shader.PropertyToID("sin_scaled"); + private static readonly int MainTex = Shader.PropertyToID("_MainTex"); #endregion + #region Start + private void Start() { InitializeTextures(); @@ -35,12 +39,14 @@ public class ComputeController : MonoBehaviour private void InitializeTextures() { - _width = Mathf.RoundToInt(_inputTexture.width); - _height = Mathf.RoundToInt(_inputTexture.height); + _width = _inputTexture.width; + _height = _inputTexture.height; _resultTexture = new(_width, _height, 24); _resultTexture.name = "ResultTexture"; _resultTexture.enableRandomWrite = true; _resultTexture.Create(); + + _resultMaterial.SetTexture(MainTex, _resultTexture); } private void InitializeShader() @@ -50,17 +56,15 @@ public class ComputeController : MonoBehaviour _computeShader.SetTexture(_kernelHandle, Result, _resultTexture); } - private void RunComputeShader() - { - _computeShader.Dispatch(_kernelHandle, _width / NumThreadX, _height / NumThreadY, 1); - } + #endregion - private void OnGUI() + #region Update + + private void Update() { ShaderUpdate(); - GUI.DrawTexture(new Rect(0, 0, _width, _height), _inputTexture); - GUI.DrawTexture(new Rect(0, _height, _width, _height), _resultTexture); + _resultMaterial.mainTexture = _resultTexture; } private void ShaderUpdate() @@ -69,4 +73,11 @@ public class ComputeController : MonoBehaviour _computeShader.SetFloat(SinScaled, _sin); RunComputeShader(); } + + private void RunComputeShader() + { + _computeShader.Dispatch(_kernelHandle, _width / NumThreadX, _height / NumThreadY, 1); + } + + #endregion } \ No newline at end of file diff --git a/ProjectSettings/ShaderGraphSettings.asset b/ProjectSettings/ShaderGraphSettings.asset new file mode 100644 index 0000000..a11ed58 --- /dev/null +++ b/ProjectSettings/ShaderGraphSettings.asset @@ -0,0 +1,16 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &1 +MonoBehaviour: + m_ObjectHideFlags: 53 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: de02f9e1d18f588468e474319d09a723, type: 3} + m_Name: + m_EditorClassIdentifier: + customInterpolatorErrorThreshold: 32 + customInterpolatorWarningThreshold: 16