@ -5,11 +5,6 @@ using UnityEngine.Rendering;
public partial class RenderModule
public partial class RenderModule
{
{
private readonly UniQuake uq ;
private readonly UniQuake uq ;
private readonly List < BrushModel > brushModels = new List < BrushModel > ( ) ;
private readonly List < AliasModel > aliasModels = new List < AliasModel > ( ) ;
private readonly Dictionary < uint , Texture2D > textures = new Dictionary < uint , Texture2D > ( ) ;
private BrushModel worldModel ;
public RenderModule ( UniQuake uniQuake )
public RenderModule ( UniQuake uniQuake )
{
{
@ -20,33 +15,6 @@ public partial class RenderModule
globalZPos - = 2 5 6f ;
globalZPos - = 2 5 6f ;
}
}
public override void Destroy ( )
{
base . Destroy ( ) ;
foreach ( var brushModel in brushModels )
{
// brushModel.Dispose(); // TODO: reactivate when done testing in editor
}
brushModels . Clear ( ) ;
worldModel = null ;
foreach ( var aliasModel in aliasModels )
{
// aliasModel.Dispose(); // TODO: reactivate when done testing in editor
}
aliasModels . Clear ( ) ;
foreach ( var texture in textures . Values )
{
// Object.Destroy(texture); // TODO: reactivate when done testing in editor
}
textures . Clear ( ) ;
}
private float xPos = - 2 0 4 8f ;
private float xPos = - 2 0 4 8f ;
private float zPos = 0f ;
private float zPos = 0f ;
private static float globalZPos = 0f ;
private static float globalZPos = 0f ;
@ -66,7 +34,8 @@ public partial class RenderModule
string modelName = System . IO . Path . GetFileNameWithoutExtension ( name ) ;
string modelName = System . IO . Path . GetFileNameWithoutExtension ( name ) ;
AliasModel aliasModel = new AliasModel ( modelName , frameType ) ;
AliasModel aliasModel = new AliasModel ( modelName , frameType ) ;
aliasModel . ImportMeshData ( header , poseVertices , triangles , stVertices ) ;
aliasModel . ImportMeshData ( header , poseVertices , triangles , stVertices ) ;
aliasModels . Add ( aliasModel ) ;
uq . GameAssets . AddAliasModel ( aliasModel ) ;
var go = new GameObject ( modelName ) ;
var go = new GameObject ( modelName ) ;
go . transform . SetPositionAndRotation ( new Vector3 ( xPos , 0 , zPos ) , Quaternion . identity ) ;
go . transform . SetPositionAndRotation ( new Vector3 ( xPos , 0 , zPos ) , Quaternion . identity ) ;
@ -75,18 +44,18 @@ public partial class RenderModule
var material = new Material ( Shader . Find ( "Universal Render Pipeline/Simple Lit" ) ) ;
var material = new Material ( Shader . Find ( "Universal Render Pipeline/Simple Lit" ) ) ;
uint texNum = glTextures [ 0 ] [ 0 ] . texNum ;
uint texNum = glTextures [ 0 ] [ 0 ] . texNum ;
if ( texNum > 0 & & textures . ContainsKey ( texNum ) )
if ( uq . GameAssets . TryGetTexture ( texNum , out var texture ) )
{
{
material . mainTexture = textures [ texNum ] ;
material . mainTexture = texture ;
}
}
if ( fbTextures ! = null & & fbTextures [ 0 ] ! = null & & fbTextures [ 0 ] [ 0 ] ! = null )
if ( fbTextures ! = null & & fbTextures [ 0 ] ! = null & & fbTextures [ 0 ] [ 0 ] ! = null )
{
{
var fbTexNum = fbTextures [ 0 ] [ 0 ] . texNum ;
var fbTexNum = fbTextures [ 0 ] [ 0 ] . texNum ;
if ( fbTexNum > 0 & & textures . ContainsKey ( fbTexNum ) )
if ( uq . GameAssets . TryGetTexture ( fbTexNum , out var fbTexture ) )
{
{
material . EnableKeyword ( "_EMISSION" ) ;
material . EnableKeyword ( "_EMISSION" ) ;
material . SetTexture ( "_EmissionMap" , textures [ fbTexNum ] ) ;
material . SetTexture ( "_EmissionMap" , fbTexture ) ;
material . SetColor ( "_EmissionColor" , Color . white ) ;
material . SetColor ( "_EmissionColor" , Color . white ) ;
}
}
}
}
@ -119,7 +88,7 @@ public partial class RenderModule
}
}
xPos + = 1 2 8f ;
xPos + = 1 2 8f ;
return aliasModels . Count ;
return 1 ;
}
}
private int UploadBrushModel ( QModel model )
private int UploadBrushModel ( QModel model )
@ -129,6 +98,20 @@ public partial class RenderModule
var brushModel = new BrushModel ( model . name ) ;
var brushModel = new BrushModel ( model . name ) ;
brushModel . ImportMeshData ( model ) ;
brushModel . ImportMeshData ( model ) ;
uq . GameAssets . AddBrushModel ( brushModel ) ;
return 1 ;
}
private int UploadWorldModel ( QModel model )
{
Debug . Log ( $"World model '{model.name}' with {model.numVertices} vertices, {model.numEdges} edges, {model.numSurfaces} surfaces" ) ;
var brushModel = new BrushModel ( model . name ) ;
brushModel . ImportMeshData ( model ) ;
uq . GameAssets . SetWorldModel ( brushModel ) ;
// DEBUG
// DEBUG
var worldGO = new GameObject ( model . name ) ;
var worldGO = new GameObject ( model . name ) ;
for ( int i = 0 ; i < brushModel . SubModelCount ; + + i )
for ( int i = 0 ; i < brushModel . SubModelCount ; + + i )
@ -147,9 +130,9 @@ public partial class RenderModule
var material = new Material ( Shader . Find ( "Universal Render Pipeline/Simple Lit" ) ) ;
var material = new Material ( Shader . Find ( "Universal Render Pipeline/Simple Lit" ) ) ;
uint texNum = surfaceMesh . Texture . TextureNum ;
uint texNum = surfaceMesh . Texture . TextureNum ;
if ( texNum > 0 & & textures . ContainsKey ( texNum ) )
if ( uq . GameAssets . TryGetTexture ( texNum , out var texture ) )
{
{
material . mainTexture = textures [ texNum ] ;
material . mainTexture = texture ;
}
}
var mr = meshGO . AddComponent < MeshRenderer > ( ) ;
var mr = meshGO . AddComponent < MeshRenderer > ( ) ;
@ -161,12 +144,9 @@ public partial class RenderModule
}
}
}
}
brushModels . Add ( brushModel ) ;
return brushModels . Count ;
return 1 ;
}
}
private uint nextTexNum = 0 x10001 ;
private bool UploadTexture ( QGLTexture texture , byte [ ] data , ref uint texNum )
private bool UploadTexture ( QGLTexture texture , byte [ ] data , ref uint texNum )
{
{
Debug . Log ( $"Texture '{texture.name}' with dimensions {texture.width}x{texture.height}, data size = {data.Length} bytes" ) ;
Debug . Log ( $"Texture '{texture.name}' with dimensions {texture.width}x{texture.height}, data size = {data.Length} bytes" ) ;
@ -174,21 +154,12 @@ public partial class RenderModule
if ( texture . width = = 0 | | texture . height = = 0 )
if ( texture . width = = 0 | | texture . height = = 0 )
return false ;
return false ;
if ( texNum = = 0 )
{
// Assign a new texture number
while ( textures . ContainsKey ( nextTexNum ) )
+ + nextTexNum ;
texNum = nextTexNum + + ;
}
var tex = new Texture2D ( ( int ) texture . width , ( int ) texture . height , TextureFormat . RGBA32 , texture . flags . HasFlag ( QTexPrefs . Mipmap ) ) ;
var tex = new Texture2D ( ( int ) texture . width , ( int ) texture . height , TextureFormat . RGBA32 , texture . flags . HasFlag ( QTexPrefs . Mipmap ) ) ;
tex . name = texture . name ;
tex . name = texture . name ;
tex . SetPixelData ( data , 0 ) ;
tex . SetPixelData ( data , 0 ) ;
tex . Apply ( ) ;
tex . Apply ( ) ;
textures [ texNum ] = tex ;
texNum = uq . GameAssets . SetTexture ( texNum , tex ) ;
return true ;
return true ;
}
}