title category import { OrbitControl } from "@oasis-engine/controls" ; import * as dat from "dat.gui" ; import { AssetType , Camera , Entity , Script , Sprite , SpriteRenderer , Texture2D , Vector2 , Vector3 , WebGLEngine } from "oasis-engine" ; const engine = new WebGLEngine ( "canvas" ) ; engine . canvas . resizeByClientSize ( ) ; const rootEntity = engine . sceneManager . activeScene . createRootEntity ( ) ; const cameraEntity = rootEntity . createChild ( "Camera" ) ; cameraEntity . transform . setPosition ( 0 , 0 , 50 ) ; cameraEntity . addComponent ( Camera ) ; cameraEntity . addComponent ( OrbitControl ) ; engine . resourceManager . load < Texture2D > ( { url : "https://gw.alipayobjects.com/mdn/rms_7c464e/afts/img/A*d3N9RYpcKncAAAAAAAAAAAAAARQnAQ" , type : AssetType . Texture2D } ) . then ( ( texture ) => { const spriteEntity = new Entity ( engine , "spritePivot" ) ; const { transform } = spriteEntity ; transform . setScale ( 3 , 3 , 3 ) ; transform . setPosition ( 0 , 5 , 0 ) ; spriteEntity . addComponent ( SpriteRenderer ) ; addPivotEntity ( spriteEntity , texture , 8 ) ; const pivotEntity = spriteEntity . clone ( ) ; pivotEntity . addComponent ( RotateScript ) ; addPivotEntity ( pivotEntity , texture , - 8 ) ; addDataGUI ( pivotEntity ) ; } ) ; engine . run ( ) ; /**
 * Add flip entity.
 */ function addPivotEntity ( entity , texture , posY ) { rootEntity . addChild ( entity ) ; entity . transform . setPosition ( 0 , posY , 0 ) ; entity . getComponent ( SpriteRenderer ) . sprite = new Sprite ( entity . engine , texture ) ; } /**
 * Add data GUI.
 */ function addDataGUI ( entity ) { const sprite = entity . getComponent ( SpriteRenderer ) . sprite ; const pivot = new Vector2 ( 0.5 , 0.5 ) ; const gui = new dat . GUI ( ) ; const guiData = { pivotX : 0.5 , pivotY : 0.5 , reset : ( ) => { guiData . pivotX = 0.5 ; guiData . pivotY = 0.5 ; pivot . setValue ( 0.5 , 0.5 ) ; sprite . pivot = pivot ; } } ; gui . add ( guiData , "pivotX" , 0.0 , 1.0 , 0.01 ) . onChange ( ( value ) => { pivot . x = value ; sprite . pivot = pivot ; } ) . listen ( ) ; gui . add ( guiData , "pivotY" , 0.0 , 1.0 , 0.01 ) . onChange ( ( value ) => { pivot . y = value ; sprite . pivot = pivot ; } ) . listen ( ) ; gui . add ( guiData , "reset" ) . name ( "重置" ) ; return guiData ; } class RotateScript extends Script { onUpdate ( dt ) { this . entity . transform . rotate ( 0 , 0 , 1 ) ; } }