Skip to content

Commit

Permalink
Merge pull request #16 from alja/zshape-final
Browse files Browse the repository at this point in the history
Add ZShape object and material for optimized rendering of digits
  • Loading branch information
ss4774 authored Mar 9, 2024
2 parents 15decda + a02af3e commit 80026f0
Show file tree
Hide file tree
Showing 12 changed files with 1,180 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/reve/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ default:
@echo No default target, please specify dist or clean.

dist: REveRenderCore-min.mjs shaders
tar cf RenderCore.tar REveRenderCore-min.mjs shaders/
tar cf RenderCore.tar ../../LICENSE REveRenderCore-min.mjs shaders/
gzip -9 RenderCore.tar
sha256sum -b RenderCore.tar.gz
@echo Now move the tarball to ROOT source and put the checksum into ROOT source tree
Expand Down
1 change: 1 addition & 0 deletions dist/reve/program_names.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"phong",
"basic",
"basic_zsprite",
"basic_zshape",
"basic_stripes",
"custom_GBufferMini_stripes",
"custom_picker_TRIANGLES",
Expand Down
52 changes: 52 additions & 0 deletions examples/outlineExample/main-ZSprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,39 @@ const CoreControl = {
);
this.tex_insta_pos.flipy = false;


// AMT ZShape pos
// Instancing, passing position for each instance in RGBA32F texture, A not used.
let sna = 2;
let SN = sna * sna * sna;
let sarr = new Float32Array(SN * 4);
{
let off = 0;
for (let i = 0; i < sna; ++i) {
for (let j = 0; j < sna; ++j) {
for (let k = 0; k < sna; ++k) {
//console.log("off", off);
// console.log(i, j, k);
sarr[off] = 0.3 * i;
sarr[off + 1] = 0.3 * j;
sarr[off + 2] = 0.3 * k;
sarr[off + 3] = 0;
off = off + 4;
}
}
}
}

this.tex_insta_num_shape = SN;
this.tex_insta_pos_shape = new RC.Texture(sarr,
RC.Texture.WRAPPING.ClampToEdgeWrapping,
RC.Texture.WRAPPING.ClampToEdgeWrapping,
RC.Texture.FILTER.NearestFilter,
RC.Texture.FILTER.NearestFilter,
RC.Texture.FORMAT.RGBA32F, RC.Texture.FORMAT.RGBA, RC.Texture.TYPE.FLOAT,
sna * sna, sna);
// AMT end

// Testing creation of texture from JS array, simple checker pattern.
let at = new Uint8Array(2 * 2 * 2);
at[0] = at[6] = 255; // luminance
Expand Down Expand Up @@ -353,6 +386,25 @@ const CoreControl = {
sprite4.drawOutline = true;
scene.add(sprite4);

// AMT begin
let shm = new RC.ZShapeBasicMaterial({
ShapeSize: [0.1, 0.1, 0.02],
color: new RC.Color(1, 0, 0),
emissive: new RC.Color(0.07, 0.07, 0.06),
diffuse: new RC.Color(0, 0.6, 0.7),
alpha: 0.5
});
shm.addInstanceData(this.tex_insta_pos_shape);
let zshape = new RC.ZShape(null, shm);
zshape.position.set(7, 7, 0);
zshape.instanced = true;
zshape.instanceCount = this.tex_insta_num_shape;
zshape.drawOutline = true;
zshape.outlineMaterial.outline_instances_setup([ 4, 6]);
scene.add(zshape);
// AMT end


let lm1 = new RC.ZSpriteBasicMaterial( { SpriteMode: RC.SPRITE_SPACE_WORLD,
SpriteSize: [8, 8],
color: new RC.Color(1, 0, 0),
Expand Down
4 changes: 3 additions & 1 deletion src/RenderCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ export {Grid} from './objects/Grid.js';
// Instanced, instance-pickable, outline-supporting materials and objects
export {ZSpriteBasicMaterial} from './materials/ZSpriteBasicMaterial.js';
export {ZSprite} from './objects/ZSprite.js';
export {ZShapeBasicMaterial} from './materials/ZShapeBasicMaterial.js';
export {ZShape} from './objects/ZShape.js';

// Program Management
export {MaterialProgramTemplate} from './program_management/MaterialProgramTemplate.js';
Expand Down Expand Up @@ -171,4 +173,4 @@ export {DoFFX} from './renderers/FX/DoFFX.js';
// Controls (Input)
export {KeyboardInput} from './controls/KeyboardInput.js';
export {MouseInput} from './controls/MouseInput.js';
export {GamepadInput} from './controls/GamepadInput.js';
export {GamepadInput} from './controls/GamepadInput.js';
2 changes: 2 additions & 0 deletions src/contrib/REveCameraControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export class REveCameraControls extends EventDispatcher {
else {
let dollyDefault = 1.25*0.5*Math.sqrt(3)*bb_R;
camTrans.moveLF(1, dollyDefault);
scope.object._near = 0.05 * dollyDefault;
scope.object._far = 2 * dollyDefault;
scope.object.updateProjectionMatrix();
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/contrib/REveRenderCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ export {Grid} from '../objects/Grid.js';
// Instanced, instance-pickable, outline-supporting materials and objects
export {ZSpriteBasicMaterial} from '../materials/ZSpriteBasicMaterial.js';
export {ZSprite} from '../objects/ZSprite.js';
export {ZShapeBasicMaterial} from '../materials/ZShapeBasicMaterial.js';
export {ZShape} from '../objects/ZShape.js';

// Program Management
export {MaterialProgramTemplate} from '../program_management/MaterialProgramTemplate.js';
Expand Down
6 changes: 6 additions & 0 deletions src/materials/CustomShaderMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export class CustomShaderMaterial extends Material {
}
}

hasSBFlag(flag) {
const index = this._flagsSB.indexOf(flag);

return (index > -1);
}

clearSBFlags() {
this._flagsSB.clear();
}
Expand Down
165 changes: 165 additions & 0 deletions src/materials/ZShapeBasicMaterial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import {CustomShaderMaterial} from './CustomShaderMaterial.js';
import {Color} from "../math/Color.js";
import {FRONT_AND_BACK_SIDE} from '../constants.js';
import {Int32Attribute} from "../core/BufferAttribute.js";

export class ZShapeBasicMaterial extends CustomShaderMaterial {

/**
* WARNING:
* - constructor does not pass arguments to parent class
* - evades "custom" in shader name by setting programName after super
*/
constructor(args = {}){
super();

this.type = "ZShapeBasicMaterial";
this.programName = "basic_zshape";

// Uniforms aspect and viewport set by MeshRenderer based on actual viewport
this.setUniform("ShapeSize", "ShapeSize" in args ? args.ShapeSize : [1.0, 1.0, 1.0]);

this.color = args.color ? args.color : new Color(Math.random() * 0xffffff);
this.emissive = args.emissive ? args.emissive : new Color(Math.random() * 0xffffff);
this.diffuse = args.diffuse ? args.diffuse : new Color(Math.random() * 0xffffff);

this.side = args.side ? args.side : FRONT_AND_BACK_SIDE;
// this._specular = new Color(Math.random() * 0xffffff);
this._specular = new Color(0.5 * 0xffffff);
this._shininess = 64;
}

set specular(val) {
this._specular = val;

// Notify onChange subscriber
if (this._onChangeListener) {
var update = {uuid: this._uuid, changes: {specular: this._specular.getHex()}};
this._onChangeListener.materialUpdate(update)
}
}
set shininess(val) {
this._shininess = val;

// Notify onChange subscriber
if (this._onChangeListener) {
var update = {uuid: this._uuid, changes: {shininess: this._shininess}};
this._onChangeListener.materialUpdate(update)
}
}

get specular() { return this._specular; }
get shininess() { return this._shininess; }

clone_for_picking() {
let o = new ZShapeBasicMaterial( {
ShapeSize: this.getUniform("ShapeSize"),
color: this.color, emissive: this.emissive, diffuse: this.diffuse,
side: this.side
} );
if (this.hasSBFlag("SCALE_PER_INSTANCE")) o.addSBFlag("SCALE_PER_INSTANCE");
if (this.hasSBFlag("MAT4_PER_INSTANCE")) o.addSBFlag("MAT4_PER_INSTANCE");
for (const m of this.maps) o.addMap(m);
o._instanceData = this._instanceData;
o.addSBFlag('PICK_MODE_UINT');
o.setUniform("u_PickInstance", false);
return o;
}

clone_for_outline() {
let o = new ZShapeBasicMaterial( {
ShapeSize: this.getUniform("ShapeSize"),
color: this.color, emissive: this.emissive, diffuse: this.diffuse,
side: this.side
} );
if (this.hasSBFlag("SCALE_PER_INSTANCE")) o.addSBFlag("SCALE_PER_INSTANCE");
if (this.hasSBFlag("MAT4_PER_INSTANCE")) o.addSBFlag("MAT4_PER_INSTANCE");
for (const m of this.maps) o.addMap(m);
o._instanceData = this._instanceData;
o.addSBFlag('OUTLINE');
o.setUniform("u_OutlineGivenInstances", false);
o.setAttribute("a_OutlineInstances", Int32Attribute([0], 1, 0x7fffffff));
return o;
}

get color() { return this._color; }
set color(val) {
this._color = val;

// Notify onChange subscriber
if (this._onChangeListener) {
var update = {uuid: this._uuid, changes: {color: this._color.getHex()}};
this._onChangeListener.materialUpdate(update)
}
}

get emissive() { return this._emissive; }
set emissive(val) {
this._emissive = val;

// Notify onChange subscriber
if (this._onChangeListener) {
var update = {uuid: this._uuid, changes: {emissive: this._emissive.getHex()}};
this._onChangeListener.materialUpdate(update)
}
}
get diffuse() { return this._diffuse; }
set diffuse(val) {
this._diffuse = val;

// Notify onChange subscriber
if (this._onChangeListener) {
var update = {uuid: this._uuid, changes: {diffuse: this._diffuse.getHex()}};
this._onChangeListener.materialUpdate(update)
}
}

// Outline - setup / reset for instance list outlining.
// To be called on outline version (with OUTLINE SB-flag).
outline_instances_setup(instance_list) {
this.setUniform("u_OutlineGivenInstances", true);
let buf_attr = this.getAttribute("a_OutlineInstances");
buf_attr.array = new Int32Array(instance_list);
buf_attr.divisor = 1;
// leaks buffers in gl-attrib-manager
// this.setAttribute("a_OutlineInstances", Int32Attribute(instance_list, 1, 1));
}
outline_instances_reset() {
this.setUniform("u_OutlineGivenInstances", false);
let buf_attr = this.getAttribute("a_OutlineInstances");
buf_attr.array = new Int32Array([0]);
buf_attr.divisor = 0x7fffffff;
// leaks buffers in gl-attrib-manager
// this.setAttribute("a_OutlineInstances", Int32Attribute([0], 1, 0x7fffffff));
}


update(data) {
super.update(data);

for (let prop in data) {
switch (prop) {
case "color":
this._color = data.color;
delete data.color;
break;
case "emissive":
this._emissive = data.emissive;
delete data.emissive;
break;
case "diffuse":
this._diffuse = data.diffuse;
delete data.diffuse;
break;
case "specular":
this._specular.setHex(data.specular);
delete data.specular;
break;
case "shininess":
this._shininess = data.shininess;
delete data.shininess;
break;
}
}
}
}
Loading

0 comments on commit 80026f0

Please sign in to comment.