Skip to content

Commit

Permalink
Full proof of concept with hard-coded sizes and UI adjustments for wo…
Browse files Browse the repository at this point in the history
…od block demo
  • Loading branch information
gjcope committed Oct 7, 2024
1 parent 9233eb6 commit b352cbe
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 23 deletions.
44 changes: 42 additions & 2 deletions source/client/components/CVModel2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
* limitations under the License.
*/

import { Vector3, Quaternion, Box3, Mesh, Group, Matrix4, Box3Helper, Object3D, FrontSide, BackSide, DoubleSide } from "three";
import { Vector3, Quaternion, Box3, Mesh, Group, Matrix4, Box3Helper, Object3D, FrontSide, BackSide, DoubleSide, AlwaysStencilFunc, IncrementWrapStencilOp, MeshBasicMaterial, DecrementWrapStencilOp } from "three";

import Notification from "@ff/ui/Notification";

import { ITypedEvent, Node, types } from "@ff/graph/Component";
import CObject3D from "@ff/scene/components/CObject3D";
import CObject3D, { IRenderContext } from "@ff/scene/components/CObject3D";

import * as helpers from "@ff/three/helpers";

Expand Down Expand Up @@ -798,4 +798,44 @@ export default class CVModel2 extends CObject3D
}
});
}


preRender(context: IRenderContext)
{
let saveMat = null;
this.object3D.traverse(object => {
const material = object["material"] as UberPBRMaterial | UberPBRAdvMaterial;
if (material && material.isUberPBRMaterial) {
saveMat = material;
}
});

if(!saveMat) {
return;
}

// modify material for stencil buffer pass
saveMat.depthWrite = false;
saveMat.depthTest = false;
saveMat.colorWrite = false;
saveMat.stencilWrite = true;
saveMat.stencilFunc = AlwaysStencilFunc;
saveMat.side = BackSide;
saveMat.stencilFail = IncrementWrapStencilOp;
saveMat.stencilZFail = IncrementWrapStencilOp;
saveMat.stencilZPass = IncrementWrapStencilOp;

this.renderer.views[0].renderer.render(this.object3D, context.camera);

// restore material for regular render pass
saveMat.depthWrite = true;
saveMat.depthTest = true;
saveMat.colorWrite = true;
saveMat.stencilWrite = true;
saveMat.stencilFunc = AlwaysStencilFunc;
saveMat.side = FrontSide;
saveMat.stencilFail = DecrementWrapStencilOp;
saveMat.stencilZFail = DecrementWrapStencilOp;
saveMat.stencilZPass = DecrementWrapStencilOp;
}
}
2 changes: 1 addition & 1 deletion source/client/components/CVScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default class CVScene extends CVNode

this.models.forEach(model => {
if(model.object3D.visible){
box.expandByObject(model.object3D);
box.expandByObject(model.object3D, true);
}
});
box.getSize(_vec3);
Expand Down
64 changes: 47 additions & 17 deletions source/client/components/CVSlicer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { Box3, DoubleSide, Euler, Mesh, MeshBasicMaterial, PlaneGeometry, Texture } from "three";
import { Box3, DoubleSide, Euler, Mesh, MeshBasicMaterial, NotEqualStencilFunc, PlaneGeometry, ReplaceStencilOp, Texture } from "three";

import { types } from "@ff/graph/Component";
import CObject3D from "@ff/scene/components/CObject3D";
Expand Down Expand Up @@ -85,10 +85,10 @@ export default class CVSlicer extends CObject3D

protected plane: number[] = null;
protected axisIndex = -1;
protected xSliceSprites: Texture = null;
protected sliceDimU = 43;
protected sliceDimV = 11;
protected sliceCount = 469;
protected sliceSprites: Texture[] = [null,null,null];
protected sliceDims = [{u: 20, v: 20}, {u: 14, v: 20}, {u: 27, v: 20}];
protected sliceSize = [{x: 70.2, y: 36.4, z: 52}, {x: 70.2, y: 52, z: 36.4}, {x: 36.4, y: 52, z: 70.2}];
protected sliceCounts = [400, 280, 540];

create()
{
Expand All @@ -102,8 +102,14 @@ export default class CVSlicer extends CObject3D
{
const ins = this.ins;

if(this.xSliceSprites == null) {
this.assetReader.getTexture("xSlice.jpg").then((texture) => this.xSliceSprites = texture);
if(this.sliceSprites[0] == null) {
this.assetReader.getTexture("xSlice.jpg").then((texture) => {this.sliceSprites[0] = texture; ins.enabled.set();});
}
if(this.sliceSprites[1] == null) {
this.assetReader.getTexture("ySlice.jpg").then((texture) => {this.sliceSprites[1] = texture; ins.enabled.set(); });
}
if(this.sliceSprites[2] == null) {
this.assetReader.getTexture("zSlice.jpg").then((texture) => {this.sliceSprites[2] = texture; ins.enabled.set(); });
}

if (ins.axis.changed) {
Expand All @@ -116,6 +122,13 @@ export default class CVSlicer extends CObject3D
else {
ins.inverted.setValue(false);
this.axisIndex = axisIndex;

if(this.object3D) {
this.sliceSprites[axisIndex].repeat.set(1/this.sliceDims[axisIndex].u,1/this.sliceDims[axisIndex].v);
this.object3D["material"].map = this.sliceSprites[axisIndex];
this.object3D.scale.set(this.sliceSize[axisIndex].x, this.sliceSize[axisIndex].y, 1.0);
this.object3D.updateMatrix();
}
}
}

Expand All @@ -134,28 +147,45 @@ export default class CVSlicer extends CObject3D

// set components of slicing plane vector
this.plane = _planes[planeIndex];
const min = boundingBox.min.getComponent(axisIndex);
const max = boundingBox.max.getComponent(axisIndex);
let min = boundingBox.min.getComponent(axisIndex);
let max = boundingBox.max.getComponent(axisIndex);
const boundsScale = this.sliceSize[axisIndex].z/(max-min);
min *= boundsScale;
max *= boundsScale;
const value = 1 - ins.position.value;
this.plane[3] = axisInverted ? value * (max - min) - max : max - value * (max - min);

// init slice plane
const angle = Math.PI*0.5;
if(!this.object3D && isFinite(min) && isFinite(max)) {
const geometry = new PlaneGeometry( max-min, max-min );
if(!this.object3D && isFinite(min) && isFinite(max) && this.sliceSprites[axisIndex]) {
//const geometry = new PlaneGeometry( ((max-min)*1.929)/1.41, (max-min)/1.41 );
const geometry = new PlaneGeometry( 1.0, 1.0 );
const material = new MeshBasicMaterial( {side: DoubleSide} );
this.xSliceSprites.repeat.set(1/this.sliceDimU,1/this.sliceDimV);
material.map = this.xSliceSprites;

material.stencilWrite = true;
material.stencilRef = 0;
material.stencilFunc = NotEqualStencilFunc;
material.stencilFail = ReplaceStencilOp;
material.stencilZFail = ReplaceStencilOp;
material.stencilZPass = ReplaceStencilOp;

this.sliceSprites[axisIndex].repeat.set(1/this.sliceDims[axisIndex].u,1/this.sliceDims[axisIndex].v);
material.map = this.sliceSprites[axisIndex];
const plane = new Mesh( geometry, material );
this.object3D = plane;
this.object3D.scale.set(this.sliceSize[axisIndex].x, this.sliceSize[axisIndex].y, 1.0);
this.object3D.renderOrder = 10;
}
// update slice plane
if(this.object3D) {
const sliceIdx = Math.round(value * (this.sliceCount-1));
this.xSliceSprites.offset.set((sliceIdx%this.sliceDimU)/this.sliceDimU, (1-(1/this.sliceDimV))-Math.floor(sliceIdx/this.sliceDimU)/this.sliceDimV);
const idx = Math.round(value * (this.sliceCounts[axisIndex]));
const sliceIdx = this.plane[0] ? this.sliceCounts[axisIndex] - idx : idx;

this.sliceSprites[axisIndex].offset.set((sliceIdx%this.sliceDims[axisIndex].u)/this.sliceDims[axisIndex].u, (1-(1/this.sliceDims[axisIndex].v))-Math.floor(sliceIdx/this.sliceDims[axisIndex].u)/this.sliceDims[axisIndex].v);

const pos = max - value * (max - min);
this.object3D.setRotationFromEuler(new Euler(this.plane[1]*angle,this.plane[0]*angle,0));
const rotPlane = _planes[axisIndex];
this.object3D.setRotationFromEuler(new Euler(rotPlane[1]*3*angle,rotPlane[0]*angle+rotPlane[2]*2*angle,-rotPlane[1]*angle+rotPlane[2]*angle));
this.object3D.position.set(Math.abs(this.plane[0])*pos, Math.abs(this.plane[1])*pos, Math.abs(this.plane[2])*pos);
this.object3D.updateMatrix();
}
Expand Down Expand Up @@ -210,7 +240,7 @@ export default class CVSlicer extends CObject3D
{
const ins = this.ins;

if (ins.enabled.changed) {
if (ins.enabled.value != material.defines["CUT_PLANE"]) {
material.enableCutPlane(ins.enabled.value);
material.needsUpdate = true;
}
Expand Down
3 changes: 2 additions & 1 deletion source/client/components/CVToolProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import CComponentProvider, {

import CVTool from "./CVTool";
import CVAnalytics from "./CVAnalytics";
import CVSliceTool from "./CVSliceTool";

////////////////////////////////////////////////////////////////////////////////

Expand All @@ -36,7 +37,7 @@ export default class CVToolProvider extends CComponentProvider<CVTool>
static readonly componentType = CVTool;

protected static readonly ins = {
visible: types.Boolean("Tools.Visible"),
visible: types.Boolean("Tools.Visible", true), // HACK for demo purposes
closed: types.Event("Tools.Closed")
};

Expand Down
4 changes: 2 additions & 2 deletions source/client/nodes/NVTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export default class NVTools extends Node
createComponents()
{
this.createComponent(CVToolProvider);
this.createComponent(CVViewTool);
/*this.createComponent(CVViewTool); // HACK for Demo purposes
this.createComponent(CVRenderTool);
this.createComponent(CVEnvironmentTool);
this.createComponent(CVLightTool);
this.createComponent(CVTapeTool);
this.createComponent(CVTapeTool);*/
this.createComponent(CVSliceTool);
}
}

0 comments on commit b352cbe

Please sign in to comment.