From 92262aee2b7f09fe3e3c6935029edf740b6fb95d Mon Sep 17 00:00:00 2001 From: fallenoak Date: Fri, 5 Jan 2024 12:04:35 -0600 Subject: [PATCH] feat(model): handle depth test and depth write material flags --- package-lock.json | 8 ++++---- package.json | 2 +- src/lib/model/ModelManager.ts | 4 +--- src/lib/model/ModelMaterial.ts | 25 +++++++++++++------------ 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index b58fae0..6df01d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.13.1", "license": "MIT", "dependencies": { - "@wowserhq/format": "^0.10.2" + "@wowserhq/format": "^0.11.0" }, "devDependencies": { "@commitlint/config-conventional": "^18.4.3", @@ -2347,9 +2347,9 @@ } }, "node_modules/@wowserhq/format": { - "version": "0.10.2", - "resolved": "https://registry.npmjs.org/@wowserhq/format/-/format-0.10.2.tgz", - "integrity": "sha512-KZ5ZIn7cQMZrRT7vpaqJFgTtNvC/gBuU5d+QlvulkB9wVpmiaFeM17VEs9Wz1HssiR6HdatJQmlTGn5mtq5v2g==", + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/@wowserhq/format/-/format-0.11.0.tgz", + "integrity": "sha512-kZRztfgwhoIyp8ZITIYBZer3MQTW1cwg4spKgqOI+068TrZSu9Wte8RGCxnYvfycGVYyHwT687BocDiYDJO03w==", "dependencies": { "@wowserhq/io": "^2.0.2", "gl-matrix": "^3.4.3" diff --git a/package.json b/package.json index d7e5676..d5e1cd2 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "three.js" ], "dependencies": { - "@wowserhq/format": "^0.10.2" + "@wowserhq/format": "^0.11.0" }, "peerDependencies": { "three": "^0.160.0" diff --git a/src/lib/model/ModelManager.ts b/src/lib/model/ModelManager.ts index 4df982b..c793671 100644 --- a/src/lib/model/ModelManager.ts +++ b/src/lib/model/ModelManager.ts @@ -157,8 +157,6 @@ class ModelManager { const textures = await Promise.all( m2Textures.map((m2Texture) => this.#createTexture(m2Texture)), ); - const side = - batch.material.flags & M2_MATERIAL_FLAG.FLAG_TWO_SIDED ? THREE.DoubleSide : THREE.FrontSide; return new ModelMaterial( vertexShader, @@ -166,7 +164,7 @@ class ModelManager { textures, batch.material.blend, M2_MATERIAL_PASS.PASS_0, - side, + batch.material.flags, ); } diff --git a/src/lib/model/ModelMaterial.ts b/src/lib/model/ModelMaterial.ts index 65ea5a4..31601c7 100644 --- a/src/lib/model/ModelMaterial.ts +++ b/src/lib/model/ModelMaterial.ts @@ -1,11 +1,11 @@ import * as THREE from 'three'; -import { M2_MATERIAL_BLEND } from '@wowserhq/format'; +import { M2_MATERIAL_BLEND, M2_MATERIAL_FLAG } from '@wowserhq/format'; import { M2_MATERIAL_BLEND_TO_THREE_BLEND, M2_MATERIAL_PASS } from './const.js'; import { THREE_BLEND_STATE } from '../blend.js'; const DEFAULT_BLEND: M2_MATERIAL_BLEND = M2_MATERIAL_BLEND.BLEND_OPAQUE; const DEFAULT_PASS: M2_MATERIAL_PASS = M2_MATERIAL_PASS.PASS_0; -const DEFAULT_SIDE: THREE.Side = THREE.FrontSide; +const DEFAULT_FLAGS: number = 0x0; const DEFAULT_ALPHA: number = 1.0; const getThreeBlendState = (blend: M2_MATERIAL_BLEND, pass: M2_MATERIAL_PASS) => @@ -24,28 +24,22 @@ class ModelMaterial extends THREE.RawShaderMaterial { textures: THREE.Texture[], blend = DEFAULT_BLEND, pass = DEFAULT_PASS, - side = DEFAULT_SIDE, + flags = DEFAULT_FLAGS, ) { super(); this.#blend = blend; this.#pass = pass; + this.side = flags & M2_MATERIAL_FLAG.FLAG_TWO_SIDED ? THREE.DoubleSide : THREE.FrontSide; + this.depthTest = !(flags & M2_MATERIAL_FLAG.FLAG_DISABLE_DEPTH_TEST); + this.depthWrite = !(flags & M2_MATERIAL_FLAG.FLAG_DISABLE_DEPTH_WRITE); this.alpha = DEFAULT_ALPHA; - this.uniforms = { - textures: { value: textures }, - alphaRef: { value: this.#alphaRef }, - fogColor: { value: new THREE.Color(0.25, 0.5, 0.8) }, - fogParams: { value: new THREE.Vector3(0.0, 1066.0, 1.0) }, - }; - this.glslVersion = THREE.GLSL3; this.vertexShader = vertexShader; this.fragmentShader = fragmentShader; - this.side = side; - const threeBlendState = getThreeBlendState(this.#blend, this.#pass); const { blending, blendSrc, blendSrcAlpha, blendDst, blendDstAlpha } = threeBlendState; @@ -54,6 +48,13 @@ class ModelMaterial extends THREE.RawShaderMaterial { this.blendSrcAlpha = blendSrcAlpha; this.blendDst = blendDst; this.blendDstAlpha = blendDstAlpha; + + this.uniforms = { + textures: { value: textures }, + alphaRef: { value: this.#alphaRef }, + fogColor: { value: new THREE.Color(0.25, 0.5, 0.8) }, + fogParams: { value: new THREE.Vector3(0.0, 1066.0, 1.0) }, + }; } get alphaRef() {