View Issue Details

IDProjectCategoryView StatusLast Update
0002297The Dark ModGraphicspublic02.12.2026 22:34
Reportertels Assigned To 
PrioritynormalSeveritynormalReproducibilityalways
Status newResolutionopen 
Platformx86_64OSLinuxOS VersionKubuntu 9.04
Product VersionTDM 1.03 
Summary0002297: Sidedness of material sticks to skin
DescriptionIt seems a two-sided texture on a model prevents a skin to be one-sided, and vice-versa.

Not sure if this is even fixable in our code. Discussion here:

https://forums.thedarkmod.com/index.php?/topic/11281-improve-the-lod-system/page/2/#comment-222226
TagsNo tags attached.

Relationships

related to 0002678 closedSteveL nodraw skinned surfaces are still active for collision detection 
related to 0004802 assignednbohr1more Nodraw surfaces in models remain invisible when given a visible texture via skin. 

Activities

tels

tels

22.08.2010 11:31

reporter   ~0003175

It might be fixable in code with the new ModelGenerator.

* If you apply a one-sided skin to a formerly-two-sided skinned model, remove every second surface.
* If you apply a two-sided skin to a formerly-one-sided skinned model, add the back surfaces again.

That needs to happen upon setting the skin, although in the case the model is loaded and the entity skin is set right away, it would be better if f.i. the backsided polygons weren't even created only to be removed for a one-sided skin again.
tels

tels

26.10.2010 16:01

reporter   ~0003274

From reading the code comments and the code, the same happens with shadows. Surfaces that are set to noshadow in a skin would still have the shadow, as the shadow hull is not rebuild.
VanishedOne

VanishedOne

12.10.2015 16:24

reporter   ~0007861

So that's the reason for http://bugs.thedarkmod.com/view.php?id=4200#c7718
VanishedOne

VanishedOne

26.03.2016 21:34

reporter   ~0008057

N.B. Whenever this gets fixed, somebody will have to make sure existing skins are also fixed where necessary. I was wondering why atdm:moveable_loot_gold_sugarcup and atdm:moveable_loot_gold_creamer have invisible insides in DR but not in TDM, and the answer seems to be that several skins used by models/darkmod/kitchen/sugar_silver.ase and models/darkmod/kitchen/creamer_silver.ase replace a two-sided material with a one-sided one.
nbohr1more

nbohr1more

02.12.2026 22:34

developer   ~0017405

The sided-ness is immutable due to the surface initialization design.
Here is a patch that generates cached variants of all potential skin configurations so the engine
can swap between them dynamically. Too much VRAM for all the variants?
Currently only fixed for static models.
I need a test map to validate...
nbohr1more

nbohr1more

02.12.2026 22:34

developer   ~0017406

tdm_skin_backside_cache.patch (8,030 bytes)   
--- a/renderer/frontend/RenderEntity.cpp
+++ b/renderer/frontend/RenderEntity.cpp
@@ -31,6 +31,10 @@
 	dynamicModel			= NULL;
 	dynamicModelFrameCount	= 0;
 	cachedDynamicModel		= NULL;
+	cachedSkinSource		= NULL;
+	cachedSkinSourceVersion	= 0;
+	cachedSkin			= NULL;
+	cachedSkinShader		= NULL;
 	referenceBounds			= bounds_zero;
 	globalReferenceBounds	= bounds_zero; //anon
 	viewCount = 0;
--- a/renderer/frontend/tr_light.cpp
+++ b/renderer/frontend/tr_light.cpp
@@ -1178,8 +1178,35 @@
 	}
 
 	else if ( model->IsDynamicModel() == DM_STATIC ) {
-		def->dynamicModel = NULL;
 		def->dynamicModelFrameCount = 0;
+
+		// Static models normally use their shared finalized geometry directly.
+		// A skin can, however, change ShouldCreateBackSides(), which determines
+		// whether FinishSurfaces must generate mirrored geometry. Keep a private
+		// per-entity cached variant in that case; never modify the shared model.
+		if ( def->parms.customSkin && !model->IsStaticWorldModel() ) {
+			idRenderModelStatic *staticModel = dynamic_cast<idRenderModelStatic *>(model);
+			if ( staticModel ) {
+				bool cacheMatches = def->cachedSkinSource == model
+					&& def->cachedSkinSourceVersion == model->GetLoadVersion()
+					&& def->cachedSkin == def->parms.customSkin
+					&& def->cachedSkinShader == def->parms.customShader;
+
+				if ( !cacheMatches ) {
+					delete def->cachedDynamicModel;
+					def->cachedDynamicModel = staticModel->CreateSkinVariant( def->parms.customSkin, def->parms.customShader );
+					def->cachedSkinSource = model;
+					def->cachedSkinSourceVersion = model->GetLoadVersion();
+					def->cachedSkin = def->parms.customSkin;
+					def->cachedSkinShader = def->parms.customShader;
+				}
+
+				def->dynamicModel = def->cachedDynamicModel;
+				return def->dynamicModel ? def->dynamicModel : model;
+			}
+		}
+
+		def->dynamicModel = NULL;
 		return model;
 	}
 
--- a/renderer/frontend/tr_lightrun.cpp
+++ b/renderer/frontend/tr_lightrun.cpp
@@ -906,11 +906,17 @@
 	// clear the dynamic model if present
 	if ( def->dynamicModel ) {
 		def->dynamicModel = NULL;
+	}
 
-		if ( !keepCachedDynamicModel ) {
-			delete def->cachedDynamicModel;
-			def->cachedDynamicModel = NULL;
-		}
+	// A cached skin variant can remain allocated even when the current skin
+	// no longer needs one, so release the cache independently of dynamicModel.
+	if ( !keepCachedDynamicModel ) {
+		delete def->cachedDynamicModel;
+		def->cachedDynamicModel = NULL;
+		def->cachedSkinSource = NULL;
+		def->cachedSkinSourceVersion = 0;
+		def->cachedSkin = NULL;
+		def->cachedSkinShader = NULL;
 	}
 
 	if ( !keepDecals ) {
--- a/renderer/resources/Model.cpp
+++ b/renderer/resources/Model.cpp
@@ -43,6 +43,7 @@
 	lastArchivedFrame = 0;
 	overlaysAdded = 0;
 	shadowHull = NULL;
+	numBaseSurfaces = 0;
 	isStaticWorldModel = false;
 	defaulted = false;
 	purged = false;
@@ -609,6 +610,10 @@
 ================
 */
 void idRenderModelStatic::FinishSurfaces() {
+	FinishSurfacesForSkin( nullptr, nullptr );
+}
+
+void idRenderModelStatic::FinishSurfacesForSkin( const idDeclSkin *skin, const idMaterial *customShader ) {
 	TRACE_CPU_SCOPE("FinishSurfaces");
 
 	int			i;
@@ -620,9 +625,13 @@
 	bounds.Zero();
 
 	if ( surfaces.Num() == 0 ) {
+		numBaseSurfaces = 0;
 		return;
 	}
 
+	// Remember the source surfaces before FinishSurfaces adds generated back sides.
+	numBaseSurfaces = surfaces.Num();
+
 	// renderBump doesn't care about most of this
 	if ( fastLoad ) {
 		bounds.Zero();
@@ -666,7 +675,8 @@
 	for ( i = 0 ; i < numOriginalSurfaces ; i++ ) {
 		const modelSurface_t	*surf = &surfaces[i];
 
-		if ( surf->material->ShouldCreateBackSides() ) {
+		const idMaterial *finishMaterial = R_RemapShaderBySkin( surf->material, skin, customShader );
+		if ( finishMaterial && finishMaterial->ShouldCreateBackSides() ) {
 			srfTriangles_t *newTri;
 
 			newTri = R_CopyStaticTriSurf( surf->geometry );
@@ -781,6 +791,60 @@
 
 /*
 =================
+idRenderModelStatic::NeedsSkinVariant
+
+A separate finalized model is needed only when a skin changes whether the
+material requires mirrored back-side geometry.
+=================
+*/
+bool idRenderModelStatic::NeedsSkinVariant( const idDeclSkin *skin, const idMaterial *customShader ) const {
+	if ( !skin || numBaseSurfaces <= 0 || fastLoad ) {
+		return false;
+	}
+
+	for ( int i = 0; i < numBaseSurfaces; i++ ) {
+		const idMaterial *original = surfaces[i].material;
+		const idMaterial *mapped = R_RemapShaderBySkin( original, skin, customShader );
+		if ( mapped && original->ShouldCreateBackSides() != mapped->ShouldCreateBackSides() ) {
+			return true;
+		}
+	}
+	return false;
+}
+
+/*
+=================
+idRenderModelStatic::CreateSkinVariant
+
+Copy only source surfaces, then finalize them using the remapped material's
+back-side requirement. The copied surfaces retain their original materials,
+so normal renderer skin remapping continues to work on every draw path.
+=================
+*/
+idRenderModelStatic *idRenderModelStatic::CreateSkinVariant( const idDeclSkin *skin, const idMaterial *customShader ) const {
+	if ( !NeedsSkinVariant( skin, customShader ) ) {
+		return nullptr;
+	}
+
+	idRenderModelStatic *variant = new idRenderModelStatic;
+	variant->InitEmpty( name.c_str() );
+	variant->isStaticWorldModel = isStaticWorldModel;
+	variant->defaulted = defaulted;
+	variant->fastLoad = fastLoad;
+	variant->timeStamp = timeStamp;
+
+	for ( int i = 0; i < numBaseSurfaces; i++ ) {
+		modelSurface_t surf = surfaces[i];
+		surf.geometry = surf.geometry ? R_CopyStaticTriSurf( surf.geometry ) : nullptr;
+		variant->AddSurface( surf );
+	}
+
+	variant->FinishSurfacesForSkin( skin, customShader );
+	return variant;
+}
+
+/*
+=================
 idRenderModelStatic::ConvertASEToModelSurfaces
 =================
 */
@@ -2393,6 +2457,7 @@
 		}
 	}
 	surfaces.ClearFree();
+	numBaseSurfaces = 0;
 
 	purged = true;
 }
--- a/renderer/resources/Model_local.h
+++ b/renderer/resources/Model_local.h
@@ -16,6 +16,8 @@
 #ifndef __MODEL_LOCAL_H__
 #define __MODEL_LOCAL_H__
 
+class idDeclSkin;
+
 /*
 ===============================================================================
 
@@ -44,6 +46,8 @@
 	virtual void				InitEmpty( const char *name ) override;
 	virtual void				AddSurface( modelSurface_t surface ) override;
 	virtual void				FinishSurfaces() override;
+	bool					NeedsSkinVariant( const idDeclSkin *skin, const idMaterial *customShader ) const;
+	idRenderModelStatic *	CreateSkinVariant( const idDeclSkin *skin, const idMaterial *customShader ) const;
 	virtual void				FreeVertexCache() override;
 	virtual const char *		Name() const override;
 	virtual void				Print() const override;
@@ -78,6 +82,7 @@
 	virtual const idMaterial *	GetSampleMaterial( const struct renderEntity_s *ent, const samplePointOnModel_t &sample ) const override;
 
 	void						MakeDefaultModel();
+	void						FinishSurfacesForSkin( const idDeclSkin *skin, const idMaterial *customShader );
 	
 	bool						LoadOBJ( const char *fileName );
 	bool						LoadASE( const char *fileName );
@@ -99,6 +104,7 @@
 
 public:
 	idList<modelSurface_t>		surfaces;
+	int						numBaseSurfaces;
 	idBounds					bounds;
 	int							overlaysAdded;
 
--- a/renderer/tr_local.h
+++ b/renderer/tr_local.h
@@ -21,6 +21,8 @@
 #include "renderer/resources/Image.h"
 
 class idRenderWorldLocal;
+class idDeclSkin;
+class idMaterial;
 
 const int FALLOFF_TEXTURE_SIZE =	64;
 
@@ -348,6 +350,11 @@
 	int						dynamicModelFrameCount;		// continuously animating dynamic models will recreate
 	// dynamicModel if this doesn't == tr.viewCount
 	idRenderModel 			*cachedDynamicModel;
+	// Per-entity cache key for a skin-dependent static model variant.
+	const idRenderModel 	*cachedSkinSource;
+	int					cachedSkinSourceVersion;
+	const idDeclSkin 		*cachedSkin;
+	const idMaterial 		*cachedSkinShader;
 
 	idBounds				referenceBounds;			// the local bounds used to place entityRefs, either from parms or a model
 	// axis aligned bounding box in world space, derived from refernceBounds and
tdm_skin_backside_cache.patch (8,030 bytes)   

Issue History

Date Modified Username Field Change
06.07.2010 05:36 tels New Issue
22.08.2010 11:31 tels Note Added: 0003175
26.10.2010 16:01 tels Note Added: 0003274
15.03.2011 18:06 tels Relationship added related to 0002678
12.10.2015 16:24 VanishedOne Note Added: 0007861
26.03.2016 21:34 VanishedOne Note Added: 0008057
28.03.2018 09:34 user81 Relationship added related to 0003771
27.05.2018 16:45 nbohr1more Relationship added related to 0004802
09.07.2022 22:42 Dragofer Relationship deleted related to 0003771
08.08.2022 01:14 nbohr1more Description Updated
02.12.2026 22:34 nbohr1more Note Added: 0017405
02.12.2026 22:34 nbohr1more Note Added: 0017406
02.12.2026 22:34 nbohr1more File Added: tdm_skin_backside_cache.patch