1 line
14 KiB
Plaintext
1 line
14 KiB
Plaintext
|
|
{"version":3,"file":"Refractor.cjs","sources":["../../src/objects/Refractor.js"],"sourcesContent":["import {\n Color,\n Matrix4,\n Mesh,\n PerspectiveCamera,\n Plane,\n Quaternion,\n ShaderMaterial,\n UniformsUtils,\n Vector3,\n Vector4,\n WebGLRenderTarget,\n NoToneMapping,\n HalfFloatType,\n} from 'three'\nimport { version } from '../_polyfill/constants'\n\nconst Refractor = /* @__PURE__ */ (() => {\n class Refractor extends Mesh {\n static RefractorShader = {\n uniforms: {\n color: {\n value: null,\n },\n\n tDiffuse: {\n value: null,\n },\n\n textureMatrix: {\n value: null,\n },\n },\n\n vertexShader: /* glsl */ `\n\n\t\tuniform mat4 textureMatrix;\n\n\t\tvarying vec4 vUv;\n\n\t\tvoid main() {\n\n\t\t\tvUv = textureMatrix * vec4( position, 1.0 );\n\t\t\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\n\t\t}`,\n\n fragmentShader: /* glsl */ `\n\n\t\tuniform vec3 color;\n\t\tuniform sampler2D tDiffuse;\n\n\t\tvarying vec4 vUv;\n\n\t\tfloat blendOverlay( float base, float blend ) {\n\n\t\t\treturn( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );\n\n\t\t}\n\n\t\tvec3 blendOverlay( vec3 base, vec3 blend ) {\n\n\t\t\treturn vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ), blendOverlay( base.b, blend.b ) );\n\n\t\t}\n\n\t\tvoid main() {\n\n\t\t\tvec4 base = texture2DProj( tDiffuse, vUv );\n\t\t\tgl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );\n\n\t\t\t#include <tonemapping_fragment>\n\t\t\t#include <${version >= 154 ? 'colorspace_fragment' : 'encodings_fragment'}>\n\n\t\t}`,\n }\n\n constructor(geometry, options = {}) {\n super(geometry)\n\n this.isRefractor = true\n\n this.type = 'Refractor'\n this.camera = new PerspectiveCamera()\n\n const scope = this\n\n const color = options.color !== undefined ? new Color(options.color) : new Color(0x7f7f7f)\n const textureWidth = options.textureWidth || 512\n const textureHeight = options.textureHeight || 512\n const clipBias = options.clipBias || 0\n const shader = options.shader || Refractor.RefractorShader\n const multisample = options.multisample !== undefined ? options.multisample : 4\n\n //\n\n const virtualCamera = this.camera\n virtualCamera.matrixAutoUpdate = false\n virtualCamera.userData.refractor = true\n\n //\n\n const refractorPlane = new Plane()\n const textureMatrix = new Matrix4()\n\n // render target\n\n const renderTarget = new WebGLRenderTarget(textureWidth, textureHeight, {\n samples: multisample,\n type: HalfFloatType,\n })\n\n // material\n\n this.material = new ShaderMaterial({\n uniforms: UniformsUtils.clone(shader.uniforms),\n vertexShader: shader.vertexShader,\n fragmentShader: shader.fragmentShader,\n transparent: true, // ensures, refractors are drawn from farthest to closest\n })\n\n this.material.uniforms['color'].value = color\n this.material.uniforms['tDiffuse'].value = renderTarget.texture\n this.material.uniforms['textureMatrix'].value = textureMatrix\n\n // functions\n\n const visible = (function () {\n const refractorWorldPosition = new Vector3()\n const cameraWorldPosition = new Vector3()\n const rotationMatrix = new Matrix4()\n\n const view = new Vector3()\n const normal = new Vector3()\n\n return function visible(camera) {\n refractorWorldPosition.setFromMatrixPosition(scope.matrixWorld)\n cameraWorldPosition.setFromMatrixPosition(camera.matrixWorld)\n\n view.subVectors(refractorWorldPosition, cameraWorldPosition)\n\n rotationMatrix.extractRotation(scope.matrixWorld)\n\n normal.set(0, 0, 1)\n normal.applyMatrix4(rotationMatrix)\n\n return view.dot(normal) < 0\n }\n })()\n\n const updateRefractorPlane = (function () {\n c
|