0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unity6 ECS 最小構成

Last updated at Posted at 2025-05-05

本記事は、Unity6 ECSで使用できるEntitiesにて、GPU インスタンシングを行うための最小コードを残しておくためのページです。

実コード

プロパティ、定義、実関数の3つのファイルに分けて管理している

BG.shader
Shader "Custom/BG"
{
    Properties
    {
        _BaseMap ("Albedo", 2D) = "white" {}
        _BaseColor ("Color", Color) = (1,1,1,1)
    }
    
    SubShader
    {
        Tags {
            "RenderType" = "Opaque"
            "RenderPipeline" = "UniversalPipeline"
            "UniversalMaterialType" = "Lit"
            "IgnoreProjector" = "True"
        }
        LOD 100

        Pass
        {
            Name "ForwardLit"
            Tags { "LightMode"="UniversalForward" }

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
			#pragma target 4.5

            #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"

			#include "BG_Input.hlsl"
			#include "BG_Function.hlsl"
            ENDHLSL
        }
    }
}
BG_Input.hlsl
#ifndef BG_INPUT
#define BG_INPUT

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);

CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;

float4 _BaseColor;
CBUFFER_END

#ifdef UNITY_DOTS_INSTANCING_ENABLED
UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)
	UNITY_DOTS_INSTANCED_PROP(float4, _BaseColor)
UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)

static float4 unity_DOTS_Sampled_BaseColor;

void SetupDOTSLitMaterialPropertyCaches()
{
	unity_DOTS_Sampled_BaseColor = UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(float4, _BaseColor);
}
#undef UNITY_SETUP_DOTS_MATERIAL_PROPERTY_CACHES
#define UNITY_SETUP_DOTS_MATERIAL_PROPERTY_CACHES() SetupDOTSLitMaterialPropertyCaches()
#define _BaseColor unity_DOTS_Sampled_BaseColor
#endif
#endif // BG_INPUT
BG_Function.hlsl
#ifndef BG_FUNCTION
#define BG_FUNCTION

struct v_in
{
    float4 vertex : POSITION;
    float2 uv : TEXCOORD0;    
    UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct v_out
{
    float2 uv : TEXCOORD0;
    float4 vertex : SV_POSITION;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    UNITY_VERTEX_OUTPUT_STEREO
};
v_out vert(v_in v)
{    
    v_out o = (v_out)0;
    
    UNITY_SETUP_INSTANCE_ID(v);
    UNITY_TRANSFER_INSTANCE_ID(v, o);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

    o.vertex = TransformObjectToHClip(v.vertex.xyz);
    o.uv = TRANSFORM_TEX(v.uv, _BaseMap);
    return o;
}
float4 frag(v_out input) : SV_Target
{
    UNITY_SETUP_INSTANCE_ID(input);
    UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
            
    float2 uv = input.uv;
    float4 out_color = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
    out_color.rgb *= _BaseColor.rgb;
    return out_color;
}
#endif // BG_FUNCTION

おまけ

BG_Input.hlslで記述している、Defineを行うことで処理速度が早くなるらしい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?