Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MToon10マテリアルをForward+レンダラーでの複数ライトに対応 #2421

Conversation

n-miyanaga
Copy link
Contributor

@n-miyanaga n-miyanaga commented Sep 4, 2024

URPでMToon10マテリアルに対してForward+レンダラーを使用したときにメインライト1つしか適用されていなかったようですので、Forward+での複数ライトに対応する変更を行いました。

動作確認のためライトを300個設置してForward+レンダラーで描画したものが以下のスクリーンショットです。(各球体からキャラに向けてスポットライトを当てています。Unity 2022.3.14f1にて動作確認。)

image

変更内容

  • URPのMToonシェーダに_FORWARD_PLUSをmulti_compileとして定義しました
  • フラグメントシェーダでUSE_FORWARD_PLUSが定義されている場合にはLIGHT_LOOP_BEGINを使用してForward+レンダラーに対応したライティングのループ処理を行うようにしました

@ousttrue ousttrue added the URP label Sep 5, 2024
Copy link
Contributor

@Santarh Santarh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forward+ 対応ありがとうございます!!

1点だけ返答の方お願いできますでしょうか?

Comment on lines +53 to +55
InputData inputData = (InputData)0;
inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.pos);
inputData.positionWS = input.positionWS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputData が未使用変数に思えるのですが、どうでしょう?

おそらく RealtimeLights.hlslGetAdditionalLight() に渡す予定の変数だったのかと思いますが
vrmc_materials_mtoon_lighting_unity.hlslGetAdditionalLight() 内での呼び出しでは positionWS は既に渡されているので、不要そうには思えます

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご確認ありがとうございます。
この部分はまず最初にinputDataの定義なしで実装したのですが、シェーダのコンパイルエラーとなったのでinputDataを定義しました。

LIGHT_LOOP_BEGINマクロのドキュメントが見つからなかったので正しい使い方なのか良く分かっていないのですが、RealtimeLights.hlslLIGHT_LOOP_BEGINマクロの定義でForward+レンダリングの場合にinputData.normalizedScreenSpaceUVとinputData.positionWSを参照しているため、inputData変数の定義が必要なようです。

#if USE_FORWARD_PLUS
    #define LIGHT_LOOP_BEGIN(lightCount) { \
    uint lightIndex; \
    ClusterIterator _urp_internal_clusterIterator = ClusterInit(inputData.normalizedScreenSpaceUV, inputData.positionWS, 0); \
    [loop] while (ClusterNext(_urp_internal_clusterIterator, lightIndex)) { \
        lightIndex += URP_FP_DIRECTIONAL_LIGHTS_COUNT; \
        FORWARD_PLUS_SUBTRACTIVE_LIGHT_CHECK
    #define LIGHT_LOOP_END } }
#elif !_USE_WEBGL1_LIGHTS

またLIGHT_LOOP_BEGINでネット情報を探したところ、Cyanilux/URP_ShaderGraphCustomLightingではInputData inputDataを定義する実装をされていたので同じようにしたところシェーダのコンパイルエラーが解消されて複数ライトでのライティングができるようになったという経緯になります。
(参考: https://github.com/Cyanilux/URP_ShaderGraphCustomLighting/blob/main/CustomLighting.hlsl#L236-L253)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!

Unity の Graphics ライブラリを確認しましたが、たしかに使用されていますね……必要な定義そうです。
https://github.com/Unity-Technologies/Graphics/blob/fc267ad41df7e0cf39300c4c4d57dd1dc890c054/Packages/com.unity.render-pipelines.universal/ShaderLibrary/RealtimeLights.hlsl#L28-L34

@@ -112,6 +112,7 @@ Shader "VRM10/Universal Render Pipeline/MToon10"
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
#pragma multi_compile _ _FORWARD_PLUS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@Santarh
Copy link
Contributor

Santarh commented Sep 5, 2024

PullReq とは直接的には関係ない、元からの問題に気づいたメモ:

Outline パスのライティング処理が variant が無いなど不完全になっている?
要 Issue 化 #2423

@Santarh Santarh self-requested a review September 5, 2024 07:52
Copy link
Contributor

@Santarh Santarh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Santarh Santarh merged commit 216e4c5 into vrm-c:master Sep 5, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants