Created
January 31, 2023 17:59
-
-
Save rngtm/e3a269ae48f24ce8ec54cfeee1c09e9f to your computer and use it in GitHub Desktop.
CommandBufferで全画面にメッシュを表示するやつ (URP 10.8.1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Rendering.Universal; | |
public class DrawMeshFeature : ScriptableRendererFeature | |
{ | |
[SerializeField] private Material materialAsset; | |
[System.NonSerialized] private Mesh _mesh; | |
DrawMeshPass drawMeshPass; | |
/// <inheritdoc/> | |
public override void Create() | |
{ | |
drawMeshPass = new DrawMeshPass(); | |
drawMeshPass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques; | |
} | |
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) | |
{ | |
if (_mesh == null) | |
{ | |
Debug.Log("Create Mesh"); | |
_mesh = CreateMesh(); | |
} | |
drawMeshPass.Setup(_mesh, materialAsset); | |
renderer.EnqueuePass(drawMeshPass); | |
} | |
// 正規化デバイス座標系で画面の4隅に張り付くメッシュを作る | |
Mesh CreateMesh () { | |
var mesh = new Mesh (); | |
mesh.name = "TestMesh"; | |
// NOTE: z = 0 だと表示されなくなるので、大きい値を入れておく | |
float z = 0.5f; | |
var uv = new List<Vector2> | |
{ | |
new Vector2(1, 1), | |
new Vector2(0, 1), | |
new Vector2(1, 0), | |
new Vector2(0, 0), | |
}; | |
var vertices = new List<Vector3> { | |
new Vector3 (1f, 1f, z), | |
new Vector3 (-1f, 1f, z), | |
new Vector3 (1f, -1f, z), | |
new Vector3 (-1f, -1f, z), | |
}; | |
var triangles = new List<int> { | |
1, 0, 2, | |
1, 2, 3 | |
}; | |
mesh.SetVertices (vertices); | |
mesh.SetUVs(0, uv); | |
mesh.SetTriangles (triangles, 0); | |
return mesh; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Rendering; | |
using UnityEngine.Rendering.Universal; | |
public class DrawMeshPass : ScriptableRenderPass | |
{ | |
private Mesh _mesh; | |
private Material _material; | |
public void Setup(Mesh mesh, Material material) | |
{ | |
_mesh = mesh; | |
_material = material; | |
} | |
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) | |
{ | |
var cmd = CommandBufferPool.Get("DrawMeshPass"); | |
if (!_mesh) return; | |
cmd.DrawMesh(_mesh, Matrix4x4.identity, _material); | |
context.ExecuteCommandBuffer(cmd); | |
cmd.Clear(); | |
CommandBufferPool.Release(cmd); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Unlit/ScreenSpaceUnlit" | |
{ | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" } | |
LOD 100 | |
Cull Off // 裏表を無視 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
// 頂点には、正規化デバイス座標系の物が入っているので、ここでは特に座標変換を行わない | |
o.vertex = float4(v.vertex.xyz, 1); | |
o.uv = v.uv; | |
return o; | |
} | |
half4 frag (v2f i) : SV_Target | |
{ | |
return half4(i.uv, 0, 1); | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment