UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

53 lines (52 loc) 2.15 kB
"use strict"; import { TypedJsNode } from "./_Base"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { JsConnectionPoint, JsConnectionPointType, JS_CONNECTION_POINT_IN_NODE_DEF } from "../utils/io/connections/Js"; import { JsType } from "../../poly/registers/nodes/types/Js"; import { Ray } from "three"; import { Poly } from "../../Poly"; const CONNECTION_OPTIONS = JS_CONNECTION_POINT_IN_NODE_DEF; class RayFromCameraJsParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); /** @param x position in screen space */ this.x = ParamConfig.FLOAT(0, { range: [-1, 1] }); /** @param y position in screen space */ this.y = ParamConfig.FLOAT(0, { range: [-1, 1] }); } } const ParamsConfig = new RayFromCameraJsParamsConfig(); export class RayFromCameraJsNode extends TypedJsNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return JsType.RAY_FROM_CAMERA; } initializeNode() { super.initializeNode(); this.io.inputs.setNamedInputConnectionPoints([ new JsConnectionPoint(JsConnectionPointType.CAMERA, JsConnectionPointType.CAMERA, CONNECTION_OPTIONS) ]); this.io.outputs.setNamedOutputConnectionPoints([ new JsConnectionPoint(JsConnectionPointType.RAY, JsConnectionPointType.RAY) ]); } setLines(shadersCollectionController) { const camera = this.variableForInput(shadersCollectionController, JsConnectionPointType.CAMERA); const x = this.variableForInputParam(shadersCollectionController, this.p.x); const y = this.variableForInputParam(shadersCollectionController, this.p.y); const out = this.jsVarName(JsConnectionPointType.RAY); const tmpVarName = shadersCollectionController.addVariable(this, new Ray()); const func = Poly.namedFunctionsRegister.getFunction("rayFromCamera", this, shadersCollectionController); const bodyLine = func.asString(camera, x, y, tmpVarName); shadersCollectionController.addBodyOrComputed(this, [ { dataType: JsConnectionPointType.FLOAT, varName: out, value: bodyLine } ]); } }