Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Selective Stretch #40

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,523 changes: 0 additions & 5,523 deletions dist/assets/dstretch/plate/dstretch_info.json

This file was deleted.

Binary file removed dist/assets/dstretch/plate/plane_0.jpg
Binary file not shown.
Binary file removed dist/assets/dstretch/rock/SanBorjita3174.jpg
Binary file not shown.
5,648 changes: 0 additions & 5,648 deletions dist/assets/dstretch/rock/info.json

This file was deleted.

Binary file added dist/assets/dstretch/selective/soldier.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dist/assets/dstretch/selective/wall_0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dist/assets/dstretch/selective/wall_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dist/assets/dstretch/selective/wall_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dist/assets/dstretch/selective/wall_4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dist/assets/dstretch/selective/wall_5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dist/assets/dstretch/standard/plane_1.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed dist/assets/dstretch/wood/plane_0.jpg
Binary file not shown.
5,543 changes: 0 additions & 5,543 deletions dist/assets/dstretch/wood/plane_1.dstretch

This file was deleted.

1 change: 1 addition & 0 deletions src/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ class Layer {
if (this.layout.type == "image") {
this.layout.width = raster.width;
this.layout.height = raster.height;
console.assert(this.shader.samplers.length > 0, "Shaders should have an assigned sampler before setting the layer as ready");
this.layout.emit('updateSize');
}
tile.size += size;
Expand Down
121 changes: 39 additions & 82 deletions src/LayerDstretch.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,58 @@
import {Layer} from './Layer.js';
import {LayerImage} from './LayerImage.js'
import {ShaderDstretch} from './ShaderDstretch.js';
import { Raster } from './Raster.js';
import { ShaderFilterDstretch } from './ShaderFilterDstretch.js';
import { ShaderFilterSelectiveStretch } from './ShaderFilterSelectiveStretch.js';

class LayerDstretch extends LayerImage {
constructor(options) {
super(options);

if(!this.url)
throw "Url option is required";

this.shaders['dstretch'] = new ShaderDstretch();
this.setShader('dstretch');
this.eulerRotation = [0,0,0];

this.worldRotation = 0; //if the canvas or ethe layer rotate, light direction neeeds to be rotated too.
if(this.url)
this.loadJson();
this.addControl('light', [0, 0]);
}

setLight(value, dt) {
this.setControl('light', value, dt);

this.eulerRotation[0] = Math.PI * this.getControl('light').current.value[0];//this.controls['light'].current[0];
this.eulerRotation[1] = Math.PI * this.getControl('light').current.value[1];//this.controls['light'].current[1];

this.shader.updateRotationMatrix(this.eulerRotation);
this.emit('update');
this.stretchType = options.stretchType;
this.filter = undefined

let FilterType;

if (this.stretchType === 'standard')
FilterType = ShaderFilterDstretch;
else
FilterType = ShaderFilterSelectiveStretch;

switch (options.sourceType) {
case 'url':
if (!options.url)
console.error("DStretch set to use an URL, but said URL wasn't specified.");
this.filter = new FilterType(options.url);
break;
case 'layer':
this.filter = new FilterType(this);
break;
case 'array':
this.filter = new FilterType(options.samples);
break;
default:
console.error("DStretch source ", sourceType, " not supported.");
break;
}
this.addShaderFilter(this.filter);
if (this.stretchType == 'selective')
this.addControl('light', [0, 0]);
}

loadJson() {
(async () => {
let json
try {
let dstretchUrl = this.url.substring(0, this.url.lastIndexOf(".")) + ".dstretch";
let response = await fetch(dstretchUrl);
console.log(response.ok);
json = await response.json();
}
catch (error) {
json = {
transformation: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
samples: []
};
this.rasters[0].loadTexture = this.loadAndSampleTexture.bind(this);
}

this.layout.setUrls([this.url]);
this.shader.init(json);
})();
}

draw(transform, viewport) {
this.shader.setMinMax();
return super.draw(transform, viewport);
}

loadAndSampleTexture(gl, img) {
this.rasters[0].width = img.width;
this.rasters[0].height = img.height;
let tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); //_MIPMAP_LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);

// Sample the texture
// Temporarily print the texture on a canvas
let canvas = document.createElement("canvas");
let context = canvas.getContext("2d");

canvas.setAttribute("width", img.width);
canvas.setAttribute("height", img.height);
context.drawImage(img, 0, 0, img.width, img.height);

// Get the data and sample the texture
let imageData = context.getImageData(0, 0, img.width, img.height).data;
let samples = [];
let rowSkip = Math.floor(img.height / 32);
let colSkip = Math.floor(img.width / 32);

console.log(rowSkip, colSkip);
setLight(value, dt) {
if (this.stretchType != 'selective')
return;

for (let i=0; i<imageData.length; i+=4) {
let row = Math.floor((i / 4) / img.width);
let col = Math.floor(i / 4) % img.width;
this.setControl('light', value, dt);

if (row % rowSkip == 0 && col % colSkip == 0)
samples.push([imageData[i], imageData[i+1], imageData[i+2]]);
}
this.filter.controls[0] = Math.PI * 1.2 * this.getControl('light').current.value[0];//this.controls['light'].current[0];
this.filter.controls[1] = Math.abs(this.getControl('light').current.value[1]);

this.shader.samples = samples;
this.shader.setMinMax();
return tex;
this.filter.setMinMax();
this.emit('update');
}
}

Expand Down
147 changes: 0 additions & 147 deletions src/ShaderDstretch.js

This file was deleted.

Loading