Sloppy material importing for maps

This commit is contained in:
Pyogenics
2025-03-31 20:48:35 +01:00
parent bbabbda16f
commit 2ce9b3aa75
3 changed files with 82 additions and 18 deletions

View File

@@ -32,21 +32,7 @@ from .A3DObjects import (
A3D_VERTEXTYPE_COLOR,
A3D_VERTEXTYPE_NORMAL2
)
def addImageTextureToMaterial(image, node_tree):
nodes = node_tree.nodes
links = node_tree.links
# Check if this material already has a texture on it
if len(nodes) > 2:
return
# Create nodes
principledBSDFNode = nodes[0]
textureNode = nodes.new(type="ShaderNodeTexImage")
links.new(textureNode.outputs["Color"], principledBSDFNode.inputs["Base Color"])
# Apply image
if image != None: textureNode.image = image
from .BlenderUtils import addImageTextureToMaterial
def mirrorUVY(uv):
x, y = uv

View File

@@ -23,10 +23,13 @@ SOFTWARE.
from json import load
import bpy
from bpy_extras.node_shader_utils import PrincipledBSDFWrapper
from bpy_extras.image_utils import load_image
import bmesh
from .A3D import A3D
from .A3DBlenderImporter import A3DBlenderImporter
from .BlenderUtils import addImageTextureToMaterial
class PropLibrary:
propCache = {}
@@ -64,7 +67,10 @@ class PropLibrary:
self.propCache[name] = ob
return self.propCache[name]
def getTexture(self, textureName):
im = load_image(textureName, self.directory)
return im
class BattleMapBlenderImporter:
# Allows subsequent map loads to be faster
@@ -77,9 +83,16 @@ class BattleMapBlenderImporter:
self.import_collision_geom = import_collision_geom
self.import_spawn_points = import_spawn_points
self.materials = {}
def importData(self):
print("Importing BattleMap data into blender")
# Process materials
for materialData in self.mapData.materials:
ma = self.createBlenderMaterial(materialData)
self.materials[materialData.ID] = ma
propObjects = []
if self.import_static_geom:
# Load props
@@ -145,7 +158,11 @@ class BattleMapBlenderImporter:
propOB.rotation_mode = "XYZ"
propOB.rotation_euler = propData.rotation
propOB.scale = propData.scale
# Material
ma = self.materials[propData.materialID]
propOB.data.materials[0] = ma
return propOB
def createBlenderCollisionTriangles(self, collisionTriangles):
@@ -235,4 +252,29 @@ class BattleMapBlenderImporter:
ob.rotation_mode = "XYZ"
ob.rotation_euler = spawnPointData.rotation
return ob
return ob
def createBlenderMaterial(self, materialData):
ma = bpy.data.materials.new(f"{materialData.ID}_{materialData.name}")
# Shader specific logic
if materialData.shader == "TankiOnline/SingleTextureShader":
# First check if we've already loaded the required prop library
if not "Remaster" in self.libraryCache:
# Load the proplib
libraryPath = f"{self.propLibrarySourcePath}/Remaster" # XXX: Get platform agnostic way of doing this
library = PropLibrary(libraryPath)
self.libraryCache["Remaster"] = library
# Try load texture
textureParameter = materialData.textureParameters[0]
library = self.libraryCache["Remaster"] #XXX: libraryName is optional
image = library.getTexture(f"{textureParameter.textureName}.webp")
# Apply texture
maWrapper = PrincipledBSDFWrapper(ma, is_readonly=False, use_nodes=True)
addImageTextureToMaterial(image, ma.node_tree)
elif materialData.shader == "TankiOnline/SpriteShader":
pass
return ma

View File

@@ -0,0 +1,36 @@
'''
Copyright (c) 2025 Pyogenics <https://github.com/Pyogenics>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
def addImageTextureToMaterial(image, node_tree):
nodes = node_tree.nodes
links = node_tree.links
# Check if this material already has a texture on it
if len(nodes) > 2:
return
# Create nodes
principledBSDFNode = nodes[0]
textureNode = nodes.new(type="ShaderNodeTexImage")
links.new(textureNode.outputs["Color"], principledBSDFNode.inputs["Base Color"])
# Apply image
if image != None: textureNode.image = image