Skip to content
Merged
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
14 changes: 7 additions & 7 deletions packages/model-viewer/src/features/scene-graph/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export declare interface ThreeDOMElementMap {
}

/** A 2D Cartesian coordinate */
export interface Vector2 {
x: number;
y: number;
export interface Vector2DInterface {
u: number;
v: number;
}

/**
Expand Down Expand Up @@ -334,12 +334,12 @@ export declare interface Sampler {
/**
* The texture scale.
*/
readonly scale: Vector2|null;
readonly scale: Vector2DInterface|null;

/**
* The texture offset.
*/
readonly offset: Vector2|null;
readonly offset: Vector2DInterface|null;

/**
* Configure the minFilter value of the Sampler.
Expand Down Expand Up @@ -371,12 +371,12 @@ export declare interface Sampler {
* Sets the texture scale, or resets it to (1, 1) if argument is null.
* As the scale value increases, the repetition of the texture will increase.
*/
setScale(scale: Vector2|null): void;
setScale(scale: Vector2DInterface|null): void;

/**
* Sets the texture offset, or resets it to (0, 0) if argument is null.
*/
setOffset(offset: Vector2|null): void;
setOffset(offset: Vector2DInterface|null): void;
}


Expand Down
23 changes: 12 additions & 11 deletions packages/model-viewer/src/features/scene-graph/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

import {Texture as ThreeTexture, Vector2} from 'three';

import {toVector2D, Vector2D} from '../../model-viewer-base.js';
import {Filter, MagFilter, MinFilter, Wrap, WrapMode} from '../../three-components/gltf-instance/gltf-2.0.js';
import {Sampler as DefaultedSampler} from '../../three-components/gltf-instance/gltf-defaulted.js';

import {Sampler as SamplerInterface} from './api.js';
import {Sampler as SamplerInterface, Vector2DInterface} from './api.js';
import {$correlatedObjects, $onUpdate, ThreeDOMElement} from './three-dom-element.js';


Expand Down Expand Up @@ -113,12 +114,12 @@ export class Sampler extends ThreeDOMElement implements SamplerInterface {
return this[$threeTexture].rotation;
}

get scale(): Vector2 {
return this[$threeTexture].repeat;
get scale(): Vector2D {
return toVector2D(this[$threeTexture].repeat);
}

get offset(): Vector2|null {
return this[$threeTexture].offset;
get offset(): Vector2D|null {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized this also needs to change - you're returning a pointer to an internal three.js construct, which means they could then use it to change out internals (in JS every object is pass by reference). You need to use our toVector2D() to make a proper copy.

return toVector2D(this[$threeTexture].offset);
}

setMinFilter(filter: MinFilter) {
Expand All @@ -145,20 +146,20 @@ export class Sampler extends ThreeDOMElement implements SamplerInterface {
this[$setProperty]('rotation', rotation);
}

setScale(scale: Vector2|null): void {
setScale(scale: Vector2DInterface|null): void {
if (scale == null) {
// Reset scale.
scale = new Vector2(1, 1);
scale = {u: 1, v: 1};
}
this[$setProperty]('repeat', scale);
this[$setProperty]('repeat', new Vector2(scale.u, scale.v));
}

setOffset(offset: Vector2|null): void {
setOffset(offset: Vector2DInterface|null): void {
if (offset == null) {
// Reset offset.
offset = new Vector2(0, 0);
offset = {u: 0, v: 0};
}
this[$setProperty]('offset', offset);
this[$setProperty]('offset', new Vector2(offset.u, offset.v));
}

private[$setProperty]<P extends 'minFilter'|'magFilter'|'wrapS'|'wrapT'|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

import {expect} from '@esm-bundle/chai';
import {Vector2} from 'three';

import {TextureInfo} from '../../../features/scene-graph/texture-info.js';
import {ModelViewerElement} from '../../../model-viewer.js';
Expand Down Expand Up @@ -82,8 +81,8 @@ suite('scene-graph/texture-info', () => {
.pbrMetallicRoughness['baseColorTexture']
.texture?.sampler!;
sampler.setRotation(0.1);
sampler.setOffset(new Vector2(0.2, 0.3));
sampler.setScale(new Vector2(0.4, 0.5));
sampler.setOffset({u: 0.2, v: 0.3});
sampler.setScale({u: 0.4, v: 0.5});

// Export model.
const exported = await element.exportScene({binary: true});
Expand All @@ -99,10 +98,10 @@ suite('scene-graph/texture-info', () => {
.pbrMetallicRoughness['baseColorTexture']
.texture?.sampler!;
expect(exported_sampler.rotation).to.be.eq(0.1, 'rotation');
expect(exported_sampler.offset)
.to.be.eql(new Vector2(0.2, 0.3), 'offset');
expect(exported_sampler.scale)
.to.be.eql(new Vector2(0.4, 0.5), 'scale');
expect(exported_sampler.offset!.u).to.be.eql(0.2, 'offset_u');
expect(exported_sampler.offset!.v).to.be.eql(0.3, 'offset_v');
expect(exported_sampler.scale!.u).to.be.eql(0.4, 'scale_u');
expect(exported_sampler.scale!.v).to.be.eql(0.5, 'scale_v');
});
});
});
8 changes: 4 additions & 4 deletions packages/modelviewer.dev/examples/scenegraph/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -525,17 +525,17 @@ <h2 class="demo-title">Transform textures</h2>

scaleSlider.addEventListener('input', (event) => {
const scale = {
x: scaleSlider.value,
y: scaleSlider.value
u: scaleSlider.value,
v: scaleSlider.value
};
sampler.setScale(scale);
scaleDisplay.textContent = scale.x;
});

offsetSlider.addEventListener('input', (event) => {
const offset = {
x: offsetSlider.value,
y: -offsetSlider.value
u: offsetSlider.value,
v: -offsetSlider.value
};
sampler.setOffset(offset);
});
Expand Down