@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
104 lines (103 loc) • 4.13 kB
JavaScript
"use strict";
import COMMON_GET_UV from "../glsl/common/getUV.glsl";
import COMMON_PACK_POSITION from "../glsl/common/packPosition.glsl";
import COMMON_UNPACK_POSITION from "../glsl/common/unpackPosition.glsl";
import THROUGH_VERT from "../glsl/through.vert.glsl";
import CORE_THROUGH_FRAG from "../glsl/through.frag.glsl";
import CORE_CONTRAINTS_FRAG from "../glsl/constraints.frag.glsl";
import CORE_MOUSE_FRAG from "../glsl/mouse.frag.glsl";
import CORE_NORMALS_FRAG from "../glsl/normals.frag.glsl";
const LINE_BREAK = "\n";
const ADD_COMMON_LINE = "// *** ADD COMMON ***";
function _addCommon(core, commonFunctions) {
const common = commonFunctions.join(LINE_BREAK);
return core.replace(ADD_COMMON_LINE, common);
}
const THROUGH_FRAG = _addCommon(CORE_THROUGH_FRAG, [COMMON_UNPACK_POSITION]);
const CONTRAINTS_FRAG = _addCommon(CORE_CONTRAINTS_FRAG, [COMMON_GET_UV, COMMON_PACK_POSITION, COMMON_UNPACK_POSITION]);
const MOUSE_FRAG = _addCommon(CORE_MOUSE_FRAG, [COMMON_GET_UV, COMMON_PACK_POSITION, COMMON_UNPACK_POSITION]);
const NORMALS_FRAG = _addCommon(CORE_NORMALS_FRAG, [COMMON_GET_UV, COMMON_PACK_POSITION, COMMON_UNPACK_POSITION]);
import { RawShaderMaterial, Vector2, Vector3 } from "three";
function createRawMaterial() {
return new RawShaderMaterial({
uniforms: {
order: { value: null },
tSize: { value: new Vector2() },
texture: { value: null }
},
vertexShader: THROUGH_VERT,
fragmentShader: THROUGH_FRAG,
fog: false,
lights: false,
depthWrite: false,
depthTest: false
});
}
export class ClothMaterialController {
constructor(mainController) {
this.mainController = mainController;
// copyToRenderTarget
this.copyShader = createRawMaterial();
// forward-integration
this.integrateShader = createRawMaterial();
// mouse displacement
this.mouseShader = createRawMaterial();
// vertices relaxation
this.constraintsShader = createRawMaterial();
// calculate normals
this.normalsShader = createRawMaterial();
const integrationFragmentShader = this.mainController.integrationFragmentShader();
if (!integrationFragmentShader) {
throw "no integrationFragmentShader";
}
this.integrateShader.fragmentShader = _addCommon(integrationFragmentShader, [COMMON_UNPACK_POSITION]);
this.integrateShader.uniforms = {
timeDelta: this.mainController.scene.timeController.timeDeltaUniform(),
time: this.mainController.scene.timeController.timeUniform(),
viscosity: { value: 0.1 },
spring: { value: 1 },
tSize: { value: new Vector2() },
order: { value: -1 },
tOriginal: { value: null },
tPrevious0: { value: null },
tPrevious1: { value: null },
tPosition0: { value: null },
tPosition1: { value: null },
tViscositySpring: { value: null }
};
this.mainController.addMaterialUniforms(this.integrateShader);
this.mouseShader.fragmentShader = MOUSE_FRAG;
this.mouseShader.uniforms = {
time: this.mainController.scene.timeController.timeUniform(),
vertex: { value: -1 },
coordinates: { value: new Vector3() },
order: { value: -1 },
tSize: { value: new Vector2() },
tOriginal: { value: null },
tPosition0: { value: null },
tPosition1: { value: null }
};
this.constraintsShader.fragmentShader = CONTRAINTS_FRAG;
this.constraintsShader.uniforms = {
time: this.mainController.scene.timeController.timeUniform(),
tSize: { value: new Vector2() },
order: { value: -1 },
constraintInfluence: { value: 0 },
tPosition0: { value: null },
tPosition1: { value: null },
tAdjacentsA: { value: null },
tAdjacentsB: { value: null },
tDistancesA: { value: null },
tDistancesB: { value: null },
secondaryMotionMult: { value: 0 }
};
this.normalsShader.fragmentShader = NORMALS_FRAG;
this.normalsShader.uniforms = {
tSize: { value: new Vector2() },
tPosition0: { value: null },
tPosition1: { value: null },
tAdjacentsA: { value: null },
tAdjacentsB: { value: null }
};
}
}