Created
March 6, 2023 22:53
-
-
Save deltakosh/28193d841b1cf06533e27104ffef36ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Capture the first pointer down | |
// And if we start on the potato we block camera control | |
var potatoHasControl; | |
scene.onPointerDown = (evt, pickResult) => { | |
if (!pickResult.hit) { | |
return; | |
} | |
if (pickResult.pickedMesh !== potato) { | |
return; | |
} | |
potatoHasControl = true; | |
scene.activeCamera.detachControl(); | |
} | |
// Restore camera control | |
scene.onPointerUp = () => { | |
if (!potatoHasControl) { | |
return; | |
} | |
potatoHasControl = false; | |
scene.activeCamera.attachControl(); | |
} | |
// Get the pointer move and act accordingly | |
scene.onPointerMove = (evt, pickResult) => { | |
if (!potatoHasControl) { | |
return; | |
} | |
if (!pickResult || !pickResult.hit) { | |
return; | |
} | |
if (pickResult.pickedMesh !== potato) { | |
return; | |
} | |
// UV of the picked point | |
var uv = pickResult.getTextureCoordinates(); | |
var size = heightmap.getSize(); | |
var centerX = Math.max(Math.round(uv.x * (size.width - 1)), 0); | |
var centerY = Math.max(Math.round((1 - uv.y) * (size.height - 1)), 0); | |
var radius = 20; | |
context.beginPath(); | |
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); | |
context.fillStyle = evt.shiftKey ? 'black' : 'white'; | |
context.fill(); | |
heightmap.update(true); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment