Skip to content

Commit

Permalink
Added OcclusionCullingData for world.zen - TODO: Need to be tested if…
Browse files Browse the repository at this point in the history
… performance is better. (Drawcalls during runtime are ~80% less at least...)
  • Loading branch information
JaXt0r committed Jun 15, 2023
1 parent f04afb2 commit c2cd326
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
# Linux shared libraries
*.so filter=lfs diff=lfs merge=lfs -text
*.a filter=lfs diff=lfs merge=lfs -text

# Linux shared libraries
OcclusionCullingData.asset filter=lfs diff=lfs merge=lfs -text
73 changes: 73 additions & 0 deletions Assets/GothicVR/Editor/GVR.Editor.OcclusionCullingTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.IO;
using GVR.Caches;
using GVR.Creator;
using GVR.Phoenix.Interface;
using GVR.Util;
using PxCs.Interface;
using UnityEditor;
using UnityEngine;

namespace GothicVR.Editor
{
public class OcclusionCullingTool : EditorWindow
{
private const string _G1DIR = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Gothic";
private static IntPtr _vdfsPtr = IntPtr.Zero;
private static MeshCreator _meshCreator;


[MenuItem("GothicVR/Tools/Occlusion Culling", true)]
private static bool ValidateMyMenuItem()
{
// If game is in playmode, disable button.
return !EditorApplication.isPlaying;
}

[MenuItem("GothicVR/Tools/Occlusion Culling")]
public static void ShowWindow()
{
// Do not show Window when game is started.
if (Application.isPlaying)
return;

_meshCreator = SingletonBehaviour<MeshCreator>.GetOrCreate();
var assetCache = SingletonBehaviour<AssetCache>.GetOrCreate();

_meshCreator.EditorInject(assetCache);

var fullPath = Path.GetFullPath(Path.Join(_G1DIR, "Data"));
_vdfsPtr = VdfsBridge.LoadVdfsInDirectory(fullPath);

// Hint: When we load meshes/textures via MeshCreator.cs, there's hard coded PhoenixBridge.VdfPtr used.
// As we ensured our Window is only active when not playing, we can safely reuse it for now.
PhoenixBridge.VdfsPtr = _vdfsPtr;

var world = WorldBridge.LoadWorld(_vdfsPtr, "world.zen");

_meshCreator.Create(world);
}

void OnGUI()
{
// Do not show Window when game is started.
if (Application.isPlaying)
{
Close();
return;
}
}

void OnDestroy()
{
if (_vdfsPtr == IntPtr.Zero)
return;

PxVdf.pxVdfDestroy(_vdfsPtr);
_vdfsPtr = IntPtr.Zero;

// Hint: If window closes as the game is started, we must not! clear PhoenixBridge.VdfPtr as it would crash the game.
// Therefore just leave it as it is...
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/GothicVR/Scenes/SampleScene.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Assets/GothicVR/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ OcclusionCullingSettings:
smallestOccluder: 5
smallestHole: 0.25
backfaceThreshold: 100
m_SceneGUID: 00000000000000000000000000000000
m_OcclusionCullingData: {fileID: 0}
m_SceneGUID: 9fc0d4010bbf28b4594072e72b8655ab
m_OcclusionCullingData: {fileID: 36300000, guid: 763c3c8b9c2f75f4683437eab3195b46, type: 2}
--- !u!104 &2
RenderSettings:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 705507994}
m_IndirectSpecularColor: {r: 0.17276807, g: 0.21589197, b: 0.29782572, a: 1}
m_IndirectSpecularColor: {r: 0.17276844, g: 0.21589246, b: 0.2978263, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down
3 changes: 3 additions & 0 deletions Assets/GothicVR/Scenes/SampleScene/OcclusionCullingData.asset
Git LFS file not shown

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions Assets/GothicVR/Scripts/Creator/MeshCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;

namespace GVR.Creator
{
public class MeshCreator : SingletonBehaviour<MeshCreator>
{
private static AssetCache assetCache;
private AssetCache assetCache;

// DEBUG - we can change special mesh entries (trees, walls) based on flags later. But for now we can live with the nature cutout shader.
private const string DEFAULT_SHADER = "Universal Render Pipeline/Unlit";

void Start()
private void Start()
{
assetCache = SingletonBehaviour<AssetCache>.GetOrCreate();
}

/// <summary>
/// Inject singletons if we use this class from EditorMode.
/// </summary>
public void EditorInject(AssetCache assetCache)
{
this.assetCache = assetCache;
}

public GameObject Create(WorldData world, GameObject parent = null)
{
var meshObj = new GameObject("Mesh");
meshObj.isStatic = true;
meshObj.SetParent(parent);

foreach (var subMesh in world.subMeshes.Values)
{
var subMeshObj = new GameObject(string.Format("submesh-{0}", subMesh.material.name));
subMeshObj.isStatic = true;

var meshFilter = subMeshObj.AddComponent<MeshFilter>();
var meshRenderer = subMeshObj.AddComponent<MeshRenderer>();
var meshCollider = subMeshObj.AddComponent<MeshCollider>();

PrepareMeshRenderer(meshRenderer, subMesh);
PrepareMeshFilter(meshFilter, subMesh);
meshCollider.sharedMesh = meshFilter.mesh;

subMeshObj.transform.parent = meshObj.transform;
subMeshObj.SetParent(meshObj);
}

return meshObj;
Expand Down
4 changes: 0 additions & 4 deletions Assets/GothicVR/Scripts/Importer/PhoenixImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ private void LoadWorld(IntPtr vdfPtr)
{
var world = WorldBridge.LoadWorld(vdfPtr, "world.zen"); // world.zen -> G1, newworld.zen/oldworld.zen/addonworld.zen -> G2

var subMeshes = WorldBridge.CreateSubmeshesForUnity(world);
world.subMeshes = subMeshes;


PhoenixBridge.VdfsPtr = vdfPtr;
PhoenixBridge.World = world;

Expand Down
3 changes: 3 additions & 0 deletions Assets/GothicVR/Scripts/Phoenix/Interface/WorldBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public static WorldData LoadWorld(IntPtr vdfsPtr, string worldName)
waypointsDict = waypointsDict,
waypointEdges = waypointEdges
};

var subMeshes = WorldBridge.CreateSubmeshesForUnity(world);
world.subMeshes = subMeshes;

PxWorld.pxWorldDestroy(worldPtr);

Expand Down

0 comments on commit c2cd326

Please sign in to comment.