-
Notifications
You must be signed in to change notification settings - Fork 1
/
DaggerTutorialLevelModule.cs
151 lines (136 loc) · 5.69 KB
/
DaggerTutorialLevelModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThunderRoad;
using UnityEngine;
using UnityEngine.VFX;
using UnityEngine.AddressableAssets;
using ExtensionMethods;
using System.Reflection;
using UnityEngine.Experimental.Rendering.Universal;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace DaggerBending {
class DaggerTutorialLevelModule : LevelModule {
RenderFeatureEnabler feature;
public override IEnumerator OnLoadCoroutine(Level level) {
var targetObj = GameObject.Find("Targets");
foreach (var target in targetObj.GetComponentsInChildren<Transform>()) {
target.gameObject.AddComponent<TargetBehaviour>();
}
feature = GameObject.Find("RenderFeature").AddComponent<RenderFeatureEnabler>();
//var updown = GameObject.Find("SandSword").AddComponent<MoveUpAndDown>();
//updown.position = updown.transform.position;
EventManager.onPossess += PlatformPlayerAttach;
var pouchData = Catalog.GetData<ItemData>("DaggerPouch");
pouchData.SpawnAsync(null, new Vector3(-1.1868f, 0.2011f, 3.7483f));
pouchData.SpawnAsync(null, new Vector3(-0.9306f, 0.1192f, 3.750866f));
return base.OnLoadCoroutine(level);
}
public void PlatformPlayerAttach(Creature creature, EventTime time) {
var handler = GameObject.Find("Platform").AddComponent<PlatformShaderHandler>();
handler.targetTransform = creature.transform;
}
public override void OnUnload(Level level) {
base.OnUnload(level);
EventManager.onPossess -= PlatformPlayerAttach;
feature.RemoveRenderFeature();
}
}
public static class ExtensionMethods {
public static T GetOrAddComponent<T>(this GameObject obj) where T : Component {
return obj.GetComponent<T>() ?? obj.AddComponent<T>();
}
}
}
[ExecuteInEditMode]
public class MoveUpAndDown : MonoBehaviour {
public Vector3 position;
// Start is called before the first frame update
void Start() {
}
// Update is called once per frame
void Update() {
transform.position = position + new Vector3(0, Mathf.Sin(Time.time / 5) / 2, 0);
}
}
public class TargetBehaviour : MonoBehaviour {
public float respawnDuration = 4;
float lastHit;
bool active = true;
VisualEffect vfx;
// Start is called before the first frame update
void Start() {
vfx = GetComponent<VisualEffect>();
}
// Update is called once per frame
void Update() {
if (!active && Time.time - lastHit > respawnDuration) {
active = true;
vfx.SetBool("Hit", false);
}
}
void OnCollisionEnter(Collision collision) {
if (!active)
return;
active = false;
var averagePoint = collision.contacts.Aggregate(Vector3.zero, (a, point) => point.point + a) / collision.contacts.Count();
lastHit = Time.time;
Catalog.GetData<EffectData>("TargetBell").Spawn(transform).Play();
vfx.SetVector3("Hit Pos", averagePoint);
vfx.SetBool("Hit", true);
}
}
public class PlatformShaderHandler : MonoBehaviour {
public Transform targetTransform;
Material material;
// Start is called before the first frame update
void Start() {
material = GetComponent<Renderer>().sharedMaterial;
}
// Update is called once per frame
void Update() {
material.SetVector("Position", targetTransform.position);
}
}
public class RenderFeatureEnabler : MonoBehaviour {
// Start is called before the first frame update
public class DepthTester : RenderObjects { }
private DepthTester depthTester;
private ScriptableRendererData scriptableRendererData;
void Start() {
scriptableRendererData = ExtractScriptableRendererData();
//Create instance of our feature
depthTester = (DepthTester)ScriptableObject.CreateInstance(typeof(DepthTester));
depthTester.settings.Event = RenderPassEvent.AfterRenderingOpaques;
depthTester.settings.filterSettings.RenderQueueType = RenderQueueType.Transparent;
depthTester.settings.filterSettings.LayerMask = LayerMask.GetMask("Default");
depthTester.settings.overrideDepthState = true;
depthTester.settings.enableWrite = true;
depthTester.settings.depthCompareFunction = CompareFunction.LessEqual;
// Remove existing DepthTesters
var toRemove = scriptableRendererData.rendererFeatures.Where(feature => feature is DepthTester).ToList();
foreach (var feature in toRemove) {
scriptableRendererData.rendererFeatures.Remove(feature);
}
//Add the feature to the render pipeline
scriptableRendererData.rendererFeatures.Add(depthTester);
//Mark SRD as dirty so it gets updated.
scriptableRendererData.SetDirty();
}
public void RemoveRenderFeature() {
var toRemove = scriptableRendererData.rendererFeatures.Where(feature => feature is DepthTester).ToList();
foreach (var feature in toRemove) {
scriptableRendererData.rendererFeatures.Remove(feature);
scriptableRendererData.SetDirty();
}
}
private static ScriptableRendererData ExtractScriptableRendererData() {
var pipeline = ((UniversalRenderPipelineAsset)GraphicsSettings.renderPipelineAsset);
FieldInfo propertyInfo = pipeline.GetType().GetField("m_RendererDataList", BindingFlags.Instance | BindingFlags.NonPublic);
return ((ScriptableRendererData[])propertyInfo?.GetValue(pipeline))?[0];
}
}